Added FileProviderMaterialisedEnumerationObserver to handle clean up operations after materialisation changes

Signed-off-by: Claudio Cambra <claudio.cambra@nextcloud.com>
This commit is contained in:
Claudio Cambra 2023-03-13 13:28:08 +01:00
Родитель 52f5b6a524
Коммит 61890ddd72
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: C839200C384636B0
2 изменённых файлов: 76 добавлений и 2 удалений

Просмотреть файл

@ -0,0 +1,70 @@
/*
* Copyright (C) 2023 by Claudio Cambra <claudio.cambra@nextcloud.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*/
import Foundation
import FileProvider
class FileProviderMaterialisedEnumerationObserver : NSObject, NSFileProviderEnumerationObserver {
let ncKitAccount: String
var allEnumeratedItemIds: Set<String> = Set<String>()
required init(ncKitAccount: String) {
self.ncKitAccount = ncKitAccount
super.init()
}
func didEnumerate(_ updatedItems: [NSFileProviderItemProtocol]) {
let updatedItemsIds = Array(updatedItems.map { $0.itemIdentifier.rawValue })
for updatedItemsId in updatedItemsIds {
allEnumeratedItemIds.insert(updatedItemsId)
}
}
func finishEnumerating(upTo nextPage: NSFileProviderPage?) {
NSLog("Handling enumerated materialised items.")
FileProviderMaterialisedEnumerationObserver.handleEnumeratedItems(self.allEnumeratedItemIds,
account: self.ncKitAccount)
}
func finishEnumeratingWithError(_ error: Error) {
NSLog("Ran into error when enumerating materialised items. Handling items enumerated so far")
FileProviderMaterialisedEnumerationObserver.handleEnumeratedItems(self.allEnumeratedItemIds,
account: self.ncKitAccount)
}
static func handleEnumeratedItems(_ itemIds: Set<String>, account: String) {
let dbManager = NextcloudFilesDatabaseManager.shared
let databaseLocalFileMetadatas = dbManager.localFileMetadatas(account: account)
var noLongerMaterialisedIds = Set<String>()
DispatchQueue.global(qos: .background).async {
for localFile in databaseLocalFileMetadatas {
let localFileOcId = localFile.ocId
guard itemIds.contains(localFileOcId) else {
noLongerMaterialisedIds.insert(localFileOcId)
continue;
}
}
DispatchQueue.main.async {
NSLog("Cleaning up local file metadatas for unmaterialised items")
for itemId in noLongerMaterialisedIds {
dbManager.deleteLocalFileMetadata(ocId: itemId)
}
}
}
}
}

Просмотреть файл

@ -14,6 +14,7 @@
5318AD9129BF42FB00CBB71C /* NextcloudItemMetadataTable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5318AD9029BF42FB00CBB71C /* NextcloudItemMetadataTable.swift */; };
5318AD9329BF432B00CBB71C /* NextcloudDirectoryMetadataTable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5318AD9229BF432B00CBB71C /* NextcloudDirectoryMetadataTable.swift */; };
5318AD9529BF438F00CBB71C /* NextcloudLocalFileMetadataTable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5318AD9429BF438F00CBB71C /* NextcloudLocalFileMetadataTable.swift */; };
5318AD9729BF493600CBB71C /* FileProviderMaterialisedEnumerationObserver.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5318AD9629BF493600CBB71C /* FileProviderMaterialisedEnumerationObserver.swift */; };
5352E85B29B7BFE6002CE85C /* Progress+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5352E85A29B7BFE6002CE85C /* Progress+Extensions.swift */; };
536EFBF7295CF58100F4CB13 /* FileProviderSocketLineProcessor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 536EFBF6295CF58100F4CB13 /* FileProviderSocketLineProcessor.swift */; };
536EFC36295E3C1100F4CB13 /* NextcloudAccount.swift in Sources */ = {isa = PBXBuildFile; fileRef = 536EFC35295E3C1100F4CB13 /* NextcloudAccount.swift */; };
@ -136,6 +137,7 @@
5318AD9029BF42FB00CBB71C /* NextcloudItemMetadataTable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NextcloudItemMetadataTable.swift; sourceTree = "<group>"; };
5318AD9229BF432B00CBB71C /* NextcloudDirectoryMetadataTable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NextcloudDirectoryMetadataTable.swift; sourceTree = "<group>"; };
5318AD9429BF438F00CBB71C /* NextcloudLocalFileMetadataTable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NextcloudLocalFileMetadataTable.swift; sourceTree = "<group>"; };
5318AD9629BF493600CBB71C /* FileProviderMaterialisedEnumerationObserver.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FileProviderMaterialisedEnumerationObserver.swift; sourceTree = "<group>"; };
5352E85A29B7BFE6002CE85C /* Progress+Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Progress+Extensions.swift"; sourceTree = "<group>"; };
536EFBF6295CF58100F4CB13 /* FileProviderSocketLineProcessor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FileProviderSocketLineProcessor.swift; sourceTree = "<group>"; };
536EFC35295E3C1100F4CB13 /* NextcloudAccount.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NextcloudAccount.swift; sourceTree = "<group>"; };
@ -213,8 +215,8 @@
isa = PBXGroup;
children = (
5307A6F129675346001E0C6A /* NextcloudFilesDatabaseManager.swift */,
5318AD9029BF42FB00CBB71C /* NextcloudItemMetadataTable.swift */,
5318AD9229BF432B00CBB71C /* NextcloudDirectoryMetadataTable.swift */,
5318AD9029BF42FB00CBB71C /* NextcloudItemMetadataTable.swift */,
5318AD9429BF438F00CBB71C /* NextcloudLocalFileMetadataTable.swift */,
);
path = Database;
@ -244,9 +246,10 @@
538E397027F4765000FA63D5 /* FileProviderEnumerator.swift */,
538E396C27F4765000FA63D5 /* FileProviderExtension.swift */,
538E396E27F4765000FA63D5 /* FileProviderItem.swift */,
5318AD9629BF493600CBB71C /* FileProviderMaterialisedEnumerationObserver.swift */,
536EFBF6295CF58100F4CB13 /* FileProviderSocketLineProcessor.swift */,
536EFC35295E3C1100F4CB13 /* NextcloudAccount.swift */,
53D056302970594F00988392 /* LocalFilesUtils.swift */,
536EFC35295E3C1100F4CB13 /* NextcloudAccount.swift */,
538E397327F4765000FA63D5 /* FileProviderExt.entitlements */,
538E397227F4765000FA63D5 /* Info.plist */,
);
@ -563,6 +566,7 @@
53D056312970594F00988392 /* LocalFilesUtils.swift in Sources */,
538E396F27F4765000FA63D5 /* FileProviderItem.swift in Sources */,
5318AD9129BF42FB00CBB71C /* NextcloudItemMetadataTable.swift in Sources */,
5318AD9729BF493600CBB71C /* FileProviderMaterialisedEnumerationObserver.swift in Sources */,
538E397127F4765000FA63D5 /* FileProviderEnumerator.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;