Skip to content

Commit e3fd862

Browse files
author
Jan Steemann
committed
added support for sparse indexes
1 parent 21350b9 commit e3fd862

File tree

2 files changed

+93
-10
lines changed

2 files changed

+93
-10
lines changed

lib/triagens/ArangoDb/CollectionHandler.php

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,11 @@ class CollectionHandler extends
168168
* skiplist index option
169169
*/
170170
const OPTION_SKIPLIST_INDEX = 'skiplist';
171+
172+
/**
173+
* sparse index option
174+
*/
175+
const OPTION_SPARSE = 'sparse';
171176

172177
/**
173178
* count option
@@ -542,7 +547,7 @@ public function getRevision($collectionId)
542547
* @param string $collectionId - the collection id
543548
* @param int $size - the size of the cap constraint
544549
*
545-
* @link http://www.arangodb.com/manuals/current/IndexCapHttp.html
550+
* @link https://docs.arangodb.com/HttpIndexes/Cap.html
546551
*
547552
* @return array - server response of the created index
548553
*/
@@ -564,7 +569,7 @@ public function createCapConstraint($collectionId, $size)
564569
* @param boolean $constraint - whether this is a constraint or not
565570
* @param boolean $ignoreNull - whether to ignore null
566571
*
567-
* @link http://www.arangodb.com/manuals/current/IndexGeoHttp.html
572+
* @link https://docs.arangodb.com/HttpIndexes/Geo.html
568573
*
569574
* @return array - server response of the created index
570575
*/
@@ -598,18 +603,22 @@ public function createGeoIndex(
598603
* @param string $collectionId - the collection id
599604
* @param array $fields - an array of fields
600605
* @param boolean $unique - whether the values in the index should be unique or not
606+
* @param boolean $sparse - whether the index should be sparse
601607
*
602-
* @link http://www.arangodb.com/manuals/current/IndexHashHttp.html
608+
* @link https://docs.arangodb.com/HttpIndexes/Hash.html
603609
*
604610
* @return array - server response of the created index
605611
*/
606-
public function createHashIndex($collectionId, array $fields, $unique = null)
612+
public function createHashIndex($collectionId, array $fields, $unique = null, $sparse = null)
607613
{
608614
$indexOptions = array();
609615

610616
if ($unique) {
611617
$indexOptions[self::OPTION_UNIQUE] = (bool) $unique;
612618
}
619+
if ($sparse) {
620+
$indexOptions[self::OPTION_SPARSE] = (bool) $sparse;
621+
}
613622

614623
return $this->index($collectionId, self::OPTION_HASH_INDEX, $fields, null, $indexOptions);
615624
}
@@ -621,7 +630,7 @@ public function createHashIndex($collectionId, array $fields, $unique = null)
621630
* @param array $fields - an array of fields
622631
* @param int $minLength - the minimum length of words to index
623632
*
624-
* @link http://www.arangodb.com/manuals/current/IndexFulltextHttp.html
633+
* @link https://docs.arangodb.com/HttpIndexes/Fulltext.html
625634
*
626635
* @return array - server response of the created index
627636
*/
@@ -642,18 +651,22 @@ public function createFulltextIndex($collectionId, array $fields, $minLength = n
642651
* @param string $collectionId - the collection id
643652
* @param array $fields - an array of fields
644653
* @param bool $unique - whether the index is unique or not
654+
* @param bool $sparse - whether the index should be sparse
645655
*
646-
* @link http://www.arangodb.com/manuals/current/IndexSkiplistHttp.html
656+
* @link https://docs.arangodb.com/HttpIndexes/Skiplist.html
647657
*
648658
* @return array - server response of the created index
649659
*/
650-
public function createSkipListIndex($collectionId, array $fields, $unique = null)
660+
public function createSkipListIndex($collectionId, array $fields, $unique = null, $sparse = null)
651661
{
652662
$indexOptions = array();
653663

654664
if ($unique) {
655665
$indexOptions[self::OPTION_UNIQUE] = (bool) $unique;
656666
}
667+
if ($sparse) {
668+
$indexOptions[self::OPTION_SPARSE] = (bool) $sparse;
669+
}
657670

658671
return $this->index($collectionId, self::OPTION_SKIPLIST_INDEX, $fields, null, $indexOptions);
659672
}
@@ -671,7 +684,7 @@ public function createSkipListIndex($collectionId, array $fields, $unique = null
671684
* @param string $type - index type: hash, skiplist or geo
672685
* @param array $attributes - an array of attributes that can be defined like array('a') or array('a', 'b.c')
673686
* @param bool $unique - true/false to create a unique index
674-
* @param array $indexOptions - an associative array of options for the index like array('geoJson' => true)
687+
* @param array $indexOptions - an associative array of options for the index like array('geoJson' => true, 'sparse' => false)
675688
*
676689
* @return array - server response of the created index
677690
*/

tests/CollectionBasicTest.php

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -566,7 +566,43 @@ public function testCreateHashIndex()
566566
$this->assertCount(2, $indexInfo['fields'], "There should only be 2 indexed fields");
567567
$this->assertEquals("hashfield1", $indexInfo['fields'][0], "The first indexed field is not 'hashfield1'");
568568
$this->assertEquals("hashfield2", $indexInfo['fields'][1], "The second indexed field is not 'hashfield2'");
569-
$this->assertEquals(true, $indexInfo[CollectionHandler::OPTION_UNIQUE], 'unique was not set to true!');
569+
$this->assertTrue($indexInfo[CollectionHandler::OPTION_UNIQUE], 'unique was not set to true!');
570+
$this->assertFalse($indexInfo[CollectionHandler::OPTION_SPARSE], 'sparse flag was not set to false!');
571+
$this->assertTrue(isset($indexInfo['selectivityEstimate']), 'selectivity estimate not present!');
572+
}
573+
574+
575+
/**
576+
* Create a sparse hash index and verify it by getting information about the index from the server
577+
*/
578+
public function testCreateSparseHashIndex()
579+
{
580+
$result = $this->collectionHandler->createHashIndex(
581+
'ArangoDB_PHP_TestSuite_IndexTestCollection',
582+
array('hashfield1', 'hashfield2'),
583+
false,
584+
array('sparse' => true)
585+
);
586+
587+
$indices = $this->collectionHandler->getIndexes('ArangoDB_PHP_TestSuite_IndexTestCollection');
588+
589+
$indicesByIdentifiers = $indices['identifiers'];
590+
591+
$this->assertArrayHasKey($result['id'], $indicesByIdentifiers, 'Hash index was not created!');
592+
593+
$indexInfo = $indicesByIdentifiers[$result['id']];
594+
595+
$this->assertEquals(
596+
CollectionHandler::OPTION_HASH_INDEX,
597+
$indexInfo[CollectionHandler::OPTION_TYPE],
598+
"Index type is not 'hash'!"
599+
);
600+
$this->assertCount(2, $indexInfo['fields'], "There should only be 2 indexed fields");
601+
$this->assertEquals("hashfield1", $indexInfo['fields'][0], "The first indexed field is not 'hashfield1'");
602+
$this->assertEquals("hashfield2", $indexInfo['fields'][1], "The second indexed field is not 'hashfield2'");
603+
$this->assertFalse($indexInfo[CollectionHandler::OPTION_UNIQUE], 'unique was not set to false!');
604+
$this->assertTrue($indexInfo[CollectionHandler::OPTION_SPARSE], 'sparse flag was not set to true!');
605+
$this->assertTrue(isset($indexInfo['selectivityEstimate']), 'selectivity estimate not present!');
570606
}
571607

572608

@@ -627,7 +663,41 @@ public function testCreateSkipListIndex()
627663
$this->assertCount(2, $indexInfo['fields'], "There should only be 2 indexed field");
628664
$this->assertEquals("skiplistfield1", $indexInfo['fields'][0], "The indexed field is not 'skiplistfield1'");
629665
$this->assertEquals("skiplistfield2", $indexInfo['fields'][1], "The indexed field is not 'skiplistfield2'");
630-
$this->assertEquals(true, $indexInfo[CollectionHandler::OPTION_UNIQUE], 'unique was not set to true!');
666+
$this->assertTrue($indexInfo[CollectionHandler::OPTION_UNIQUE], 'unique was not set to true!');
667+
$this->assertFalse($indexInfo[CollectionHandler::OPTION_SPARSE], 'sparse flag was not set to false!');
668+
}
669+
670+
671+
/**
672+
* Create a sparse skiplist index and verify it by getting information about the index from the server
673+
*/
674+
public function testCreateSparseSkipListIndex()
675+
{
676+
$result = $this->collectionHandler->createSkipListIndex(
677+
'ArangoDB_PHP_TestSuite_IndexTestCollection',
678+
array('skiplistfield1', 'skiplistfield2'),
679+
false,
680+
array('sparse' => true)
681+
);
682+
683+
$indices = $this->collectionHandler->getIndexes('ArangoDB_PHP_TestSuite_IndexTestCollection');
684+
685+
$indicesByIdentifiers = $indices['identifiers'];
686+
687+
$this->assertArrayHasKey($result['id'], $indicesByIdentifiers, 'skip-list index was not created!');
688+
689+
$indexInfo = $indicesByIdentifiers[$result['id']];
690+
691+
$this->assertEquals(
692+
CollectionHandler::OPTION_SKIPLIST_INDEX,
693+
$indexInfo[CollectionHandler::OPTION_TYPE],
694+
"Index type is not 'skip-list'!"
695+
);
696+
$this->assertCount(2, $indexInfo['fields'], "There should only be 2 indexed field");
697+
$this->assertEquals("skiplistfield1", $indexInfo['fields'][0], "The indexed field is not 'skiplistfield1'");
698+
$this->assertEquals("skiplistfield2", $indexInfo['fields'][1], "The indexed field is not 'skiplistfield2'");
699+
$this->assertFalse($indexInfo[CollectionHandler::OPTION_UNIQUE], 'unique was not set to false!');
700+
$this->assertTrue($indexInfo[CollectionHandler::OPTION_SPARSE], 'sparse flag was not set to true!');
631701
}
632702

633703

0 commit comments

Comments
 (0)