Compare commits

...

1 Commits

Author SHA1 Message Date
Dendy fa28ee472a Move Item loading to MediaDB, add $key => to map 2023-07-11 03:04:51 +02:00
2 changed files with 45 additions and 28 deletions

View File

@ -20,25 +20,14 @@ class Item implements JsonSerializable
public static function load(MediaDB $db, string $id): ?Item public static function load(MediaDB $db, string $id): ?Item
{ {
$ret = null; return $db->loadItem($id);
$db->map(function ($line) use (&$ret, $id) {
if ($line[0] == $id) {
$ret = Item::fromLine($line);
return true;
} }
return false; public static function fromLine(string $line): Item
});
return $ret;
}
public static function fromLine(array $line): Item
{ {
$ret = new Item(); $ret = new Item();
$line = explode(' ', $line);
$ret->hash = array_shift($line); $ret->hash = array_shift($line);
$ret->tags = $line; $ret->tags = $line;
$ret->extensionFromTags(); $ret->extensionFromTags();

View File

@ -2,7 +2,7 @@
class MediaDB class MediaDB
{ {
private $handler; private $handle;
private array $lines = []; private array $lines = [];
// TODO: create a persist() method that updates the line if the offset where // TODO: create a persist() method that updates the line if the offset where
@ -13,21 +13,17 @@ class MediaDB
public function __construct() public function __construct()
{ {
// Just initialize the file handler // Just initialize the file handler
$this->handler = fopen($GLOBALS['path_mediadb'], 'r'); $this->handle = fopen($GLOBALS['path_mediadb'], 'r');
if ($this->handler === false) if ($this->handle === false)
Json::error('Error opening media DB'); Json::error('Error opening media DB');
} }
private function getLine(): ?array private function getLine(): ?string
{ {
$ret = fgetcsv($this->handler, 0, ' '); return feof($this->handle) ? null : fgets($this->handle);
if ($ret === false)
return null;
return $ret;
} }
public static function add(array|Item $item) public static function add(Item $item)
{ {
$file = fopen($GLOBALS['path_mediadb'], 'a+'); $file = fopen($GLOBALS['path_mediadb'], 'a+');
if ($file === false) if ($file === false)
@ -42,12 +38,11 @@ class MediaDB
// The $this->list will be updated on trying to find new items // The $this->list will be updated on trying to find new items
} }
public function map(callable $func) public function map(callable $func)
{ {
// First read from what's already in memory // First read from what's already in memory
foreach ($this->lines as $i_line) { foreach ($this->lines as $key => $i_line) {
if ($func($i_line) === true) return; if ($func($i_line, $key) === true) return;
} }
// If we run out, read from the file and append to array so the next // If we run out, read from the file and append to array so the next
@ -55,7 +50,40 @@ class MediaDB
while ($line = $this->getLine()) { while ($line = $this->getLine()) {
$this->lines[] = $line; $this->lines[] = $line;
if ($func($line) === true) return; // NOTE: We are kinda assuming that the keys are autoincrementing
// unsigned integers... I think it should be fine, but o_0
$key = count($this->lines) - 1;
if ($func($line, $key) === true) return;
} }
} }
// ---- ITEM OPERATIONS ----
public function loadItem(string $id): ?Item
{
$key = $this->searchItem($id);
if ($key === null) return null;
return Item::fromLine($this->lines[$key]);
}
private function searchItem(string $id): ?int
{
$ret = null;
$this->map(function ($line, $key) use (&$ret, $id) {
$item = Item::fromLine($line);
if ($item->getHash() == $id) {
$ret = $key;
return true;
}
return false;
});
return $ret;
}
} }