Skip to content

Commit c97568e

Browse files
Merge pull request #37216 from nextcloud/backport/37029/stable25
[stable25] Add chunking in SystemTagObjectMapper::getTagIdsForObjects
2 parents efdd81b + f404122 commit c97568e

File tree

2 files changed

+25
-12
lines changed

2 files changed

+25
-12
lines changed

lib/private/SystemTag/SystemTagObjectMapper.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,23 +77,25 @@ public function getTagIdsForObjects($objIds, string $objectType): array {
7777
->from(self::RELATION_TABLE)
7878
->where($query->expr()->in('objectid', $query->createParameter('objectids')))
7979
->andWhere($query->expr()->eq('objecttype', $query->createParameter('objecttype')))
80-
->setParameter('objectids', $objIds, IQueryBuilder::PARAM_STR_ARRAY)
8180
->setParameter('objecttype', $objectType)
8281
->addOrderBy('objectid', 'ASC')
8382
->addOrderBy('systemtagid', 'ASC');
84-
83+
$chunks = array_chunk($objIds, 900, false);
8584
$mapping = [];
8685
foreach ($objIds as $objId) {
8786
$mapping[$objId] = [];
8887
}
88+
foreach ($chunks as $chunk) {
89+
$query->setParameter('objectids', $chunk, IQueryBuilder::PARAM_STR_ARRAY);
90+
$result = $query->executeQuery();
91+
while ($row = $result->fetch()) {
92+
$objectId = $row['objectid'];
93+
$mapping[$objectId][] = $row['systemtagid'];
94+
}
8995

90-
$result = $query->execute();
91-
while ($row = $result->fetch()) {
92-
$objectId = $row['objectid'];
93-
$mapping[$objectId][] = $row['systemtagid'];
96+
$result->closeCursor();
9497
}
9598

96-
$result->closeCursor();
9799

98100
return $mapping;
99101
}

tests/lib/SystemTag/SystemTagObjectMapperTest.php

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,17 @@ public function testGetTagIdsForNoObjects() {
142142
$this->assertEquals([], $tagIdMapping);
143143
}
144144

145+
public function testGetTagIdsForALotOfObjects() {
146+
$ids = range(1, 10500);
147+
$tagIdMapping = $this->tagMapper->getTagIdsForObjects(
148+
$ids,
149+
'testtype'
150+
);
151+
152+
$this->assertEquals(10500, count($tagIdMapping));
153+
$this->assertEquals([$this->tag1->getId(), $this->tag2->getId()], $tagIdMapping[1]);
154+
}
155+
145156
public function testGetObjectsForTags() {
146157
$objectIds = $this->tagMapper->getObjectIdsForTags(
147158
[$this->tag1->getId(), $this->tag2->getId(), $this->tag3->getId()],
@@ -166,7 +177,7 @@ public function testGetObjectsForTagsLimit() {
166177
], $objectIds);
167178
}
168179

169-
180+
170181
public function testGetObjectsForTagsLimitWithMultipleTags() {
171182
$this->expectException(\InvalidArgumentException::class);
172183

@@ -190,7 +201,7 @@ public function testGetObjectsForTagsLimitOffset() {
190201
], $objectIds);
191202
}
192203

193-
204+
194205
public function testGetObjectsForNonExistingTag() {
195206
$this->expectException(\OCP\SystemTag\TagNotFoundException::class);
196207

@@ -228,7 +239,7 @@ public function testReAssignUnassignTags() {
228239
$this->assertTrue(true, 'No error when reassigning/unassigning');
229240
}
230241

231-
242+
232243
public function testAssignNonExistingTags() {
233244
$this->expectException(\OCP\SystemTag\TagNotFoundException::class);
234245

@@ -255,7 +266,7 @@ public function testAssignNonExistingTagInArray() {
255266
], $tagIdMapping, 'None of the tags got assigned');
256267
}
257268

258-
269+
259270
public function testUnassignNonExistingTags() {
260271
$this->expectException(\OCP\SystemTag\TagNotFoundException::class);
261272

@@ -386,7 +397,7 @@ public function testHaveTagAtLeastOneMatch() {
386397
);
387398
}
388399

389-
400+
390401
public function testHaveTagNonExisting() {
391402
$this->expectException(\OCP\SystemTag\TagNotFoundException::class);
392403

0 commit comments

Comments
 (0)