-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathResultSet.swift
206 lines (180 loc) · 6.95 KB
/
ResultSet.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import Differ
import Schemata
/// An ordered set of results from a query.
///
/// Result sets that haven't been grouped use `None` as `Key`.
public struct ResultSet<Key: Hashable, Projection: PersistDB.ModelProjection>: Hashable {
/// The groups in the result set.
public private(set) var groups: [Group<Key, Projection>]
/// All the values from all the groups in the set.
public private(set) var values: [Projection]
/// Whether the result set has been grouped by some key.
public let isGrouped: Bool
/// Create an empty result set.
public init() {
self.init([])
}
fileprivate init(_ groups: [Group<Key, Projection>], isGrouped: Bool) {
let values = groups.flatMap { $0.values }
precondition(Set(groups.map { $0.key }).count == groups.count)
precondition(Set(values).count == values.count)
self.groups = groups
self.values = values
self.isGrouped = isGrouped
}
/// Create a result set with the given groups.
public init(_ groups: [Group<Key, Projection>]) {
self.init(groups, isGrouped: Key.self != None.self)
}
}
extension ResultSet {
/// All the keys from all the groups.
public var keys: [Key] {
return groups.map { $0.key }
}
/// Return a new result set where the group keys have been transformed.
///
/// - important: The keys **must** remain unique.
public func mapKeys<T>(_ transform: (Key) -> T) -> ResultSet<T, Projection> {
let groups = self.groups.map { Group(key: transform($0.key), values: $0.values) }
return ResultSet<T, Projection>(groups, isGrouped: isGrouped)
}
}
extension ResultSet where Key == None {
/// Create a ungrouped result set with the given projections.
public init(_ projections: [Projection]) {
self.init([ Group(key: .none, values: projections) ])
}
}
extension ResultSet: Collection {
public var startIndex: Int {
return values.startIndex
}
public var endIndex: Int {
return values.endIndex
}
public subscript(_ i: Int) -> Projection {
return values[i]
}
public func index(after i: Int) -> Int {
return values.index(after: i)
}
}
private func diff<K, P>(
from old: ResultSet<K, P>,
to new: ResultSet<K, P>
) -> ResultSet<K, P>.Diff {
var diff = ResultSet<K, P>.Diff()
func indicesByID(in resultSet: ResultSet<K, P>) -> [P.Model.ID: (Int, Int)] {
var result: [P.Model.ID: (Int, Int)] = [:]
for (g, group) in resultSet.groups.enumerated() {
for (v, value) in group.values.enumerated() {
result[value.id] = (g, v)
}
}
return result
}
let oldIndicesByID = indicesByID(in: old)
let newIndicesByID = indicesByID(in: new)
// 1. Diff the group keys by themselves.
//
// This generates all `deleteGroup`, `insertGroup`, and `moveGroup`s.
let groupsDiff = old.keys.extendedDiff(new.keys)
let groupDeltas = groupsDiff
.elements
.map { element -> ResultSet<K, P>.Diff.Delta in
switch element {
case let .insert(index):
return .insertGroup(index)
case let .delete(index):
return .deleteGroup(index)
case let .move(from, to):
return .moveGroup(from, to)
}
}
diff.deltas.formUnion(groupDeltas)
// 2. Diff the IDs to find inserts, deletes, and moves
var oldGroups = old.groups
for case let .move(from, to) in groupsDiff.patch(from: old.keys, to: new.keys) {
let group = oldGroups.remove(at: from)
oldGroups.insert(group, at: to)
}
let oldIDs = oldGroups.flatMap { $0.values }.map { $0.id }
let newIDs = new.values.map { $0.id }
let valueElements = oldIDs.extendedDiff(newIDs).elements
let valueDeltas = valueElements.compactMap { element -> ResultSet<K, P>.Diff.Delta? in
switch element {
case let .insert(index):
let id = new[index].id
let new = newIndicesByID[id]!
return .insertValue(new.0, new.1)
case let .delete(index):
let id = old[index].id
let old = oldIndicesByID[id]!
return .deleteValue(old.0, old.1)
case let .move(from, _):
let id = old[from].id
let old = oldIndicesByID[id]!
let new = newIndicesByID[id]!
return .moveValue(old.0, old.1, new.0, new.1)
}
}
diff.deltas.formUnion(valueDeltas)
// 3. Find updated values and values that shifted group boundaries
let updated = new
.compactMap { newValue -> ResultSet<K, P>.Diff.Delta? in
guard let oldIndex = oldIndicesByID[newValue.id] else { return nil }
let newIndex = newIndicesByID[newValue.id]!
let oldValue = old.groups[oldIndex.0].values[oldIndex.1]
if oldValue != newValue {
return .updateValue(newIndex.0, newIndex.1)
}
if old.groups[oldIndex.0].key != new.groups[newIndex.0].key {
return .moveValue(oldIndex.0, oldIndex.1, newIndex.0, newIndex.1)
}
return nil
}
diff.deltas.formUnion(updated)
return diff
}
extension ResultSet {
/// The difference between two result sets.
public struct Diff: Hashable {
/// A change within a diff.
public enum Delta: Hashable {
/// The group at the given index in the old set was deleted.
///
/// Values within the group are assumed to be deleted unless in a `.updateValue`.
case deleteGroup(Int)
/// The group at the given index in the new set was inserted.
case insertGroup(Int)
/// The group was moved from the given index in the old set to the given index in the
/// new set.
case moveGroup(Int, Int)
/// The value at the given group and value indices in the old set was deleted.
case deleteValue(Int, Int)
/// The value at the given group and value indices in the new set was inserted.
case insertValue(Int, Int)
/// The value was moved from the given group and value indices in the old set to the
/// given group and value indices in the new set.
case moveValue(Int, Int, Int, Int)
/// The values at the given group and value indices in the new set was updated, but
/// not moved.
case updateValue(Int, Int)
}
/// The changes that make up the diff.
public var deltas: Set<Delta>
/// Create an empty diff.
public init() {
deltas = []
}
/// Create a diff with the given deltas.
public init(_ deltas: Set<Delta>) {
self.deltas = deltas
}
}
/// Calculate the diff from `resultSet` to `self`.
public func diff(from resultSet: ResultSet) -> Diff {
return PersistDB.diff(from: resultSet, to: self)
}
}