Add document to watching pool for save

@param Document $document
@return \Sokil\Mongo\Persistence
356324
public function persist(Document $document)
    {
        $this->pool->attach($document, self::STATE_SAVE);

        return $this;
    }
1132300
public function saveDocument(string $id, Document $document): self
    {
        $this->documents[$id] = $document;
        $this->filesystem->put($this->getDocumentPath($id), $this->format->stringify($document->getArrayCopy()));

        return $this;
    }
1246765
public function insert($document)
    {
        $this->ensureAllowedMethodCall(__METHOD__);

        if (null !== $this->listener) {
            $this->listener->beforeInsert($document);
        }

        $id = $this->getWrappedBulk()->insert($document);
        if (null === $id) {
            if ($document instanceof DocumentInterface && !$document->getId()) {
                throw new LogicException(
                    '$document contains an id, but it\'s method getId() does not return it.'
                );
            }

            $id = $this->extractIdFromDocument($document);
        } elseif ($document instanceof DocumentInterface) {
            if ($document->getId()) {
                throw new LogicException(
                    '$document\'s method getId() returns not the same id document will be inserted with.'
                );
            }
            $document->setId($id);
        }

        $this->insertedIds[$this->currentPosition] = $id;
        $this->currentPosition += 1;

        return $id;
    }
722419
public function insert(array $document)
    {
        $result = $this->getMongoCollection()->insert($document);

        // if write concern acknowledged
        if (is_array($result)) {
            if ($result['ok'] != 1) {
                throw new Exception('Insert error: ' . $result['err'] . ': ' . $result['errmsg']);
            }

            return $this;
        }

        // if write concern unacknowledged
        if (!$result) {
            throw new Exception('Insert error');
        }

        return $this;
    }
1341784
public function getDocumentPersister(string $documentName) : Persisters\DocumentPersister
    {
        if (! isset($this->persisters[$documentName])) {
            $class                           = $this->dm->getClassMetadata($documentName);
            $pb                              = $this->getPersistenceBuilder();
            $this->persisters[$documentName] = new Persisters\DocumentPersister($pb, $this->dm, $this, $this->hydratorFactory, $class);
        }
        return $this->persisters[$documentName];
    }
1100838
public function createReferrerCollection($document)
    {
        $node = $this->registry->getNodeForDocument($document);
        $locale = $this->registry->getOriginalLocaleForDocument($document);

        return new ReferrerCollection(
            $node,
            $this->dispatcher,
            $locale
        );
    }
1126674
public static function bulkInsert($documents)
    {
        $instance = new static();

        $autoIncrementField = $instance->getAutoIncrementField();
        $allowNull = $instance->isAllowNullValue();
        $fieldTypes = $instance->getFieldTypes();
        foreach ($documents as $i => $document) {
            if ($autoIncrementField && !isset($document[$autoIncrementField])) {
                $document[$autoIncrementField] = $instance->getNextAutoIncrementId();
            }
            foreach ((array)$fieldTypes as $field => $type) {
                if (isset($document[$field])) {
                    $document[$field] = $instance->normalizeValue($type, $document[$field]);
                } elseif ($field !== '_id') {
                    $document[$field] = $allowNull ? null : $instance->normalizeValue($type, '');
                }
            }
            $documents[$i] = $document;
        }

        return $instance->getConnection()->bulkInsert($instance->getSource(), $documents);
    }
1187892
public function isPendingForPersist(Document $document)
    {
        return isset($this->persist[get_class($document)][spl_object_hash($document)]);
    }
1256875
private function executeInserts(ClassMetadata $class, array $documents)
    {
        $persister = $this->getDocumentPersister($class->name);

        //TODO: use WritePool
        foreach ($documents as $oid => $document) {
            try {
                $persister->insert($document);

                /* All collections except for ones using addToSet have already been saved
                 * We have left these to be handled separately to avoid checking the collection
                 * for uniqueness on PHP side. This is only a concern for addToSet as other
                 * strategies don't guarantee uniqueness.
                 */
                $persister->handleCollections($document);

                unset($this->documentInsertions[$oid]);
            } catch (\Aws\DynamoDb\Exception\DynamoDbException $e) {
                unset($this->documentInsertions[$oid]);

                throw $e;
            }

            $this->lifecycleEventManager->postPersist($class, $document);
        }
    }
841627
public function getDocumentPersister(string $documentClass): DocumentPersister
    {
        if (isset($this->documentPersisters[$documentClass])) {
            return $this->documentPersisters[$documentClass];
        }

        return $this->documentPersisters[$documentClass] = new DocumentPersister($this->manager, $this->manager->getClassMetadata($documentClass));
    }
