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 @override
move( Future<void> move(
Account account, Account account,
File f, File f,
String destination, { String destination, {
bool? shouldOverwrite, bool? shouldOverwrite,
}) async { }) 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 @override
@ -786,7 +793,7 @@ class FileCachedDataSource implements FileDataSource {
} }
@override @override
move( Future<void> move(
Account account, Account account,
File f, File f,
String destination, { String destination, {
@ -794,6 +801,13 @@ class FileCachedDataSource implements FileDataSource {
}) async { }) async {
await _remoteSrc.move(account, f, destination, await _remoteSrc.move(account, f, destination,
shouldOverwrite: shouldOverwrite); 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 @override

View file

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

View file

@ -320,11 +320,7 @@ extension SqliteDbExtension on SqliteDb {
/// ///
/// Returned files are NOT guaranteed to be sorted as [fileIds] /// Returned files are NOT guaranteed to be sorted as [fileIds]
Future<List<AccountFileRowIdsWithFileId>> accountFileRowIdsByFileIds( Future<List<AccountFileRowIdsWithFileId>> accountFileRowIdsByFileIds(
Iterable<int> fileIds, { ByAccount account, Iterable<int> fileIds) {
Account? sqlAccount,
app.Account? appAccount,
}) {
assert((sqlAccount != null) != (appAccount != null));
return fileIds.withPartition((sublist) { return fileIds.withPartition((sublist) {
final query = queryFiles().run((q) { final query = queryFiles().run((q) {
q.setQueryMode(FilesQueryMode.expression, expressions: [ q.setQueryMode(FilesQueryMode.expression, expressions: [
@ -333,10 +329,10 @@ extension SqliteDbExtension on SqliteDb {
accountFiles.file, accountFiles.file,
files.fileId, files.fileId,
]); ]);
if (sqlAccount != null) { if (account.sqlAccount != null) {
q.setSqlAccount(sqlAccount); q.setSqlAccount(account.sqlAccount!);
} else { } else {
q.setAppAccount(appAccount!); q.setAppAccount(account.appAccount!);
} }
q.byFileIds(sublist); q.byFileIds(sublist);
return q.build(); return q.build();
@ -477,6 +473,16 @@ extension SqliteDbExtension on SqliteDb {
}, maxByFileIdsSize); }, 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({ Future<List<Tag>> allTags({
Account? sqlAccount, Account? sqlAccount,
app.Account? appAccount, app.Account? appAccount,

View file

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

View file

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