From d5d69144de336ba7dad9d3be438f3f8da37df659 Mon Sep 17 00:00:00 2001 From: Ming Ming Date: Sun, 4 Dec 2022 20:06:51 +0800 Subject: [PATCH] Remove unused code --- app/lib/entity/file/data_source.dart | 173 -------------------- app/lib/entity/file/file_cache_manager.dart | 8 +- 2 files changed, 1 insertion(+), 180 deletions(-) diff --git a/app/lib/entity/file/data_source.dart b/app/lib/entity/file/data_source.dart index 89dc6474..ad800240 100644 --- a/app/lib/entity/file/data_source.dart +++ b/app/lib/entity/file/data_source.dart @@ -572,7 +572,6 @@ class FileCachedDataSource implements FileDataSource { FileCachedDataSource( this._c, { this.shouldCheckCache = false, - this.forwardCacheManager, }) : _sqliteDbSrc = FileSqliteDbDataSource(_c); @override @@ -582,7 +581,6 @@ class FileCachedDataSource implements FileDataSource { cacheSrc: _sqliteDbSrc, remoteSrc: _remoteSrc, shouldCheckCache: shouldCheckCache, - forwardCacheManager: forwardCacheManager, ); final cache = await cacheLoader(account, dir); if (cacheLoader.isGood) { @@ -741,7 +739,6 @@ class FileCachedDataSource implements FileDataSource { final DiContainer _c; final bool shouldCheckCache; - final FileForwardCacheManager? forwardCacheManager; final _remoteSrc = const FileWebdavDataSource(); final FileSqliteDbDataSource _sqliteDbSrc; @@ -749,176 +746,6 @@ class FileCachedDataSource implements FileDataSource { static final _log = Logger("entity.file.data_source.FileCachedDataSource"); } -/// Forward cache for listing AppDb dirs -/// -/// It's very expensive to list a dir and its sub-dirs one by one in multiple -/// queries. This class will instead query every sub-dirs when a new dir is -/// passed to us in one transaction. For this reason, this should only be used -/// when it's necessary to query everything -class FileForwardCacheManager { - FileForwardCacheManager(this._c, Map knownFiles) { - _fileCache.addAll(knownFiles); - } - - static bool require(DiContainer c) => true; - - /// Transform a list of files to a map suitable to be passed as the - /// [knownFiles] argument - static Map prepareFileMap(List knownFiles) => knownFiles - .where((f) => f.fileId != null) - .map((e) => MapEntry(e.fileId!, e)) - .run((obj) => Map.fromEntries(obj)); - - Future> list(Account account, File dir) async { - // check cache - var childFileIds = _dirCache[dir.strippedPathWithEmpty]; - if (childFileIds == null) { - _log.info( - "[list] No cache and querying everything under ${logFilename(dir.path)}"); - await _cacheDir(account, dir); - childFileIds = _dirCache[dir.strippedPathWithEmpty]; - if (childFileIds == null) { - throw CacheNotFoundException("No entry: ${dir.path}"); - } - } else { - _log.fine("[list] Returning data from cache: ${logFilename(dir.path)}"); - } - return _listByFileIds(dir, childFileIds); - } - - Future _cacheDir(Account account, File dir) async { - await _c.sqliteDb.use((db) async { - final dbAccount = await db.accountOf(account); - final dirCache = >{}; - await _fillDirCacheForDir( - db, dirCache, dbAccount, null, dir.fileId, dir.strippedPathWithEmpty); - _log.info( - "[_cacheDir] Cached ${dirCache.length} dirs under ${logFilename(dir.path)}"); - await _fillFileCache( - db, account, dbAccount, dirCache.values.flatten().toSet()); - _dirCache.addAll(dirCache); - }); - } - - Future _fillDirCacheForDir( - sql.SqliteDb db, - Map> dirCache, - sql.Account dbAccount, - int? dirRowId, - int? dirFileId, - String dirRelativePath) async { - // get rowId - final myDirRowId = dirRowId ?? - await _queryFileIdOfDir(db, dbAccount, dirFileId, dirRelativePath); - if (myDirRowId == null) { - // no cache - return; - } - final children = await _queryChildOfDir(db, dbAccount, myDirRowId); - if (children.isEmpty) { - // no cache - return; - } - final childFileIds = children.map((c) => c.fileId).toList(); - dirCache[dirRelativePath] = childFileIds; - - // recursively fill child dirs - for (final c in children - .where((c) => c.rowId != myDirRowId && c.isCollection == true)) { - await _fillDirCacheForDir( - db, dirCache, dbAccount, c.rowId, c.fileId, c.relativePath); - } - } - - Future _fillFileCache(sql.SqliteDb db, Account account, - sql.Account dbAccount, Iterable fileIds) async { - final needQuery = fileIds.where((id) => !_fileCache.containsKey(id)); - if (needQuery.isNotEmpty) { - _log.info("[_fillFileCache] ${needQuery.length} files need querying"); - final dbFiles = - await db.completeFilesByFileIds(needQuery, sqlAccount: dbAccount); - for (final f in await dbFiles.convertToAppFile(account)) { - _fileCache[f.fileId!] = f; - } - _log.info("[_fillFileCache] Cached ${dbFiles.length} files"); - } else { - _log.info("[_fillFileCache] 0 files need querying"); - } - } - - List _listByFileIds(File dir, List childFileIds) { - return childFileIds.map((id) { - try { - return _fileCache[id]!; - } catch (_) { - _log.warning( - "[_listByFileIds] Missing file ($id) in db for dir: ${logFilename(dir.path)}"); - throw CacheNotFoundException("No entry for dir child: $id"); - } - }).toList(); - } - - Future _queryFileIdOfDir(sql.SqliteDb db, sql.Account dbAccount, - int? dirFileId, String dirRelativePath) async { - final dirQuery = db.queryFiles().run((q) { - q - ..setQueryMode(sql.FilesQueryMode.expression, - expressions: [db.files.rowId]) - ..setSqlAccount(dbAccount); - if (dirFileId != null) { - q.byFileId(dirFileId); - } else { - q.byRelativePath(dirRelativePath); - } - return q.build()..limit(1); - }); - return await dirQuery.map((r) => r.read(db.files.rowId)).getSingleOrNull(); - } - - Future> _queryChildOfDir( - sql.SqliteDb db, sql.Account dbAccount, int dirRowId) async { - final childQuery = db.selectOnly(db.files).join([ - sql.innerJoin(db.dirFiles, db.dirFiles.child.equalsExp(db.files.rowId), - useColumns: false), - sql.innerJoin( - db.accountFiles, db.accountFiles.file.equalsExp(db.files.rowId), - useColumns: false), - ]) - ..addColumns([ - db.files.rowId, - db.files.fileId, - db.files.isCollection, - db.accountFiles.relativePath, - ]) - ..where(db.dirFiles.dir.equals(dirRowId)) - ..where(db.accountFiles.account.equals(dbAccount.rowId)); - return await childQuery - .map((r) => _ForwardCacheQueryChildResult( - r.read(db.files.rowId)!, - r.read(db.files.fileId)!, - r.read(db.accountFiles.relativePath)!, - r.read(db.files.isCollection), - )) - .get(); - } - - final DiContainer _c; - final _dirCache = >{}; - final _fileCache = {}; - - static final _log = Logger("entity.file.data_source.FileForwardCacheManager"); -} - -class _ForwardCacheQueryChildResult { - const _ForwardCacheQueryChildResult( - this.rowId, this.fileId, this.relativePath, this.isCollection); - - final int rowId; - final int fileId; - final String relativePath; - final bool? isCollection; -} - bool _validateFile(File f) { // See: https://gitlab.com/nkming2/nc-photos/-/issues/9 return f.lastModified != null; diff --git a/app/lib/entity/file/file_cache_manager.dart b/app/lib/entity/file/file_cache_manager.dart index d2bc1764..44a829fb 100644 --- a/app/lib/entity/file/file_cache_manager.dart +++ b/app/lib/entity/file/file_cache_manager.dart @@ -22,7 +22,6 @@ class FileCacheLoader { required this.cacheSrc, required this.remoteSrc, this.shouldCheckCache = false, - this.forwardCacheManager, }) : assert(require(_c)); static bool require(DiContainer c) => DiContainer.has(c, DiType.fileRepo); @@ -33,11 +32,7 @@ class FileCacheLoader { Future?> call(Account account, File dir) async { List? cache; try { - if (forwardCacheManager != null) { - cache = await forwardCacheManager!.list(account, dir); - } else { - cache = await cacheSrc.list(account, dir); - } + cache = await cacheSrc.list(account, dir); // compare the cached root final cacheEtag = cache.firstWhere((f) => f.compareServerIdentity(dir)).etag!; @@ -83,7 +78,6 @@ class FileCacheLoader { final FileWebdavDataSource remoteSrc; final FileDataSource cacheSrc; final bool shouldCheckCache; - final FileForwardCacheManager? forwardCacheManager; var _isGood = false; String? _remoteEtag;