Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/Database/Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -733,6 +733,13 @@ abstract public function createOrUpdateDocuments(
array $changes
): array;

/**
* @param string $collection
* @param array<Document> $documents
* @return array<Document>
*/
abstract public function getSequences(string $collection, array $documents): array;

/**
* Delete Document
*
Expand Down
20 changes: 1 addition & 19 deletions src/Database/Adapter/MariaDB.php
Original file line number Diff line number Diff line change
Expand Up @@ -1171,8 +1171,6 @@ public function createOrUpdateDocuments(
$bindIndex = 0;
$batchKeys = [];
$bindValues = [];
$documentIds = [];
$documentTenants = [];

foreach ($changes as $change) {
$document = $change->getNew();
Expand All @@ -1184,14 +1182,10 @@ public function createOrUpdateDocuments(

if (!empty($document->getSequence())) {
$attributes['_id'] = $document->getSequence();
} else {
$documentIds[] = $document->getId();
}

if ($this->sharedTables) {
$attributes['_tenant']
= $documentTenants[]
= $document->getTenant();
$attributes['_tenant'] = $document->getTenant();
}

\ksort($attributes);
Expand Down Expand Up @@ -1349,18 +1343,6 @@ public function createOrUpdateDocuments(
}
$stmtAddPermissions->execute();
}

$sequences = $this->getSequences(
$collection,
$documentIds,
$documentTenants
);

foreach ($changes as $change) {
if (isset($sequences[$change->getNew()->getId()])) {
$change->getNew()->setAttribute('$sequence', $sequences[$change->getNew()->getId()]);
}
}
} catch (PDOException $e) {
throw $this->processException($e);
}
Expand Down
5 changes: 5 additions & 0 deletions src/Database/Adapter/Pool.php
Original file line number Diff line number Diff line change
Expand Up @@ -494,4 +494,9 @@ protected function execute(mixed $stmt): bool
{
return $this->delegate(__FUNCTION__, \func_get_args());
}

public function getSequences(string $collection, array $documents): array
{
return $this->delegate(__FUNCTION__, \func_get_args());
}
}
87 changes: 43 additions & 44 deletions src/Database/Adapter/SQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -710,49 +710,64 @@ public function deleteDocuments(string $collection, array $sequences, array $per
}

/**
* Get internal IDs for the given documents
* Assign internal IDs for the given documents
*
* @param string $collection
* @param array<string> $documentIds
* @param array<?int> $documentTenants
* @return array<string>
* @param array<Document> $documents
* @return array<Document>
* @throws DatabaseException
*/
protected function getSequences(string $collection, array $documentIds, array $documentTenants = []): array
public function getSequences(string $collection, array $documents): array
{
$sequences = [];
$documentIds = [];
$keys = [];
$binds = [];

/**
* UID, _tenant bottleneck is ~ 5000 rows since we use _uid IN query
*/
foreach (\array_chunk($documentIds, 1000) as $documentIdsChunk) {
$sql = "
SELECT _uid, _id
FROM {$this->getSQLTable($collection)}
WHERE {$this->quote('_uid')} IN (" . implode(',', array_map(fn ($index) => ":_key_{$index}", array_keys($documentIdsChunk))) . ")
{$this->getTenantQuery($collection, tenantCount: \count($documentIdsChunk))}
";
foreach ($documents as $i => $document) {
if (empty($document->getSequence())) {
$documentIds[] = $document->getId();

$stmt = $this->getPDO()->prepare($sql);
$key = ":uid_{$i}";

foreach ($documentIdsChunk as $index => $id) {
$stmt->bindValue(":_key_{$index}", $id);
}
$binds[$key] = $document->getId();
$keys[] = $key;

if ($this->sharedTables) {
foreach ($documentIdsChunk as $index => $id) {
$stmt->bindValue(":_tenant_{$index}", \array_shift($documentTenants));
if ($this->sharedTables) {
$binds[':_tenant_'.$i] = $document->getTenant();
}
}
}

$stmt->execute();
$results = $stmt->fetchAll(\PDO::FETCH_KEY_PAIR); // Fetch as [documentId => sequence]
$stmt->closeCursor();
if (empty($documentIds)) {
return $documents;
}

$sequences = [...$sequences, ...$results];
$placeholders = implode(',', array_values($keys));

$sql = "
SELECT _uid, _id
FROM {$this->getSQLTable($collection)}
WHERE {$this->quote('_uid')} IN ({$placeholders})
{$this->getTenantQuery($collection, tenantCount: \count($documentIds))}
";

$stmt = $this->getPDO()->prepare($sql);

foreach ($binds as $key => $value) {
$stmt->bindValue($key, $value);
}

return $sequences;
$stmt->execute();
$sequences = $stmt->fetchAll(\PDO::FETCH_KEY_PAIR); // Fetch as [documentId => sequence]
$stmt->closeCursor();

foreach ($documents as $document) {
if (isset($sequences[$document->getId()])) {
$document['$sequence'] = $sequences[$document->getId()];
}
}

return $documents;
}

/**
Expand Down Expand Up @@ -1808,8 +1823,6 @@ public function createDocuments(string $collection, array $documents): array
$batchKeys = [];
$bindValues = [];
$permissions = [];
$documentIds = [];
$documentTenants = [];

foreach ($documents as $index => $document) {
$attributes = $document->getAttributes();
Expand All @@ -1820,13 +1833,10 @@ public function createDocuments(string $collection, array $documents): array

if (!empty($document->getSequence())) {
$attributes['_id'] = $document->getSequence();
} else {
$documentIds[] = $document->getId();
}

if ($this->sharedTables) {
$attributes['_tenant'] = $document->getTenant();
$documentTenants[] = $document->getTenant();
}

$bindKeys = [];
Expand Down Expand Up @@ -1889,17 +1899,6 @@ public function createDocuments(string $collection, array $documents): array
$this->execute($stmtPermissions);
}

$sequences = $this->getSequences(
$collection,
$documentIds,
$documentTenants
);

foreach ($documents as $document) {
if (isset($sequences[$document->getId()])) {
$document['$sequence'] = $sequences[$document->getId()];
}
}
} catch (PDOException $e) {
throw $this->processException($e);
}
Expand Down
4 changes: 4 additions & 0 deletions src/Database/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -3759,6 +3759,8 @@ public function createDocuments(
return $this->adapter->createDocuments($collection->getId(), $chunk);
});

$batch = $this->adapter->getSequences($collection->getId(), $batch);

foreach ($batch as $document) {
if ($this->resolveRelationships) {
$document = $this->silent(fn () => $this->populateDocumentRelationships($collection, $document));
Expand Down Expand Up @@ -5074,6 +5076,8 @@ public function createOrUpdateDocumentsWithIncrease(
$chunk
)));

$batch = $this->adapter->getSequences($collection->getId(), $batch);

foreach ($chunk as $change) {
if ($change->getOld()->isEmpty()) {
$created++;
Expand Down
2 changes: 2 additions & 0 deletions tests/e2e/Adapter/Scopes/DocumentTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,7 @@ public function testUpsertDocuments(): void
foreach ($results as $index => $document) {
$createdAt[$index] = $document->getCreatedAt();
$this->assertNotEmpty(true, $document->getId());
$this->assertNotEmpty(true, $document->getSequence());
$this->assertIsString($document->getAttribute('string'));
$this->assertEquals('text📝', $document->getAttribute('string')); // Also makes sure an emoji is working
$this->assertIsInt($document->getAttribute('integer'));
Expand Down Expand Up @@ -481,6 +482,7 @@ public function testUpsertDocuments(): void

foreach ($results as $document) {
$this->assertNotEmpty(true, $document->getId());
$this->assertNotEmpty(true, $document->getSequence());
$this->assertIsString($document->getAttribute('string'));
$this->assertEquals('new text📝', $document->getAttribute('string')); // Also makes sure an emoji is working
$this->assertIsInt($document->getAttribute('integer'));
Expand Down