Skip to content

Commit

Permalink
Revert "Optimize cacheMetadata(itemMetadataList:) by using UPSERT"
Browse files Browse the repository at this point in the history
This reverts commit 406887b.
  • Loading branch information
phil1995 committed Jul 19, 2022
1 parent e8ba35b commit 3653b04
Showing 1 changed file with 10 additions and 29 deletions.
39 changes: 10 additions & 29 deletions CryptomatorFileProvider/DB/ItemMetadataDBManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,38 +80,19 @@ class ItemMetadataDBManager: ItemMetadataManager {
}
}

// TODO: Optimize Code and/or DB Scheme
func cacheMetadata(_ itemMetadataList: [ItemMetadata]) throws {
try database.write { db in
for metadata in itemMetadataList {
try db.execute(
sql: """
INSERT INTO \(ItemMetadata.databaseTableName)
(\(ItemMetadata.Columns.name), \(ItemMetadata.Columns.type), \(ItemMetadata.Columns.size), \(ItemMetadata.Columns.parentID), \(ItemMetadata.Columns.lastModifiedDate), \(ItemMetadata.Columns.statusCode), \(ItemMetadata.Columns.cloudPath), \(ItemMetadata.Columns.isPlaceholderItem), \(ItemMetadata.Columns.isMaybeOutdated), \(ItemMetadata.Columns.favoriteRank), \(ItemMetadata.Columns.tagData)) VALUES
(:name, :type, :size, :parentID, :lastModifiedDate, :statusCode, :cloudPath, :isPlaceholderItem, :isMaybeOutdated, :favoriteRank, :tagData)
ON CONFLICT (\(ItemMetadata.Columns.cloudPath))
DO UPDATE SET \(ItemMetadata.Columns.name) = excluded.\(ItemMetadata.Columns.name),
\(ItemMetadata.Columns.type) = excluded.\(ItemMetadata.Columns.type),
\(ItemMetadata.Columns.size) = excluded.\(ItemMetadata.Columns.size),
\(ItemMetadata.Columns.parentID) = excluded.\(ItemMetadata.Columns.parentID),
\(ItemMetadata.Columns.lastModifiedDate) = excluded.\(ItemMetadata.Columns.lastModifiedDate),
\(ItemMetadata.Columns.cloudPath) = excluded.\(ItemMetadata.Columns.cloudPath),
\(ItemMetadata.Columns.isPlaceholderItem) = excluded.\(ItemMetadata.Columns.isPlaceholderItem),
\(ItemMetadata.Columns.isMaybeOutdated) = excluded.\(ItemMetadata.Columns.isMaybeOutdated)
""",
arguments: ["name": metadata.name,
"type": metadata.type,
"size": metadata.size,
"parentID": metadata.parentID,
"lastModifiedDate": metadata.lastModifiedDate,
"statusCode": metadata.statusCode,
"cloudPath": metadata.cloudPath,
"isPlaceholderItem": metadata.isPlaceholderItem,
"isMaybeOutdated": metadata.isMaybeOutdated,
"favoriteRank": metadata.favoriteRank,
"tagData": metadata.tagData]
)
let metadataID = db.lastInsertedRowID
metadata.id = metadataID
if let cachedMetadata = try ItemMetadata.fetchOne(db, key: ["cloudPath": metadata.cloudPath]) {
metadata.id = cachedMetadata.id
metadata.statusCode = cachedMetadata.statusCode
metadata.tagData = cachedMetadata.tagData
metadata.favoriteRank = cachedMetadata.favoriteRank
try metadata.update(db)
} else {
try metadata.insert(db)
}
}
}
}
Expand Down

0 comments on commit 3653b04

Please sign in to comment.