Compare commits

..

No commits in common. "main" and "e26c0604803ba26e27332a62d16e856cad1ed185" have entirely different histories.

2 changed files with 28 additions and 45 deletions

View File

@ -20,14 +20,25 @@ class Item implements JsonSerializable
public static function load(MediaDB $db, string $id): ?Item public static function load(MediaDB $db, string $id): ?Item
{ {
return $db->loadItem($id); $ret = null;
$db->map(function ($line) use (&$ret, $id) {
if ($line[0] == $id) {
$ret = Item::fromLine($line);
return true;
} }
public static function fromLine(string $line): Item return false;
});
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 $handle; private $handler;
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,17 +13,21 @@ class MediaDB
public function __construct() public function __construct()
{ {
// Just initialize the file handler // Just initialize the file handler
$this->handle = fopen($GLOBALS['path_mediadb'], 'r'); $this->handler = fopen($GLOBALS['path_mediadb'], 'r');
if ($this->handle === false) if ($this->handler === false)
Json::error('Error opening media DB'); Json::error('Error opening media DB');
} }
private function getLine(): ?string private function getLine(): ?array
{ {
return feof($this->handle) ? null : fgets($this->handle); $ret = fgetcsv($this->handler, 0, ' ');
if ($ret === false)
return null;
return $ret;
} }
public static function add(Item $item) public static function add(array|Item $item)
{ {
$file = fopen($GLOBALS['path_mediadb'], 'a+'); $file = fopen($GLOBALS['path_mediadb'], 'a+');
if ($file === false) if ($file === false)
@ -38,11 +42,12 @@ 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 $key => $i_line) { foreach ($this->lines as $i_line) {
if ($func($i_line, $key) === true) return; if ($func($i_line) === 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
@ -50,40 +55,7 @@ class MediaDB
while ($line = $this->getLine()) { while ($line = $this->getLine()) {
$this->lines[] = $line; $this->lines[] = $line;
// NOTE: We are kinda assuming that the keys are autoincrementing if ($func($line) === true) return;
// 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;
}
} }