Don't unnecessarily open sync transactions

This commit is contained in:
Niels Andriesse 2021-07-22 13:07:51 +10:00
parent 498f5a73b0
commit 41fe33525c
3 changed files with 7 additions and 9 deletions

View File

@ -387,9 +387,7 @@ public final class SnodeAPI : NSObject {
let (promise, seal) = RawResponsePromise.pending()
let storage = SNSnodeKitConfiguration.shared.storage
Threading.workQueue.async {
storage.writeSync { transaction in
storage.pruneLastMessageHashInfoIfExpired(for: snode, associatedWith: publicKey, using: transaction)
}
storage.pruneLastMessageHashInfoIfExpired(for: snode, associatedWith: publicKey)
let lastHash = storage.getLastMessageHash(for: snode, associatedWith: publicKey) ?? ""
let parameters = [ "pubKey" : Features.useTestnet ? publicKey.removing05PrefixIfNeeded() : publicKey, "lastHash" : lastHash ]
invoke(.getMessages, on: snode, associatedWith: publicKey, parameters: parameters).done2 { seal.fulfill($0) }.catch2 { seal.reject($0) }
@ -403,9 +401,7 @@ public final class SnodeAPI : NSObject {
Threading.workQueue.async {
attempt(maxRetryCount: maxRetryCount, recoveringOn: Threading.workQueue) {
getTargetSnodes(for: publicKey).mapValues2 { targetSnode in
storage.writeSync { transaction in
storage.pruneLastMessageHashInfoIfExpired(for: targetSnode, associatedWith: publicKey, using: transaction)
}
storage.pruneLastMessageHashInfoIfExpired(for: targetSnode, associatedWith: publicKey)
let lastHash = storage.getLastMessageHash(for: targetSnode, associatedWith: publicKey) ?? ""
let parameters = [ "pubKey" : Features.useTestnet ? publicKey.removing05PrefixIfNeeded() : publicKey, "lastHash" : lastHash ]
return invoke(.getMessages, on: targetSnode, associatedWith: publicKey, parameters: parameters).map2 { rawResponse in

View File

@ -103,12 +103,14 @@ extension Storage {
(transaction as! YapDatabaseReadWriteTransaction).setObject(lastMessageHashInfo, forKey: key, inCollection: Storage.lastMessageHashCollection)
}
public func pruneLastMessageHashInfoIfExpired(for snode: Snode, associatedWith publicKey: String, using transaction: Any) {
public func pruneLastMessageHashInfoIfExpired(for snode: Snode, associatedWith publicKey: String) {
guard let lastMessageHashInfo = getLastMessageHashInfo(for: snode, associatedWith: publicKey),
(lastMessageHashInfo["hash"] as? String) != nil, let expirationDate = (lastMessageHashInfo["expirationDate"] as? NSNumber)?.uint64Value else { return }
let now = NSDate.millisecondTimestamp()
if now >= expirationDate {
removeLastMessageHashInfo(for: snode, associatedWith: publicKey, using: transaction)
Storage.writeSync { transaction in
self.removeLastMessageHashInfo(for: snode, associatedWith: publicKey, using: transaction)
}
}
}

View File

@ -22,7 +22,7 @@ public protocol SessionSnodeKitStorageProtocol {
func setSwarm(to swarm: Set<Snode>, for publicKey: String, using transaction: Any)
func getLastMessageHash(for snode: Snode, associatedWith publicKey: String) -> String?
func setLastMessageHashInfo(for snode: Snode, associatedWith publicKey: String, to lastMessageHashInfo: JSON, using transaction: Any)
func pruneLastMessageHashInfoIfExpired(for snode: Snode, associatedWith publicKey: String, using transaction: Any)
func pruneLastMessageHashInfoIfExpired(for snode: Snode, associatedWith publicKey: String)
func getReceivedMessages(for publicKey: String) -> Set<String>
func setReceivedMessages(to receivedMessages: Set<String>, for publicKey: String, using transaction: Any)
}