Skip to content

Commit

Permalink
Add chunking in SystemTagObjectMapper::getTagIdsForObjects
Browse files Browse the repository at this point in the history
This avoids crashing on Oracle with more than 1000 objects

Signed-off-by: Côme Chilliet <come.chilliet@nextcloud.com>
  • Loading branch information
come-nc committed Mar 13, 2023
1 parent 4b79e75 commit 9b1d779
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
16 changes: 9 additions & 7 deletions lib/private/SystemTag/SystemTagObjectMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,23 +77,25 @@ public function getTagIdsForObjects($objIds, string $objectType): array {
->from(self::RELATION_TABLE)
->where($query->expr()->in('objectid', $query->createParameter('objectids')))
->andWhere($query->expr()->eq('objecttype', $query->createParameter('objecttype')))
->setParameter('objectids', $objIds, IQueryBuilder::PARAM_STR_ARRAY)
->setParameter('objecttype', $objectType)
->addOrderBy('objectid', 'ASC')
->addOrderBy('systemtagid', 'ASC');

$chunks = array_chunk($objIds, 900, false);
$mapping = [];
foreach ($objIds as $objId) {
$mapping[$objId] = [];
}
foreach ($chunks as $chunk) {
$query->setParameter('objectids', $chunk, IQueryBuilder::PARAM_STR_ARRAY);
$result = $query->executeQuery();
while ($row = $result->fetch()) {
$objectId = $row['objectid'];
$mapping[$objectId][] = $row['systemtagid'];
}

$result = $query->execute();
while ($row = $result->fetch()) {
$objectId = $row['objectid'];
$mapping[$objectId][] = $row['systemtagid'];
$result->closeCursor();
}

$result->closeCursor();

return $mapping;
}
Expand Down
21 changes: 16 additions & 5 deletions tests/lib/SystemTag/SystemTagObjectMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,17 @@ public function testGetTagIdsForNoObjects() {
$this->assertEquals([], $tagIdMapping);
}

public function testGetTagIdsForALotOfObjects() {
$ids = range(1, 10500);
$tagIdMapping = $this->tagMapper->getTagIdsForObjects(
$ids,
'testtype'
);

$this->assertEquals(10500, count($tagIdMapping));
$this->assertEquals([$this->tag1->getId(), $this->tag2->getId()], $tagIdMapping[1]);
}

public function testGetObjectsForTags() {
$objectIds = $this->tagMapper->getObjectIdsForTags(
[$this->tag1->getId(), $this->tag2->getId(), $this->tag3->getId()],
Expand All @@ -165,7 +176,7 @@ public function testGetObjectsForTagsLimit() {
], $objectIds);
}


public function testGetObjectsForTagsLimitWithMultipleTags() {
$this->expectException(\InvalidArgumentException::class);

Expand All @@ -189,7 +200,7 @@ public function testGetObjectsForTagsLimitOffset() {
], $objectIds);
}


public function testGetObjectsForNonExistingTag() {
$this->expectException(\OCP\SystemTag\TagNotFoundException::class);

Expand Down Expand Up @@ -227,7 +238,7 @@ public function testReAssignUnassignTags() {
$this->assertTrue(true, 'No error when reassigning/unassigning');
}


public function testAssignNonExistingTags() {
$this->expectException(\OCP\SystemTag\TagNotFoundException::class);

Expand All @@ -254,7 +265,7 @@ public function testAssignNonExistingTagInArray() {
], $tagIdMapping, 'None of the tags got assigned');
}


public function testUnassignNonExistingTags() {
$this->expectException(\OCP\SystemTag\TagNotFoundException::class);

Expand Down Expand Up @@ -385,7 +396,7 @@ public function testHaveTagAtLeastOneMatch() {
);
}


public function testHaveTagNonExisting() {
$this->expectException(\OCP\SystemTag\TagNotFoundException::class);

Expand Down

0 comments on commit 9b1d779

Please sign in to comment.