Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Internal ids chunks #521

Merged
merged 33 commits into from
Feb 12, 2025
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
8e14b9b
internal ids chunks
fogelito Feb 6, 2025
d8110bf
lock
fogelito Feb 6, 2025
c989b3b
INSERT_BATCH_SIZE limit
fogelito Feb 6, 2025
0a12dee
Chunk document ids
fogelito Feb 6, 2025
a63f8eb
translate document ids
fogelito Feb 9, 2025
6cb0e4b
INSERT_BATCH_SIZE
fogelito Feb 9, 2025
ee79c00
return type
fogelito Feb 9, 2025
6fabc26
Create documents chunking from outside
fogelito Feb 11, 2025
d193bfb
Merge branch 'main' of github.com:utopia-php/database into chunk-docu…
fogelito Feb 11, 2025
f085ca0
updateDocuments batch limit
fogelito Feb 11, 2025
a28fa1e
Chunk createOrUpdateDocuments
fogelito Feb 11, 2025
e47ea69
Formatting
fogelito Feb 11, 2025
3ed992d
column counts
fogelito Feb 11, 2025
d641a4b
Fix elseif
fogelito Feb 11, 2025
314286f
Check message
fogelito Feb 11, 2025
578d18a
lint
fogelito Feb 11, 2025
271794c
Fix analyse
fogelito Feb 11, 2025
519cbb4
Fix Code QL
fogelito Feb 11, 2025
748f601
Fix Code QL intval
fogelito Feb 11, 2025
1d85d11
Fix Code QL casting
fogelito Feb 11, 2025
d0be423
Fix Code QL casting
fogelito Feb 11, 2025
23b2937
Split 2 lines
fogelito Feb 11, 2025
735bc7e
Another try
fogelito Feb 11, 2025
7ad36d6
Add var
fogelito Feb 11, 2025
de89918
Another try
fogelito Feb 11, 2025
a3fac0f
Another try
fogelito Feb 11, 2025
9ae826f
Another try
fogelito Feb 11, 2025
07832ba
Another abs
fogelito Feb 11, 2025
6db3d71
Avoid negative
fogelito Feb 11, 2025
e0f2d5b
Remove comment
fogelito Feb 11, 2025
77f4216
Apply suggestions from code review
abnegate Feb 12, 2025
735337f
Update src/Database/Adapter/MariaDB.php
abnegate Feb 12, 2025
8f13aef
Merge remote-tracking branch 'origin/main' into chunk-document-ids
abnegate Feb 12, 2025
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
32 changes: 16 additions & 16 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 27 additions & 15 deletions src/Database/Adapter/MariaDB.php
Original file line number Diff line number Diff line change
Expand Up @@ -981,7 +981,7 @@ public function createDocuments(string $collection, array $documents, int $batch
try {
$name = $this->filter($collection);
$batches = \array_chunk($documents, \max(1, $batchSize));
$documentIds = \array_map(fn ($document) => $document->getId(), $documents);
$documentIds = [];

foreach ($batches as $batch) {
$bindIndex = 0;
Expand All @@ -990,15 +990,19 @@ public function createDocuments(string $collection, array $documents, int $batch
$permissions = [];

foreach ($batch as $document) {
/**
* @var Document $document
*/
$attributes = $document->getAttributes();
$attributes['_uid'] = $document->getId();
$attributes['_createdAt'] = $document->getCreatedAt();
$attributes['_updatedAt'] = $document->getUpdatedAt();
$attributes['_permissions'] = \json_encode($document->getPermissions());

if (!empty($document->getInternalId())) {
abnegate marked this conversation as resolved.
Show resolved Hide resolved
$internalIds[$document->getId()] = true;
$attributes['_id'] = $document->getInternalId();
} else {
$documentIds[] = $document->getId();
}

if ($this->sharedTables) {
Expand Down Expand Up @@ -1077,27 +1081,35 @@ public function createDocuments(string $collection, array $documents, int $batch
}
}

// Get internal IDs
$sql = "
$internalIds = [];

/**
* UID, _tenant bottleneck is ~ 5000 rows since we use _uid IN query
*/
foreach (\array_chunk($documentIds, 3000) as $documentIdsChunk) {
// Get internal IDs
$sql = "
SELECT _uid, _id
FROM {$this->getSQLTable($collection)}
WHERE _uid IN (" . implode(',', array_map(fn ($index) => ":_key_{$index}", array_keys($documentIds))) . ")
WHERE _uid IN (" . implode(',', array_map(fn ($index) => ":_key_{$index}", array_keys($documentIdsChunk))) . ")
{$this->getTenantQuery($collection)}
";
$stmt = $this->getPDO()->prepare($sql);

$stmt = $this->getPDO()->prepare($sql);
foreach ($documentIdsChunk as $index => $id) {
$stmt->bindValue(":_key_{$index}", $id);
}

foreach ($documentIds as $index => $id) {
$stmt->bindValue(":_key_{$index}", $id);
}
if ($this->sharedTables) {
$stmt->bindValue(':_tenant', $this->tenant);
}

if ($this->sharedTables) {
$stmt->bindValue(':_tenant', $this->tenant);
}
$stmt->execute();
$results = $stmt->fetchAll(PDO::FETCH_KEY_PAIR); // Fetch as [documentId => internalId]
$stmt->closeCursor();

$stmt->execute();
$internalIds = $stmt->fetchAll(PDO::FETCH_KEY_PAIR); // Fetch as [documentId => internalId]
$stmt->closeCursor();
$internalIds = array_merge($internalIds, $results);
}

foreach ($documents as $document) {
if (isset($internalIds[$document->getId()])) {
Expand Down
14 changes: 12 additions & 2 deletions src/Database/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,8 @@ class Database
public const EVENT_INDEX_CREATE = 'index_create';
public const EVENT_INDEX_DELETE = 'index_delete';

public const INSERT_BATCH_SIZE = 10_000;
public const DELETE_BATCH_SIZE = 10_000;
public const INSERT_BATCH_SIZE = 3_000;
public const DELETE_BATCH_SIZE = 3_000;

/**
* List of Internal attributes
Expand Down Expand Up @@ -3418,6 +3418,16 @@ public function createDocuments(

$collection = $this->silent(fn () => $this->getCollection($collection));

/**
* Check collection exist
*/
if ($collection->getId() !== self::METADATA) {
$authorization = new Authorization(self::PERMISSION_CREATE);
if (!$authorization->isValid($collection->getCreate())) {
throw new AuthorizationException($authorization->getDescription());
}
}

$time = DateTime::now();

foreach ($documents as $key => $document) {
Expand Down