Skip to content

Commit 5baab5c

Browse files
committed
fix: reduce memory usage for fetching cached mount into
Signed-off-by: Robin Appelman <robin@icewind.nl>
1 parent 812a4d5 commit 5baab5c

File tree

1 file changed

+21
-23
lines changed

1 file changed

+21
-23
lines changed

lib/private/Files/Config/UserMountCache.php

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
use OC\User\LazyUser;
1111
use OCP\Cache\CappedMemoryCache;
12+
use OCP\DB\IResult;
1213
use OCP\DB\QueryBuilder\IQueryBuilder;
1314
use OCP\Diagnostics\IEventLogger;
1415
use OCP\Files\Config\ICachedMountFileInfo;
@@ -190,6 +191,19 @@ private function removeFromCache(ICachedMountInfo $mount) {
190191
$query->execute();
191192
}
192193

194+
/**
195+
* @param IResult $result
196+
* @return CachedMountInfo[]
197+
*/
198+
private function fetchMountInfo(IResult $result, ?callable $pathCallback = null): array {
199+
$mounts = [];
200+
while ($row = $result->fetch()) {
201+
$mount = $this->dbRowToMountInfo($row, $pathCallback);
202+
$mounts[] = $mount;
203+
}
204+
return array_filter($mounts);
205+
}
206+
193207
/**
194208
* @param array $row
195209
* @param (callable(CachedMountInfo): string)|null $pathCallback
@@ -239,19 +253,11 @@ public function getMountsForUser(IUser $user) {
239253
->from('mounts', 'm')
240254
->where($builder->expr()->eq('user_id', $builder->createNamedParameter($userUID)));
241255

242-
$result = $query->execute();
243-
$rows = $result->fetchAll();
244-
$result->closeCursor();
245-
246-
/** @var array<string, ICachedMountInfo> $mounts */
247-
$mounts = [];
248-
foreach ($rows as $row) {
249-
$mount = $this->dbRowToMountInfo($row, [$this, 'getInternalPathForMountInfo']);
250-
if ($mount !== null) {
251-
$mounts[$mount->getKey()] = $mount;
252-
}
253-
}
254-
$this->mountsForUsers[$userUID] = $mounts;
256+
$mounts = $this->fetchMountInfo($query->execute(), [$this, 'getInternalPathForMountInfo']);
257+
$keys = array_map(function(ICachedMountInfo $mount) {
258+
return $mount->getKey();
259+
}, $mounts);
260+
$this->mountsForUsers[$userUID] = array_combine($keys, $mounts);
255261
}
256262
return $this->mountsForUsers[$userUID];
257263
}
@@ -284,11 +290,7 @@ public function getMountsForStorageId($numericStorageId, $user = null) {
284290
$query->andWhere($builder->expr()->eq('user_id', $builder->createNamedParameter($user)));
285291
}
286292

287-
$result = $query->execute();
288-
$rows = $result->fetchAll();
289-
$result->closeCursor();
290-
291-
return array_filter(array_map([$this, 'dbRowToMountInfo'], $rows));
293+
return $this->fetchMountInfo($query->executeQuery());
292294
}
293295

294296
/**
@@ -302,11 +304,7 @@ public function getMountsForRootId($rootFileId) {
302304
->innerJoin('m', 'filecache', 'f', $builder->expr()->eq('m.root_id', 'f.fileid'))
303305
->where($builder->expr()->eq('root_id', $builder->createNamedParameter($rootFileId, IQueryBuilder::PARAM_INT)));
304306

305-
$result = $query->execute();
306-
$rows = $result->fetchAll();
307-
$result->closeCursor();
308-
309-
return array_filter(array_map([$this, 'dbRowToMountInfo'], $rows));
307+
return $this->fetchMountInfo($query->executeQuery());
310308
}
311309

312310
/**

0 commit comments

Comments
 (0)