Skip to content

Commit 0801ad8

Browse files
committed
added support for persistent indexes
1 parent 330e562 commit 0801ad8

File tree

2 files changed

+102
-1
lines changed

2 files changed

+102
-1
lines changed

lib/triagens/ArangoDb/CollectionHandler.php

Lines changed: 33 additions & 1 deletion
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+
* persistent index option
174+
*/
175+
const OPTION_PERSISTENT_INDEX = 'persistent';
171176

172177
/**
173178
* sparse index option
@@ -907,6 +912,33 @@ public function createSkipListIndex($collectionId, array $fields, $unique = null
907912

908913
return $this->index($collectionId, self::OPTION_SKIPLIST_INDEX, $fields, null, $indexOptions);
909914
}
915+
916+
/**
917+
* Create a persistent index
918+
*
919+
* @param string $collectionId - the collection id
920+
* @param array $fields - an array of fields
921+
* @param bool $unique - whether the index is unique or not
922+
* @param bool $sparse - whether the index should be sparse
923+
*
924+
* @link https://docs.arangodb.com/HttpIndexes/Skiplist.html
925+
*
926+
* @return array - server response of the created index
927+
* @throws \triagens\ArangoDb\Exception
928+
*/
929+
public function createPersistentIndex($collectionId, array $fields, $unique = null, $sparse = null)
930+
{
931+
$indexOptions = [];
932+
933+
if ($unique) {
934+
$indexOptions[self::OPTION_UNIQUE] = (bool) $unique;
935+
}
936+
if ($sparse) {
937+
$indexOptions[self::OPTION_SPARSE] = (bool) $sparse;
938+
}
939+
940+
return $this->index($collectionId, self::OPTION_PERSISTENT_INDEX, $fields, null, $indexOptions);
941+
}
910942

911943
/**
912944
* Create a geo index
@@ -957,7 +989,7 @@ public function createGeoIndex(
957989
* @throws Exception
958990
*
959991
* @param mixed $collectionId - The id of the collection where the index is to be created
960-
* @param string $type - index type: hash, skiplist or geo
992+
* @param string $type - index type: hash, skiplist, geo, fulltext, or persistent
961993
* @param array $attributes - an array of attributes that can be defined like array('a') or array('a', 'b.c')
962994
* @param bool $unique - true/false to create a unique index
963995
* @param array $indexOptions - an associative array of options for the index like array('geoJson' => true, 'sparse' => false)

tests/CollectionBasicTest.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -692,6 +692,75 @@ public function testCreateSparseSkipListIndex()
692692
static::assertTrue($indexInfo[CollectionHandler::OPTION_SPARSE], 'sparse flag was not set to true!');
693693
}
694694
}
695+
696+
697+
/**
698+
* Create a persistent index and verify it by getting information about the index from the server
699+
*/
700+
public function testCreatePersistentIndex()
701+
{
702+
$result = $this->collectionHandler->createPersistentIndex(
703+
'ArangoDB_PHP_TestSuite_IndexTestCollection',
704+
['field1', 'field2'],
705+
true
706+
);
707+
708+
$indices = $this->collectionHandler->getIndexes('ArangoDB_PHP_TestSuite_IndexTestCollection');
709+
710+
$indicesByIdentifiers = $indices['identifiers'];
711+
712+
static::assertArrayHasKey($result['id'], $indicesByIdentifiers, 'persistent index was not created!');
713+
714+
$indexInfo = $indicesByIdentifiers[$result['id']];
715+
716+
static::assertEquals(
717+
CollectionHandler::OPTION_PERSISTENT_INDEX,
718+
$indexInfo[CollectionHandler::OPTION_TYPE],
719+
"Index type is not 'persistent'!"
720+
);
721+
static::assertCount(2, $indexInfo['fields'], 'There should only be 2 indexed fields');
722+
static::assertEquals('field1', $indexInfo['fields'][0], "The indexed field is not 'field1'");
723+
static::assertEquals('field2', $indexInfo['fields'][1], "The indexed field is not 'field2'");
724+
static::assertTrue($indexInfo[CollectionHandler::OPTION_UNIQUE], 'unique was not set to true!');
725+
if ($this->hasSparseIndexes) {
726+
static::assertFalse($indexInfo[CollectionHandler::OPTION_SPARSE], 'sparse flag was not set to false!');
727+
}
728+
}
729+
730+
731+
/**
732+
* Create a sparse persistent index and verify it by getting information about the index from the server
733+
*/
734+
public function testCreateSparsePersistentIndex()
735+
{
736+
$result = $this->collectionHandler->createPersistentIndex(
737+
'ArangoDB_PHP_TestSuite_IndexTestCollection',
738+
['field1', 'field2'],
739+
false,
740+
['sparse' => true]
741+
);
742+
743+
$indices = $this->collectionHandler->getIndexes('ArangoDB_PHP_TestSuite_IndexTestCollection');
744+
745+
$indicesByIdentifiers = $indices['identifiers'];
746+
747+
static::assertArrayHasKey($result['id'], $indicesByIdentifiers, 'persistent index was not created!');
748+
749+
$indexInfo = $indicesByIdentifiers[$result['id']];
750+
751+
static::assertEquals(
752+
CollectionHandler::OPTION_PERSISTENT_INDEX,
753+
$indexInfo[CollectionHandler::OPTION_TYPE],
754+
"Index type is not 'persistent'!"
755+
);
756+
static::assertCount(2, $indexInfo['fields'], 'There should only be 2 indexed fields');
757+
static::assertEquals('field1', $indexInfo['fields'][0], "The indexed field is not 'field1'");
758+
static::assertEquals('field2', $indexInfo['fields'][1], "The indexed field is not 'field2'");
759+
static::assertFalse($indexInfo[CollectionHandler::OPTION_UNIQUE], 'unique was not set to false!');
760+
if ($this->hasSparseIndexes) {
761+
static::assertTrue($indexInfo[CollectionHandler::OPTION_SPARSE], 'sparse flag was not set to true!');
762+
}
763+
}
695764

696765

697766
/**

0 commit comments

Comments
 (0)