Skip to content

Commit e1cfbc9

Browse files
committed
feat: optimize batch processing in incrementBatch and setBatch methods
1 parent e55c542 commit e1cfbc9

1 file changed

Lines changed: 15 additions & 15 deletions

File tree

src/Usage/Adapter/Database.php

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ protected function getColumnDefinition(string $id): string
9999

100100
public function incrementBatch(array $metrics, int $batchSize = 1000): bool
101101
{
102-
$this->db->getAuthorization()->skip(function () use ($metrics) {
102+
$this->db->getAuthorization()->skip(function () use ($metrics, $batchSize) {
103103
$documentsById = [];
104104
foreach ($metrics as $metric) {
105105
$period = $metric['period'] ?? '1h';
@@ -133,13 +133,13 @@ public function incrementBatch(array $metrics, int $batchSize = 1000): bool
133133
}
134134
}
135135

136-
$documents = [];
137-
foreach ($documentsById as $doc) {
138-
$documents[] = new Document($doc);
139-
}
136+
$documents = array_values(array_map(
137+
static fn (array $doc) => new Document($doc),
138+
$documentsById
139+
));
140140

141-
if (!empty($documents)) {
142-
$this->db->upsertDocumentsWithIncrease($this->collection, 'value', $documents);
141+
foreach (array_chunk($documents, max(1, $batchSize)) as $chunk) {
142+
$this->db->upsertDocumentsWithIncrease($this->collection, 'value', $chunk);
143143
}
144144
});
145145

@@ -157,7 +157,7 @@ public function incrementBatch(array $metrics, int $batchSize = 1000): bool
157157
*/
158158
public function setBatch(array $metrics, int $batchSize = 1000): bool
159159
{
160-
$this->db->getAuthorization()->skip(function () use ($metrics) {
160+
$this->db->getAuthorization()->skip(function () use ($metrics, $batchSize) {
161161
$documentsById = [];
162162
foreach ($metrics as $metric) {
163163
$period = $metric['period'] ?? '1h';
@@ -176,7 +176,7 @@ public function setBatch(array $metrics, int $batchSize = 1000): bool
176176

177177
$id = $this->buildDeterministicId($metric['metric'], $period, $time);
178178

179-
// Last one wins for the same ID (counter behavior, not aggregating)
179+
// Last one wins for the same ID (replace behavior, not aggregating)
180180
$documentsById[$id] = [
181181
'$id' => $id,
182182
'$permissions' => [],
@@ -188,13 +188,13 @@ public function setBatch(array $metrics, int $batchSize = 1000): bool
188188
];
189189
}
190190

191-
$documents = [];
192-
foreach ($documentsById as $doc) {
193-
$documents[] = new Document($doc);
194-
}
191+
$documents = array_values(array_map(
192+
static fn (array $doc) => new Document($doc),
193+
$documentsById
194+
));
195195

196-
if (!empty($documents)) {
197-
$this->db->upsertDocuments($this->collection, $documents);
196+
foreach (array_chunk($documents, max(1, $batchSize)) as $chunk) {
197+
$this->db->upsertDocuments($this->collection, $chunk);
198198
}
199199
});
200200

0 commit comments

Comments
 (0)