2017-09-11 15:46:32 +02:00
|
|
|
|
//
|
2017-09-11 22:11:29 +02:00
|
|
|
|
// RelatedObjectsMap.swift
|
2017-09-11 15:46:32 +02:00
|
|
|
|
// RSDatabase
|
|
|
|
|
//
|
|
|
|
|
// Created by Brent Simmons on 9/10/17.
|
|
|
|
|
// Copyright © 2017 Ranchero Software, LLC. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
|
|
2017-09-11 22:11:29 +02:00
|
|
|
|
// Map objectID to [DatabaseObject] (related objects).
|
2017-09-12 06:58:22 +02:00
|
|
|
|
// It’s used as the return value for DatabaseLookupTable.fetchRelatedObjects.
|
2017-09-11 22:11:29 +02:00
|
|
|
|
|
|
|
|
|
public struct RelatedObjectsMap {
|
2017-09-11 15:46:32 +02:00
|
|
|
|
|
|
|
|
|
private let dictionary: [String: [DatabaseObject]] // objectID: relatedObjects
|
|
|
|
|
|
2017-09-12 06:58:22 +02:00
|
|
|
|
init(relatedObjects: [DatabaseObject], relatedObjectIDsMap: RelatedObjectIDsMap) {
|
2017-09-11 15:46:32 +02:00
|
|
|
|
|
|
|
|
|
var d = [String: [DatabaseObject]]()
|
|
|
|
|
let relatedObjectsDictionary = relatedObjects.dictionary()
|
|
|
|
|
|
2017-09-12 06:58:22 +02:00
|
|
|
|
for objectID in relatedObjectIDsMap.objectIDs() {
|
2017-09-11 15:46:32 +02:00
|
|
|
|
|
2017-09-12 06:58:22 +02:00
|
|
|
|
if let relatedObjectIDs = relatedObjectIDsMap[objectID] {
|
2018-01-28 03:50:48 +01:00
|
|
|
|
let relatedObjects = relatedObjectIDs.compactMap{ relatedObjectsDictionary[$0] }
|
2017-09-11 15:46:32 +02:00
|
|
|
|
if !relatedObjects.isEmpty {
|
|
|
|
|
d[objectID] = relatedObjects
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self.dictionary = d
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public func objectIDs() -> Set<String> {
|
|
|
|
|
|
|
|
|
|
return Set(dictionary.keys)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public subscript(_ objectID: String) -> [DatabaseObject]? {
|
2018-02-14 22:14:25 +01:00
|
|
|
|
return dictionary[objectID]
|
2017-09-11 15:46:32 +02:00
|
|
|
|
}
|
|
|
|
|
}
|