Skip to content

Commit 4d83e8b

Browse files
author
Carl Schwan
committed
perf(cards): fetch all cards at once
Instead of one by one
1 parent 0ed8b21 commit 4d83e8b

3 files changed

Lines changed: 45 additions & 17 deletions

File tree

lib/Db/CardMapper.php

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,11 @@ public function find($id, bool $enhance = true): Card {
131131
return $card;
132132
}
133133

134-
public function findAll($stackId, $limit = null, $offset = null, $since = -1) {
134+
/**
135+
* @return Card[]
136+
* @throws \OCP\DB\Exception
137+
*/
138+
public function findAll(int $stackId, int $limit = null, int $offset = null, int $since = -1) {
135139
$qb = $this->db->getQueryBuilder();
136140
$qb->select('*')
137141
->from('deck_cards')
@@ -146,6 +150,32 @@ public function findAll($stackId, $limit = null, $offset = null, $since = -1) {
146150
return $this->findEntities($qb);
147151
}
148152

153+
/**
154+
* @param int[] $stackIds
155+
* @return array<int, Card[]>
156+
* @throws \OCP\DB\Exception
157+
*/
158+
public function findAllForStacks(array $stackIds, int $limit = null, int $offset = null, int $since = -1): array {
159+
$qb = $this->db->getQueryBuilder();
160+
$qb->select('*')
161+
->from('deck_cards')
162+
->where($qb->expr()->in('stack_id', $qb->createNamedParameter($stackIds, IQueryBuilder::PARAM_INT_ARRAY)))
163+
->andWhere($qb->expr()->eq('archived', $qb->createNamedParameter(false, IQueryBuilder::PARAM_BOOL)))
164+
->andWhere($qb->expr()->eq('deleted_at', $qb->createNamedParameter(0, IQueryBuilder::PARAM_INT)))
165+
->andWhere($qb->expr()->gt('last_modified', $qb->createNamedParameter($since, IQueryBuilder::PARAM_INT)))
166+
->setMaxResults($limit)
167+
->setFirstResult($offset)
168+
->orderBy('order')
169+
->addOrderBy('id');
170+
171+
$rawCards = $this->findEntities($qb);
172+
$cards = array_fill_keys($stackIds, 0);
173+
foreach ($rawCards as $card) {
174+
$cards[$card->getId()][] = $card;
175+
}
176+
return $cards;
177+
}
178+
149179
public function queryCardsByBoard(int $boardId): IQueryBuilder {
150180
$qb = $this->db->getQueryBuilder();
151181
$qb->select('c.*')

lib/Db/StackMapper.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,10 @@ public function findStackFromCardId($cardId): ?Stack {
7676
}
7777

7878
/**
79-
* @param numeric $boardId
80-
* @param int|null $limit
81-
* @param int|null $offset
8279
* @return Stack[]
8380
* @throws \OCP\DB\Exception
8481
*/
85-
public function findAll($boardId, $limit = null, $offset = null): array {
82+
public function findAll(int $boardId, int $limit = null, int $offset = null): array {
8683
$qb = $this->db->getQueryBuilder();
8784
$qb->select('*')
8885
->from($this->getTableName())

lib/Service/StackService.php

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -75,19 +75,20 @@ public function __construct(
7575
$this->stackServiceValidator = $stackServiceValidator;
7676
}
7777

78-
private function enrichStackWithCards($stack, $since = -1) {
79-
$cards = $this->cardMapper->findAll($stack->getId(), null, null, $since);
78+
private function enrichStacksWithCards(array $stacks, $since = -1): void {
79+
$cardsByStackId = $this->cardMapper->findAllForStacks(array_map(fn (Stack $stack) => $stack->getId(), $stacks), null, null, $since);
8080

81-
if (\count($cards) === 0) {
82-
return;
83-
}
84-
85-
$stack->setCards($this->cardService->enrichCards($cards));
86-
}
81+
foreach ($cardsByStackId as $stackId => $cards) {
82+
if (\count($cards) === 0) {
83+
return;
84+
}
8785

88-
private function enrichStacksWithCards($stacks, $since = -1) {
89-
foreach ($stacks as $stack) {
90-
$this->enrichStackWithCards($stack, $since);
86+
foreach ($stacks as $stack) {
87+
if ($stack->getId() === $stackId) {
88+
$stack->setCards($this->cardService->enrichCards($cards));
89+
break;
90+
}
91+
}
9192
}
9293
}
9394

@@ -247,7 +248,7 @@ public function delete($id) {
247248
);
248249
$this->changeHelper->boardChanged($stack->getBoardId());
249250
$this->eventDispatcher->dispatchTyped(new BoardUpdatedEvent($stack->getBoardId()));
250-
$this->enrichStackWithCards($stack);
251+
$this->enrichStacksWithCards([$stack]);
251252

252253
return $stack;
253254
}

0 commit comments

Comments
 (0)