Exemplo n.º 1
0
 /**
  * @param Collection $collection
  */
 public function __construct(Collection $collection)
 {
     $this->collection = $collection;
     $this->collection->createIndex(['identity' => 1], ['unique']);
     $this->collection->createIndex(['type' => 1]);
     $this->collection->createIndex(['type' => 1, 'associations' => 1]);
     $this->collection->createIndex(['identity' => 1, 'type' => 1, 'associations' => 1]);
 }
Exemplo n.º 2
0
 /**
  * @param Collection $collection
  * @param string $identityField
  * @param string $documentClass
  */
 public function __construct(Collection $collection, string $identityField, string $documentClass)
 {
     $this->collection = $collection;
     $this->identityField = $identityField;
     $this->documentClass = $documentClass;
     $this->converter = new ArrayConverter();
     $this->collection->createIndex([$this->identityField => 1], ['unique']);
 }
Exemplo n.º 3
0
 public function createIndexes()
 {
     try {
         $this->collection->dropIndexes();
     } catch (RuntimeException $e) {
     }
     $this->collection->createIndex(['timestamp' => 1], ['expireAfterSeconds' => 302]);
     $this->collection->createIndex(['sessionId' => 1], ['unique' => false]);
 }
Exemplo n.º 4
0
 /**
  * Open session
  *
  * @param string $savePath
  * @param string $name
  * @return bool
  */
 public function open($savePath, $name)
 {
     // Note: session save path is not used
     $this->sessionName = $name;
     $this->lifetime = (int) ini_get('session.gc_maxlifetime');
     $this->mongoCollection = $this->mongoClient->selectCollection($this->options->getDatabase(), $this->options->getCollection());
     $this->mongoCollection->createIndex([$this->options->getModifiedField() => 1], $this->options->useExpireAfterSecondsIndex() ? ['expireAfterSeconds' => $this->lifetime] : []);
     return true;
 }
Exemplo n.º 5
0
 /**
  * 
  */
 public function __construct(AbstractController $controller, Account $account)
 {
     $this->controller = $controller;
     $this->env = $controller->getGwEnvironment();
     $this->lang = $this->env->getLang();
     $cache = $this->env->getCache();
     /* @var $cache MongoCache */
     $this->collection = $cache->getMongoDB()->selectCollection('statistics');
     $this->collection->createIndex(['account' => 1]);
     $this->account = $account;
 }
Exemplo n.º 6
0
 /**
  * Ensure index of correct specification and a unique name whether the specification or name already exist or not.
  * Will not create index if $index is a prefix of an existing index
  *
  * @param array $index index to create in same format as \MongoDB\Collection::createIndex()
  *
  * @return void
  *
  * @throws \Exception couldnt create index after 5 attempts
  */
 private function ensureIndex(array $index)
 {
     //if $index is a prefix of any existing index we are good
     foreach ($this->collection->listIndexes() as $existingIndex) {
         $slice = array_slice($existingIndex['key'], 0, count($index), true);
         if ($slice === $index) {
             return;
         }
     }
     for ($i = 0; $i < 5; ++$i) {
         for ($name = uniqid(); strlen($name) > 0; $name = substr($name, 0, -1)) {
             //creating an index with same name and different spec does nothing.
             //creating an index with same spec and different name does nothing.
             //so we use any generated name, and then find the right spec after we have called,
             //and just go with that name.
             try {
                 $this->collection->createIndex($index, ['name' => $name, 'background' => true]);
             } catch (\MongoDB\Exception\Exception $e) {
                 //this happens when the name was too long, let continue
             }
             foreach ($this->collection->listIndexes() as $existingIndex) {
                 if ($existingIndex['key'] === $index) {
                     return;
                 }
             }
         }
     }
     throw new \Exception('couldnt create index after 5 attempts');
     //@codeCoverageIgnoreEnd
 }
Exemplo n.º 7
0
 /**
  * Creates an index on the given field(s), or does nothing if the index already exists
  *
  * @link http://www.php.net/manual/en/mongocollection.createindex.php
  * @param array $keys Field or fields to use as index.
  * @param array $options [optional] This parameter is an associative array of the form array("optionname" => <boolean>, ...).
  * @return array Returns the database response.
  *
  * @todo This method does not yet return the correct result
  */
 public function createIndex(array $keys, array $options = [])
 {
     // Note: this is what the result array should look like
     //        $expected = [
     //            'createdCollectionAutomatically' => true,
     //            'numIndexesBefore' => 1,
     //            'numIndexesAfter' => 2,
     //            'ok' => 1.0
     //        ];
     return $this->collection->createIndex($keys, $options);
 }
