public createIndex ( $collectionName, Webiny\Component\Mongo\Index\IndexInterface $index, array $options = [] ) | ||
$index | Webiny\Component\Mongo\Index\IndexInterface | |
$options | array |
/** * @dataProvider driverSet */ public function testSingleIndex(Mongo $mongo) { $collection = 'TestIndexCollection'; $mongo->dropCollection($collection); $index = new SingleIndex('Name', 'name', false, true); $mongo->createIndex($collection, $index); $index = new CompoundIndex('TitleCategory', ['title', 'category']); $mongo->createIndex($collection, $index); $index = new TextIndex('Title', ['title', 'category']); $mongo->createIndex($collection, $index); $indexes = $mongo->getIndexInfo($collection); $this->assertEquals(4, count($indexes)); }
/** * Import IPv6 entries. */ public function importIp6CityBlock() { // city blocks db file $cbFile = $this->dbFolder . 'GeoLite2-City-Blocks-IPv6.csv'; // start the import echo "\nImporting city IPv6 block ... please give it couple of minutes to finish. (about 5min)"; $handle = fopen($cbFile, "r"); fgetcsv($handle, 0, ","); // remove the header row while (($row = fgetcsv($handle, 0, ",")) !== false) { // calculate ip range block $cidrRange = Ipv6Helper::calculateIpv6CidrRange($row[0]); // save the record $cityBlockEntity = new CityBlockIp6Entity(); $cityBlockEntity->rangeStart = $cidrRange['start']; $cityBlockEntity->rangeEnd = $cidrRange['end']; $cityBlockEntity->geoId = (int) $row[1]; $cityBlockEntity->save(); } fclose($handle); // ensure range index $index = new SingleIndex('ipStart', 'rangeStart'); $this->mongo->createIndex('GeoIpCityBlockIp6', $index); echo "\nCity IPv6 block import done\n"; }
/** * Installs the required mongo collection for cache storage and creates the required indexes. * * @return bool */ public function installCollections() { $collections = $this->mongoInstance->listCollections(); foreach ($collections as $collection) { /* @var $collection CollectionInfo */ if ($collection->getName() == self::collection) { return true; } } $this->mongoInstance->createCollection(self::collection); $this->mongoInstance->createIndex(self::collection, new SingleIndex('key', 'key', false, true)); $this->mongoInstance->createIndex(self::collection, new SingleIndex('ttl', 'ttl', false, false, false, 0)); return true; }
/** * @dataProvider driverSet */ public function testIndexes(Mongo $mongo) { $collection = 'IndexCollection'; $mongo->dropCollection($collection); $index = new SingleIndex('Name', 'name', false, true); $indexName = $mongo->createIndex($collection, $index); $this->assertEquals('Name', $indexName); $index = new CompoundIndex('TitleCategory', ['title', 'category']); $indexName = $mongo->createIndex($collection, $index); $this->assertEquals('TitleCategory', $indexName); $index = new TextIndex('CompoundTextIndex', ['title', 'category']); $indexName = $mongo->createIndex($collection, $index); $this->assertEquals('CompoundTextIndex', $indexName); // Drop index because we can't have 2 text indexes in one collection $mongo->dropIndex($collection, 'CompoundTextIndex'); $index = new TextIndex('SingleTextIndex', 'name'); $indexName = $mongo->createIndex($collection, $index); $this->assertEquals('SingleTextIndex', $indexName); $index = new SphereIndex('Location', 'location'); $indexName = $mongo->createIndex($collection, $index); $this->assertEquals('Location', $indexName); $indexes = $mongo->listIndexes($collection); $this->assertEquals(5, count($indexes)); }
/** * Creates the necessary indexes and collections if they don't exist. */ private function createCollections() { $collections = $this->mongo->listCollections(); $collectionsCreated = false; foreach ($collections as $collection) { /* @var $collection CollectionInfo */ if ($collection->getName() == self::ADB_STATS_DAILY) { $collectionsCreated = true; break; } } if (!$collectionsCreated) { // create collections $this->mongo->createCollection(self::ADB_STATS_DAILY); $this->mongo->createCollection(self::ADB_STATS_MONTHLY); $this->mongo->createCollection(self::ADB_DIMS); // ensure indexes $this->mongo->createIndex(self::ADB_STATS_DAILY, new CompoundIndex('entityTsEntry', ['entity', 'ref', 'ts'], true, true)); $this->mongo->createIndex(self::ADB_STATS_MONTHLY, new CompoundIndex('entityMonthEntry', ['entity', 'ref', 'ts'], true, true)); $this->mongo->createIndex(self::ADB_DIMS, new CompoundIndex('dimension', ['name', 'value', 'entity', 'ts'], true, true)); $this->mongo->createIndex(self::ADB_DIMS, new CompoundIndex('dimension_entity', ['entity', 'ts'], true)); } }