mirror of
https://gitlab.com/nkming2/nc-photos.git
synced 2025-02-24 10:28:50 +01:00
Fix AppDb.use returns before db is closed
This commit is contained in:
parent
5554c32781
commit
0c23b8e7e4
16 changed files with 541 additions and 477 deletions
|
@ -34,13 +34,19 @@ class AppDb {
|
|||
/// This function guarantees that:
|
||||
/// 1) Database is always closed after [fn] exits, even with an error
|
||||
/// 2) Only at most 1 database instance being opened at any time
|
||||
Future<T> use<T>(FutureOr<T> Function(Database db) fn) async {
|
||||
Future<T> use<T>(Transaction Function(Database db) transactionBuilder,
|
||||
FutureOr<T> Function(Transaction transaction) fn) async {
|
||||
// make sure only one client is opening the db
|
||||
return await platform.Lock.synchronized(k.appDbLockId, () async {
|
||||
final db = await _open();
|
||||
Transaction? transaction;
|
||||
try {
|
||||
return await fn(db);
|
||||
transaction = transactionBuilder(db);
|
||||
return await fn(transaction);
|
||||
} finally {
|
||||
if (transaction != null) {
|
||||
await transaction.completed;
|
||||
}
|
||||
db.close();
|
||||
}
|
||||
});
|
||||
|
|
|
@ -398,34 +398,36 @@ class AlbumAppDbDataSource implements AlbumDataSource {
|
|||
@override
|
||||
get(Account account, File albumFile) {
|
||||
_log.info("[get] ${albumFile.path}");
|
||||
return appDb.use((db) async {
|
||||
final transaction = db.transaction(AppDb.albumStoreName, idbModeReadOnly);
|
||||
final store = transaction.objectStore(AppDb.albumStoreName);
|
||||
final index = store.index(AppDbAlbumEntry.indexName);
|
||||
final path = AppDbAlbumEntry.toPathFromFile(account, albumFile);
|
||||
final range = KeyRange.bound([path, 0], [path, int_util.int32Max]);
|
||||
final List results = await index.getAll(range);
|
||||
if (results.isNotEmpty == true) {
|
||||
final entries = results.map((e) =>
|
||||
AppDbAlbumEntry.fromJson(e.cast<String, dynamic>(), account));
|
||||
if (entries.length > 1) {
|
||||
final items = entries.map((e) {
|
||||
_log.info("[get] ${e.path}[${e.index}]");
|
||||
return AlbumStaticProvider.of(e.album).items;
|
||||
}).reduce((value, element) => value + element);
|
||||
return entries.first.album.copyWith(
|
||||
lastUpdated: OrNull(null),
|
||||
provider: AlbumStaticProvider.of(entries.first.album).copyWith(
|
||||
items: items,
|
||||
),
|
||||
);
|
||||
return appDb.use(
|
||||
(db) => db.transaction(AppDb.albumStoreName, idbModeReadOnly),
|
||||
(transaction) async {
|
||||
final store = transaction.objectStore(AppDb.albumStoreName);
|
||||
final index = store.index(AppDbAlbumEntry.indexName);
|
||||
final path = AppDbAlbumEntry.toPathFromFile(account, albumFile);
|
||||
final range = KeyRange.bound([path, 0], [path, int_util.int32Max]);
|
||||
final List results = await index.getAll(range);
|
||||
if (results.isNotEmpty == true) {
|
||||
final entries = results.map((e) =>
|
||||
AppDbAlbumEntry.fromJson(e.cast<String, dynamic>(), account));
|
||||
if (entries.length > 1) {
|
||||
final items = entries.map((e) {
|
||||
_log.info("[get] ${e.path}[${e.index}]");
|
||||
return AlbumStaticProvider.of(e.album).items;
|
||||
}).reduce((value, element) => value + element);
|
||||
return entries.first.album.copyWith(
|
||||
lastUpdated: OrNull(null),
|
||||
provider: AlbumStaticProvider.of(entries.first.album).copyWith(
|
||||
items: items,
|
||||
),
|
||||
);
|
||||
} else {
|
||||
return entries.first.album;
|
||||
}
|
||||
} else {
|
||||
return entries.first.album;
|
||||
throw CacheNotFoundException("No entry: $path");
|
||||
}
|
||||
} else {
|
||||
throw CacheNotFoundException("No entry: $path");
|
||||
}
|
||||
});
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
|
@ -437,12 +439,13 @@ class AlbumAppDbDataSource implements AlbumDataSource {
|
|||
@override
|
||||
update(Account account, Album album) {
|
||||
_log.info("[update] ${album.albumFile!.path}");
|
||||
return appDb.use((db) async {
|
||||
final transaction =
|
||||
db.transaction(AppDb.albumStoreName, idbModeReadWrite);
|
||||
final store = transaction.objectStore(AppDb.albumStoreName);
|
||||
await _cacheAlbum(store, account, album);
|
||||
});
|
||||
return appDb.use(
|
||||
(db) => db.transaction(AppDb.albumStoreName, idbModeReadWrite),
|
||||
(transaction) async {
|
||||
final store = transaction.objectStore(AppDb.albumStoreName);
|
||||
await _cacheAlbum(store, account, album);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
|
@ -492,43 +495,45 @@ class AlbumCachedDataSource implements AlbumDataSource {
|
|||
|
||||
@override
|
||||
cleanUp(Account account, String rootDir, List<File> albumFiles) async {
|
||||
appDb.use((db) async {
|
||||
final transaction =
|
||||
db.transaction(AppDb.albumStoreName, idbModeReadWrite);
|
||||
final store = transaction.objectStore(AppDb.albumStoreName);
|
||||
final index = store.index(AppDbAlbumEntry.indexName);
|
||||
final rootPath = AppDbAlbumEntry.toPath(account, rootDir);
|
||||
final range = KeyRange.bound(
|
||||
["$rootPath/", 0], ["$rootPath/\uffff", int_util.int32Max]);
|
||||
final danglingKeys = await index
|
||||
// get all albums for this account
|
||||
.openKeyCursor(range: range, autoAdvance: true)
|
||||
.map((cursor) => Tuple2((cursor.key as List)[0], cursor.primaryKey))
|
||||
// and pick the dangling ones
|
||||
.where((pair) => !albumFiles.any(
|
||||
(f) => pair.item1 == AppDbAlbumEntry.toPathFromFile(account, f)))
|
||||
// map to primary keys
|
||||
.map((pair) => pair.item2)
|
||||
.toList();
|
||||
for (final k in danglingKeys) {
|
||||
_log.fine("[cleanUp] Removing albumStore entry: $k");
|
||||
try {
|
||||
await store.delete(k);
|
||||
} catch (e, stackTrace) {
|
||||
_log.shout(
|
||||
"[cleanUp] Failed removing albumStore entry", e, stackTrace);
|
||||
appDb.use(
|
||||
(db) => db.transaction(AppDb.albumStoreName, idbModeReadWrite),
|
||||
(transaction) async {
|
||||
final store = transaction.objectStore(AppDb.albumStoreName);
|
||||
final index = store.index(AppDbAlbumEntry.indexName);
|
||||
final rootPath = AppDbAlbumEntry.toPath(account, rootDir);
|
||||
final range = KeyRange.bound(
|
||||
["$rootPath/", 0], ["$rootPath/\uffff", int_util.int32Max]);
|
||||
final danglingKeys = await index
|
||||
// get all albums for this account
|
||||
.openKeyCursor(range: range, autoAdvance: true)
|
||||
.map((cursor) => Tuple2((cursor.key as List)[0], cursor.primaryKey))
|
||||
// and pick the dangling ones
|
||||
.where((pair) => !albumFiles.any((f) =>
|
||||
pair.item1 == AppDbAlbumEntry.toPathFromFile(account, f)))
|
||||
// map to primary keys
|
||||
.map((pair) => pair.item2)
|
||||
.toList();
|
||||
for (final k in danglingKeys) {
|
||||
_log.fine("[cleanUp] Removing albumStore entry: $k");
|
||||
try {
|
||||
await store.delete(k);
|
||||
} catch (e, stackTrace) {
|
||||
_log.shout(
|
||||
"[cleanUp] Failed removing albumStore entry", e, stackTrace);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _cacheResult(Account account, Album result) {
|
||||
return appDb.use((db) async {
|
||||
final transaction =
|
||||
db.transaction(AppDb.albumStoreName, idbModeReadWrite);
|
||||
final store = transaction.objectStore(AppDb.albumStoreName);
|
||||
await _cacheAlbum(store, account, result);
|
||||
});
|
||||
return appDb.use(
|
||||
(db) => db.transaction(AppDb.albumStoreName, idbModeReadWrite),
|
||||
(transaction) async {
|
||||
final store = transaction.objectStore(AppDb.albumStoreName);
|
||||
await _cacheAlbum(store, account, result);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
final AppDb appDb;
|
||||
|
|
|
@ -269,31 +269,34 @@ class FileAppDbDataSource implements FileDataSource {
|
|||
@override
|
||||
list(Account account, File dir) {
|
||||
_log.info("[list] ${dir.path}");
|
||||
return appDb.use((db) async {
|
||||
final transaction = db.transaction(
|
||||
[AppDb.dirStoreName, AppDb.file2StoreName], idbModeReadOnly);
|
||||
final fileStore = transaction.objectStore(AppDb.file2StoreName);
|
||||
final dirStore = transaction.objectStore(AppDb.dirStoreName);
|
||||
final dirItem = await dirStore
|
||||
.getObject(AppDbDirEntry.toPrimaryKeyForDir(account, dir)) as Map?;
|
||||
if (dirItem == null) {
|
||||
throw CacheNotFoundException("No entry: ${dir.path}");
|
||||
}
|
||||
final dirEntry = AppDbDirEntry.fromJson(dirItem.cast<String, dynamic>());
|
||||
final entries = await Future.wait(dirEntry.children.map((c) async {
|
||||
final fileItem = await fileStore
|
||||
.getObject(AppDbFile2Entry.toPrimaryKey(account, c)) as Map?;
|
||||
if (fileItem == null) {
|
||||
_log.warning(
|
||||
"[list] Missing file ($c) in db for dir: ${logFilename(dir.path)}");
|
||||
throw CacheNotFoundException("No entry for dir child: $c");
|
||||
return appDb.use(
|
||||
(db) => db.transaction(
|
||||
[AppDb.dirStoreName, AppDb.file2StoreName], idbModeReadOnly),
|
||||
(transaction) async {
|
||||
final fileStore = transaction.objectStore(AppDb.file2StoreName);
|
||||
final dirStore = transaction.objectStore(AppDb.dirStoreName);
|
||||
final dirItem = await dirStore
|
||||
.getObject(AppDbDirEntry.toPrimaryKeyForDir(account, dir)) as Map?;
|
||||
if (dirItem == null) {
|
||||
throw CacheNotFoundException("No entry: ${dir.path}");
|
||||
}
|
||||
return AppDbFile2Entry.fromJson(fileItem.cast<String, dynamic>());
|
||||
}));
|
||||
// we need to add dir to match the remote query
|
||||
return [dirEntry.dir] +
|
||||
entries.map((e) => e.file).where((f) => _validateFile(f)).toList();
|
||||
});
|
||||
final dirEntry =
|
||||
AppDbDirEntry.fromJson(dirItem.cast<String, dynamic>());
|
||||
final entries = await Future.wait(dirEntry.children.map((c) async {
|
||||
final fileItem = await fileStore
|
||||
.getObject(AppDbFile2Entry.toPrimaryKey(account, c)) as Map?;
|
||||
if (fileItem == null) {
|
||||
_log.warning(
|
||||
"[list] Missing file ($c) in db for dir: ${logFilename(dir.path)}");
|
||||
throw CacheNotFoundException("No entry for dir child: $c");
|
||||
}
|
||||
return AppDbFile2Entry.fromJson(fileItem.cast<String, dynamic>());
|
||||
}));
|
||||
// we need to add dir to match the remote query
|
||||
return [dirEntry.dir] +
|
||||
entries.map((e) => e.file).where((f) => _validateFile(f)).toList();
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
|
@ -307,19 +310,21 @@ class FileAppDbDataSource implements FileDataSource {
|
|||
Future<List<File>> listByDate(
|
||||
Account account, int fromEpochMs, int toEpochMs) async {
|
||||
_log.info("[listByDate] [$fromEpochMs, $toEpochMs]");
|
||||
final items = await appDb.use((db) async {
|
||||
final transaction = db.transaction(AppDb.file2StoreName, idbModeReadOnly);
|
||||
final fileStore = transaction.objectStore(AppDb.file2StoreName);
|
||||
final dateTimeEpochMsIndex =
|
||||
fileStore.index(AppDbFile2Entry.dateTimeEpochMsIndexName);
|
||||
final range = KeyRange.bound(
|
||||
AppDbFile2Entry.toDateTimeEpochMsIndexKey(account, fromEpochMs),
|
||||
AppDbFile2Entry.toDateTimeEpochMsIndexKey(account, toEpochMs),
|
||||
false,
|
||||
true,
|
||||
);
|
||||
return await dateTimeEpochMsIndex.getAll(range);
|
||||
});
|
||||
final items = await appDb.use(
|
||||
(db) => db.transaction(AppDb.file2StoreName, idbModeReadOnly),
|
||||
(transaction) async {
|
||||
final fileStore = transaction.objectStore(AppDb.file2StoreName);
|
||||
final dateTimeEpochMsIndex =
|
||||
fileStore.index(AppDbFile2Entry.dateTimeEpochMsIndexName);
|
||||
final range = KeyRange.bound(
|
||||
AppDbFile2Entry.toDateTimeEpochMsIndexKey(account, fromEpochMs),
|
||||
AppDbFile2Entry.toDateTimeEpochMsIndexKey(account, toEpochMs),
|
||||
false,
|
||||
true,
|
||||
);
|
||||
return await dateTimeEpochMsIndex.getAll(range);
|
||||
},
|
||||
);
|
||||
return items
|
||||
.cast<Map>()
|
||||
.map((i) => AppDbFile2Entry.fromJson(i.cast<String, dynamic>()))
|
||||
|
@ -357,21 +362,21 @@ class FileAppDbDataSource implements FileDataSource {
|
|||
bool? favorite,
|
||||
}) {
|
||||
_log.info("[updateProperty] ${f.path}");
|
||||
return appDb.use((db) async {
|
||||
final transaction =
|
||||
db.transaction(AppDb.file2StoreName, idbModeReadWrite);
|
||||
|
||||
// update file store
|
||||
final newFile = f.copyWith(
|
||||
metadata: metadata,
|
||||
isArchived: isArchived,
|
||||
overrideDateTime: overrideDateTime,
|
||||
isFavorite: favorite,
|
||||
);
|
||||
final fileStore = transaction.objectStore(AppDb.file2StoreName);
|
||||
await fileStore.put(AppDbFile2Entry.fromFile(account, newFile).toJson(),
|
||||
AppDbFile2Entry.toPrimaryKeyForFile(account, newFile));
|
||||
});
|
||||
return appDb.use(
|
||||
(db) => db.transaction(AppDb.file2StoreName, idbModeReadWrite),
|
||||
(transaction) async {
|
||||
// update file store
|
||||
final newFile = f.copyWith(
|
||||
metadata: metadata,
|
||||
isArchived: isArchived,
|
||||
overrideDateTime: overrideDateTime,
|
||||
isFavorite: favorite,
|
||||
);
|
||||
final fileStore = transaction.objectStore(AppDb.file2StoreName);
|
||||
await fileStore.put(AppDbFile2Entry.fromFile(account, newFile).toJson(),
|
||||
AppDbFile2Entry.toPrimaryKeyForFile(account, newFile));
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
|
@ -577,20 +582,22 @@ class FileForwardCacheManager {
|
|||
}
|
||||
|
||||
Future<void> _cacheDir(Account account, File dir) async {
|
||||
final dirItems = await appDb.use((db) async {
|
||||
final transaction = db.transaction(AppDb.dirStoreName, idbModeReadOnly);
|
||||
final store = transaction.objectStore(AppDb.dirStoreName);
|
||||
final dirItem = await store
|
||||
.getObject(AppDbDirEntry.toPrimaryKeyForDir(account, dir)) as Map?;
|
||||
if (dirItem == null) {
|
||||
return null;
|
||||
}
|
||||
final range = KeyRange.bound(
|
||||
AppDbDirEntry.toPrimaryLowerKeyForSubDirs(account, dir),
|
||||
AppDbDirEntry.toPrimaryUpperKeyForSubDirs(account, dir),
|
||||
);
|
||||
return [dirItem] + (await store.getAll(range)).cast<Map>();
|
||||
});
|
||||
final dirItems = await appDb.use(
|
||||
(db) => db.transaction(AppDb.dirStoreName, idbModeReadOnly),
|
||||
(transaction) async {
|
||||
final store = transaction.objectStore(AppDb.dirStoreName);
|
||||
final dirItem = await store
|
||||
.getObject(AppDbDirEntry.toPrimaryKeyForDir(account, dir)) as Map?;
|
||||
if (dirItem == null) {
|
||||
return null;
|
||||
}
|
||||
final range = KeyRange.bound(
|
||||
AppDbDirEntry.toPrimaryLowerKeyForSubDirs(account, dir),
|
||||
AppDbDirEntry.toPrimaryUpperKeyForSubDirs(account, dir),
|
||||
);
|
||||
return [dirItem] + (await store.getAll(range)).cast<Map>();
|
||||
},
|
||||
);
|
||||
if (dirItems == null) {
|
||||
// no cache
|
||||
return;
|
||||
|
@ -606,12 +613,14 @@ class FileForwardCacheManager {
|
|||
// cache files
|
||||
final fileIds = dirs.map((e) => e.children).fold<List<int>>(
|
||||
[], (previousValue, element) => previousValue + element);
|
||||
final fileItems = await appDb.use((db) async {
|
||||
final transaction = db.transaction(AppDb.file2StoreName, idbModeReadOnly);
|
||||
final store = transaction.objectStore(AppDb.file2StoreName);
|
||||
return await Future.wait(fileIds.map(
|
||||
(id) => store.getObject(AppDbFile2Entry.toPrimaryKey(account, id))));
|
||||
});
|
||||
final fileItems = await appDb.use(
|
||||
(db) => db.transaction(AppDb.file2StoreName, idbModeReadOnly),
|
||||
(transaction) async {
|
||||
final store = transaction.objectStore(AppDb.file2StoreName);
|
||||
return await Future.wait(fileIds.map((id) =>
|
||||
store.getObject(AppDbFile2Entry.toPrimaryKey(account, id))));
|
||||
},
|
||||
);
|
||||
final files = fileItems
|
||||
.cast<Map?>()
|
||||
.whereType<Map>()
|
||||
|
|
|
@ -124,27 +124,30 @@ class FileCacheUpdater {
|
|||
}
|
||||
|
||||
Future<void> _cacheRemote(Account account, File dir, List<File> remote) {
|
||||
return appDb.use((db) async {
|
||||
final transaction = db.transaction(
|
||||
[AppDb.dirStoreName, AppDb.file2StoreName], idbModeReadWrite);
|
||||
final dirStore = transaction.objectStore(AppDb.dirStoreName);
|
||||
final fileStore = transaction.objectStore(AppDb.file2StoreName);
|
||||
return appDb.use(
|
||||
(db) => db.transaction(
|
||||
[AppDb.dirStoreName, AppDb.file2StoreName], idbModeReadWrite),
|
||||
(transaction) async {
|
||||
final dirStore = transaction.objectStore(AppDb.dirStoreName);
|
||||
final fileStore = transaction.objectStore(AppDb.file2StoreName);
|
||||
|
||||
// add files to db
|
||||
await Future.wait(remote.map((f) => fileStore.put(
|
||||
AppDbFile2Entry.fromFile(account, f).toJson(),
|
||||
AppDbFile2Entry.toPrimaryKeyForFile(account, f))));
|
||||
// add files to db
|
||||
await Future.wait(remote.map((f) => fileStore.put(
|
||||
AppDbFile2Entry.fromFile(account, f).toJson(),
|
||||
AppDbFile2Entry.toPrimaryKeyForFile(account, f))));
|
||||
|
||||
// results from remote also contain the dir itself
|
||||
final resultGroup =
|
||||
remote.groupListsBy((f) => f.compareServerIdentity(dir));
|
||||
final remoteDir = resultGroup[true]!.first;
|
||||
final remoteChildren = resultGroup[false] ?? [];
|
||||
// add dir to db
|
||||
await dirStore.put(
|
||||
AppDbDirEntry.fromFiles(account, remoteDir, remoteChildren).toJson(),
|
||||
AppDbDirEntry.toPrimaryKeyForDir(account, remoteDir));
|
||||
});
|
||||
// results from remote also contain the dir itself
|
||||
final resultGroup =
|
||||
remote.groupListsBy((f) => f.compareServerIdentity(dir));
|
||||
final remoteDir = resultGroup[true]!.first;
|
||||
final remoteChildren = resultGroup[false] ?? [];
|
||||
// add dir to db
|
||||
await dirStore.put(
|
||||
AppDbDirEntry.fromFiles(account, remoteDir, remoteChildren)
|
||||
.toJson(),
|
||||
AppDbDirEntry.toPrimaryKeyForDir(account, remoteDir));
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
/// Remove extra entries from local cache based on remote contents
|
||||
|
@ -159,27 +162,29 @@ class FileCacheUpdater {
|
|||
_log.info(
|
||||
"[_cleanUpCache] Removed: ${removed.map((f) => f.path).toReadableString()}");
|
||||
|
||||
await appDb.use((db) async {
|
||||
final transaction = db.transaction(
|
||||
[AppDb.dirStoreName, AppDb.file2StoreName], idbModeReadWrite);
|
||||
final dirStore = transaction.objectStore(AppDb.dirStoreName);
|
||||
final fileStore = transaction.objectStore(AppDb.file2StoreName);
|
||||
for (final f in removed) {
|
||||
try {
|
||||
if (f.isCollection == true) {
|
||||
await _removeDirFromAppDb(account, f,
|
||||
dirStore: dirStore, fileStore: fileStore);
|
||||
} else {
|
||||
await _removeFileFromAppDb(account, f, fileStore: fileStore);
|
||||
await appDb.use(
|
||||
(db) => db.transaction(
|
||||
[AppDb.dirStoreName, AppDb.file2StoreName], idbModeReadWrite),
|
||||
(transaction) async {
|
||||
final dirStore = transaction.objectStore(AppDb.dirStoreName);
|
||||
final fileStore = transaction.objectStore(AppDb.file2StoreName);
|
||||
for (final f in removed) {
|
||||
try {
|
||||
if (f.isCollection == true) {
|
||||
await _removeDirFromAppDb(account, f,
|
||||
dirStore: dirStore, fileStore: fileStore);
|
||||
} else {
|
||||
await _removeFileFromAppDb(account, f, fileStore: fileStore);
|
||||
}
|
||||
} catch (e, stackTrace) {
|
||||
_log.shout(
|
||||
"[_cleanUpCache] Failed while removing file: ${logFilename(f.path)}",
|
||||
e,
|
||||
stackTrace);
|
||||
}
|
||||
} catch (e, stackTrace) {
|
||||
_log.shout(
|
||||
"[_cleanUpCache] Failed while removing file: ${logFilename(f.path)}",
|
||||
e,
|
||||
stackTrace);
|
||||
}
|
||||
}
|
||||
});
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
final AppDb appDb;
|
||||
|
@ -198,23 +203,28 @@ class FileCacheRemover {
|
|||
/// If [f] is a file, the file will be removed from file2Store, but no changes
|
||||
/// to dirStore.
|
||||
Future<void> call(Account account, File f) async {
|
||||
await appDb.use((db) async {
|
||||
if (f.isCollection != false) {
|
||||
// removing dir is basically a superset of removing file, so we'll treat
|
||||
// unspecified file as dir
|
||||
final transaction = db.transaction(
|
||||
[AppDb.dirStoreName, AppDb.file2StoreName], idbModeReadWrite);
|
||||
final dirStore = transaction.objectStore(AppDb.dirStoreName);
|
||||
final fileStore = transaction.objectStore(AppDb.file2StoreName);
|
||||
await _removeDirFromAppDb(account, f,
|
||||
dirStore: dirStore, fileStore: fileStore);
|
||||
} else {
|
||||
final transaction =
|
||||
db.transaction(AppDb.file2StoreName, idbModeReadWrite);
|
||||
final fileStore = transaction.objectStore(AppDb.file2StoreName);
|
||||
await _removeFileFromAppDb(account, f, fileStore: fileStore);
|
||||
}
|
||||
});
|
||||
if (f.isCollection != false) {
|
||||
// removing dir is basically a superset of removing file, so we'll treat
|
||||
// unspecified file as dir
|
||||
await appDb.use(
|
||||
(db) => db.transaction(
|
||||
[AppDb.dirStoreName, AppDb.file2StoreName], idbModeReadWrite),
|
||||
(transaction) async {
|
||||
final dirStore = transaction.objectStore(AppDb.dirStoreName);
|
||||
final fileStore = transaction.objectStore(AppDb.file2StoreName);
|
||||
await _removeDirFromAppDb(account, f,
|
||||
dirStore: dirStore, fileStore: fileStore);
|
||||
},
|
||||
);
|
||||
} else {
|
||||
await appDb.use(
|
||||
(db) => db.transaction(AppDb.file2StoreName, idbModeReadWrite),
|
||||
(transaction) async {
|
||||
final fileStore = transaction.objectStore(AppDb.file2StoreName);
|
||||
await _removeFileFromAppDb(account, f, fileStore: fileStore);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
final AppDb appDb;
|
||||
|
|
|
@ -37,35 +37,36 @@ class CacheFavorite {
|
|||
if (newFavorites.isEmpty && removedFavorites.isEmpty) {
|
||||
return;
|
||||
}
|
||||
await _c.appDb.use((db) async {
|
||||
final transaction =
|
||||
db.transaction(AppDb.file2StoreName, idbModeReadWrite);
|
||||
final fileStore = transaction.objectStore(AppDb.file2StoreName);
|
||||
await Future.wait(newFavorites.map((f) async {
|
||||
_log.info("[call] New favorite: ${f.path}");
|
||||
try {
|
||||
await fileStore.put(AppDbFile2Entry.fromFile(account, f).toJson(),
|
||||
AppDbFile2Entry.toPrimaryKeyForFile(account, f));
|
||||
} catch (e, stackTrace) {
|
||||
_log.shout(
|
||||
"[call] Failed while writing new favorite to AppDb: ${logFilename(f.path)}",
|
||||
e,
|
||||
stackTrace);
|
||||
}
|
||||
}));
|
||||
await Future.wait(removedFavorites.map((f) async {
|
||||
_log.info("[call] Remove favorite: ${f.path}");
|
||||
try {
|
||||
await fileStore.put(AppDbFile2Entry.fromFile(account, f).toJson(),
|
||||
AppDbFile2Entry.toPrimaryKeyForFile(account, f));
|
||||
} catch (e, stackTrace) {
|
||||
_log.shout(
|
||||
"[call] Failed while writing removed favorite to AppDb: ${logFilename(f.path)}",
|
||||
e,
|
||||
stackTrace);
|
||||
}
|
||||
}));
|
||||
});
|
||||
await _c.appDb.use(
|
||||
(db) => db.transaction(AppDb.file2StoreName, idbModeReadWrite),
|
||||
(transaction) async {
|
||||
final fileStore = transaction.objectStore(AppDb.file2StoreName);
|
||||
await Future.wait(newFavorites.map((f) async {
|
||||
_log.info("[call] New favorite: ${f.path}");
|
||||
try {
|
||||
await fileStore.put(AppDbFile2Entry.fromFile(account, f).toJson(),
|
||||
AppDbFile2Entry.toPrimaryKeyForFile(account, f));
|
||||
} catch (e, stackTrace) {
|
||||
_log.shout(
|
||||
"[call] Failed while writing new favorite to AppDb: ${logFilename(f.path)}",
|
||||
e,
|
||||
stackTrace);
|
||||
}
|
||||
}));
|
||||
await Future.wait(removedFavorites.map((f) async {
|
||||
_log.info("[call] Remove favorite: ${f.path}");
|
||||
try {
|
||||
await fileStore.put(AppDbFile2Entry.fromFile(account, f).toJson(),
|
||||
AppDbFile2Entry.toPrimaryKeyForFile(account, f));
|
||||
} catch (e, stackTrace) {
|
||||
_log.shout(
|
||||
"[call] Failed while writing removed favorite to AppDb: ${logFilename(f.path)}",
|
||||
e,
|
||||
stackTrace);
|
||||
}
|
||||
}));
|
||||
},
|
||||
);
|
||||
|
||||
KiwiContainer()
|
||||
.resolve<EventBus>()
|
||||
|
|
|
@ -12,14 +12,15 @@ class CompatV37 {
|
|||
static Future<void> setAppDbMigrationFlag(AppDb appDb) async {
|
||||
_log.info("[setAppDbMigrationFlag] Set db flag");
|
||||
try {
|
||||
await appDb.use((db) async {
|
||||
final transaction =
|
||||
db.transaction(AppDb.metaStoreName, idbModeReadWrite);
|
||||
final metaStore = transaction.objectStore(AppDb.metaStoreName);
|
||||
await metaStore
|
||||
.put(const AppDbMetaEntryCompatV37(false).toEntry().toJson());
|
||||
await transaction.completed;
|
||||
});
|
||||
await appDb.use(
|
||||
(db) => db.transaction(AppDb.metaStoreName, idbModeReadWrite),
|
||||
(transaction) async {
|
||||
final metaStore = transaction.objectStore(AppDb.metaStoreName);
|
||||
await metaStore
|
||||
.put(const AppDbMetaEntryCompatV37(false).toEntry().toJson());
|
||||
await transaction.completed;
|
||||
},
|
||||
);
|
||||
} catch (e, stackTrace) {
|
||||
_log.shout(
|
||||
"[setAppDbMigrationFlag] Failed while setting db flag, drop db instead",
|
||||
|
@ -30,11 +31,13 @@ class CompatV37 {
|
|||
}
|
||||
|
||||
static Future<bool> isAppDbNeedMigration(AppDb appDb) async {
|
||||
final dbItem = await appDb.use((db) async {
|
||||
final transaction = db.transaction(AppDb.metaStoreName, idbModeReadOnly);
|
||||
final metaStore = transaction.objectStore(AppDb.metaStoreName);
|
||||
return await metaStore.getObject(AppDbMetaEntryCompatV37.key) as Map?;
|
||||
});
|
||||
final dbItem = await appDb.use(
|
||||
(db) => db.transaction(AppDb.metaStoreName, idbModeReadOnly),
|
||||
(transaction) async {
|
||||
final metaStore = transaction.objectStore(AppDb.metaStoreName);
|
||||
return await metaStore.getObject(AppDbMetaEntryCompatV37.key) as Map?;
|
||||
},
|
||||
);
|
||||
if (dbItem == null) {
|
||||
return false;
|
||||
}
|
||||
|
@ -51,51 +54,53 @@ class CompatV37 {
|
|||
static Future<void> migrateAppDb(AppDb appDb) async {
|
||||
_log.info("[migrateAppDb] Migrate AppDb");
|
||||
try {
|
||||
await appDb.use((db) async {
|
||||
final transaction = db.transaction(
|
||||
await appDb.use(
|
||||
(db) => db.transaction(
|
||||
[AppDb.file2StoreName, AppDb.dirStoreName, AppDb.metaStoreName],
|
||||
idbModeReadWrite);
|
||||
final noMediaFiles = <_NoMediaFile>[];
|
||||
try {
|
||||
final fileStore = transaction.objectStore(AppDb.file2StoreName);
|
||||
final dirStore = transaction.objectStore(AppDb.dirStoreName);
|
||||
// scan the db to see which dirs contain a no media marker
|
||||
await for (final c in fileStore.openCursor()) {
|
||||
final item = c.value as Map;
|
||||
final strippedPath = item["strippedPath"] as String;
|
||||
if (file_util.isNoMediaMarkerPath(strippedPath)) {
|
||||
noMediaFiles.add(_NoMediaFile(
|
||||
item["server"],
|
||||
item["userId"],
|
||||
path_lib
|
||||
.dirname(item["strippedPath"])
|
||||
.run((p) => p == "." ? "" : p),
|
||||
item["file"]["fileId"],
|
||||
));
|
||||
idbModeReadWrite),
|
||||
(transaction) async {
|
||||
final noMediaFiles = <_NoMediaFile>[];
|
||||
try {
|
||||
final fileStore = transaction.objectStore(AppDb.file2StoreName);
|
||||
final dirStore = transaction.objectStore(AppDb.dirStoreName);
|
||||
// scan the db to see which dirs contain a no media marker
|
||||
await for (final c in fileStore.openCursor()) {
|
||||
final item = c.value as Map;
|
||||
final strippedPath = item["strippedPath"] as String;
|
||||
if (file_util.isNoMediaMarkerPath(strippedPath)) {
|
||||
noMediaFiles.add(_NoMediaFile(
|
||||
item["server"],
|
||||
item["userId"],
|
||||
path_lib
|
||||
.dirname(item["strippedPath"])
|
||||
.run((p) => p == "." ? "" : p),
|
||||
item["file"]["fileId"],
|
||||
));
|
||||
}
|
||||
c.next();
|
||||
}
|
||||
c.next();
|
||||
}
|
||||
// sort to make sure parent dirs are always in front of sub dirs
|
||||
noMediaFiles
|
||||
.sort((a, b) => a.strippedDirPath.compareTo(b.strippedDirPath));
|
||||
_log.info(
|
||||
"[migrateAppDb] nomedia dirs: ${noMediaFiles.toReadableString()}");
|
||||
// sort to make sure parent dirs are always in front of sub dirs
|
||||
noMediaFiles
|
||||
.sort((a, b) => a.strippedDirPath.compareTo(b.strippedDirPath));
|
||||
_log.info(
|
||||
"[migrateAppDb] nomedia dirs: ${noMediaFiles.toReadableString()}");
|
||||
|
||||
if (noMediaFiles.isNotEmpty) {
|
||||
await _migrateAppDbFileStore(appDb, noMediaFiles,
|
||||
fileStore: fileStore);
|
||||
await _migrateAppDbDirStore(appDb, noMediaFiles,
|
||||
dirStore: dirStore);
|
||||
}
|
||||
if (noMediaFiles.isNotEmpty) {
|
||||
await _migrateAppDbFileStore(appDb, noMediaFiles,
|
||||
fileStore: fileStore);
|
||||
await _migrateAppDbDirStore(appDb, noMediaFiles,
|
||||
dirStore: dirStore);
|
||||
}
|
||||
|
||||
final metaStore = transaction.objectStore(AppDb.metaStoreName);
|
||||
await metaStore
|
||||
.put(const AppDbMetaEntryCompatV37(true).toEntry().toJson());
|
||||
} catch (_) {
|
||||
transaction.abort();
|
||||
rethrow;
|
||||
}
|
||||
});
|
||||
final metaStore = transaction.objectStore(AppDb.metaStoreName);
|
||||
await metaStore
|
||||
.put(const AppDbMetaEntryCompatV37(true).toEntry().toJson());
|
||||
} catch (_) {
|
||||
transaction.abort();
|
||||
rethrow;
|
||||
}
|
||||
},
|
||||
);
|
||||
} catch (e, stackTrace) {
|
||||
_log.shout("[migrateAppDb] Failed while migrating, drop db instead", e,
|
||||
stackTrace);
|
||||
|
|
|
@ -7,11 +7,13 @@ import 'package:nc_photos/object_extension.dart';
|
|||
|
||||
class DbCompatV5 {
|
||||
static Future<bool> isNeedMigration(AppDb appDb) async {
|
||||
final dbItem = await appDb.use((db) async {
|
||||
final transaction = db.transaction(AppDb.metaStoreName, idbModeReadOnly);
|
||||
final metaStore = transaction.objectStore(AppDb.metaStoreName);
|
||||
return await metaStore.getObject(AppDbMetaEntryDbCompatV5.key) as Map?;
|
||||
});
|
||||
final dbItem = await appDb.use(
|
||||
(db) => db.transaction(AppDb.metaStoreName, idbModeReadOnly),
|
||||
(transaction) async {
|
||||
final metaStore = transaction.objectStore(AppDb.metaStoreName);
|
||||
return await metaStore.getObject(AppDbMetaEntryDbCompatV5.key) as Map?;
|
||||
},
|
||||
);
|
||||
if (dbItem == null) {
|
||||
return false;
|
||||
}
|
||||
|
@ -28,36 +30,38 @@ class DbCompatV5 {
|
|||
static Future<void> migrate(AppDb appDb) async {
|
||||
_log.info("[migrate] Migrate AppDb");
|
||||
try {
|
||||
await appDb.use((db) async {
|
||||
final transaction = db.transaction(
|
||||
[AppDb.file2StoreName, AppDb.metaStoreName], idbModeReadWrite);
|
||||
try {
|
||||
final fileStore = transaction.objectStore(AppDb.file2StoreName);
|
||||
await for (final c in fileStore.openCursor()) {
|
||||
final item = c.value as Map;
|
||||
// migrate file entry: add bestDateTime
|
||||
final fileEntry = item.cast<String, dynamic>().run((json) {
|
||||
final f = File.fromJson(json["file"].cast<String, dynamic>());
|
||||
return AppDbFile2Entry(
|
||||
json["server"],
|
||||
(json["userId"] as String).toCi(),
|
||||
json["strippedPath"],
|
||||
f.bestDateTime.millisecondsSinceEpoch,
|
||||
File.fromJson(json["file"].cast<String, dynamic>()),
|
||||
);
|
||||
});
|
||||
await c.update(fileEntry.toJson());
|
||||
await appDb.use(
|
||||
(db) => db.transaction(
|
||||
[AppDb.file2StoreName, AppDb.metaStoreName], idbModeReadWrite),
|
||||
(transaction) async {
|
||||
try {
|
||||
final fileStore = transaction.objectStore(AppDb.file2StoreName);
|
||||
await for (final c in fileStore.openCursor()) {
|
||||
final item = c.value as Map;
|
||||
// migrate file entry: add bestDateTime
|
||||
final fileEntry = item.cast<String, dynamic>().run((json) {
|
||||
final f = File.fromJson(json["file"].cast<String, dynamic>());
|
||||
return AppDbFile2Entry(
|
||||
json["server"],
|
||||
(json["userId"] as String).toCi(),
|
||||
json["strippedPath"],
|
||||
f.bestDateTime.millisecondsSinceEpoch,
|
||||
File.fromJson(json["file"].cast<String, dynamic>()),
|
||||
);
|
||||
});
|
||||
await c.update(fileEntry.toJson());
|
||||
|
||||
c.next();
|
||||
c.next();
|
||||
}
|
||||
final metaStore = transaction.objectStore(AppDb.metaStoreName);
|
||||
await metaStore
|
||||
.put(const AppDbMetaEntryDbCompatV5(true).toEntry().toJson());
|
||||
} catch (_) {
|
||||
transaction.abort();
|
||||
rethrow;
|
||||
}
|
||||
final metaStore = transaction.objectStore(AppDb.metaStoreName);
|
||||
await metaStore
|
||||
.put(const AppDbMetaEntryDbCompatV5(true).toEntry().toJson());
|
||||
} catch (_) {
|
||||
transaction.abort();
|
||||
rethrow;
|
||||
}
|
||||
});
|
||||
},
|
||||
);
|
||||
} catch (e, stackTrace) {
|
||||
_log.shout(
|
||||
"[migrate] Failed while migrating, drop db instead", e, stackTrace);
|
||||
|
|
|
@ -19,12 +19,14 @@ class FindFile {
|
|||
List<int> fileIds, {
|
||||
void Function(int fileId)? onFileNotFound,
|
||||
}) async {
|
||||
final dbItems = await _c.appDb.use((db) async {
|
||||
final transaction = db.transaction(AppDb.file2StoreName, idbModeReadOnly);
|
||||
final fileStore = transaction.objectStore(AppDb.file2StoreName);
|
||||
return await Future.wait(fileIds.map((id) =>
|
||||
fileStore.getObject(AppDbFile2Entry.toPrimaryKey(account, id))));
|
||||
});
|
||||
final dbItems = await _c.appDb.use(
|
||||
(db) => db.transaction(AppDb.file2StoreName, idbModeReadOnly),
|
||||
(transaction) async {
|
||||
final fileStore = transaction.objectStore(AppDb.file2StoreName);
|
||||
return await Future.wait(fileIds.map((id) =>
|
||||
fileStore.getObject(AppDbFile2Entry.toPrimaryKey(account, id))));
|
||||
},
|
||||
);
|
||||
final files = <File>[];
|
||||
for (final pair in zip([fileIds, dbItems])) {
|
||||
final dbItem = pair[1] as Map?;
|
||||
|
|
|
@ -18,24 +18,26 @@ class ListFavoriteOffline {
|
|||
final rootDirs = account.roots
|
||||
.map((r) => File(path: file_util.unstripPath(account, r)))
|
||||
.toList();
|
||||
return _c.appDb.use((db) async {
|
||||
final transaction = db.transaction(AppDb.file2StoreName, idbModeReadOnly);
|
||||
final fileStore = transaction.objectStore(AppDb.file2StoreName);
|
||||
final fileIsFavoriteIndex =
|
||||
fileStore.index(AppDbFile2Entry.fileIsFavoriteIndexName);
|
||||
return await fileIsFavoriteIndex
|
||||
.openCursor(
|
||||
key: AppDbFile2Entry.toFileIsFavoriteIndexKey(account, true),
|
||||
autoAdvance: true,
|
||||
)
|
||||
.map((c) => AppDbFile2Entry.fromJson(
|
||||
(c.value as Map).cast<String, dynamic>()))
|
||||
.map((e) => e.file)
|
||||
.where((f) =>
|
||||
file_util.isSupportedFormat(f) &&
|
||||
rootDirs.any((r) => file_util.isOrUnderDir(f, r)))
|
||||
.toList();
|
||||
});
|
||||
return _c.appDb.use(
|
||||
(db) => db.transaction(AppDb.file2StoreName, idbModeReadOnly),
|
||||
(transaction) async {
|
||||
final fileStore = transaction.objectStore(AppDb.file2StoreName);
|
||||
final fileIsFavoriteIndex =
|
||||
fileStore.index(AppDbFile2Entry.fileIsFavoriteIndexName);
|
||||
return await fileIsFavoriteIndex
|
||||
.openCursor(
|
||||
key: AppDbFile2Entry.toFileIsFavoriteIndexKey(account, true),
|
||||
autoAdvance: true,
|
||||
)
|
||||
.map((c) => AppDbFile2Entry.fromJson(
|
||||
(c.value as Map).cast<String, dynamic>()))
|
||||
.map((e) => e.file)
|
||||
.where((f) =>
|
||||
file_util.isSupportedFormat(f) &&
|
||||
rootDirs.any((r) => file_util.isOrUnderDir(f, r)))
|
||||
.toList();
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
final DiContainer _c;
|
||||
|
|
|
@ -11,20 +11,22 @@ class PopulatePerson {
|
|||
|
||||
/// Return a list of files of the faces
|
||||
Future<List<File>> call(Account account, List<Face> faces) async {
|
||||
return await appDb.use((db) async {
|
||||
final transaction = db.transaction(AppDb.file2StoreName, idbModeReadOnly);
|
||||
final store = transaction.objectStore(AppDb.file2StoreName);
|
||||
final products = <File>[];
|
||||
for (final f in faces) {
|
||||
try {
|
||||
products.add(await _populateOne(account, f, fileStore: store));
|
||||
} catch (e, stackTrace) {
|
||||
_log.severe("[call] Failed populating file of face: ${f.fileId}", e,
|
||||
stackTrace);
|
||||
return await appDb.use(
|
||||
(db) => db.transaction(AppDb.file2StoreName, idbModeReadOnly),
|
||||
(transaction) async {
|
||||
final store = transaction.objectStore(AppDb.file2StoreName);
|
||||
final products = <File>[];
|
||||
for (final f in faces) {
|
||||
try {
|
||||
products.add(await _populateOne(account, f, fileStore: store));
|
||||
} catch (e, stackTrace) {
|
||||
_log.severe("[call] Failed populating file of face: ${f.fileId}", e,
|
||||
stackTrace);
|
||||
}
|
||||
}
|
||||
}
|
||||
return products;
|
||||
});
|
||||
return products;
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
Future<File> _populateOne(
|
||||
|
|
|
@ -20,17 +20,19 @@ class ResyncAlbum {
|
|||
final items = AlbumStaticProvider.of(album).items;
|
||||
final fileIds =
|
||||
items.whereType<AlbumFileItem>().map((i) => i.file.fileId!).toList();
|
||||
final dbItems = Map.fromEntries(await appDb.use((db) async {
|
||||
final transaction = db.transaction(AppDb.file2StoreName, idbModeReadOnly);
|
||||
final store = transaction.objectStore(AppDb.file2StoreName);
|
||||
return await Future.wait(fileIds.map(
|
||||
(id) async => MapEntry(
|
||||
id,
|
||||
await store.getObject(AppDbFile2Entry.toPrimaryKey(account, id))
|
||||
as Map?,
|
||||
),
|
||||
));
|
||||
}));
|
||||
final dbItems = Map.fromEntries(await appDb.use(
|
||||
(db) => db.transaction(AppDb.file2StoreName, idbModeReadOnly),
|
||||
(transaction) async {
|
||||
final store = transaction.objectStore(AppDb.file2StoreName);
|
||||
return await Future.wait(fileIds.map(
|
||||
(id) async => MapEntry(
|
||||
id,
|
||||
await store.getObject(AppDbFile2Entry.toPrimaryKey(account, id))
|
||||
as Map?,
|
||||
),
|
||||
));
|
||||
},
|
||||
));
|
||||
return items.map((i) {
|
||||
if (i is AlbumFileItem) {
|
||||
try {
|
||||
|
|
|
@ -12,22 +12,25 @@ class ScanDirOffline {
|
|||
|
||||
/// List all files under a dir recursively from the local DB
|
||||
Future<List<File>> call(Account account, File root) async {
|
||||
return await _c.appDb.use((db) async {
|
||||
final transaction = db.transaction(AppDb.file2StoreName, idbModeReadOnly);
|
||||
final store = transaction.objectStore(AppDb.file2StoreName);
|
||||
final index = store.index(AppDbFile2Entry.strippedPathIndexName);
|
||||
final range = KeyRange.bound(
|
||||
AppDbFile2Entry.toStrippedPathIndexLowerKeyForDir(account, root),
|
||||
AppDbFile2Entry.toStrippedPathIndexUpperKeyForDir(account, root),
|
||||
);
|
||||
return await index
|
||||
.openCursor(range: range, autoAdvance: true)
|
||||
.map((c) => c.value)
|
||||
.cast<Map>()
|
||||
.map((e) => AppDbFile2Entry.fromJson(e.cast<String, dynamic>()).file)
|
||||
.where((f) => file_util.isSupportedFormat(f))
|
||||
.toList();
|
||||
});
|
||||
return await _c.appDb.use(
|
||||
(db) => db.transaction(AppDb.file2StoreName, idbModeReadOnly),
|
||||
(transaction) async {
|
||||
final store = transaction.objectStore(AppDb.file2StoreName);
|
||||
final index = store.index(AppDbFile2Entry.strippedPathIndexName);
|
||||
final range = KeyRange.bound(
|
||||
AppDbFile2Entry.toStrippedPathIndexLowerKeyForDir(account, root),
|
||||
AppDbFile2Entry.toStrippedPathIndexUpperKeyForDir(account, root),
|
||||
);
|
||||
return await index
|
||||
.openCursor(range: range, autoAdvance: true)
|
||||
.map((c) => c.value)
|
||||
.cast<Map>()
|
||||
.map(
|
||||
(e) => AppDbFile2Entry.fromJson(e.cast<String, dynamic>()).file)
|
||||
.where((f) => file_util.isSupportedFormat(f))
|
||||
.toList();
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
final DiContainer _c;
|
||||
|
|
|
@ -100,7 +100,8 @@ class MockAppDb implements AppDb {
|
|||
}
|
||||
|
||||
@override
|
||||
Future<T> use<T>(FutureOr<T> Function(Database db) fn) async {
|
||||
Future<T> use<T>(Transaction Function(Database db) transactionBuilder,
|
||||
FutureOr<T> Function(Transaction transaction) fn) async {
|
||||
final db = await _dbFactory.open(
|
||||
"test.db",
|
||||
version: 1,
|
||||
|
@ -110,9 +111,14 @@ class MockAppDb implements AppDb {
|
|||
},
|
||||
);
|
||||
|
||||
Transaction? transaction;
|
||||
try {
|
||||
return await fn(db);
|
||||
transaction = transactionBuilder(db);
|
||||
return await fn(transaction);
|
||||
} finally {
|
||||
if (transaction != null) {
|
||||
await transaction.completed;
|
||||
}
|
||||
db.close();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -341,36 +341,43 @@ Sharee buildSharee({
|
|||
|
||||
Future<void> fillAppDb(
|
||||
AppDb appDb, Account account, Iterable<File> files) async {
|
||||
await appDb.use((db) async {
|
||||
final transaction = db.transaction(AppDb.file2StoreName, idbModeReadWrite);
|
||||
final file2Store = transaction.objectStore(AppDb.file2StoreName);
|
||||
for (final f in files) {
|
||||
await file2Store.put(AppDbFile2Entry.fromFile(account, f).toJson(),
|
||||
AppDbFile2Entry.toPrimaryKeyForFile(account, f));
|
||||
}
|
||||
});
|
||||
await appDb.use(
|
||||
(db) => db.transaction(AppDb.file2StoreName, idbModeReadWrite),
|
||||
(transaction) async {
|
||||
final file2Store = transaction.objectStore(AppDb.file2StoreName);
|
||||
for (final f in files) {
|
||||
await file2Store.put(AppDbFile2Entry.fromFile(account, f).toJson(),
|
||||
AppDbFile2Entry.toPrimaryKeyForFile(account, f));
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> fillAppDbDir(
|
||||
AppDb appDb, Account account, File dir, List<File> children) async {
|
||||
await appDb.use((db) async {
|
||||
final transaction = db.transaction(AppDb.dirStoreName, idbModeReadWrite);
|
||||
final dirStore = transaction.objectStore(AppDb.dirStoreName);
|
||||
await dirStore.put(AppDbDirEntry.fromFiles(account, dir, children).toJson(),
|
||||
AppDbDirEntry.toPrimaryKeyForDir(account, dir));
|
||||
});
|
||||
await appDb.use(
|
||||
(db) => db.transaction(AppDb.dirStoreName, idbModeReadWrite),
|
||||
(transaction) async {
|
||||
final dirStore = transaction.objectStore(AppDb.dirStoreName);
|
||||
await dirStore.put(
|
||||
AppDbDirEntry.fromFiles(account, dir, children).toJson(),
|
||||
AppDbDirEntry.toPrimaryKeyForDir(account, dir));
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
Future<List<T>> listAppDb<T>(
|
||||
AppDb appDb, String storeName, T Function(JsonObj) transform) {
|
||||
return appDb.use((db) async {
|
||||
final transaction = db.transaction(storeName, idbModeReadOnly);
|
||||
final store = transaction.objectStore(storeName);
|
||||
return await store
|
||||
.openCursor(autoAdvance: true)
|
||||
.map((c) => c.value)
|
||||
.cast<Map>()
|
||||
.map((e) => transform(e.cast<String, dynamic>()))
|
||||
.toList();
|
||||
});
|
||||
return appDb.use(
|
||||
(db) => db.transaction(storeName, idbModeReadOnly),
|
||||
(transaction) async {
|
||||
final store = transaction.objectStore(storeName);
|
||||
return await store
|
||||
.openCursor(autoAdvance: true)
|
||||
.map((c) => c.value)
|
||||
.cast<Map>()
|
||||
.map((e) => transform(e.cast<String, dynamic>()))
|
||||
.toList();
|
||||
},
|
||||
);
|
||||
}
|
||||
|
|
|
@ -29,12 +29,14 @@ void main() {
|
|||
/// Expect: true
|
||||
Future<void> _isAppDbNeedMigrationEntryFalse() async {
|
||||
final appDb = MockAppDb();
|
||||
await appDb.use((db) async {
|
||||
final transaction = db.transaction(AppDb.metaStoreName, idbModeReadWrite);
|
||||
final metaStore = transaction.objectStore(AppDb.metaStoreName);
|
||||
const entry = AppDbMetaEntryCompatV37(false);
|
||||
await metaStore.put(entry.toEntry().toJson());
|
||||
});
|
||||
await appDb.use(
|
||||
(db) => db.transaction(AppDb.metaStoreName, idbModeReadWrite),
|
||||
(transaction) async {
|
||||
final metaStore = transaction.objectStore(AppDb.metaStoreName);
|
||||
const entry = AppDbMetaEntryCompatV37(false);
|
||||
await metaStore.put(entry.toEntry().toJson());
|
||||
},
|
||||
);
|
||||
|
||||
expect(await CompatV37.isAppDbNeedMigration(appDb), true);
|
||||
}
|
||||
|
@ -44,12 +46,14 @@ Future<void> _isAppDbNeedMigrationEntryFalse() async {
|
|||
/// Expect: false
|
||||
Future<void> _isAppDbNeedMigrationEntryTrue() async {
|
||||
final appDb = MockAppDb();
|
||||
await appDb.use((db) async {
|
||||
final transaction = db.transaction(AppDb.metaStoreName, idbModeReadWrite);
|
||||
final metaStore = transaction.objectStore(AppDb.metaStoreName);
|
||||
const entry = AppDbMetaEntryCompatV37(true);
|
||||
await metaStore.put(entry.toEntry().toJson());
|
||||
});
|
||||
await appDb.use(
|
||||
(db) => db.transaction(AppDb.metaStoreName, idbModeReadWrite),
|
||||
(transaction) async {
|
||||
final metaStore = transaction.objectStore(AppDb.metaStoreName);
|
||||
const entry = AppDbMetaEntryCompatV37(true);
|
||||
await metaStore.put(entry.toEntry().toJson());
|
||||
},
|
||||
);
|
||||
|
||||
expect(await CompatV37.isAppDbNeedMigration(appDb), false);
|
||||
}
|
||||
|
@ -59,12 +63,14 @@ Future<void> _isAppDbNeedMigrationEntryTrue() async {
|
|||
/// Expect: false
|
||||
Future<void> _isAppDbNeedMigrationWithoutEntry() async {
|
||||
final appDb = MockAppDb();
|
||||
await appDb.use((db) async {
|
||||
final transaction = db.transaction(AppDb.metaStoreName, idbModeReadWrite);
|
||||
final metaStore = transaction.objectStore(AppDb.metaStoreName);
|
||||
const entry = AppDbMetaEntryCompatV37(true);
|
||||
await metaStore.put(entry.toEntry().toJson());
|
||||
});
|
||||
await appDb.use(
|
||||
(db) => db.transaction(AppDb.metaStoreName, idbModeReadWrite),
|
||||
(transaction) async {
|
||||
final metaStore = transaction.objectStore(AppDb.metaStoreName);
|
||||
const entry = AppDbMetaEntryCompatV37(true);
|
||||
await metaStore.put(entry.toEntry().toJson());
|
||||
},
|
||||
);
|
||||
|
||||
expect(await CompatV37.isAppDbNeedMigration(appDb), false);
|
||||
}
|
||||
|
@ -81,11 +87,9 @@ Future<void> _migrateAppDbWithoutNomedia() async {
|
|||
..addJpeg("admin/dir1/test2.jpg"))
|
||||
.build();
|
||||
final appDb = MockAppDb();
|
||||
await appDb.use((db) async {
|
||||
await util.fillAppDb(appDb, account, files);
|
||||
await util.fillAppDbDir(appDb, account, files[0], files.slice(1, 3));
|
||||
await util.fillAppDbDir(appDb, account, files[2], [files[3]]);
|
||||
});
|
||||
await util.fillAppDb(appDb, account, files);
|
||||
await util.fillAppDbDir(appDb, account, files[0], files.slice(1, 3));
|
||||
await util.fillAppDbDir(appDb, account, files[2], [files[3]]);
|
||||
await CompatV37.migrateAppDb(appDb);
|
||||
|
||||
final fileObjs = await util.listAppDb(
|
||||
|
@ -113,11 +117,9 @@ Future<void> _migrateAppDb() async {
|
|||
..addJpeg("admin/dir1/test2.jpg"))
|
||||
.build();
|
||||
final appDb = MockAppDb();
|
||||
await appDb.use((db) async {
|
||||
await util.fillAppDb(appDb, account, files);
|
||||
await util.fillAppDbDir(appDb, account, files[0], files.slice(1, 3));
|
||||
await util.fillAppDbDir(appDb, account, files[2], files.slice(3, 5));
|
||||
});
|
||||
await util.fillAppDb(appDb, account, files);
|
||||
await util.fillAppDbDir(appDb, account, files[0], files.slice(1, 3));
|
||||
await util.fillAppDbDir(appDb, account, files[2], files.slice(3, 5));
|
||||
await CompatV37.migrateAppDb(appDb);
|
||||
|
||||
final fileObjs = await util.listAppDb(
|
||||
|
@ -147,12 +149,10 @@ Future<void> _migrateAppDbNestedDir() async {
|
|||
..addJpeg("admin/dir1/dir1-1/test3.jpg"))
|
||||
.build();
|
||||
final appDb = MockAppDb();
|
||||
await appDb.use((db) async {
|
||||
await util.fillAppDb(appDb, account, files);
|
||||
await util.fillAppDbDir(appDb, account, files[0], files.slice(1, 3));
|
||||
await util.fillAppDbDir(appDb, account, files[2], files.slice(3, 6));
|
||||
await util.fillAppDbDir(appDb, account, files[5], [files[6]]);
|
||||
});
|
||||
await util.fillAppDb(appDb, account, files);
|
||||
await util.fillAppDbDir(appDb, account, files[0], files.slice(1, 3));
|
||||
await util.fillAppDbDir(appDb, account, files[2], files.slice(3, 6));
|
||||
await util.fillAppDbDir(appDb, account, files[5], [files[6]]);
|
||||
await CompatV37.migrateAppDb(appDb);
|
||||
|
||||
final fileObjs = await util.listAppDb(
|
||||
|
@ -183,12 +183,10 @@ Future<void> _migrateAppDbNestedMarker() async {
|
|||
..addJpeg("admin/dir1/dir1-1/test3.jpg"))
|
||||
.build();
|
||||
final appDb = MockAppDb();
|
||||
await appDb.use((db) async {
|
||||
await util.fillAppDb(appDb, account, files);
|
||||
await util.fillAppDbDir(appDb, account, files[0], files.slice(1, 3));
|
||||
await util.fillAppDbDir(appDb, account, files[2], files.slice(3, 6));
|
||||
await util.fillAppDbDir(appDb, account, files[5], files.slice(6, 8));
|
||||
});
|
||||
await util.fillAppDb(appDb, account, files);
|
||||
await util.fillAppDbDir(appDb, account, files[0], files.slice(1, 3));
|
||||
await util.fillAppDbDir(appDb, account, files[2], files.slice(3, 6));
|
||||
await util.fillAppDbDir(appDb, account, files[5], files.slice(6, 8));
|
||||
await CompatV37.migrateAppDb(appDb);
|
||||
|
||||
final fileObjs = await util.listAppDb(
|
||||
|
@ -216,11 +214,9 @@ Future<void> _migrateAppDbRoot() async {
|
|||
..addJpeg("admin/dir1/test2.jpg"))
|
||||
.build();
|
||||
final appDb = MockAppDb();
|
||||
await appDb.use((db) async {
|
||||
await util.fillAppDb(appDb, account, files);
|
||||
await util.fillAppDbDir(appDb, account, files[0], files.slice(1, 4));
|
||||
await util.fillAppDbDir(appDb, account, files[3], [files[4]]);
|
||||
});
|
||||
await util.fillAppDb(appDb, account, files);
|
||||
await util.fillAppDbDir(appDb, account, files[0], files.slice(1, 4));
|
||||
await util.fillAppDbDir(appDb, account, files[3], [files[4]]);
|
||||
await CompatV37.migrateAppDb(appDb);
|
||||
|
||||
final objs = await util.listAppDb(
|
||||
|
|
|
@ -13,39 +13,42 @@ void main() {
|
|||
group("isNeedMigration", () {
|
||||
test("w/ meta entry == false", () async {
|
||||
final appDb = MockAppDb();
|
||||
await appDb.use((db) async {
|
||||
final transaction =
|
||||
db.transaction(AppDb.metaStoreName, idbModeReadWrite);
|
||||
final metaStore = transaction.objectStore(AppDb.metaStoreName);
|
||||
const entry = AppDbMetaEntryDbCompatV5(false);
|
||||
await metaStore.put(entry.toEntry().toJson());
|
||||
});
|
||||
await appDb.use(
|
||||
(db) => db.transaction(AppDb.metaStoreName, idbModeReadWrite),
|
||||
(transaction) async {
|
||||
final metaStore = transaction.objectStore(AppDb.metaStoreName);
|
||||
const entry = AppDbMetaEntryDbCompatV5(false);
|
||||
await metaStore.put(entry.toEntry().toJson());
|
||||
},
|
||||
);
|
||||
|
||||
expect(await DbCompatV5.isNeedMigration(appDb), true);
|
||||
});
|
||||
|
||||
test("w/ meta entry == true", () async {
|
||||
final appDb = MockAppDb();
|
||||
await appDb.use((db) async {
|
||||
final transaction =
|
||||
db.transaction(AppDb.metaStoreName, idbModeReadWrite);
|
||||
final metaStore = transaction.objectStore(AppDb.metaStoreName);
|
||||
const entry = AppDbMetaEntryDbCompatV5(true);
|
||||
await metaStore.put(entry.toEntry().toJson());
|
||||
});
|
||||
await appDb.use(
|
||||
(db) => db.transaction(AppDb.metaStoreName, idbModeReadWrite),
|
||||
(transaction) async {
|
||||
final metaStore = transaction.objectStore(AppDb.metaStoreName);
|
||||
const entry = AppDbMetaEntryDbCompatV5(true);
|
||||
await metaStore.put(entry.toEntry().toJson());
|
||||
},
|
||||
);
|
||||
|
||||
expect(await DbCompatV5.isNeedMigration(appDb), false);
|
||||
});
|
||||
|
||||
test("w/o meta entry", () async {
|
||||
final appDb = MockAppDb();
|
||||
await appDb.use((db) async {
|
||||
final transaction =
|
||||
db.transaction(AppDb.metaStoreName, idbModeReadWrite);
|
||||
final metaStore = transaction.objectStore(AppDb.metaStoreName);
|
||||
const entry = AppDbMetaEntryDbCompatV5(true);
|
||||
await metaStore.put(entry.toEntry().toJson());
|
||||
});
|
||||
await appDb.use(
|
||||
(db) => db.transaction(AppDb.metaStoreName, idbModeReadWrite),
|
||||
(transaction) async {
|
||||
final metaStore = transaction.objectStore(AppDb.metaStoreName);
|
||||
const entry = AppDbMetaEntryDbCompatV5(true);
|
||||
await metaStore.put(entry.toEntry().toJson());
|
||||
},
|
||||
);
|
||||
|
||||
expect(await DbCompatV5.isNeedMigration(appDb), false);
|
||||
});
|
||||
|
@ -60,17 +63,18 @@ void main() {
|
|||
))
|
||||
.build();
|
||||
final appDb = MockAppDb();
|
||||
await appDb.use((db) async {
|
||||
final transaction =
|
||||
db.transaction(AppDb.file2StoreName, idbModeReadWrite);
|
||||
final fileStore = transaction.objectStore(AppDb.file2StoreName);
|
||||
await fileStore.put({
|
||||
"server": account.url,
|
||||
"userId": account.username.toCaseInsensitiveString(),
|
||||
"strippedPath": files[0].strippedPathWithEmpty,
|
||||
"file": files[0].toJson(),
|
||||
}, "${account.url}/${account.username.toCaseInsensitiveString()}/${files[0].fileId}");
|
||||
});
|
||||
await appDb.use(
|
||||
(db) => db.transaction(AppDb.file2StoreName, idbModeReadWrite),
|
||||
(transaction) async {
|
||||
final fileStore = transaction.objectStore(AppDb.file2StoreName);
|
||||
await fileStore.put({
|
||||
"server": account.url,
|
||||
"userId": account.username.toCaseInsensitiveString(),
|
||||
"strippedPath": files[0].strippedPathWithEmpty,
|
||||
"file": files[0].toJson(),
|
||||
}, "${account.url}/${account.username.toCaseInsensitiveString()}/${files[0].fileId}");
|
||||
},
|
||||
);
|
||||
await DbCompatV5.migrate(appDb);
|
||||
|
||||
final objs =
|
||||
|
|
Loading…
Reference in a new issue