-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathPersistable_ValueWithNoMetadata.swift
More file actions
231 lines (187 loc) · 7.24 KB
/
Persistable_ValueWithNoMetadata.swift
File metadata and controls
231 lines (187 loc) · 7.24 KB
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
//
// Persistable_ObjectWithNoMetadata.swift
// YapDatabaseExtensions
//
// Created by Daniel Thorpe on 11/10/2015.
//
//
import Foundation
import ValueCoding
// MARK: - Persistable
extension Persistable where
Self: ValueCoding,
Self.MetadataType == Void,
Self.Coder: NSCoding,
Self.Coder.ValueType == Self {
// Writing
/**
Write the item using an existing transaction.
- parameter transaction: a YapDatabaseReadWriteTransaction
- returns: the receiver.
*/
public func write<WriteTransaction: WriteTransactionType>(transaction: WriteTransaction) -> Self {
return transaction.write(self)
}
/**
Write the item synchronously using a connection.
- parameter connection: a YapDatabaseConnection
- returns: the receiver.
*/
public func write<Connection: ConnectionType>(connection: Connection) -> Self {
return connection.write(self)
}
/**
Write the item asynchronously using a connection.
- parameter connection: a YapDatabaseConnection
- returns: a closure which receives as an argument the receiver of this function.
*/
public func asyncWrite<Connection: ConnectionType>(connection: Connection, queue: dispatch_queue_t = dispatch_get_main_queue(), completion: (Self -> Void)? = .None) {
return connection.asyncWrite(self, queue: queue, completion: completion)
}
/**
Write the item synchronously using a connection as an NSOperation
- parameter connection: a YapDatabaseConnection
- returns: an `NSOperation`
*/
public func writeOperation<Connection: ConnectionType>(connection: Connection) -> NSOperation {
return NSBlockOperation { connection.write(self) }
}
}
extension SequenceType where
Generator.Element: Persistable,
Generator.Element: ValueCoding,
Generator.Element.MetadataType == Void,
Generator.Element.Coder: NSCoding,
Generator.Element.Coder.ValueType == Generator.Element {
/**
Write the items using an existing transaction.
- parameter transaction: a WriteTransactionType e.g. YapDatabaseReadWriteTransaction
- returns: the receiver.
*/
public func write<WriteTransaction: WriteTransactionType>(transaction: WriteTransaction) -> [Generator.Element] {
return transaction.write(self)
}
/**
Write the items synchronously using a connection.
- parameter connection: a ConnectionType e.g. YapDatabaseConnection
- returns: the receiver.
*/
public func write<Connection: ConnectionType>(connection: Connection) -> [Generator.Element] {
return connection.write(self)
}
/**
Write the items asynchronously using a connection.
- parameter connection: a ConnectionType e.g. YapDatabaseConnection
- returns: a closure which receives as an argument the receiver of this function.
*/
public func asyncWrite<Connection: ConnectionType>(connection: Connection, queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ([Generator.Element] -> Void)? = .None) {
return connection.asyncWrite(self, queue: queue, completion: completion)
}
/**
Write the item synchronously using a connection as an NSOperation
- parameter connection: a YapDatabaseConnection
- returns: an `NSOperation`
*/
public func writeOperation<Connection: ConnectionType>(connection: Connection) -> NSOperation {
return NSBlockOperation { connection.write(self) }
}
}
// MARK: - Readable
extension Readable where
ItemType: ValueCoding,
ItemType: Persistable,
ItemType.MetadataType == Void,
ItemType.Coder: NSCoding,
ItemType.Coder.ValueType == ItemType {
func inTransaction(transaction: Database.Connection.ReadTransaction, atIndex index: YapDB.Index) -> ItemType? {
return transaction.readAtIndex(index)
}
func inTransactionAtIndex(transaction: Database.Connection.ReadTransaction) -> YapDB.Index -> ItemType? {
return { self.inTransaction(transaction, atIndex: $0) }
}
func atIndexInTransaction(index: YapDB.Index) -> Database.Connection.ReadTransaction -> ItemType? {
return { self.inTransaction($0, atIndex: index) }
}
func atIndexesInTransaction<
Indexes where
Indexes: SequenceType,
Indexes.Generator.Element == YapDB.Index>(indexes: Indexes) -> Database.Connection.ReadTransaction -> [ItemType] {
let atIndex = inTransactionAtIndex
return { indexes.flatMap(atIndex($0)) }
}
func inTransaction(transaction: Database.Connection.ReadTransaction, byKey key: String) -> ItemType? {
return inTransaction(transaction, atIndex: ItemType.indexWithKey(key))
}
func inTransactionByKey(transaction: Database.Connection.ReadTransaction) -> String -> ItemType? {
return { self.inTransaction(transaction, byKey: $0) }
}
func byKeyInTransaction(key: String) -> Database.Connection.ReadTransaction -> ItemType? {
return { self.inTransaction($0, byKey: key) }
}
func byKeysInTransaction(keys: [String]? = .None) -> Database.Connection.ReadTransaction -> [ItemType] {
let byKey = inTransactionByKey
return { transaction in
let keys = keys ?? transaction.keysInCollection(ItemType.collection)
return keys.flatMap(byKey(transaction))
}
}
/**
Reads the item at a given index.
- parameter index: a YapDB.Index
- returns: an optional `ItemType`
*/
public func atIndex(index: YapDB.Index) -> ItemType? {
return sync(atIndexInTransaction(index))
}
/**
Reads the items at the indexes.
- parameter indexes: a SequenceType of YapDB.Index values
- returns: an array of `ItemType`
*/
public func atIndexes<
Indexes where
Indexes: SequenceType,
Indexes.Generator.Element == YapDB.Index>(indexes: Indexes) -> [ItemType] {
return sync(atIndexesInTransaction(indexes))
}
/**
Reads the item at the key.
- parameter key: a String
- returns: an optional `ItemType`
*/
public func byKey(key: String) -> ItemType? {
return sync(byKeyInTransaction(key))
}
/**
Reads the items by the keys.
- parameter keys: a SequenceType of String values
- returns: an array of `ItemType`
*/
public func byKeys<
Keys where
Keys: SequenceType,
Keys.Generator.Element == String>(keys: Keys) -> [ItemType] {
return sync(byKeysInTransaction(Array(keys)))
}
/**
Reads all the `ItemType` in the database.
- returns: an array of `ItemType`
*/
public func all() -> [ItemType] {
return sync(byKeysInTransaction())
}
/**
Returns th existing items and missing keys..
- parameter keys: a SequenceType of String values
- returns: a tuple of type `([ItemType], [String])`
*/
public func filterExisting(keys: [String]) -> (existing: [ItemType], missing: [String]) {
let existingInTransaction = byKeysInTransaction(keys)
return sync { transaction -> ([ItemType], [String]) in
let existing = existingInTransaction(transaction)
let existingKeys = existing.map(keyForPersistable)
let missingKeys = keys.filter { !existingKeys.contains($0) }
return (existing, missingKeys)
}
}
}