mirror of
https://github.com/twisterarmy/twister-phpbot.git
synced 2025-03-12 05:21:35 +00:00
binary db (8x smaller)
This commit is contained in:
parent
528edd3bfd
commit
d957239a1b
@ -16,8 +16,8 @@ $twister = new TwisterPost('habr_ru'); // set user name
|
||||
$twister->twisterPath = '.' . DIRECTORY_SEPARATOR . 'twister-win32-bundle' . DIRECTORY_SEPARATOR;
|
||||
|
||||
// Initialise RSS database
|
||||
require_once 'habrrssdb.php';
|
||||
$db = new HabrRSSDb('habr_db.dat');
|
||||
require_once 'rssdb.php';
|
||||
$db = new RSSDb('habr_ru.dat');
|
||||
|
||||
foreach ($rss->channel->item as $item) {
|
||||
$link = (string)$item->link;
|
||||
|
@ -1,30 +0,0 @@
|
||||
<?php
|
||||
|
||||
class HabrRSSDb
|
||||
{
|
||||
protected $hDatabase = null;
|
||||
|
||||
public function __construct($dbFile)
|
||||
{
|
||||
fclose(fopen($dbFile, 'a')); // create file if not exists
|
||||
$this->hDatabase = fopen($dbFile, 'r+');
|
||||
}
|
||||
|
||||
public function __destruct()
|
||||
{
|
||||
fclose($this->hDatabase);
|
||||
}
|
||||
|
||||
public function isPublished($id)
|
||||
{
|
||||
fseek($this->hDatabase, $id);
|
||||
$status = fgetc($this->hDatabase);
|
||||
return $status === '1';
|
||||
}
|
||||
|
||||
public function setPublished($id)
|
||||
{
|
||||
fseek($this->hDatabase, $id);
|
||||
fwrite($this->hDatabase, '1');
|
||||
}
|
||||
}
|
41
rssdb.php
Normal file
41
rssdb.php
Normal file
@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
class RSSDb
|
||||
{
|
||||
protected $hDatabase = null;
|
||||
protected $minId = 0;
|
||||
protected $bits = array(0=>1, 2, 4, 8, 16, 32, 64, 128);
|
||||
|
||||
public function __construct($dbFile, $minId = 0)
|
||||
{
|
||||
fclose(fopen($dbFile, 'a')); // create file if not exists
|
||||
$this->hDatabase = fopen($dbFile, 'r+');
|
||||
$this->minId = $minId;
|
||||
}
|
||||
|
||||
public function __destruct()
|
||||
{
|
||||
fclose($this->hDatabase);
|
||||
}
|
||||
|
||||
public function isPublished($id)
|
||||
{
|
||||
$id -= $this->minId;
|
||||
if ($id < 0) return true;
|
||||
$pos = intval($id/8);
|
||||
fseek($this->hDatabase, $pos);
|
||||
$status = ord(fgetc($this->hDatabase));
|
||||
return (bool)($status & $this->bits[$id % 8]);
|
||||
}
|
||||
|
||||
public function setPublished($id)
|
||||
{
|
||||
$id -= $this->minId;
|
||||
if ($id < 0) return;
|
||||
$pos = intval($id/8);
|
||||
fseek($this->hDatabase, $pos);
|
||||
$status = ord(fgetc($this->hDatabase));
|
||||
fseek($this->hDatabase, $pos);
|
||||
fwrite($this->hDatabase, chr($status | $this->bits[$id % 8]));
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user