Updated the SnodeSet table name to match the type

Shifted all GRDB Snode convenience methods to be extensions on Snode (instead of SnodeSet) for consistency
pull/612/head
Morgan Pretty 3 years ago
parent 63db2a4e3d
commit 410f37f0d5

@ -65,7 +65,7 @@ extension Snode {
}
}
// MARK: - Convenience
// MARK: - GRDB Interactions
internal extension Snode {
static func fetchSet(_ db: Database, publicKey: String) throws -> Set<Snode> {
@ -77,9 +77,41 @@ internal extension Snode {
)
.fetchSet(db)
}
static func fetchAllOnionRequestPaths(_ db: Database) throws -> [[Snode]] {
struct ResultWrapper: Decodable, FetchableRecord {
let key: String
let nodeIndex: Int
let address: String
let port: UInt16
let snode: Snode
}
return try SnodeSet
.filter(SnodeSet.Columns.key.like("\(SnodeSet.onionRequestPathPrefix)%"))
.order(SnodeSet.Columns.nodeIndex)
.order(SnodeSet.Columns.key)
.including(required: SnodeSet.node)
.asRequest(of: ResultWrapper.self)
.fetchAll(db)
.reduce(into: [:]) { prev, next in // Reducing will lose the 'key' sorting
prev[next.key] = (prev[next.key] ?? []).appending(next.snode)
}
.asArray()
.sorted(by: { lhs, rhs in lhs.key < rhs.key })
.compactMap { _, nodes in !nodes.isEmpty ? nodes : nil } // Exclude empty sets
}
static func clearOnionRequestPaths(_ db: Database) throws {
try SnodeSet
.filter(SnodeSet.Columns.key.like("\(SnodeSet.onionRequestPathPrefix)%"))
.deleteAll(db)
}
}
internal extension Collection where Element == Snode {
/// This method is used to save Swarms
func save(_ db: Database, key: String) throws {
try self.enumerated().forEach { nodeIndex, node in
try node.save(db)
@ -95,6 +127,7 @@ internal extension Collection where Element == Snode {
}
internal extension Collection where Element == [Snode] {
/// This method is used to save onion reuqest paths
func save(_ db: Database) throws {
try self.enumerated().forEach { pathIndex, path in
try path.save(db, key: "\(SnodeSet.onionRequestPathPrefix)\(pathIndex)")

@ -36,7 +36,11 @@ public extension SnodeReceivedMessageInfo {
self.hash = hash
self.expirationDateMs = (expirationDateMs ?? 0)
}
}
// MARK: - GRDB Interactions
public extension SnodeReceivedMessageInfo {
static func pruneLastMessageHashInfoIfExpired(for snode: Snode, associatedWith publicKey: String) {
// Clear out the 'expirationDateMs' value for all expired (but non-0) message infos
GRDBStorage.shared.write { db in

@ -6,7 +6,7 @@ import SessionUtilitiesKit
public struct SnodeSet: Codable, FetchableRecord, EncodableRecord, PersistableRecord, TableRecord, ColumnExpressible {
public static let onionRequestPathPrefix = "OnionRequestPath-"
public static var databaseTableName: String { "snodeSetAssociation" }
public static var databaseTableName: String { "snodeSet" }
static let node = hasOne(Snode.self, using: Snode.snodeSetForeignKey)
public typealias Columns = CodingKeys
@ -26,37 +26,3 @@ public struct SnodeSet: Codable, FetchableRecord, EncodableRecord, PersistableRe
request(for: SnodeSet.node)
}
}
// MARK: - Convenience
internal extension SnodeSet {
static func fetchAllOnionRequestPaths(_ db: Database) throws -> [[Snode]] {
struct ResultWrapper: Decodable, FetchableRecord {
let key: String
let nodeIndex: Int
let address: String
let port: UInt16
let snode: Snode
}
return try SnodeSet
.filter(SnodeSet.Columns.key.like("\(SnodeSet.onionRequestPathPrefix)%"))
.order(SnodeSet.Columns.nodeIndex)
.order(SnodeSet.Columns.key)
.including(required: SnodeSet.node)
.asRequest(of: ResultWrapper.self)
.fetchAll(db)
.reduce(into: [:]) { prev, next in // Reducing will lose the 'key' sorting
prev[next.key] = (prev[next.key] ?? []).appending(next.snode)
}
.asArray()
.sorted(by: { lhs, rhs in lhs.key < rhs.key })
.compactMap { _, nodes in !nodes.isEmpty ? nodes : nil } // Exclude empty sets
}
static func clearOnionRequestPaths(_ db: Database) throws {
try SnodeSet
.filter(SnodeSet.Columns.key.like("\(SnodeSet.onionRequestPathPrefix)%"))
.deleteAll(db)
}
}

@ -24,7 +24,7 @@ public enum OnionRequestAPI {
if let paths: [[Snode]] = _paths { return paths }
let results: [[Snode]]? = GRDBStorage.shared.read { db in
try? SnodeSet.fetchAllOnionRequestPaths(db)
try? Snode.fetchAllOnionRequestPaths(db)
}
if results?.isEmpty == false { _paths = results }
@ -283,7 +283,7 @@ public enum OnionRequestAPI {
GRDBStorage.shared.write { db in
guard !paths.isEmpty else {
SNLog("Clearing onion request paths.")
try? SnodeSet.clearOnionRequestPaths(db)
try? Snode.clearOnionRequestPaths(db)
return
}

Loading…
Cancel
Save