Remove cache from DatabaseLookupTable because it made the code too complex. If profiling says we need caching, we can add it back later.
This commit is contained in:
parent
258abab9f6
commit
45063d2d96
|
@ -318,137 +318,3 @@ private extension DatabaseLookupTable {
|
|||
}
|
||||
}
|
||||
|
||||
// MARK: -
|
||||
|
||||
private struct LookupTable {
|
||||
|
||||
private let dictionary: [String: Set<String>] // objectID: Set<relatedObjectID>
|
||||
|
||||
init(dictionary: [String: Set<String>]) {
|
||||
|
||||
self.dictionary = dictionary
|
||||
}
|
||||
|
||||
init(lookupValues: Set<LookupValue>) {
|
||||
|
||||
var d = [String: Set<String>]()
|
||||
|
||||
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<String> {
|
||||
|
||||
return Set(dictionary.keys)
|
||||
}
|
||||
|
||||
func relatedObjectIDs() -> Set<String> {
|
||||
|
||||
var ids = Set<String>()
|
||||
for (_, relatedObjectIDs) in dictionary {
|
||||
ids.formUnion(relatedObjectIDs)
|
||||
}
|
||||
return ids
|
||||
}
|
||||
|
||||
subscript(_ objectID: String) -> Set<String>? {
|
||||
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<String>()
|
||||
private let relationshipName: String
|
||||
private var cachedLookups = [String: Set<String>]() // objectID: Set<relatedObjectID>
|
||||
|
||||
init(_ relationshipName: String) {
|
||||
|
||||
self.relationshipName = relationshipName
|
||||
}
|
||||
|
||||
func update(with objects: [DatabaseObject]) {
|
||||
|
||||
var idsWithRelationships = Set<String>()
|
||||
var idsWithNoRelationships = Set<String>()
|
||||
|
||||
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<String>? {
|
||||
get {
|
||||
return cachedLookups[objectID]
|
||||
}
|
||||
set {
|
||||
cachedLookups[objectID] = newValue
|
||||
}
|
||||
}
|
||||
|
||||
func objectIDsThatMayHaveRelatedObjects(_ objectIDs: Set<String>) -> Set<String> {
|
||||
|
||||
// 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<String>) -> LookupTable {
|
||||
|
||||
var d = [String: Set<String>]()
|
||||
for objectID in objectIDs {
|
||||
if let relatedObjectIDs = self[objectID] {
|
||||
d[objectID] = relatedObjectIDs
|
||||
}
|
||||
}
|
||||
return LookupTable(dictionary: d)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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]]()
|
||||
|
||||
|
|
Loading…
Reference in New Issue