Exemplo n.º 8
0
 /**
  * Creates an index on the given field(s), or does nothing if the index already exists
  *
  * @link http://www.php.net/manual/en/mongocollection.createindex.php
  * @param array $keys Field or fields to use as index.
  * @param array $options [optional] This parameter is an associative array of the form array("optionname" => <boolean>, ...).
  * @return array Returns the database response.
  */
 public function createIndex($keys, array $options = [])
 {
     if (is_string($keys)) {
         if (empty($keys)) {
             throw new MongoException('empty string passed as key field');
         }
         $keys = [$keys => 1];
     }
     if (is_object($keys)) {
         $keys = (array) $keys;
     }
     if (!is_array($keys) || !count($keys)) {
         throw new MongoException('index specification has no elements');
     }
     if (!isset($options['name'])) {
         $options['name'] = \MongoDB\generate_index_name($keys);
     }
     $indexes = iterator_to_array($this->collection->listIndexes());
     $indexCount = count($indexes);
     $collectionExists = true;
     $indexExists = false;
     // listIndexes returns 0 for non-existing collections while the legacy driver returns 1
     if ($indexCount === 0) {
         $collectionExists = false;
         $indexCount = 1;
     }
     foreach ($indexes as $index) {
         if ($index->getKey() === $keys || $index->getName() === $options['name']) {
             $indexExists = true;
             break;
         }
     }
     try {
         foreach (['w', 'wTimeoutMS', 'safe', 'timeout', 'wtimeout'] as $invalidOption) {
             if (isset($options[$invalidOption])) {
                 unset($options[$invalidOption]);
             }
         }
         $this->collection->createIndex($keys, $options);
     } catch (\MongoDB\Driver\Exception\Exception $e) {
         throw ExceptionConverter::toLegacy($e, 'MongoResultException');
     }
     $result = ['createdCollectionAutomatically' => !$collectionExists, 'numIndexesBefore' => $indexCount, 'numIndexesAfter' => $indexCount, 'note' => 'all indexes already exist', 'ok' => 1.0];
     if (!$indexExists) {
         $result['numIndexesAfter']++;
         unset($result['note']);
     }
     return $result;
 }
 /**
  * Creates an index on the given field(s), or does nothing if the index already exists
  *
  * @link http://www.php.net/manual/en/mongocollection.createindex.php
  * @param array $keys Field or fields to use as index.
  * @param array $options [optional] This parameter is an associative array of the form array("optionname" => <boolean>, ...).
  * @return array Returns the database response.
  *
  * @todo This method does not yet return the correct result
  */
 public function createIndex($keys, array $options = [])
 {
     if (is_string($keys)) {
         if (empty($keys)) {
             throw new MongoException('empty string passed as key field');
         }
         $keys = [$keys => 1];
     }
     if (is_object($keys)) {
         $keys = (array) $keys;
     }
     if (!is_array($keys) || !count($keys)) {
         throw new MongoException('keys cannot be empty');
     }
     // duplicate
     $neededOptions = ['unique' => 1, 'sparse' => 1, 'expireAfterSeconds' => 1, 'background' => 1, 'dropDups' => 1];
     $indexOptions = array_intersect_key($options, $neededOptions);
     $indexes = $this->collection->listIndexes();
     foreach ($indexes as $index) {
         if (!empty($options['name']) && $index->getName() === $options['name']) {
             throw new \MongoResultException(sprintf('index with name: %s already exists', $index->getName()));
         }
         if ($index->getKey() == $keys) {
             $currentIndexOptions = array_intersect_key($index->__debugInfo(), $neededOptions);
             unset($currentIndexOptions['name']);
             if ($currentIndexOptions != $indexOptions) {
                 throw new \MongoResultException('Index with same keys but different options already exists');
             }
             return ['createdCollectionAutomatically' => false, 'numIndexesBefore' => count($indexes), 'numIndexesAfter' => count($indexes), 'note' => 'all indexes already exist', 'ok' => 1.0];
         }
     }
     try {
         $this->collection->createIndex($keys, $this->convertWriteConcernOptions($options));
     } catch (\MongoDB\Driver\Exception\Exception $e) {
         ExceptionConverter::toLegacy($e);
     }
     return ['createdCollectionAutomatically' => true, 'numIndexesBefore' => count($indexes), 'numIndexesAfter' => count($indexes) + 1, 'ok' => 1.0];
 }
Exemplo n.º 10
0
 public function ensureIndexes(Database $database, Collection $collection)
 {
     $collection->createIndex(['title' => 'text']);
 }
Exemplo n.º 11
0
 /**
  * Ensure $indexToEnsure on the given mongo $collection
  * @param Collection $collection
  * @param array $indexToEnsure
  */
 protected function ensureIndex(Collection $collection, array $indexToEnsure)
 {
     $collection->createIndex($indexToEnsure, array('background' => 1));
 }
Exemplo n.º 12
0
 public static function createCollection(Manager $manager, $namespace)
 {
     $collection = new Collection($manager, $namespace);
     $collection->createIndex(['expireAt' => 1], ['expireAfterSeconds' => 0]);
     return $collection;
 }
Exemplo n.º 13
0
 /**
  * @param Collection $collection
  * @param string $identityField
  */
 public function __construct(Collection $collection, string $identityField)
 {
     $this->collection = $collection;
     $this->identityField = $identityField;
     $this->collection->createIndex([$this->identityField => 1]);
 }
Exemplo n.º 14
0
 /**
  * Construct a new instance of MongoCache.
  *
  * @param Collection $collection        The collection containing the cached data.
  * @param integer    $defaultTimeToLive The default time to live in seconds.
  */
 public function __construct(Collection $collection, $defaultTimeToLive = CacheInterface::MAX_TTL)
 {
     $this->setDefaultTTL($defaultTimeToLive);
     $this->collection = $collection;
     $this->collection->createIndex(['expires' => 1], ['expireAfterSeconds' => 0, 'background' => true]);
 }