/
PipelineProcessing.swift
182 lines (150 loc) · 5.33 KB
/
PipelineProcessing.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
//
// PipelineProcessing.swift
// Siesta
//
// Created by Paul on 2016/8/7.
// Copyright © 2016 Bust Out Solutions. All rights reserved.
//
import Foundation
internal extension Pipeline
{
private var stagesInOrder: [PipelineStage]
{ return order.flatMap { self[$0] } }
private typealias StageAndEntry = (PipelineStage, CacheEntryProtocol?)
private func stagesAndEntries(for resource: Resource) -> [StageAndEntry]
{
return stagesInOrder.map
{ stage in (stage, stage.cacheBox?.buildEntry(resource)) }
}
internal func makeProcessor(_ rawResponse: Response, resource: Resource) -> () -> Response
{
// Generate cache keys on main thread (because this touches Resource)
let stagesAndEntries = self.stagesAndEntries(for: resource)
// Return deferred processor to run on background queue
return
{
let result = Pipeline.processAndCache(rawResponse, using: stagesAndEntries)
debugLog(.pipeline, [" └╴Response after pipeline:", result.summary()])
debugLog(.networkDetails, [" Details:", result.dump(" ")])
return result
}
}
// Runs on a background queue
private static func processAndCache<StagesAndEntries: Collection>(
_ rawResponse: Response,
using stagesAndEntries: StagesAndEntries)
-> Response
where StagesAndEntries.Iterator.Element == StageAndEntry
{
return stagesAndEntries.reduce(rawResponse)
{
let input = $0,
(stage, cacheEntry) = $1
let output = stage.process(input)
if case .success(let entity) = output,
let cacheEntry = cacheEntry
{
debugLog(.cache, [" ├╴Caching entity with", type(of: entity.content), "content in", cacheEntry])
cacheEntry.write(entity)
}
return output
}
}
internal func cachedEntity(for resource: Resource, onHit: @escaping (Entity<Any>) -> ())
{
// Extract cache keys on main thread
let stagesAndEntries = self.stagesAndEntries(for: resource)
defaultEntityCacheWorkQueue.async
{
if let entity = Pipeline.cacheLookup(using: stagesAndEntries)
{
DispatchQueue.main.async
{ onHit(entity) }
}
}
}
// Runs on a background queue
private static func cacheLookup(using stagesAndEntries: [StageAndEntry]) -> Entity<Any>?
{
for (index, (_, cacheEntry)) in stagesAndEntries.enumerated().reversed()
{
if let result = cacheEntry?.read()
{
debugLog(.cache, ["Cache hit for", cacheEntry])
let processed = Pipeline.processAndCache(
.success(result),
using: stagesAndEntries.suffix(from: index + 1))
switch processed
{
case .failure:
debugLog(.cache, ["Error processing cached entity; will ignore cached value. Error:", processed])
case .success(let entity):
return entity
}
}
}
return nil
}
internal func updateCacheEntryTimestamps(_ timestamp: TimeInterval, for resource: Resource)
{
for (_, cacheEntry) in stagesAndEntries(for: resource)
{ cacheEntry?.updateTimestamp(timestamp) }
}
internal func removeCacheEntries(for resource: Resource)
{
for (_, cacheEntry) in stagesAndEntries(for: resource)
{ cacheEntry?.remove() }
}
}
// MARK: Type erasure dance
internal struct CacheBox
{
fileprivate let buildEntry: (Resource) -> (CacheEntryProtocol?)
internal let description: String
init?<T: EntityCache>(cache: T?)
{
guard let cache = cache else { return nil }
buildEntry = { CacheEntry(cache: cache, resource: $0) }
description = String(describing: type(of: cache))
}
}
private protocol CacheEntryProtocol
{
func read() -> Entity<Any>?
func write(_ entity: Entity<Any>)
func updateTimestamp(_ timestamp: TimeInterval)
func remove()
}
private struct CacheEntry<Cache, Key>: CacheEntryProtocol
where Cache: EntityCache, Cache.Key == Key
{
let cache: Cache
let key: Key
init?(cache: Cache, resource: Resource)
{
DispatchQueue.mainThreadPrecondition()
guard let key = cache.key(for: resource) else { return nil }
self.cache = cache
self.key = key
}
func read() -> Entity<Any>?
{
return cache.workQueue.sync
{ self.cache.readEntity(forKey: self.key) }
}
func write(_ entity: Entity<Any>)
{
cache.workQueue.async
{ self.cache.writeEntity(entity, forKey: self.key) }
}
func updateTimestamp(_ timestamp: TimeInterval)
{
cache.workQueue.async
{ self.cache.updateEntityTimestamp(timestamp, forKey: self.key) }
}
func remove()
{
cache.workQueue.async
{ self.cache.removeEntity(forKey: self.key) }
}
}