Пример #1
0
 /**
  * Builds the index for the given collection
  *
  * @param DatabaseInterface|\Iterator $database
  * @return $this
  */
 public function indexDatabase($database)
 {
     // Clear the map
     $this->map = array();
     /** @var SplFixedArray $collection */
     $collection = null;
     if ($database instanceof DatabaseRawDataInterface) {
         $collection = $database->getRawData();
     }
     if ($database instanceof SplFixedArray) {
         // Use the fixed array as is
     } elseif (is_array($database)) {
         $collection = SplFixedArray::fromArray($database);
     } elseif ($database instanceof \Iterator) {
         $collection = SplFixedArray::fromArray(iterator_to_array($database));
     } else {
         throw new InvalidIndexException(sprintf('Can not build index of argument of type %s', is_object($database) ? get_class($database) : gettype($database)));
     }
     $position = 0;
     $count = $collection->getSize();
     if ($count > 0) {
         do {
             $tempEntry = DocumentUtility::assertDocumentIdentifier($database[$position]);
             $this->addEntryWithPosition($tempEntry, $position);
         } while (++$position < $count);
     }
 }
Пример #2
0
 /**
  * Builds the index for the given collection
  *
  * @param DatabaseInterface|\Iterator $database
  * @return $this
  */
 public function indexDatabase($database)
 {
     // Clear the map
     $this->map = array();
     /** @var SplFixedArray $collection */
     $collection = null;
     if ($database instanceof DatabaseRawDataInterface) {
         $collection = $database->getRawData();
     } elseif ($database instanceof SplFixedArray) {
         $collection = $database;
     } elseif (is_array($database)) {
         $collection = SplFixedArray::fromArray($database);
     } elseif ($database instanceof \Iterator) {
         $collection = SplFixedArray::fromArray(iterator_to_array($database));
     } else {
         throw new InvalidIndexException(sprintf('Can not build index of argument of type %s', is_object($database) ? get_class($database) : gettype($database)));
     }
     $position = 0;
     $count = $collection->getSize();
     $tempMap = array();
     if ($count > 0) {
         $collectionContainsDocumentObjects = $collection[0] instanceof DocumentInterface;
         do {
             $entry = $collection[$position];
             if ($collectionContainsDocumentObjects) {
                 $key = DocumentUtility::assertDocumentIdentifier($entry)->getId();
             } else {
                 $key = DocumentUtility::getIdentifierForDocument($entry);
             }
             //				DebugUtility::var_dump('Index', $key);
             if (isset($tempMap[$key])) {
                 throw new DuplicateEntryException(sprintf('Duplicate entry \'%s\' for identifier', $key), 1415046937);
             }
             $tempMap[$key] = $position;
         } while (++$position < $count);
     }
     $this->map = $tempMap;
 }
Пример #3
0
 /**
  * Removes the given Document from the database
  *
  * @param DocumentInterface $document
  */
 public function remove($document)
 {
     $this->_assertDataInstancesDatabaseIdentifier($document);
     DocumentUtility::assertDocumentIdentifier($document);
     if (!$this->contains($document)) {
         throw new InvalidDataException(sprintf('Object with GUID %s does not exist in the database. Maybe the values of the identifier is not expressive', $document->getGuid()), 1412800595);
     }
     $index = $this->_getIndexForIdentifier($document->getId());
     if ($index === -1) {
         throw new RuntimeException(sprintf('Could not determine the index of object with GUID %s', $document->getId()), 1412801014);
     }
     $this->_removeObjectDataForIndex($index);
     $this->_removeRawDataForIndex($index);
     $this->_removeFromIndex($document);
     if ($this->contains($document)) {
         throw new RuntimeException(sprintf('Database still contains object %s', $document->getGuid()), 1413290094);
     }
     SharedEventEmitter::emit(Event::DATABASE_DOCUMENT_REMOVED, array($document));
 }