Time some fn

This commit is contained in:
Ming Ming 2022-06-06 18:09:11 +08:00
parent f01ab2f648
commit 5d8e5b25b3

View file

@ -123,31 +123,37 @@ class FileCacheUpdater {
} }
} }
Future<void> _cacheRemote(Account account, File dir, List<File> remote) { Future<void> _cacheRemote(
return appDb.use( Account account, File dir, List<File> remote) async {
(db) => db.transaction( final s = Stopwatch()..start();
[AppDb.dirStoreName, AppDb.file2StoreName], idbModeReadWrite), try {
(transaction) async { await appDb.use(
final dirStore = transaction.objectStore(AppDb.dirStoreName); (db) => db.transaction(
final fileStore = transaction.objectStore(AppDb.file2StoreName); [AppDb.dirStoreName, AppDb.file2StoreName], idbModeReadWrite),
(transaction) async {
final dirStore = transaction.objectStore(AppDb.dirStoreName);
final fileStore = transaction.objectStore(AppDb.file2StoreName);
// add files to db // add files to db
await Future.wait(remote.map((f) => fileStore.put( await Future.wait(remote.map((f) => fileStore.put(
AppDbFile2Entry.fromFile(account, f).toJson(), AppDbFile2Entry.fromFile(account, f).toJson(),
AppDbFile2Entry.toPrimaryKeyForFile(account, f)))); AppDbFile2Entry.toPrimaryKeyForFile(account, f))));
// results from remote also contain the dir itself // results from remote also contain the dir itself
final resultGroup = final resultGroup =
remote.groupListsBy((f) => f.compareServerIdentity(dir)); remote.groupListsBy((f) => f.compareServerIdentity(dir));
final remoteDir = resultGroup[true]!.first; final remoteDir = resultGroup[true]!.first;
final remoteChildren = resultGroup[false] ?? []; final remoteChildren = resultGroup[false] ?? [];
// add dir to db // add dir to db
await dirStore.put( await dirStore.put(
AppDbDirEntry.fromFiles(account, remoteDir, remoteChildren) AppDbDirEntry.fromFiles(account, remoteDir, remoteChildren)
.toJson(), .toJson(),
AppDbDirEntry.toPrimaryKeyForDir(account, remoteDir)); AppDbDirEntry.toPrimaryKeyForDir(account, remoteDir));
}, },
); );
} finally {
_log.info("[_cacheRemote] Elapsed time: ${s.elapsedMilliseconds}ms");
}
} }
/// Remove extra entries from local cache based on remote contents /// Remove extra entries from local cache based on remote contents