1201923
public function includesReferenceTo(object $document) : self
    {
        $this->requiresCurrentField();
        $mapping   = $this->getReferenceMapping();
        $reference = $this->dm->createReference($document, $mapping);
        $storeAs   = $mapping['storeAs'] ?? null;
        $keys      = [];

        switch ($storeAs) {
            case ClassMetadata::REFERENCE_STORE_AS_ID:
                $this->query[$mapping['name']] = $reference;
                return $this;
                break;

            case ClassMetadata::REFERENCE_STORE_AS_REF:
                $keys = ['id' => true];
                break;

            case ClassMetadata::REFERENCE_STORE_AS_DB_REF:
            case ClassMetadata::REFERENCE_STORE_AS_DB_REF_WITH_DB:
                $keys = ['$ref' => true, '$id' => true, '$db' => true];

                if ($storeAs === ClassMetadata::REFERENCE_STORE_AS_DB_REF) {
                    unset($keys['$db']);
                }

                if (isset($mapping['targetDocument'])) {
                    unset($keys['$ref'], $keys['$db']);
                }
                break;

            default:
                throw new InvalidArgumentException(sprintf('Reference type %s is invalid.', $storeAs));
        }

        foreach ($keys as $key => $value) {
            $this->query[$mapping['name']]['$elemMatch'][$key] = $reference[$key];
        }

        return $this;
    }
364394
public function updateDocument(\Document $document)
  {
    $arrSet = $this->buildDocumentDataArray($document, false);
    
    $this->Database->prepare("UPDATE tl_dms_documents %s WHERE id=?")->set($arrSet)->execute($document->id);
    
    return $document;
  }
646484
public function addObject($object, Document $doc = null)
    {
        if (!isset($this->_serializer)) {
            throw new RuntimeException('No serializer defined');
        }

        $data = \call_user_func($this->_serializer, $object);
        if (!$doc) {
            $doc = new Document();
        }
        $doc->setData($data);

        return $this->addDocument($doc);
    }
932541
public function prepareInsertData($document)
    {
        $class     = $this->dm->getClassMetadata(get_class($document));
        $changeset = $this->uow->getDocumentChangeSet($document);

        $insertData = [];
        foreach ($class->fieldMappings as $mapping) {
            $new = $changeset[$mapping['fieldName']][1] ?? null;

            if ($new === null && $mapping['nullable']) {
                $insertData[$mapping['name']] = null;
            }

            /* Nothing more to do for null values, since we're either storing
             * them (if nullable was true) or not.
             */
            if ($new === null) {
                continue;
            }

            // @Field, @String, @Date, etc.
            if (! isset($mapping['association'])) {
                $insertData[$mapping['name']] = Type::getType($mapping['type'])->convertToDatabaseValue($new);

            // @ReferenceOne
            } elseif (isset($mapping['association']) && $mapping['association'] === ClassMetadata::REFERENCE_ONE) {
                $insertData[$mapping['name']] = $this->prepareReferencedDocumentValue($mapping, $new);

            // @EmbedOne
            } elseif (isset($mapping['association']) && $mapping['association'] === ClassMetadata::EMBED_ONE) {
                $insertData[$mapping['name']] = $this->prepareEmbeddedDocumentValue($mapping, $new);

            // @ReferenceMany, @EmbedMany
            // We're excluding collections using addToSet since there is a risk
            // of duplicated entries stored in the collection
            } elseif ($mapping['type'] === ClassMetadata::MANY && ! $mapping['isInverseSide']
                    && $mapping['strategy'] !== ClassMetadata::STORAGE_STRATEGY_ADD_TO_SET && ! $new->isEmpty()) {
                $insertData[$mapping['name']] = $this->prepareAssociatedCollectionValue($new, true);
            }
        }

        // add discriminator if the class has one
        if (isset($class->discriminatorField)) {
            $discriminatorValue = $class->discriminatorValue;

            if ($discriminatorValue === null) {
                if (! empty($class->discriminatorMap)) {
                    throw MappingException::unlistedClassInDiscriminatorMap($class->name);
                }

                $discriminatorValue = $class->name;
            }

            $insertData[$class->discriminatorField] = $discriminatorValue;
        }

        return $insertData;
    }
1240136
protected function addTopLevelLinks(Document $document, array &$json)
    {
        $name = 'links';

        if (
            0 < count($document)
            and $$name = $this->topLevelLinkSerializer->serialize($document)
        ) {
            $json[$name] = $$name;
        }

        return $this;
    }
442056
public function setDocument(\Neobazaar\Entity\Document $document = null)
    {
        $this->document = $document;
    
        return $this;
    }
1145383
private function hydrateEntity($document)
    {
        $class = $this->entityClass;
        $reflectionClass = new \ReflectionClass(AbstractDocument::class);
        $entity = new $class();

        if (!($entity instanceof AbstractDocument)) {
            throw new \Exception(sprintf('"%s" is not a subclass of "%s"', $this->entityClass, AbstractDocument::class));
        }

        $documentProperty = $reflectionClass->getProperty('document');
        $documentProperty->setAccessible(true);
        $documentProperty->setValue($entity, $document);

        return $entity;
    }