/
CachingItemSource.swift
188 lines (154 loc) · 6.14 KB
/
CachingItemSource.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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
import Deferred
import Foundation
import Shared
private let log = Logger.syncLogger
private class CachedSource {
// We track not just mappings between values and non-nil items, but also whether we've tried
// to look up a value at all. This allows us to distinguish between a cache miss and a
// cache hit that didn't find an item in the backing store.
// We expect, given our prefetching, that cache misses will be rare.
private var cache: [GUID: BookmarkMirrorItem] = [:]
private var seen: Set<GUID> = Set()
subscript(guid: GUID) -> BookmarkMirrorItem? {
get {
return self.cache[guid]
}
set(value) {
self.cache[guid] = value
}
}
func lookup(guid: GUID) -> Deferred<Maybe<BookmarkMirrorItem>>? {
guard self.seen.contains(guid) else {
log.warning("Cache miss for \(guid).")
return nil
}
guard let found = self.cache[guid] else {
log.verbose("Cache hit, but no record found for \(guid).")
return deferMaybe(NoSuchRecordError(guid: guid))
}
log.verbose("Cache hit for \(guid).")
return deferMaybe(found)
}
var isEmpty: Bool {
return self.cache.isEmpty
}
// fill and seen are separate: we won't find every item in the DB.
func fill(items: [GUID: BookmarkMirrorItem]) -> Success {
for (x, y) in items {
self.cache[x] = y
}
return succeed()
}
func markSeen(guid: GUID) {
self.seen.insert(guid)
}
func markSeen<T: CollectionType where T.Generator.Element == GUID>(guids: T) {
self.seen.unionInPlace(guids)
}
func takingGUIDs<T: CollectionType where T.Generator.Element == GUID>(guids: T) -> Deferred<Maybe<[GUID: BookmarkMirrorItem]>> {
var out: [GUID: BookmarkMirrorItem] = [:]
guids.forEach {
if let v = self.cache[$0] {
out[$0] = v
}
}
return deferMaybe(out)
}
}
// Sorry about the boilerplate.
// These are separate protocols so that the method names don't collide when implemented
// by the same class, but that means extracting more base implementation is more trouble than
// it's worth.
public class CachingLocalItemSource: LocalItemSource {
private let cache: CachedSource
private let source: LocalItemSource
public init(source: LocalItemSource) {
self.cache = CachedSource()
self.source = source
}
public func getLocalItemWithGUID(guid: GUID) -> Deferred<Maybe<BookmarkMirrorItem>> {
if let found = self.cache.lookup(guid) {
return found
}
return self.source.getLocalItemWithGUID(guid) >>== effect {
self.cache.markSeen(guid)
self.cache[guid] = $0
}
}
public func getLocalItemsWithGUIDs<T: CollectionType where T.Generator.Element == GUID>(guids: T) -> Deferred<Maybe<[GUID: BookmarkMirrorItem]>> {
return self.prefetchLocalItemsWithGUIDs(guids) >>> { self.cache.takingGUIDs(guids) }
}
public func prefetchLocalItemsWithGUIDs<T: CollectionType where T.Generator.Element == GUID>(guids: T) -> Success {
log.debug("Prefetching \(guids.count) local items: \(guids.prefix(10))….")
if guids.isEmpty {
return succeed()
}
return self.source.getLocalItemsWithGUIDs(guids) >>== {
self.cache.markSeen(guids)
return self.cache.fill($0)
}
}
}
public class CachingMirrorItemSource: MirrorItemSource {
private let cache: CachedSource
private let source: MirrorItemSource
public init(source: MirrorItemSource) {
self.cache = CachedSource()
self.source = source
}
public func getMirrorItemWithGUID(guid: GUID) -> Deferred<Maybe<BookmarkMirrorItem>> {
if let found = self.cache.lookup(guid) {
return found
}
return self.source.getMirrorItemWithGUID(guid) >>== effect {
self.cache.markSeen(guid)
self.cache[guid] = $0
}
}
public func getMirrorItemsWithGUIDs<T: CollectionType where T.Generator.Element == GUID>(guids: T) -> Deferred<Maybe<[GUID: BookmarkMirrorItem]>> {
return self.prefetchMirrorItemsWithGUIDs(guids) >>> { self.cache.takingGUIDs(guids) }
}
public func prefetchMirrorItemsWithGUIDs<T: CollectionType where T.Generator.Element == GUID>(guids: T) -> Success {
log.debug("Prefetching \(guids.count) mirror items: \(guids.prefix(10))….")
if guids.isEmpty {
return succeed()
}
return self.source.getMirrorItemsWithGUIDs(guids) >>== {
self.cache.markSeen(guids)
return self.cache.fill($0)
}
}
}
public class CachingBufferItemSource: BufferItemSource {
private let cache: CachedSource
private let source: BufferItemSource
public init(source: BufferItemSource) {
self.cache = CachedSource()
self.source = source
}
public func getBufferItemWithGUID(guid: GUID) -> Deferred<Maybe<BookmarkMirrorItem>> {
if let found = self.cache.lookup(guid) {
return found
}
return self.source.getBufferItemWithGUID(guid) >>== effect {
self.cache.markSeen(guid)
self.cache[guid] = $0
}
}
public func getBufferItemsWithGUIDs<T: CollectionType where T.Generator.Element == GUID>(guids: T) -> Deferred<Maybe<[GUID: BookmarkMirrorItem]>> {
return self.prefetchBufferItemsWithGUIDs(guids) >>> { self.cache.takingGUIDs(guids) }
}
public func prefetchBufferItemsWithGUIDs<T: CollectionType where T.Generator.Element == GUID>(guids: T) -> Success {
log.debug("Prefetching \(guids.count) buffer items: \(guids.prefix(10))….")
if guids.isEmpty {
return succeed()
}
return self.source.getBufferItemsWithGUIDs(guids) >>== {
self.cache.markSeen(guids)
return self.cache.fill($0)
}
}
}