Fix moving files back and forth causing cache out sync

This commit is contained in:
Ming Ming 2023-05-16 22:58:53 +08:00
parent 022184f30e
commit 929f282098
5 changed files with 37 additions and 20 deletions

View file

@ -549,13 +549,20 @@ class FileSqliteDbDataSource implements FileDataSource {
}
@override
move(
Future<void> move(
Account account,
File f,
String destination, {
bool? shouldOverwrite,
}) async {
// do nothing
_log.info("[move] ${f.path} to $destination");
await _c.sqliteDb.use((db) async {
await db.moveFileByFileId(
sql.ByAccount.app(account),
f.fileId!,
File(path: destination).strippedPathWithEmpty,
);
});
}
@override
@ -786,7 +793,7 @@ class FileCachedDataSource implements FileDataSource {
}
@override
move(
Future<void> move(
Account account,
File f,
String destination, {
@ -794,6 +801,13 @@ class FileCachedDataSource implements FileDataSource {
}) async {
await _remoteSrc.move(account, f, destination,
shouldOverwrite: shouldOverwrite);
try {
await _sqliteDbSrc.move(account, f, destination);
} catch (e, stackTrace) {
// ignore cache failure
_log.warning(
"Failed while move: ${logFilename(f.strippedPath)}", e, stackTrace);
}
}
@override

View file

@ -191,9 +191,7 @@ class FileSqliteCacheUpdater {
) async {
// query list of rowIds for files in [remoteFiles]
final rowIds = await db.accountFileRowIdsByFileIds(
remoteFiles.map((f) => f.fileId!),
sqlAccount: dbAccount,
);
sql.ByAccount.sql(dbAccount), remoteFiles.map((f) => f.fileId!));
final rowIdsMap = Map.fromEntries(rowIds.map((e) => MapEntry(e.fileId, e)));
final inserts = <sql.CompleteFileCompanion>[];

View file

@ -320,11 +320,7 @@ extension SqliteDbExtension on SqliteDb {
///
/// Returned files are NOT guaranteed to be sorted as [fileIds]
Future<List<AccountFileRowIdsWithFileId>> accountFileRowIdsByFileIds(
Iterable<int> fileIds, {
Account? sqlAccount,
app.Account? appAccount,
}) {
assert((sqlAccount != null) != (appAccount != null));
ByAccount account, Iterable<int> fileIds) {
return fileIds.withPartition((sublist) {
final query = queryFiles().run((q) {
q.setQueryMode(FilesQueryMode.expression, expressions: [
@ -333,10 +329,10 @@ extension SqliteDbExtension on SqliteDb {
accountFiles.file,
files.fileId,
]);
if (sqlAccount != null) {
q.setSqlAccount(sqlAccount);
if (account.sqlAccount != null) {
q.setSqlAccount(account.sqlAccount!);
} else {
q.setAppAccount(appAccount!);
q.setAppAccount(account.appAccount!);
}
q.byFileIds(sublist);
return q.build();
@ -477,6 +473,16 @@ extension SqliteDbExtension on SqliteDb {
}, maxByFileIdsSize);
}
Future<void> moveFileByFileId(
ByAccount account, int fileId, String destinationRelativePath) async {
final rowId = (await accountFileRowIdsByFileIds(account, [fileId])).first;
final q = update(accountFiles)
..where((t) => t.rowId.equals(rowId.accountFileRowId));
await q.write(AccountFilesCompanion(
relativePath: Value(destinationRelativePath),
));
}
Future<List<Tag>> allTags({
Account? sqlAccount,
app.Account? appAccount,

View file

@ -42,8 +42,8 @@ class CacheFavorite {
var updateCount = 0;
if (newFileIds.isNotEmpty) {
final rowIds = await db.accountFileRowIdsByFileIds(newFileIds,
sqlAccount: dbAccount);
final rowIds = await db.accountFileRowIdsByFileIds(
sql.ByAccount.sql(dbAccount), newFileIds);
final counts =
await rowIds.map((id) => id.accountFileRowId).withPartition(
(sublist) async {

View file

@ -478,12 +478,11 @@ Future<void> insertFiles(
Future<void> insertDirRelation(
sql.SqliteDb db, Account account, File dir, Iterable<File> children) async {
final dbAccount = await db.accountOf(account);
final dirRowIds = (await db
.accountFileRowIdsByFileIds([dir.fileId!], sqlAccount: dbAccount))
final dirRowIds = (await db.accountFileRowIdsByFileIds(
sql.ByAccount.sql(dbAccount), [dir.fileId!]))
.first;
final childRowIds = await db.accountFileRowIdsByFileIds(
[dir, ...children].map((f) => f.fileId!),
sqlAccount: dbAccount);
sql.ByAccount.sql(dbAccount), [dir, ...children].map((f) => f.fileId!));
await db.batch((batch) {
batch.insertAll(
db.dirFiles,