dbjson : store the _id in key too (will be quicker when using indexes)

git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@2584 bf3deb0d-5f1a-0410-827f-c0cc1f45334c
This commit is contained in:
2016-02-28 19:25:01 +00:00
parent 09c859ee98
commit 7fad53016a

View File

@@ -67,11 +67,12 @@ class dbjson
*/ */
public function insertOne ($collection, $document) public function insertOne ($collection, $document)
{ {
$uniqueKey = $this->uniqueKey ();
$this->lockEX (); $this->lockEX ();
$this->db = $this->readDB (); $this->db = $this->readDB ();
$this->db[$collection]["content"][] = array_merge (array ( $this->db[$collection]["content"][$uniqueKey] = array_merge (
"_id"=>$this->uniqueKey ()), array ("_id"=>$uniqueKey),
$document); $document);
$this->writeDB ($this->db); $this->writeDB ($this->db);
$this->lockUN (); $this->lockUN ();
return 1; return 1;
@@ -91,9 +92,10 @@ class dbjson
$this->db = $this->readDB (); $this->db = $this->readDB ();
foreach ($documents as $document) foreach ($documents as $document)
{ {
$this->db[$collection]["content"][] = array_merge (array ( $uniqueKey = $this->uniqueKey ();
"_id"=>$this->uniqueKey ()), $this->db[$collection]["content"][$uniqueKey] = array_merge (
$document); array ("_id"=>$uniqueKey),
$document);
} }
$this->writeDB ($this->db); $this->writeDB ($this->db);
$this->db = null; $this->db = null;
@@ -172,7 +174,22 @@ class dbjson
{ {
// Merge the new document with the old // Merge the new document with the old
$tmp = $this->db[$collection]["content"][$key]; $tmp = $this->db[$collection]["content"][$key];
$tmp = array_merge ($tmp, $document); // We need to merge the old and the new document.
// If there is an array in the document, it is overwrited
foreach ($document as $k=>$v)
{
if (is_array ($v))
{
if (isset ($tmp[$k]))
$tmp[$k] = array_merge ($tmp[$k], $v);
else
$tmp[$k] = $v;
}
else
{
$tmp[$k] = $v;
}
}
$this->db[$collection]["content"][$key] = $tmp; $this->db[$collection]["content"][$key] = $tmp;
// Remove the needed unset fields // Remove the needed unset fields
foreach ($unset as $field) foreach ($unset as $field)
@@ -298,7 +315,8 @@ class dbjson
$fvals[1] !== "<" && $fvals[1] !== "<" &&
$fvals[1] !== ">" && $fvals[1] !== ">" &&
$fvals[1] !== "exists" && $fvals[1] !== "exists" &&
$fvals[1] !== "not exists") $fvals[1] !== "not exists" &&
$fvals[1] !== "in_array")
throw new \Exception ("Invalid filter operator provided", 500); throw new \Exception ("Invalid filter operator provided", 500);
if ($fvals[1] === "==" && $document[$fkey] === $fvals[0]) if ($fvals[1] === "==" && $document[$fkey] === $fvals[0])
$matchFilter = true; $matchFilter = true;
@@ -316,6 +334,9 @@ class dbjson
elseif (strtolower ($fvals[1]) === "not exists" && elseif (strtolower ($fvals[1]) === "not exists" &&
! array_key_exists ($fkey, $document)) ! array_key_exists ($fkey, $document))
$matchFilter = true; $matchFilter = true;
elseif (strtolower ($fvals[1]) === "in_array" &&
in_array ($fvals[0], $document[$fkey]))
$matchFilter = true;
else else
{ {
$matchFilter = false; $matchFilter = false;