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

[stable27] fix(dav): Reduce CalDAV backend memory footprint #41245

Merged
merged 1 commit into from
Nov 15, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
64 changes: 34 additions & 30 deletions apps/dav/lib/CalDAV/CalDavBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -281,14 +281,15 @@ public function getDeletedCalendars(int $deletedBefore): array {
->where($qb->expr()->isNotNull('deleted_at'))
->andWhere($qb->expr()->lt('deleted_at', $qb->createNamedParameter($deletedBefore)));
$result = $qb->executeQuery();
$raw = $result->fetchAll();
$result->closeCursor();
return array_map(function ($row) {
return [
$calendars = [];
while (($row = $result->fetch()) !== false) {
$calendars[] = [
'id' => (int) $row['id'],
'deleted_at' => (int) $row['deleted_at'],
];
}, $raw);
}
$result->closeCursor();
return $calendars;
}

/**
Expand Down Expand Up @@ -1011,7 +1012,7 @@ public function getCalendarObjects($calendarId, $calendarType = self::CALENDAR_T
$stmt = $query->executeQuery();

$result = [];
foreach ($stmt->fetchAll() as $row) {
while (($row = $stmt->fetch()) !== false) {
$result[] = [
'id' => $row['id'],
'uri' => $row['uri'],
Expand All @@ -1038,7 +1039,7 @@ public function getDeletedCalendarObjects(int $deletedBefore): array {
$stmt = $query->executeQuery();

$result = [];
foreach ($stmt->fetchAll() as $row) {
while (($row = $stmt->fetch()) !== false) {
$result[] = [
'id' => $row['id'],
'uri' => $row['uri'],
Expand Down Expand Up @@ -1925,13 +1926,15 @@ public function search(array $calendarInfo, $pattern, array $searchProperties,
}

$result = $outerQuery->executeQuery();
$calendarObjects = array_filter($result->fetchAll(), function (array $row) use ($options) {
$calendarObjects = [];
while (($row = $result->fetch()) !== false) {
$start = $options['timerange']['start'] ?? null;
$end = $options['timerange']['end'] ?? null;

if ($start === null || !($start instanceof DateTimeInterface) || $end === null || !($end instanceof DateTimeInterface)) {
// No filter required
return true;
$calendarObjects[] = $row;
continue;
}

$isValid = $this->validateFilterForObject($row, [
Expand All @@ -1956,8 +1959,10 @@ public function search(array $calendarInfo, $pattern, array $searchProperties,
// Put the stream back to the beginning so it can be read another time
rewind($row['calendardata']);
}
return $isValid;
});
if ($isValid) {
$calendarObjects[] = $row;
}
}
$result->closeCursor();

return array_map(function ($o) use ($options) {
Expand Down Expand Up @@ -2157,28 +2162,28 @@ public function searchPrincipalUri(string $principalUri,
}

$result = $calendarObjectIdQuery->executeQuery();
$matches = $result->fetchAll();
$matches = [];
while (($row = $result->fetch()) !== false) {
$matches[] = (int) $row['objectid'];
}
$result->closeCursor();
$matches = array_map(static function (array $match):int {
return (int) $match['objectid'];
}, $matches);

$query = $this->db->getQueryBuilder();
$query->select('calendardata', 'uri', 'calendarid', 'calendartype')
->from('calendarobjects')
->where($query->expr()->in('id', $query->createNamedParameter($matches, IQueryBuilder::PARAM_INT_ARRAY)));

$result = $query->executeQuery();
$calendarObjects = $result->fetchAll();
$result->closeCursor();

return array_map(function (array $array): array {
$calendarObjects = [];
while (($array = $result->fetch()) !== false) {
$array['calendarid'] = (int)$array['calendarid'];
$array['calendartype'] = (int)$array['calendartype'];
$array['calendardata'] = $this->readBlob($array['calendardata']);

return $array;
}, $calendarObjects);
$calendarObjects[] = $array;
}
$result->closeCursor();
return $calendarObjects;
}, $this->db);
}

Expand Down Expand Up @@ -2656,9 +2661,9 @@ public function getSchedulingObjects($principalUri) {
->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri)))
->executeQuery();

$result = [];
foreach ($stmt->fetchAll() as $row) {
$result[] = [
$results = [];
while (($row = $stmt->fetch()) !== false) {
$results[] = [
'calendardata' => $row['calendardata'],
'uri' => $row['uri'],
'lastmodified' => $row['lastmodified'],
Expand All @@ -2668,7 +2673,7 @@ public function getSchedulingObjects($principalUri) {
}
$stmt->closeCursor();

return $result;
return $results;
}

/**
Expand Down Expand Up @@ -3046,14 +3051,13 @@ public function deleteAllBirthdayCalendars() {
->where($query->expr()->eq('uri', $query->createNamedParameter(BirthdayService::BIRTHDAY_CALENDAR_URI)))
->executeQuery();

$ids = $result->fetchAll();
$result->closeCursor();
foreach ($ids as $id) {
while (($row = $result->fetch()) !== false) {
$this->deleteCalendar(
$id['id'],
$row['id'],
true // No data to keep in the trashbin, if the user re-enables then we regenerate
);
}
$result->closeCursor();
}, $this->db);
}

Expand All @@ -3070,7 +3074,7 @@ public function purgeAllCachedEventsForSubscription($subscriptionId) {
$stmt = $query->executeQuery();

$uris = [];
foreach ($stmt->fetchAll() as $row) {
while (($row = $stmt->fetch()) !== false) {
$uris[] = $row['uri'];
}
$stmt->closeCursor();
Expand Down