Skip to content

Commit

Permalink
cache calendar objects from calendarQuery
Browse files Browse the repository at this point in the history
Signed-off-by: Robin Appelman <[email protected]>
  • Loading branch information
icewind1991 authored and backportbot-nextcloud[bot] committed Aug 14, 2023
1 parent 234ae93 commit 526a140
Showing 1 changed file with 30 additions and 5 deletions.
35 changes: 30 additions & 5 deletions apps/dav/lib/CalDAV/CalDavBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ class CalDavBackend extends AbstractBackend implements SyncSupport, Subscription
private IConfig $config;
private bool $legacyEndpoint;
private string $dbObjectPropertiesTable = 'calendarobjects_props';
private array $cachedObjects = [];

public function __construct(IDBConnection $db,
Principal $principalBackend,
Expand Down Expand Up @@ -1112,6 +1113,10 @@ public function getDeletedCalendarObjectsByPrincipal(string $principalUri): arra
* @return array|null
*/
public function getCalendarObject($calendarId, $objectUri, int $calendarType = self::CALENDAR_TYPE_CALENDAR) {
$key = $calendarId . '::' . $objectUri . '::' . $calendarType;
if (isset($this->cachedObjects[$key])) {
return $this->cachedObjects[$key];
}
$query = $this->db->getQueryBuilder();
$query->select(['id', 'uri', 'lastmodified', 'etag', 'calendarid', 'size', 'calendardata', 'componenttype', 'classification', 'deleted_at'])
->from('calendarobjects')
Expand All @@ -1126,6 +1131,12 @@ public function getCalendarObject($calendarId, $objectUri, int $calendarType = s
return null;
}

$object = $this->rowToCalendarObject($row);
$this->cachedObjects[$key] = $object;
return $object;
}

private function rowToCalendarObject(array $row): array {
return [
'id' => $row['id'],
'uri' => $row['uri'],
Expand Down Expand Up @@ -1212,6 +1223,7 @@ public function getMultipleCalendarObjects($calendarId, array $uris, $calendarTy
* @return string
*/
public function createCalendarObject($calendarId, $objectUri, $calendarData, $calendarType = self::CALENDAR_TYPE_CALENDAR) {
$this->cachedObjects = [];
$extraData = $this->getDenormalizedData($calendarData);

return $this->atomic(function () use ($calendarId, $objectUri, $calendarData, $extraData, $calendarType) {
Expand Down Expand Up @@ -1306,6 +1318,7 @@ public function createCalendarObject($calendarId, $objectUri, $calendarData, $ca
* @return string
*/
public function updateCalendarObject($calendarId, $objectUri, $calendarData, $calendarType = self::CALENDAR_TYPE_CALENDAR) {
$this->cachedObjects = [];
$extraData = $this->getDenormalizedData($calendarData);

return $this->atomic(function () use ($calendarId, $objectUri, $calendarData, $extraData, $calendarType) {
Expand Down Expand Up @@ -1359,6 +1372,7 @@ public function updateCalendarObject($calendarId, $objectUri, $calendarData, $ca
* @throws Exception
*/
public function moveCalendarObject(int $sourceCalendarId, int $targetCalendarId, int $objectId, string $oldPrincipalUri, string $newPrincipalUri, int $calendarType = self::CALENDAR_TYPE_CALENDAR): bool {
$this->cachedObjects = [];
return $this->atomic(function () use ($sourceCalendarId, $targetCalendarId, $objectId, $oldPrincipalUri, $newPrincipalUri, $calendarType) {
$object = $this->getCalendarObjectById($oldPrincipalUri, $objectId);
if (empty($object)) {
Expand Down Expand Up @@ -1406,6 +1420,7 @@ public function moveCalendarObject(int $sourceCalendarId, int $targetCalendarId,
* @param int $classification
*/
public function setClassification($calendarObjectId, $classification) {
$this->cachedObjects = [];
if (!in_array($classification, [
self::CLASSIFICATION_PUBLIC, self::CLASSIFICATION_PRIVATE, self::CLASSIFICATION_CONFIDENTIAL
])) {
Expand All @@ -1430,6 +1445,7 @@ public function setClassification($calendarObjectId, $classification) {
* @return void
*/
public function deleteCalendarObject($calendarId, $objectUri, $calendarType = self::CALENDAR_TYPE_CALENDAR, bool $forceDeletePermanently = false) {
$this->cachedObjects = [];
$this->atomic(function () use ($calendarId, $objectUri, $calendarType, $forceDeletePermanently) {
$data = $this->getCalendarObject($calendarId, $objectUri, $calendarType);

Expand Down Expand Up @@ -1511,6 +1527,7 @@ public function deleteCalendarObject($calendarId, $objectUri, $calendarType = se
* @throws Forbidden
*/
public function restoreCalendarObject(array $objectData): void {
$this->cachedObjects = [];
$this->atomic(function () use ($objectData) {
$id = (int) $objectData['id'];
$restoreUri = str_replace("-deleted.ics", ".ics", $objectData['uri']);
Expand Down Expand Up @@ -1638,12 +1655,8 @@ public function calendarQuery($calendarId, array $filters, $calendarType = self:
}
}
}
$columns = ['uri'];
if ($requirePostFilter) {
$columns = ['uri', 'calendardata'];
}
$query = $this->db->getQueryBuilder();
$query->select($columns)
$query->select(['id', 'uri', 'lastmodified', 'etag', 'calendarid', 'size', 'calendardata', 'componenttype', 'classification', 'deleted_at'])
->from('calendarobjects')
->where($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId)))
->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter($calendarType)))
Expand All @@ -1664,6 +1677,11 @@ public function calendarQuery($calendarId, array $filters, $calendarType = self:

$result = [];
while ($row = $stmt->fetch()) {
// if we leave it as a blob we can't read it both from the post filter and the rowToCalendarObject
if (isset($row['calendardata'])) {
$row['calendardata'] = $this->readBlob($row['calendardata']);
}

if ($requirePostFilter) {
// validateFilterForObject will parse the calendar data
// catch parsing errors
Expand All @@ -1688,6 +1706,8 @@ public function calendarQuery($calendarId, array $filters, $calendarType = self:
}
}
$result[] = $row['uri'];
$key = $calendarId . '::' . $row['uri'] . '::' . $calendarType;
$this->cachedObjects[$key] = $this->rowToCalendarObject($row);
}

return $result;
Expand Down Expand Up @@ -2648,6 +2668,7 @@ public function getSchedulingObjects($principalUri) {
* @return void
*/
public function deleteSchedulingObject($principalUri, $objectUri) {
$this->cachedObjects = [];
$query = $this->db->getQueryBuilder();
$query->delete('schedulingobjects')
->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri)))
Expand All @@ -2664,6 +2685,7 @@ public function deleteSchedulingObject($principalUri, $objectUri) {
* @return void
*/
public function createSchedulingObject($principalUri, $objectUri, $objectData) {
$this->cachedObjects = [];
$query = $this->db->getQueryBuilder();
$query->insert('schedulingobjects')
->values([
Expand All @@ -2687,6 +2709,7 @@ public function createSchedulingObject($principalUri, $objectUri, $objectData) {
* @return void
*/
protected function addChange(int $calendarId, string $objectUri, int $operation, int $calendarType = self::CALENDAR_TYPE_CALENDAR): void {
$this->cachedObjects = [];
$table = $calendarType === self::CALENDAR_TYPE_CALENDAR ? 'calendars': 'calendarsubscriptions';

$this->atomic(function () use ($calendarId, $objectUri, $operation, $calendarType, $table) {
Expand Down Expand Up @@ -2933,6 +2956,7 @@ public function applyShareAcl(int $resourceId, array $acl): array {
* @param int $calendarType
*/
public function updateProperties($calendarId, $objectUri, $calendarData, $calendarType = self::CALENDAR_TYPE_CALENDAR) {
$this->cachedObjects = [];
$this->atomic(function () use ($calendarId, $objectUri, $calendarData, $calendarType) {
$objectId = $this->getCalendarObjectId($calendarId, $objectUri, $calendarType);

Expand Down Expand Up @@ -3097,6 +3121,7 @@ protected function readCalendarData($objectData) {
* @param int $objectId
*/
protected function purgeProperties($calendarId, $objectId) {
$this->cachedObjects = [];
$query = $this->db->getQueryBuilder();
$query->delete($this->dbObjectPropertiesTable)
->where($query->expr()->eq('objectid', $query->createNamedParameter($objectId)))
Expand Down

0 comments on commit 526a140

Please sign in to comment.