From 4b54760ac29282d5f684437b81849a6a0ab86b18 Mon Sep 17 00:00:00 2001 From: Katherine Walker Date: Tue, 3 Apr 2018 18:00:08 -0400 Subject: [PATCH] PHPLIB-80: Add IndexInfo helper methods for special index type options --- docs/reference/enumeration-classes.txt | 3 + .../MongoDBModelIndexInfo-is2dSphere.txt | 59 ++++++++++++++ .../MongoDBModelIndexInfo-isGeoHaystack.txt | 59 ++++++++++++++ .../method/MongoDBModelIndexInfo-isText.txt | 58 ++++++++++++++ src/Model/IndexInfo.php | 30 +++++++ tests/Model/IndexInfoFunctionalTest.php | 78 +++++++++++++++++++ tests/Model/IndexInfoTest.php | 75 ++++++++++++++++++ 7 files changed, 362 insertions(+) create mode 100644 docs/reference/method/MongoDBModelIndexInfo-is2dSphere.txt create mode 100644 docs/reference/method/MongoDBModelIndexInfo-isGeoHaystack.txt create mode 100644 docs/reference/method/MongoDBModelIndexInfo-isText.txt create mode 100644 tests/Model/IndexInfoFunctionalTest.php diff --git a/docs/reference/enumeration-classes.txt b/docs/reference/enumeration-classes.txt index 045091acb..6437fbb5e 100644 --- a/docs/reference/enumeration-classes.txt +++ b/docs/reference/enumeration-classes.txt @@ -134,7 +134,10 @@ Methods /reference/method/MongoDBModelIndexInfo-getName /reference/method/MongoDBModelIndexInfo-getNamespace /reference/method/MongoDBModelIndexInfo-getVersion + /reference/method/MongoDBModelIndexInfo-is2dSphere + /reference/method/MongoDBModelIndexInfo-isGeoHaystack /reference/method/MongoDBModelIndexInfo-isSparse + /reference/method/MongoDBModelIndexInfo-isText /reference/method/MongoDBModelIndexInfo-isTtl /reference/method/MongoDBModelIndexInfo-isUnique diff --git a/docs/reference/method/MongoDBModelIndexInfo-is2dSphere.txt b/docs/reference/method/MongoDBModelIndexInfo-is2dSphere.txt new file mode 100644 index 000000000..a6bb398be --- /dev/null +++ b/docs/reference/method/MongoDBModelIndexInfo-is2dSphere.txt @@ -0,0 +1,59 @@ +======================================= +MongoDB\\Model\\IndexInfo::is2dSphere() +======================================= + +.. versionadded:: 1.4 + +.. default-domain:: mongodb + +.. contents:: On this page + :local: + :backlinks: none + :depth: 1 + :class: singlecol + +Definition +---------- + +.. phpmethod:: MongoDB\\Model\\IndexInfo::is2dSphere() + + Return whether the index is a :manual:`2dsphere ` + index. + + .. code-block:: php + + function is2dSphere(): boolean + +Return Values +------------- + +A boolean indicating whether the index is a 2dsphere index. + +Examples +-------- + +.. code-block:: php + + selectCollection('test', 'places'); + + $collection->createIndex(['pos' => '2dsphere']); + + foreach ($collection->listIndexes() as $index) { + if ($index->is2dSphere()) { + printf("%s has 2dsphereIndexVersion: %d\n", $index->getName(), $index['2dsphereIndexVersion']); + } + } + +The output would then resemble:: + + pos_2dsphere has 2dsphereIndexVersion: 3 + +See Also +-------- + +- :phpmethod:`MongoDB\\Collection::createIndex()` +- :phpmethod:`MongoDB\\Collection::listIndexes()` +- :manual:`2dsphere Indexes ` reference in the MongoDB + manual diff --git a/docs/reference/method/MongoDBModelIndexInfo-isGeoHaystack.txt b/docs/reference/method/MongoDBModelIndexInfo-isGeoHaystack.txt new file mode 100644 index 000000000..a2d9a028b --- /dev/null +++ b/docs/reference/method/MongoDBModelIndexInfo-isGeoHaystack.txt @@ -0,0 +1,59 @@ +========================================== +MongoDB\\Model\\IndexInfo::isGeoHaystack() +========================================== + +.. versionadded:: 1.4 + +.. default-domain:: mongodb + +.. contents:: On this page + :local: + :backlinks: none + :depth: 1 + :class: singlecol + +Definition +---------- + +.. phpmethod:: MongoDB\\Model\\IndexInfo::isGeoHaystack() + + Return whether the index is a :manual:`geoHaystack + ` index. + + .. code-block:: php + + function isGeoHaystack(): boolean + +Return Values +------------- + +A boolean indicating whether the index is a geoHaystack index. + +Examples +-------- + +.. code-block:: php + + selectCollection('test', 'places'); + + $collection->createIndex(['pos' => 'geoHaystack', 'x' => 1], ['bucketSize' => 5]); + + foreach ($collection->listIndexes() as $index) { + if ($index->isGeoHaystack()) { + printf("%s has bucketSize: %d\n", $index->getName(), $index['bucketSize']); + } + } + +The output would then resemble:: + + pos_geoHaystack_x_1 has bucketSize: 5 + +See Also +-------- + +- :phpmethod:`MongoDB\\Collection::createIndex()` +- :phpmethod:`MongoDB\\Collection::listIndexes()` +- :manual:`geoHaystack Indexes ` reference in the MongoDB + manual diff --git a/docs/reference/method/MongoDBModelIndexInfo-isText.txt b/docs/reference/method/MongoDBModelIndexInfo-isText.txt new file mode 100644 index 000000000..083335968 --- /dev/null +++ b/docs/reference/method/MongoDBModelIndexInfo-isText.txt @@ -0,0 +1,58 @@ +=================================== +MongoDB\\Model\\IndexInfo::isText() +=================================== + +.. versionadded:: 1.4 + +.. default-domain:: mongodb + +.. contents:: On this page + :local: + :backlinks: none + :depth: 1 + :class: singlecol + +Definition +---------- + +.. phpmethod:: MongoDB\\Model\\IndexInfo::isText() + + Return whether the index is a :manual:`text ` index. + + .. code-block:: php + + function isText(): boolean + +Return Values +------------- + +A boolean indicating whether the index is a text index. + +Examples +-------- + +.. code-block:: php + + selectCollection('test', 'restaurants'); + + $collection->createIndex(['name' => 'text']); + + foreach ($collection->listIndexes() as $index) { + if ($index->isText()) { + printf("%s has default language: %d\n", $index->getName(), $index['default_language']); + } + } + +The output would then resemble:: + + name_text has default language: english + +See Also +-------- + +- :phpmethod:`MongoDB\\Collection::createIndex()` +- :phpmethod:`MongoDB\\Collection::listIndexes()` +- :manual:`Text Indexes ` reference in the MongoDB + manual diff --git a/src/Model/IndexInfo.php b/src/Model/IndexInfo.php index 7b660fe58..7491f34aa 100644 --- a/src/Model/IndexInfo.php +++ b/src/Model/IndexInfo.php @@ -110,6 +110,26 @@ public function getVersion() return (integer) $this->info['v']; } + /** + * Return whether or not this index is of type 2dsphere. + * + * @return boolean + */ + public function is2dSphere() + { + return array_search('2dsphere', $this->getKey(), true) !== false; + } + + /** + * Return whether or not this index is of type geoHaystack. + * + * @return boolean + */ + public function isGeoHaystack() + { + return array_search('geoHaystack', $this->getKey(), true) !== false; + } + /** * Return whether this is a sparse index. * @@ -121,6 +141,16 @@ public function isSparse() return ! empty($this->info['sparse']); } + /** + * Return whether or not this index is of type text. + * + * @return boolean + */ + public function isText() + { + return array_search('text', $this->getKey(), true) !== false; + } + /** * Return whether this is a TTL index. * diff --git a/tests/Model/IndexInfoFunctionalTest.php b/tests/Model/IndexInfoFunctionalTest.php new file mode 100644 index 000000000..c02617e81 --- /dev/null +++ b/tests/Model/IndexInfoFunctionalTest.php @@ -0,0 +1,78 @@ +collection = new Collection($this->manager, $this->getDatabaseName(), $this->getCollectionName()); + $this->collection->drop(); + } + + public function tearDown() + { + if ($this->hasFailed()) { + return; + } + + $this->collection->drop(); + } + + public function testIs2dSphere() + { + $indexName = $this->collection->createIndex(['pos' => '2dsphere']); + $result = $this->collection->listIndexes(); + + $result->rewind(); + $result->next(); + $index = $result->current(); + + $this->assertEquals($indexName, $index->getName()); + $this->assertTrue($index->is2dSphere()); + + $expectedVersion = version_compare($this->getServerVersion(), '3.2.0', '<') ? 2 : 3; + $this->assertEquals($expectedVersion, $index['2dsphereIndexVersion']); + } + + public function testIsGeoHaystack() + { + $indexName = $this->collection->createIndex(['pos' => 'geoHaystack', 'x' => 1], ['bucketSize' => 5]); + $result = $this->collection->listIndexes(); + + $result->rewind(); + $result->next(); + $index = $result->current(); + + $this->assertEquals($indexName, $index->getName()); + $this->assertTrue($index->isGeoHaystack()); + $this->assertEquals(5, $index['bucketSize']); + } + + public function testIsText() + { + $indexName = $this->collection->createIndex(['x' => 'text']); + $result = $this->collection->listIndexes(); + + $result->rewind(); + $result->next(); + $index = $result->current(); + + $this->assertEquals($indexName, $index->getName()); + $this->assertTrue($index->isText()); + $this->assertEquals('english', $index['default_language']); + $this->assertEquals('language', $index['language_override']); + + $expectedVersion = version_compare($this->getServerVersion(), '3.2.0', '<') ? 2 : 3; + $this->assertEquals($expectedVersion, $index['textIndexVersion']); + + $this->assertSameDocument(['x' => 1], $index['weights']); + } +} diff --git a/tests/Model/IndexInfoTest.php b/tests/Model/IndexInfoTest.php index e2d8a345b..d84e591bd 100644 --- a/tests/Model/IndexInfoTest.php +++ b/tests/Model/IndexInfoTest.php @@ -21,7 +21,10 @@ public function testBasicIndex() $this->assertSame(['x' => 1], $info->getKey()); $this->assertSame('x_1', $info->getName()); $this->assertSame('foo.bar', $info->getNamespace()); + $this->assertFalse($info->is2dSphere()); + $this->assertFalse($info->isGeoHaystack()); $this->assertFalse($info->isSparse()); + $this->assertFalse($info->isText()); $this->assertFalse($info->isTtl()); $this->assertFalse($info->isUnique()); } @@ -40,7 +43,10 @@ public function testSparseIndex() $this->assertSame(['y' => 1], $info->getKey()); $this->assertSame('y_sparse', $info->getName()); $this->assertSame('foo.bar', $info->getNamespace()); + $this->assertFalse($info->is2dSphere()); + $this->assertFalse($info->isGeoHaystack()); $this->assertTrue($info->isSparse()); + $this->assertFalse($info->isText()); $this->assertFalse($info->isTtl()); $this->assertFalse($info->isUnique()); } @@ -59,7 +65,10 @@ public function testUniqueIndex() $this->assertSame(['z' => 1], $info->getKey()); $this->assertSame('z_unique', $info->getName()); $this->assertSame('foo.bar', $info->getNamespace()); + $this->assertFalse($info->is2dSphere()); + $this->assertFalse($info->isGeoHaystack()); $this->assertFalse($info->isSparse()); + $this->assertFalse($info->isText()); $this->assertFalse($info->isTtl()); $this->assertTrue($info->isUnique()); } @@ -78,7 +87,10 @@ public function testTtlIndex() $this->assertSame(['z' => 1], $info->getKey()); $this->assertSame('z_unique', $info->getName()); $this->assertSame('foo.bar', $info->getNamespace()); + $this->assertFalse($info->is2dSphere()); + $this->assertFalse($info->isGeoHaystack()); $this->assertFalse($info->isSparse()); + $this->assertFalse($info->isText()); $this->assertTrue($info->isTtl()); $this->assertFalse($info->isUnique()); $this->assertArrayHasKey('expireAfterSeconds', $info); @@ -139,4 +151,67 @@ public function testOffsetUnsetCannotBeCalled() $this->expectExceptionMessage('MongoDB\Model\IndexInfo is immutable'); unset($info['v']); } + + public function testIs2dSphere() + { + $info = new IndexInfo([ + 'v' => 2, + 'key' => ['pos' => '2dsphere'], + 'name' => 'pos_2dsphere', + 'ns' => 'foo.bar', + ]); + + $this->assertSame(2, $info->getVersion()); + $this->assertSame(['pos' => '2dsphere'], $info->getKey()); + $this->assertSame('pos_2dsphere', $info->getName()); + $this->assertSame('foo.bar', $info->getNamespace()); + $this->assertTrue($info->is2dSphere()); + $this->assertFalse($info->isGeoHaystack()); + $this->assertFalse($info->isSparse()); + $this->assertFalse($info->isText()); + $this->assertFalse($info->isTtl()); + $this->assertFalse($info->isUnique()); + } + + public function testIsGeoHaystack() + { + $info = new IndexInfo([ + 'v' => 2, + 'key' => ['pos2' => 'geoHaystack', 'x' => 1], + 'name' => 'pos2_geoHaystack_x_1', + 'ns' => 'foo.bar', + ]); + + $this->assertSame(2, $info->getVersion()); + $this->assertSame(['pos2' => 'geoHaystack', 'x' => 1], $info->getKey()); + $this->assertSame('pos2_geoHaystack_x_1', $info->getName()); + $this->assertSame('foo.bar', $info->getNamespace()); + $this->assertFalse($info->is2dSphere()); + $this->assertTrue($info->isGeoHaystack()); + $this->assertFalse($info->isSparse()); + $this->assertFalse($info->isText()); + $this->assertFalse($info->isTtl()); + $this->assertFalse($info->isUnique()); + } + + public function testIsText() + { + $info = new IndexInfo([ + 'v' => 2, + 'key' => ['_fts' => 'text', '_ftsx' => 1], + 'name' => 'title_text_description_text', + 'ns' => 'foo.bar', + ]); + + $this->assertSame(2, $info->getVersion()); + $this->assertSame(['_fts' => 'text', '_ftsx' => 1], $info->getKey()); + $this->assertSame('title_text_description_text', $info->getName()); + $this->assertSame('foo.bar', $info->getNamespace()); + $this->assertFalse($info->is2dSphere()); + $this->assertFalse($info->isGeoHaystack()); + $this->assertFalse($info->isSparse()); + $this->assertTrue($info->isText()); + $this->assertFalse($info->isTtl()); + $this->assertFalse($info->isUnique()); + } }