From 45063d2d96dd7bb3c23a72ec10bd620216504d06 Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Mon, 11 Sep 2017 13:15:12 -0700 Subject: [PATCH] Remove cache from DatabaseLookupTable because it made the code too complex. If profiling says we need caching, we can add it back later. --- .../Related Objects/DatabaseLookupTable.swift | 134 ------------------ .../Related Objects/RelatedObjectIDsMap.swift | 19 +++ .../Related Objects/RelatedObjectsMap.swift | 2 +- 3 files changed, 20 insertions(+), 135 deletions(-) diff --git a/Frameworks/RSDatabase/Related Objects/DatabaseLookupTable.swift b/Frameworks/RSDatabase/Related Objects/DatabaseLookupTable.swift index 88bc9c2e1..1250004e9 100644 --- a/Frameworks/RSDatabase/Related Objects/DatabaseLookupTable.swift +++ b/Frameworks/RSDatabase/Related Objects/DatabaseLookupTable.swift @@ -318,137 +318,3 @@ private extension DatabaseLookupTable { } } -// MARK: - - -private struct LookupTable { - - private let dictionary: [String: Set] // objectID: Set - - init(dictionary: [String: Set]) { - - self.dictionary = dictionary - } - - init(lookupValues: Set) { - - var d = [String: Set]() - - for lookupValue in lookupValues { - let objectID = lookupValue.objectID - let relatedObjectID: String = lookupValue.relatedObjectID - if d[objectID] == nil { - d[objectID] = Set([relatedObjectID]) - } - else { - d[objectID]!.insert(relatedObjectID) - } - } - - self.init(dictionary: d) - } - - func objectIDs() -> Set { - - return Set(dictionary.keys) - } - - func relatedObjectIDs() -> Set { - - var ids = Set() - for (_, relatedObjectIDs) in dictionary { - ids.formUnion(relatedObjectIDs) - } - return ids - } - - subscript(_ objectID: String) -> Set? { - get { - return dictionary[objectID] - } - } -} - -private struct LookupValue: Hashable { - - let objectID: String - let relatedObjectID: String - let hashValue: Int - - init(objectID: String, relatedObjectID: String) { - - self.objectID = objectID - self.relatedObjectID = relatedObjectID - self.hashValue = (objectID + relatedObjectID).hashValue - } - - static public func ==(lhs: LookupValue, rhs: LookupValue) -> Bool { - - return lhs.objectID == rhs.objectID && lhs.relatedObjectID == rhs.relatedObjectID - } -} - -private final class DatabaseLookupTableCache { - - var objectIDsWithNoRelationship = Set() - private let relationshipName: String - private var cachedLookups = [String: Set]() // objectID: Set - - init(_ relationshipName: String) { - - self.relationshipName = relationshipName - } - - func update(with objects: [DatabaseObject]) { - - var idsWithRelationships = Set() - var idsWithNoRelationships = Set() - - for object in objects { - let objectID = object.databaseID - if let relatedObjects = object.relatedObjectsWithName(relationshipName), !relatedObjects.isEmpty { - idsWithRelationships.insert(objectID) - self[objectID] = relatedObjects.databaseIDs() - } - else { - idsWithNoRelationships.insert(objectID) - self[objectID] = nil - } - } - - objectIDsWithNoRelationship.subtract(idsWithRelationships) - objectIDsWithNoRelationship.formUnion(idsWithNoRelationships) - } - - subscript(_ objectID: String) -> Set? { - get { - return cachedLookups[objectID] - } - set { - cachedLookups[objectID] = newValue - } - } - - func objectIDsThatMayHaveRelatedObjects(_ objectIDs: Set) -> Set { - - // Filter out objects that are known to have no related objects - return Set(objectIDs.filter{ !objectIDsWithNoRelationship.contains($0) }) - } - -// func objectsThatMayHaveRelatedObjects(_ objects: [DatabaseObject]) -> [DatabaseObject] { -// -// // Filter out objects that are known to have no related objects -// return objects.filter{ !objectIDsWithNoRelationship.contains($0.databaseID) } -// } - - func lookupTableForObjectIDs(_ objectIDs: Set) -> LookupTable { - - var d = [String: Set]() - for objectID in objectIDs { - if let relatedObjectIDs = self[objectID] { - d[objectID] = relatedObjectIDs - } - } - return LookupTable(dictionary: d) - } -} - diff --git a/Frameworks/RSDatabase/Related Objects/RelatedObjectIDsMap.swift b/Frameworks/RSDatabase/Related Objects/RelatedObjectIDsMap.swift index ea034af53..6548ff374 100644 --- a/Frameworks/RSDatabase/Related Objects/RelatedObjectIDsMap.swift +++ b/Frameworks/RSDatabase/Related Objects/RelatedObjectIDsMap.swift @@ -57,3 +57,22 @@ struct RelatedObjectIDsMap { } } } + +struct LookupValue: Hashable { + + let objectID: String + let relatedObjectID: String + let hashValue: Int + + init(objectID: String, relatedObjectID: String) { + + self.objectID = objectID + self.relatedObjectID = relatedObjectID + self.hashValue = (objectID + relatedObjectID).hashValue + } + + static func ==(lhs: LookupValue, rhs: LookupValue) -> Bool { + + return lhs.objectID == rhs.objectID && lhs.relatedObjectID == rhs.relatedObjectID + } +} diff --git a/Frameworks/RSDatabase/Related Objects/RelatedObjectsMap.swift b/Frameworks/RSDatabase/Related Objects/RelatedObjectsMap.swift index bcbbb6f5b..62e7f0fdb 100644 --- a/Frameworks/RSDatabase/Related Objects/RelatedObjectsMap.swift +++ b/Frameworks/RSDatabase/Related Objects/RelatedObjectsMap.swift @@ -14,7 +14,7 @@ public struct RelatedObjectsMap { private let dictionary: [String: [DatabaseObject]] // objectID: relatedObjects - init(relatedObjects: relatedObjects, lookupTable: LookupTable) { + init(relatedObjects: relatedObjects, lookupTable: RelatedObjectIDsMap) { var d = [String: [DatabaseObject]]()