dbjson : the keys are the _id (no more N² searches)

git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@2649 bf3deb0d-5f1a-0410-827f-c0cc1f45334c
This commit is contained in:
2016-03-05 16:10:40 +00:00
parent 29007c5afa
commit 089707e88f
2 changed files with 64 additions and 56 deletions

View File

@@ -48,14 +48,14 @@ class test_dbjson extends PHPUnit_Framework_TestCase
// Return all the keys (filter = array ()) // Return all the keys (filter = array ())
$dbjson = new dbjson ("dbjson://".dbfile); $dbjson = new dbjson ("dbjson://".dbfile);
$res = $dbjson->filter ("collection", array ()); $res = $dbjson->filter ("collection", array ());
$this->assertSame ($res, array (0,1,2,3)); $this->assertSame (array_keys ($res), array (0,1,2,3));
} }
public function test_filter2 () public function test_filter2 ()
{ {
// Return the keys where filter = array ("key2"=>"val2")) // Return the keys where filter = array ("key2"=>"val2"))
$dbjson = new dbjson ("dbjson://".dbfile); $dbjson = new dbjson ("dbjson://".dbfile);
$res = $dbjson->filter ("collection", array ("key2"=>"val2")); $res = $dbjson->filter ("collection", array ("key2"=>"val2"));
$this->assertSame ($res, array (0,1,2)); $this->assertSame (array_keys ($res), array (0,1,2));
} }
public function test_filter3 () public function test_filter3 ()
{ {
@@ -63,7 +63,7 @@ class test_dbjson extends PHPUnit_Framework_TestCase
$dbjson = new dbjson ("dbjson://".dbfile); $dbjson = new dbjson ("dbjson://".dbfile);
$res = $dbjson->filter ("collection", array ("key1"=>"val3", $res = $dbjson->filter ("collection", array ("key1"=>"val3",
"key2"=>"val2")); "key2"=>"val2"));
$this->assertSame ($res, array (2)); $this->assertSame (count ($res), 1);
} }
public function test_filter4 () public function test_filter4 ()
{ {
@@ -72,7 +72,7 @@ class test_dbjson extends PHPUnit_Framework_TestCase
$dbjson = new dbjson ("dbjson://".dbfile); $dbjson = new dbjson ("dbjson://".dbfile);
$res = $dbjson->filter ("collection",array ("key1"=>array ("val3", "=="), $res = $dbjson->filter ("collection",array ("key1"=>array ("val3", "=="),
"key2"=>array ("val2", "=="))); "key2"=>array ("val2", "==")));
$this->assertSame ($res, array (2)); $this->assertSame (count ($res), 1);
} }
public function test_filter5 () public function test_filter5 ()
{ {
@@ -81,7 +81,7 @@ class test_dbjson extends PHPUnit_Framework_TestCase
$dbjson = new dbjson ("dbjson://".dbfile); $dbjson = new dbjson ("dbjson://".dbfile);
$res = $dbjson->filter ("collection", array ("key1"=>array ("val3", "<="), $res = $dbjson->filter ("collection", array ("key1"=>array ("val3", "<="),
"key2"=>array ("val2", "=="))); "key2"=>array ("val2", "==")));
$this->assertSame ($res, array (0,1,2)); $this->assertSame (array_keys ($res), array (0,1,2));
} }
public function test_filter6 () public function test_filter6 ()
{ {
@@ -90,7 +90,7 @@ class test_dbjson extends PHPUnit_Framework_TestCase
$dbjson = new dbjson ("dbjson://".dbfile); $dbjson = new dbjson ("dbjson://".dbfile);
$res = $dbjson->filter ("collection", array ("key1"=>array ("val3", "<="), $res = $dbjson->filter ("collection", array ("key1"=>array ("val3", "<="),
"key2"=>array ("val2", ">"))); "key2"=>array ("val2", ">")));
$this->assertSame ($res, array (3)); $this->assertSame (count ($res), 1);
} }
public function test_find1 () public function test_find1 ()
@@ -98,10 +98,11 @@ class test_dbjson extends PHPUnit_Framework_TestCase
$dbjson = new dbjson ("dbjson://".dbfile); $dbjson = new dbjson ("dbjson://".dbfile);
$res = $dbjson->find ("collection", array ("key1"=>array ("val3", "<="), $res = $dbjson->find ("collection", array ("key1"=>array ("val3", "<="),
"key2"=>array ("val2", ">"))); "key2"=>array ("val2", ">")));
$res = array_values ($res);
// ["_id"] is random : skip the test // ["_id"] is random : skip the test
unset ($res[3]["_id"]); unset ($res[0]["_id"]);
$this->assertSame ($res, array (3=>array ("key1"=>"val3", $this->assertSame ($res, array (0=>array ("key1"=>"val3",
"key2"=>"val4"))); "key2"=>"val4")));
} }
public function test_find2 () public function test_find2 ()
{ {
@@ -109,10 +110,11 @@ class test_dbjson extends PHPUnit_Framework_TestCase
$res = $dbjson->find ("collection", array ("key1"=>array ("val3", "<="), $res = $dbjson->find ("collection", array ("key1"=>array ("val3", "<="),
"key2"=>array ("val2", ">")), "key2"=>array ("val2", ">")),
"*"); "*");
$res = array_values ($res);
// ["_id"] is random : skip the test // ["_id"] is random : skip the test
unset ($res[3]["_id"]); unset ($res[0]["_id"]);
$this->assertSame ($res, array (3=>array ("key1"=>"val3", $this->assertSame ($res, array (0=>array ("key1"=>"val3",
"key2"=>"val4"))); "key2"=>"val4")));
} }
public function test_find3 () public function test_find3 ()
{ {
@@ -130,8 +132,9 @@ class test_dbjson extends PHPUnit_Framework_TestCase
"key2"=>array ("val2", ">")), "key2"=>array ("val2", ">")),
array ("key1")); array ("key1"));
// ["_id"] is random : skip the test // ["_id"] is random : skip the test
unset ($res[3]["_id"]); $res = array_values ($res);
$this->assertSame ($res, array (3=>array ("key1"=>"val3"))); unset ($res[0]["_id"]);
$this->assertSame ($res, array (0=>array ("key1"=>"val3")));
} }
public function test_find5 () public function test_find5 ()
{ {
@@ -139,10 +142,11 @@ class test_dbjson extends PHPUnit_Framework_TestCase
$res = $dbjson->find ("collection", array ("key1"=>array ("val3", "<="), $res = $dbjson->find ("collection", array ("key1"=>array ("val3", "<="),
"key2"=>array ("val2", ">")), "key2"=>array ("val2", ">")),
array ("key1", "key2")); array ("key1", "key2"));
$res = array_values ($res);
// ["_id"] is random : skip the test // ["_id"] is random : skip the test
unset ($res[3]["_id"]); unset ($res[0]["_id"]);
$this->assertSame ($res, array (3=>array ("key1"=>"val3", $this->assertSame ($res, array (0=>array ("key1"=>"val3",
"key2"=>"val4"))); "key2"=>"val4")));
} }
public function test_find6 () public function test_find6 ()
{ {
@@ -150,6 +154,7 @@ class test_dbjson extends PHPUnit_Framework_TestCase
$res = $dbjson->find ("collection", array ("key1"=>array ("val3", "<="), $res = $dbjson->find ("collection", array ("key1"=>array ("val3", "<="),
"key2"=>array ("val2", "==")), "key2"=>array ("val2", "==")),
array ("key1", "key2")); array ("key1", "key2"));
$res = array_values ($res);
// ["_id"] is random : skip the test // ["_id"] is random : skip the test
unset ($res[0]["_id"]); unset ($res[0]["_id"]);
unset ($res[1]["_id"]); unset ($res[1]["_id"]);
@@ -167,13 +172,14 @@ class test_dbjson extends PHPUnit_Framework_TestCase
$res = $dbjson->find ("collection", array ("key1"=>array ("val3", "<="), $res = $dbjson->find ("collection", array ("key1"=>array ("val3", "<="),
"key2"=>array ("val2", "==")), "key2"=>array ("val2", "==")),
array ("key2")); array ("key2"));
$res = array_values ($res);
// ["_id"] is random : skip the test // ["_id"] is random : skip the test
unset ($res[0]["_id"]); unset ($res[0]["_id"]);
unset ($res[1]["_id"]); unset ($res[1]["_id"]);
unset ($res[2]["_id"]); unset ($res[2]["_id"]);
$this->assertSame ($res, array (0=>array ("key2"=>"val2"), $this->assertSame ($res, array (0=>array ("key2"=>"val2"),
1=>array ("key2"=>"val2"), 1=>array ("key2"=>"val2"),
2=>array ("key2"=>"val2"))); 2=>array ("key2"=>"val2")));
} }
public function test_find8 () public function test_find8 ()
{ {
@@ -181,6 +187,7 @@ class test_dbjson extends PHPUnit_Framework_TestCase
$res = $dbjson->find ("collection", array ("key1"=>array ("val3", "<="), $res = $dbjson->find ("collection", array ("key1"=>array ("val3", "<="),
"key2"=>array ("val2", "==")), "key2"=>array ("val2", "==")),
array ("key2"), 1); array ("key2"), 1);
$res = array_values ($res);
// ["_id"] is random : skip the test // ["_id"] is random : skip the test
unset ($res[0]["_id"]); unset ($res[0]["_id"]);
$this->assertSame ($res, array (0=>array ("key2"=>"val2"))); $this->assertSame ($res, array (0=>array ("key2"=>"val2")));
@@ -200,11 +207,12 @@ class test_dbjson extends PHPUnit_Framework_TestCase
$res = $dbjson->find ("collection", array ("key1"=>array ("val3", "<="), $res = $dbjson->find ("collection", array ("key1"=>array ("val3", "<="),
"key2"=>array ("val2", "==")), "key2"=>array ("val2", "==")),
array ("key2")); array ("key2"));
$res = array_values ($res);
// ["_id"] is random : skip the test // ["_id"] is random : skip the test
unset ($res[0]["_id"]);
unset ($res[1]["_id"]); unset ($res[1]["_id"]);
unset ($res[2]["_id"]); $this->assertSame ($res, array (0=>array ("key2"=>"val2"),
$this->assertSame ($res, array (1=>array ("key2"=>"val2"), 1=>array ("key2"=>"val2")));
2=>array ("key2"=>"val2")));
} }
public function test_deleteMany1 () public function test_deleteMany1 ()
@@ -230,10 +238,10 @@ class test_dbjson extends PHPUnit_Framework_TestCase
public function test_find11 () public function test_find11 ()
{ {
$dbjson = new dbjson ("dbjson://".dbfile); $dbjson = new dbjson ("dbjson://".dbfile);
$res = $dbjson->find ("collection"); $res = array_values ($dbjson->find ("collection"));
// ["_id"] is random : skip the test // ["_id"] is random : skip the test
unset ($res[3]["_id"]); unset ($res[0]["_id"]);
$this->assertSame ($res, array (3=>array ("key1"=>"val3", "key2"=>"val4"))); $this->assertSame ($res, array (0=>array ("key1"=>"val3", "key2"=>"val4")));
} }
public function test_replace1 () public function test_replace1 ()
@@ -245,10 +253,10 @@ class test_dbjson extends PHPUnit_Framework_TestCase
public function test_find12 () public function test_find12 ()
{ {
$dbjson = new dbjson ("dbjson://".dbfile); $dbjson = new dbjson ("dbjson://".dbfile);
$res = $dbjson->find ("collection"); $res = array_values ($dbjson->find ("collection"));
// ["_id"] is random : skip the test // ["_id"] is random : skip the test
unset ($res[3]["_id"]); unset ($res[0]["_id"]);
$this->assertSame ($res, array (3=>array ("key2"=>"val5"))); $this->assertSame ($res, array (0=>array ("key2"=>"val5")));
} }
public function test_update1 () public function test_update1 ()
@@ -261,10 +269,10 @@ class test_dbjson extends PHPUnit_Framework_TestCase
public function test_find13 () public function test_find13 ()
{ {
$dbjson = new dbjson ("dbjson://".dbfile); $dbjson = new dbjson ("dbjson://".dbfile);
$res = $dbjson->find ("collection"); $res = array_values ($dbjson->find ("collection"));
// ["_id"] is random : skip the test // ["_id"] is random : skip the test
unset ($res[3]["_id"]); unset ($res[0]["_id"]);
$this->assertSame ($res, array (3=>array ("key2"=>"val6", $this->assertSame ($res, array (0=>array ("key2"=>"val6",
"key5"=>"val5"))); "key5"=>"val5")));
} }
public function test_insertOne3 () public function test_insertOne3 ()
@@ -278,14 +286,14 @@ class test_dbjson extends PHPUnit_Framework_TestCase
public function test_find14 () public function test_find14 ()
{ {
$dbjson = new dbjson ("dbjson://".dbfile); $dbjson = new dbjson ("dbjson://".dbfile);
$res = $dbjson->find ("collection"); $res = array_values ($dbjson->find ("collection"));
// ["_id"] is random : skip the test // ["_id"] is random : skip the test
unset ($res[3]["_id"]); unset ($res[0]["_id"]);
unset ($res[4]["_id"]); unset ($res[1]["_id"]);
$this->assertSame ($res, array (3=>array ("key2"=>"val6", $this->assertSame ($res, array (0=>array ("key2"=>"val6",
"key5"=>"val5"), "key5"=>"val5"),
4=>array ("key1"=>"val1", 1=>array ("key1"=>"val1",
"key2"=>"val2"))); "key2"=>"val2")));
} }
public function test_update2 () public function test_update2 ()
@@ -298,13 +306,13 @@ class test_dbjson extends PHPUnit_Framework_TestCase
public function test_find15 () public function test_find15 ()
{ {
$dbjson = new dbjson ("dbjson://".dbfile); $dbjson = new dbjson ("dbjson://".dbfile);
$res = $dbjson->find ("collection"); $res = array_values ($dbjson->find ("collection"));
// ["_id"] is random : skip the test // ["_id"] is random : skip the test
unset ($res[3]["_id"]); unset ($res[0]["_id"]);
unset ($res[4]["_id"]); unset ($res[1]["_id"]);
$this->assertSame ($res, array (3=>array ("key2"=>"val7", $this->assertSame ($res, array (0=>array ("key2"=>"val7",
"key5"=>"val8"), "key5"=>"val8"),
4=>array ("key1"=>"val1", 1=>array ("key1"=>"val1",
"key2"=>"val7", "key2"=>"val7",
"key5"=>"val8"))); "key5"=>"val8")));
} }
@@ -321,12 +329,12 @@ class test_dbjson extends PHPUnit_Framework_TestCase
public function test_find16 () public function test_find16 ()
{ {
$dbjson = new dbjson ("dbjson://".dbfile); $dbjson = new dbjson ("dbjson://".dbfile);
$res = $dbjson->find ("collection"); $res = array_values ($dbjson->find ("collection"));
// ["_id"] is random : skip the test // ["_id"] is random : skip the test
unset ($res[3]["_id"]); unset ($res[0]["_id"]);
unset ($res[4]["_id"]); unset ($res[1]["_id"]);
$this->assertSame ($res, array (3=>array ("key5"=>"val7"), $this->assertSame ($res, array (0=>array ("key5"=>"val7"),
4=>array ("key1"=>"val1", 1=>array ("key1"=>"val1",
"key5"=>"val7"))); "key5"=>"val7")));
} }
@@ -337,15 +345,15 @@ class test_dbjson extends PHPUnit_Framework_TestCase
$dbjson2 = new dbjson ("dbjson://".dbfile); $dbjson2 = new dbjson ("dbjson://".dbfile);
$dbjson1->insertOne ("collection", $dbjson1->insertOne ("collection",
array ("key1"=>"val1", "key2"=>"val2")); array ("key1"=>"val1", "key2"=>"val2"));
$res = $dbjson2->find ("collection"); $res = array_values ($dbjson2->find ("collection"));
// ["_id"] is random : skip the test // ["_id"] is random : skip the test
unset ($res[3]["_id"]); unset ($res[0]["_id"]);
unset ($res[4]["_id"]); unset ($res[1]["_id"]);
unset ($res[5]["_id"]); unset ($res[2]["_id"]);
$this->assertSame ($res, array (3=>array ("key5"=>"val7"), $this->assertSame ($res, array (0=>array ("key5"=>"val7"),
4=>array ("key1"=>"val1", 1=>array ("key1"=>"val1",
"key5"=>"val7"), "key5"=>"val7"),
5=>array ("key1"=>"val1", 2=>array ("key1"=>"val1",
"key2"=>"val2"))); "key2"=>"val2")));
} }
} }

View File

@@ -243,7 +243,7 @@ class dbjson
if (count ($keys) === 0) if (count ($keys) === 0)
return 0; return 0;
reset ($keys); reset ($keys);
$key = key ($keys); $key = reset ($keys);
unset ($this->db[$collection]["content"][$key]); unset ($this->db[$collection]["content"][$key]);
$this->writeDB (); $this->writeDB ();
$this->lockUN (); $this->lockUN ();