From b7dee8b469391b9d257579fa0d2095438ceb12f3 Mon Sep 17 00:00:00 2001 From: Jim Roepcke Date: Fri, 9 Sep 2016 10:48:11 -0700 Subject: [PATCH 01/14] comment out the metadata portions from the Persistable protocol --- .../Shared/YapDatabaseExtensions.swift | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/YapDatabaseExtensions/Shared/YapDatabaseExtensions.swift b/YapDatabaseExtensions/Shared/YapDatabaseExtensions.swift index d1cf280..20b84cf 100644 --- a/YapDatabaseExtensions/Shared/YapDatabaseExtensions.swift +++ b/YapDatabaseExtensions/Shared/YapDatabaseExtensions.swift @@ -181,15 +181,15 @@ defined in this framework. It assumes that all instances of a type are stored in the same YapDatabase collection. */ public protocol Persistable: Identifiable { - - /// The nested type of the metadata. Defaults to Void. - associatedtype MetadataType +// +// /// The nested type of the metadata. Defaults to Void. +// associatedtype MetadataType /// The YapDatabase collection name the type is stored in. static var collection: String { get } - - /// A metadata which is set when reading, and get when writing. - var metadata: MetadataType? { get set } +// +// /// A metadata which is set when reading, and get when writing. +// var metadata: MetadataType? { get set } } extension Persistable { From 11f7543c866702e95bdbdaa1dc66e80951ca594b Mon Sep 17 00:00:00 2001 From: Jim Roepcke Date: Fri, 9 Sep 2016 17:03:27 -0700 Subject: [PATCH 02/14] fix nometadata versions --- .../Curried_ObjectWithNoMetadata.swift | 3 +- .../Curried/Curried_ValueWithNoMetadata.swift | 3 +- .../Functional_ObjectWithNoMetadata.swift | 130 +++++------- .../Functional_ValueWithNoMetadata.swift | 194 ++++++++---------- .../Persistable_ObjectWithNoMetadata.swift | 9 +- .../Persistable_ValueWithNoMetadata.swift | 3 - 6 files changed, 151 insertions(+), 191 deletions(-) diff --git a/YapDatabaseExtensions/Shared/Curried/Curried_ObjectWithNoMetadata.swift b/YapDatabaseExtensions/Shared/Curried/Curried_ObjectWithNoMetadata.swift index 55d7062..4ec7b60 100644 --- a/YapDatabaseExtensions/Shared/Curried/Curried_ObjectWithNoMetadata.swift +++ b/YapDatabaseExtensions/Shared/Curried/Curried_ObjectWithNoMetadata.swift @@ -12,8 +12,7 @@ import ValueCoding // MARK: - Persistable extension Persistable where - Self: NSCoding, - Self.MetadataType == Void { + Self: NSCoding { /** Returns a closure which, given a read transaction will return diff --git a/YapDatabaseExtensions/Shared/Curried/Curried_ValueWithNoMetadata.swift b/YapDatabaseExtensions/Shared/Curried/Curried_ValueWithNoMetadata.swift index 3fc1df7..60cd00a 100644 --- a/YapDatabaseExtensions/Shared/Curried/Curried_ValueWithNoMetadata.swift +++ b/YapDatabaseExtensions/Shared/Curried/Curried_ValueWithNoMetadata.swift @@ -14,8 +14,7 @@ import ValueCoding extension Persistable where Self: ValueCoding, Self.Coder: NSCoding, - Self.Coder.ValueType == Self, - Self.MetadataType == Void { + Self.Coder.ValueType == Self { /** Returns a closure which, given a read transaction will return diff --git a/YapDatabaseExtensions/Shared/Functional/Functional_ObjectWithNoMetadata.swift b/YapDatabaseExtensions/Shared/Functional/Functional_ObjectWithNoMetadata.swift index 5100354..6cebd10 100644 --- a/YapDatabaseExtensions/Shared/Functional/Functional_ObjectWithNoMetadata.swift +++ b/YapDatabaseExtensions/Shared/Functional/Functional_ObjectWithNoMetadata.swift @@ -21,11 +21,10 @@ extension ReadTransactionType { - returns: an optional `ItemType` */ public func readAtIndex< - ObjectWithNoMetadata where - ObjectWithNoMetadata: Persistable, - ObjectWithNoMetadata: NSCoding, - ObjectWithNoMetadata.MetadataType == Void>(index: YapDB.Index) -> ObjectWithNoMetadata? { - return readAtIndex(index) as? ObjectWithNoMetadata + Object where + Object: Persistable, + Object: NSCoding>(index: YapDB.Index) -> Object? { + return readAtIndex(index) as? Object } /** @@ -35,12 +34,11 @@ extension ReadTransactionType { - returns: an array of `ItemType` */ public func readAtIndexes< - Indexes, ObjectWithNoMetadata where + Indexes, Object where Indexes: SequenceType, Indexes.Generator.Element == YapDB.Index, - ObjectWithNoMetadata: Persistable, - ObjectWithNoMetadata: NSCoding, - ObjectWithNoMetadata.MetadataType == Void>(indexes: Indexes) -> [ObjectWithNoMetadata] { + Object: Persistable, + Object: NSCoding>(indexes: Indexes) -> [Object] { return indexes.flatMap(readAtIndex) } @@ -51,11 +49,10 @@ extension ReadTransactionType { - returns: an optional `ItemType` */ public func readByKey< - ObjectWithNoMetadata where - ObjectWithNoMetadata: Persistable, - ObjectWithNoMetadata: NSCoding, - ObjectWithNoMetadata.MetadataType == Void>(key: String) -> ObjectWithNoMetadata? { - return readAtIndex(ObjectWithNoMetadata.indexWithKey(key)) + Object where + Object: Persistable, + Object: NSCoding>(key: String) -> Object? { + return readAtIndex(Object.indexWithKey(key)) } /** @@ -65,13 +62,12 @@ extension ReadTransactionType { - returns: an array of `ItemType` */ public func readByKeys< - Keys, ObjectWithNoMetadata where + Keys, Object where Keys: SequenceType, Keys.Generator.Element == String, - ObjectWithNoMetadata: Persistable, - ObjectWithNoMetadata: NSCoding, - ObjectWithNoMetadata.MetadataType == Void>(keys: Keys) -> [ObjectWithNoMetadata] { - return readAtIndexes(ObjectWithNoMetadata.indexesWithKeys(keys)) + Object: Persistable, + Object: NSCoding>(keys: Keys) -> [Object] { + return readAtIndexes(Object.indexesWithKeys(keys)) } /** @@ -80,11 +76,10 @@ extension ReadTransactionType { - returns: an array of `ItemType` */ public func readAll< - ObjectWithNoMetadata where - ObjectWithNoMetadata: Persistable, - ObjectWithNoMetadata: NSCoding, - ObjectWithNoMetadata.MetadataType == Void>() -> [ObjectWithNoMetadata] { - return readByKeys(keysInCollection(ObjectWithNoMetadata.collection)) + Object where + Object: Persistable, + Object: NSCoding>() -> [Object] { + return readByKeys(keysInCollection(Object.collection)) } } @@ -97,10 +92,9 @@ extension ConnectionType { - returns: an optional `ItemType` */ public func readAtIndex< - ObjectWithNoMetadata where - ObjectWithNoMetadata: Persistable, - ObjectWithNoMetadata: NSCoding, - ObjectWithNoMetadata.MetadataType == Void>(index: YapDB.Index) -> ObjectWithNoMetadata? { + Object where + Object: Persistable, + Object: NSCoding>(index: YapDB.Index) -> Object? { return read { $0.readAtIndex(index) } } @@ -111,12 +105,11 @@ extension ConnectionType { - returns: an array of `ItemType` */ public func readAtIndexes< - Indexes, ObjectWithNoMetadata where + Indexes, Object where Indexes: SequenceType, Indexes.Generator.Element == YapDB.Index, - ObjectWithNoMetadata: Persistable, - ObjectWithNoMetadata: NSCoding, - ObjectWithNoMetadata.MetadataType == Void>(indexes: Indexes) -> [ObjectWithNoMetadata] { + Object: Persistable, + Object: NSCoding>(indexes: Indexes) -> [Object] { return read { $0.readAtIndexes(indexes) } } @@ -127,11 +120,10 @@ extension ConnectionType { - returns: an optional `ItemType` */ public func readByKey< - ObjectWithNoMetadata where - ObjectWithNoMetadata: Persistable, - ObjectWithNoMetadata: NSCoding, - ObjectWithNoMetadata.MetadataType == Void>(key: String) -> ObjectWithNoMetadata? { - return readAtIndex(ObjectWithNoMetadata.indexWithKey(key)) + Object where + Object: Persistable, + Object: NSCoding>(key: String) -> Object? { + return readAtIndex(Object.indexWithKey(key)) } /** @@ -141,13 +133,12 @@ extension ConnectionType { - returns: an array of `ItemType` */ public func readByKeys< - Keys, ObjectWithNoMetadata where + Keys, Object where Keys: SequenceType, Keys.Generator.Element == String, - ObjectWithNoMetadata: Persistable, - ObjectWithNoMetadata: NSCoding, - ObjectWithNoMetadata.MetadataType == Void>(keys: Keys) -> [ObjectWithNoMetadata] { - return readAtIndexes(ObjectWithNoMetadata.indexesWithKeys(keys)) + Object: Persistable, + Object: NSCoding>(keys: Keys) -> [Object] { + return readAtIndexes(Object.indexesWithKeys(keys)) } /** @@ -156,10 +147,9 @@ extension ConnectionType { - returns: an array of `ItemType` */ public func readAll< - ObjectWithNoMetadata where - ObjectWithNoMetadata: Persistable, - ObjectWithNoMetadata: NSCoding, - ObjectWithNoMetadata.MetadataType == Void>() -> [ObjectWithNoMetadata] { + Object where + Object: Persistable, + Object: NSCoding>() -> [Object] { return read { $0.readAll() } } } @@ -175,10 +165,9 @@ extension WriteTransactionType { - returns: the same item */ public func write< - ObjectWithNoMetadata where - ObjectWithNoMetadata: Persistable, - ObjectWithNoMetadata: NSCoding, - ObjectWithNoMetadata.MetadataType == Void>(item: ObjectWithNoMetadata) -> ObjectWithNoMetadata { + Object where + Object: Persistable, + Object: NSCoding>(item: Object) -> Object { writeAtIndex(item.index, object: item, metadata: .None) return item } @@ -190,12 +179,11 @@ extension WriteTransactionType { - returns: the same items, in an array. */ public func write< - Items, ObjectWithNoMetadata where + Items, Object where Items: SequenceType, - Items.Generator.Element == ObjectWithNoMetadata, - ObjectWithNoMetadata: Persistable, - ObjectWithNoMetadata: NSCoding, - ObjectWithNoMetadata.MetadataType == Void>(items: Items) -> [ObjectWithNoMetadata] { + Items.Generator.Element == Object, + Object: Persistable, + Object: NSCoding>(items: Items) -> [Object] { return items.map(write) } } @@ -209,10 +197,9 @@ extension ConnectionType { - returns: the same item */ public func write< - ObjectWithNoMetadata where - ObjectWithNoMetadata: Persistable, - ObjectWithNoMetadata: NSCoding, - ObjectWithNoMetadata.MetadataType == Void>(item: ObjectWithNoMetadata) -> ObjectWithNoMetadata { + Object where + Object: Persistable, + Object: NSCoding>(item: Object) -> Object { return write { $0.write(item) } } @@ -223,12 +210,11 @@ extension ConnectionType { - returns: the same items, in an array. */ public func write< - Items, ObjectWithNoMetadata where + Items, Object where Items: SequenceType, - Items.Generator.Element == ObjectWithNoMetadata, - ObjectWithNoMetadata: Persistable, - ObjectWithNoMetadata: NSCoding, - ObjectWithNoMetadata.MetadataType == Void>(items: Items) -> [ObjectWithNoMetadata] { + Items.Generator.Element == Object, + Object: Persistable, + Object: NSCoding>(items: Items) -> [Object] { return write { $0.write(items) } } @@ -240,10 +226,9 @@ extension ConnectionType { - parameter completion: a dispatch_block_t for completion. */ public func asyncWrite< - ObjectWithNoMetadata where - ObjectWithNoMetadata: Persistable, - ObjectWithNoMetadata: NSCoding, - ObjectWithNoMetadata.MetadataType == Void>(item: ObjectWithNoMetadata, queue: dispatch_queue_t = dispatch_get_main_queue(), completion: (ObjectWithNoMetadata -> Void)? = .None) { + Object where + Object: Persistable, + Object: NSCoding>(item: Object, queue: dispatch_queue_t = dispatch_get_main_queue(), completion: (Object -> Void)? = .None) { asyncWrite({ $0.write(item) }, queue: queue, completion: completion) } @@ -255,12 +240,11 @@ extension ConnectionType { - parameter completion: a dispatch_block_t for completion. */ public func asyncWrite< - Items, ObjectWithNoMetadata where + Items, Object where Items: SequenceType, - Items.Generator.Element == ObjectWithNoMetadata, - ObjectWithNoMetadata: Persistable, - ObjectWithNoMetadata: NSCoding, - ObjectWithNoMetadata.MetadataType == Void>(items: Items, queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ([ObjectWithNoMetadata] -> Void)? = .None) { + Items.Generator.Element == Object, + Object: Persistable, + Object: NSCoding>(items: Items, queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ([Object] -> Void)? = .None) { asyncWrite({ $0.write(items) }, queue: queue, completion: completion) } } diff --git a/YapDatabaseExtensions/Shared/Functional/Functional_ValueWithNoMetadata.swift b/YapDatabaseExtensions/Shared/Functional/Functional_ValueWithNoMetadata.swift index f1ebbe5..a29a136 100644 --- a/YapDatabaseExtensions/Shared/Functional/Functional_ValueWithNoMetadata.swift +++ b/YapDatabaseExtensions/Shared/Functional/Functional_ValueWithNoMetadata.swift @@ -21,13 +21,12 @@ extension ReadTransactionType { - returns: an optional `ItemType` */ public func readAtIndex< - ValueWithNoMetadata where - ValueWithNoMetadata: Persistable, - ValueWithNoMetadata: ValueCoding, - ValueWithNoMetadata.Coder: NSCoding, - ValueWithNoMetadata.Coder.ValueType == ValueWithNoMetadata, - ValueWithNoMetadata.MetadataType == Void>(index: YapDB.Index) -> ValueWithNoMetadata? { - return ValueWithNoMetadata.decode(readAtIndex(index)) + Value where + Value: Persistable, + Value: ValueCoding, + Value.Coder: NSCoding, + Value.Coder.ValueType == Value>(index: YapDB.Index) -> Value? { + return Value.decode(readAtIndex(index)) } /** @@ -37,14 +36,13 @@ extension ReadTransactionType { - returns: an array of `ItemType` */ public func readAtIndexes< - Indexes, ValueWithNoMetadata where + Indexes, Value where Indexes: SequenceType, Indexes.Generator.Element == YapDB.Index, - ValueWithNoMetadata: Persistable, - ValueWithNoMetadata: ValueCoding, - ValueWithNoMetadata.Coder: NSCoding, - ValueWithNoMetadata.Coder.ValueType == ValueWithNoMetadata, - ValueWithNoMetadata.MetadataType == Void>(indexes: Indexes) -> [ValueWithNoMetadata] { + Value: Persistable, + Value: ValueCoding, + Value.Coder: NSCoding, + Value.Coder.ValueType == Value>(indexes: Indexes) -> [Value] { return indexes.flatMap(readAtIndex) } @@ -55,13 +53,12 @@ extension ReadTransactionType { - returns: an optional `ItemType` */ public func readByKey< - ValueWithNoMetadata where - ValueWithNoMetadata: Persistable, - ValueWithNoMetadata: ValueCoding, - ValueWithNoMetadata.Coder: NSCoding, - ValueWithNoMetadata.Coder.ValueType == ValueWithNoMetadata, - ValueWithNoMetadata.MetadataType == Void>(key: String) -> ValueWithNoMetadata? { - return readAtIndex(ValueWithNoMetadata.indexWithKey(key)) + Value where + Value: Persistable, + Value: ValueCoding, + Value.Coder: NSCoding, + Value.Coder.ValueType == Value>(key: String) -> Value? { + return readAtIndex(Value.indexWithKey(key)) } /** @@ -71,15 +68,14 @@ extension ReadTransactionType { - returns: an array of `ItemType` */ public func readByKeys< - Keys, ValueWithNoMetadata where + Keys, Value where Keys: SequenceType, Keys.Generator.Element == String, - ValueWithNoMetadata: Persistable, - ValueWithNoMetadata: ValueCoding, - ValueWithNoMetadata.Coder: NSCoding, - ValueWithNoMetadata.Coder.ValueType == ValueWithNoMetadata, - ValueWithNoMetadata.MetadataType == Void>(keys: Keys) -> [ValueWithNoMetadata] { - return readAtIndexes(ValueWithNoMetadata.indexesWithKeys(keys)) + Value: Persistable, + Value: ValueCoding, + Value.Coder: NSCoding, + Value.Coder.ValueType == Value>(keys: Keys) -> [Value] { + return readAtIndexes(Value.indexesWithKeys(keys)) } /** @@ -88,13 +84,12 @@ extension ReadTransactionType { - returns: an array of `ItemType` */ public func readAll< - ValueWithNoMetadata where - ValueWithNoMetadata: Persistable, - ValueWithNoMetadata: ValueCoding, - ValueWithNoMetadata.Coder: NSCoding, - ValueWithNoMetadata.Coder.ValueType == ValueWithNoMetadata, - ValueWithNoMetadata.MetadataType == Void>() -> [ValueWithNoMetadata] { - return readByKeys(keysInCollection(ValueWithNoMetadata.collection)) + Value where + Value: Persistable, + Value: ValueCoding, + Value.Coder: NSCoding, + Value.Coder.ValueType == Value>() -> [Value] { + return readByKeys(keysInCollection(Value.collection)) } } @@ -107,12 +102,11 @@ extension ConnectionType { - returns: an optional `ItemType` */ public func readAtIndex< - ValueWithNoMetadata where - ValueWithNoMetadata: Persistable, - ValueWithNoMetadata: ValueCoding, - ValueWithNoMetadata.Coder: NSCoding, - ValueWithNoMetadata.Coder.ValueType == ValueWithNoMetadata, - ValueWithNoMetadata.MetadataType == Void>(index: YapDB.Index) -> ValueWithNoMetadata? { + Value where + Value: Persistable, + Value: ValueCoding, + Value.Coder: NSCoding, + Value.Coder.ValueType == Value>(index: YapDB.Index) -> Value? { return read { $0.readAtIndex(index) } } @@ -123,14 +117,13 @@ extension ConnectionType { - returns: an array of `ItemType` */ public func readAtIndexes< - Indexes, ValueWithNoMetadata where + Indexes, Value where Indexes: SequenceType, Indexes.Generator.Element == YapDB.Index, - ValueWithNoMetadata: Persistable, - ValueWithNoMetadata: ValueCoding, - ValueWithNoMetadata.Coder: NSCoding, - ValueWithNoMetadata.Coder.ValueType == ValueWithNoMetadata, - ValueWithNoMetadata.MetadataType == Void>(indexes: Indexes) -> [ValueWithNoMetadata] { + Value: Persistable, + Value: ValueCoding, + Value.Coder: NSCoding, + Value.Coder.ValueType == Value>(indexes: Indexes) -> [Value] { return read { $0.readAtIndexes(indexes) } } @@ -141,13 +134,12 @@ extension ConnectionType { - returns: an optional `ItemType` */ public func readByKey< - ValueWithNoMetadata where - ValueWithNoMetadata: Persistable, - ValueWithNoMetadata: ValueCoding, - ValueWithNoMetadata.Coder: NSCoding, - ValueWithNoMetadata.Coder.ValueType == ValueWithNoMetadata, - ValueWithNoMetadata.MetadataType == Void>(key: String) -> ValueWithNoMetadata? { - return readAtIndex(ValueWithNoMetadata.indexWithKey(key)) + Value where + Value: Persistable, + Value: ValueCoding, + Value.Coder: NSCoding, + Value.Coder.ValueType == Value>(key: String) -> Value? { + return readAtIndex(Value.indexWithKey(key)) } /** @@ -157,15 +149,14 @@ extension ConnectionType { - returns: an array of `ItemType` */ public func readByKeys< - Keys, ValueWithNoMetadata where + Keys, Value where Keys: SequenceType, Keys.Generator.Element == String, - ValueWithNoMetadata: Persistable, - ValueWithNoMetadata: ValueCoding, - ValueWithNoMetadata.Coder: NSCoding, - ValueWithNoMetadata.Coder.ValueType == ValueWithNoMetadata, - ValueWithNoMetadata.MetadataType == Void>(keys: Keys) -> [ValueWithNoMetadata] { - return readAtIndexes(ValueWithNoMetadata.indexesWithKeys(keys)) + Value: Persistable, + Value: ValueCoding, + Value.Coder: NSCoding, + Value.Coder.ValueType == Value>(keys: Keys) -> [Value] { + return readAtIndexes(Value.indexesWithKeys(keys)) } /** @@ -174,12 +165,11 @@ extension ConnectionType { - returns: an array of `ItemType` */ public func readAll< - ValueWithNoMetadata where - ValueWithNoMetadata: Persistable, - ValueWithNoMetadata: ValueCoding, - ValueWithNoMetadata.Coder: NSCoding, - ValueWithNoMetadata.Coder.ValueType == ValueWithNoMetadata, - ValueWithNoMetadata.MetadataType == Void>() -> [ValueWithNoMetadata] { + Value where + Value: Persistable, + Value: ValueCoding, + Value.Coder: NSCoding, + Value.Coder.ValueType == Value>() -> [Value] { return read { $0.readAll() } } } @@ -194,12 +184,11 @@ extension WriteTransactionType { - parameter item: the item to store. */ public func write< - ValueWithNoMetadata where - ValueWithNoMetadata: Persistable, - ValueWithNoMetadata: ValueCoding, - ValueWithNoMetadata.Coder: NSCoding, - ValueWithNoMetadata.Coder.ValueType == ValueWithNoMetadata, - ValueWithNoMetadata.MetadataType == Void>(item: ValueWithNoMetadata) -> ValueWithNoMetadata { + Value where + Value: Persistable, + Value: ValueCoding, + Value.Coder: NSCoding, + Value.Coder.ValueType == Value>(item: Value) -> Value { writeAtIndex(item.index, object: item.encoded, metadata: .None) return item } @@ -210,14 +199,13 @@ extension WriteTransactionType { - parameter items: a SequenceType of items to store. */ public func write< - Items, ValueWithNoMetadata where + Items, Value where Items: SequenceType, - Items.Generator.Element == ValueWithNoMetadata, - ValueWithNoMetadata: Persistable, - ValueWithNoMetadata: ValueCoding, - ValueWithNoMetadata.Coder: NSCoding, - ValueWithNoMetadata.Coder.ValueType == ValueWithNoMetadata, - ValueWithNoMetadata.MetadataType == Void>(items: Items) -> [ValueWithNoMetadata] { + Items.Generator.Element == Value, + Value: Persistable, + Value: ValueCoding, + Value.Coder: NSCoding, + Value.Coder.ValueType == Value>(items: Items) -> [Value] { return items.map(write) } } @@ -230,12 +218,11 @@ extension ConnectionType { - parameter item: the item to store. */ public func write< - ValueWithNoMetadata where - ValueWithNoMetadata: Persistable, - ValueWithNoMetadata: ValueCoding, - ValueWithNoMetadata.Coder: NSCoding, - ValueWithNoMetadata.Coder.ValueType == ValueWithNoMetadata, - ValueWithNoMetadata.MetadataType == Void>(item: ValueWithNoMetadata) -> ValueWithNoMetadata { + Value where + Value: Persistable, + Value: ValueCoding, + Value.Coder: NSCoding, + Value.Coder.ValueType == Value>(item: Value) -> Value { return write { $0.write(item) } } @@ -245,14 +232,13 @@ extension ConnectionType { - parameter items: a SequenceType of items to store. */ public func write< - Items, ValueWithNoMetadata where + Items, Value where Items: SequenceType, - Items.Generator.Element == ValueWithNoMetadata, - ValueWithNoMetadata: Persistable, - ValueWithNoMetadata: ValueCoding, - ValueWithNoMetadata.Coder: NSCoding, - ValueWithNoMetadata.Coder.ValueType == ValueWithNoMetadata, - ValueWithNoMetadata.MetadataType == Void>(items: Items) -> [ValueWithNoMetadata] { + Items.Generator.Element == Value, + Value: Persistable, + Value: ValueCoding, + Value.Coder: NSCoding, + Value.Coder.ValueType == Value>(items: Items) -> [Value] { return write { $0.write(items) } } @@ -264,12 +250,11 @@ extension ConnectionType { - parameter completion: a dispatch_block_t for completion. */ public func asyncWrite< - ValueWithNoMetadata where - ValueWithNoMetadata: Persistable, - ValueWithNoMetadata: ValueCoding, - ValueWithNoMetadata.Coder: NSCoding, - ValueWithNoMetadata.Coder.ValueType == ValueWithNoMetadata, - ValueWithNoMetadata.MetadataType == Void>(item: ValueWithNoMetadata, queue: dispatch_queue_t = dispatch_get_main_queue(), completion: (ValueWithNoMetadata -> Void)? = .None) { + Value where + Value: Persistable, + Value: ValueCoding, + Value.Coder: NSCoding, + Value.Coder.ValueType == Value>(item: Value, queue: dispatch_queue_t = dispatch_get_main_queue(), completion: (Value -> Void)? = .None) { asyncWrite({ $0.write(item) }, queue: queue, completion: completion) } @@ -281,14 +266,13 @@ extension ConnectionType { - parameter completion: a dispatch_block_t for completion. */ public func asyncWrite< - Items, ValueWithNoMetadata where + Items, Value where Items: SequenceType, - Items.Generator.Element == ValueWithNoMetadata, - ValueWithNoMetadata: Persistable, - ValueWithNoMetadata: ValueCoding, - ValueWithNoMetadata.Coder: NSCoding, - ValueWithNoMetadata.Coder.ValueType == ValueWithNoMetadata, - ValueWithNoMetadata.MetadataType == Void>(items: Items, queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ([ValueWithNoMetadata] -> Void)? = .None) { + Items.Generator.Element == Value, + Value: Persistable, + Value: ValueCoding, + Value.Coder: NSCoding, + Value.Coder.ValueType == Value>(items: Items, queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ([Value] -> Void)? = .None) { asyncWrite({ $0.write(items) }, queue: queue, completion: completion) } } diff --git a/YapDatabaseExtensions/Shared/Persistable/Persistable_ObjectWithNoMetadata.swift b/YapDatabaseExtensions/Shared/Persistable/Persistable_ObjectWithNoMetadata.swift index 17c3b13..e390fd0 100644 --- a/YapDatabaseExtensions/Shared/Persistable/Persistable_ObjectWithNoMetadata.swift +++ b/YapDatabaseExtensions/Shared/Persistable/Persistable_ObjectWithNoMetadata.swift @@ -12,8 +12,7 @@ import ValueCoding // MARK: - Persistable extension Persistable where - Self: NSCoding, - Self.MetadataType == Void { + Self: NSCoding { // Writing @@ -60,8 +59,7 @@ extension Persistable where extension SequenceType where Generator.Element: Persistable, - Generator.Element: NSCoding, - Generator.Element.MetadataType == Void { + Generator.Element: NSCoding { /** Write the items using an existing transaction. @@ -108,8 +106,7 @@ extension SequenceType where extension Readable where ItemType: NSCoding, - ItemType: Persistable, - ItemType.MetadataType == Void { + ItemType: Persistable { func inTransaction(transaction: Database.Connection.ReadTransaction, atIndex index: YapDB.Index) -> ItemType? { return transaction.readAtIndex(index) diff --git a/YapDatabaseExtensions/Shared/Persistable/Persistable_ValueWithNoMetadata.swift b/YapDatabaseExtensions/Shared/Persistable/Persistable_ValueWithNoMetadata.swift index e52e301..3e19487 100644 --- a/YapDatabaseExtensions/Shared/Persistable/Persistable_ValueWithNoMetadata.swift +++ b/YapDatabaseExtensions/Shared/Persistable/Persistable_ValueWithNoMetadata.swift @@ -13,7 +13,6 @@ import ValueCoding extension Persistable where Self: ValueCoding, - Self.MetadataType == Void, Self.Coder: NSCoding, Self.Coder.ValueType == Self { @@ -63,7 +62,6 @@ extension Persistable where extension SequenceType where Generator.Element: Persistable, Generator.Element: ValueCoding, - Generator.Element.MetadataType == Void, Generator.Element.Coder: NSCoding, Generator.Element.Coder.ValueType == Generator.Element { @@ -113,7 +111,6 @@ extension SequenceType where extension Readable where ItemType: ValueCoding, ItemType: Persistable, - ItemType.MetadataType == Void, ItemType.Coder: NSCoding, ItemType.Coder.ValueType == ItemType { From e69cc43e0a8cd6a11dc645ffad7201cdb4889acd Mon Sep 17 00:00:00 2001 From: Jim Roepcke Date: Fri, 9 Sep 2016 17:04:31 -0700 Subject: [PATCH 03/14] fix functional with metadata versions --- .../Functional_ObjectWithObjectMetadata.swift | 155 +++++----- .../Functional_ObjectWithValueMetadata.swift | 219 +++++++------- .../Functional_ValueWIthObjectMetadata.swift | 219 +++++++------- .../Functional_ValueWIthValueMetadata.swift | 283 +++++++++--------- 4 files changed, 436 insertions(+), 440 deletions(-) diff --git a/YapDatabaseExtensions/Shared/Functional/Functional_ObjectWithObjectMetadata.swift b/YapDatabaseExtensions/Shared/Functional/Functional_ObjectWithObjectMetadata.swift index baeb3c9..7eff7f9 100644 --- a/YapDatabaseExtensions/Shared/Functional/Functional_ObjectWithObjectMetadata.swift +++ b/YapDatabaseExtensions/Shared/Functional/Functional_ObjectWithObjectMetadata.swift @@ -21,15 +21,13 @@ extension ReadTransactionType { - returns: an optional `ItemType` */ public func readAtIndex< - ObjectWithObjectMetadata where - ObjectWithObjectMetadata: Persistable, - ObjectWithObjectMetadata: NSCoding, - ObjectWithObjectMetadata.MetadataType: NSCoding>(index: YapDB.Index) -> ObjectWithObjectMetadata? { - if var item = readAtIndex(index) as? ObjectWithObjectMetadata { - item.metadata = readMetadataAtIndex(index) - return item - } - return .None + Object, Metadata where + Object: Persistable, + Object: NSCoding, + Metadata: NSCoding>(index: YapDB.Index) -> (Object, Metadata?)? { + guard let item: Object = readAtIndex(index) else { return nil } + let metadata: Metadata? = readMetadataAtIndex(index) + return (item, metadata) } /** @@ -39,12 +37,13 @@ extension ReadTransactionType { - returns: an array of `ItemType` */ public func readAtIndexes< - Indexes, ObjectWithObjectMetadata where + Indexes, Object, Metadata where Indexes: SequenceType, Indexes.Generator.Element == YapDB.Index, - ObjectWithObjectMetadata: Persistable, - ObjectWithObjectMetadata: NSCoding, - ObjectWithObjectMetadata.MetadataType: NSCoding>(indexes: Indexes) -> [ObjectWithObjectMetadata] { + Object: Persistable, + Object: NSCoding, + Metadata: NSCoding>(indexes: Indexes) -> [(Object, Metadata?)] { + // FIXME: using flatMap means the output length need not match the input length return indexes.flatMap(readAtIndex) } @@ -55,11 +54,11 @@ extension ReadTransactionType { - returns: an optional `ItemType` */ public func readByKey< - ObjectWithObjectMetadata where - ObjectWithObjectMetadata: Persistable, - ObjectWithObjectMetadata: NSCoding, - ObjectWithObjectMetadata.MetadataType: NSCoding>(key: String) -> ObjectWithObjectMetadata? { - return readAtIndex(ObjectWithObjectMetadata.indexWithKey(key)) + Object, Metadata where + Object: Persistable, + Object: NSCoding, + Metadata: NSCoding>(key: String) -> (Object, Metadata?)? { + return readAtIndex(Object.indexWithKey(key)) } /** @@ -69,13 +68,13 @@ extension ReadTransactionType { - returns: an array of `ItemType` */ public func readByKeys< - Keys, ObjectWithObjectMetadata where + Keys, Object, Metadata where Keys: SequenceType, Keys.Generator.Element == String, - ObjectWithObjectMetadata: Persistable, - ObjectWithObjectMetadata: NSCoding, - ObjectWithObjectMetadata.MetadataType: NSCoding>(keys: Keys) -> [ObjectWithObjectMetadata] { - return readAtIndexes(ObjectWithObjectMetadata.indexesWithKeys(keys)) + Object: Persistable, + Object: NSCoding, + Metadata: NSCoding>(keys: Keys) -> [(Object, Metadata?)] { + return readAtIndexes(Object.indexesWithKeys(keys)) } /** @@ -84,11 +83,11 @@ extension ReadTransactionType { - returns: an array of `ItemType` */ public func readAll< - ObjectWithObjectMetadata where - ObjectWithObjectMetadata: Persistable, - ObjectWithObjectMetadata: NSCoding, - ObjectWithObjectMetadata.MetadataType: NSCoding>() -> [ObjectWithObjectMetadata] { - return readByKeys(keysInCollection(ObjectWithObjectMetadata.collection)) + Object, Metadata where + Object: Persistable, + Object: NSCoding, + Metadata: NSCoding>() -> [(Object, Metadata?)] { + return readByKeys(keysInCollection(Object.collection)) } } @@ -101,10 +100,10 @@ extension ConnectionType { - returns: an optional `ItemType` */ public func readAtIndex< - ObjectWithObjectMetadata where - ObjectWithObjectMetadata: Persistable, - ObjectWithObjectMetadata: NSCoding, - ObjectWithObjectMetadata.MetadataType: NSCoding>(index: YapDB.Index) -> ObjectWithObjectMetadata? { + Object, Metadata where + Object: Persistable, + Object: NSCoding, + Metadata: NSCoding>(index: YapDB.Index) -> (Object, Metadata?)? { return read { $0.readAtIndex(index) } } @@ -115,12 +114,12 @@ extension ConnectionType { - returns: an array of `ItemType` */ public func readAtIndexes< - Indexes, ObjectWithObjectMetadata where + Indexes, Object, Metadata where Indexes: SequenceType, Indexes.Generator.Element == YapDB.Index, - ObjectWithObjectMetadata: Persistable, - ObjectWithObjectMetadata: NSCoding, - ObjectWithObjectMetadata.MetadataType: NSCoding>(indexes: Indexes) -> [ObjectWithObjectMetadata] { + Object: Persistable, + Object: NSCoding, + Metadata: NSCoding>(indexes: Indexes) -> [(Object, Metadata?)] { return read { $0.readAtIndexes(indexes) } } @@ -131,11 +130,11 @@ extension ConnectionType { - returns: an optional `ItemType` */ public func readByKey< - ObjectWithObjectMetadata where - ObjectWithObjectMetadata: Persistable, - ObjectWithObjectMetadata: NSCoding, - ObjectWithObjectMetadata.MetadataType: NSCoding>(key: String) -> ObjectWithObjectMetadata? { - return readAtIndex(ObjectWithObjectMetadata.indexWithKey(key)) + Object, Metadata where + Object: Persistable, + Object: NSCoding, + Metadata: NSCoding>(key: String) -> (Object, Metadata?)? { + return readAtIndex(Object.indexWithKey(key)) } /** @@ -145,13 +144,13 @@ extension ConnectionType { - returns: an array of `ItemType` */ public func readByKeys< - Keys, ObjectWithObjectMetadata where + Keys, Object, Metadata where Keys: SequenceType, Keys.Generator.Element == String, - ObjectWithObjectMetadata: Persistable, - ObjectWithObjectMetadata: NSCoding, - ObjectWithObjectMetadata.MetadataType: NSCoding>(keys: Keys) -> [ObjectWithObjectMetadata] { - return readAtIndexes(ObjectWithObjectMetadata.indexesWithKeys(keys)) + Object: Persistable, + Object: NSCoding, + Metadata: NSCoding>(keys: Keys) -> [(Object, Metadata?)] { + return readAtIndexes(Object.indexesWithKeys(keys)) } /** @@ -160,10 +159,10 @@ extension ConnectionType { - returns: an array of `ItemType` */ public func readAll< - ObjectWithObjectMetadata where - ObjectWithObjectMetadata: Persistable, - ObjectWithObjectMetadata: NSCoding, - ObjectWithObjectMetadata.MetadataType: NSCoding>() -> [ObjectWithObjectMetadata] { + Object, Metadata where + Object: Persistable, + Object: NSCoding, + Metadata: NSCoding>() -> [(Object, Metadata?)] { return read { $0.readAll() } } } @@ -178,11 +177,11 @@ extension WriteTransactionType { - parameter item: the item to store. */ public func write< - ObjectWithObjectMetadata where - ObjectWithObjectMetadata: Persistable, - ObjectWithObjectMetadata: NSCoding, - ObjectWithObjectMetadata.MetadataType: NSCoding>(item: ObjectWithObjectMetadata) -> ObjectWithObjectMetadata { - writeAtIndex(item.index, object: item, metadata: item.metadata) + Object, Metadata where + Object: Persistable, + Object: NSCoding, + Metadata: NSCoding>(item: (Object, Metadata?)) -> (Object, Metadata?) { + writeAtIndex(item.0.index, object: item.0, metadata: item.1) return item } @@ -192,12 +191,12 @@ extension WriteTransactionType { - parameter items: a SequenceType of items to store. */ public func write< - Items, ObjectWithObjectMetadata where + Items, Object, Metadata where Items: SequenceType, - Items.Generator.Element == ObjectWithObjectMetadata, - ObjectWithObjectMetadata: Persistable, - ObjectWithObjectMetadata: NSCoding, - ObjectWithObjectMetadata.MetadataType: NSCoding>(items: Items) -> [ObjectWithObjectMetadata] { + Items.Generator.Element == (Object, Metadata?), + Object: Persistable, + Object: NSCoding, + Metadata: NSCoding>(items: Items) -> [(Object, Metadata?)] { return items.map(write) } } @@ -210,10 +209,10 @@ extension ConnectionType { - parameter item: the item to store. */ public func write< - ObjectWithObjectMetadata where - ObjectWithObjectMetadata: Persistable, - ObjectWithObjectMetadata: NSCoding, - ObjectWithObjectMetadata.MetadataType: NSCoding>(item: ObjectWithObjectMetadata) -> ObjectWithObjectMetadata { + Object, Metadata where + Object: Persistable, + Object: NSCoding, + Metadata: NSCoding>(item: (Object, Metadata?)) -> (Object, Metadata?) { return write { $0.write(item) } } @@ -223,12 +222,12 @@ extension ConnectionType { - parameter items: a SequenceType of items to store. */ public func write< - Items, ObjectWithObjectMetadata where + Items, Object, Metadata where Items: SequenceType, - Items.Generator.Element == ObjectWithObjectMetadata, - ObjectWithObjectMetadata: Persistable, - ObjectWithObjectMetadata: NSCoding, - ObjectWithObjectMetadata.MetadataType: NSCoding>(items: Items) -> [ObjectWithObjectMetadata] { + Items.Generator.Element == (Object, Metadata?), + Object: Persistable, + Object: NSCoding, + Metadata: NSCoding>(items: Items) -> [(Object, Metadata?)] { return write { $0.write(items) } } @@ -240,10 +239,10 @@ extension ConnectionType { - parameter completion: a dispatch_block_t for completion. */ public func asyncWrite< - ObjectWithObjectMetadata where - ObjectWithObjectMetadata: Persistable, - ObjectWithObjectMetadata: NSCoding, - ObjectWithObjectMetadata.MetadataType: NSCoding>(item: ObjectWithObjectMetadata, queue: dispatch_queue_t = dispatch_get_main_queue(), completion: (ObjectWithObjectMetadata -> Void)? = .None) { + Object, Metadata where + Object: Persistable, + Object: NSCoding, + Metadata: NSCoding>(item: (Object, Metadata?), queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ((Object, Metadata?) -> Void)? = .None) { asyncWrite({ $0.write(item) }, queue: queue, completion: completion) } @@ -255,12 +254,12 @@ extension ConnectionType { - parameter completion: a dispatch_block_t for completion. */ public func asyncWrite< - Items, ObjectWithObjectMetadata where + Items, Object, Metadata where Items: SequenceType, - Items.Generator.Element == ObjectWithObjectMetadata, - ObjectWithObjectMetadata: Persistable, - ObjectWithObjectMetadata: NSCoding, - ObjectWithObjectMetadata.MetadataType: NSCoding>(items: Items, queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ([ObjectWithObjectMetadata] -> Void)? = .None) { + Items.Generator.Element == (Object, Metadata?), + Object: Persistable, + Object: NSCoding, + Metadata: NSCoding>(items: Items, queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ([(Object, Metadata?)] -> Void)? = .None) { asyncWrite({ $0.write(items) }, queue: queue, completion: completion) } } diff --git a/YapDatabaseExtensions/Shared/Functional/Functional_ObjectWithValueMetadata.swift b/YapDatabaseExtensions/Shared/Functional/Functional_ObjectWithValueMetadata.swift index 30ee717..cc5f0a8 100644 --- a/YapDatabaseExtensions/Shared/Functional/Functional_ObjectWithValueMetadata.swift +++ b/YapDatabaseExtensions/Shared/Functional/Functional_ObjectWithValueMetadata.swift @@ -21,17 +21,15 @@ extension ReadTransactionType { - returns: an optional `ItemType` */ public func readAtIndex< - ObjectWithValueMetadata where - ObjectWithValueMetadata: Persistable, - ObjectWithValueMetadata: NSCoding, - ObjectWithValueMetadata.MetadataType: ValueCoding, - ObjectWithValueMetadata.MetadataType.Coder: NSCoding, - ObjectWithValueMetadata.MetadataType.Coder.ValueType == ObjectWithValueMetadata.MetadataType>(index: YapDB.Index) -> ObjectWithValueMetadata? { - if var item = readAtIndex(index) as? ObjectWithValueMetadata { - item.metadata = readMetadataAtIndex(index) - return item - } - return .None + Object, Metadata where + Object: Persistable, + Object: NSCoding, + Metadata: ValueCoding, + Metadata.Coder: NSCoding, + Metadata.Coder.ValueType == Metadata>(index: YapDB.Index) -> (Object, Metadata?)? { + guard let item: Object = readAtIndex(index) else { return nil } + let metadata: Metadata? = readMetadataAtIndex(index) + return (item, metadata) } /** @@ -41,14 +39,15 @@ extension ReadTransactionType { - returns: an array of `ItemType` */ public func readAtIndexes< - Indexes, ObjectWithValueMetadata where + Indexes, Object, Metadata where Indexes: SequenceType, Indexes.Generator.Element == YapDB.Index, - ObjectWithValueMetadata: Persistable, - ObjectWithValueMetadata: NSCoding, - ObjectWithValueMetadata.MetadataType: ValueCoding, - ObjectWithValueMetadata.MetadataType.Coder: NSCoding, - ObjectWithValueMetadata.MetadataType.Coder.ValueType == ObjectWithValueMetadata.MetadataType>(indexes: Indexes) -> [ObjectWithValueMetadata] { + Object: Persistable, + Object: NSCoding, + Metadata: ValueCoding, + Metadata.Coder: NSCoding, + Metadata.Coder.ValueType == Metadata>(indexes: Indexes) -> [(Object, Metadata?)] { + // FIXME: using flatMap means the output length need not match the input length return indexes.flatMap(readAtIndex) } @@ -59,13 +58,13 @@ extension ReadTransactionType { - returns: an optional `ItemType` */ public func readByKey< - ObjectWithValueMetadata where - ObjectWithValueMetadata: Persistable, - ObjectWithValueMetadata: NSCoding, - ObjectWithValueMetadata.MetadataType: ValueCoding, - ObjectWithValueMetadata.MetadataType.Coder: NSCoding, - ObjectWithValueMetadata.MetadataType.Coder.ValueType == ObjectWithValueMetadata.MetadataType>(key: String) -> ObjectWithValueMetadata? { - return readAtIndex(ObjectWithValueMetadata.indexWithKey(key)) + Object, Metadata where + Object: Persistable, + Object: NSCoding, + Metadata: ValueCoding, + Metadata.Coder: NSCoding, + Metadata.Coder.ValueType == Metadata>(key: String) -> (Object, Metadata?)? { + return readAtIndex(Object.indexWithKey(key)) } /** @@ -75,15 +74,15 @@ extension ReadTransactionType { - returns: an array of `ItemType` */ public func readByKeys< - Keys, ObjectWithValueMetadata where + Keys, Object, Metadata where Keys: SequenceType, Keys.Generator.Element == String, - ObjectWithValueMetadata: Persistable, - ObjectWithValueMetadata: NSCoding, - ObjectWithValueMetadata.MetadataType: ValueCoding, - ObjectWithValueMetadata.MetadataType.Coder: NSCoding, - ObjectWithValueMetadata.MetadataType.Coder.ValueType == ObjectWithValueMetadata.MetadataType>(keys: Keys) -> [ObjectWithValueMetadata] { - return readAtIndexes(ObjectWithValueMetadata.indexesWithKeys(keys)) + Object: Persistable, + Object: NSCoding, + Metadata: ValueCoding, + Metadata.Coder: NSCoding, + Metadata.Coder.ValueType == Metadata>(keys: Keys) -> [(Object, Metadata?)] { + return readAtIndexes(Object.indexesWithKeys(keys)) } /** @@ -92,13 +91,13 @@ extension ReadTransactionType { - returns: an array of `ItemType` */ public func readAll< - ObjectWithValueMetadata where - ObjectWithValueMetadata: Persistable, - ObjectWithValueMetadata: NSCoding, - ObjectWithValueMetadata.MetadataType: ValueCoding, - ObjectWithValueMetadata.MetadataType.Coder: NSCoding, - ObjectWithValueMetadata.MetadataType.Coder.ValueType == ObjectWithValueMetadata.MetadataType>() -> [ObjectWithValueMetadata] { - return readByKeys(keysInCollection(ObjectWithValueMetadata.collection)) + Object, Metadata where + Object: Persistable, + Object: NSCoding, + Metadata: ValueCoding, + Metadata.Coder: NSCoding, + Metadata.Coder.ValueType == Metadata>() -> [(Object, Metadata?)] { + return readByKeys(keysInCollection(Object.collection)) } } @@ -111,12 +110,12 @@ extension ConnectionType { - returns: an optional `ItemType` */ public func readAtIndex< - ObjectWithValueMetadata where - ObjectWithValueMetadata: Persistable, - ObjectWithValueMetadata: NSCoding, - ObjectWithValueMetadata.MetadataType: ValueCoding, - ObjectWithValueMetadata.MetadataType.Coder: NSCoding, - ObjectWithValueMetadata.MetadataType.Coder.ValueType == ObjectWithValueMetadata.MetadataType>(index: YapDB.Index) -> ObjectWithValueMetadata? { + Object, Metadata where + Object: Persistable, + Object: NSCoding, + Metadata: ValueCoding, + Metadata.Coder: NSCoding, + Metadata.Coder.ValueType == Metadata>(index: YapDB.Index) -> (Object, Metadata?)? { return read { $0.readAtIndex(index) } } @@ -127,14 +126,14 @@ extension ConnectionType { - returns: an array of `ItemType` */ public func readAtIndexes< - Indexes, ObjectWithValueMetadata where + Indexes, Object, Metadata where Indexes: SequenceType, Indexes.Generator.Element == YapDB.Index, - ObjectWithValueMetadata: Persistable, - ObjectWithValueMetadata: NSCoding, - ObjectWithValueMetadata.MetadataType: ValueCoding, - ObjectWithValueMetadata.MetadataType.Coder: NSCoding, - ObjectWithValueMetadata.MetadataType.Coder.ValueType == ObjectWithValueMetadata.MetadataType>(indexes: Indexes) -> [ObjectWithValueMetadata] { + Object: Persistable, + Object: NSCoding, + Metadata: ValueCoding, + Metadata.Coder: NSCoding, + Metadata.Coder.ValueType == Metadata>(indexes: Indexes) -> [(Object, Metadata?)] { return read { $0.readAtIndexes(indexes) } } @@ -145,13 +144,13 @@ extension ConnectionType { - returns: an optional `ItemType` */ public func readByKey< - ObjectWithValueMetadata where - ObjectWithValueMetadata: Persistable, - ObjectWithValueMetadata: NSCoding, - ObjectWithValueMetadata.MetadataType: ValueCoding, - ObjectWithValueMetadata.MetadataType.Coder: NSCoding, - ObjectWithValueMetadata.MetadataType.Coder.ValueType == ObjectWithValueMetadata.MetadataType>(key: String) -> ObjectWithValueMetadata? { - return readAtIndex(ObjectWithValueMetadata.indexWithKey(key)) + Object, Metadata where + Object: Persistable, + Object: NSCoding, + Metadata: ValueCoding, + Metadata.Coder: NSCoding, + Metadata.Coder.ValueType == Metadata>(key: String) -> (Object, Metadata?)? { + return readAtIndex(Object.indexWithKey(key)) } /** @@ -161,15 +160,15 @@ extension ConnectionType { - returns: an array of `ItemType` */ public func readByKeys< - Keys, ObjectWithValueMetadata where + Keys, Object, Metadata where Keys: SequenceType, Keys.Generator.Element == String, - ObjectWithValueMetadata: Persistable, - ObjectWithValueMetadata: NSCoding, - ObjectWithValueMetadata.MetadataType: ValueCoding, - ObjectWithValueMetadata.MetadataType.Coder: NSCoding, - ObjectWithValueMetadata.MetadataType.Coder.ValueType == ObjectWithValueMetadata.MetadataType>(keys: Keys) -> [ObjectWithValueMetadata] { - return readAtIndexes(ObjectWithValueMetadata.indexesWithKeys(keys)) + Object: Persistable, + Object: NSCoding, + Metadata: ValueCoding, + Metadata.Coder: NSCoding, + Metadata.Coder.ValueType == Metadata>(keys: Keys) -> [(Object, Metadata?)] { + return readAtIndexes(Object.indexesWithKeys(keys)) } /** @@ -178,12 +177,12 @@ extension ConnectionType { - returns: an array of `ItemType` */ public func readAll< - ObjectWithValueMetadata where - ObjectWithValueMetadata: Persistable, - ObjectWithValueMetadata: NSCoding, - ObjectWithValueMetadata.MetadataType: ValueCoding, - ObjectWithValueMetadata.MetadataType.Coder: NSCoding, - ObjectWithValueMetadata.MetadataType.Coder.ValueType == ObjectWithValueMetadata.MetadataType>() -> [ObjectWithValueMetadata] { + Object, Metadata where + Object: Persistable, + Object: NSCoding, + Metadata: ValueCoding, + Metadata.Coder: NSCoding, + Metadata.Coder.ValueType == Metadata>() -> [(Object, Metadata?)] { return read { $0.readAll() } } } @@ -198,13 +197,13 @@ extension WriteTransactionType { - parameter item: the item to store. */ public func write< - ObjectWithValueMetadata where - ObjectWithValueMetadata: Persistable, - ObjectWithValueMetadata: NSCoding, - ObjectWithValueMetadata.MetadataType: ValueCoding, - ObjectWithValueMetadata.MetadataType.Coder: NSCoding, - ObjectWithValueMetadata.MetadataType.Coder.ValueType == ObjectWithValueMetadata.MetadataType>(item: ObjectWithValueMetadata) -> ObjectWithValueMetadata { - writeAtIndex(item.index, object: item, metadata: item.metadata?.encoded) + Object, Metadata where + Object: Persistable, + Object: NSCoding, + Metadata: ValueCoding, + Metadata.Coder: NSCoding, + Metadata.Coder.ValueType == Metadata>(item: (Object, Metadata?)) -> (Object, Metadata?) { + writeAtIndex(item.0.index, object: item.0, metadata: item.1?.encoded) return item } @@ -214,14 +213,14 @@ extension WriteTransactionType { - parameter items: a SequenceType of items to store. */ public func write< - Items, ObjectWithValueMetadata where + Items, Object, Metadata where Items: SequenceType, - Items.Generator.Element == ObjectWithValueMetadata, - ObjectWithValueMetadata: Persistable, - ObjectWithValueMetadata: NSCoding, - ObjectWithValueMetadata.MetadataType: ValueCoding, - ObjectWithValueMetadata.MetadataType.Coder: NSCoding, - ObjectWithValueMetadata.MetadataType.Coder.ValueType == ObjectWithValueMetadata.MetadataType>(items: Items) -> [ObjectWithValueMetadata] { + Items.Generator.Element == (Object, Metadata?), + Object: Persistable, + Object: NSCoding, + Metadata: ValueCoding, + Metadata.Coder: NSCoding, + Metadata.Coder.ValueType == Metadata>(items: Items) -> [(Object, Metadata?)] { return items.map(write) } } @@ -234,12 +233,12 @@ extension ConnectionType { - parameter item: the item to store. */ public func write< - ObjectWithValueMetadata where - ObjectWithValueMetadata: Persistable, - ObjectWithValueMetadata: NSCoding, - ObjectWithValueMetadata.MetadataType: ValueCoding, - ObjectWithValueMetadata.MetadataType.Coder: NSCoding, - ObjectWithValueMetadata.MetadataType.Coder.ValueType == ObjectWithValueMetadata.MetadataType>(item: ObjectWithValueMetadata) -> ObjectWithValueMetadata { + Object, Metadata where + Object: Persistable, + Object: NSCoding, + Metadata: ValueCoding, + Metadata.Coder: NSCoding, + Metadata.Coder.ValueType == Metadata>(item: (Object, Metadata?)) -> (Object, Metadata?) { return write { $0.write(item) } } @@ -249,14 +248,14 @@ extension ConnectionType { - parameter items: a SequenceType of items to store. */ public func write< - Items, ObjectWithValueMetadata where + Items, Object, Metadata where Items: SequenceType, - Items.Generator.Element == ObjectWithValueMetadata, - ObjectWithValueMetadata: Persistable, - ObjectWithValueMetadata: NSCoding, - ObjectWithValueMetadata.MetadataType: ValueCoding, - ObjectWithValueMetadata.MetadataType.Coder: NSCoding, - ObjectWithValueMetadata.MetadataType.Coder.ValueType == ObjectWithValueMetadata.MetadataType>(items: Items) -> [ObjectWithValueMetadata] { + Items.Generator.Element == (Object, Metadata?), + Object: Persistable, + Object: NSCoding, + Metadata: ValueCoding, + Metadata.Coder: NSCoding, + Metadata.Coder.ValueType == Metadata>(items: Items) -> [(Object, Metadata?)] { return write { $0.write(items) } } @@ -268,12 +267,12 @@ extension ConnectionType { - parameter completion: a dispatch_block_t for completion. */ public func asyncWrite< - ObjectWithValueMetadata where - ObjectWithValueMetadata: Persistable, - ObjectWithValueMetadata: NSCoding, - ObjectWithValueMetadata.MetadataType: ValueCoding, - ObjectWithValueMetadata.MetadataType.Coder: NSCoding, - ObjectWithValueMetadata.MetadataType.Coder.ValueType == ObjectWithValueMetadata.MetadataType>(item: ObjectWithValueMetadata, queue: dispatch_queue_t = dispatch_get_main_queue(), completion: (ObjectWithValueMetadata -> Void)? = .None) { + Object, Metadata where + Object: Persistable, + Object: NSCoding, + Metadata: ValueCoding, + Metadata.Coder: NSCoding, + Metadata.Coder.ValueType == Metadata>(item: (Object, Metadata?), queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ((Object, Metadata?) -> Void)? = .None) { asyncWrite({ $0.write(item) }, queue: queue, completion: completion) } @@ -285,14 +284,14 @@ extension ConnectionType { - parameter completion: a dispatch_block_t for completion. */ public func asyncWrite< - Items, ObjectWithValueMetadata where + Items, Object, Metadata where Items: SequenceType, - Items.Generator.Element == ObjectWithValueMetadata, - ObjectWithValueMetadata: Persistable, - ObjectWithValueMetadata: NSCoding, - ObjectWithValueMetadata.MetadataType: ValueCoding, - ObjectWithValueMetadata.MetadataType.Coder: NSCoding, - ObjectWithValueMetadata.MetadataType.Coder.ValueType == ObjectWithValueMetadata.MetadataType>(items: Items, queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ([ObjectWithValueMetadata] -> Void)? = .None) { + Items.Generator.Element == (Object, Metadata?), + Object: Persistable, + Object: NSCoding, + Metadata: ValueCoding, + Metadata.Coder: NSCoding, + Metadata.Coder.ValueType == Metadata>(items: Items, queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ([(Object, Metadata?)] -> Void)? = .None) { asyncWrite({ $0.write(items) }, queue: queue, completion: completion) } } diff --git a/YapDatabaseExtensions/Shared/Functional/Functional_ValueWIthObjectMetadata.swift b/YapDatabaseExtensions/Shared/Functional/Functional_ValueWIthObjectMetadata.swift index 4a09f50..1344b75 100644 --- a/YapDatabaseExtensions/Shared/Functional/Functional_ValueWIthObjectMetadata.swift +++ b/YapDatabaseExtensions/Shared/Functional/Functional_ValueWIthObjectMetadata.swift @@ -21,17 +21,15 @@ extension ReadTransactionType { - returns: an optional `ItemType` */ public func readAtIndex< - ValueWithObjectMetadata where - ValueWithObjectMetadata: Persistable, - ValueWithObjectMetadata: ValueCoding, - ValueWithObjectMetadata.Coder: NSCoding, - ValueWithObjectMetadata.Coder.ValueType == ValueWithObjectMetadata, - ValueWithObjectMetadata.MetadataType: NSCoding>(index: YapDB.Index) -> ValueWithObjectMetadata? { - if var item = ValueWithObjectMetadata.decode(readAtIndex(index)) { - item.metadata = readMetadataAtIndex(index) - return item - } - return .None + Value, Metadata where + Value: Persistable, + Value: ValueCoding, + Value.Coder: NSCoding, + Value.Coder.ValueType == Value, + Metadata: NSCoding>(index: YapDB.Index) -> (Value, Metadata?)? { + guard let item: Value = Value.decode(readAtIndex(index)) else { return nil } + let metadata: Metadata? = readMetadataAtIndex(index) + return (item, metadata) } /** @@ -41,14 +39,15 @@ extension ReadTransactionType { - returns: an array of `ItemType` */ public func readAtIndexes< - Indexes, ValueWithObjectMetadata where + Indexes, Value, Metadata where Indexes: SequenceType, Indexes.Generator.Element == YapDB.Index, - ValueWithObjectMetadata: Persistable, - ValueWithObjectMetadata: ValueCoding, - ValueWithObjectMetadata.Coder: NSCoding, - ValueWithObjectMetadata.Coder.ValueType == ValueWithObjectMetadata, - ValueWithObjectMetadata.MetadataType: NSCoding>(indexes: Indexes) -> [ValueWithObjectMetadata] { + Value: Persistable, + Value: ValueCoding, + Value.Coder: NSCoding, + Value.Coder.ValueType == Value, + Metadata: NSCoding>(indexes: Indexes) -> [(Value, Metadata?)] { + // FIXME: using flatMap means the output length need not match the input length return indexes.flatMap(readAtIndex) } @@ -59,13 +58,13 @@ extension ReadTransactionType { - returns: an optional `ItemType` */ public func readByKey< - ValueWithObjectMetadata where - ValueWithObjectMetadata: Persistable, - ValueWithObjectMetadata: ValueCoding, - ValueWithObjectMetadata.Coder: NSCoding, - ValueWithObjectMetadata.Coder.ValueType == ValueWithObjectMetadata, - ValueWithObjectMetadata.MetadataType: NSCoding>(key: String) -> ValueWithObjectMetadata? { - return readAtIndex(ValueWithObjectMetadata.indexWithKey(key)) + Value, Metadata where + Value: Persistable, + Value: ValueCoding, + Value.Coder: NSCoding, + Value.Coder.ValueType == Value, + Metadata: NSCoding>(key: String) -> (Value, Metadata?)? { + return readAtIndex(Value.indexWithKey(key)) } /** @@ -75,15 +74,15 @@ extension ReadTransactionType { - returns: an array of `ItemType` */ public func readByKeys< - Keys, ValueWithObjectMetadata where + Keys, Value, Metadata where Keys: SequenceType, Keys.Generator.Element == String, - ValueWithObjectMetadata: Persistable, - ValueWithObjectMetadata: ValueCoding, - ValueWithObjectMetadata.Coder: NSCoding, - ValueWithObjectMetadata.Coder.ValueType == ValueWithObjectMetadata, - ValueWithObjectMetadata.MetadataType: NSCoding>(keys: Keys) -> [ValueWithObjectMetadata] { - return readAtIndexes(ValueWithObjectMetadata.indexesWithKeys(keys)) + Value: Persistable, + Value: ValueCoding, + Value.Coder: NSCoding, + Value.Coder.ValueType == Value, + Metadata: NSCoding>(keys: Keys) -> [(Value, Metadata?)] { + return readAtIndexes(Value.indexesWithKeys(keys)) } /** @@ -92,13 +91,13 @@ extension ReadTransactionType { - returns: an array of `ItemType` */ public func readAll< - ValueWithObjectMetadata where - ValueWithObjectMetadata: Persistable, - ValueWithObjectMetadata: ValueCoding, - ValueWithObjectMetadata.Coder: NSCoding, - ValueWithObjectMetadata.Coder.ValueType == ValueWithObjectMetadata, - ValueWithObjectMetadata.MetadataType: NSCoding>() -> [ValueWithObjectMetadata] { - return readByKeys(keysInCollection(ValueWithObjectMetadata.collection)) + Value, Metadata where + Value: Persistable, + Value: ValueCoding, + Value.Coder: NSCoding, + Value.Coder.ValueType == Value, + Metadata: NSCoding>() -> [(Value, Metadata?)] { + return readByKeys(keysInCollection(Value.collection)) } } @@ -111,12 +110,12 @@ extension ConnectionType { - returns: an optional `ItemType` */ public func readAtIndex< - ValueWithObjectMetadata where - ValueWithObjectMetadata: Persistable, - ValueWithObjectMetadata: ValueCoding, - ValueWithObjectMetadata.Coder: NSCoding, - ValueWithObjectMetadata.Coder.ValueType == ValueWithObjectMetadata, - ValueWithObjectMetadata.MetadataType: NSCoding>(index: YapDB.Index) -> ValueWithObjectMetadata? { + Value, Metadata where + Value: Persistable, + Value: ValueCoding, + Value.Coder: NSCoding, + Value.Coder.ValueType == Value, + Metadata: NSCoding>(index: YapDB.Index) -> (Value, Metadata?)? { return read { $0.readAtIndex(index) } } @@ -127,14 +126,14 @@ extension ConnectionType { - returns: an array of `ItemType` */ public func readAtIndexes< - Indexes, ValueWithObjectMetadata where + Indexes, Value, Metadata where Indexes: SequenceType, Indexes.Generator.Element == YapDB.Index, - ValueWithObjectMetadata: Persistable, - ValueWithObjectMetadata: ValueCoding, - ValueWithObjectMetadata.Coder: NSCoding, - ValueWithObjectMetadata.Coder.ValueType == ValueWithObjectMetadata, - ValueWithObjectMetadata.MetadataType: NSCoding>(indexes: Indexes) -> [ValueWithObjectMetadata] { + Value: Persistable, + Value: ValueCoding, + Value.Coder: NSCoding, + Value.Coder.ValueType == Value, + Metadata: NSCoding>(indexes: Indexes) -> [(Value, Metadata?)] { return read { $0.readAtIndexes(indexes) } } @@ -145,13 +144,13 @@ extension ConnectionType { - returns: an optional `ItemType` */ public func readByKey< - ValueWithObjectMetadata where - ValueWithObjectMetadata: Persistable, - ValueWithObjectMetadata: ValueCoding, - ValueWithObjectMetadata.Coder: NSCoding, - ValueWithObjectMetadata.Coder.ValueType == ValueWithObjectMetadata, - ValueWithObjectMetadata.MetadataType: NSCoding>(key: String) -> ValueWithObjectMetadata? { - return readAtIndex(ValueWithObjectMetadata.indexWithKey(key)) + Value, Metadata where + Value: Persistable, + Value: ValueCoding, + Value.Coder: NSCoding, + Value.Coder.ValueType == Value, + Metadata: NSCoding>(key: String) -> (Value, Metadata?)? { + return readAtIndex(Value.indexWithKey(key)) } /** @@ -161,15 +160,15 @@ extension ConnectionType { - returns: an array of `ItemType` */ public func readByKeys< - Keys, ValueWithObjectMetadata where + Keys, Value, Metadata where Keys: SequenceType, Keys.Generator.Element == String, - ValueWithObjectMetadata: Persistable, - ValueWithObjectMetadata: ValueCoding, - ValueWithObjectMetadata.Coder: NSCoding, - ValueWithObjectMetadata.Coder.ValueType == ValueWithObjectMetadata, - ValueWithObjectMetadata.MetadataType: NSCoding>(keys: Keys) -> [ValueWithObjectMetadata] { - return readAtIndexes(ValueWithObjectMetadata.indexesWithKeys(keys)) + Value: Persistable, + Value: ValueCoding, + Value.Coder: NSCoding, + Value.Coder.ValueType == Value, + Metadata: NSCoding>(keys: Keys) -> [(Value, Metadata?)] { + return readAtIndexes(Value.indexesWithKeys(keys)) } /** @@ -178,12 +177,12 @@ extension ConnectionType { - returns: an array of `ItemType` */ public func readAll< - ValueWithObjectMetadata where - ValueWithObjectMetadata: Persistable, - ValueWithObjectMetadata: ValueCoding, - ValueWithObjectMetadata.Coder: NSCoding, - ValueWithObjectMetadata.Coder.ValueType == ValueWithObjectMetadata, - ValueWithObjectMetadata.MetadataType: NSCoding>() -> [ValueWithObjectMetadata] { + Value, Metadata where + Value: Persistable, + Value: ValueCoding, + Value.Coder: NSCoding, + Value.Coder.ValueType == Value, + Metadata: NSCoding>() -> [(Value, Metadata?)] { return read { $0.readAll() } } } @@ -198,13 +197,13 @@ extension WriteTransactionType { - parameter item: the item to store. */ public func write< - ValueWithObjectMetadata where - ValueWithObjectMetadata: Persistable, - ValueWithObjectMetadata: ValueCoding, - ValueWithObjectMetadata.Coder: NSCoding, - ValueWithObjectMetadata.Coder.ValueType == ValueWithObjectMetadata, - ValueWithObjectMetadata.MetadataType: NSCoding>(item: ValueWithObjectMetadata) -> ValueWithObjectMetadata { - writeAtIndex(item.index, object: item.encoded, metadata: item.metadata) + Value, Metadata where + Value: Persistable, + Value: ValueCoding, + Value.Coder: NSCoding, + Value.Coder.ValueType == Value, + Metadata: NSCoding>(item: (Value, Metadata?)) -> (Value, Metadata?) { + writeAtIndex(item.0.index, object: item.0.encoded, metadata: item.1) return item } @@ -214,14 +213,14 @@ extension WriteTransactionType { - parameter items: a SequenceType of items to store. */ public func write< - Items, ValueWithObjectMetadata where + Items, Value, Metadata where Items: SequenceType, - Items.Generator.Element == ValueWithObjectMetadata, - ValueWithObjectMetadata: Persistable, - ValueWithObjectMetadata: ValueCoding, - ValueWithObjectMetadata.Coder: NSCoding, - ValueWithObjectMetadata.Coder.ValueType == ValueWithObjectMetadata, - ValueWithObjectMetadata.MetadataType: NSCoding>(items: Items) -> [ValueWithObjectMetadata] { + Items.Generator.Element == (Value, Metadata?), + Value: Persistable, + Value: ValueCoding, + Value.Coder: NSCoding, + Value.Coder.ValueType == Value, + Metadata: NSCoding>(items: Items) -> [(Value, Metadata?)] { return items.map(write) } } @@ -234,12 +233,12 @@ extension ConnectionType { - parameter item: the item to store. */ public func write< - ValueWithObjectMetadata where - ValueWithObjectMetadata: Persistable, - ValueWithObjectMetadata: ValueCoding, - ValueWithObjectMetadata.Coder: NSCoding, - ValueWithObjectMetadata.Coder.ValueType == ValueWithObjectMetadata, - ValueWithObjectMetadata.MetadataType: NSCoding>(item: ValueWithObjectMetadata) -> ValueWithObjectMetadata { + Value, Metadata where + Value: Persistable, + Value: ValueCoding, + Value.Coder: NSCoding, + Value.Coder.ValueType == Value, + Metadata: NSCoding>(item: (Value, Metadata?)) -> (Value, Metadata?) { return write { $0.write(item) } } @@ -249,14 +248,14 @@ extension ConnectionType { - parameter items: a SequenceType of items to store. */ public func write< - Items, ValueWithObjectMetadata where + Items, Value, Metadata where Items: SequenceType, - Items.Generator.Element == ValueWithObjectMetadata, - ValueWithObjectMetadata: Persistable, - ValueWithObjectMetadata: ValueCoding, - ValueWithObjectMetadata.Coder: NSCoding, - ValueWithObjectMetadata.Coder.ValueType == ValueWithObjectMetadata, - ValueWithObjectMetadata.MetadataType: NSCoding>(items: Items) -> [ValueWithObjectMetadata] { + Items.Generator.Element == (Value, Metadata?), + Value: Persistable, + Value: ValueCoding, + Value.Coder: NSCoding, + Value.Coder.ValueType == Value, + Metadata: NSCoding>(items: Items) -> [(Value, Metadata?)] { return write { $0.write(items) } } @@ -268,12 +267,12 @@ extension ConnectionType { - parameter completion: a dispatch_block_t for completion. */ public func asyncWrite< - ValueWithObjectMetadata where - ValueWithObjectMetadata: Persistable, - ValueWithObjectMetadata: ValueCoding, - ValueWithObjectMetadata.Coder: NSCoding, - ValueWithObjectMetadata.Coder.ValueType == ValueWithObjectMetadata, - ValueWithObjectMetadata.MetadataType: NSCoding>(item: ValueWithObjectMetadata, queue: dispatch_queue_t = dispatch_get_main_queue(), completion: (ValueWithObjectMetadata -> Void)? = .None) { + Value, Metadata where + Value: Persistable, + Value: ValueCoding, + Value.Coder: NSCoding, + Value.Coder.ValueType == Value, + Metadata: NSCoding>(item: (Value, Metadata?), queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ((Value, Metadata?) -> Void)? = .None) { asyncWrite({ $0.write(item) }, queue: queue, completion: completion) } @@ -285,14 +284,14 @@ extension ConnectionType { - parameter completion: a dispatch_block_t for completion. */ public func asyncWrite< - Items, ValueWithObjectMetadata where + Items, Value, Metadata where Items: SequenceType, - Items.Generator.Element == ValueWithObjectMetadata, - ValueWithObjectMetadata: Persistable, - ValueWithObjectMetadata: ValueCoding, - ValueWithObjectMetadata.Coder: NSCoding, - ValueWithObjectMetadata.Coder.ValueType == ValueWithObjectMetadata, - ValueWithObjectMetadata.MetadataType: NSCoding>(items: Items, queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ([ValueWithObjectMetadata] -> Void)? = .None) { + Items.Generator.Element == (Value, Metadata?), + Value: Persistable, + Value: ValueCoding, + Value.Coder: NSCoding, + Value.Coder.ValueType == Value, + Metadata: NSCoding>(items: Items, queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ([(Value, Metadata?)] -> Void)? = .None) { asyncWrite({ $0.write(items) }, queue: queue, completion: completion) } } diff --git a/YapDatabaseExtensions/Shared/Functional/Functional_ValueWIthValueMetadata.swift b/YapDatabaseExtensions/Shared/Functional/Functional_ValueWIthValueMetadata.swift index 66bf836..217205d 100644 --- a/YapDatabaseExtensions/Shared/Functional/Functional_ValueWIthValueMetadata.swift +++ b/YapDatabaseExtensions/Shared/Functional/Functional_ValueWIthValueMetadata.swift @@ -21,19 +21,17 @@ extension ReadTransactionType { - returns: an optional `ItemType` */ public func readAtIndex< - ValueWithValueMetadata where - ValueWithValueMetadata: Persistable, - ValueWithValueMetadata: ValueCoding, - ValueWithValueMetadata.Coder: NSCoding, - ValueWithValueMetadata.Coder.ValueType == ValueWithValueMetadata, - ValueWithValueMetadata.MetadataType: ValueCoding, - ValueWithValueMetadata.MetadataType.Coder: NSCoding, - ValueWithValueMetadata.MetadataType.Coder.ValueType == ValueWithValueMetadata.MetadataType>(index: YapDB.Index) -> ValueWithValueMetadata? { - if var item = ValueWithValueMetadata.decode(readAtIndex(index)) { - item.metadata = readMetadataAtIndex(index) - return item - } - return .None + Value, Metadata where + Value: Persistable, + Value: ValueCoding, + Value.Coder: NSCoding, + Value.Coder.ValueType == Value, + Metadata: ValueCoding, + Metadata.Coder: NSCoding, + Metadata.Coder.ValueType == Metadata>(index: YapDB.Index) -> (Value, Metadata?)? { + guard let item: Value = Value.decode(readAtIndex(index)) else { return nil } + let metadata: Metadata? = readMetadataAtIndex(index) + return (item, metadata) } /** @@ -43,16 +41,17 @@ extension ReadTransactionType { - returns: an array of `ItemType` */ public func readAtIndexes< - Indexes, ValueWithValueMetadata where + Indexes, Value, Metadata where Indexes: SequenceType, Indexes.Generator.Element == YapDB.Index, - ValueWithValueMetadata: Persistable, - ValueWithValueMetadata: ValueCoding, - ValueWithValueMetadata.Coder: NSCoding, - ValueWithValueMetadata.Coder.ValueType == ValueWithValueMetadata, - ValueWithValueMetadata.MetadataType: ValueCoding, - ValueWithValueMetadata.MetadataType.Coder: NSCoding, - ValueWithValueMetadata.MetadataType.Coder.ValueType == ValueWithValueMetadata.MetadataType>(indexes: Indexes) -> [ValueWithValueMetadata] { + Value: Persistable, + Value: ValueCoding, + Value.Coder: NSCoding, + Value.Coder.ValueType == Value, + Metadata: ValueCoding, + Metadata.Coder: NSCoding, + Metadata.Coder.ValueType == Metadata>(indexes: Indexes) -> [(Value, Metadata?)] { + // FIXME: using flatMap means the output length need not match the input length return indexes.flatMap(readAtIndex) } @@ -63,15 +62,15 @@ extension ReadTransactionType { - returns: an optional `ItemType` */ public func readByKey< - ValueWithValueMetadata where - ValueWithValueMetadata: Persistable, - ValueWithValueMetadata: ValueCoding, - ValueWithValueMetadata.Coder: NSCoding, - ValueWithValueMetadata.Coder.ValueType == ValueWithValueMetadata, - ValueWithValueMetadata.MetadataType: ValueCoding, - ValueWithValueMetadata.MetadataType.Coder: NSCoding, - ValueWithValueMetadata.MetadataType.Coder.ValueType == ValueWithValueMetadata.MetadataType>(key: String) -> ValueWithValueMetadata? { - return readAtIndex(ValueWithValueMetadata.indexWithKey(key)) + Value, Metadata where + Value: Persistable, + Value: ValueCoding, + Value.Coder: NSCoding, + Value.Coder.ValueType == Value, + Metadata: ValueCoding, + Metadata.Coder: NSCoding, + Metadata.Coder.ValueType == Metadata>(key: String) -> (Value, Metadata?)? { + return readAtIndex(Value.indexWithKey(key)) } /** @@ -81,17 +80,17 @@ extension ReadTransactionType { - returns: an array of `ItemType` */ public func readByKeys< - Keys, ValueWithValueMetadata where + Keys, Value, Metadata where Keys: SequenceType, Keys.Generator.Element == String, - ValueWithValueMetadata: Persistable, - ValueWithValueMetadata: ValueCoding, - ValueWithValueMetadata.Coder: NSCoding, - ValueWithValueMetadata.Coder.ValueType == ValueWithValueMetadata, - ValueWithValueMetadata.MetadataType: ValueCoding, - ValueWithValueMetadata.MetadataType.Coder: NSCoding, - ValueWithValueMetadata.MetadataType.Coder.ValueType == ValueWithValueMetadata.MetadataType>(keys: Keys) -> [ValueWithValueMetadata] { - return readAtIndexes(ValueWithValueMetadata.indexesWithKeys(keys)) + Value: Persistable, + Value: ValueCoding, + Value.Coder: NSCoding, + Value.Coder.ValueType == Value, + Metadata: ValueCoding, + Metadata.Coder: NSCoding, + Metadata.Coder.ValueType == Metadata>(keys: Keys) -> [(Value, Metadata?)] { + return readAtIndexes(Value.indexesWithKeys(keys)) } /** @@ -100,15 +99,15 @@ extension ReadTransactionType { - returns: an array of `ItemType` */ public func readAll< - ValueWithValueMetadata where - ValueWithValueMetadata: Persistable, - ValueWithValueMetadata: ValueCoding, - ValueWithValueMetadata.Coder: NSCoding, - ValueWithValueMetadata.Coder.ValueType == ValueWithValueMetadata, - ValueWithValueMetadata.MetadataType: ValueCoding, - ValueWithValueMetadata.MetadataType.Coder: NSCoding, - ValueWithValueMetadata.MetadataType.Coder.ValueType == ValueWithValueMetadata.MetadataType>() -> [ValueWithValueMetadata] { - return readByKeys(keysInCollection(ValueWithValueMetadata.collection)) + Value, Metadata where + Value: Persistable, + Value: ValueCoding, + Value.Coder: NSCoding, + Value.Coder.ValueType == Value, + Metadata: ValueCoding, + Metadata.Coder: NSCoding, + Metadata.Coder.ValueType == Metadata>() -> [(Value, Metadata?)] { + return readByKeys(keysInCollection(Value.collection)) } } @@ -121,14 +120,14 @@ extension ConnectionType { - returns: an optional `ItemType` */ public func readAtIndex< - ValueWithValueMetadata where - ValueWithValueMetadata: Persistable, - ValueWithValueMetadata: ValueCoding, - ValueWithValueMetadata.Coder: NSCoding, - ValueWithValueMetadata.Coder.ValueType == ValueWithValueMetadata, - ValueWithValueMetadata.MetadataType: ValueCoding, - ValueWithValueMetadata.MetadataType.Coder: NSCoding, - ValueWithValueMetadata.MetadataType.Coder.ValueType == ValueWithValueMetadata.MetadataType>(index: YapDB.Index) -> ValueWithValueMetadata? { + Value, Metadata where + Value: Persistable, + Value: ValueCoding, + Value.Coder: NSCoding, + Value.Coder.ValueType == Value, + Metadata: ValueCoding, + Metadata.Coder: NSCoding, + Metadata.Coder.ValueType == Metadata>(index: YapDB.Index) -> (Value, Metadata?)? { return read { $0.readAtIndex(index) } } @@ -139,16 +138,16 @@ extension ConnectionType { - returns: an array of `ItemType` */ public func readAtIndexes< - Indexes, ValueWithValueMetadata where + Indexes, Value, Metadata where Indexes: SequenceType, Indexes.Generator.Element == YapDB.Index, - ValueWithValueMetadata: Persistable, - ValueWithValueMetadata: ValueCoding, - ValueWithValueMetadata.Coder: NSCoding, - ValueWithValueMetadata.Coder.ValueType == ValueWithValueMetadata, - ValueWithValueMetadata.MetadataType: ValueCoding, - ValueWithValueMetadata.MetadataType.Coder: NSCoding, - ValueWithValueMetadata.MetadataType.Coder.ValueType == ValueWithValueMetadata.MetadataType>(indexes: Indexes) -> [ValueWithValueMetadata] { + Value: Persistable, + Value: ValueCoding, + Value.Coder: NSCoding, + Value.Coder.ValueType == Value, + Metadata: ValueCoding, + Metadata.Coder: NSCoding, + Metadata.Coder.ValueType == Metadata>(indexes: Indexes) -> [(Value, Metadata?)] { return read { $0.readAtIndexes(indexes) } } @@ -159,15 +158,15 @@ extension ConnectionType { - returns: an optional `ItemType` */ public func readByKey< - ValueWithValueMetadata where - ValueWithValueMetadata: Persistable, - ValueWithValueMetadata: ValueCoding, - ValueWithValueMetadata.Coder: NSCoding, - ValueWithValueMetadata.Coder.ValueType == ValueWithValueMetadata, - ValueWithValueMetadata.MetadataType: ValueCoding, - ValueWithValueMetadata.MetadataType.Coder: NSCoding, - ValueWithValueMetadata.MetadataType.Coder.ValueType == ValueWithValueMetadata.MetadataType>(key: String) -> ValueWithValueMetadata? { - return readAtIndex(ValueWithValueMetadata.indexWithKey(key)) + Value, Metadata where + Value: Persistable, + Value: ValueCoding, + Value.Coder: NSCoding, + Value.Coder.ValueType == Value, + Metadata: ValueCoding, + Metadata.Coder: NSCoding, + Metadata.Coder.ValueType == Metadata>(key: String) -> (Value, Metadata?)? { + return readAtIndex(Value.indexWithKey(key)) } /** @@ -177,17 +176,17 @@ extension ConnectionType { - returns: an array of `ItemType` */ public func readByKeys< - Keys, ValueWithValueMetadata where + Keys, Value, Metadata where Keys: SequenceType, Keys.Generator.Element == String, - ValueWithValueMetadata: Persistable, - ValueWithValueMetadata: ValueCoding, - ValueWithValueMetadata.Coder: NSCoding, - ValueWithValueMetadata.Coder.ValueType == ValueWithValueMetadata, - ValueWithValueMetadata.MetadataType: ValueCoding, - ValueWithValueMetadata.MetadataType.Coder: NSCoding, - ValueWithValueMetadata.MetadataType.Coder.ValueType == ValueWithValueMetadata.MetadataType>(keys: Keys) -> [ValueWithValueMetadata] { - return readAtIndexes(ValueWithValueMetadata.indexesWithKeys(keys)) + Value: Persistable, + Value: ValueCoding, + Value.Coder: NSCoding, + Value.Coder.ValueType == Value, + Metadata: ValueCoding, + Metadata.Coder: NSCoding, + Metadata.Coder.ValueType == Metadata>(keys: Keys) -> [(Value, Metadata?)] { + return readAtIndexes(Value.indexesWithKeys(keys)) } /** @@ -196,14 +195,14 @@ extension ConnectionType { - returns: an array of `ItemType` */ public func readAll< - ValueWithValueMetadata where - ValueWithValueMetadata: Persistable, - ValueWithValueMetadata: ValueCoding, - ValueWithValueMetadata.Coder: NSCoding, - ValueWithValueMetadata.Coder.ValueType == ValueWithValueMetadata, - ValueWithValueMetadata.MetadataType: ValueCoding, - ValueWithValueMetadata.MetadataType.Coder: NSCoding, - ValueWithValueMetadata.MetadataType.Coder.ValueType == ValueWithValueMetadata.MetadataType>() -> [ValueWithValueMetadata] { + Value, Metadata where + Value: Persistable, + Value: ValueCoding, + Value.Coder: NSCoding, + Value.Coder.ValueType == Value, + Metadata: ValueCoding, + Metadata.Coder: NSCoding, + Metadata.Coder.ValueType == Metadata>() -> [(Value, Metadata?)] { return read { $0.readAll() } } } @@ -218,15 +217,15 @@ extension WriteTransactionType { - parameter item: the item to store. */ public func write< - ValueWithValueMetadata where - ValueWithValueMetadata: Persistable, - ValueWithValueMetadata: ValueCoding, - ValueWithValueMetadata.Coder: NSCoding, - ValueWithValueMetadata.Coder.ValueType == ValueWithValueMetadata, - ValueWithValueMetadata.MetadataType: ValueCoding, - ValueWithValueMetadata.MetadataType.Coder: NSCoding, - ValueWithValueMetadata.MetadataType.Coder.ValueType == ValueWithValueMetadata.MetadataType>(item: ValueWithValueMetadata) -> ValueWithValueMetadata { - writeAtIndex(item.index, object: item.encoded, metadata: item.metadata?.encoded) + Value, Metadata where + Value: Persistable, + Value: ValueCoding, + Value.Coder: NSCoding, + Value.Coder.ValueType == Value, + Metadata: ValueCoding, + Metadata.Coder: NSCoding, + Metadata.Coder.ValueType == Metadata>(item: (Value, Metadata?)) -> (Value, Metadata?) { + writeAtIndex(item.0.index, object: item.0.encoded, metadata: item.1?.encoded) return item } @@ -236,16 +235,16 @@ extension WriteTransactionType { - parameter items: a SequenceType of items to store. */ public func write< - Items, ValueWithValueMetadata where + Items, Value, Metadata where Items: SequenceType, - Items.Generator.Element == ValueWithValueMetadata, - ValueWithValueMetadata: Persistable, - ValueWithValueMetadata: ValueCoding, - ValueWithValueMetadata.Coder: NSCoding, - ValueWithValueMetadata.Coder.ValueType == ValueWithValueMetadata, - ValueWithValueMetadata.MetadataType: ValueCoding, - ValueWithValueMetadata.MetadataType.Coder: NSCoding, - ValueWithValueMetadata.MetadataType.Coder.ValueType == ValueWithValueMetadata.MetadataType>(items: Items) -> [ValueWithValueMetadata] { + Items.Generator.Element == (Value, Metadata?), + Value: Persistable, + Value: ValueCoding, + Value.Coder: NSCoding, + Value.Coder.ValueType == Value, + Metadata: ValueCoding, + Metadata.Coder: NSCoding, + Metadata.Coder.ValueType == Metadata>(items: Items) -> [(Value, Metadata?)] { return items.map(write) } } @@ -258,14 +257,14 @@ extension ConnectionType { - parameter item: the item to store. */ public func write< - ValueWithValueMetadata where - ValueWithValueMetadata: Persistable, - ValueWithValueMetadata: ValueCoding, - ValueWithValueMetadata.Coder: NSCoding, - ValueWithValueMetadata.Coder.ValueType == ValueWithValueMetadata, - ValueWithValueMetadata.MetadataType: ValueCoding, - ValueWithValueMetadata.MetadataType.Coder: NSCoding, - ValueWithValueMetadata.MetadataType.Coder.ValueType == ValueWithValueMetadata.MetadataType>(item: ValueWithValueMetadata) -> ValueWithValueMetadata { + Value, Metadata where + Value: Persistable, + Value: ValueCoding, + Value.Coder: NSCoding, + Value.Coder.ValueType == Value, + Metadata: ValueCoding, + Metadata.Coder: NSCoding, + Metadata.Coder.ValueType == Metadata>(item: (Value, Metadata?)) -> (Value, Metadata?) { return write { $0.write(item) } } @@ -275,16 +274,16 @@ extension ConnectionType { - parameter items: a SequenceType of items to store. */ public func write< - Items, ValueWithValueMetadata where + Items, Value, Metadata where Items: SequenceType, - Items.Generator.Element == ValueWithValueMetadata, - ValueWithValueMetadata: Persistable, - ValueWithValueMetadata: ValueCoding, - ValueWithValueMetadata.Coder: NSCoding, - ValueWithValueMetadata.Coder.ValueType == ValueWithValueMetadata, - ValueWithValueMetadata.MetadataType: ValueCoding, - ValueWithValueMetadata.MetadataType.Coder: NSCoding, - ValueWithValueMetadata.MetadataType.Coder.ValueType == ValueWithValueMetadata.MetadataType>(items: Items) -> [ValueWithValueMetadata] { + Items.Generator.Element == (Value, Metadata?), + Value: Persistable, + Value: ValueCoding, + Value.Coder: NSCoding, + Value.Coder.ValueType == Value, + Metadata: ValueCoding, + Metadata.Coder: NSCoding, + Metadata.Coder.ValueType == Metadata>(items: Items) -> [(Value, Metadata?)] { return write { $0.write(items) } } @@ -296,14 +295,14 @@ extension ConnectionType { - parameter completion: a dispatch_block_t for completion. */ public func asyncWrite< - ValueWithValueMetadata where - ValueWithValueMetadata: Persistable, - ValueWithValueMetadata: ValueCoding, - ValueWithValueMetadata.Coder: NSCoding, - ValueWithValueMetadata.Coder.ValueType == ValueWithValueMetadata, - ValueWithValueMetadata.MetadataType: ValueCoding, - ValueWithValueMetadata.MetadataType.Coder: NSCoding, - ValueWithValueMetadata.MetadataType.Coder.ValueType == ValueWithValueMetadata.MetadataType>(item: ValueWithValueMetadata, queue: dispatch_queue_t = dispatch_get_main_queue(), completion: (ValueWithValueMetadata -> Void)? = .None) { + Value, Metadata where + Value: Persistable, + Value: ValueCoding, + Value.Coder: NSCoding, + Value.Coder.ValueType == Value, + Metadata: ValueCoding, + Metadata.Coder: NSCoding, + Metadata.Coder.ValueType == Metadata>(item: (Value, Metadata?), queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ((Value, Metadata?) -> Void)? = .None) { asyncWrite({ $0.write(item) }, queue: queue, completion: completion) } @@ -315,16 +314,16 @@ extension ConnectionType { - parameter completion: a dispatch_block_t for completion. */ public func asyncWrite< - Items, ValueWithValueMetadata where + Items, Value, Metadata where Items: SequenceType, - Items.Generator.Element == ValueWithValueMetadata, - ValueWithValueMetadata: Persistable, - ValueWithValueMetadata: ValueCoding, - ValueWithValueMetadata.Coder: NSCoding, - ValueWithValueMetadata.Coder.ValueType == ValueWithValueMetadata, - ValueWithValueMetadata.MetadataType: ValueCoding, - ValueWithValueMetadata.MetadataType.Coder: NSCoding, - ValueWithValueMetadata.MetadataType.Coder.ValueType == ValueWithValueMetadata.MetadataType>(items: Items, queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ([ValueWithValueMetadata] -> Void)? = .None) { + Items.Generator.Element == (Value, Metadata?), + Value: Persistable, + Value: ValueCoding, + Value.Coder: NSCoding, + Value.Coder.ValueType == Value, + Metadata: ValueCoding, + Metadata.Coder: NSCoding, + Metadata.Coder.ValueType == Metadata>(items: Items, queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ([(Value, Metadata?)] -> Void)? = .None) { asyncWrite({ $0.write(items) }, queue: queue, completion: completion) } } From 39b9461cdca840643fa40409d3d1cb9953344e7d Mon Sep 17 00:00:00 2001 From: Jim Roepcke Date: Fri, 9 Sep 2016 17:04:42 -0700 Subject: [PATCH 04/14] fix persistable versions --- .../Persistable_AnyWithObjectMetadata.swift | 26 +-- .../Persistable_AnyWithValueMetadata.swift | 46 +++-- ...Persistable_ObjectWithObjectMetadata.swift | 119 ++++++----- .../Persistable_ObjectWithValueMetadata.swift | 190 +++++++++++++----- .../Persistable_ValueWithObjectMetadata.swift | 118 ++++++----- .../Persistable_ValueWithValueMetadata.swift | 190 +++++++++++++----- 6 files changed, 466 insertions(+), 223 deletions(-) diff --git a/YapDatabaseExtensions/Shared/Persistable/Persistable_AnyWithObjectMetadata.swift b/YapDatabaseExtensions/Shared/Persistable/Persistable_AnyWithObjectMetadata.swift index 13cf725..6208e01 100644 --- a/YapDatabaseExtensions/Shared/Persistable/Persistable_AnyWithObjectMetadata.swift +++ b/YapDatabaseExtensions/Shared/Persistable/Persistable_AnyWithObjectMetadata.swift @@ -12,27 +12,26 @@ import ValueCoding // MARK: - Readable extension Readable where - ItemType: Persistable, - ItemType.MetadataType: NSCoding { + ItemType: Persistable { - func metadataInTransaction(transaction: Database.Connection.ReadTransaction, atIndex index: YapDB.Index) -> ItemType.MetadataType? { + func metadataInTransaction(transaction: Database.Connection.ReadTransaction, atIndex index: YapDB.Index) -> Metadata? { return transaction.readMetadataAtIndex(index) } - func metadataAtIndexInTransaction(index: YapDB.Index) -> Database.Connection.ReadTransaction -> ItemType.MetadataType? { + func metadataAtIndexInTransaction(index: YapDB.Index) -> Database.Connection.ReadTransaction -> Metadata? { return { self.metadataInTransaction($0, atIndex: index) } } - func metadataInTransactionAtIndex(transaction: Database.Connection.ReadTransaction) -> YapDB.Index -> ItemType.MetadataType? { + func metadataInTransactionAtIndex(transaction: Database.Connection.ReadTransaction) -> YapDB.Index -> Metadata? { return { self.metadataInTransaction(transaction, atIndex: $0) } } func metadataAtIndexesInTransaction< - Indexes where + Indexes, Metadata where Indexes: SequenceType, - Indexes.Generator.Element == YapDB.Index>(indexes: Indexes) -> Database.Connection.ReadTransaction -> [ItemType.MetadataType] { - let atIndex = metadataInTransactionAtIndex - return { indexes.flatMap(atIndex($0)) } + Indexes.Generator.Element == YapDB.Index, + Metadata: NSCoding>(indexes: Indexes) -> Database.Connection.ReadTransaction -> [Metadata] { + return { indexes.flatMap(self.metadataInTransactionAtIndex($0)) } } /** @@ -41,7 +40,9 @@ extension Readable where - parameter index: a YapDB.Index - returns: an optional `ItemType.MetadataType` */ - public func metadataAtIndex(index: YapDB.Index) -> ItemType.MetadataType? { + public func metadataAtIndex< + Metadata where + Metadata: NSCoding>(index: YapDB.Index) -> Metadata? { return sync(metadataAtIndexInTransaction(index)) } @@ -52,9 +53,10 @@ extension Readable where - returns: an array of `ItemType.MetadataType` */ public func metadataAtIndexes< - Indexes where + Indexes, Metadata where Indexes: SequenceType, - Indexes.Generator.Element == YapDB.Index>(indexes: Indexes) -> [ItemType.MetadataType] { + Indexes.Generator.Element == YapDB.Index, + Metadata: NSCoding>(indexes: Indexes) -> [Metadata] { return sync(metadataAtIndexesInTransaction(indexes)) } } diff --git a/YapDatabaseExtensions/Shared/Persistable/Persistable_AnyWithValueMetadata.swift b/YapDatabaseExtensions/Shared/Persistable/Persistable_AnyWithValueMetadata.swift index c27762b..1011383 100644 --- a/YapDatabaseExtensions/Shared/Persistable/Persistable_AnyWithValueMetadata.swift +++ b/YapDatabaseExtensions/Shared/Persistable/Persistable_AnyWithValueMetadata.swift @@ -12,29 +12,40 @@ import ValueCoding // MARK: - Readable extension Readable where - ItemType: Persistable, - ItemType.MetadataType: ValueCoding, - ItemType.MetadataType.Coder: NSCoding, - ItemType.MetadataType.Coder.ValueType == ItemType.MetadataType { + ItemType: Persistable { - func metadataInTransaction(transaction: Database.Connection.ReadTransaction, atIndex index: YapDB.Index) -> ItemType.MetadataType? { + func metadataInTransaction< + Metadata where + Metadata: ValueCoding, + Metadata.Coder: NSCoding, + Metadata.Coder.ValueType == Metadata>(transaction: Database.Connection.ReadTransaction, atIndex index: YapDB.Index) -> Metadata? { return transaction.readMetadataAtIndex(index) } - func metadataAtIndexInTransaction(index: YapDB.Index) -> Database.Connection.ReadTransaction -> ItemType.MetadataType? { + func metadataAtIndexInTransaction< + Metadata where + Metadata: ValueCoding, + Metadata.Coder: NSCoding, + Metadata.Coder.ValueType == Metadata>(index: YapDB.Index) -> Database.Connection.ReadTransaction -> Metadata? { return { self.metadataInTransaction($0, atIndex: index) } } - func metadataInTransactionAtIndex(transaction: Database.Connection.ReadTransaction) -> YapDB.Index -> ItemType.MetadataType? { + func metadataInTransactionAtIndex< + Metadata where + Metadata: ValueCoding, + Metadata.Coder: NSCoding, + Metadata.Coder.ValueType == Metadata>(transaction: Database.Connection.ReadTransaction) -> YapDB.Index -> Metadata? { return { self.metadataInTransaction(transaction, atIndex: $0) } } func metadataAtIndexesInTransaction< - Indexes where + Indexes, Metadata where Indexes: SequenceType, - Indexes.Generator.Element == YapDB.Index>(indexes: Indexes) -> Database.Connection.ReadTransaction -> [ItemType.MetadataType] { - let atIndex = metadataInTransactionAtIndex - return { indexes.flatMap(atIndex($0)) } + Indexes.Generator.Element == YapDB.Index, + Metadata: ValueCoding, + Metadata.Coder: NSCoding, + Metadata.Coder.ValueType == Metadata>(indexes: Indexes) -> Database.Connection.ReadTransaction -> [Metadata] { + return { indexes.flatMap(self.metadataInTransactionAtIndex($0)) } } /** @@ -43,7 +54,11 @@ extension Readable where - parameter index: a YapDB.Index - returns: an optional `ItemType.MetadataType` */ - public func metadataAtIndex(index: YapDB.Index) -> ItemType.MetadataType? { + public func metadataAtIndex< + Metadata where + Metadata: ValueCoding, + Metadata.Coder: NSCoding, + Metadata.Coder.ValueType == Metadata>(index: YapDB.Index) -> Metadata? { return sync(metadataAtIndexInTransaction(index)) } @@ -54,9 +69,12 @@ extension Readable where - returns: an array of `ItemType.MetadataType` */ public func metadataAtIndexes< - Indexes where + Indexes, Metadata where Indexes: SequenceType, - Indexes.Generator.Element == YapDB.Index>(indexes: Indexes) -> [ItemType.MetadataType] { + Indexes.Generator.Element == YapDB.Index, + Metadata: ValueCoding, + Metadata.Coder: NSCoding, + Metadata.Coder.ValueType == Metadata>(indexes: Indexes) -> [Metadata] { return sync(metadataAtIndexesInTransaction(indexes)) } } diff --git a/YapDatabaseExtensions/Shared/Persistable/Persistable_ObjectWithObjectMetadata.swift b/YapDatabaseExtensions/Shared/Persistable/Persistable_ObjectWithObjectMetadata.swift index e910b72..49f9770 100644 --- a/YapDatabaseExtensions/Shared/Persistable/Persistable_ObjectWithObjectMetadata.swift +++ b/YapDatabaseExtensions/Shared/Persistable/Persistable_ObjectWithObjectMetadata.swift @@ -7,13 +7,11 @@ // import Foundation -import ValueCoding // MARK: - Persistable extension Persistable where - Self: NSCoding, - Self.MetadataType: NSCoding { + Self: NSCoding { // Writing @@ -23,8 +21,11 @@ extension Persistable where - parameter transaction: a YapDatabaseReadWriteTransaction - returns: the receiver. */ - public func write(transaction: WriteTransaction) -> Self { - return transaction.write(self) + public func write< + WriteTransaction, Metadata where + WriteTransaction: WriteTransactionType, + Metadata: NSCoding>(transaction: WriteTransaction, metadata: Metadata?) -> (Self, Metadata?) { + return transaction.write((self, metadata)) } /** @@ -33,8 +34,11 @@ extension Persistable where - parameter connection: a YapDatabaseConnection - returns: the receiver. */ - public func write(connection: Connection) -> Self { - return connection.write(self) + public func write< + Connection, Metadata where + Connection: ConnectionType, + Metadata: NSCoding>(connection: Connection, metadata: Metadata?) -> (Self, Metadata?) { + return connection.write((self, metadata)) } /** @@ -43,8 +47,11 @@ extension Persistable where - parameter connection: a YapDatabaseConnection - returns: a closure which receives as an argument the receiver of this function. */ - public func asyncWrite(connection: Connection, queue: dispatch_queue_t = dispatch_get_main_queue(), completion: (Self -> Void)? = .None) { - return connection.asyncWrite(self, queue: queue, completion: completion) + public func asyncWrite< + Connection, Metadata where + Connection: ConnectionType, + Metadata: NSCoding>(connection: Connection, metadata: Metadata?, queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ((Self, Metadata?) -> Void)? = .None) { + return connection.asyncWrite((self, metadata), queue: queue, completion: completion) } /** @@ -53,15 +60,17 @@ extension Persistable where - parameter connection: a YapDatabaseConnection - returns: an `NSOperation` */ - public func writeOperation(connection: Connection) -> NSOperation { - return NSBlockOperation { connection.write(self) } + public func writeOperation< + Connection, Metadata where + Connection: ConnectionType, + Metadata: NSCoding>(connection: Connection, metadata: Metadata?) -> NSOperation { + return NSBlockOperation { connection.write((self, metadata)) } } } extension SequenceType where Generator.Element: Persistable, - Generator.Element: NSCoding, - Generator.Element.MetadataType: NSCoding { + Generator.Element: NSCoding { /** Write the items using an existing transaction. @@ -69,8 +78,12 @@ extension SequenceType where - parameter transaction: a WriteTransactionType e.g. YapDatabaseReadWriteTransaction - returns: the receiver. */ - public func write(transaction: WriteTransaction) -> [Generator.Element] { - return transaction.write(self) + public func write< + WriteTransaction, Metadata where + WriteTransaction: WriteTransactionType, + Metadata: NSCoding>(transaction: WriteTransaction, metadata: [Metadata?]) -> [(Generator.Element, Metadata?)] { + let items = enumerate().map({ (index, element) in (element, metadata[index]) }) + return transaction.write(items) } /** @@ -79,8 +92,12 @@ extension SequenceType where - parameter connection: a ConnectionType e.g. YapDatabaseConnection - returns: the receiver. */ - public func write(connection: Connection) -> [Generator.Element] { - return connection.write(self) + public func write< + Connection, Metadata where + Connection: ConnectionType, + Metadata: NSCoding>(connection: Connection, metadata: [Metadata?]) -> [(Generator.Element, Metadata?)] { + let items = enumerate().map({ (index, element) in (element, metadata[index]) }) + return connection.write(items) } /** @@ -89,8 +106,12 @@ extension SequenceType where - parameter connection: a ConnectionType e.g. YapDatabaseConnection - returns: a closure which receives as an argument the receiver of this function. */ - public func asyncWrite(connection: Connection, queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ([Generator.Element] -> Void)? = .None) { - return connection.asyncWrite(self, queue: queue, completion: completion) + public func asyncWrite< + Connection, Metadata where + Connection: ConnectionType, + Metadata: NSCoding>(connection: Connection, metadata: [Metadata?], queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ([(Generator.Element, Metadata?)] -> Void)? = .None) { + let items = enumerate().map({ (index, element) in (element, metadata[index]) }) + return connection.asyncWrite(items, queue: queue, completion: completion) } /** @@ -99,8 +120,12 @@ extension SequenceType where - parameter connection: a YapDatabaseConnection - returns: an `NSOperation` */ - public func writeOperation(connection: Connection) -> NSOperation { - return NSBlockOperation { connection.write(self) } + public func writeOperation< + Connection, Metadata where + Connection: ConnectionType, + Metadata: NSCoding>(connection: Connection, metadata: [Metadata?]) -> NSOperation { + let items = enumerate().map({ (index, element) in (element, metadata[index]) }) + return NSBlockOperation { connection.write(items) } } } @@ -109,46 +134,44 @@ extension SequenceType where extension Readable where ItemType: NSCoding, - ItemType: Persistable, - ItemType.MetadataType: NSCoding { + ItemType: Persistable { - func inTransaction(transaction: Database.Connection.ReadTransaction, atIndex index: YapDB.Index) -> ItemType? { + func inTransaction(transaction: Database.Connection.ReadTransaction, atIndex index: YapDB.Index) -> (ItemType, Metadata?)? { return transaction.readAtIndex(index) } - func inTransactionAtIndex(transaction: Database.Connection.ReadTransaction) -> YapDB.Index -> ItemType? { + func inTransactionAtIndex(transaction: Database.Connection.ReadTransaction) -> YapDB.Index -> (ItemType, Metadata?)? { return { self.inTransaction(transaction, atIndex: $0) } } - func atIndexInTransaction(index: YapDB.Index) -> Database.Connection.ReadTransaction -> ItemType? { + func atIndexInTransaction(index: YapDB.Index) -> Database.Connection.ReadTransaction -> (ItemType, Metadata?)? { return { self.inTransaction($0, atIndex: index) } } func atIndexesInTransaction< - Indexes where + Indexes, Metadata where Indexes: SequenceType, - Indexes.Generator.Element == YapDB.Index>(indexes: Indexes) -> Database.Connection.ReadTransaction -> [ItemType] { - let atIndex = inTransactionAtIndex - return { indexes.flatMap(atIndex($0)) } + Indexes.Generator.Element == YapDB.Index, + Metadata: NSCoding>(indexes: Indexes) -> Database.Connection.ReadTransaction -> [(ItemType, Metadata?)] { + return { indexes.flatMap(self.inTransactionAtIndex($0)) } } - func inTransaction(transaction: Database.Connection.ReadTransaction, byKey key: String) -> ItemType? { + func inTransaction(transaction: Database.Connection.ReadTransaction, byKey key: String) -> (ItemType, Metadata?)? { return inTransaction(transaction, atIndex: ItemType.indexWithKey(key)) } - func inTransactionByKey(transaction: Database.Connection.ReadTransaction) -> String -> ItemType? { + func inTransactionByKey(transaction: Database.Connection.ReadTransaction) -> String -> (ItemType, Metadata?)? { return { self.inTransaction(transaction, byKey: $0) } } - func byKeyInTransaction(key: String) -> Database.Connection.ReadTransaction -> ItemType? { + func byKeyInTransaction(key: String) -> Database.Connection.ReadTransaction -> (ItemType, Metadata?)? { return { self.inTransaction($0, byKey: key) } } - func byKeysInTransaction(keys: [String]? = .None) -> Database.Connection.ReadTransaction -> [ItemType] { - let byKey = inTransactionByKey + func byKeysInTransaction(keys: [String]? = .None) -> Database.Connection.ReadTransaction -> [(ItemType, Metadata?)] { return { transaction in let keys = keys ?? transaction.keysInCollection(ItemType.collection) - return keys.flatMap(byKey(transaction)) + return keys.flatMap(self.inTransactionByKey(transaction)) } } @@ -158,7 +181,7 @@ extension Readable where - parameter index: a YapDB.Index - returns: an optional `ItemType` */ - public func atIndex(index: YapDB.Index) -> ItemType? { + public func atIndex(index: YapDB.Index) -> (ItemType, Metadata?)? { return sync(atIndexInTransaction(index)) } @@ -169,9 +192,10 @@ extension Readable where - returns: an array of `ItemType` */ public func atIndexes< - Indexes where + Indexes, Metadata where Indexes: SequenceType, - Indexes.Generator.Element == YapDB.Index>(indexes: Indexes) -> [ItemType] { + Indexes.Generator.Element == YapDB.Index, + Metadata: NSCoding>(indexes: Indexes) -> [(ItemType, Metadata?)] { return sync(atIndexesInTransaction(indexes)) } @@ -181,7 +205,7 @@ extension Readable where - parameter key: a String - returns: an optional `ItemType` */ - public func byKey(key: String) -> ItemType? { + public func byKey(key: String) -> (ItemType, Metadata?)? { return sync(byKeyInTransaction(key)) } @@ -192,9 +216,10 @@ extension Readable where - returns: an array of `ItemType` */ public func byKeys< - Keys where + Keys, Metadata where Keys: SequenceType, - Keys.Generator.Element == String>(keys: Keys) -> [ItemType] { + Keys.Generator.Element == String, + Metadata: NSCoding>(keys: Keys) -> [(ItemType, Metadata?)] { return sync(byKeysInTransaction(Array(keys))) } @@ -203,7 +228,7 @@ extension Readable where - returns: an array of `ItemType` */ - public func all() -> [ItemType] { + public func all() -> [(ItemType, Metadata?)] { return sync(byKeysInTransaction()) } @@ -213,11 +238,11 @@ extension Readable where - 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 + public func filterExisting(keys: [String]) -> (existing: [(ItemType, Metadata?)], missing: [String]) { + let existingInTransaction: Database.Connection.ReadTransaction -> [(ItemType, Metadata?)] = byKeysInTransaction(keys) + return sync { transaction -> ([(ItemType, Metadata?)], [String]) in let existing = existingInTransaction(transaction) - let existingKeys = existing.map(keyForPersistable) + let existingKeys = existing.map {keyForPersistable($0.0)} let missingKeys = keys.filter { !existingKeys.contains($0) } return (existing, missingKeys) } diff --git a/YapDatabaseExtensions/Shared/Persistable/Persistable_ObjectWithValueMetadata.swift b/YapDatabaseExtensions/Shared/Persistable/Persistable_ObjectWithValueMetadata.swift index 2d63a2a..aa45d61 100644 --- a/YapDatabaseExtensions/Shared/Persistable/Persistable_ObjectWithValueMetadata.swift +++ b/YapDatabaseExtensions/Shared/Persistable/Persistable_ObjectWithValueMetadata.swift @@ -12,10 +12,7 @@ import ValueCoding // MARK: - Persistable extension Persistable where - Self: NSCoding, - Self.MetadataType: ValueCoding, - Self.MetadataType.Coder: NSCoding, - Self.MetadataType.Coder.ValueType == Self.MetadataType { + Self: NSCoding { // Writing @@ -25,8 +22,13 @@ extension Persistable where - parameter transaction: a YapDatabaseReadWriteTransaction - returns: the receiver. */ - public func write(transaction: WriteTransaction) -> Self { - return transaction.write(self) + public func write< + WriteTransaction, Metadata where + WriteTransaction: WriteTransactionType, + Metadata: ValueCoding, + Metadata.Coder: NSCoding, + Metadata.Coder.ValueType == Metadata>(transaction: WriteTransaction, metadata: Metadata?) -> (Self, Metadata?) { + return transaction.write((self, metadata)) } /** @@ -35,8 +37,13 @@ extension Persistable where - parameter connection: a YapDatabaseConnection - returns: the receiver. */ - public func write(connection: Connection) -> Self { - return connection.write(self) + public func write< + Connection, Metadata where + Connection: ConnectionType, + Metadata: ValueCoding, + Metadata.Coder: NSCoding, + Metadata.Coder.ValueType == Metadata>(connection: Connection, metadata: Metadata?) -> (Self, Metadata?) { + return connection.write((self, metadata)) } /** @@ -45,8 +52,13 @@ extension Persistable where - parameter connection: a YapDatabaseConnection - returns: a closure which receives as an argument the receiver of this function. */ - public func asyncWrite(connection: Connection, queue: dispatch_queue_t = dispatch_get_main_queue(), completion: (Self -> Void)? = .None) { - return connection.asyncWrite(self, queue: queue, completion: completion) + public func asyncWrite< + Connection, Metadata where + Connection: ConnectionType, + Metadata: ValueCoding, + Metadata.Coder: NSCoding, + Metadata.Coder.ValueType == Metadata>(connection: Connection, metadata: Metadata?, queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ((Self, Metadata?) -> Void)? = .None) { + return connection.asyncWrite((self, metadata), queue: queue, completion: completion) } /** @@ -55,17 +67,19 @@ extension Persistable where - parameter connection: a YapDatabaseConnection - returns: an `NSOperation` */ - public func writeOperation(connection: Connection) -> NSOperation { - return NSBlockOperation { connection.write(self) } + public func writeOperation< + Connection, Metadata where + Connection: ConnectionType, + Metadata: ValueCoding, + Metadata.Coder: NSCoding, + Metadata.Coder.ValueType == Metadata>(connection: Connection, metadata: Metadata?) -> NSOperation { + return NSBlockOperation { connection.write((self, metadata)) } } } extension SequenceType where Generator.Element: Persistable, - Generator.Element: NSCoding, - Generator.Element.MetadataType: ValueCoding, - Generator.Element.MetadataType.Coder: NSCoding, - Generator.Element.MetadataType.Coder.ValueType == Generator.Element.MetadataType { + Generator.Element: NSCoding { /** Write the items using an existing transaction. @@ -73,8 +87,14 @@ extension SequenceType where - parameter transaction: a WriteTransactionType e.g. YapDatabaseReadWriteTransaction - returns: the receiver. */ - public func write(transaction: WriteTransaction) -> [Generator.Element] { - return transaction.write(self) + public func write< + WriteTransaction, Metadata where + WriteTransaction: WriteTransactionType, + Metadata: ValueCoding, + Metadata.Coder: NSCoding, + Metadata.Coder.ValueType == Metadata>(transaction: WriteTransaction, metadata: [Metadata?]) -> [(Generator.Element, Metadata?)] { + let items = enumerate().map({ (index, element) in (element, metadata[index]) }) + return transaction.write(items) } /** @@ -83,8 +103,14 @@ extension SequenceType where - parameter connection: a ConnectionType e.g. YapDatabaseConnection - returns: the receiver. */ - public func write(connection: Connection) -> [Generator.Element] { - return connection.write(self) + public func write< + Connection, Metadata where + Connection: ConnectionType, + Metadata: ValueCoding, + Metadata.Coder: NSCoding, + Metadata.Coder.ValueType == Metadata>(connection: Connection, metadata: [Metadata?]) -> [(Generator.Element, Metadata?)] { + let items = enumerate().map({ (index, element) in (element, metadata[index]) }) + return connection.write(items) } /** @@ -93,8 +119,14 @@ extension SequenceType where - parameter connection: a ConnectionType e.g. YapDatabaseConnection - returns: a closure which receives as an argument the receiver of this function. */ - public func asyncWrite(connection: Connection, queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ([Generator.Element] -> Void)? = .None) { - return connection.asyncWrite(self, queue: queue, completion: completion) + public func asyncWrite< + Connection, Metadata where + Connection: ConnectionType, + Metadata: ValueCoding, + Metadata.Coder: NSCoding, + Metadata.Coder.ValueType == Metadata>(connection: Connection, metadata: [Metadata?], queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ([(Generator.Element, Metadata?)] -> Void)? = .None) { + let items = enumerate().map({ (index, element) in (element, metadata[index]) }) + return connection.asyncWrite(items, queue: queue, completion: completion) } /** @@ -103,8 +135,14 @@ extension SequenceType where - parameter connection: a YapDatabaseConnection - returns: an `NSOperation` */ - public func writeOperation(connection: Connection) -> NSOperation { - return NSBlockOperation { connection.write(self) } + public func writeOperation< + Connection, Metadata where + Connection: ConnectionType, + Metadata: ValueCoding, + Metadata.Coder: NSCoding, + Metadata.Coder.ValueType == Metadata>(connection: Connection, metadata: [Metadata?]) -> NSOperation { + let items = enumerate().map({ (index, element) in (element, metadata[index]) }) + return NSBlockOperation { connection.write(items) } } } @@ -112,48 +150,74 @@ extension SequenceType where extension Readable where ItemType: NSCoding, - ItemType: Persistable, - ItemType.MetadataType: ValueCoding, - ItemType.MetadataType.Coder: NSCoding, - ItemType.MetadataType.Coder.ValueType == ItemType.MetadataType { + ItemType: Persistable { - func inTransaction(transaction: Database.Connection.ReadTransaction, atIndex index: YapDB.Index) -> ItemType? { + func inTransaction< + Metadata where + Metadata: ValueCoding, + Metadata.Coder: NSCoding, + Metadata.Coder.ValueType == Metadata>(transaction: Database.Connection.ReadTransaction, atIndex index: YapDB.Index) -> (ItemType, Metadata?)? { return transaction.readAtIndex(index) } - func inTransactionAtIndex(transaction: Database.Connection.ReadTransaction) -> YapDB.Index -> ItemType? { + func inTransactionAtIndex< + Metadata where + Metadata: ValueCoding, + Metadata.Coder: NSCoding, + Metadata.Coder.ValueType == Metadata>(transaction: Database.Connection.ReadTransaction) -> YapDB.Index -> (ItemType, Metadata?)? { return { self.inTransaction(transaction, atIndex: $0) } } - func atIndexInTransaction(index: YapDB.Index) -> Database.Connection.ReadTransaction -> ItemType? { + func atIndexInTransaction< + Metadata where + Metadata: ValueCoding, + Metadata.Coder: NSCoding, + Metadata.Coder.ValueType == Metadata>(index: YapDB.Index) -> Database.Connection.ReadTransaction -> (ItemType, Metadata?)? { return { self.inTransaction($0, atIndex: index) } } func atIndexesInTransaction< - Indexes where + Indexes, Metadata where Indexes: SequenceType, - Indexes.Generator.Element == YapDB.Index>(indexes: Indexes) -> Database.Connection.ReadTransaction -> [ItemType] { - let atIndex = inTransactionAtIndex - return { indexes.flatMap(atIndex($0)) } + Indexes.Generator.Element == YapDB.Index, + Metadata: ValueCoding, + Metadata.Coder: NSCoding, + Metadata.Coder.ValueType == Metadata>(indexes: Indexes) -> Database.Connection.ReadTransaction -> [(ItemType, Metadata?)] { + return { indexes.flatMap(self.inTransactionAtIndex($0)) } } - func inTransaction(transaction: Database.Connection.ReadTransaction, byKey key: String) -> ItemType? { + func inTransaction< + Metadata where + Metadata: ValueCoding, + Metadata.Coder: NSCoding, + Metadata.Coder.ValueType == Metadata>(transaction: Database.Connection.ReadTransaction, byKey key: String) -> (ItemType, Metadata?)? { return inTransaction(transaction, atIndex: ItemType.indexWithKey(key)) } - func inTransactionByKey(transaction: Database.Connection.ReadTransaction) -> String -> ItemType? { + func inTransactionByKey< + Metadata where + Metadata: ValueCoding, + Metadata.Coder: NSCoding, + Metadata.Coder.ValueType == Metadata>(transaction: Database.Connection.ReadTransaction) -> String -> (ItemType, Metadata?)? { return { self.inTransaction(transaction, byKey: $0) } } - func byKeyInTransaction(key: String) -> Database.Connection.ReadTransaction -> ItemType? { + func byKeyInTransaction< + Metadata where + Metadata: ValueCoding, + Metadata.Coder: NSCoding, + Metadata.Coder.ValueType == Metadata>(key: String) -> Database.Connection.ReadTransaction -> (ItemType, Metadata?)? { return { self.inTransaction($0, byKey: key) } } - func byKeysInTransaction(keys: [String]? = .None) -> Database.Connection.ReadTransaction -> [ItemType] { - let byKey = inTransactionByKey + func byKeysInTransaction< + Metadata where + Metadata: ValueCoding, + Metadata.Coder: NSCoding, + Metadata.Coder.ValueType == Metadata>(keys: [String]? = .None) -> Database.Connection.ReadTransaction -> [(ItemType, Metadata?)] { return { transaction in let keys = keys ?? transaction.keysInCollection(ItemType.collection) - return keys.flatMap(byKey(transaction)) + return keys.flatMap(self.inTransactionByKey(transaction)) } } @@ -163,7 +227,11 @@ extension Readable where - parameter index: a YapDB.Index - returns: an optional `ItemType` */ - public func atIndex(index: YapDB.Index) -> ItemType? { + public func atIndex< + Metadata where + Metadata: ValueCoding, + Metadata.Coder: NSCoding, + Metadata.Coder.ValueType == Metadata>(index: YapDB.Index) -> (ItemType, Metadata?)? { return sync(atIndexInTransaction(index)) } @@ -174,9 +242,12 @@ extension Readable where - returns: an array of `ItemType` */ public func atIndexes< - Indexes where + Indexes, Metadata where Indexes: SequenceType, - Indexes.Generator.Element == YapDB.Index>(indexes: Indexes) -> [ItemType] { + Indexes.Generator.Element == YapDB.Index, + Metadata: ValueCoding, + Metadata.Coder: NSCoding, + Metadata.Coder.ValueType == Metadata>(indexes: Indexes) -> [(ItemType, Metadata?)] { return sync(atIndexesInTransaction(indexes)) } @@ -186,7 +257,11 @@ extension Readable where - parameter key: a String - returns: an optional `ItemType` */ - public func byKey(key: String) -> ItemType? { + public func byKey< + Metadata where + Metadata: ValueCoding, + Metadata.Coder: NSCoding, + Metadata.Coder.ValueType == Metadata>(key: String) -> (ItemType, Metadata?)? { return sync(byKeyInTransaction(key)) } @@ -197,9 +272,12 @@ extension Readable where - returns: an array of `ItemType` */ public func byKeys< - Keys where + Keys, Metadata where Keys: SequenceType, - Keys.Generator.Element == String>(keys: Keys) -> [ItemType] { + Keys.Generator.Element == String, + Metadata: ValueCoding, + Metadata.Coder: NSCoding, + Metadata.Coder.ValueType == Metadata>(keys: Keys) -> [(ItemType, Metadata?)] { return sync(byKeysInTransaction(Array(keys))) } @@ -208,7 +286,11 @@ extension Readable where - returns: an array of `ItemType` */ - public func all() -> [ItemType] { + public func all< + Metadata where + Metadata: ValueCoding, + Metadata.Coder: NSCoding, + Metadata.Coder.ValueType == Metadata>() -> [(ItemType, Metadata?)] { return sync(byKeysInTransaction()) } @@ -218,11 +300,15 @@ extension Readable where - 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 + public func filterExisting< + Metadata where + Metadata: ValueCoding, + Metadata.Coder: NSCoding, + Metadata.Coder.ValueType == Metadata>(keys: [String]) -> (existing: [(ItemType, Metadata?)], missing: [String]) { + let existingInTransaction: Database.Connection.ReadTransaction -> [(ItemType, Metadata?)] = byKeysInTransaction(keys) + return sync { transaction -> ([(ItemType, Metadata?)], [String]) in let existing = existingInTransaction(transaction) - let existingKeys = existing.map(keyForPersistable) + let existingKeys = existing.map {keyForPersistable($0.0)} let missingKeys = keys.filter { !existingKeys.contains($0) } return (existing, missingKeys) } diff --git a/YapDatabaseExtensions/Shared/Persistable/Persistable_ValueWithObjectMetadata.swift b/YapDatabaseExtensions/Shared/Persistable/Persistable_ValueWithObjectMetadata.swift index ceb3f47..5e2e458 100644 --- a/YapDatabaseExtensions/Shared/Persistable/Persistable_ValueWithObjectMetadata.swift +++ b/YapDatabaseExtensions/Shared/Persistable/Persistable_ValueWithObjectMetadata.swift @@ -14,8 +14,7 @@ import ValueCoding extension Persistable where Self: ValueCoding, Self.Coder: NSCoding, - Self.Coder.ValueType == Self, - Self.MetadataType: NSCoding { + Self.Coder.ValueType == Self { // Writing @@ -25,8 +24,11 @@ extension Persistable where - parameter transaction: a YapDatabaseReadWriteTransaction - returns: the receiver. */ - public func write(transaction: WriteTransaction) -> Self { - return transaction.write(self) + public func write< + WriteTransaction, Metadata where + WriteTransaction: WriteTransactionType, + Metadata: NSCoding>(transaction: WriteTransaction, metadata: Metadata?) -> (Self, Metadata?) { + return transaction.write((self, metadata)) } /** @@ -35,8 +37,11 @@ extension Persistable where - parameter connection: a YapDatabaseConnection - returns: the receiver. */ - public func write(connection: Connection) -> Self { - return connection.write(self) + public func write< + Connection, Metadata where + Connection: ConnectionType, + Metadata: NSCoding>(connection: Connection, metadata: Metadata?) -> (Self, Metadata?) { + return connection.write((self, metadata)) } /** @@ -45,8 +50,11 @@ extension Persistable where - parameter connection: a YapDatabaseConnection - returns: a closure which receives as an argument the receiver of this function. */ - public func asyncWrite(connection: Connection, queue: dispatch_queue_t = dispatch_get_main_queue(), completion: (Self -> Void)? = .None) { - return connection.asyncWrite(self, queue: queue, completion: completion) + public func asyncWrite< + Connection, Metadata where + Connection: ConnectionType, + Metadata: NSCoding>(connection: Connection, metadata: Metadata?, queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ((Self, Metadata?) -> Void)? = .None) { + return connection.asyncWrite((self, metadata), queue: queue, completion: completion) } /** @@ -55,8 +63,11 @@ extension Persistable where - parameter connection: a YapDatabaseConnection - returns: an `NSOperation` */ - public func writeOperation(connection: Connection) -> NSOperation { - return NSBlockOperation { connection.write(self) } + public func writeOperation< + Connection, Metadata where + Connection: ConnectionType, + Metadata: NSCoding>(connection: Connection, metadata: Metadata?) -> NSOperation { + return NSBlockOperation { connection.write((self, metadata)) } } } @@ -64,8 +75,7 @@ extension SequenceType where Generator.Element: Persistable, Generator.Element: ValueCoding, Generator.Element.Coder: NSCoding, - Generator.Element.Coder.ValueType == Generator.Element, - Generator.Element.MetadataType: NSCoding { + Generator.Element.Coder.ValueType == Generator.Element { /** Write the items using an existing transaction. @@ -73,8 +83,12 @@ extension SequenceType where - parameter transaction: a WriteTransactionType e.g. YapDatabaseReadWriteTransaction - returns: the receiver. */ - public func write(transaction: WriteTransaction) -> [Generator.Element] { - return transaction.write(self) + public func write< + WriteTransaction, Metadata where + WriteTransaction: WriteTransactionType, + Metadata: NSCoding>(transaction: WriteTransaction, metadata: [Metadata?]) -> [(Generator.Element, Metadata?)] { + let items = enumerate().map({ (index, element) in (element, metadata[index]) }) + return transaction.write(items) } /** @@ -83,8 +97,12 @@ extension SequenceType where - parameter connection: a ConnectionType e.g. YapDatabaseConnection - returns: the receiver. */ - public func write(connection: Connection) -> [Generator.Element] { - return connection.write(self) + public func write< + Connection, Metadata where + Connection: ConnectionType, + Metadata: NSCoding>(connection: Connection, metadata: [Metadata?]) -> [(Generator.Element, Metadata?)] { + let items = enumerate().map({ (index, element) in (element, metadata[index]) }) + return connection.write(items) } /** @@ -93,8 +111,12 @@ extension SequenceType where - parameter connection: a ConnectionType e.g. YapDatabaseConnection - returns: a closure which receives as an argument the receiver of this function. */ - public func asyncWrite(connection: Connection, queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ([Generator.Element] -> Void)? = .None) { - return connection.asyncWrite(self, queue: queue, completion: completion) + public func asyncWrite< + Connection, Metadata where + Connection: ConnectionType, + Metadata: NSCoding>(connection: Connection, metadata: [Metadata?], queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ([(Generator.Element, Metadata?)] -> Void)? = .None) { + let items = enumerate().map({ (index, element) in (element, metadata[index]) }) + return connection.asyncWrite(items, queue: queue, completion: completion) } /** @@ -103,8 +125,12 @@ extension SequenceType where - parameter connection: a YapDatabaseConnection - returns: an `NSOperation` */ - public func writeOperation(connection: Connection) -> NSOperation { - return NSBlockOperation { connection.write(self) } + public func writeOperation< + Connection, Metadata where + Connection: ConnectionType, + Metadata: NSCoding>(connection: Connection, metadata: [Metadata?]) -> NSOperation { + let items = enumerate().map({ (index, element) in (element, metadata[index]) }) + return NSBlockOperation { connection.write(items) } } } @@ -115,46 +141,44 @@ extension Readable where ItemType: ValueCoding, ItemType: Persistable, ItemType.Coder: NSCoding, - ItemType.Coder.ValueType == ItemType, - ItemType.MetadataType: NSCoding { + ItemType.Coder.ValueType == ItemType { - func inTransaction(transaction: Database.Connection.ReadTransaction, atIndex index: YapDB.Index) -> ItemType? { + func inTransaction(transaction: Database.Connection.ReadTransaction, atIndex index: YapDB.Index) -> (ItemType, Metadata?)? { return transaction.readAtIndex(index) } - func inTransactionAtIndex(transaction: Database.Connection.ReadTransaction) -> YapDB.Index -> ItemType? { + func inTransactionAtIndex(transaction: Database.Connection.ReadTransaction) -> YapDB.Index -> (ItemType, Metadata?)? { return { self.inTransaction(transaction, atIndex: $0) } } - func atIndexInTransaction(index: YapDB.Index) -> Database.Connection.ReadTransaction -> ItemType? { + func atIndexInTransaction(index: YapDB.Index) -> Database.Connection.ReadTransaction -> (ItemType, Metadata?)? { return { self.inTransaction($0, atIndex: index) } } func atIndexesInTransaction< - Indexes where + Indexes, Metadata where Indexes: SequenceType, - Indexes.Generator.Element == YapDB.Index>(indexes: Indexes) -> Database.Connection.ReadTransaction -> [ItemType] { - let atIndex = inTransactionAtIndex - return { indexes.flatMap(atIndex($0)) } + Indexes.Generator.Element == YapDB.Index, + Metadata: NSCoding>(indexes: Indexes) -> Database.Connection.ReadTransaction -> [(ItemType, Metadata?)] { + return { indexes.flatMap(self.inTransactionAtIndex($0)) } } - func inTransaction(transaction: Database.Connection.ReadTransaction, byKey key: String) -> ItemType? { + func inTransaction(transaction: Database.Connection.ReadTransaction, byKey key: String) -> (ItemType, Metadata?)? { return inTransaction(transaction, atIndex: ItemType.indexWithKey(key)) } - func inTransactionByKey(transaction: Database.Connection.ReadTransaction) -> String -> ItemType? { + func inTransactionByKey(transaction: Database.Connection.ReadTransaction) -> String -> (ItemType, Metadata?)? { return { self.inTransaction(transaction, byKey: $0) } } - func byKeyInTransaction(key: String) -> Database.Connection.ReadTransaction -> ItemType? { + func byKeyInTransaction(key: String) -> Database.Connection.ReadTransaction -> (ItemType, Metadata?)? { return { self.inTransaction($0, byKey: key) } } - func byKeysInTransaction(keys: [String]? = .None) -> Database.Connection.ReadTransaction -> [ItemType] { - let byKey = inTransactionByKey + func byKeysInTransaction(keys: [String]? = .None) -> Database.Connection.ReadTransaction -> [(ItemType, Metadata?)] { return { transaction in let keys = keys ?? transaction.keysInCollection(ItemType.collection) - return keys.flatMap(byKey(transaction)) + return keys.flatMap(self.inTransactionByKey(transaction)) } } @@ -164,7 +188,7 @@ extension Readable where - parameter index: a YapDB.Index - returns: an optional `ItemType` */ - public func atIndex(index: YapDB.Index) -> ItemType? { + public func atIndex(index: YapDB.Index) -> (ItemType, Metadata?)? { return sync(atIndexInTransaction(index)) } @@ -175,9 +199,10 @@ extension Readable where - returns: an array of `ItemType` */ public func atIndexes< - Indexes where + Indexes, Metadata where Indexes: SequenceType, - Indexes.Generator.Element == YapDB.Index>(indexes: Indexes) -> [ItemType] { + Indexes.Generator.Element == YapDB.Index, + Metadata: NSCoding>(indexes: Indexes) -> [(ItemType, Metadata?)] { return sync(atIndexesInTransaction(indexes)) } @@ -187,7 +212,7 @@ extension Readable where - parameter key: a String - returns: an optional `ItemType` */ - public func byKey(key: String) -> ItemType? { + public func byKey(key: String) -> (ItemType, Metadata?)? { return sync(byKeyInTransaction(key)) } @@ -198,9 +223,10 @@ extension Readable where - returns: an array of `ItemType` */ public func byKeys< - Keys where + Keys, Metadata where Keys: SequenceType, - Keys.Generator.Element == String>(keys: Keys) -> [ItemType] { + Keys.Generator.Element == String, + Metadata: NSCoding>(keys: Keys) -> [(ItemType, Metadata?)] { return sync(byKeysInTransaction(Array(keys))) } @@ -209,7 +235,7 @@ extension Readable where - returns: an array of `ItemType` */ - public func all() -> [ItemType] { + public func all() -> [(ItemType, Metadata?)] { return sync(byKeysInTransaction()) } @@ -219,11 +245,11 @@ extension Readable where - 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 + public func filterExisting(keys: [String]) -> (existing: [(ItemType, Metadata?)], missing: [String]) { + let existingInTransaction: Database.Connection.ReadTransaction -> [(ItemType, Metadata?)] = byKeysInTransaction(keys) + return sync { transaction -> ([(ItemType, Metadata?)], [String]) in let existing = existingInTransaction(transaction) - let existingKeys = existing.map(keyForPersistable) + let existingKeys = existing.map {keyForPersistable($0.0)} let missingKeys = keys.filter { !existingKeys.contains($0) } return (existing, missingKeys) } diff --git a/YapDatabaseExtensions/Shared/Persistable/Persistable_ValueWithValueMetadata.swift b/YapDatabaseExtensions/Shared/Persistable/Persistable_ValueWithValueMetadata.swift index 5b7b0fd..69582c5 100644 --- a/YapDatabaseExtensions/Shared/Persistable/Persistable_ValueWithValueMetadata.swift +++ b/YapDatabaseExtensions/Shared/Persistable/Persistable_ValueWithValueMetadata.swift @@ -14,10 +14,7 @@ import ValueCoding extension Persistable where Self: ValueCoding, Self.Coder: NSCoding, - Self.Coder.ValueType == Self, - Self.MetadataType: ValueCoding, - Self.MetadataType.Coder: NSCoding, - Self.MetadataType.Coder.ValueType == Self.MetadataType { + Self.Coder.ValueType == Self { // Writing @@ -27,8 +24,13 @@ extension Persistable where - parameter transaction: a YapDatabaseReadWriteTransaction - returns: the receiver. */ - public func write(transaction: WriteTransaction) -> Self { - return transaction.write(self) + public func write< + WriteTransaction, Metadata where + WriteTransaction: WriteTransactionType, + Metadata: ValueCoding, + Metadata.Coder: NSCoding, + Metadata.Coder.ValueType == Metadata>(transaction: WriteTransaction, metadata: Metadata?) -> (Self, Metadata?) { + return transaction.write((self, metadata)) } /** @@ -37,8 +39,13 @@ extension Persistable where - parameter connection: a YapDatabaseConnection - returns: the receiver. */ - public func write(connection: Connection) -> Self { - return connection.write(self) + public func write< + Connection, Metadata where + Connection: ConnectionType, + Metadata: ValueCoding, + Metadata.Coder: NSCoding, + Metadata.Coder.ValueType == Metadata>(connection: Connection, metadata: Metadata?) -> (Self, Metadata?) { + return connection.write((self, metadata)) } /** @@ -47,8 +54,13 @@ extension Persistable where - parameter connection: a YapDatabaseConnection - returns: a closure which receives as an argument the receiver of this function. */ - public func asyncWrite(connection: Connection, queue: dispatch_queue_t = dispatch_get_main_queue(), completion: (Self -> Void)? = .None) { - return connection.asyncWrite(self, queue: queue, completion: completion) + public func asyncWrite< + Connection, Metadata where + Connection: ConnectionType, + Metadata: ValueCoding, + Metadata.Coder: NSCoding, + Metadata.Coder.ValueType == Metadata>(connection: Connection, metadata: Metadata?, queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ((Self, Metadata?) -> Void)? = .None) { + return connection.asyncWrite((self, metadata), queue: queue, completion: completion) } /** @@ -57,8 +69,13 @@ extension Persistable where - parameter connection: a YapDatabaseConnection - returns: an `NSOperation` */ - public func writeOperation(connection: Connection) -> NSOperation { - return NSBlockOperation { connection.write(self) } + public func writeOperation< + Connection, Metadata where + Connection: ConnectionType, + Metadata: ValueCoding, + Metadata.Coder: NSCoding, + Metadata.Coder.ValueType == Metadata>(connection: Connection, metadata: Metadata?) -> NSOperation { + return NSBlockOperation { connection.write((self, metadata)) } } } @@ -66,10 +83,7 @@ extension SequenceType where Generator.Element: Persistable, Generator.Element: ValueCoding, Generator.Element.Coder: NSCoding, - Generator.Element.Coder.ValueType == Generator.Element, - Generator.Element.MetadataType: ValueCoding, - Generator.Element.MetadataType.Coder: NSCoding, - Generator.Element.MetadataType.Coder.ValueType == Generator.Element.MetadataType { + Generator.Element.Coder.ValueType == Generator.Element { /** Write the items using an existing transaction. @@ -77,8 +91,14 @@ extension SequenceType where - parameter transaction: a WriteTransactionType e.g. YapDatabaseReadWriteTransaction - returns: the receiver. */ - public func write(transaction: WriteTransaction) -> [Generator.Element] { - return transaction.write(self) + public func write< + WriteTransaction, Metadata where + WriteTransaction: WriteTransactionType, + Metadata: ValueCoding, + Metadata.Coder: NSCoding, + Metadata.Coder.ValueType == Metadata>(transaction: WriteTransaction, metadata: [Metadata?]) -> [(Generator.Element, Metadata?)] { + let items = enumerate().map({ (index, element) in (element, metadata[index]) }) + return transaction.write(items) } /** @@ -87,8 +107,14 @@ extension SequenceType where - parameter connection: a ConnectionType e.g. YapDatabaseConnection - returns: the receiver. */ - public func write(connection: Connection) -> [Generator.Element] { - return connection.write(self) + public func write< + Connection, Metadata where + Connection: ConnectionType, + Metadata: ValueCoding, + Metadata.Coder: NSCoding, + Metadata.Coder.ValueType == Metadata>(connection: Connection, metadata: [Metadata?]) -> [(Generator.Element, Metadata?)] { + let items = enumerate().map({ (index, element) in (element, metadata[index]) }) + return connection.write(items) } /** @@ -97,8 +123,14 @@ extension SequenceType where - parameter connection: a ConnectionType e.g. YapDatabaseConnection - returns: a closure which receives as an argument the receiver of this function. */ - public func asyncWrite(connection: Connection, queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ([Generator.Element] -> Void)? = .None) { - return connection.asyncWrite(self, queue: queue, completion: completion) + public func asyncWrite< + Connection, Metadata where + Connection: ConnectionType, + Metadata: ValueCoding, + Metadata.Coder: NSCoding, + Metadata.Coder.ValueType == Metadata>(connection: Connection, metadata: [Metadata?], queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ([(Generator.Element, Metadata?)] -> Void)? = .None) { + let items = enumerate().map({ (index, element) in (element, metadata[index]) }) + return connection.asyncWrite(items, queue: queue, completion: completion) } /** @@ -107,8 +139,14 @@ extension SequenceType where - parameter connection: a YapDatabaseConnection - returns: an `NSOperation` */ - public func writeOperation(connection: Connection) -> NSOperation { - return NSBlockOperation { connection.write(self) } + public func writeOperation< + Connection, Metadata where + Connection: ConnectionType, + Metadata: ValueCoding, + Metadata.Coder: NSCoding, + Metadata.Coder.ValueType == Metadata>(connection: Connection, metadata: [Metadata?]) -> NSOperation { + let items = enumerate().map({ (index, element) in (element, metadata[index]) }) + return NSBlockOperation { connection.write(items) } } } @@ -118,48 +156,74 @@ extension Readable where ItemType: ValueCoding, ItemType: Persistable, ItemType.Coder: NSCoding, - ItemType.Coder.ValueType == ItemType, - ItemType.MetadataType: ValueCoding, - ItemType.MetadataType.Coder: NSCoding, - ItemType.MetadataType.Coder.ValueType == ItemType.MetadataType { + ItemType.Coder.ValueType == ItemType { - func inTransaction(transaction: Database.Connection.ReadTransaction, atIndex index: YapDB.Index) -> ItemType? { + func inTransaction< + Metadata where + Metadata: ValueCoding, + Metadata.Coder: NSCoding, + Metadata.Coder.ValueType == Metadata>(transaction: Database.Connection.ReadTransaction, atIndex index: YapDB.Index) -> (ItemType, Metadata?)? { return transaction.readAtIndex(index) } - func inTransactionAtIndex(transaction: Database.Connection.ReadTransaction) -> YapDB.Index -> ItemType? { + func inTransactionAtIndex< + Metadata where + Metadata: ValueCoding, + Metadata.Coder: NSCoding, + Metadata.Coder.ValueType == Metadata>(transaction: Database.Connection.ReadTransaction) -> YapDB.Index -> (ItemType, Metadata?)? { return { self.inTransaction(transaction, atIndex: $0) } } - func atIndexInTransaction(index: YapDB.Index) -> Database.Connection.ReadTransaction -> ItemType? { + func atIndexInTransaction< + Metadata where + Metadata: ValueCoding, + Metadata.Coder: NSCoding, + Metadata.Coder.ValueType == Metadata>(index: YapDB.Index) -> Database.Connection.ReadTransaction -> (ItemType, Metadata?)? { return { self.inTransaction($0, atIndex: index) } } func atIndexesInTransaction< - Indexes where + Indexes, Metadata where Indexes: SequenceType, - Indexes.Generator.Element == YapDB.Index>(indexes: Indexes) -> Database.Connection.ReadTransaction -> [ItemType] { - let atIndex = inTransactionAtIndex - return { indexes.flatMap(atIndex($0)) } + Indexes.Generator.Element == YapDB.Index, + Metadata: ValueCoding, + Metadata.Coder: NSCoding, + Metadata.Coder.ValueType == Metadata>(indexes: Indexes) -> Database.Connection.ReadTransaction -> [(ItemType, Metadata?)] { + return { indexes.flatMap(self.inTransactionAtIndex($0)) } } - func inTransaction(transaction: Database.Connection.ReadTransaction, byKey key: String) -> ItemType? { + func inTransaction< + Metadata where + Metadata: ValueCoding, + Metadata.Coder: NSCoding, + Metadata.Coder.ValueType == Metadata>(transaction: Database.Connection.ReadTransaction, byKey key: String) -> (ItemType, Metadata?)? { return inTransaction(transaction, atIndex: ItemType.indexWithKey(key)) } - func inTransactionByKey(transaction: Database.Connection.ReadTransaction) -> String -> ItemType? { + func inTransactionByKey< + Metadata where + Metadata: ValueCoding, + Metadata.Coder: NSCoding, + Metadata.Coder.ValueType == Metadata>(transaction: Database.Connection.ReadTransaction) -> String -> (ItemType, Metadata?)? { return { self.inTransaction(transaction, byKey: $0) } } - func byKeyInTransaction(key: String) -> Database.Connection.ReadTransaction -> ItemType? { + func byKeyInTransaction< + Metadata where + Metadata: ValueCoding, + Metadata.Coder: NSCoding, + Metadata.Coder.ValueType == Metadata>(key: String) -> Database.Connection.ReadTransaction -> (ItemType, Metadata?)? { return { self.inTransaction($0, byKey: key) } } - func byKeysInTransaction(keys: [String]? = .None) -> Database.Connection.ReadTransaction -> [ItemType] { - let byKey = inTransactionByKey + func byKeysInTransaction< + Metadata where + Metadata: ValueCoding, + Metadata.Coder: NSCoding, + Metadata.Coder.ValueType == Metadata>(keys: [String]? = .None) -> Database.Connection.ReadTransaction -> [(ItemType, Metadata?)] { return { transaction in let keys = keys ?? transaction.keysInCollection(ItemType.collection) - return keys.flatMap(byKey(transaction)) + return keys.flatMap(self.inTransactionByKey(transaction)) } } @@ -169,7 +233,11 @@ extension Readable where - parameter index: a YapDB.Index - returns: an optional `ItemType` */ - public func atIndex(index: YapDB.Index) -> ItemType? { + public func atIndex< + Metadata where + Metadata: ValueCoding, + Metadata.Coder: NSCoding, + Metadata.Coder.ValueType == Metadata>(index: YapDB.Index) -> (ItemType, Metadata?)? { return sync(atIndexInTransaction(index)) } @@ -180,9 +248,12 @@ extension Readable where - returns: an array of `ItemType` */ public func atIndexes< - Indexes where + Indexes, Metadata where Indexes: SequenceType, - Indexes.Generator.Element == YapDB.Index>(indexes: Indexes) -> [ItemType] { + Indexes.Generator.Element == YapDB.Index, + Metadata: ValueCoding, + Metadata.Coder: NSCoding, + Metadata.Coder.ValueType == Metadata>(indexes: Indexes) -> [(ItemType, Metadata?)] { return sync(atIndexesInTransaction(indexes)) } @@ -192,7 +263,11 @@ extension Readable where - parameter key: a String - returns: an optional `ItemType` */ - public func byKey(key: String) -> ItemType? { + public func byKey< + Metadata where + Metadata: ValueCoding, + Metadata.Coder: NSCoding, + Metadata.Coder.ValueType == Metadata>(key: String) -> (ItemType, Metadata?)? { return sync(byKeyInTransaction(key)) } @@ -203,9 +278,12 @@ extension Readable where - returns: an array of `ItemType` */ public func byKeys< - Keys where + Keys, Metadata where Keys: SequenceType, - Keys.Generator.Element == String>(keys: Keys) -> [ItemType] { + Keys.Generator.Element == String, + Metadata: ValueCoding, + Metadata.Coder: NSCoding, + Metadata.Coder.ValueType == Metadata>(keys: Keys) -> [(ItemType, Metadata?)] { return sync(byKeysInTransaction(Array(keys))) } @@ -214,7 +292,11 @@ extension Readable where - returns: an array of `ItemType` */ - public func all() -> [ItemType] { + public func all< + Metadata where + Metadata: ValueCoding, + Metadata.Coder: NSCoding, + Metadata.Coder.ValueType == Metadata>() -> [(ItemType, Metadata?)] { return sync(byKeysInTransaction()) } @@ -224,11 +306,15 @@ extension Readable where - 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 + public func filterExisting< + Metadata where + Metadata: ValueCoding, + Metadata.Coder: NSCoding, + Metadata.Coder.ValueType == Metadata>(keys: [String]) -> (existing: [(ItemType, Metadata?)], missing: [String]) { + let existingInTransaction: Database.Connection.ReadTransaction -> [(ItemType, Metadata?)] = byKeysInTransaction(keys) + return sync { transaction -> ([(ItemType, Metadata?)], [String]) in let existing = existingInTransaction(transaction) - let existingKeys = existing.map(keyForPersistable) + let existingKeys = existing.map {keyForPersistable($0.0)} let missingKeys = keys.filter { !existingKeys.contains($0) } return (existing, missingKeys) } From 10ec086907f9a2800ae7af10c366210fbfe6b232 Mon Sep 17 00:00:00 2001 From: Jim Roepcke Date: Fri, 9 Sep 2016 19:48:02 -0700 Subject: [PATCH 05/14] fix curried with metadata versions --- .../Curried_ObjectWithObjectMetadata.swift | 38 ++++++++------ .../Curried_ObjectWithValueMetadata.swift | 42 ++++++++++------ .../Curried_ValueWithObjectMetadata.swift | 38 ++++++++------ .../Curried_ValueWithValueMetadata.swift | 50 ++++++++++++------- 4 files changed, 104 insertions(+), 64 deletions(-) diff --git a/YapDatabaseExtensions/Shared/Curried/Curried_ObjectWithObjectMetadata.swift b/YapDatabaseExtensions/Shared/Curried/Curried_ObjectWithObjectMetadata.swift index d763a90..a26adec 100644 --- a/YapDatabaseExtensions/Shared/Curried/Curried_ObjectWithObjectMetadata.swift +++ b/YapDatabaseExtensions/Shared/Curried/Curried_ObjectWithObjectMetadata.swift @@ -12,8 +12,7 @@ import ValueCoding // MARK: - Persistable extension Persistable where - Self: NSCoding, - Self.MetadataType: NSCoding { + Self: NSCoding { /** Returns a closure which, given a read transaction will return @@ -23,9 +22,10 @@ extension Persistable where - returns: a (ReadTransaction) -> Self? closure. */ public static func readAtIndex< - ReadTransaction where - ReadTransaction: ReadTransactionType>(index: YapDB.Index) -> ReadTransaction -> Self? { - return { $0.readAtIndex(index) } + ReadTransaction, Metadata where + ReadTransaction: ReadTransactionType, + Metadata: NSCoding>(index: YapDB.Index) -> ReadTransaction -> (Self, Metadata?)? { + return { $0.readAtIndex(index) } } /** @@ -36,11 +36,12 @@ extension Persistable where - returns: a (ReadTransaction) -> [Self] closure. */ public static func readAtIndexes< - Indexes, ReadTransaction where + Indexes, ReadTransaction, Metadata where Indexes: SequenceType, Indexes.Generator.Element == YapDB.Index, - ReadTransaction: ReadTransactionType>(indexes: Indexes) -> ReadTransaction -> [Self] { - return { $0.readAtIndexes(indexes) } + ReadTransaction: ReadTransactionType, + Metadata: NSCoding>(indexes: Indexes) -> ReadTransaction -> [(Self, Metadata?)] { + return { $0.readAtIndexes(indexes) } } /** @@ -51,9 +52,10 @@ extension Persistable where - returns: a (ReadTransaction) -> Self? closure. */ public static func readByKey< - ReadTransaction where - ReadTransaction: ReadTransactionType>(key: String) -> ReadTransaction -> Self? { - return { $0.readByKey(key) } + ReadTransaction, Metadata where + ReadTransaction: ReadTransactionType, + Metadata: NSCoding>(key: String) -> ReadTransaction -> (Self, Metadata?)? { + return { $0.readByKey(key) } } /** @@ -64,11 +66,12 @@ extension Persistable where - returns: a (ReadTransaction) -> [Self] closure. */ public static func readByKeys< - Keys, ReadTransaction where + Keys, ReadTransaction, Metadata where Keys: SequenceType, Keys.Generator.Element == String, - ReadTransaction: ReadTransactionType>(keys: Keys) -> ReadTransaction -> [Self] { - return { $0.readAtIndexes(Self.indexesWithKeys(keys)) } + ReadTransaction: ReadTransactionType, + Metadata: NSCoding>(keys: Keys) -> ReadTransaction -> [(Self, Metadata?)] { + return { $0.readAtIndexes(Self.indexesWithKeys(keys)) } } /** @@ -78,7 +81,10 @@ extension Persistable where - warning: Be aware that this will capure `self`. - returns: a (WriteTransaction) -> Self closure */ - public func write() -> WriteTransaction -> Self { - return { $0.write(self) } + public func write< + WriteTransaction, Metadata where + WriteTransaction: WriteTransactionType, + Metadata: NSCoding>(metadata: Metadata? = nil) -> WriteTransaction -> (Self, Metadata?) { + return { $0.write((self, metadata)) } } } diff --git a/YapDatabaseExtensions/Shared/Curried/Curried_ObjectWithValueMetadata.swift b/YapDatabaseExtensions/Shared/Curried/Curried_ObjectWithValueMetadata.swift index 6690f2f..c6fc8c7 100644 --- a/YapDatabaseExtensions/Shared/Curried/Curried_ObjectWithValueMetadata.swift +++ b/YapDatabaseExtensions/Shared/Curried/Curried_ObjectWithValueMetadata.swift @@ -12,10 +12,7 @@ import ValueCoding // MARK: - Persistable extension Persistable where - Self: NSCoding, - Self.MetadataType: ValueCoding, - Self.MetadataType.Coder: NSCoding, - Self.MetadataType.Coder.ValueType == Self.MetadataType { + Self: NSCoding { /** Returns a closure which, given a read transaction will return @@ -25,8 +22,11 @@ extension Persistable where - returns: a (ReadTransaction) -> Self? closure. */ public static func readAtIndex< - ReadTransaction where - ReadTransaction: ReadTransactionType>(index: YapDB.Index) -> ReadTransaction -> Self? { + ReadTransaction, Metadata where + ReadTransaction: ReadTransactionType, + Metadata: ValueCoding, + Metadata.Coder: NSCoding, + Metadata.Coder.ValueType == Metadata>(index: YapDB.Index) -> ReadTransaction -> (Self, Metadata?)? { return { $0.readAtIndex(index) } } @@ -38,10 +38,13 @@ extension Persistable where - returns: a (ReadTransaction) -> [Self] closure. */ public static func readAtIndexes< - Indexes, ReadTransaction where + Indexes, ReadTransaction, Metadata where Indexes: SequenceType, Indexes.Generator.Element == YapDB.Index, - ReadTransaction: ReadTransactionType>(indexes: Indexes) -> ReadTransaction -> [Self] { + ReadTransaction: ReadTransactionType, + Metadata: ValueCoding, + Metadata.Coder: NSCoding, + Metadata.Coder.ValueType == Metadata>(indexes: Indexes) -> ReadTransaction -> [(Self, Metadata?)] { return { $0.readAtIndexes(indexes) } } @@ -53,8 +56,11 @@ extension Persistable where - returns: a (ReadTransaction) -> Self? closure. */ public static func readByKey< - ReadTransaction where - ReadTransaction: ReadTransactionType>(key: String) -> ReadTransaction -> Self? { + ReadTransaction, Metadata where + ReadTransaction: ReadTransactionType, + Metadata: ValueCoding, + Metadata.Coder: NSCoding, + Metadata.Coder.ValueType == Metadata>(key: String) -> ReadTransaction -> (Self, Metadata?)? { return { $0.readByKey(key) } } @@ -66,10 +72,13 @@ extension Persistable where - returns: a (ReadTransaction) -> [Self] closure. */ public static func readByKeys< - Keys, ReadTransaction where + Keys, ReadTransaction, Metadata where Keys: SequenceType, Keys.Generator.Element == String, - ReadTransaction: ReadTransactionType>(keys: Keys) -> ReadTransaction -> [Self] { + ReadTransaction: ReadTransactionType, + Metadata: ValueCoding, + Metadata.Coder: NSCoding, + Metadata.Coder.ValueType == Metadata>(keys: Keys) -> ReadTransaction -> [(Self, Metadata?)] { return { $0.readAtIndexes(Self.indexesWithKeys(keys)) } } @@ -80,7 +89,12 @@ extension Persistable where - warning: Be aware that this will capure `self`. - returns: a (WriteTransaction) -> Self closure */ - public func write() -> WriteTransaction -> Self { - return { $0.write(self) } + public func write< + WriteTransaction, Metadata where + WriteTransaction: WriteTransactionType, + Metadata: ValueCoding, + Metadata.Coder: NSCoding, + Metadata.Coder.ValueType == Metadata>(metadata: Metadata? = nil) -> WriteTransaction -> (Self, Metadata?) { + return { $0.write((self, metadata)) } } } diff --git a/YapDatabaseExtensions/Shared/Curried/Curried_ValueWithObjectMetadata.swift b/YapDatabaseExtensions/Shared/Curried/Curried_ValueWithObjectMetadata.swift index 6245a38..d2e925c 100644 --- a/YapDatabaseExtensions/Shared/Curried/Curried_ValueWithObjectMetadata.swift +++ b/YapDatabaseExtensions/Shared/Curried/Curried_ValueWithObjectMetadata.swift @@ -14,8 +14,7 @@ import ValueCoding extension Persistable where Self: ValueCoding, Self.Coder: NSCoding, - Self.Coder.ValueType == Self, - Self.MetadataType: NSCoding { + Self.Coder.ValueType == Self { /** Returns a closure which, given a read transaction will return @@ -25,9 +24,10 @@ extension Persistable where - returns: a (ReadTransaction) -> Self? closure. */ public static func readAtIndex< - ReadTransaction where - ReadTransaction: ReadTransactionType>(index: YapDB.Index) -> ReadTransaction -> Self? { - return { $0.readAtIndex(index) } + ReadTransaction, Metadata where + ReadTransaction: ReadTransactionType, + Metadata: NSCoding>(index: YapDB.Index) -> ReadTransaction -> (Self, Metadata?)? { + return { $0.readAtIndex(index) } } /** @@ -38,11 +38,12 @@ extension Persistable where - returns: a (ReadTransaction) -> [Self] closure. */ public static func readAtIndexes< - Indexes, ReadTransaction where + Indexes, ReadTransaction, Metadata where Indexes: SequenceType, Indexes.Generator.Element == YapDB.Index, - ReadTransaction: ReadTransactionType>(indexes: Indexes) -> ReadTransaction -> [Self] { - return { $0.readAtIndexes(indexes) } + ReadTransaction: ReadTransactionType, + Metadata: NSCoding>(indexes: Indexes) -> ReadTransaction -> [(Self, Metadata?)] { + return { $0.readAtIndexes(indexes) } } /** @@ -53,9 +54,10 @@ extension Persistable where - returns: a (ReadTransaction) -> Self? closure. */ public static func readByKey< - ReadTransaction where - ReadTransaction: ReadTransactionType>(key: String) -> ReadTransaction -> Self? { - return { $0.readByKey(key) } + ReadTransaction, Metadata where + ReadTransaction: ReadTransactionType, + Metadata: NSCoding>(key: String) -> ReadTransaction -> (Self, Metadata?)? { + return { $0.readByKey(key) } } /** @@ -66,11 +68,12 @@ extension Persistable where - returns: a (ReadTransaction) -> [Self] closure. */ public static func readByKeys< - Keys, ReadTransaction where + Keys, ReadTransaction, Metadata where Keys: SequenceType, Keys.Generator.Element == String, - ReadTransaction: ReadTransactionType>(keys: Keys) -> ReadTransaction -> [Self] { - return { $0.readAtIndexes(Self.indexesWithKeys(keys)) } + ReadTransaction: ReadTransactionType, + Metadata: NSCoding>(keys: Keys) -> ReadTransaction -> [(Self, Metadata?)] { + return { $0.readAtIndexes(Self.indexesWithKeys(keys)) } } /** @@ -80,7 +83,10 @@ extension Persistable where - warning: Be aware that this will capure `self`. - returns: a (WriteTransaction) -> Self closure */ - public func write() -> WriteTransaction -> Self { - return { $0.write(self) } + public func write< + WriteTransaction, Metadata where + WriteTransaction: WriteTransactionType, + Metadata: NSCoding>(metadata: Metadata? = nil) -> WriteTransaction -> (Self, Metadata?) { + return { $0.write((self, metadata)) } } } diff --git a/YapDatabaseExtensions/Shared/Curried/Curried_ValueWithValueMetadata.swift b/YapDatabaseExtensions/Shared/Curried/Curried_ValueWithValueMetadata.swift index c59f580..0e0b267 100644 --- a/YapDatabaseExtensions/Shared/Curried/Curried_ValueWithValueMetadata.swift +++ b/YapDatabaseExtensions/Shared/Curried/Curried_ValueWithValueMetadata.swift @@ -14,10 +14,7 @@ import ValueCoding extension Persistable where Self: ValueCoding, Self.Coder: NSCoding, - Self.Coder.ValueType == Self, - Self.MetadataType: ValueCoding, - Self.MetadataType.Coder: NSCoding, - Self.MetadataType.Coder.ValueType == Self.MetadataType { + Self.Coder.ValueType == Self { /** Returns a closure which, given a read transaction will return @@ -27,9 +24,12 @@ extension Persistable where - returns: a (ReadTransaction) -> Self? closure. */ public static func readAtIndex< - ReadTransaction where - ReadTransaction: ReadTransactionType>(index: YapDB.Index) -> ReadTransaction -> Self? { - return { $0.readAtIndex(index) } + ReadTransaction, Metadata where + ReadTransaction: ReadTransactionType, + Metadata: ValueCoding, + Metadata.Coder: NSCoding, + Metadata.Coder.ValueType == Metadata>(index: YapDB.Index) -> ReadTransaction -> (Self, Metadata?)? { + return { $0.readAtIndex(index) } } /** @@ -40,11 +40,14 @@ extension Persistable where - returns: a (ReadTransaction) -> [Self] closure. */ public static func readAtIndexes< - Indexes, ReadTransaction where + Indexes, ReadTransaction, Metadata where Indexes: SequenceType, Indexes.Generator.Element == YapDB.Index, - ReadTransaction: ReadTransactionType>(indexes: Indexes) -> ReadTransaction -> [Self] { - return { $0.readAtIndexes(indexes) } + ReadTransaction: ReadTransactionType, + Metadata: ValueCoding, + Metadata.Coder: NSCoding, + Metadata.Coder.ValueType == Metadata>(indexes: Indexes) -> ReadTransaction -> [(Self, Metadata?)] { + return { $0.readAtIndexes(indexes) } } /** @@ -55,9 +58,12 @@ extension Persistable where - returns: a (ReadTransaction) -> Self? closure. */ public static func readByKey< - ReadTransaction where - ReadTransaction: ReadTransactionType>(key: String) -> ReadTransaction -> Self? { - return { $0.readByKey(key) } + ReadTransaction, Metadata where + ReadTransaction: ReadTransactionType, + Metadata: ValueCoding, + Metadata.Coder: NSCoding, + Metadata.Coder.ValueType == Metadata>(key: String) -> ReadTransaction -> (Self, Metadata?)? { + return { $0.readByKey(key) } } /** @@ -68,11 +74,14 @@ extension Persistable where - returns: a (ReadTransaction) -> [Self] closure. */ public static func readByKeys< - Keys, ReadTransaction where + Keys, ReadTransaction, Metadata where Keys: SequenceType, Keys.Generator.Element == String, - ReadTransaction: ReadTransactionType>(keys: Keys) -> ReadTransaction -> [Self] { - return { $0.readAtIndexes(Self.indexesWithKeys(keys)) } + ReadTransaction: ReadTransactionType, + Metadata: ValueCoding, + Metadata.Coder: NSCoding, + Metadata.Coder.ValueType == Metadata>(keys: Keys) -> ReadTransaction -> [(Self, Metadata?)] { + return { $0.readAtIndexes(Self.indexesWithKeys(keys)) } } /** @@ -82,7 +91,12 @@ extension Persistable where - warning: Be aware that this will capure `self`. - returns: a (WriteTransaction) -> Self closure */ - public func write() -> WriteTransaction -> Self { - return { $0.write(self) } + public func write< + WriteTransaction, Metadata where + WriteTransaction: WriteTransactionType, + Metadata: ValueCoding, + Metadata.Coder: NSCoding, + Metadata.Coder.ValueType == Metadata>(metadata: Metadata? = nil) -> WriteTransaction -> (Self, Metadata?) { + return { $0.write((self, metadata)) } } } From 7fb0d329f3b72f56f0cb6d029dd0b13fa83bc986 Mon Sep 17 00:00:00 2001 From: Jim Roepcke Date: Fri, 9 Sep 2016 19:48:28 -0700 Subject: [PATCH 06/14] give metadata parameters default values in write methods --- .../Persistable_ObjectWithObjectMetadata.swift | 16 ++++++++-------- .../Persistable_ObjectWithValueMetadata.swift | 16 ++++++++-------- .../Persistable_ValueWithObjectMetadata.swift | 16 ++++++++-------- .../Persistable_ValueWithValueMetadata.swift | 16 ++++++++-------- 4 files changed, 32 insertions(+), 32 deletions(-) diff --git a/YapDatabaseExtensions/Shared/Persistable/Persistable_ObjectWithObjectMetadata.swift b/YapDatabaseExtensions/Shared/Persistable/Persistable_ObjectWithObjectMetadata.swift index 49f9770..294be2b 100644 --- a/YapDatabaseExtensions/Shared/Persistable/Persistable_ObjectWithObjectMetadata.swift +++ b/YapDatabaseExtensions/Shared/Persistable/Persistable_ObjectWithObjectMetadata.swift @@ -24,7 +24,7 @@ extension Persistable where public func write< WriteTransaction, Metadata where WriteTransaction: WriteTransactionType, - Metadata: NSCoding>(transaction: WriteTransaction, metadata: Metadata?) -> (Self, Metadata?) { + Metadata: NSCoding>(transaction: WriteTransaction, metadata: Metadata? = nil) -> (Self, Metadata?) { return transaction.write((self, metadata)) } @@ -37,7 +37,7 @@ extension Persistable where public func write< Connection, Metadata where Connection: ConnectionType, - Metadata: NSCoding>(connection: Connection, metadata: Metadata?) -> (Self, Metadata?) { + Metadata: NSCoding>(connection: Connection, metadata: Metadata? = nil) -> (Self, Metadata?) { return connection.write((self, metadata)) } @@ -50,7 +50,7 @@ extension Persistable where public func asyncWrite< Connection, Metadata where Connection: ConnectionType, - Metadata: NSCoding>(connection: Connection, metadata: Metadata?, queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ((Self, Metadata?) -> Void)? = .None) { + Metadata: NSCoding>(connection: Connection, metadata: Metadata? = nil, queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ((Self, Metadata?) -> Void)? = .None) { return connection.asyncWrite((self, metadata), queue: queue, completion: completion) } @@ -63,7 +63,7 @@ extension Persistable where public func writeOperation< Connection, Metadata where Connection: ConnectionType, - Metadata: NSCoding>(connection: Connection, metadata: Metadata?) -> NSOperation { + Metadata: NSCoding>(connection: Connection, metadata: Metadata? = nil) -> NSOperation { return NSBlockOperation { connection.write((self, metadata)) } } } @@ -81,7 +81,7 @@ extension SequenceType where public func write< WriteTransaction, Metadata where WriteTransaction: WriteTransactionType, - Metadata: NSCoding>(transaction: WriteTransaction, metadata: [Metadata?]) -> [(Generator.Element, Metadata?)] { + Metadata: NSCoding>(transaction: WriteTransaction, metadata: [Metadata?] = []) -> [(Generator.Element, Metadata?)] { let items = enumerate().map({ (index, element) in (element, metadata[index]) }) return transaction.write(items) } @@ -95,7 +95,7 @@ extension SequenceType where public func write< Connection, Metadata where Connection: ConnectionType, - Metadata: NSCoding>(connection: Connection, metadata: [Metadata?]) -> [(Generator.Element, Metadata?)] { + Metadata: NSCoding>(connection: Connection, metadata: [Metadata?] = []) -> [(Generator.Element, Metadata?)] { let items = enumerate().map({ (index, element) in (element, metadata[index]) }) return connection.write(items) } @@ -109,7 +109,7 @@ extension SequenceType where public func asyncWrite< Connection, Metadata where Connection: ConnectionType, - Metadata: NSCoding>(connection: Connection, metadata: [Metadata?], queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ([(Generator.Element, Metadata?)] -> Void)? = .None) { + Metadata: NSCoding>(connection: Connection, metadata: [Metadata?] = [], queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ([(Generator.Element, Metadata?)] -> Void)? = .None) { let items = enumerate().map({ (index, element) in (element, metadata[index]) }) return connection.asyncWrite(items, queue: queue, completion: completion) } @@ -123,7 +123,7 @@ extension SequenceType where public func writeOperation< Connection, Metadata where Connection: ConnectionType, - Metadata: NSCoding>(connection: Connection, metadata: [Metadata?]) -> NSOperation { + Metadata: NSCoding>(connection: Connection, metadata: [Metadata?] = []) -> NSOperation { let items = enumerate().map({ (index, element) in (element, metadata[index]) }) return NSBlockOperation { connection.write(items) } } diff --git a/YapDatabaseExtensions/Shared/Persistable/Persistable_ObjectWithValueMetadata.swift b/YapDatabaseExtensions/Shared/Persistable/Persistable_ObjectWithValueMetadata.swift index aa45d61..6270433 100644 --- a/YapDatabaseExtensions/Shared/Persistable/Persistable_ObjectWithValueMetadata.swift +++ b/YapDatabaseExtensions/Shared/Persistable/Persistable_ObjectWithValueMetadata.swift @@ -27,7 +27,7 @@ extension Persistable where WriteTransaction: WriteTransactionType, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(transaction: WriteTransaction, metadata: Metadata?) -> (Self, Metadata?) { + Metadata.Coder.ValueType == Metadata>(transaction: WriteTransaction, metadata: Metadata? = nil) -> (Self, Metadata?) { return transaction.write((self, metadata)) } @@ -42,7 +42,7 @@ extension Persistable where Connection: ConnectionType, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(connection: Connection, metadata: Metadata?) -> (Self, Metadata?) { + Metadata.Coder.ValueType == Metadata>(connection: Connection, metadata: Metadata? = nil) -> (Self, Metadata?) { return connection.write((self, metadata)) } @@ -57,7 +57,7 @@ extension Persistable where Connection: ConnectionType, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(connection: Connection, metadata: Metadata?, queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ((Self, Metadata?) -> Void)? = .None) { + Metadata.Coder.ValueType == Metadata>(connection: Connection, metadata: Metadata? = nil, queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ((Self, Metadata?) -> Void)? = .None) { return connection.asyncWrite((self, metadata), queue: queue, completion: completion) } @@ -72,7 +72,7 @@ extension Persistable where Connection: ConnectionType, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(connection: Connection, metadata: Metadata?) -> NSOperation { + Metadata.Coder.ValueType == Metadata>(connection: Connection, metadata: Metadata? = nil) -> NSOperation { return NSBlockOperation { connection.write((self, metadata)) } } } @@ -92,7 +92,7 @@ extension SequenceType where WriteTransaction: WriteTransactionType, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(transaction: WriteTransaction, metadata: [Metadata?]) -> [(Generator.Element, Metadata?)] { + Metadata.Coder.ValueType == Metadata>(transaction: WriteTransaction, metadata: [Metadata?] = []) -> [(Generator.Element, Metadata?)] { let items = enumerate().map({ (index, element) in (element, metadata[index]) }) return transaction.write(items) } @@ -108,7 +108,7 @@ extension SequenceType where Connection: ConnectionType, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(connection: Connection, metadata: [Metadata?]) -> [(Generator.Element, Metadata?)] { + Metadata.Coder.ValueType == Metadata>(connection: Connection, metadata: [Metadata?] = []) -> [(Generator.Element, Metadata?)] { let items = enumerate().map({ (index, element) in (element, metadata[index]) }) return connection.write(items) } @@ -124,7 +124,7 @@ extension SequenceType where Connection: ConnectionType, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(connection: Connection, metadata: [Metadata?], queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ([(Generator.Element, Metadata?)] -> Void)? = .None) { + Metadata.Coder.ValueType == Metadata>(connection: Connection, metadata: [Metadata?] = [], queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ([(Generator.Element, Metadata?)] -> Void)? = .None) { let items = enumerate().map({ (index, element) in (element, metadata[index]) }) return connection.asyncWrite(items, queue: queue, completion: completion) } @@ -140,7 +140,7 @@ extension SequenceType where Connection: ConnectionType, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(connection: Connection, metadata: [Metadata?]) -> NSOperation { + Metadata.Coder.ValueType == Metadata>(connection: Connection, metadata: [Metadata?] = []) -> NSOperation { let items = enumerate().map({ (index, element) in (element, metadata[index]) }) return NSBlockOperation { connection.write(items) } } diff --git a/YapDatabaseExtensions/Shared/Persistable/Persistable_ValueWithObjectMetadata.swift b/YapDatabaseExtensions/Shared/Persistable/Persistable_ValueWithObjectMetadata.swift index 5e2e458..fb398eb 100644 --- a/YapDatabaseExtensions/Shared/Persistable/Persistable_ValueWithObjectMetadata.swift +++ b/YapDatabaseExtensions/Shared/Persistable/Persistable_ValueWithObjectMetadata.swift @@ -27,7 +27,7 @@ extension Persistable where public func write< WriteTransaction, Metadata where WriteTransaction: WriteTransactionType, - Metadata: NSCoding>(transaction: WriteTransaction, metadata: Metadata?) -> (Self, Metadata?) { + Metadata: NSCoding>(transaction: WriteTransaction, metadata: Metadata? = nil) -> (Self, Metadata?) { return transaction.write((self, metadata)) } @@ -40,7 +40,7 @@ extension Persistable where public func write< Connection, Metadata where Connection: ConnectionType, - Metadata: NSCoding>(connection: Connection, metadata: Metadata?) -> (Self, Metadata?) { + Metadata: NSCoding>(connection: Connection, metadata: Metadata? = nil) -> (Self, Metadata?) { return connection.write((self, metadata)) } @@ -53,7 +53,7 @@ extension Persistable where public func asyncWrite< Connection, Metadata where Connection: ConnectionType, - Metadata: NSCoding>(connection: Connection, metadata: Metadata?, queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ((Self, Metadata?) -> Void)? = .None) { + Metadata: NSCoding>(connection: Connection, metadata: Metadata? = nil, queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ((Self, Metadata?) -> Void)? = .None) { return connection.asyncWrite((self, metadata), queue: queue, completion: completion) } @@ -66,7 +66,7 @@ extension Persistable where public func writeOperation< Connection, Metadata where Connection: ConnectionType, - Metadata: NSCoding>(connection: Connection, metadata: Metadata?) -> NSOperation { + Metadata: NSCoding>(connection: Connection, metadata: Metadata? = nil) -> NSOperation { return NSBlockOperation { connection.write((self, metadata)) } } } @@ -86,7 +86,7 @@ extension SequenceType where public func write< WriteTransaction, Metadata where WriteTransaction: WriteTransactionType, - Metadata: NSCoding>(transaction: WriteTransaction, metadata: [Metadata?]) -> [(Generator.Element, Metadata?)] { + Metadata: NSCoding>(transaction: WriteTransaction, metadata: [Metadata?] = []) -> [(Generator.Element, Metadata?)] { let items = enumerate().map({ (index, element) in (element, metadata[index]) }) return transaction.write(items) } @@ -100,7 +100,7 @@ extension SequenceType where public func write< Connection, Metadata where Connection: ConnectionType, - Metadata: NSCoding>(connection: Connection, metadata: [Metadata?]) -> [(Generator.Element, Metadata?)] { + Metadata: NSCoding>(connection: Connection, metadata: [Metadata?] = []) -> [(Generator.Element, Metadata?)] { let items = enumerate().map({ (index, element) in (element, metadata[index]) }) return connection.write(items) } @@ -114,7 +114,7 @@ extension SequenceType where public func asyncWrite< Connection, Metadata where Connection: ConnectionType, - Metadata: NSCoding>(connection: Connection, metadata: [Metadata?], queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ([(Generator.Element, Metadata?)] -> Void)? = .None) { + Metadata: NSCoding>(connection: Connection, metadata: [Metadata?] = [], queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ([(Generator.Element, Metadata?)] -> Void)? = .None) { let items = enumerate().map({ (index, element) in (element, metadata[index]) }) return connection.asyncWrite(items, queue: queue, completion: completion) } @@ -128,7 +128,7 @@ extension SequenceType where public func writeOperation< Connection, Metadata where Connection: ConnectionType, - Metadata: NSCoding>(connection: Connection, metadata: [Metadata?]) -> NSOperation { + Metadata: NSCoding>(connection: Connection, metadata: [Metadata?] = []) -> NSOperation { let items = enumerate().map({ (index, element) in (element, metadata[index]) }) return NSBlockOperation { connection.write(items) } } diff --git a/YapDatabaseExtensions/Shared/Persistable/Persistable_ValueWithValueMetadata.swift b/YapDatabaseExtensions/Shared/Persistable/Persistable_ValueWithValueMetadata.swift index 69582c5..689bd3c 100644 --- a/YapDatabaseExtensions/Shared/Persistable/Persistable_ValueWithValueMetadata.swift +++ b/YapDatabaseExtensions/Shared/Persistable/Persistable_ValueWithValueMetadata.swift @@ -29,7 +29,7 @@ extension Persistable where WriteTransaction: WriteTransactionType, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(transaction: WriteTransaction, metadata: Metadata?) -> (Self, Metadata?) { + Metadata.Coder.ValueType == Metadata>(transaction: WriteTransaction, metadata: Metadata? = nil) -> (Self, Metadata?) { return transaction.write((self, metadata)) } @@ -44,7 +44,7 @@ extension Persistable where Connection: ConnectionType, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(connection: Connection, metadata: Metadata?) -> (Self, Metadata?) { + Metadata.Coder.ValueType == Metadata>(connection: Connection, metadata: Metadata? = nil) -> (Self, Metadata?) { return connection.write((self, metadata)) } @@ -59,7 +59,7 @@ extension Persistable where Connection: ConnectionType, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(connection: Connection, metadata: Metadata?, queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ((Self, Metadata?) -> Void)? = .None) { + Metadata.Coder.ValueType == Metadata>(connection: Connection, metadata: Metadata? = nil, queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ((Self, Metadata?) -> Void)? = .None) { return connection.asyncWrite((self, metadata), queue: queue, completion: completion) } @@ -74,7 +74,7 @@ extension Persistable where Connection: ConnectionType, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(connection: Connection, metadata: Metadata?) -> NSOperation { + Metadata.Coder.ValueType == Metadata>(connection: Connection, metadata: Metadata? = nil) -> NSOperation { return NSBlockOperation { connection.write((self, metadata)) } } } @@ -96,7 +96,7 @@ extension SequenceType where WriteTransaction: WriteTransactionType, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(transaction: WriteTransaction, metadata: [Metadata?]) -> [(Generator.Element, Metadata?)] { + Metadata.Coder.ValueType == Metadata>(transaction: WriteTransaction, metadata: [Metadata?] = []) -> [(Generator.Element, Metadata?)] { let items = enumerate().map({ (index, element) in (element, metadata[index]) }) return transaction.write(items) } @@ -112,7 +112,7 @@ extension SequenceType where Connection: ConnectionType, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(connection: Connection, metadata: [Metadata?]) -> [(Generator.Element, Metadata?)] { + Metadata.Coder.ValueType == Metadata>(connection: Connection, metadata: [Metadata?] = []) -> [(Generator.Element, Metadata?)] { let items = enumerate().map({ (index, element) in (element, metadata[index]) }) return connection.write(items) } @@ -128,7 +128,7 @@ extension SequenceType where Connection: ConnectionType, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(connection: Connection, metadata: [Metadata?], queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ([(Generator.Element, Metadata?)] -> Void)? = .None) { + Metadata.Coder.ValueType == Metadata>(connection: Connection, metadata: [Metadata?] = [], queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ([(Generator.Element, Metadata?)] -> Void)? = .None) { let items = enumerate().map({ (index, element) in (element, metadata[index]) }) return connection.asyncWrite(items, queue: queue, completion: completion) } @@ -144,7 +144,7 @@ extension SequenceType where Connection: ConnectionType, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(connection: Connection, metadata: [Metadata?]) -> NSOperation { + Metadata.Coder.ValueType == Metadata>(connection: Connection, metadata: [Metadata?] = []) -> NSOperation { let items = enumerate().map({ (index, element) in (element, metadata[index]) }) return NSBlockOperation { connection.write(items) } } From cf71843ee891a9c8cf72030c4e736050a7962cce Mon Sep 17 00:00:00 2001 From: Jim Roepcke Date: Fri, 9 Sep 2016 19:49:36 -0700 Subject: [PATCH 07/14] remove commented metadata portions of Persistable protocol --- .../Shared/YapDatabaseExtensions.swift | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/YapDatabaseExtensions/Shared/YapDatabaseExtensions.swift b/YapDatabaseExtensions/Shared/YapDatabaseExtensions.swift index 20b84cf..e7ea5c9 100644 --- a/YapDatabaseExtensions/Shared/YapDatabaseExtensions.swift +++ b/YapDatabaseExtensions/Shared/YapDatabaseExtensions.swift @@ -181,15 +181,9 @@ defined in this framework. It assumes that all instances of a type are stored in the same YapDatabase collection. */ public protocol Persistable: Identifiable { -// -// /// The nested type of the metadata. Defaults to Void. -// associatedtype MetadataType /// The YapDatabase collection name the type is stored in. static var collection: String { get } -// -// /// A metadata which is set when reading, and get when writing. -// var metadata: MetadataType? { get set } } extension Persistable { @@ -222,15 +216,6 @@ extension Persistable { return Set(keys).map { YapDB.Index(collection: collection, key: $0) } } - /** - Default metadata property. Implement this to re-define your - own MetadataType. - */ - public var metadata: Void? { - get { return .None } - set { } - } - /** Convenience computed property to get the key for a persistable, which is just the identifier's description. From 2987602feac3a0d6c7611fe52ec8f898471f1250 Mon Sep 17 00:00:00 2001 From: Jim Roepcke Date: Fri, 9 Sep 2016 21:07:21 -0700 Subject: [PATCH 08/14] update comments --- .../Persistable/Persistable_AnyWithObjectMetadata.swift | 4 ++-- .../Shared/Persistable/Persistable_AnyWithValueMetadata.swift | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/YapDatabaseExtensions/Shared/Persistable/Persistable_AnyWithObjectMetadata.swift b/YapDatabaseExtensions/Shared/Persistable/Persistable_AnyWithObjectMetadata.swift index 6208e01..fbe08ed 100644 --- a/YapDatabaseExtensions/Shared/Persistable/Persistable_AnyWithObjectMetadata.swift +++ b/YapDatabaseExtensions/Shared/Persistable/Persistable_AnyWithObjectMetadata.swift @@ -38,7 +38,7 @@ extension Readable where Reads the metadata at a given index. - parameter index: a YapDB.Index - - returns: an optional `ItemType.MetadataType` + - returns: an optional `Metadata` */ public func metadataAtIndex< Metadata where @@ -50,7 +50,7 @@ extension Readable where Reads the metadata at the indexes. - parameter indexes: a SequenceType of YapDB.Index values - - returns: an array of `ItemType.MetadataType` + - returns: an array of `Metadata` */ public func metadataAtIndexes< Indexes, Metadata where diff --git a/YapDatabaseExtensions/Shared/Persistable/Persistable_AnyWithValueMetadata.swift b/YapDatabaseExtensions/Shared/Persistable/Persistable_AnyWithValueMetadata.swift index 1011383..9147903 100644 --- a/YapDatabaseExtensions/Shared/Persistable/Persistable_AnyWithValueMetadata.swift +++ b/YapDatabaseExtensions/Shared/Persistable/Persistable_AnyWithValueMetadata.swift @@ -52,7 +52,7 @@ extension Readable where Reads the metadata at a given index. - parameter index: a YapDB.Index - - returns: an optional `ItemType.MetadataType` + - returns: an optional `Metadata` */ public func metadataAtIndex< Metadata where @@ -66,7 +66,7 @@ extension Readable where Reads the metadata at the indexes. - parameter indexes: a SequenceType of YapDB.Index values - - returns: an array of `ItemType.MetadataType` + - returns: an array of `Metadata` */ public func metadataAtIndexes< Indexes, Metadata where From 8c7c4c7fa188b5ae91cdea67a700d0fc80a65657 Mon Sep 17 00:00:00 2001 From: Jim Roepcke Date: Fri, 9 Sep 2016 21:57:57 -0700 Subject: [PATCH 09/14] add zipToWrite function that creates a list of tuples - hopefully this can be removed after a refactor later --- .../Persistable_ObjectWithObjectMetadata.swift | 8 ++++---- .../Persistable_ObjectWithValueMetadata.swift | 8 ++++---- .../Persistable_ValueWithObjectMetadata.swift | 8 ++++---- .../Persistable_ValueWithValueMetadata.swift | 8 ++++---- .../Shared/YapDatabaseExtensions.swift | 10 ++++++++++ 5 files changed, 26 insertions(+), 16 deletions(-) diff --git a/YapDatabaseExtensions/Shared/Persistable/Persistable_ObjectWithObjectMetadata.swift b/YapDatabaseExtensions/Shared/Persistable/Persistable_ObjectWithObjectMetadata.swift index 294be2b..a6c7180 100644 --- a/YapDatabaseExtensions/Shared/Persistable/Persistable_ObjectWithObjectMetadata.swift +++ b/YapDatabaseExtensions/Shared/Persistable/Persistable_ObjectWithObjectMetadata.swift @@ -82,7 +82,7 @@ extension SequenceType where WriteTransaction, Metadata where WriteTransaction: WriteTransactionType, Metadata: NSCoding>(transaction: WriteTransaction, metadata: [Metadata?] = []) -> [(Generator.Element, Metadata?)] { - let items = enumerate().map({ (index, element) in (element, metadata[index]) }) + let items = zipToWrite(self, metadata) return transaction.write(items) } @@ -96,7 +96,7 @@ extension SequenceType where Connection, Metadata where Connection: ConnectionType, Metadata: NSCoding>(connection: Connection, metadata: [Metadata?] = []) -> [(Generator.Element, Metadata?)] { - let items = enumerate().map({ (index, element) in (element, metadata[index]) }) + let items = zipToWrite(self, metadata) return connection.write(items) } @@ -110,7 +110,7 @@ extension SequenceType where Connection, Metadata where Connection: ConnectionType, Metadata: NSCoding>(connection: Connection, metadata: [Metadata?] = [], queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ([(Generator.Element, Metadata?)] -> Void)? = .None) { - let items = enumerate().map({ (index, element) in (element, metadata[index]) }) + let items = zipToWrite(self, metadata) return connection.asyncWrite(items, queue: queue, completion: completion) } @@ -124,7 +124,7 @@ extension SequenceType where Connection, Metadata where Connection: ConnectionType, Metadata: NSCoding>(connection: Connection, metadata: [Metadata?] = []) -> NSOperation { - let items = enumerate().map({ (index, element) in (element, metadata[index]) }) + let items = zipToWrite(self, metadata) return NSBlockOperation { connection.write(items) } } } diff --git a/YapDatabaseExtensions/Shared/Persistable/Persistable_ObjectWithValueMetadata.swift b/YapDatabaseExtensions/Shared/Persistable/Persistable_ObjectWithValueMetadata.swift index 6270433..7642ba8 100644 --- a/YapDatabaseExtensions/Shared/Persistable/Persistable_ObjectWithValueMetadata.swift +++ b/YapDatabaseExtensions/Shared/Persistable/Persistable_ObjectWithValueMetadata.swift @@ -93,7 +93,7 @@ extension SequenceType where Metadata: ValueCoding, Metadata.Coder: NSCoding, Metadata.Coder.ValueType == Metadata>(transaction: WriteTransaction, metadata: [Metadata?] = []) -> [(Generator.Element, Metadata?)] { - let items = enumerate().map({ (index, element) in (element, metadata[index]) }) + let items = zipToWrite(self, metadata) return transaction.write(items) } @@ -109,7 +109,7 @@ extension SequenceType where Metadata: ValueCoding, Metadata.Coder: NSCoding, Metadata.Coder.ValueType == Metadata>(connection: Connection, metadata: [Metadata?] = []) -> [(Generator.Element, Metadata?)] { - let items = enumerate().map({ (index, element) in (element, metadata[index]) }) + let items = zipToWrite(self, metadata) return connection.write(items) } @@ -125,7 +125,7 @@ extension SequenceType where Metadata: ValueCoding, Metadata.Coder: NSCoding, Metadata.Coder.ValueType == Metadata>(connection: Connection, metadata: [Metadata?] = [], queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ([(Generator.Element, Metadata?)] -> Void)? = .None) { - let items = enumerate().map({ (index, element) in (element, metadata[index]) }) + let items = zipToWrite(self, metadata) return connection.asyncWrite(items, queue: queue, completion: completion) } @@ -141,7 +141,7 @@ extension SequenceType where Metadata: ValueCoding, Metadata.Coder: NSCoding, Metadata.Coder.ValueType == Metadata>(connection: Connection, metadata: [Metadata?] = []) -> NSOperation { - let items = enumerate().map({ (index, element) in (element, metadata[index]) }) + let items = zipToWrite(self, metadata) return NSBlockOperation { connection.write(items) } } } diff --git a/YapDatabaseExtensions/Shared/Persistable/Persistable_ValueWithObjectMetadata.swift b/YapDatabaseExtensions/Shared/Persistable/Persistable_ValueWithObjectMetadata.swift index fb398eb..9d2703c 100644 --- a/YapDatabaseExtensions/Shared/Persistable/Persistable_ValueWithObjectMetadata.swift +++ b/YapDatabaseExtensions/Shared/Persistable/Persistable_ValueWithObjectMetadata.swift @@ -87,7 +87,7 @@ extension SequenceType where WriteTransaction, Metadata where WriteTransaction: WriteTransactionType, Metadata: NSCoding>(transaction: WriteTransaction, metadata: [Metadata?] = []) -> [(Generator.Element, Metadata?)] { - let items = enumerate().map({ (index, element) in (element, metadata[index]) }) + let items = zipToWrite(self, metadata) return transaction.write(items) } @@ -101,7 +101,7 @@ extension SequenceType where Connection, Metadata where Connection: ConnectionType, Metadata: NSCoding>(connection: Connection, metadata: [Metadata?] = []) -> [(Generator.Element, Metadata?)] { - let items = enumerate().map({ (index, element) in (element, metadata[index]) }) + let items = zipToWrite(self, metadata) return connection.write(items) } @@ -115,7 +115,7 @@ extension SequenceType where Connection, Metadata where Connection: ConnectionType, Metadata: NSCoding>(connection: Connection, metadata: [Metadata?] = [], queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ([(Generator.Element, Metadata?)] -> Void)? = .None) { - let items = enumerate().map({ (index, element) in (element, metadata[index]) }) + let items = zipToWrite(self, metadata) return connection.asyncWrite(items, queue: queue, completion: completion) } @@ -129,7 +129,7 @@ extension SequenceType where Connection, Metadata where Connection: ConnectionType, Metadata: NSCoding>(connection: Connection, metadata: [Metadata?] = []) -> NSOperation { - let items = enumerate().map({ (index, element) in (element, metadata[index]) }) + let items = zipToWrite(self, metadata) return NSBlockOperation { connection.write(items) } } } diff --git a/YapDatabaseExtensions/Shared/Persistable/Persistable_ValueWithValueMetadata.swift b/YapDatabaseExtensions/Shared/Persistable/Persistable_ValueWithValueMetadata.swift index 689bd3c..db778c1 100644 --- a/YapDatabaseExtensions/Shared/Persistable/Persistable_ValueWithValueMetadata.swift +++ b/YapDatabaseExtensions/Shared/Persistable/Persistable_ValueWithValueMetadata.swift @@ -97,7 +97,7 @@ extension SequenceType where Metadata: ValueCoding, Metadata.Coder: NSCoding, Metadata.Coder.ValueType == Metadata>(transaction: WriteTransaction, metadata: [Metadata?] = []) -> [(Generator.Element, Metadata?)] { - let items = enumerate().map({ (index, element) in (element, metadata[index]) }) + let items = zipToWrite(self, metadata) return transaction.write(items) } @@ -113,7 +113,7 @@ extension SequenceType where Metadata: ValueCoding, Metadata.Coder: NSCoding, Metadata.Coder.ValueType == Metadata>(connection: Connection, metadata: [Metadata?] = []) -> [(Generator.Element, Metadata?)] { - let items = enumerate().map({ (index, element) in (element, metadata[index]) }) + let items = zipToWrite(self, metadata) return connection.write(items) } @@ -129,7 +129,7 @@ extension SequenceType where Metadata: ValueCoding, Metadata.Coder: NSCoding, Metadata.Coder.ValueType == Metadata>(connection: Connection, metadata: [Metadata?] = [], queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ([(Generator.Element, Metadata?)] -> Void)? = .None) { - let items = enumerate().map({ (index, element) in (element, metadata[index]) }) + let items = zipToWrite(self, metadata) return connection.asyncWrite(items, queue: queue, completion: completion) } @@ -145,7 +145,7 @@ extension SequenceType where Metadata: ValueCoding, Metadata.Coder: NSCoding, Metadata.Coder.ValueType == Metadata>(connection: Connection, metadata: [Metadata?] = []) -> NSOperation { - let items = enumerate().map({ (index, element) in (element, metadata[index]) }) + let items = zipToWrite(self, metadata) return NSBlockOperation { connection.write(items) } } } diff --git a/YapDatabaseExtensions/Shared/YapDatabaseExtensions.swift b/YapDatabaseExtensions/Shared/YapDatabaseExtensions.swift index e7ea5c9..aa3a1b8 100644 --- a/YapDatabaseExtensions/Shared/YapDatabaseExtensions.swift +++ b/YapDatabaseExtensions/Shared/YapDatabaseExtensions.swift @@ -119,6 +119,16 @@ public struct YapDB { } } +public func zipToWrite< + Value, Values, Metadata, Metadatas where + Values: SequenceType, + Values.Generator.Element == Value, + Metadatas: SequenceType, + Metadatas.Generator.Element == Metadata + >(values: Values, _ metadatas: Metadatas) -> [(Value, Metadata)] { + return zip(values, metadatas).map { ($0, $1) } +} + extension YapDB { /** From 756a308e30c66794fe83ce56a14be9ec770640b6 Mon Sep 17 00:00:00 2001 From: Jim Roepcke Date: Sat, 10 Sep 2016 12:34:15 -0700 Subject: [PATCH 10/14] =?UTF-8?q?add=20=E2=80=9CWithMetadata=E2=80=9D=20to?= =?UTF-8?q?=20API=20that=20reads=20and=20writes=20items=20and=20their=20me?= =?UTF-8?q?tadata?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Curried_ObjectWithObjectMetadata.swift | 20 ++--- .../Curried_ObjectWithValueMetadata.swift | 20 ++--- .../Curried_ValueWithObjectMetadata.swift | 20 ++--- .../Curried_ValueWithValueMetadata.swift | 20 ++--- .../Functional_ObjectWithObjectMetadata.swift | 60 ++++++------- .../Functional_ObjectWithValueMetadata.swift | 60 ++++++------- .../Functional_ValueWIthObjectMetadata.swift | 60 ++++++------- .../Functional_ValueWIthValueMetadata.swift | 60 ++++++------- ...Persistable_ObjectWithObjectMetadata.swift | 88 +++++++++---------- .../Persistable_ObjectWithValueMetadata.swift | 88 +++++++++---------- .../Persistable_ValueWithObjectMetadata.swift | 88 +++++++++---------- .../Persistable_ValueWithValueMetadata.swift | 88 +++++++++---------- 12 files changed, 336 insertions(+), 336 deletions(-) diff --git a/YapDatabaseExtensions/Shared/Curried/Curried_ObjectWithObjectMetadata.swift b/YapDatabaseExtensions/Shared/Curried/Curried_ObjectWithObjectMetadata.swift index a26adec..333c42e 100644 --- a/YapDatabaseExtensions/Shared/Curried/Curried_ObjectWithObjectMetadata.swift +++ b/YapDatabaseExtensions/Shared/Curried/Curried_ObjectWithObjectMetadata.swift @@ -21,11 +21,11 @@ extension Persistable where - parameter index: a YapDB.Index - returns: a (ReadTransaction) -> Self? closure. */ - public static func readAtIndex< + public static func readWithMetadataAtIndex< ReadTransaction, Metadata where ReadTransaction: ReadTransactionType, Metadata: NSCoding>(index: YapDB.Index) -> ReadTransaction -> (Self, Metadata?)? { - return { $0.readAtIndex(index) } + return { $0.readWithMetadataAtIndex(index) } } /** @@ -35,13 +35,13 @@ extension Persistable where - parameter indexes: a SequenceType of YapDB.Index values - returns: a (ReadTransaction) -> [Self] closure. */ - public static func readAtIndexes< + public static func readWithMetadataAtIndexes< Indexes, ReadTransaction, Metadata where Indexes: SequenceType, Indexes.Generator.Element == YapDB.Index, ReadTransaction: ReadTransactionType, Metadata: NSCoding>(indexes: Indexes) -> ReadTransaction -> [(Self, Metadata?)] { - return { $0.readAtIndexes(indexes) } + return { $0.readWithMetadataAtIndexes(indexes) } } /** @@ -51,11 +51,11 @@ extension Persistable where - parameter key: a String - returns: a (ReadTransaction) -> Self? closure. */ - public static func readByKey< + public static func readWithMetadataByKey< ReadTransaction, Metadata where ReadTransaction: ReadTransactionType, Metadata: NSCoding>(key: String) -> ReadTransaction -> (Self, Metadata?)? { - return { $0.readByKey(key) } + return { $0.readWithMetadataByKey(key) } } /** @@ -65,13 +65,13 @@ extension Persistable where - parameter keys: a SequenceType of String values - returns: a (ReadTransaction) -> [Self] closure. */ - public static func readByKeys< + public static func readWithMetadataByKeys< Keys, ReadTransaction, Metadata where Keys: SequenceType, Keys.Generator.Element == String, ReadTransaction: ReadTransactionType, Metadata: NSCoding>(keys: Keys) -> ReadTransaction -> [(Self, Metadata?)] { - return { $0.readAtIndexes(Self.indexesWithKeys(keys)) } + return { $0.readWithMetadataAtIndexes(Self.indexesWithKeys(keys)) } } /** @@ -81,10 +81,10 @@ extension Persistable where - warning: Be aware that this will capure `self`. - returns: a (WriteTransaction) -> Self closure */ - public func write< + public func writeWithMetadata< WriteTransaction, Metadata where WriteTransaction: WriteTransactionType, Metadata: NSCoding>(metadata: Metadata? = nil) -> WriteTransaction -> (Self, Metadata?) { - return { $0.write((self, metadata)) } + return { $0.writeWithMetadata((self, metadata)) } } } diff --git a/YapDatabaseExtensions/Shared/Curried/Curried_ObjectWithValueMetadata.swift b/YapDatabaseExtensions/Shared/Curried/Curried_ObjectWithValueMetadata.swift index c6fc8c7..b9b29a4 100644 --- a/YapDatabaseExtensions/Shared/Curried/Curried_ObjectWithValueMetadata.swift +++ b/YapDatabaseExtensions/Shared/Curried/Curried_ObjectWithValueMetadata.swift @@ -21,13 +21,13 @@ extension Persistable where - parameter index: a YapDB.Index - returns: a (ReadTransaction) -> Self? closure. */ - public static func readAtIndex< + public static func readWithMetadataAtIndex< ReadTransaction, Metadata where ReadTransaction: ReadTransactionType, Metadata: ValueCoding, Metadata.Coder: NSCoding, Metadata.Coder.ValueType == Metadata>(index: YapDB.Index) -> ReadTransaction -> (Self, Metadata?)? { - return { $0.readAtIndex(index) } + return { $0.readWithMetadataAtIndex(index) } } /** @@ -37,7 +37,7 @@ extension Persistable where - parameter indexes: a SequenceType of YapDB.Index values - returns: a (ReadTransaction) -> [Self] closure. */ - public static func readAtIndexes< + public static func readWithMetadataAtIndexes< Indexes, ReadTransaction, Metadata where Indexes: SequenceType, Indexes.Generator.Element == YapDB.Index, @@ -45,7 +45,7 @@ extension Persistable where Metadata: ValueCoding, Metadata.Coder: NSCoding, Metadata.Coder.ValueType == Metadata>(indexes: Indexes) -> ReadTransaction -> [(Self, Metadata?)] { - return { $0.readAtIndexes(indexes) } + return { $0.readWithMetadataAtIndexes(indexes) } } /** @@ -55,13 +55,13 @@ extension Persistable where - parameter key: a String - returns: a (ReadTransaction) -> Self? closure. */ - public static func readByKey< + public static func readWithMetadataByKey< ReadTransaction, Metadata where ReadTransaction: ReadTransactionType, Metadata: ValueCoding, Metadata.Coder: NSCoding, Metadata.Coder.ValueType == Metadata>(key: String) -> ReadTransaction -> (Self, Metadata?)? { - return { $0.readByKey(key) } + return { $0.readWithMetadataByKey(key) } } /** @@ -71,7 +71,7 @@ extension Persistable where - parameter keys: a SequenceType of String values - returns: a (ReadTransaction) -> [Self] closure. */ - public static func readByKeys< + public static func readWithMetadataByKeys< Keys, ReadTransaction, Metadata where Keys: SequenceType, Keys.Generator.Element == String, @@ -79,7 +79,7 @@ extension Persistable where Metadata: ValueCoding, Metadata.Coder: NSCoding, Metadata.Coder.ValueType == Metadata>(keys: Keys) -> ReadTransaction -> [(Self, Metadata?)] { - return { $0.readAtIndexes(Self.indexesWithKeys(keys)) } + return { $0.readWithMetadataAtIndexes(Self.indexesWithKeys(keys)) } } /** @@ -89,12 +89,12 @@ extension Persistable where - warning: Be aware that this will capure `self`. - returns: a (WriteTransaction) -> Self closure */ - public func write< + public func writeWithMetadata< WriteTransaction, Metadata where WriteTransaction: WriteTransactionType, Metadata: ValueCoding, Metadata.Coder: NSCoding, Metadata.Coder.ValueType == Metadata>(metadata: Metadata? = nil) -> WriteTransaction -> (Self, Metadata?) { - return { $0.write((self, metadata)) } + return { $0.writeWithMetadata((self, metadata)) } } } diff --git a/YapDatabaseExtensions/Shared/Curried/Curried_ValueWithObjectMetadata.swift b/YapDatabaseExtensions/Shared/Curried/Curried_ValueWithObjectMetadata.swift index d2e925c..c4c86bd 100644 --- a/YapDatabaseExtensions/Shared/Curried/Curried_ValueWithObjectMetadata.swift +++ b/YapDatabaseExtensions/Shared/Curried/Curried_ValueWithObjectMetadata.swift @@ -23,11 +23,11 @@ extension Persistable where - parameter index: a YapDB.Index - returns: a (ReadTransaction) -> Self? closure. */ - public static func readAtIndex< + public static func readWithMetadataAtIndex< ReadTransaction, Metadata where ReadTransaction: ReadTransactionType, Metadata: NSCoding>(index: YapDB.Index) -> ReadTransaction -> (Self, Metadata?)? { - return { $0.readAtIndex(index) } + return { $0.readWithMetadataAtIndex(index) } } /** @@ -37,13 +37,13 @@ extension Persistable where - parameter indexes: a SequenceType of YapDB.Index values - returns: a (ReadTransaction) -> [Self] closure. */ - public static func readAtIndexes< + public static func readWithMetadataAtIndexes< Indexes, ReadTransaction, Metadata where Indexes: SequenceType, Indexes.Generator.Element == YapDB.Index, ReadTransaction: ReadTransactionType, Metadata: NSCoding>(indexes: Indexes) -> ReadTransaction -> [(Self, Metadata?)] { - return { $0.readAtIndexes(indexes) } + return { $0.readWithMetadataAtIndexes(indexes) } } /** @@ -53,11 +53,11 @@ extension Persistable where - parameter key: a String - returns: a (ReadTransaction) -> Self? closure. */ - public static func readByKey< + public static func readWithMetadataByKey< ReadTransaction, Metadata where ReadTransaction: ReadTransactionType, Metadata: NSCoding>(key: String) -> ReadTransaction -> (Self, Metadata?)? { - return { $0.readByKey(key) } + return { $0.readWithMetadataByKey(key) } } /** @@ -67,13 +67,13 @@ extension Persistable where - parameter keys: a SequenceType of String values - returns: a (ReadTransaction) -> [Self] closure. */ - public static func readByKeys< + public static func readWithMetadataByKeys< Keys, ReadTransaction, Metadata where Keys: SequenceType, Keys.Generator.Element == String, ReadTransaction: ReadTransactionType, Metadata: NSCoding>(keys: Keys) -> ReadTransaction -> [(Self, Metadata?)] { - return { $0.readAtIndexes(Self.indexesWithKeys(keys)) } + return { $0.readWithMetadataAtIndexes(Self.indexesWithKeys(keys)) } } /** @@ -83,10 +83,10 @@ extension Persistable where - warning: Be aware that this will capure `self`. - returns: a (WriteTransaction) -> Self closure */ - public func write< + public func writeWithMetadata< WriteTransaction, Metadata where WriteTransaction: WriteTransactionType, Metadata: NSCoding>(metadata: Metadata? = nil) -> WriteTransaction -> (Self, Metadata?) { - return { $0.write((self, metadata)) } + return { $0.writeWithMetadata((self, metadata)) } } } diff --git a/YapDatabaseExtensions/Shared/Curried/Curried_ValueWithValueMetadata.swift b/YapDatabaseExtensions/Shared/Curried/Curried_ValueWithValueMetadata.swift index 0e0b267..a3e59fe 100644 --- a/YapDatabaseExtensions/Shared/Curried/Curried_ValueWithValueMetadata.swift +++ b/YapDatabaseExtensions/Shared/Curried/Curried_ValueWithValueMetadata.swift @@ -23,13 +23,13 @@ extension Persistable where - parameter index: a YapDB.Index - returns: a (ReadTransaction) -> Self? closure. */ - public static func readAtIndex< + public static func readWithMetadataAtIndex< ReadTransaction, Metadata where ReadTransaction: ReadTransactionType, Metadata: ValueCoding, Metadata.Coder: NSCoding, Metadata.Coder.ValueType == Metadata>(index: YapDB.Index) -> ReadTransaction -> (Self, Metadata?)? { - return { $0.readAtIndex(index) } + return { $0.readWithMetadataAtIndex(index) } } /** @@ -39,7 +39,7 @@ extension Persistable where - parameter indexes: a SequenceType of YapDB.Index values - returns: a (ReadTransaction) -> [Self] closure. */ - public static func readAtIndexes< + public static func readWithMetadataAtIndexes< Indexes, ReadTransaction, Metadata where Indexes: SequenceType, Indexes.Generator.Element == YapDB.Index, @@ -47,7 +47,7 @@ extension Persistable where Metadata: ValueCoding, Metadata.Coder: NSCoding, Metadata.Coder.ValueType == Metadata>(indexes: Indexes) -> ReadTransaction -> [(Self, Metadata?)] { - return { $0.readAtIndexes(indexes) } + return { $0.readWithMetadataAtIndexes(indexes) } } /** @@ -57,13 +57,13 @@ extension Persistable where - parameter key: a String - returns: a (ReadTransaction) -> Self? closure. */ - public static func readByKey< + public static func readWithMetadataByKey< ReadTransaction, Metadata where ReadTransaction: ReadTransactionType, Metadata: ValueCoding, Metadata.Coder: NSCoding, Metadata.Coder.ValueType == Metadata>(key: String) -> ReadTransaction -> (Self, Metadata?)? { - return { $0.readByKey(key) } + return { $0.readWithMetadataByKey(key) } } /** @@ -73,7 +73,7 @@ extension Persistable where - parameter keys: a SequenceType of String values - returns: a (ReadTransaction) -> [Self] closure. */ - public static func readByKeys< + public static func readWithMetadataByKeys< Keys, ReadTransaction, Metadata where Keys: SequenceType, Keys.Generator.Element == String, @@ -81,7 +81,7 @@ extension Persistable where Metadata: ValueCoding, Metadata.Coder: NSCoding, Metadata.Coder.ValueType == Metadata>(keys: Keys) -> ReadTransaction -> [(Self, Metadata?)] { - return { $0.readAtIndexes(Self.indexesWithKeys(keys)) } + return { $0.readWithMetadataAtIndexes(Self.indexesWithKeys(keys)) } } /** @@ -91,12 +91,12 @@ extension Persistable where - warning: Be aware that this will capure `self`. - returns: a (WriteTransaction) -> Self closure */ - public func write< + public func writeWithMetadata< WriteTransaction, Metadata where WriteTransaction: WriteTransactionType, Metadata: ValueCoding, Metadata.Coder: NSCoding, Metadata.Coder.ValueType == Metadata>(metadata: Metadata? = nil) -> WriteTransaction -> (Self, Metadata?) { - return { $0.write((self, metadata)) } + return { $0.writeWithMetadata((self, metadata)) } } } diff --git a/YapDatabaseExtensions/Shared/Functional/Functional_ObjectWithObjectMetadata.swift b/YapDatabaseExtensions/Shared/Functional/Functional_ObjectWithObjectMetadata.swift index 7eff7f9..eb92925 100644 --- a/YapDatabaseExtensions/Shared/Functional/Functional_ObjectWithObjectMetadata.swift +++ b/YapDatabaseExtensions/Shared/Functional/Functional_ObjectWithObjectMetadata.swift @@ -20,7 +20,7 @@ extension ReadTransactionType { - parameter index: a YapDB.Index - returns: an optional `ItemType` */ - public func readAtIndex< + public func readWithMetadataAtIndex< Object, Metadata where Object: Persistable, Object: NSCoding, @@ -36,7 +36,7 @@ extension ReadTransactionType { - parameter indexes: a SequenceType of YapDB.Index values - returns: an array of `ItemType` */ - public func readAtIndexes< + public func readWithMetadataAtIndexes< Indexes, Object, Metadata where Indexes: SequenceType, Indexes.Generator.Element == YapDB.Index, @@ -44,7 +44,7 @@ extension ReadTransactionType { Object: NSCoding, Metadata: NSCoding>(indexes: Indexes) -> [(Object, Metadata?)] { // FIXME: using flatMap means the output length need not match the input length - return indexes.flatMap(readAtIndex) + return indexes.flatMap(readWithMetadataAtIndex) } /** @@ -53,12 +53,12 @@ extension ReadTransactionType { - parameter key: a String - returns: an optional `ItemType` */ - public func readByKey< + public func readWithMetadataByKey< Object, Metadata where Object: Persistable, Object: NSCoding, Metadata: NSCoding>(key: String) -> (Object, Metadata?)? { - return readAtIndex(Object.indexWithKey(key)) + return readWithMetadataAtIndex(Object.indexWithKey(key)) } /** @@ -67,14 +67,14 @@ extension ReadTransactionType { - parameter keys: a SequenceType of String values - returns: an array of `ItemType` */ - public func readByKeys< + public func readWithMetadataByKeys< Keys, Object, Metadata where Keys: SequenceType, Keys.Generator.Element == String, Object: Persistable, Object: NSCoding, Metadata: NSCoding>(keys: Keys) -> [(Object, Metadata?)] { - return readAtIndexes(Object.indexesWithKeys(keys)) + return readWithMetadataAtIndexes(Object.indexesWithKeys(keys)) } /** @@ -82,12 +82,12 @@ extension ReadTransactionType { - returns: an array of `ItemType` */ - public func readAll< + public func readWithMetadataAll< Object, Metadata where Object: Persistable, Object: NSCoding, Metadata: NSCoding>() -> [(Object, Metadata?)] { - return readByKeys(keysInCollection(Object.collection)) + return readWithMetadataByKeys(keysInCollection(Object.collection)) } } @@ -99,12 +99,12 @@ extension ConnectionType { - parameter index: a YapDB.Index - returns: an optional `ItemType` */ - public func readAtIndex< + public func readWithMetadataAtIndex< Object, Metadata where Object: Persistable, Object: NSCoding, Metadata: NSCoding>(index: YapDB.Index) -> (Object, Metadata?)? { - return read { $0.readAtIndex(index) } + return read { $0.readWithMetadataAtIndex(index) } } /** @@ -113,14 +113,14 @@ extension ConnectionType { - parameter indexes: a SequenceType of YapDB.Index values - returns: an array of `ItemType` */ - public func readAtIndexes< + public func readWithMetadataAtIndexes< Indexes, Object, Metadata where Indexes: SequenceType, Indexes.Generator.Element == YapDB.Index, Object: Persistable, Object: NSCoding, Metadata: NSCoding>(indexes: Indexes) -> [(Object, Metadata?)] { - return read { $0.readAtIndexes(indexes) } + return read { $0.readWithMetadataAtIndexes(indexes) } } /** @@ -129,12 +129,12 @@ extension ConnectionType { - parameter key: a String - returns: an optional `ItemType` */ - public func readByKey< + public func readWithMetadataByKey< Object, Metadata where Object: Persistable, Object: NSCoding, Metadata: NSCoding>(key: String) -> (Object, Metadata?)? { - return readAtIndex(Object.indexWithKey(key)) + return readWithMetadataAtIndex(Object.indexWithKey(key)) } /** @@ -143,14 +143,14 @@ extension ConnectionType { - parameter keys: a SequenceType of String values - returns: an array of `ItemType` */ - public func readByKeys< + public func readWithMetadataByKeys< Keys, Object, Metadata where Keys: SequenceType, Keys.Generator.Element == String, Object: Persistable, Object: NSCoding, Metadata: NSCoding>(keys: Keys) -> [(Object, Metadata?)] { - return readAtIndexes(Object.indexesWithKeys(keys)) + return readWithMetadataAtIndexes(Object.indexesWithKeys(keys)) } /** @@ -158,12 +158,12 @@ extension ConnectionType { - returns: an array of `ItemType` */ - public func readAll< + public func readWithMetadataAll< Object, Metadata where Object: Persistable, Object: NSCoding, Metadata: NSCoding>() -> [(Object, Metadata?)] { - return read { $0.readAll() } + return read { $0.readWithMetadataAll() } } } @@ -176,7 +176,7 @@ extension WriteTransactionType { - parameter item: the item to store. */ - public func write< + public func writeWithMetadata< Object, Metadata where Object: Persistable, Object: NSCoding, @@ -190,14 +190,14 @@ extension WriteTransactionType { - parameter items: a SequenceType of items to store. */ - public func write< + public func writeWithMetadata< Items, Object, Metadata where Items: SequenceType, Items.Generator.Element == (Object, Metadata?), Object: Persistable, Object: NSCoding, Metadata: NSCoding>(items: Items) -> [(Object, Metadata?)] { - return items.map(write) + return items.map(writeWithMetadata) } } @@ -208,12 +208,12 @@ extension ConnectionType { - parameter item: the item to store. */ - public func write< + public func writeWithMetadata< Object, Metadata where Object: Persistable, Object: NSCoding, Metadata: NSCoding>(item: (Object, Metadata?)) -> (Object, Metadata?) { - return write { $0.write(item) } + return write { $0.writeWithMetadata(item) } } /** @@ -221,14 +221,14 @@ extension ConnectionType { - parameter items: a SequenceType of items to store. */ - public func write< + public func writeWithMetadata< Items, Object, Metadata where Items: SequenceType, Items.Generator.Element == (Object, Metadata?), Object: Persistable, Object: NSCoding, Metadata: NSCoding>(items: Items) -> [(Object, Metadata?)] { - return write { $0.write(items) } + return write { $0.writeWithMetadata(items) } } /** @@ -238,12 +238,12 @@ extension ConnectionType { - parameter queue: the dispatch_queue_t to run the completion block on. - parameter completion: a dispatch_block_t for completion. */ - public func asyncWrite< + public func asyncWriteWithMetadata< Object, Metadata where Object: Persistable, Object: NSCoding, Metadata: NSCoding>(item: (Object, Metadata?), queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ((Object, Metadata?) -> Void)? = .None) { - asyncWrite({ $0.write(item) }, queue: queue, completion: completion) + asyncWrite({ $0.writeWithMetadata(item) }, queue: queue, completion: completion) } /** @@ -253,14 +253,14 @@ extension ConnectionType { - parameter queue: the dispatch_queue_t to run the completion block on. - parameter completion: a dispatch_block_t for completion. */ - public func asyncWrite< + public func asyncWriteWithMetadata< Items, Object, Metadata where Items: SequenceType, Items.Generator.Element == (Object, Metadata?), Object: Persistable, Object: NSCoding, Metadata: NSCoding>(items: Items, queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ([(Object, Metadata?)] -> Void)? = .None) { - asyncWrite({ $0.write(items) }, queue: queue, completion: completion) + asyncWrite({ $0.writeWithMetadata(items) }, queue: queue, completion: completion) } } diff --git a/YapDatabaseExtensions/Shared/Functional/Functional_ObjectWithValueMetadata.swift b/YapDatabaseExtensions/Shared/Functional/Functional_ObjectWithValueMetadata.swift index cc5f0a8..cd0b332 100644 --- a/YapDatabaseExtensions/Shared/Functional/Functional_ObjectWithValueMetadata.swift +++ b/YapDatabaseExtensions/Shared/Functional/Functional_ObjectWithValueMetadata.swift @@ -20,7 +20,7 @@ extension ReadTransactionType { - parameter index: a YapDB.Index - returns: an optional `ItemType` */ - public func readAtIndex< + public func readWithMetadataAtIndex< Object, Metadata where Object: Persistable, Object: NSCoding, @@ -38,7 +38,7 @@ extension ReadTransactionType { - parameter indexes: a SequenceType of YapDB.Index values - returns: an array of `ItemType` */ - public func readAtIndexes< + public func readWithMetadataAtIndexes< Indexes, Object, Metadata where Indexes: SequenceType, Indexes.Generator.Element == YapDB.Index, @@ -48,7 +48,7 @@ extension ReadTransactionType { Metadata.Coder: NSCoding, Metadata.Coder.ValueType == Metadata>(indexes: Indexes) -> [(Object, Metadata?)] { // FIXME: using flatMap means the output length need not match the input length - return indexes.flatMap(readAtIndex) + return indexes.flatMap(readWithMetadataAtIndex) } /** @@ -57,14 +57,14 @@ extension ReadTransactionType { - parameter key: a String - returns: an optional `ItemType` */ - public func readByKey< + public func readWithMetadataByKey< Object, Metadata where Object: Persistable, Object: NSCoding, Metadata: ValueCoding, Metadata.Coder: NSCoding, Metadata.Coder.ValueType == Metadata>(key: String) -> (Object, Metadata?)? { - return readAtIndex(Object.indexWithKey(key)) + return readWithMetadataAtIndex(Object.indexWithKey(key)) } /** @@ -73,7 +73,7 @@ extension ReadTransactionType { - parameter keys: a SequenceType of String values - returns: an array of `ItemType` */ - public func readByKeys< + public func readWithMetadataByKeys< Keys, Object, Metadata where Keys: SequenceType, Keys.Generator.Element == String, @@ -82,7 +82,7 @@ extension ReadTransactionType { Metadata: ValueCoding, Metadata.Coder: NSCoding, Metadata.Coder.ValueType == Metadata>(keys: Keys) -> [(Object, Metadata?)] { - return readAtIndexes(Object.indexesWithKeys(keys)) + return readWithMetadataAtIndexes(Object.indexesWithKeys(keys)) } /** @@ -90,14 +90,14 @@ extension ReadTransactionType { - returns: an array of `ItemType` */ - public func readAll< + public func readWithMetadataAll< Object, Metadata where Object: Persistable, Object: NSCoding, Metadata: ValueCoding, Metadata.Coder: NSCoding, Metadata.Coder.ValueType == Metadata>() -> [(Object, Metadata?)] { - return readByKeys(keysInCollection(Object.collection)) + return readWithMetadataByKeys(keysInCollection(Object.collection)) } } @@ -109,14 +109,14 @@ extension ConnectionType { - parameter index: a YapDB.Index - returns: an optional `ItemType` */ - public func readAtIndex< + public func readWithMetadataAtIndex< Object, Metadata where Object: Persistable, Object: NSCoding, Metadata: ValueCoding, Metadata.Coder: NSCoding, Metadata.Coder.ValueType == Metadata>(index: YapDB.Index) -> (Object, Metadata?)? { - return read { $0.readAtIndex(index) } + return read { $0.readWithMetadataAtIndex(index) } } /** @@ -125,7 +125,7 @@ extension ConnectionType { - parameter indexes: a SequenceType of YapDB.Index values - returns: an array of `ItemType` */ - public func readAtIndexes< + public func readWithMetadataAtIndexes< Indexes, Object, Metadata where Indexes: SequenceType, Indexes.Generator.Element == YapDB.Index, @@ -134,7 +134,7 @@ extension ConnectionType { Metadata: ValueCoding, Metadata.Coder: NSCoding, Metadata.Coder.ValueType == Metadata>(indexes: Indexes) -> [(Object, Metadata?)] { - return read { $0.readAtIndexes(indexes) } + return read { $0.readWithMetadataAtIndexes(indexes) } } /** @@ -143,14 +143,14 @@ extension ConnectionType { - parameter key: a String - returns: an optional `ItemType` */ - public func readByKey< + public func readWithMetadataByKey< Object, Metadata where Object: Persistable, Object: NSCoding, Metadata: ValueCoding, Metadata.Coder: NSCoding, Metadata.Coder.ValueType == Metadata>(key: String) -> (Object, Metadata?)? { - return readAtIndex(Object.indexWithKey(key)) + return readWithMetadataAtIndex(Object.indexWithKey(key)) } /** @@ -159,7 +159,7 @@ extension ConnectionType { - parameter keys: a SequenceType of String values - returns: an array of `ItemType` */ - public func readByKeys< + public func readWithMetadataByKeys< Keys, Object, Metadata where Keys: SequenceType, Keys.Generator.Element == String, @@ -168,7 +168,7 @@ extension ConnectionType { Metadata: ValueCoding, Metadata.Coder: NSCoding, Metadata.Coder.ValueType == Metadata>(keys: Keys) -> [(Object, Metadata?)] { - return readAtIndexes(Object.indexesWithKeys(keys)) + return readWithMetadataAtIndexes(Object.indexesWithKeys(keys)) } /** @@ -176,14 +176,14 @@ extension ConnectionType { - returns: an array of `ItemType` */ - public func readAll< + public func readWithMetadataAll< Object, Metadata where Object: Persistable, Object: NSCoding, Metadata: ValueCoding, Metadata.Coder: NSCoding, Metadata.Coder.ValueType == Metadata>() -> [(Object, Metadata?)] { - return read { $0.readAll() } + return read { $0.readWithMetadataAll() } } } @@ -196,7 +196,7 @@ extension WriteTransactionType { - parameter item: the item to store. */ - public func write< + public func writeWithMetadata< Object, Metadata where Object: Persistable, Object: NSCoding, @@ -212,7 +212,7 @@ extension WriteTransactionType { - parameter items: a SequenceType of items to store. */ - public func write< + public func writeWithMetadata< Items, Object, Metadata where Items: SequenceType, Items.Generator.Element == (Object, Metadata?), @@ -221,7 +221,7 @@ extension WriteTransactionType { Metadata: ValueCoding, Metadata.Coder: NSCoding, Metadata.Coder.ValueType == Metadata>(items: Items) -> [(Object, Metadata?)] { - return items.map(write) + return items.map(writeWithMetadata) } } @@ -232,14 +232,14 @@ extension ConnectionType { - parameter item: the item to store. */ - public func write< + public func writeWithMetadata< Object, Metadata where Object: Persistable, Object: NSCoding, Metadata: ValueCoding, Metadata.Coder: NSCoding, Metadata.Coder.ValueType == Metadata>(item: (Object, Metadata?)) -> (Object, Metadata?) { - return write { $0.write(item) } + return write { $0.writeWithMetadata(item) } } /** @@ -247,7 +247,7 @@ extension ConnectionType { - parameter items: a SequenceType of items to store. */ - public func write< + public func writeWithMetadata< Items, Object, Metadata where Items: SequenceType, Items.Generator.Element == (Object, Metadata?), @@ -256,7 +256,7 @@ extension ConnectionType { Metadata: ValueCoding, Metadata.Coder: NSCoding, Metadata.Coder.ValueType == Metadata>(items: Items) -> [(Object, Metadata?)] { - return write { $0.write(items) } + return write { $0.writeWithMetadata(items) } } /** @@ -266,14 +266,14 @@ extension ConnectionType { - parameter queue: the dispatch_queue_t to run the completion block on. - parameter completion: a dispatch_block_t for completion. */ - public func asyncWrite< + public func asyncWriteWithMetadata< Object, Metadata where Object: Persistable, Object: NSCoding, Metadata: ValueCoding, Metadata.Coder: NSCoding, Metadata.Coder.ValueType == Metadata>(item: (Object, Metadata?), queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ((Object, Metadata?) -> Void)? = .None) { - asyncWrite({ $0.write(item) }, queue: queue, completion: completion) + asyncWrite({ $0.writeWithMetadata(item) }, queue: queue, completion: completion) } /** @@ -283,7 +283,7 @@ extension ConnectionType { - parameter queue: the dispatch_queue_t to run the completion block on. - parameter completion: a dispatch_block_t for completion. */ - public func asyncWrite< + public func asyncWriteWithMetadata< Items, Object, Metadata where Items: SequenceType, Items.Generator.Element == (Object, Metadata?), @@ -292,7 +292,7 @@ extension ConnectionType { Metadata: ValueCoding, Metadata.Coder: NSCoding, Metadata.Coder.ValueType == Metadata>(items: Items, queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ([(Object, Metadata?)] -> Void)? = .None) { - asyncWrite({ $0.write(items) }, queue: queue, completion: completion) + asyncWrite({ $0.writeWithMetadata(items) }, queue: queue, completion: completion) } } diff --git a/YapDatabaseExtensions/Shared/Functional/Functional_ValueWIthObjectMetadata.swift b/YapDatabaseExtensions/Shared/Functional/Functional_ValueWIthObjectMetadata.swift index 1344b75..1b14983 100644 --- a/YapDatabaseExtensions/Shared/Functional/Functional_ValueWIthObjectMetadata.swift +++ b/YapDatabaseExtensions/Shared/Functional/Functional_ValueWIthObjectMetadata.swift @@ -20,7 +20,7 @@ extension ReadTransactionType { - parameter index: a YapDB.Index - returns: an optional `ItemType` */ - public func readAtIndex< + public func readWithMetadataAtIndex< Value, Metadata where Value: Persistable, Value: ValueCoding, @@ -38,7 +38,7 @@ extension ReadTransactionType { - parameter indexes: a SequenceType of YapDB.Index values - returns: an array of `ItemType` */ - public func readAtIndexes< + public func readWithMetadataAtIndexes< Indexes, Value, Metadata where Indexes: SequenceType, Indexes.Generator.Element == YapDB.Index, @@ -48,7 +48,7 @@ extension ReadTransactionType { Value.Coder.ValueType == Value, Metadata: NSCoding>(indexes: Indexes) -> [(Value, Metadata?)] { // FIXME: using flatMap means the output length need not match the input length - return indexes.flatMap(readAtIndex) + return indexes.flatMap(readWithMetadataAtIndex) } /** @@ -57,14 +57,14 @@ extension ReadTransactionType { - parameter key: a String - returns: an optional `ItemType` */ - public func readByKey< + public func readWithMetadataByKey< Value, Metadata where Value: Persistable, Value: ValueCoding, Value.Coder: NSCoding, Value.Coder.ValueType == Value, Metadata: NSCoding>(key: String) -> (Value, Metadata?)? { - return readAtIndex(Value.indexWithKey(key)) + return readWithMetadataAtIndex(Value.indexWithKey(key)) } /** @@ -73,7 +73,7 @@ extension ReadTransactionType { - parameter keys: a SequenceType of String values - returns: an array of `ItemType` */ - public func readByKeys< + public func readWithMetadataByKeys< Keys, Value, Metadata where Keys: SequenceType, Keys.Generator.Element == String, @@ -82,7 +82,7 @@ extension ReadTransactionType { Value.Coder: NSCoding, Value.Coder.ValueType == Value, Metadata: NSCoding>(keys: Keys) -> [(Value, Metadata?)] { - return readAtIndexes(Value.indexesWithKeys(keys)) + return readWithMetadataAtIndexes(Value.indexesWithKeys(keys)) } /** @@ -90,14 +90,14 @@ extension ReadTransactionType { - returns: an array of `ItemType` */ - public func readAll< + public func readWithMetadataAll< Value, Metadata where Value: Persistable, Value: ValueCoding, Value.Coder: NSCoding, Value.Coder.ValueType == Value, Metadata: NSCoding>() -> [(Value, Metadata?)] { - return readByKeys(keysInCollection(Value.collection)) + return readWithMetadataByKeys(keysInCollection(Value.collection)) } } @@ -109,14 +109,14 @@ extension ConnectionType { - parameter index: a YapDB.Index - returns: an optional `ItemType` */ - public func readAtIndex< + public func readWithMetadataAtIndex< Value, Metadata where Value: Persistable, Value: ValueCoding, Value.Coder: NSCoding, Value.Coder.ValueType == Value, Metadata: NSCoding>(index: YapDB.Index) -> (Value, Metadata?)? { - return read { $0.readAtIndex(index) } + return read { $0.readWithMetadataAtIndex(index) } } /** @@ -125,7 +125,7 @@ extension ConnectionType { - parameter indexes: a SequenceType of YapDB.Index values - returns: an array of `ItemType` */ - public func readAtIndexes< + public func readWithMetadataAtIndexes< Indexes, Value, Metadata where Indexes: SequenceType, Indexes.Generator.Element == YapDB.Index, @@ -134,7 +134,7 @@ extension ConnectionType { Value.Coder: NSCoding, Value.Coder.ValueType == Value, Metadata: NSCoding>(indexes: Indexes) -> [(Value, Metadata?)] { - return read { $0.readAtIndexes(indexes) } + return read { $0.readWithMetadataAtIndexes(indexes) } } /** @@ -143,14 +143,14 @@ extension ConnectionType { - parameter key: a String - returns: an optional `ItemType` */ - public func readByKey< + public func readWithMetadataByKey< Value, Metadata where Value: Persistable, Value: ValueCoding, Value.Coder: NSCoding, Value.Coder.ValueType == Value, Metadata: NSCoding>(key: String) -> (Value, Metadata?)? { - return readAtIndex(Value.indexWithKey(key)) + return readWithMetadataAtIndex(Value.indexWithKey(key)) } /** @@ -159,7 +159,7 @@ extension ConnectionType { - parameter keys: a SequenceType of String values - returns: an array of `ItemType` */ - public func readByKeys< + public func readWithMetadataByKeys< Keys, Value, Metadata where Keys: SequenceType, Keys.Generator.Element == String, @@ -168,7 +168,7 @@ extension ConnectionType { Value.Coder: NSCoding, Value.Coder.ValueType == Value, Metadata: NSCoding>(keys: Keys) -> [(Value, Metadata?)] { - return readAtIndexes(Value.indexesWithKeys(keys)) + return readWithMetadataAtIndexes(Value.indexesWithKeys(keys)) } /** @@ -176,14 +176,14 @@ extension ConnectionType { - returns: an array of `ItemType` */ - public func readAll< + public func readWithMetadataAll< Value, Metadata where Value: Persistable, Value: ValueCoding, Value.Coder: NSCoding, Value.Coder.ValueType == Value, Metadata: NSCoding>() -> [(Value, Metadata?)] { - return read { $0.readAll() } + return read { $0.readWithMetadataAll() } } } @@ -196,7 +196,7 @@ extension WriteTransactionType { - parameter item: the item to store. */ - public func write< + public func writeWithMetadata< Value, Metadata where Value: Persistable, Value: ValueCoding, @@ -212,7 +212,7 @@ extension WriteTransactionType { - parameter items: a SequenceType of items to store. */ - public func write< + public func writeWithMetadata< Items, Value, Metadata where Items: SequenceType, Items.Generator.Element == (Value, Metadata?), @@ -221,7 +221,7 @@ extension WriteTransactionType { Value.Coder: NSCoding, Value.Coder.ValueType == Value, Metadata: NSCoding>(items: Items) -> [(Value, Metadata?)] { - return items.map(write) + return items.map(writeWithMetadata) } } @@ -232,14 +232,14 @@ extension ConnectionType { - parameter item: the item to store. */ - public func write< + public func writeWithMetadata< Value, Metadata where Value: Persistable, Value: ValueCoding, Value.Coder: NSCoding, Value.Coder.ValueType == Value, Metadata: NSCoding>(item: (Value, Metadata?)) -> (Value, Metadata?) { - return write { $0.write(item) } + return write { $0.writeWithMetadata(item) } } /** @@ -247,7 +247,7 @@ extension ConnectionType { - parameter items: a SequenceType of items to store. */ - public func write< + public func writeWithMetadata< Items, Value, Metadata where Items: SequenceType, Items.Generator.Element == (Value, Metadata?), @@ -256,7 +256,7 @@ extension ConnectionType { Value.Coder: NSCoding, Value.Coder.ValueType == Value, Metadata: NSCoding>(items: Items) -> [(Value, Metadata?)] { - return write { $0.write(items) } + return write { $0.writeWithMetadata(items) } } /** @@ -266,14 +266,14 @@ extension ConnectionType { - parameter queue: the dispatch_queue_t to run the completion block on. - parameter completion: a dispatch_block_t for completion. */ - public func asyncWrite< + public func asyncWriteWithMetadata< Value, Metadata where Value: Persistable, Value: ValueCoding, Value.Coder: NSCoding, Value.Coder.ValueType == Value, Metadata: NSCoding>(item: (Value, Metadata?), queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ((Value, Metadata?) -> Void)? = .None) { - asyncWrite({ $0.write(item) }, queue: queue, completion: completion) + asyncWrite({ $0.writeWithMetadata(item) }, queue: queue, completion: completion) } /** @@ -283,7 +283,7 @@ extension ConnectionType { - parameter queue: the dispatch_queue_t to run the completion block on. - parameter completion: a dispatch_block_t for completion. */ - public func asyncWrite< + public func asyncWriteWithMetadata< Items, Value, Metadata where Items: SequenceType, Items.Generator.Element == (Value, Metadata?), @@ -292,7 +292,7 @@ extension ConnectionType { Value.Coder: NSCoding, Value.Coder.ValueType == Value, Metadata: NSCoding>(items: Items, queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ([(Value, Metadata?)] -> Void)? = .None) { - asyncWrite({ $0.write(items) }, queue: queue, completion: completion) + asyncWrite({ $0.writeWithMetadata(items) }, queue: queue, completion: completion) } } diff --git a/YapDatabaseExtensions/Shared/Functional/Functional_ValueWIthValueMetadata.swift b/YapDatabaseExtensions/Shared/Functional/Functional_ValueWIthValueMetadata.swift index 217205d..ea95096 100644 --- a/YapDatabaseExtensions/Shared/Functional/Functional_ValueWIthValueMetadata.swift +++ b/YapDatabaseExtensions/Shared/Functional/Functional_ValueWIthValueMetadata.swift @@ -20,7 +20,7 @@ extension ReadTransactionType { - parameter index: a YapDB.Index - returns: an optional `ItemType` */ - public func readAtIndex< + public func readWithMetadataAtIndex< Value, Metadata where Value: Persistable, Value: ValueCoding, @@ -40,7 +40,7 @@ extension ReadTransactionType { - parameter indexes: a SequenceType of YapDB.Index values - returns: an array of `ItemType` */ - public func readAtIndexes< + public func readWithMetadataAtIndexes< Indexes, Value, Metadata where Indexes: SequenceType, Indexes.Generator.Element == YapDB.Index, @@ -52,7 +52,7 @@ extension ReadTransactionType { Metadata.Coder: NSCoding, Metadata.Coder.ValueType == Metadata>(indexes: Indexes) -> [(Value, Metadata?)] { // FIXME: using flatMap means the output length need not match the input length - return indexes.flatMap(readAtIndex) + return indexes.flatMap(readWithMetadataAtIndex) } /** @@ -61,7 +61,7 @@ extension ReadTransactionType { - parameter key: a String - returns: an optional `ItemType` */ - public func readByKey< + public func readWithMetadataByKey< Value, Metadata where Value: Persistable, Value: ValueCoding, @@ -70,7 +70,7 @@ extension ReadTransactionType { Metadata: ValueCoding, Metadata.Coder: NSCoding, Metadata.Coder.ValueType == Metadata>(key: String) -> (Value, Metadata?)? { - return readAtIndex(Value.indexWithKey(key)) + return readWithMetadataAtIndex(Value.indexWithKey(key)) } /** @@ -79,7 +79,7 @@ extension ReadTransactionType { - parameter keys: a SequenceType of String values - returns: an array of `ItemType` */ - public func readByKeys< + public func readWithMetadataByKeys< Keys, Value, Metadata where Keys: SequenceType, Keys.Generator.Element == String, @@ -90,7 +90,7 @@ extension ReadTransactionType { Metadata: ValueCoding, Metadata.Coder: NSCoding, Metadata.Coder.ValueType == Metadata>(keys: Keys) -> [(Value, Metadata?)] { - return readAtIndexes(Value.indexesWithKeys(keys)) + return readWithMetadataAtIndexes(Value.indexesWithKeys(keys)) } /** @@ -98,7 +98,7 @@ extension ReadTransactionType { - returns: an array of `ItemType` */ - public func readAll< + public func readWithMetadataAll< Value, Metadata where Value: Persistable, Value: ValueCoding, @@ -107,7 +107,7 @@ extension ReadTransactionType { Metadata: ValueCoding, Metadata.Coder: NSCoding, Metadata.Coder.ValueType == Metadata>() -> [(Value, Metadata?)] { - return readByKeys(keysInCollection(Value.collection)) + return readWithMetadataByKeys(keysInCollection(Value.collection)) } } @@ -119,7 +119,7 @@ extension ConnectionType { - parameter index: a YapDB.Index - returns: an optional `ItemType` */ - public func readAtIndex< + public func readWithMetadataAtIndex< Value, Metadata where Value: Persistable, Value: ValueCoding, @@ -128,7 +128,7 @@ extension ConnectionType { Metadata: ValueCoding, Metadata.Coder: NSCoding, Metadata.Coder.ValueType == Metadata>(index: YapDB.Index) -> (Value, Metadata?)? { - return read { $0.readAtIndex(index) } + return read { $0.readWithMetadataAtIndex(index) } } /** @@ -137,7 +137,7 @@ extension ConnectionType { - parameter indexes: a SequenceType of YapDB.Index values - returns: an array of `ItemType` */ - public func readAtIndexes< + public func readWithMetadataAtIndexes< Indexes, Value, Metadata where Indexes: SequenceType, Indexes.Generator.Element == YapDB.Index, @@ -148,7 +148,7 @@ extension ConnectionType { Metadata: ValueCoding, Metadata.Coder: NSCoding, Metadata.Coder.ValueType == Metadata>(indexes: Indexes) -> [(Value, Metadata?)] { - return read { $0.readAtIndexes(indexes) } + return read { $0.readWithMetadataAtIndexes(indexes) } } /** @@ -157,7 +157,7 @@ extension ConnectionType { - parameter key: a String - returns: an optional `ItemType` */ - public func readByKey< + public func readWithMetadataByKey< Value, Metadata where Value: Persistable, Value: ValueCoding, @@ -166,7 +166,7 @@ extension ConnectionType { Metadata: ValueCoding, Metadata.Coder: NSCoding, Metadata.Coder.ValueType == Metadata>(key: String) -> (Value, Metadata?)? { - return readAtIndex(Value.indexWithKey(key)) + return readWithMetadataAtIndex(Value.indexWithKey(key)) } /** @@ -175,7 +175,7 @@ extension ConnectionType { - parameter keys: a SequenceType of String values - returns: an array of `ItemType` */ - public func readByKeys< + public func readWithMetadataByKeys< Keys, Value, Metadata where Keys: SequenceType, Keys.Generator.Element == String, @@ -186,7 +186,7 @@ extension ConnectionType { Metadata: ValueCoding, Metadata.Coder: NSCoding, Metadata.Coder.ValueType == Metadata>(keys: Keys) -> [(Value, Metadata?)] { - return readAtIndexes(Value.indexesWithKeys(keys)) + return readWithMetadataAtIndexes(Value.indexesWithKeys(keys)) } /** @@ -194,7 +194,7 @@ extension ConnectionType { - returns: an array of `ItemType` */ - public func readAll< + public func readWithMetadataAll< Value, Metadata where Value: Persistable, Value: ValueCoding, @@ -203,7 +203,7 @@ extension ConnectionType { Metadata: ValueCoding, Metadata.Coder: NSCoding, Metadata.Coder.ValueType == Metadata>() -> [(Value, Metadata?)] { - return read { $0.readAll() } + return read { $0.readWithMetadataAll() } } } @@ -216,7 +216,7 @@ extension WriteTransactionType { - parameter item: the item to store. */ - public func write< + public func writeWithMetadata< Value, Metadata where Value: Persistable, Value: ValueCoding, @@ -234,7 +234,7 @@ extension WriteTransactionType { - parameter items: a SequenceType of items to store. */ - public func write< + public func writeWithMetadata< Items, Value, Metadata where Items: SequenceType, Items.Generator.Element == (Value, Metadata?), @@ -245,7 +245,7 @@ extension WriteTransactionType { Metadata: ValueCoding, Metadata.Coder: NSCoding, Metadata.Coder.ValueType == Metadata>(items: Items) -> [(Value, Metadata?)] { - return items.map(write) + return items.map(writeWithMetadata) } } @@ -256,7 +256,7 @@ extension ConnectionType { - parameter item: the item to store. */ - public func write< + public func writeWithMetadata< Value, Metadata where Value: Persistable, Value: ValueCoding, @@ -265,7 +265,7 @@ extension ConnectionType { Metadata: ValueCoding, Metadata.Coder: NSCoding, Metadata.Coder.ValueType == Metadata>(item: (Value, Metadata?)) -> (Value, Metadata?) { - return write { $0.write(item) } + return write { $0.writeWithMetadata(item) } } /** @@ -273,7 +273,7 @@ extension ConnectionType { - parameter items: a SequenceType of items to store. */ - public func write< + public func writeWithMetadata< Items, Value, Metadata where Items: SequenceType, Items.Generator.Element == (Value, Metadata?), @@ -284,7 +284,7 @@ extension ConnectionType { Metadata: ValueCoding, Metadata.Coder: NSCoding, Metadata.Coder.ValueType == Metadata>(items: Items) -> [(Value, Metadata?)] { - return write { $0.write(items) } + return write { $0.writeWithMetadata(items) } } /** @@ -294,7 +294,7 @@ extension ConnectionType { - parameter queue: the dispatch_queue_t to run the completion block on. - parameter completion: a dispatch_block_t for completion. */ - public func asyncWrite< + public func asyncWriteWithMetadata< Value, Metadata where Value: Persistable, Value: ValueCoding, @@ -303,7 +303,7 @@ extension ConnectionType { Metadata: ValueCoding, Metadata.Coder: NSCoding, Metadata.Coder.ValueType == Metadata>(item: (Value, Metadata?), queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ((Value, Metadata?) -> Void)? = .None) { - asyncWrite({ $0.write(item) }, queue: queue, completion: completion) + asyncWrite({ $0.writeWithMetadata(item) }, queue: queue, completion: completion) } /** @@ -313,7 +313,7 @@ extension ConnectionType { - parameter queue: the dispatch_queue_t to run the completion block on. - parameter completion: a dispatch_block_t for completion. */ - public func asyncWrite< + public func asyncWriteWithMetadata< Items, Value, Metadata where Items: SequenceType, Items.Generator.Element == (Value, Metadata?), @@ -324,7 +324,7 @@ extension ConnectionType { Metadata: ValueCoding, Metadata.Coder: NSCoding, Metadata.Coder.ValueType == Metadata>(items: Items, queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ([(Value, Metadata?)] -> Void)? = .None) { - asyncWrite({ $0.write(items) }, queue: queue, completion: completion) + asyncWrite({ $0.writeWithMetadata(items) }, queue: queue, completion: completion) } } diff --git a/YapDatabaseExtensions/Shared/Persistable/Persistable_ObjectWithObjectMetadata.swift b/YapDatabaseExtensions/Shared/Persistable/Persistable_ObjectWithObjectMetadata.swift index a6c7180..95c0444 100644 --- a/YapDatabaseExtensions/Shared/Persistable/Persistable_ObjectWithObjectMetadata.swift +++ b/YapDatabaseExtensions/Shared/Persistable/Persistable_ObjectWithObjectMetadata.swift @@ -21,11 +21,11 @@ extension Persistable where - parameter transaction: a YapDatabaseReadWriteTransaction - returns: the receiver. */ - public func write< + public func writeWithMetadata< WriteTransaction, Metadata where WriteTransaction: WriteTransactionType, Metadata: NSCoding>(transaction: WriteTransaction, metadata: Metadata? = nil) -> (Self, Metadata?) { - return transaction.write((self, metadata)) + return transaction.writeWithMetadata((self, metadata)) } /** @@ -34,11 +34,11 @@ extension Persistable where - parameter connection: a YapDatabaseConnection - returns: the receiver. */ - public func write< + public func writeWithMetadata< Connection, Metadata where Connection: ConnectionType, Metadata: NSCoding>(connection: Connection, metadata: Metadata? = nil) -> (Self, Metadata?) { - return connection.write((self, metadata)) + return connection.writeWithMetadata((self, metadata)) } /** @@ -47,11 +47,11 @@ extension Persistable where - parameter connection: a YapDatabaseConnection - returns: a closure which receives as an argument the receiver of this function. */ - public func asyncWrite< + public func asyncWriteWithMetadata< Connection, Metadata where Connection: ConnectionType, Metadata: NSCoding>(connection: Connection, metadata: Metadata? = nil, queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ((Self, Metadata?) -> Void)? = .None) { - return connection.asyncWrite((self, metadata), queue: queue, completion: completion) + return connection.asyncWriteWithMetadata((self, metadata), queue: queue, completion: completion) } /** @@ -60,11 +60,11 @@ extension Persistable where - parameter connection: a YapDatabaseConnection - returns: an `NSOperation` */ - public func writeOperation< + public func writeWithMetadataOperation< Connection, Metadata where Connection: ConnectionType, Metadata: NSCoding>(connection: Connection, metadata: Metadata? = nil) -> NSOperation { - return NSBlockOperation { connection.write((self, metadata)) } + return NSBlockOperation { connection.writeWithMetadata((self, metadata)) } } } @@ -78,12 +78,12 @@ extension SequenceType where - parameter transaction: a WriteTransactionType e.g. YapDatabaseReadWriteTransaction - returns: the receiver. */ - public func write< + public func writeWithMetadata< WriteTransaction, Metadata where WriteTransaction: WriteTransactionType, Metadata: NSCoding>(transaction: WriteTransaction, metadata: [Metadata?] = []) -> [(Generator.Element, Metadata?)] { let items = zipToWrite(self, metadata) - return transaction.write(items) + return transaction.writeWithMetadata(items) } /** @@ -92,12 +92,12 @@ extension SequenceType where - parameter connection: a ConnectionType e.g. YapDatabaseConnection - returns: the receiver. */ - public func write< + public func writeWithMetadata< Connection, Metadata where Connection: ConnectionType, Metadata: NSCoding>(connection: Connection, metadata: [Metadata?] = []) -> [(Generator.Element, Metadata?)] { let items = zipToWrite(self, metadata) - return connection.write(items) + return connection.writeWithMetadata(items) } /** @@ -106,12 +106,12 @@ extension SequenceType where - parameter connection: a ConnectionType e.g. YapDatabaseConnection - returns: a closure which receives as an argument the receiver of this function. */ - public func asyncWrite< + public func asyncWriteWithMetadata< Connection, Metadata where Connection: ConnectionType, Metadata: NSCoding>(connection: Connection, metadata: [Metadata?] = [], queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ([(Generator.Element, Metadata?)] -> Void)? = .None) { let items = zipToWrite(self, metadata) - return connection.asyncWrite(items, queue: queue, completion: completion) + return connection.asyncWriteWithMetadata(items, queue: queue, completion: completion) } /** @@ -120,12 +120,12 @@ extension SequenceType where - parameter connection: a YapDatabaseConnection - returns: an `NSOperation` */ - public func writeOperation< + public func writeWithMetadataOperation< Connection, Metadata where Connection: ConnectionType, Metadata: NSCoding>(connection: Connection, metadata: [Metadata?] = []) -> NSOperation { let items = zipToWrite(self, metadata) - return NSBlockOperation { connection.write(items) } + return NSBlockOperation { connection.writeWithMetadata(items) } } } @@ -136,42 +136,42 @@ extension Readable where ItemType: NSCoding, ItemType: Persistable { - func inTransaction(transaction: Database.Connection.ReadTransaction, atIndex index: YapDB.Index) -> (ItemType, Metadata?)? { - return transaction.readAtIndex(index) + func withMetadataInTransaction(transaction: Database.Connection.ReadTransaction, atIndex index: YapDB.Index) -> (ItemType, Metadata?)? { + return transaction.readWithMetadataAtIndex(index) } - func inTransactionAtIndex(transaction: Database.Connection.ReadTransaction) -> YapDB.Index -> (ItemType, Metadata?)? { - return { self.inTransaction(transaction, atIndex: $0) } + func withMetadataInTransactionAtIndex(transaction: Database.Connection.ReadTransaction) -> YapDB.Index -> (ItemType, Metadata?)? { + return { self.withMetadataInTransaction(transaction, atIndex: $0) } } - func atIndexInTransaction(index: YapDB.Index) -> Database.Connection.ReadTransaction -> (ItemType, Metadata?)? { - return { self.inTransaction($0, atIndex: index) } + func withMetadataAtIndexInTransaction(index: YapDB.Index) -> Database.Connection.ReadTransaction -> (ItemType, Metadata?)? { + return { self.withMetadataInTransaction($0, atIndex: index) } } - func atIndexesInTransaction< + func withMetadataAtIndexesInTransaction< Indexes, Metadata where Indexes: SequenceType, Indexes.Generator.Element == YapDB.Index, Metadata: NSCoding>(indexes: Indexes) -> Database.Connection.ReadTransaction -> [(ItemType, Metadata?)] { - return { indexes.flatMap(self.inTransactionAtIndex($0)) } + return { indexes.flatMap(self.withMetadataInTransactionAtIndex($0)) } } - func inTransaction(transaction: Database.Connection.ReadTransaction, byKey key: String) -> (ItemType, Metadata?)? { - return inTransaction(transaction, atIndex: ItemType.indexWithKey(key)) + func withMetadataInTransaction(transaction: Database.Connection.ReadTransaction, byKey key: String) -> (ItemType, Metadata?)? { + return withMetadataInTransaction(transaction, atIndex: ItemType.indexWithKey(key)) } - func inTransactionByKey(transaction: Database.Connection.ReadTransaction) -> String -> (ItemType, Metadata?)? { - return { self.inTransaction(transaction, byKey: $0) } + func withMetadataInTransactionByKey(transaction: Database.Connection.ReadTransaction) -> String -> (ItemType, Metadata?)? { + return { self.withMetadataInTransaction(transaction, byKey: $0) } } - func byKeyInTransaction(key: String) -> Database.Connection.ReadTransaction -> (ItemType, Metadata?)? { - return { self.inTransaction($0, byKey: key) } + func withMetadataByKeyInTransaction(key: String) -> Database.Connection.ReadTransaction -> (ItemType, Metadata?)? { + return { self.withMetadataInTransaction($0, byKey: key) } } - func byKeysInTransaction(keys: [String]? = .None) -> Database.Connection.ReadTransaction -> [(ItemType, Metadata?)] { + func withMetadataByKeysInTransaction(keys: [String]? = .None) -> Database.Connection.ReadTransaction -> [(ItemType, Metadata?)] { return { transaction in let keys = keys ?? transaction.keysInCollection(ItemType.collection) - return keys.flatMap(self.inTransactionByKey(transaction)) + return keys.flatMap(self.withMetadataInTransactionByKey(transaction)) } } @@ -181,8 +181,8 @@ extension Readable where - parameter index: a YapDB.Index - returns: an optional `ItemType` */ - public func atIndex(index: YapDB.Index) -> (ItemType, Metadata?)? { - return sync(atIndexInTransaction(index)) + public func withMetadataAtIndex(index: YapDB.Index) -> (ItemType, Metadata?)? { + return sync(withMetadataAtIndexInTransaction(index)) } /** @@ -191,12 +191,12 @@ extension Readable where - parameter indexes: a SequenceType of YapDB.Index values - returns: an array of `ItemType` */ - public func atIndexes< + public func withMetadataAtIndexes< Indexes, Metadata where Indexes: SequenceType, Indexes.Generator.Element == YapDB.Index, Metadata: NSCoding>(indexes: Indexes) -> [(ItemType, Metadata?)] { - return sync(atIndexesInTransaction(indexes)) + return sync(withMetadataAtIndexesInTransaction(indexes)) } /** @@ -205,8 +205,8 @@ extension Readable where - parameter key: a String - returns: an optional `ItemType` */ - public func byKey(key: String) -> (ItemType, Metadata?)? { - return sync(byKeyInTransaction(key)) + public func withMetadataByKey(key: String) -> (ItemType, Metadata?)? { + return sync(withMetadataByKeyInTransaction(key)) } /** @@ -215,12 +215,12 @@ extension Readable where - parameter keys: a SequenceType of String values - returns: an array of `ItemType` */ - public func byKeys< + public func withMetadataByKeys< Keys, Metadata where Keys: SequenceType, Keys.Generator.Element == String, Metadata: NSCoding>(keys: Keys) -> [(ItemType, Metadata?)] { - return sync(byKeysInTransaction(Array(keys))) + return sync(withMetadataByKeysInTransaction(Array(keys))) } /** @@ -228,8 +228,8 @@ extension Readable where - returns: an array of `ItemType` */ - public func all() -> [(ItemType, Metadata?)] { - return sync(byKeysInTransaction()) + public func withMetadataAll() -> [(ItemType, Metadata?)] { + return sync(withMetadataByKeysInTransaction()) } /** @@ -238,8 +238,8 @@ extension Readable where - parameter keys: a SequenceType of String values - returns: a tuple of type `([ItemType], [String])` */ - public func filterExisting(keys: [String]) -> (existing: [(ItemType, Metadata?)], missing: [String]) { - let existingInTransaction: Database.Connection.ReadTransaction -> [(ItemType, Metadata?)] = byKeysInTransaction(keys) + public func withMetadataFilterExisting(keys: [String]) -> (existing: [(ItemType, Metadata?)], missing: [String]) { + let existingInTransaction: Database.Connection.ReadTransaction -> [(ItemType, Metadata?)] = withMetadataByKeysInTransaction(keys) return sync { transaction -> ([(ItemType, Metadata?)], [String]) in let existing = existingInTransaction(transaction) let existingKeys = existing.map {keyForPersistable($0.0)} diff --git a/YapDatabaseExtensions/Shared/Persistable/Persistable_ObjectWithValueMetadata.swift b/YapDatabaseExtensions/Shared/Persistable/Persistable_ObjectWithValueMetadata.swift index 7642ba8..7cff7de 100644 --- a/YapDatabaseExtensions/Shared/Persistable/Persistable_ObjectWithValueMetadata.swift +++ b/YapDatabaseExtensions/Shared/Persistable/Persistable_ObjectWithValueMetadata.swift @@ -22,13 +22,13 @@ extension Persistable where - parameter transaction: a YapDatabaseReadWriteTransaction - returns: the receiver. */ - public func write< + public func writeWithMetadata< WriteTransaction, Metadata where WriteTransaction: WriteTransactionType, Metadata: ValueCoding, Metadata.Coder: NSCoding, Metadata.Coder.ValueType == Metadata>(transaction: WriteTransaction, metadata: Metadata? = nil) -> (Self, Metadata?) { - return transaction.write((self, metadata)) + return transaction.writeWithMetadata((self, metadata)) } /** @@ -37,13 +37,13 @@ extension Persistable where - parameter connection: a YapDatabaseConnection - returns: the receiver. */ - public func write< + public func writeWithMetadata< Connection, Metadata where Connection: ConnectionType, Metadata: ValueCoding, Metadata.Coder: NSCoding, Metadata.Coder.ValueType == Metadata>(connection: Connection, metadata: Metadata? = nil) -> (Self, Metadata?) { - return connection.write((self, metadata)) + return connection.writeWithMetadata((self, metadata)) } /** @@ -52,13 +52,13 @@ extension Persistable where - parameter connection: a YapDatabaseConnection - returns: a closure which receives as an argument the receiver of this function. */ - public func asyncWrite< + public func asyncWriteWithMetadata< Connection, Metadata where Connection: ConnectionType, Metadata: ValueCoding, Metadata.Coder: NSCoding, Metadata.Coder.ValueType == Metadata>(connection: Connection, metadata: Metadata? = nil, queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ((Self, Metadata?) -> Void)? = .None) { - return connection.asyncWrite((self, metadata), queue: queue, completion: completion) + return connection.asyncWriteWithMetadata((self, metadata), queue: queue, completion: completion) } /** @@ -67,13 +67,13 @@ extension Persistable where - parameter connection: a YapDatabaseConnection - returns: an `NSOperation` */ - public func writeOperation< + public func writeWithMetadataOperation< Connection, Metadata where Connection: ConnectionType, Metadata: ValueCoding, Metadata.Coder: NSCoding, Metadata.Coder.ValueType == Metadata>(connection: Connection, metadata: Metadata? = nil) -> NSOperation { - return NSBlockOperation { connection.write((self, metadata)) } + return NSBlockOperation { connection.writeWithMetadata((self, metadata)) } } } @@ -87,14 +87,14 @@ extension SequenceType where - parameter transaction: a WriteTransactionType e.g. YapDatabaseReadWriteTransaction - returns: the receiver. */ - public func write< + public func writeWithMetadata< WriteTransaction, Metadata where WriteTransaction: WriteTransactionType, Metadata: ValueCoding, Metadata.Coder: NSCoding, Metadata.Coder.ValueType == Metadata>(transaction: WriteTransaction, metadata: [Metadata?] = []) -> [(Generator.Element, Metadata?)] { let items = zipToWrite(self, metadata) - return transaction.write(items) + return transaction.writeWithMetadata(items) } /** @@ -103,14 +103,14 @@ extension SequenceType where - parameter connection: a ConnectionType e.g. YapDatabaseConnection - returns: the receiver. */ - public func write< + public func writeWithMetadata< Connection, Metadata where Connection: ConnectionType, Metadata: ValueCoding, Metadata.Coder: NSCoding, Metadata.Coder.ValueType == Metadata>(connection: Connection, metadata: [Metadata?] = []) -> [(Generator.Element, Metadata?)] { let items = zipToWrite(self, metadata) - return connection.write(items) + return connection.writeWithMetadata(items) } /** @@ -119,14 +119,14 @@ extension SequenceType where - parameter connection: a ConnectionType e.g. YapDatabaseConnection - returns: a closure which receives as an argument the receiver of this function. */ - public func asyncWrite< + public func asyncWriteWithMetadata< Connection, Metadata where Connection: ConnectionType, Metadata: ValueCoding, Metadata.Coder: NSCoding, Metadata.Coder.ValueType == Metadata>(connection: Connection, metadata: [Metadata?] = [], queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ([(Generator.Element, Metadata?)] -> Void)? = .None) { let items = zipToWrite(self, metadata) - return connection.asyncWrite(items, queue: queue, completion: completion) + return connection.asyncWriteWithMetadata(items, queue: queue, completion: completion) } /** @@ -135,14 +135,14 @@ extension SequenceType where - parameter connection: a YapDatabaseConnection - returns: an `NSOperation` */ - public func writeOperation< + public func writeWithMetadataOperation< Connection, Metadata where Connection: ConnectionType, Metadata: ValueCoding, Metadata.Coder: NSCoding, Metadata.Coder.ValueType == Metadata>(connection: Connection, metadata: [Metadata?] = []) -> NSOperation { let items = zipToWrite(self, metadata) - return NSBlockOperation { connection.write(items) } + return NSBlockOperation { connection.writeWithMetadata(items) } } } @@ -152,72 +152,72 @@ extension Readable where ItemType: NSCoding, ItemType: Persistable { - func inTransaction< + func withMetadataInTransaction< Metadata where Metadata: ValueCoding, Metadata.Coder: NSCoding, Metadata.Coder.ValueType == Metadata>(transaction: Database.Connection.ReadTransaction, atIndex index: YapDB.Index) -> (ItemType, Metadata?)? { - return transaction.readAtIndex(index) + return transaction.readWithMetadataAtIndex(index) } - func inTransactionAtIndex< + func withMetadataInTransactionAtIndex< Metadata where Metadata: ValueCoding, Metadata.Coder: NSCoding, Metadata.Coder.ValueType == Metadata>(transaction: Database.Connection.ReadTransaction) -> YapDB.Index -> (ItemType, Metadata?)? { - return { self.inTransaction(transaction, atIndex: $0) } + return { self.withMetadataInTransaction(transaction, atIndex: $0) } } - func atIndexInTransaction< + func withMetadataAtIndexInTransaction< Metadata where Metadata: ValueCoding, Metadata.Coder: NSCoding, Metadata.Coder.ValueType == Metadata>(index: YapDB.Index) -> Database.Connection.ReadTransaction -> (ItemType, Metadata?)? { - return { self.inTransaction($0, atIndex: index) } + return { self.withMetadataInTransaction($0, atIndex: index) } } - func atIndexesInTransaction< + func withMetadataAtIndexesInTransaction< Indexes, Metadata where Indexes: SequenceType, Indexes.Generator.Element == YapDB.Index, Metadata: ValueCoding, Metadata.Coder: NSCoding, Metadata.Coder.ValueType == Metadata>(indexes: Indexes) -> Database.Connection.ReadTransaction -> [(ItemType, Metadata?)] { - return { indexes.flatMap(self.inTransactionAtIndex($0)) } + return { indexes.flatMap(self.withMetadataInTransactionAtIndex($0)) } } - func inTransaction< + func withMetadataInTransaction< Metadata where Metadata: ValueCoding, Metadata.Coder: NSCoding, Metadata.Coder.ValueType == Metadata>(transaction: Database.Connection.ReadTransaction, byKey key: String) -> (ItemType, Metadata?)? { - return inTransaction(transaction, atIndex: ItemType.indexWithKey(key)) + return withMetadataInTransaction(transaction, atIndex: ItemType.indexWithKey(key)) } - func inTransactionByKey< + func withMetadataInTransactionByKey< Metadata where Metadata: ValueCoding, Metadata.Coder: NSCoding, Metadata.Coder.ValueType == Metadata>(transaction: Database.Connection.ReadTransaction) -> String -> (ItemType, Metadata?)? { - return { self.inTransaction(transaction, byKey: $0) } + return { self.withMetadataInTransaction(transaction, byKey: $0) } } - func byKeyInTransaction< + func withMetadataByKeyInTransaction< Metadata where Metadata: ValueCoding, Metadata.Coder: NSCoding, Metadata.Coder.ValueType == Metadata>(key: String) -> Database.Connection.ReadTransaction -> (ItemType, Metadata?)? { - return { self.inTransaction($0, byKey: key) } + return { self.withMetadataInTransaction($0, byKey: key) } } - func byKeysInTransaction< + func withMetadataByKeysInTransaction< Metadata where Metadata: ValueCoding, Metadata.Coder: NSCoding, Metadata.Coder.ValueType == Metadata>(keys: [String]? = .None) -> Database.Connection.ReadTransaction -> [(ItemType, Metadata?)] { return { transaction in let keys = keys ?? transaction.keysInCollection(ItemType.collection) - return keys.flatMap(self.inTransactionByKey(transaction)) + return keys.flatMap(self.withMetadataInTransactionByKey(transaction)) } } @@ -227,12 +227,12 @@ extension Readable where - parameter index: a YapDB.Index - returns: an optional `ItemType` */ - public func atIndex< + public func withMetadataAtIndex< Metadata where Metadata: ValueCoding, Metadata.Coder: NSCoding, Metadata.Coder.ValueType == Metadata>(index: YapDB.Index) -> (ItemType, Metadata?)? { - return sync(atIndexInTransaction(index)) + return sync(withMetadataAtIndexInTransaction(index)) } /** @@ -241,14 +241,14 @@ extension Readable where - parameter indexes: a SequenceType of YapDB.Index values - returns: an array of `ItemType` */ - public func atIndexes< + public func withMetadataAtIndexes< Indexes, Metadata where Indexes: SequenceType, Indexes.Generator.Element == YapDB.Index, Metadata: ValueCoding, Metadata.Coder: NSCoding, Metadata.Coder.ValueType == Metadata>(indexes: Indexes) -> [(ItemType, Metadata?)] { - return sync(atIndexesInTransaction(indexes)) + return sync(withMetadataAtIndexesInTransaction(indexes)) } /** @@ -257,12 +257,12 @@ extension Readable where - parameter key: a String - returns: an optional `ItemType` */ - public func byKey< + public func withMetadataByKey< Metadata where Metadata: ValueCoding, Metadata.Coder: NSCoding, Metadata.Coder.ValueType == Metadata>(key: String) -> (ItemType, Metadata?)? { - return sync(byKeyInTransaction(key)) + return sync(withMetadataByKeyInTransaction(key)) } /** @@ -271,14 +271,14 @@ extension Readable where - parameter keys: a SequenceType of String values - returns: an array of `ItemType` */ - public func byKeys< + public func withMetadataByKeys< Keys, Metadata where Keys: SequenceType, Keys.Generator.Element == String, Metadata: ValueCoding, Metadata.Coder: NSCoding, Metadata.Coder.ValueType == Metadata>(keys: Keys) -> [(ItemType, Metadata?)] { - return sync(byKeysInTransaction(Array(keys))) + return sync(withMetadataByKeysInTransaction(Array(keys))) } /** @@ -286,12 +286,12 @@ extension Readable where - returns: an array of `ItemType` */ - public func all< + public func withMetadataAll< Metadata where Metadata: ValueCoding, Metadata.Coder: NSCoding, Metadata.Coder.ValueType == Metadata>() -> [(ItemType, Metadata?)] { - return sync(byKeysInTransaction()) + return sync(withMetadataByKeysInTransaction()) } /** @@ -300,12 +300,12 @@ extension Readable where - parameter keys: a SequenceType of String values - returns: a tuple of type `([ItemType], [String])` */ - public func filterExisting< + public func withMetadataFilterExisting< Metadata where Metadata: ValueCoding, Metadata.Coder: NSCoding, Metadata.Coder.ValueType == Metadata>(keys: [String]) -> (existing: [(ItemType, Metadata?)], missing: [String]) { - let existingInTransaction: Database.Connection.ReadTransaction -> [(ItemType, Metadata?)] = byKeysInTransaction(keys) + let existingInTransaction: Database.Connection.ReadTransaction -> [(ItemType, Metadata?)] = withMetadataByKeysInTransaction(keys) return sync { transaction -> ([(ItemType, Metadata?)], [String]) in let existing = existingInTransaction(transaction) let existingKeys = existing.map {keyForPersistable($0.0)} diff --git a/YapDatabaseExtensions/Shared/Persistable/Persistable_ValueWithObjectMetadata.swift b/YapDatabaseExtensions/Shared/Persistable/Persistable_ValueWithObjectMetadata.swift index 9d2703c..0533da6 100644 --- a/YapDatabaseExtensions/Shared/Persistable/Persistable_ValueWithObjectMetadata.swift +++ b/YapDatabaseExtensions/Shared/Persistable/Persistable_ValueWithObjectMetadata.swift @@ -24,11 +24,11 @@ extension Persistable where - parameter transaction: a YapDatabaseReadWriteTransaction - returns: the receiver. */ - public func write< + public func writeWithMetadata< WriteTransaction, Metadata where WriteTransaction: WriteTransactionType, Metadata: NSCoding>(transaction: WriteTransaction, metadata: Metadata? = nil) -> (Self, Metadata?) { - return transaction.write((self, metadata)) + return transaction.writeWithMetadata((self, metadata)) } /** @@ -37,11 +37,11 @@ extension Persistable where - parameter connection: a YapDatabaseConnection - returns: the receiver. */ - public func write< + public func writeWithMetadata< Connection, Metadata where Connection: ConnectionType, Metadata: NSCoding>(connection: Connection, metadata: Metadata? = nil) -> (Self, Metadata?) { - return connection.write((self, metadata)) + return connection.writeWithMetadata((self, metadata)) } /** @@ -50,11 +50,11 @@ extension Persistable where - parameter connection: a YapDatabaseConnection - returns: a closure which receives as an argument the receiver of this function. */ - public func asyncWrite< + public func asyncWriteWithMetadata< Connection, Metadata where Connection: ConnectionType, Metadata: NSCoding>(connection: Connection, metadata: Metadata? = nil, queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ((Self, Metadata?) -> Void)? = .None) { - return connection.asyncWrite((self, metadata), queue: queue, completion: completion) + return connection.asyncWriteWithMetadata((self, metadata), queue: queue, completion: completion) } /** @@ -63,11 +63,11 @@ extension Persistable where - parameter connection: a YapDatabaseConnection - returns: an `NSOperation` */ - public func writeOperation< + public func writeWithMetadataOperation< Connection, Metadata where Connection: ConnectionType, Metadata: NSCoding>(connection: Connection, metadata: Metadata? = nil) -> NSOperation { - return NSBlockOperation { connection.write((self, metadata)) } + return NSBlockOperation { connection.writeWithMetadata((self, metadata)) } } } @@ -83,12 +83,12 @@ extension SequenceType where - parameter transaction: a WriteTransactionType e.g. YapDatabaseReadWriteTransaction - returns: the receiver. */ - public func write< + public func writeWithMetadata< WriteTransaction, Metadata where WriteTransaction: WriteTransactionType, Metadata: NSCoding>(transaction: WriteTransaction, metadata: [Metadata?] = []) -> [(Generator.Element, Metadata?)] { let items = zipToWrite(self, metadata) - return transaction.write(items) + return transaction.writeWithMetadata(items) } /** @@ -97,12 +97,12 @@ extension SequenceType where - parameter connection: a ConnectionType e.g. YapDatabaseConnection - returns: the receiver. */ - public func write< + public func writeWithMetadata< Connection, Metadata where Connection: ConnectionType, Metadata: NSCoding>(connection: Connection, metadata: [Metadata?] = []) -> [(Generator.Element, Metadata?)] { let items = zipToWrite(self, metadata) - return connection.write(items) + return connection.writeWithMetadata(items) } /** @@ -111,12 +111,12 @@ extension SequenceType where - parameter connection: a ConnectionType e.g. YapDatabaseConnection - returns: a closure which receives as an argument the receiver of this function. */ - public func asyncWrite< + public func asyncWriteWithMetadata< Connection, Metadata where Connection: ConnectionType, Metadata: NSCoding>(connection: Connection, metadata: [Metadata?] = [], queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ([(Generator.Element, Metadata?)] -> Void)? = .None) { let items = zipToWrite(self, metadata) - return connection.asyncWrite(items, queue: queue, completion: completion) + return connection.asyncWriteWithMetadata(items, queue: queue, completion: completion) } /** @@ -125,12 +125,12 @@ extension SequenceType where - parameter connection: a YapDatabaseConnection - returns: an `NSOperation` */ - public func writeOperation< + public func writeWithMetadataOperation< Connection, Metadata where Connection: ConnectionType, Metadata: NSCoding>(connection: Connection, metadata: [Metadata?] = []) -> NSOperation { let items = zipToWrite(self, metadata) - return NSBlockOperation { connection.write(items) } + return NSBlockOperation { connection.writeWithMetadata(items) } } } @@ -143,42 +143,42 @@ extension Readable where ItemType.Coder: NSCoding, ItemType.Coder.ValueType == ItemType { - func inTransaction(transaction: Database.Connection.ReadTransaction, atIndex index: YapDB.Index) -> (ItemType, Metadata?)? { - return transaction.readAtIndex(index) + func withMetadataInTransaction(transaction: Database.Connection.ReadTransaction, atIndex index: YapDB.Index) -> (ItemType, Metadata?)? { + return transaction.readWithMetadataAtIndex(index) } - func inTransactionAtIndex(transaction: Database.Connection.ReadTransaction) -> YapDB.Index -> (ItemType, Metadata?)? { - return { self.inTransaction(transaction, atIndex: $0) } + func withMetadataInTransactionAtIndex(transaction: Database.Connection.ReadTransaction) -> YapDB.Index -> (ItemType, Metadata?)? { + return { self.withMetadataInTransaction(transaction, atIndex: $0) } } - func atIndexInTransaction(index: YapDB.Index) -> Database.Connection.ReadTransaction -> (ItemType, Metadata?)? { - return { self.inTransaction($0, atIndex: index) } + func withMetadataAtIndexInTransaction(index: YapDB.Index) -> Database.Connection.ReadTransaction -> (ItemType, Metadata?)? { + return { self.withMetadataInTransaction($0, atIndex: index) } } - func atIndexesInTransaction< + func withMetadataAtIndexesInTransaction< Indexes, Metadata where Indexes: SequenceType, Indexes.Generator.Element == YapDB.Index, Metadata: NSCoding>(indexes: Indexes) -> Database.Connection.ReadTransaction -> [(ItemType, Metadata?)] { - return { indexes.flatMap(self.inTransactionAtIndex($0)) } + return { indexes.flatMap(self.withMetadataInTransactionAtIndex($0)) } } - func inTransaction(transaction: Database.Connection.ReadTransaction, byKey key: String) -> (ItemType, Metadata?)? { - return inTransaction(transaction, atIndex: ItemType.indexWithKey(key)) + func withMetadataInTransaction(transaction: Database.Connection.ReadTransaction, byKey key: String) -> (ItemType, Metadata?)? { + return withMetadataInTransaction(transaction, atIndex: ItemType.indexWithKey(key)) } - func inTransactionByKey(transaction: Database.Connection.ReadTransaction) -> String -> (ItemType, Metadata?)? { - return { self.inTransaction(transaction, byKey: $0) } + func withMetadataInTransactionByKey(transaction: Database.Connection.ReadTransaction) -> String -> (ItemType, Metadata?)? { + return { self.withMetadataInTransaction(transaction, byKey: $0) } } - func byKeyInTransaction(key: String) -> Database.Connection.ReadTransaction -> (ItemType, Metadata?)? { - return { self.inTransaction($0, byKey: key) } + func withMetadataByKeyInTransaction(key: String) -> Database.Connection.ReadTransaction -> (ItemType, Metadata?)? { + return { self.withMetadataInTransaction($0, byKey: key) } } - func byKeysInTransaction(keys: [String]? = .None) -> Database.Connection.ReadTransaction -> [(ItemType, Metadata?)] { + func withMetadataByKeysInTransaction(keys: [String]? = .None) -> Database.Connection.ReadTransaction -> [(ItemType, Metadata?)] { return { transaction in let keys = keys ?? transaction.keysInCollection(ItemType.collection) - return keys.flatMap(self.inTransactionByKey(transaction)) + return keys.flatMap(self.withMetadataInTransactionByKey(transaction)) } } @@ -188,8 +188,8 @@ extension Readable where - parameter index: a YapDB.Index - returns: an optional `ItemType` */ - public func atIndex(index: YapDB.Index) -> (ItemType, Metadata?)? { - return sync(atIndexInTransaction(index)) + public func withMetadataAtIndex(index: YapDB.Index) -> (ItemType, Metadata?)? { + return sync(withMetadataAtIndexInTransaction(index)) } /** @@ -198,12 +198,12 @@ extension Readable where - parameter indexes: a SequenceType of YapDB.Index values - returns: an array of `ItemType` */ - public func atIndexes< + public func withMetadataAtIndexes< Indexes, Metadata where Indexes: SequenceType, Indexes.Generator.Element == YapDB.Index, Metadata: NSCoding>(indexes: Indexes) -> [(ItemType, Metadata?)] { - return sync(atIndexesInTransaction(indexes)) + return sync(withMetadataAtIndexesInTransaction(indexes)) } /** @@ -212,8 +212,8 @@ extension Readable where - parameter key: a String - returns: an optional `ItemType` */ - public func byKey(key: String) -> (ItemType, Metadata?)? { - return sync(byKeyInTransaction(key)) + public func withMetadataByKey(key: String) -> (ItemType, Metadata?)? { + return sync(withMetadataByKeyInTransaction(key)) } /** @@ -222,12 +222,12 @@ extension Readable where - parameter keys: a SequenceType of String values - returns: an array of `ItemType` */ - public func byKeys< + public func withMetadataByKeys< Keys, Metadata where Keys: SequenceType, Keys.Generator.Element == String, Metadata: NSCoding>(keys: Keys) -> [(ItemType, Metadata?)] { - return sync(byKeysInTransaction(Array(keys))) + return sync(withMetadataByKeysInTransaction(Array(keys))) } /** @@ -235,8 +235,8 @@ extension Readable where - returns: an array of `ItemType` */ - public func all() -> [(ItemType, Metadata?)] { - return sync(byKeysInTransaction()) + public func withMetadataAll() -> [(ItemType, Metadata?)] { + return sync(withMetadataByKeysInTransaction()) } /** @@ -245,8 +245,8 @@ extension Readable where - parameter keys: a SequenceType of String values - returns: a tuple of type `([ItemType], [String])` */ - public func filterExisting(keys: [String]) -> (existing: [(ItemType, Metadata?)], missing: [String]) { - let existingInTransaction: Database.Connection.ReadTransaction -> [(ItemType, Metadata?)] = byKeysInTransaction(keys) + public func withMetadataFilterExisting(keys: [String]) -> (existing: [(ItemType, Metadata?)], missing: [String]) { + let existingInTransaction: Database.Connection.ReadTransaction -> [(ItemType, Metadata?)] = withMetadataByKeysInTransaction(keys) return sync { transaction -> ([(ItemType, Metadata?)], [String]) in let existing = existingInTransaction(transaction) let existingKeys = existing.map {keyForPersistable($0.0)} diff --git a/YapDatabaseExtensions/Shared/Persistable/Persistable_ValueWithValueMetadata.swift b/YapDatabaseExtensions/Shared/Persistable/Persistable_ValueWithValueMetadata.swift index db778c1..a651ef5 100644 --- a/YapDatabaseExtensions/Shared/Persistable/Persistable_ValueWithValueMetadata.swift +++ b/YapDatabaseExtensions/Shared/Persistable/Persistable_ValueWithValueMetadata.swift @@ -24,13 +24,13 @@ extension Persistable where - parameter transaction: a YapDatabaseReadWriteTransaction - returns: the receiver. */ - public func write< + public func writeWithMetadata< WriteTransaction, Metadata where WriteTransaction: WriteTransactionType, Metadata: ValueCoding, Metadata.Coder: NSCoding, Metadata.Coder.ValueType == Metadata>(transaction: WriteTransaction, metadata: Metadata? = nil) -> (Self, Metadata?) { - return transaction.write((self, metadata)) + return transaction.writeWithMetadata((self, metadata)) } /** @@ -39,13 +39,13 @@ extension Persistable where - parameter connection: a YapDatabaseConnection - returns: the receiver. */ - public func write< + public func writeWithMetadata< Connection, Metadata where Connection: ConnectionType, Metadata: ValueCoding, Metadata.Coder: NSCoding, Metadata.Coder.ValueType == Metadata>(connection: Connection, metadata: Metadata? = nil) -> (Self, Metadata?) { - return connection.write((self, metadata)) + return connection.writeWithMetadata((self, metadata)) } /** @@ -54,13 +54,13 @@ extension Persistable where - parameter connection: a YapDatabaseConnection - returns: a closure which receives as an argument the receiver of this function. */ - public func asyncWrite< + public func asyncWriteWithMetadata< Connection, Metadata where Connection: ConnectionType, Metadata: ValueCoding, Metadata.Coder: NSCoding, Metadata.Coder.ValueType == Metadata>(connection: Connection, metadata: Metadata? = nil, queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ((Self, Metadata?) -> Void)? = .None) { - return connection.asyncWrite((self, metadata), queue: queue, completion: completion) + return connection.asyncWriteWithMetadata((self, metadata), queue: queue, completion: completion) } /** @@ -69,13 +69,13 @@ extension Persistable where - parameter connection: a YapDatabaseConnection - returns: an `NSOperation` */ - public func writeOperation< + public func writeWithMetadataOperation< Connection, Metadata where Connection: ConnectionType, Metadata: ValueCoding, Metadata.Coder: NSCoding, Metadata.Coder.ValueType == Metadata>(connection: Connection, metadata: Metadata? = nil) -> NSOperation { - return NSBlockOperation { connection.write((self, metadata)) } + return NSBlockOperation { connection.writeWithMetadata((self, metadata)) } } } @@ -91,14 +91,14 @@ extension SequenceType where - parameter transaction: a WriteTransactionType e.g. YapDatabaseReadWriteTransaction - returns: the receiver. */ - public func write< + public func writeWithMetadata< WriteTransaction, Metadata where WriteTransaction: WriteTransactionType, Metadata: ValueCoding, Metadata.Coder: NSCoding, Metadata.Coder.ValueType == Metadata>(transaction: WriteTransaction, metadata: [Metadata?] = []) -> [(Generator.Element, Metadata?)] { let items = zipToWrite(self, metadata) - return transaction.write(items) + return transaction.writeWithMetadata(items) } /** @@ -107,14 +107,14 @@ extension SequenceType where - parameter connection: a ConnectionType e.g. YapDatabaseConnection - returns: the receiver. */ - public func write< + public func writeWithMetadata< Connection, Metadata where Connection: ConnectionType, Metadata: ValueCoding, Metadata.Coder: NSCoding, Metadata.Coder.ValueType == Metadata>(connection: Connection, metadata: [Metadata?] = []) -> [(Generator.Element, Metadata?)] { let items = zipToWrite(self, metadata) - return connection.write(items) + return connection.writeWithMetadata(items) } /** @@ -123,14 +123,14 @@ extension SequenceType where - parameter connection: a ConnectionType e.g. YapDatabaseConnection - returns: a closure which receives as an argument the receiver of this function. */ - public func asyncWrite< + public func asyncWriteWithMetadata< Connection, Metadata where Connection: ConnectionType, Metadata: ValueCoding, Metadata.Coder: NSCoding, Metadata.Coder.ValueType == Metadata>(connection: Connection, metadata: [Metadata?] = [], queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ([(Generator.Element, Metadata?)] -> Void)? = .None) { let items = zipToWrite(self, metadata) - return connection.asyncWrite(items, queue: queue, completion: completion) + return connection.asyncWriteWithMetadata(items, queue: queue, completion: completion) } /** @@ -139,14 +139,14 @@ extension SequenceType where - parameter connection: a YapDatabaseConnection - returns: an `NSOperation` */ - public func writeOperation< + public func writeWithMetadataOperation< Connection, Metadata where Connection: ConnectionType, Metadata: ValueCoding, Metadata.Coder: NSCoding, Metadata.Coder.ValueType == Metadata>(connection: Connection, metadata: [Metadata?] = []) -> NSOperation { let items = zipToWrite(self, metadata) - return NSBlockOperation { connection.write(items) } + return NSBlockOperation { connection.writeWithMetadata(items) } } } @@ -158,72 +158,72 @@ extension Readable where ItemType.Coder: NSCoding, ItemType.Coder.ValueType == ItemType { - func inTransaction< + func withMetadataInTransaction< Metadata where Metadata: ValueCoding, Metadata.Coder: NSCoding, Metadata.Coder.ValueType == Metadata>(transaction: Database.Connection.ReadTransaction, atIndex index: YapDB.Index) -> (ItemType, Metadata?)? { - return transaction.readAtIndex(index) + return transaction.readWithMetadataAtIndex(index) } - func inTransactionAtIndex< + func withMetadataInTransactionAtIndex< Metadata where Metadata: ValueCoding, Metadata.Coder: NSCoding, Metadata.Coder.ValueType == Metadata>(transaction: Database.Connection.ReadTransaction) -> YapDB.Index -> (ItemType, Metadata?)? { - return { self.inTransaction(transaction, atIndex: $0) } + return { self.withMetadataInTransaction(transaction, atIndex: $0) } } - func atIndexInTransaction< + func withMetadataAtIndexInTransaction< Metadata where Metadata: ValueCoding, Metadata.Coder: NSCoding, Metadata.Coder.ValueType == Metadata>(index: YapDB.Index) -> Database.Connection.ReadTransaction -> (ItemType, Metadata?)? { - return { self.inTransaction($0, atIndex: index) } + return { self.withMetadataInTransaction($0, atIndex: index) } } - func atIndexesInTransaction< + func withMetadataAtIndexesInTransaction< Indexes, Metadata where Indexes: SequenceType, Indexes.Generator.Element == YapDB.Index, Metadata: ValueCoding, Metadata.Coder: NSCoding, Metadata.Coder.ValueType == Metadata>(indexes: Indexes) -> Database.Connection.ReadTransaction -> [(ItemType, Metadata?)] { - return { indexes.flatMap(self.inTransactionAtIndex($0)) } + return { indexes.flatMap(self.withMetadataInTransactionAtIndex($0)) } } - func inTransaction< + func withMetadataInTransaction< Metadata where Metadata: ValueCoding, Metadata.Coder: NSCoding, Metadata.Coder.ValueType == Metadata>(transaction: Database.Connection.ReadTransaction, byKey key: String) -> (ItemType, Metadata?)? { - return inTransaction(transaction, atIndex: ItemType.indexWithKey(key)) + return withMetadataInTransaction(transaction, atIndex: ItemType.indexWithKey(key)) } - func inTransactionByKey< + func withMetadataInTransactionByKey< Metadata where Metadata: ValueCoding, Metadata.Coder: NSCoding, Metadata.Coder.ValueType == Metadata>(transaction: Database.Connection.ReadTransaction) -> String -> (ItemType, Metadata?)? { - return { self.inTransaction(transaction, byKey: $0) } + return { self.withMetadataInTransaction(transaction, byKey: $0) } } - func byKeyInTransaction< + func withMetadataByKeyInTransaction< Metadata where Metadata: ValueCoding, Metadata.Coder: NSCoding, Metadata.Coder.ValueType == Metadata>(key: String) -> Database.Connection.ReadTransaction -> (ItemType, Metadata?)? { - return { self.inTransaction($0, byKey: key) } + return { self.withMetadataInTransaction($0, byKey: key) } } - func byKeysInTransaction< + func withMetadataByKeysInTransaction< Metadata where Metadata: ValueCoding, Metadata.Coder: NSCoding, Metadata.Coder.ValueType == Metadata>(keys: [String]? = .None) -> Database.Connection.ReadTransaction -> [(ItemType, Metadata?)] { return { transaction in let keys = keys ?? transaction.keysInCollection(ItemType.collection) - return keys.flatMap(self.inTransactionByKey(transaction)) + return keys.flatMap(self.withMetadataInTransactionByKey(transaction)) } } @@ -233,12 +233,12 @@ extension Readable where - parameter index: a YapDB.Index - returns: an optional `ItemType` */ - public func atIndex< + public func withMetadataAtIndex< Metadata where Metadata: ValueCoding, Metadata.Coder: NSCoding, Metadata.Coder.ValueType == Metadata>(index: YapDB.Index) -> (ItemType, Metadata?)? { - return sync(atIndexInTransaction(index)) + return sync(withMetadataAtIndexInTransaction(index)) } /** @@ -247,14 +247,14 @@ extension Readable where - parameter indexes: a SequenceType of YapDB.Index values - returns: an array of `ItemType` */ - public func atIndexes< + public func withMetadataAtIndexes< Indexes, Metadata where Indexes: SequenceType, Indexes.Generator.Element == YapDB.Index, Metadata: ValueCoding, Metadata.Coder: NSCoding, Metadata.Coder.ValueType == Metadata>(indexes: Indexes) -> [(ItemType, Metadata?)] { - return sync(atIndexesInTransaction(indexes)) + return sync(withMetadataAtIndexesInTransaction(indexes)) } /** @@ -263,12 +263,12 @@ extension Readable where - parameter key: a String - returns: an optional `ItemType` */ - public func byKey< + public func withMetadataByKey< Metadata where Metadata: ValueCoding, Metadata.Coder: NSCoding, Metadata.Coder.ValueType == Metadata>(key: String) -> (ItemType, Metadata?)? { - return sync(byKeyInTransaction(key)) + return sync(withMetadataByKeyInTransaction(key)) } /** @@ -277,14 +277,14 @@ extension Readable where - parameter keys: a SequenceType of String values - returns: an array of `ItemType` */ - public func byKeys< + public func withMetadataByKeys< Keys, Metadata where Keys: SequenceType, Keys.Generator.Element == String, Metadata: ValueCoding, Metadata.Coder: NSCoding, Metadata.Coder.ValueType == Metadata>(keys: Keys) -> [(ItemType, Metadata?)] { - return sync(byKeysInTransaction(Array(keys))) + return sync(withMetadataByKeysInTransaction(Array(keys))) } /** @@ -292,12 +292,12 @@ extension Readable where - returns: an array of `ItemType` */ - public func all< + public func withMetadataAll< Metadata where Metadata: ValueCoding, Metadata.Coder: NSCoding, Metadata.Coder.ValueType == Metadata>() -> [(ItemType, Metadata?)] { - return sync(byKeysInTransaction()) + return sync(withMetadataByKeysInTransaction()) } /** @@ -306,12 +306,12 @@ extension Readable where - parameter keys: a SequenceType of String values - returns: a tuple of type `([ItemType], [String])` */ - public func filterExisting< + public func withMetadataFilterExisting< Metadata where Metadata: ValueCoding, Metadata.Coder: NSCoding, Metadata.Coder.ValueType == Metadata>(keys: [String]) -> (existing: [(ItemType, Metadata?)], missing: [String]) { - let existingInTransaction: Database.Connection.ReadTransaction -> [(ItemType, Metadata?)] = byKeysInTransaction(keys) + let existingInTransaction: Database.Connection.ReadTransaction -> [(ItemType, Metadata?)] = withMetadataByKeysInTransaction(keys) return sync { transaction -> ([(ItemType, Metadata?)], [String]) in let existing = existingInTransaction(transaction) let existingKeys = existing.map {keyForPersistable($0.0)} From b01270936bd6b0d8a3aa2adca774d680437278e0 Mon Sep 17 00:00:00 2001 From: Jim Roepcke Date: Sat, 10 Sep 2016 13:48:17 -0700 Subject: [PATCH 11/14] remove default values for metadata arguments --- .../Persistable_ObjectWithObjectMetadata.swift | 16 ++++++++-------- .../Persistable_ObjectWithValueMetadata.swift | 16 ++++++++-------- .../Persistable_ValueWithObjectMetadata.swift | 16 ++++++++-------- .../Persistable_ValueWithValueMetadata.swift | 16 ++++++++-------- 4 files changed, 32 insertions(+), 32 deletions(-) diff --git a/YapDatabaseExtensions/Shared/Persistable/Persistable_ObjectWithObjectMetadata.swift b/YapDatabaseExtensions/Shared/Persistable/Persistable_ObjectWithObjectMetadata.swift index 95c0444..0d86004 100644 --- a/YapDatabaseExtensions/Shared/Persistable/Persistable_ObjectWithObjectMetadata.swift +++ b/YapDatabaseExtensions/Shared/Persistable/Persistable_ObjectWithObjectMetadata.swift @@ -24,7 +24,7 @@ extension Persistable where public func writeWithMetadata< WriteTransaction, Metadata where WriteTransaction: WriteTransactionType, - Metadata: NSCoding>(transaction: WriteTransaction, metadata: Metadata? = nil) -> (Self, Metadata?) { + Metadata: NSCoding>(transaction: WriteTransaction, metadata: Metadata?) -> (Self, Metadata?) { return transaction.writeWithMetadata((self, metadata)) } @@ -37,7 +37,7 @@ extension Persistable where public func writeWithMetadata< Connection, Metadata where Connection: ConnectionType, - Metadata: NSCoding>(connection: Connection, metadata: Metadata? = nil) -> (Self, Metadata?) { + Metadata: NSCoding>(connection: Connection, metadata: Metadata?) -> (Self, Metadata?) { return connection.writeWithMetadata((self, metadata)) } @@ -50,7 +50,7 @@ extension Persistable where public func asyncWriteWithMetadata< Connection, Metadata where Connection: ConnectionType, - Metadata: NSCoding>(connection: Connection, metadata: Metadata? = nil, queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ((Self, Metadata?) -> Void)? = .None) { + Metadata: NSCoding>(connection: Connection, metadata: Metadata?, queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ((Self, Metadata?) -> Void)? = .None) { return connection.asyncWriteWithMetadata((self, metadata), queue: queue, completion: completion) } @@ -63,7 +63,7 @@ extension Persistable where public func writeWithMetadataOperation< Connection, Metadata where Connection: ConnectionType, - Metadata: NSCoding>(connection: Connection, metadata: Metadata? = nil) -> NSOperation { + Metadata: NSCoding>(connection: Connection, metadata: Metadata?) -> NSOperation { return NSBlockOperation { connection.writeWithMetadata((self, metadata)) } } } @@ -81,7 +81,7 @@ extension SequenceType where public func writeWithMetadata< WriteTransaction, Metadata where WriteTransaction: WriteTransactionType, - Metadata: NSCoding>(transaction: WriteTransaction, metadata: [Metadata?] = []) -> [(Generator.Element, Metadata?)] { + Metadata: NSCoding>(transaction: WriteTransaction, metadata: [Metadata?]) -> [(Generator.Element, Metadata?)] { let items = zipToWrite(self, metadata) return transaction.writeWithMetadata(items) } @@ -95,7 +95,7 @@ extension SequenceType where public func writeWithMetadata< Connection, Metadata where Connection: ConnectionType, - Metadata: NSCoding>(connection: Connection, metadata: [Metadata?] = []) -> [(Generator.Element, Metadata?)] { + Metadata: NSCoding>(connection: Connection, metadata: [Metadata?]) -> [(Generator.Element, Metadata?)] { let items = zipToWrite(self, metadata) return connection.writeWithMetadata(items) } @@ -109,7 +109,7 @@ extension SequenceType where public func asyncWriteWithMetadata< Connection, Metadata where Connection: ConnectionType, - Metadata: NSCoding>(connection: Connection, metadata: [Metadata?] = [], queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ([(Generator.Element, Metadata?)] -> Void)? = .None) { + Metadata: NSCoding>(connection: Connection, metadata: [Metadata?], queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ([(Generator.Element, Metadata?)] -> Void)? = .None) { let items = zipToWrite(self, metadata) return connection.asyncWriteWithMetadata(items, queue: queue, completion: completion) } @@ -123,7 +123,7 @@ extension SequenceType where public func writeWithMetadataOperation< Connection, Metadata where Connection: ConnectionType, - Metadata: NSCoding>(connection: Connection, metadata: [Metadata?] = []) -> NSOperation { + Metadata: NSCoding>(connection: Connection, metadata: [Metadata?]) -> NSOperation { let items = zipToWrite(self, metadata) return NSBlockOperation { connection.writeWithMetadata(items) } } diff --git a/YapDatabaseExtensions/Shared/Persistable/Persistable_ObjectWithValueMetadata.swift b/YapDatabaseExtensions/Shared/Persistable/Persistable_ObjectWithValueMetadata.swift index 7cff7de..06a9570 100644 --- a/YapDatabaseExtensions/Shared/Persistable/Persistable_ObjectWithValueMetadata.swift +++ b/YapDatabaseExtensions/Shared/Persistable/Persistable_ObjectWithValueMetadata.swift @@ -27,7 +27,7 @@ extension Persistable where WriteTransaction: WriteTransactionType, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(transaction: WriteTransaction, metadata: Metadata? = nil) -> (Self, Metadata?) { + Metadata.Coder.ValueType == Metadata>(transaction: WriteTransaction, metadata: Metadata?) -> (Self, Metadata?) { return transaction.writeWithMetadata((self, metadata)) } @@ -42,7 +42,7 @@ extension Persistable where Connection: ConnectionType, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(connection: Connection, metadata: Metadata? = nil) -> (Self, Metadata?) { + Metadata.Coder.ValueType == Metadata>(connection: Connection, metadata: Metadata?) -> (Self, Metadata?) { return connection.writeWithMetadata((self, metadata)) } @@ -57,7 +57,7 @@ extension Persistable where Connection: ConnectionType, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(connection: Connection, metadata: Metadata? = nil, queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ((Self, Metadata?) -> Void)? = .None) { + Metadata.Coder.ValueType == Metadata>(connection: Connection, metadata: Metadata?, queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ((Self, Metadata?) -> Void)? = .None) { return connection.asyncWriteWithMetadata((self, metadata), queue: queue, completion: completion) } @@ -72,7 +72,7 @@ extension Persistable where Connection: ConnectionType, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(connection: Connection, metadata: Metadata? = nil) -> NSOperation { + Metadata.Coder.ValueType == Metadata>(connection: Connection, metadata: Metadata?) -> NSOperation { return NSBlockOperation { connection.writeWithMetadata((self, metadata)) } } } @@ -92,7 +92,7 @@ extension SequenceType where WriteTransaction: WriteTransactionType, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(transaction: WriteTransaction, metadata: [Metadata?] = []) -> [(Generator.Element, Metadata?)] { + Metadata.Coder.ValueType == Metadata>(transaction: WriteTransaction, metadata: [Metadata?]) -> [(Generator.Element, Metadata?)] { let items = zipToWrite(self, metadata) return transaction.writeWithMetadata(items) } @@ -108,7 +108,7 @@ extension SequenceType where Connection: ConnectionType, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(connection: Connection, metadata: [Metadata?] = []) -> [(Generator.Element, Metadata?)] { + Metadata.Coder.ValueType == Metadata>(connection: Connection, metadata: [Metadata?]) -> [(Generator.Element, Metadata?)] { let items = zipToWrite(self, metadata) return connection.writeWithMetadata(items) } @@ -124,7 +124,7 @@ extension SequenceType where Connection: ConnectionType, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(connection: Connection, metadata: [Metadata?] = [], queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ([(Generator.Element, Metadata?)] -> Void)? = .None) { + Metadata.Coder.ValueType == Metadata>(connection: Connection, metadata: [Metadata?], queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ([(Generator.Element, Metadata?)] -> Void)? = .None) { let items = zipToWrite(self, metadata) return connection.asyncWriteWithMetadata(items, queue: queue, completion: completion) } @@ -140,7 +140,7 @@ extension SequenceType where Connection: ConnectionType, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(connection: Connection, metadata: [Metadata?] = []) -> NSOperation { + Metadata.Coder.ValueType == Metadata>(connection: Connection, metadata: [Metadata?]) -> NSOperation { let items = zipToWrite(self, metadata) return NSBlockOperation { connection.writeWithMetadata(items) } } diff --git a/YapDatabaseExtensions/Shared/Persistable/Persistable_ValueWithObjectMetadata.swift b/YapDatabaseExtensions/Shared/Persistable/Persistable_ValueWithObjectMetadata.swift index 0533da6..2a200dd 100644 --- a/YapDatabaseExtensions/Shared/Persistable/Persistable_ValueWithObjectMetadata.swift +++ b/YapDatabaseExtensions/Shared/Persistable/Persistable_ValueWithObjectMetadata.swift @@ -27,7 +27,7 @@ extension Persistable where public func writeWithMetadata< WriteTransaction, Metadata where WriteTransaction: WriteTransactionType, - Metadata: NSCoding>(transaction: WriteTransaction, metadata: Metadata? = nil) -> (Self, Metadata?) { + Metadata: NSCoding>(transaction: WriteTransaction, metadata: Metadata?) -> (Self, Metadata?) { return transaction.writeWithMetadata((self, metadata)) } @@ -40,7 +40,7 @@ extension Persistable where public func writeWithMetadata< Connection, Metadata where Connection: ConnectionType, - Metadata: NSCoding>(connection: Connection, metadata: Metadata? = nil) -> (Self, Metadata?) { + Metadata: NSCoding>(connection: Connection, metadata: Metadata?) -> (Self, Metadata?) { return connection.writeWithMetadata((self, metadata)) } @@ -53,7 +53,7 @@ extension Persistable where public func asyncWriteWithMetadata< Connection, Metadata where Connection: ConnectionType, - Metadata: NSCoding>(connection: Connection, metadata: Metadata? = nil, queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ((Self, Metadata?) -> Void)? = .None) { + Metadata: NSCoding>(connection: Connection, metadata: Metadata?, queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ((Self, Metadata?) -> Void)? = .None) { return connection.asyncWriteWithMetadata((self, metadata), queue: queue, completion: completion) } @@ -66,7 +66,7 @@ extension Persistable where public func writeWithMetadataOperation< Connection, Metadata where Connection: ConnectionType, - Metadata: NSCoding>(connection: Connection, metadata: Metadata? = nil) -> NSOperation { + Metadata: NSCoding>(connection: Connection, metadata: Metadata?) -> NSOperation { return NSBlockOperation { connection.writeWithMetadata((self, metadata)) } } } @@ -86,7 +86,7 @@ extension SequenceType where public func writeWithMetadata< WriteTransaction, Metadata where WriteTransaction: WriteTransactionType, - Metadata: NSCoding>(transaction: WriteTransaction, metadata: [Metadata?] = []) -> [(Generator.Element, Metadata?)] { + Metadata: NSCoding>(transaction: WriteTransaction, metadata: [Metadata?]) -> [(Generator.Element, Metadata?)] { let items = zipToWrite(self, metadata) return transaction.writeWithMetadata(items) } @@ -100,7 +100,7 @@ extension SequenceType where public func writeWithMetadata< Connection, Metadata where Connection: ConnectionType, - Metadata: NSCoding>(connection: Connection, metadata: [Metadata?] = []) -> [(Generator.Element, Metadata?)] { + Metadata: NSCoding>(connection: Connection, metadata: [Metadata?]) -> [(Generator.Element, Metadata?)] { let items = zipToWrite(self, metadata) return connection.writeWithMetadata(items) } @@ -114,7 +114,7 @@ extension SequenceType where public func asyncWriteWithMetadata< Connection, Metadata where Connection: ConnectionType, - Metadata: NSCoding>(connection: Connection, metadata: [Metadata?] = [], queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ([(Generator.Element, Metadata?)] -> Void)? = .None) { + Metadata: NSCoding>(connection: Connection, metadata: [Metadata?], queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ([(Generator.Element, Metadata?)] -> Void)? = .None) { let items = zipToWrite(self, metadata) return connection.asyncWriteWithMetadata(items, queue: queue, completion: completion) } @@ -128,7 +128,7 @@ extension SequenceType where public func writeWithMetadataOperation< Connection, Metadata where Connection: ConnectionType, - Metadata: NSCoding>(connection: Connection, metadata: [Metadata?] = []) -> NSOperation { + Metadata: NSCoding>(connection: Connection, metadata: [Metadata?]) -> NSOperation { let items = zipToWrite(self, metadata) return NSBlockOperation { connection.writeWithMetadata(items) } } diff --git a/YapDatabaseExtensions/Shared/Persistable/Persistable_ValueWithValueMetadata.swift b/YapDatabaseExtensions/Shared/Persistable/Persistable_ValueWithValueMetadata.swift index a651ef5..5dea161 100644 --- a/YapDatabaseExtensions/Shared/Persistable/Persistable_ValueWithValueMetadata.swift +++ b/YapDatabaseExtensions/Shared/Persistable/Persistable_ValueWithValueMetadata.swift @@ -29,7 +29,7 @@ extension Persistable where WriteTransaction: WriteTransactionType, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(transaction: WriteTransaction, metadata: Metadata? = nil) -> (Self, Metadata?) { + Metadata.Coder.ValueType == Metadata>(transaction: WriteTransaction, metadata: Metadata?) -> (Self, Metadata?) { return transaction.writeWithMetadata((self, metadata)) } @@ -44,7 +44,7 @@ extension Persistable where Connection: ConnectionType, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(connection: Connection, metadata: Metadata? = nil) -> (Self, Metadata?) { + Metadata.Coder.ValueType == Metadata>(connection: Connection, metadata: Metadata?) -> (Self, Metadata?) { return connection.writeWithMetadata((self, metadata)) } @@ -59,7 +59,7 @@ extension Persistable where Connection: ConnectionType, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(connection: Connection, metadata: Metadata? = nil, queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ((Self, Metadata?) -> Void)? = .None) { + Metadata.Coder.ValueType == Metadata>(connection: Connection, metadata: Metadata?, queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ((Self, Metadata?) -> Void)? = .None) { return connection.asyncWriteWithMetadata((self, metadata), queue: queue, completion: completion) } @@ -74,7 +74,7 @@ extension Persistable where Connection: ConnectionType, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(connection: Connection, metadata: Metadata? = nil) -> NSOperation { + Metadata.Coder.ValueType == Metadata>(connection: Connection, metadata: Metadata?) -> NSOperation { return NSBlockOperation { connection.writeWithMetadata((self, metadata)) } } } @@ -96,7 +96,7 @@ extension SequenceType where WriteTransaction: WriteTransactionType, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(transaction: WriteTransaction, metadata: [Metadata?] = []) -> [(Generator.Element, Metadata?)] { + Metadata.Coder.ValueType == Metadata>(transaction: WriteTransaction, metadata: [Metadata?]) -> [(Generator.Element, Metadata?)] { let items = zipToWrite(self, metadata) return transaction.writeWithMetadata(items) } @@ -112,7 +112,7 @@ extension SequenceType where Connection: ConnectionType, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(connection: Connection, metadata: [Metadata?] = []) -> [(Generator.Element, Metadata?)] { + Metadata.Coder.ValueType == Metadata>(connection: Connection, metadata: [Metadata?]) -> [(Generator.Element, Metadata?)] { let items = zipToWrite(self, metadata) return connection.writeWithMetadata(items) } @@ -128,7 +128,7 @@ extension SequenceType where Connection: ConnectionType, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(connection: Connection, metadata: [Metadata?] = [], queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ([(Generator.Element, Metadata?)] -> Void)? = .None) { + Metadata.Coder.ValueType == Metadata>(connection: Connection, metadata: [Metadata?], queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ([(Generator.Element, Metadata?)] -> Void)? = .None) { let items = zipToWrite(self, metadata) return connection.asyncWriteWithMetadata(items, queue: queue, completion: completion) } @@ -144,7 +144,7 @@ extension SequenceType where Connection: ConnectionType, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(connection: Connection, metadata: [Metadata?] = []) -> NSOperation { + Metadata.Coder.ValueType == Metadata>(connection: Connection, metadata: [Metadata?]) -> NSOperation { let items = zipToWrite(self, metadata) return NSBlockOperation { connection.writeWithMetadata(items) } } From 0480a120245e3b51d492168264b80f57e1572971 Mon Sep 17 00:00:00 2001 From: Jim Roepcke Date: Fri, 9 Sep 2016 21:07:27 -0700 Subject: [PATCH 12/14] update tests --- Tests/Shared/Models.swift | 23 +- Tests/Shared/ObjectWithNoMetadataTests.swift | 13 - .../ObjectWithObjectMetadataTests.swift | 207 ++++++++-------- .../Shared/ObjectWithValueMetadataTests.swift | 211 ++++++++--------- Tests/Shared/ReadWriteTests.swift | 15 +- Tests/Shared/ValueWithNoMetadataTests.swift | 13 - .../Shared/ValueWithObjectMetadataTests.swift | 223 +++++++++--------- .../Shared/ValueWithValueMetadataTests.swift | 223 +++++++++--------- Tests/Shared/YapDatabaseExtensionsTests.swift | 26 +- 9 files changed, 473 insertions(+), 481 deletions(-) diff --git a/Tests/Shared/Models.swift b/Tests/Shared/Models.swift index 1994c7b..22a2efa 100644 --- a/Tests/Shared/Models.swift +++ b/Tests/Shared/Models.swift @@ -31,13 +31,11 @@ public struct Product: Identifiable, Equatable { } } - public var metadata: Metadata? = .None public let identifier: Identifier internal let name: String internal let barcode: Barcode - public init(metadata: Metadata? = .None, identifier: Identifier, name: String, barcode: Barcode) { - self.metadata = metadata + public init(identifier: Identifier, name: String, barcode: Barcode) { self.identifier = identifier self.name = name self.barcode = barcode @@ -46,7 +44,6 @@ public struct Product: Identifiable, Equatable { public struct Inventory: Identifiable, Equatable { let product: Product - public var metadata: NSNumber? = .None public var identifier: Identifier { return product.identifier @@ -77,14 +74,12 @@ public class NamedEntity: NSObject, NSCoding { public class Person: NamedEntity { } public class Employee: NamedEntity { - public var metadata: NSDate? = .None } public class Manager: NamedEntity { public struct Metadata: Equatable { public let numberOfDirectReports: Int } - public var metadata: Metadata? = .None } // MARK: - Equatable @@ -109,7 +104,7 @@ public func == (a: Product.Metadata, b: Product.Metadata) -> Bool { } public func == (a: Inventory, b: Inventory) -> Bool { - return (a.product == b.product) && (a.metadata == b.metadata) + return (a.product == b.product) } public func == (a: NamedEntity, b: NamedEntity) -> Bool { @@ -140,6 +135,18 @@ extension Inventory: Hashable { } } +extension Product.Metadata: Hashable { + public var hashValue: Int { + return categoryIdentifier.hashValue + } +} + +extension Manager.Metadata: Hashable { + public var hashValue: Int { + return numberOfDirectReports.hashValue + } +} + extension NamedEntity { public override var description: String { @@ -352,7 +359,7 @@ public class InventoryCoder: NSObject, NSCoding, CodingType { public required init?(coder aDecoder: NSCoder) { let product = Product.decode(aDecoder.decodeObjectForKey("product")) - value = Inventory(product: product!, metadata: .None) + value = Inventory(product: product!) } public func encodeWithCoder(aCoder: NSCoder) { diff --git a/Tests/Shared/ObjectWithNoMetadataTests.swift b/Tests/Shared/ObjectWithNoMetadataTests.swift index ce6fe18..5c125d6 100644 --- a/Tests/Shared/ObjectWithNoMetadataTests.swift +++ b/Tests/Shared/ObjectWithNoMetadataTests.swift @@ -112,7 +112,6 @@ class ObjectWithNoMetadataTests: XCTestCase { } XCTAssertEqual(readTransaction.didReadAtIndex, index) XCTAssertEqual(result.identifier, item.identifier) - XCTAssertNil(result.metadata) return true } @@ -135,18 +134,6 @@ class ObjectWithNoMetadataTests: XCTestCase { } } -class Base_ObjectWithNoMetadataTests: ObjectWithNoMetadataTests { - - func test__metadata_is_nil() { - XCTAssertNil(item.metadata) - } - - func test__metadata_cannot_be_set() { - item.metadata = Void() - XCTAssertNil(item.metadata) - } -} - class Functional_Read_ObjectWithNoMetadataTests: ObjectWithNoMetadataTests { // Functional API - ReadTransactionType - Reading diff --git a/Tests/Shared/ObjectWithObjectMetadataTests.swift b/Tests/Shared/ObjectWithObjectMetadataTests.swift index a4f71fb..a91ec71 100644 --- a/Tests/Shared/ObjectWithObjectMetadataTests.swift +++ b/Tests/Shared/ObjectWithObjectMetadataTests.swift @@ -13,12 +13,15 @@ import XCTest class ObjectWithObjectMetadataTests: XCTestCase { typealias TypeUnderTest = Employee + typealias MetadataTypeUnderTest = NSDate var item: TypeUnderTest! + var metadata: MetadataTypeUnderTest! var index: YapDB.Index! var key: String! var items: [TypeUnderTest]! + var metadatas: [MetadataTypeUnderTest?]! var indexes: [YapDB.Index]! var keys: [String]! @@ -56,9 +59,11 @@ class ObjectWithObjectMetadataTests: XCTestCase { override func tearDown() { item = nil + metadata = nil index = nil key = nil items = nil + metadatas = nil indexes = nil keys = nil @@ -73,55 +78,56 @@ class ObjectWithObjectMetadataTests: XCTestCase { func createPersistables() { item = TypeUnderTest(id: "beatle-1", name: "John") - item.metadata = NSDate() + metadata = NSDate() items = [ item, TypeUnderTest(id: "beatle-2", name: "Paul"), TypeUnderTest(id: "beatle-3", name: "George"), TypeUnderTest(id: "beatle-4", name: "Ringo") ] - items.suffixFrom(1).forEach { $0.metadata = NSDate() } + metadatas = [metadata] + items.suffixFrom(1).forEach { _ in metadatas.append(NSDate()) } } func configureForReadingSingle() { readTransaction.object = item - readTransaction.metadata = item.metadata + readTransaction.metadata = metadata } func configureForReadingMultiple() { readTransaction.objects = items - readTransaction.metadatas = items.map { $0.metadata } + readTransaction.metadatas = metadatas.map { $0 } readTransaction.keys = keys } - func checkTransactionDidWriteItem(result: TypeUnderTest) { - XCTAssertEqual(result.identifier, item.identifier) + func checkTransactionDidWriteItem(result: (TypeUnderTest, MetadataTypeUnderTest?)) { + XCTAssertEqual(result.0.identifier, item.identifier) XCTAssertFalse(writeTransaction.didWriteAtIndexes.isEmpty) XCTAssertEqual(writeTransaction.didWriteAtIndexes[0].0, index) XCTAssertEqual(writeTransaction.didWriteAtIndexes[0].1.identifier, item.identifier) - XCTAssertEqual(writeTransaction.didWriteAtIndexes[0].2 as? NSDate, item.metadata) + XCTAssertEqual(writeTransaction.didWriteAtIndexes[0].2 as? NSDate, metadata) } - func checkTransactionDidWriteItems(result: [TypeUnderTest]) { + func checkTransactionDidWriteItems(result: [(TypeUnderTest, MetadataTypeUnderTest?)]) { XCTAssertFalse(writeTransaction.didWriteAtIndexes.isEmpty) XCTAssertEqual(writeTransaction.didWriteAtIndexes.map { $0.0.key }.sort(), indexes.map { $0.key }.sort()) XCTAssertEqual(writeTransaction.didWriteAtIndexes.map { $0.2 }.count, items.count) XCTAssertFalse(result.isEmpty) - XCTAssertEqual(Set(result), Set(items)) + XCTAssertEqual(Set(result.map({$0.0})), Set(items)) } - func checkTransactionDidReadItem(result: TypeUnderTest?) -> Bool { + func checkTransactionDidReadItem(result: (TypeUnderTest, MetadataTypeUnderTest?)?) -> Bool { guard let result = result else { return false } XCTAssertEqual(readTransaction.didReadAtIndex, index) - XCTAssertEqual(result.identifier, item.identifier) + XCTAssertEqual(result.0.identifier, item.identifier) XCTAssertEqual(readTransaction.didReadMetadataAtIndex, index) - XCTAssertEqual(result.metadata, item.metadata) + XCTAssertEqual(result.1, metadata) return true } - func checkTransactionDidReadItems(result: [TypeUnderTest]) -> Bool { + func checkTransactionDidReadItems(result: [(TypeUnderTest, MetadataTypeUnderTest?)]) -> Bool { if result.isEmpty { return false } @@ -145,7 +151,7 @@ class ObjectWithObjectMetadataTests: XCTestCase { class Base_ObjectWithObjectMetadataTests: ObjectWithObjectMetadataTests { func test__metadata_is_not_nil() { - XCTAssertNotNil(item.metadata) + XCTAssertNotNil(metadata) } } @@ -155,116 +161,116 @@ class Functional_Read_ObjectWithObjectMetadataTests: ObjectWithObjectMetadataTes func test__transaction__read_at_index_with_data() { configureForReadingSingle() - XCTAssertTrue(checkTransactionDidReadItem(readTransaction.readAtIndex(index))) + XCTAssertTrue(checkTransactionDidReadItem(readTransaction.readWithMetadataAtIndex(index))) } func test__transaction__read_at_index_without_data() { - XCTAssertFalse(checkTransactionDidReadItem(readTransaction.readAtIndex(index))) + XCTAssertFalse(checkTransactionDidReadItem(readTransaction.readWithMetadataAtIndex(index))) } func test__transaction__read_at_indexes_with_data() { configureForReadingMultiple() - XCTAssertTrue(checkTransactionDidReadItems(readTransaction.readAtIndexes(indexes))) + XCTAssertTrue(checkTransactionDidReadItems(readTransaction.readWithMetadataAtIndexes(indexes))) } func test__transaction__read_at_indexes_with_data_2() { configureForReadingMultiple() - XCTAssertTrue(checkTransactionDidReadItems(readTransaction.readAtIndexes(Set(indexes)))) + XCTAssertTrue(checkTransactionDidReadItems(readTransaction.readWithMetadataAtIndexes(Set(indexes)))) } func test__transaction__read_at_indexes_without_data() { - XCTAssertFalse(checkTransactionDidReadItems(readTransaction.readAtIndexes(indexes))) + XCTAssertFalse(checkTransactionDidReadItems(readTransaction.readWithMetadataAtIndexes(indexes))) } func test__transaction__read_by_key_with_data() { configureForReadingSingle() - XCTAssertTrue(checkTransactionDidReadItem(readTransaction.readByKey(key))) + XCTAssertTrue(checkTransactionDidReadItem(readTransaction.readWithMetadataByKey(key))) } func test__transaction__read_by_key_without_data() { - XCTAssertFalse(checkTransactionDidReadItem(readTransaction.readByKey(key))) + XCTAssertFalse(checkTransactionDidReadItem(readTransaction.readWithMetadataByKey(key))) } func test__transaction__read_by_keys_with_data() { configureForReadingMultiple() - XCTAssertTrue(checkTransactionDidReadItems(readTransaction.readByKeys(keys))) + XCTAssertTrue(checkTransactionDidReadItems(readTransaction.readWithMetadataByKeys(keys))) } func test__transaction__read_by_keys_with_data_2() { configureForReadingMultiple() - XCTAssertTrue(checkTransactionDidReadItems(readTransaction.readByKeys(Set(keys)))) + XCTAssertTrue(checkTransactionDidReadItems(readTransaction.readWithMetadataByKeys(Set(keys)))) } func test__transaction__read_by_keys_without_data() { - XCTAssertFalse(checkTransactionDidReadItems(readTransaction.readByKeys(keys))) + XCTAssertFalse(checkTransactionDidReadItems(readTransaction.readWithMetadataByKeys(keys))) } func test__transaction__read_all_with_data() { configureForReadingMultiple() - XCTAssertTrue(checkTransactionDidReadItems(readTransaction.readAll())) + XCTAssertTrue(checkTransactionDidReadItems(readTransaction.readWithMetadataAll())) } // Functional API - ConnectionType - Reading func test__connection__read_at_index_with_data() { configureForReadingSingle() - XCTAssertTrue(checkTransactionDidReadItem(connection.readAtIndex(index))) + XCTAssertTrue(checkTransactionDidReadItem(connection.readWithMetadataAtIndex(index))) XCTAssertTrue(connection.didRead) } func test__connection__read_at_index_without_data() { - XCTAssertFalse(checkTransactionDidReadItem(connection.readAtIndex(index))) + XCTAssertFalse(checkTransactionDidReadItem(connection.readWithMetadataAtIndex(index))) XCTAssertTrue(connection.didRead) } func test__connection__read_at_indexes_with_data() { configureForReadingMultiple() - XCTAssertTrue(checkTransactionDidReadItems(connection.readAtIndexes(indexes))) + XCTAssertTrue(checkTransactionDidReadItems(connection.readWithMetadataAtIndexes(indexes))) XCTAssertTrue(connection.didRead) } func test__connection__read_at_indexes_with_data_2() { configureForReadingMultiple() - XCTAssertTrue(checkTransactionDidReadItems(connection.readAtIndexes(Set(indexes)))) + XCTAssertTrue(checkTransactionDidReadItems(connection.readWithMetadataAtIndexes(Set(indexes)))) XCTAssertTrue(connection.didRead) } func test__connection__read_at_indexes_without_data() { - XCTAssertFalse(checkTransactionDidReadItems(connection.readAtIndexes(indexes))) + XCTAssertFalse(checkTransactionDidReadItems(connection.readWithMetadataAtIndexes(indexes))) XCTAssertTrue(connection.didRead) } func test__connection__read_by_key_with_data() { configureForReadingSingle() - XCTAssertTrue(checkTransactionDidReadItem(connection.readByKey(key))) + XCTAssertTrue(checkTransactionDidReadItem(connection.readWithMetadataByKey(key))) XCTAssertTrue(connection.didRead) } func test__connection__read_by_key_without_data() { - XCTAssertFalse(checkTransactionDidReadItem(connection.readByKey(key))) + XCTAssertFalse(checkTransactionDidReadItem(connection.readWithMetadataByKey(key))) XCTAssertTrue(connection.didRead) } func test__connection__read_by_keys_with_data() { configureForReadingMultiple() - XCTAssertTrue(checkTransactionDidReadItems(connection.readByKeys(keys))) + XCTAssertTrue(checkTransactionDidReadItems(connection.readWithMetadataByKeys(keys))) XCTAssertTrue(connection.didRead) } func test__connection__read_by_keys_with_data_2() { configureForReadingMultiple() - XCTAssertTrue(checkTransactionDidReadItems(connection.readByKeys(Set(keys)))) + XCTAssertTrue(checkTransactionDidReadItems(connection.readWithMetadataByKeys(Set(keys)))) XCTAssertTrue(connection.didRead) } func test__connection__read_by_keys_without_data() { - XCTAssertFalse(checkTransactionDidReadItems(connection.readByKeys(keys))) + XCTAssertFalse(checkTransactionDidReadItems(connection.readWithMetadataByKeys(keys))) XCTAssertTrue(connection.didRead) } func test__connection__read_all_with_data() { configureForReadingMultiple() - XCTAssertTrue(checkTransactionDidReadItems(connection.readAll())) + XCTAssertTrue(checkTransactionDidReadItems(connection.readWithMetadataAll())) XCTAssertTrue(connection.didRead) } } @@ -272,38 +278,29 @@ class Functional_Read_ObjectWithObjectMetadataTests: ObjectWithObjectMetadataTes class Functional_Write_ObjectWithObjectMetadataTests: ObjectWithObjectMetadataTests { func test__transaction__write_item() { - checkTransactionDidWriteItem(writeTransaction.write(item)) + checkTransactionDidWriteItem(writeTransaction.writeWithMetadata((item, metadata))) } func test__transaction__write_items() { - checkTransactionDidWriteItems(writeTransaction.write(items)) - } - - func test__transaction__write_items_2() { - checkTransactionDidWriteItems(writeTransaction.write(Set(items))) + checkTransactionDidWriteItems(writeTransaction.writeWithMetadata(zipToWrite(items, metadatas))) } // MARK: - Functional API - Connection - Writing func test__connection__write_item() { - checkTransactionDidWriteItem(connection.write(item)) + checkTransactionDidWriteItem(connection.writeWithMetadata((item, metadata))) XCTAssertTrue(connection.didWrite) } func test__connection__write_items() { - checkTransactionDidWriteItems(connection.write(items)) - XCTAssertTrue(connection.didWrite) - } - - func test__connection__write_items_2() { - checkTransactionDidWriteItems(connection.write(Set(items))) + checkTransactionDidWriteItems(connection.writeWithMetadata(zipToWrite(items, metadatas))) XCTAssertTrue(connection.didWrite) } func test__connection__async_write_item() { - var result: TypeUnderTest! + var result: (TypeUnderTest, MetadataTypeUnderTest?)! let expectation = expectationWithDescription("Test: \(#function)") - connection.asyncWrite(item) { tmp in + connection.asyncWriteWithMetadata((item, metadata)) { tmp in result = tmp expectation.fulfill() } @@ -313,9 +310,9 @@ class Functional_Write_ObjectWithObjectMetadataTests: ObjectWithObjectMetadataTe } func test__connection__async_write_items() { - var result: [TypeUnderTest] = [] + var result: [(TypeUnderTest, MetadataTypeUnderTest?)] = [] let expectation = expectationWithDescription("Test: \(#function)") - connection.asyncWrite(items) { received in + connection.asyncWriteWithMetadata(zipToWrite(items, metadatas)) { received in result = received expectation.fulfill() } @@ -382,45 +379,45 @@ class Curried_Read_ObjectWithObjectMetadataTests: ObjectWithObjectMetadataTests func test__curried__read_at_index_with_data() { configureForReadingSingle() - XCTAssertTrue(checkTransactionDidReadItem(connection.read(TypeUnderTest.readAtIndex(index)))) + XCTAssertTrue(checkTransactionDidReadItem(connection.read(TypeUnderTest.readWithMetadataAtIndex(index)))) XCTAssertTrue(connection.didRead) } func test__curried__read_at_index_with_no_data() { - XCTAssertFalse(checkTransactionDidReadItem(connection.read(TypeUnderTest.readAtIndex(index)))) + XCTAssertFalse(checkTransactionDidReadItem(connection.read(TypeUnderTest.readWithMetadataAtIndex(index)))) XCTAssertTrue(connection.didRead) } func test__curried__read_at_indexes_with_data() { configureForReadingMultiple() - XCTAssertTrue(checkTransactionDidReadItems(connection.read(TypeUnderTest.readAtIndexes(indexes)))) + XCTAssertTrue(checkTransactionDidReadItems(connection.read(TypeUnderTest.readWithMetadataAtIndexes(indexes)))) XCTAssertTrue(connection.didRead) } func test__curried__read_at_indexes_with_no_data() { - XCTAssertFalse(checkTransactionDidReadItems(connection.read(TypeUnderTest.readAtIndexes(indexes)))) + XCTAssertFalse(checkTransactionDidReadItems(connection.read(TypeUnderTest.readWithMetadataAtIndexes(indexes)))) XCTAssertTrue(connection.didRead) } func test__curried__read_by_key_with_data() { configureForReadingSingle() - XCTAssertTrue(checkTransactionDidReadItem(connection.read(TypeUnderTest.readByKey(key)))) + XCTAssertTrue(checkTransactionDidReadItem(connection.read(TypeUnderTest.readWithMetadataByKey(key)))) XCTAssertTrue(connection.didRead) } func test__curried__read_by_key_with_no_data() { - XCTAssertFalse(checkTransactionDidReadItem(connection.read(TypeUnderTest.readByKey(key)))) + XCTAssertFalse(checkTransactionDidReadItem(connection.read(TypeUnderTest.readWithMetadataByKey(key)))) XCTAssertTrue(connection.didRead) } func test__curried__read_by_keys_with_data() { configureForReadingMultiple() - XCTAssertTrue(checkTransactionDidReadItems(connection.read(TypeUnderTest.readByKeys(keys)))) + XCTAssertTrue(checkTransactionDidReadItems(connection.read(TypeUnderTest.readWithMetadataByKeys(keys)))) XCTAssertTrue(connection.didRead) } func test__curried__read_by_keys_with_no_data() { - XCTAssertFalse(checkTransactionDidReadItems(connection.read(TypeUnderTest.readByKeys(keys)))) + XCTAssertFalse(checkTransactionDidReadItems(connection.read(TypeUnderTest.readWithMetadataByKeys(keys)))) XCTAssertTrue(connection.didRead) } } @@ -428,7 +425,7 @@ class Curried_Read_ObjectWithObjectMetadataTests: ObjectWithObjectMetadataTests class Curried_Write_ObjectWithObjectMetadataTests: ObjectWithObjectMetadataTests { func test__curried__write() { - checkTransactionDidWriteItem(connection.write(item.write())) + checkTransactionDidWriteItem(connection.write(item.writeWithMetadata(metadata))) XCTAssertTrue(connection.didWrite) } } @@ -440,69 +437,69 @@ class Persistable_Read_ObjectWithObjectMetadataTests: ObjectWithObjectMetadataTe func test__reader__in_transaction_at_index() { configureForReadingSingle() reader = Read(readTransaction) - XCTAssertTrue(checkTransactionDidReadItem(reader.inTransaction(readTransaction, atIndex: index))) + XCTAssertTrue(checkTransactionDidReadItem(reader.withMetadataInTransaction(readTransaction, atIndex: index))) } func test__reader__in_transaction_at_index_2() { configureForReadingSingle() reader = Read(readTransaction) - let atIndex = reader.inTransactionAtIndex(readTransaction) + let atIndex: YapDB.Index -> (TypeUnderTest, MetadataTypeUnderTest?)? = reader.withMetadataInTransactionAtIndex(readTransaction) XCTAssertTrue(checkTransactionDidReadItem(atIndex(index))) } func test__reader__at_index_in_transaction() { configureForReadingSingle() reader = Read(readTransaction) - let inTransaction = reader.atIndexInTransaction(index) + let inTransaction: TestableReadTransaction -> (TypeUnderTest, MetadataTypeUnderTest?)? = reader.withMetadataAtIndexInTransaction(index) XCTAssertTrue(checkTransactionDidReadItem(inTransaction(readTransaction))) } func test__reader__at_indexes_in_transaction_with_items() { configureForReadingMultiple() reader = Read(readTransaction) - XCTAssertTrue(checkTransactionDidReadItems(reader.atIndexesInTransaction(indexes)(readTransaction))) + XCTAssertTrue(checkTransactionDidReadItems(reader.withMetadataAtIndexesInTransaction(indexes)(readTransaction))) } func test__reader__at_indexes_in_transaction_with_no_items() { reader = Read(readTransaction) - XCTAssertFalse(checkTransactionDidReadItems(reader.atIndexesInTransaction(indexes)(readTransaction))) + XCTAssertFalse(checkTransactionDidReadItems(reader.withMetadataAtIndexesInTransaction(indexes)(readTransaction))) } func test__reader__in_transaction_by_key() { configureForReadingSingle() reader = Read(readTransaction) - XCTAssertTrue(checkTransactionDidReadItem(reader.inTransaction(readTransaction, byKey: key))) + XCTAssertTrue(checkTransactionDidReadItem(reader.withMetadataInTransaction(readTransaction, byKey: key))) } func test__reader__in_transaction_by_key_2() { configureForReadingSingle() reader = Read(readTransaction) - let byKey = reader.inTransactionByKey(readTransaction) + let byKey: String -> (TypeUnderTest, MetadataTypeUnderTest?)? = reader.withMetadataInTransactionByKey(readTransaction) XCTAssertTrue(checkTransactionDidReadItem(byKey(key))) } func test__reader__by_key_in_transaction() { configureForReadingSingle() reader = Read(readTransaction) - let inTransaction = reader.byKeyInTransaction(key) + let inTransaction: TestableReadTransaction -> (TypeUnderTest, MetadataTypeUnderTest?)? = reader.withMetadataByKeyInTransaction(key) XCTAssertTrue(checkTransactionDidReadItem(inTransaction(readTransaction))) } func test__reader__by_keys_in_transaction_with_items() { configureForReadingMultiple() reader = Read(readTransaction) - XCTAssertTrue(checkTransactionDidReadItems(reader.byKeysInTransaction(keys)(readTransaction))) + XCTAssertTrue(checkTransactionDidReadItems(reader.withMetadataByKeysInTransaction(keys)(readTransaction))) } func test__reader__by_keys_in_transaction_with_items_with_keys() { configureForReadingMultiple() reader = Read(readTransaction) - XCTAssertTrue(checkTransactionDidReadItems(reader.byKeysInTransaction()(readTransaction))) + XCTAssertTrue(checkTransactionDidReadItems(reader.withMetadataByKeysInTransaction()(readTransaction))) } func test__reader__by_keys_in_transaction_with_no_items() { reader = Read(readTransaction) - XCTAssertFalse(checkTransactionDidReadItems(reader.byKeysInTransaction(keys)(readTransaction))) + XCTAssertFalse(checkTransactionDidReadItems(reader.withMetadataByKeysInTransaction(keys)(readTransaction))) } // Reading - With Transaction @@ -510,66 +507,66 @@ class Persistable_Read_ObjectWithObjectMetadataTests: ObjectWithObjectMetadataTe func test__reader_with_transaction__at_index_with_item() { configureForReadingSingle() reader = Read(readTransaction) - XCTAssertTrue(checkTransactionDidReadItem(reader.inTransaction(readTransaction, atIndex: index))) + XCTAssertTrue(checkTransactionDidReadItem(reader.withMetadataInTransaction(readTransaction, atIndex: index))) } func test__reader_with_transaction__at_index_with_no_item() { reader = Read(readTransaction) - XCTAssertFalse(checkTransactionDidReadItem(reader.atIndex(index))) + XCTAssertFalse(checkTransactionDidReadItem(reader.withMetadataAtIndex(index))) } func test__reader_with_transaction__at_indexes_with_items() { configureForReadingMultiple() reader = Read(readTransaction) - XCTAssertTrue(checkTransactionDidReadItems(reader.atIndexes(indexes))) + XCTAssertTrue(checkTransactionDidReadItems(reader.withMetadataAtIndexes(indexes))) } func test__reader_with_transaction__at_indexes_with_no_items() { reader = Read(readTransaction) - XCTAssertFalse(checkTransactionDidReadItems(reader.atIndexes(indexes))) + XCTAssertFalse(checkTransactionDidReadItems(reader.withMetadataAtIndexes(indexes))) } func test__reader_with_transaction__by_key_with_item() { configureForReadingSingle() reader = Read(readTransaction) - XCTAssertTrue(checkTransactionDidReadItem(reader.inTransaction(readTransaction, atIndex: index))) + XCTAssertTrue(checkTransactionDidReadItem(reader.withMetadataInTransaction(readTransaction, atIndex: index))) } func test__reader_with_transaction__by_key_with_no_item() { reader = Read(readTransaction) - XCTAssertFalse(checkTransactionDidReadItem(reader.byKey(key))) + XCTAssertFalse(checkTransactionDidReadItem(reader.withMetadataByKey(key))) } func test__reader_with_transaction__by_keys_with_items() { configureForReadingMultiple() reader = Read(readTransaction) - XCTAssertTrue(checkTransactionDidReadItems(reader.byKeys(keys))) + XCTAssertTrue(checkTransactionDidReadItems(reader.withMetadataByKeys(keys))) } func test__reader_with_transaction__by_keys_with_no_items() { reader = Read(readTransaction) - XCTAssertFalse(checkTransactionDidReadItems(reader.byKeys(keys))) + XCTAssertFalse(checkTransactionDidReadItems(reader.withMetadataByKeys(keys))) } func test__reader_with_transaction__all_with_items() { configureForReadingMultiple() reader = Read(readTransaction) - XCTAssertTrue(checkTransactionDidReadItems(reader.all())) + XCTAssertTrue(checkTransactionDidReadItems(reader.withMetadataAll())) XCTAssertEqual(readTransaction.didKeysInCollection, TypeUnderTest.collection) } func test__reader_with_transaction__all_with_no_items() { reader = Read(readTransaction) - XCTAssertFalse(checkTransactionDidReadItems(reader.all())) + XCTAssertFalse(checkTransactionDidReadItems(reader.withMetadataAll())) XCTAssertEqual(readTransaction.didKeysInCollection, TypeUnderTest.collection) } func test__reader_with_transaction__filter() { configureForReadingSingle() reader = Read(readTransaction) - let (items, missing) = reader.filterExisting(keys) + let (items, missing): ([(TypeUnderTest, MetadataTypeUnderTest?)], [String]) = reader.withMetadataFilterExisting(keys) XCTAssertEqual(readTransaction.didReadAtIndexes.first!, indexes.first!) - XCTAssertEqual(items.map { $0.identifier }, items.prefixUpTo(1).map { $0.identifier }) + XCTAssertEqual(items.map { $0.0.identifier }, items.prefixUpTo(1).map { $0.0.identifier }) XCTAssertEqual(missing, Array(keys.suffixFrom(1))) } @@ -578,66 +575,66 @@ class Persistable_Read_ObjectWithObjectMetadataTests: ObjectWithObjectMetadataTe func test__reader_with_connection__at_index_with_item() { configureForReadingSingle() reader = Read(connection) - XCTAssertTrue(checkTransactionDidReadItem(reader.atIndex(index))) + XCTAssertTrue(checkTransactionDidReadItem(reader.withMetadataAtIndex(index))) XCTAssertTrue(connection.didRead) } func test__reader_with_connection__at_index_with_no_item() { reader = Read(connection) - XCTAssertFalse(checkTransactionDidReadItem(reader.atIndex(index))) + XCTAssertFalse(checkTransactionDidReadItem(reader.withMetadataAtIndex(index))) XCTAssertTrue(connection.didRead) } func test__reader_with_connection__at_indexes_with_items() { configureForReadingMultiple() reader = Read(connection) - XCTAssertTrue(checkTransactionDidReadItems(reader.atIndexes(indexes))) + XCTAssertTrue(checkTransactionDidReadItems(reader.withMetadataAtIndexes(indexes))) XCTAssertTrue(connection.didRead) } func test__reader_with_connection__at_indexes_with_no_items() { reader = Read(connection) - XCTAssertFalse(checkTransactionDidReadItems(reader.atIndexes(indexes))) + XCTAssertFalse(checkTransactionDidReadItems(reader.withMetadataAtIndexes(indexes))) XCTAssertTrue(connection.didRead) } func test__reader_with_connection__by_key_with_item() { configureForReadingSingle() reader = Read(connection) - XCTAssertTrue(checkTransactionDidReadItem(reader.byKey(key))) + XCTAssertTrue(checkTransactionDidReadItem(reader.withMetadataByKey(key))) XCTAssertTrue(connection.didRead) } func test__reader_with_connection__by_key_with_no_item() { reader = Read(connection) - XCTAssertFalse(checkTransactionDidReadItem(reader.byKey(key))) + XCTAssertFalse(checkTransactionDidReadItem(reader.withMetadataByKey(key))) XCTAssertTrue(connection.didRead) } func test__reader_with_connection__by_keys_with_items() { configureForReadingMultiple() reader = Read(connection) - XCTAssertTrue(checkTransactionDidReadItems(reader.byKeys(keys))) + XCTAssertTrue(checkTransactionDidReadItems(reader.withMetadataByKeys(keys))) XCTAssertTrue(connection.didRead) } func test__reader_with_connection__by_keys_with_no_items() { reader = Read(connection) - XCTAssertFalse(checkTransactionDidReadItems(reader.byKeys(keys))) + XCTAssertFalse(checkTransactionDidReadItems(reader.withMetadataByKeys(keys))) XCTAssertTrue(connection.didRead) } func test__reader_with_connection__all_with_items() { configureForReadingMultiple() reader = Read(connection) - XCTAssertTrue(checkTransactionDidReadItems(reader.all())) + XCTAssertTrue(checkTransactionDidReadItems(reader.withMetadataAll())) XCTAssertTrue(connection.didRead) XCTAssertEqual(readTransaction.didKeysInCollection, TypeUnderTest.collection) } func test__reader_with_connection__all_with_no_items() { reader = Read(connection) - XCTAssertFalse(checkTransactionDidReadItems(reader.all())) + XCTAssertFalse(checkTransactionDidReadItems(reader.withMetadataAll())) XCTAssertTrue(connection.didRead) XCTAssertEqual(readTransaction.didKeysInCollection, TypeUnderTest.collection) } @@ -657,19 +654,19 @@ class Persistable_Read_ObjectWithObjectMetadataTests: ObjectWithObjectMetadataTe class Persistable_Write_ObjectWithObjectMetadataTests: ObjectWithObjectMetadataTests { func test__item_persistable__write_using_transaction() { - checkTransactionDidWriteItem(item.write(writeTransaction)) + checkTransactionDidWriteItem(item.writeWithMetadata(writeTransaction, metadata: metadata)) } func test__item_persistable__write_using_connection() { - checkTransactionDidWriteItem(item.write(connection)) + checkTransactionDidWriteItem(item.writeWithMetadata(connection, metadata: metadata)) XCTAssertTrue(connection.didWrite) } func test__item_persistable__write_async_using_connection() { let expectation = expectationWithDescription("Test: \(#function)") - var result: TypeUnderTest! = nil + var result: (TypeUnderTest, MetadataTypeUnderTest?)! = nil - item.asyncWrite(connection) { tmp in + item.asyncWriteWithMetadata(connection, metadata: metadata) { tmp in result = tmp expectation.fulfill() } @@ -681,7 +678,7 @@ class Persistable_Write_ObjectWithObjectMetadataTests: ObjectWithObjectMetadataT func test__item_persistable__write_using_opertion() { let expectation = expectationWithDescription("Test: \(#function)") - let operation = item.writeOperation(connection) + let operation = item.writeWithMetadataOperation(connection, metadata: metadata) operation.completionBlock = { expectation.fulfill() } @@ -691,24 +688,24 @@ class Persistable_Write_ObjectWithObjectMetadataTests: ObjectWithObjectMetadataT XCTAssertFalse(writeTransaction.didWriteAtIndexes.isEmpty) XCTAssertEqual(writeTransaction.didWriteAtIndexes[0].0, index) XCTAssertEqual(writeTransaction.didWriteAtIndexes[0].1.identifier, item.identifier) - XCTAssertEqual(writeTransaction.didWriteAtIndexes[0].2 as? NSDate, item.metadata) + XCTAssertEqual(writeTransaction.didWriteAtIndexes[0].2 as? NSDate, metadata) XCTAssertTrue(connection.didWrite) } func test__items_persistable__write_using_transaction() { - checkTransactionDidWriteItems(items.write(writeTransaction)) + checkTransactionDidWriteItems(items.writeWithMetadata(writeTransaction, metadata: metadatas)) } func test__items_persistable__write_using_connection() { - checkTransactionDidWriteItems(items.write(connection)) + checkTransactionDidWriteItems(items.writeWithMetadata(connection, metadata: metadatas)) XCTAssertTrue(connection.didWrite) } func test__items_persistable__write_async_using_connection() { let expectation = expectationWithDescription("Test: \(#function)") - var result: [TypeUnderTest] = [] + var result: [(TypeUnderTest, MetadataTypeUnderTest?)] = [] - items.asyncWrite(connection) { tmp in + items.asyncWriteWithMetadata(connection, metadata: metadatas) { tmp in result = tmp expectation.fulfill() } diff --git a/Tests/Shared/ObjectWithValueMetadataTests.swift b/Tests/Shared/ObjectWithValueMetadataTests.swift index ddbe249..919eec4 100644 --- a/Tests/Shared/ObjectWithValueMetadataTests.swift +++ b/Tests/Shared/ObjectWithValueMetadataTests.swift @@ -13,12 +13,15 @@ import XCTest class ObjectWithValueMetadataTests: XCTestCase { typealias TypeUnderTest = Manager + typealias MetadataTypeUnderTest = Manager.Metadata var item: TypeUnderTest! + var metadata: MetadataTypeUnderTest! var index: YapDB.Index! var key: String! var items: [TypeUnderTest]! + var metadatas: [MetadataTypeUnderTest?]! var indexes: [YapDB.Index]! var keys: [String]! @@ -56,9 +59,11 @@ class ObjectWithValueMetadataTests: XCTestCase { override func tearDown() { item = nil + metadata = nil index = nil key = nil items = nil + metadatas = nil indexes = nil keys = nil @@ -73,55 +78,60 @@ class ObjectWithValueMetadataTests: XCTestCase { func createPersistables() { item = TypeUnderTest(id: "beatle-1", name: "John") - item.metadata = TypeUnderTest.Metadata(numberOfDirectReports: 4) + metadata = MetadataTypeUnderTest(numberOfDirectReports: 4) items = [ item, TypeUnderTest(id: "beatle-2", name: "Paul"), TypeUnderTest(id: "beatle-3", name: "George"), TypeUnderTest(id: "beatle-4", name: "Ringo") ] - items.suffixFrom(1).forEach { $0.metadata = TypeUnderTest.Metadata(numberOfDirectReports: 1) } + metadatas = [ + metadata, + MetadataTypeUnderTest(numberOfDirectReports: 3), + MetadataTypeUnderTest(numberOfDirectReports: 2), + MetadataTypeUnderTest(numberOfDirectReports: 1) + ] } func configureForReadingSingle() { readTransaction.object = item - readTransaction.metadata = item.metadata?.encoded + readTransaction.metadata = metadata?.encoded } func configureForReadingMultiple() { readTransaction.objects = items - readTransaction.metadatas = items.map { $0.metadata?.encoded } + readTransaction.metadatas = metadatas.map { $0?.encoded } readTransaction.keys = keys } - func checkTransactionDidWriteItem(result: TypeUnderTest) { - XCTAssertEqual(result.identifier, item.identifier) + func checkTransactionDidWriteItem(result: (TypeUnderTest, MetadataTypeUnderTest?)) { + XCTAssertEqual(result.0.identifier, item.identifier) XCTAssertFalse(writeTransaction.didWriteAtIndexes.isEmpty) XCTAssertEqual(writeTransaction.didWriteAtIndexes[0].0, index) XCTAssertEqual(writeTransaction.didWriteAtIndexes[0].1.identifier, item.identifier) - XCTAssertEqual(TypeUnderTest.MetadataType.decode(writeTransaction.didWriteAtIndexes[0].2), item.metadata) + XCTAssertEqual(MetadataTypeUnderTest.decode(writeTransaction.didWriteAtIndexes[0].2), metadata) } - func checkTransactionDidWriteItems(result: [TypeUnderTest]) { + func checkTransactionDidWriteItems(result: [(TypeUnderTest, MetadataTypeUnderTest?)]) { XCTAssertFalse(writeTransaction.didWriteAtIndexes.isEmpty) XCTAssertEqual(writeTransaction.didWriteAtIndexes.map { $0.0.key }.sort(), indexes.map { $0.key }.sort()) XCTAssertEqual(writeTransaction.didWriteAtIndexes.map { $0.2 }.count, items.count) XCTAssertFalse(result.isEmpty) - XCTAssertEqual(Set(result), Set(items)) + XCTAssertEqual(Set(result.map({$0.0})), Set(items)) } - func checkTransactionDidReadItem(result: TypeUnderTest?) -> Bool { + func checkTransactionDidReadItem(result: (TypeUnderTest, MetadataTypeUnderTest?)?) -> Bool { guard let result = result else { return false } XCTAssertEqual(readTransaction.didReadAtIndex, index) - XCTAssertEqual(result.identifier, item.identifier) + XCTAssertEqual(result.0.identifier, item.identifier) XCTAssertEqual(readTransaction.didReadMetadataAtIndex, index) - XCTAssertEqual(result.metadata, item.metadata) + XCTAssertEqual(result.1, metadata) return true } - func checkTransactionDidReadItems(result: [TypeUnderTest]) -> Bool { + func checkTransactionDidReadItems(result: [(TypeUnderTest, MetadataTypeUnderTest?)]) -> Bool { if result.isEmpty { return false } @@ -145,7 +155,7 @@ class ObjectWithValueMetadataTests: XCTestCase { class Base_ObjectWithValueMetadataTests: ObjectWithValueMetadataTests { func test__metadata_is_not_nil() { - XCTAssertNotNil(item.metadata) + XCTAssertNotNil(metadata) } } @@ -155,116 +165,116 @@ class Functional_Read_ObjectWithValueMetadataTests: ObjectWithValueMetadataTests func test__transaction__read_at_index_with_data() { configureForReadingSingle() - XCTAssertTrue(checkTransactionDidReadItem(readTransaction.readAtIndex(index))) + XCTAssertTrue(checkTransactionDidReadItem(readTransaction.readWithMetadataAtIndex(index))) } func test__transaction__read_at_index_without_data() { - XCTAssertFalse(checkTransactionDidReadItem(readTransaction.readAtIndex(index))) + XCTAssertFalse(checkTransactionDidReadItem(readTransaction.readWithMetadataAtIndex(index))) } func test__transaction__read_at_indexes_with_data() { configureForReadingMultiple() - XCTAssertTrue(checkTransactionDidReadItems(readTransaction.readAtIndexes(indexes))) + XCTAssertTrue(checkTransactionDidReadItems(readTransaction.readWithMetadataAtIndexes(indexes))) } func test__transaction__read_at_indexes_with_data_2() { configureForReadingMultiple() - XCTAssertTrue(checkTransactionDidReadItems(readTransaction.readAtIndexes(Set(indexes)))) + XCTAssertTrue(checkTransactionDidReadItems(readTransaction.readWithMetadataAtIndexes(Set(indexes)))) } func test__transaction__read_at_indexes_without_data() { - XCTAssertFalse(checkTransactionDidReadItems(readTransaction.readAtIndexes(indexes))) + XCTAssertFalse(checkTransactionDidReadItems(readTransaction.readWithMetadataAtIndexes(indexes))) } func test__transaction__read_by_key_with_data() { configureForReadingSingle() - XCTAssertTrue(checkTransactionDidReadItem(readTransaction.readByKey(key))) + XCTAssertTrue(checkTransactionDidReadItem(readTransaction.readWithMetadataByKey(key))) } func test__transaction__read_by_key_without_data() { - XCTAssertFalse(checkTransactionDidReadItem(readTransaction.readByKey(key))) + XCTAssertFalse(checkTransactionDidReadItem(readTransaction.readWithMetadataByKey(key))) } func test__transaction__read_by_keys_with_data() { configureForReadingMultiple() - XCTAssertTrue(checkTransactionDidReadItems(readTransaction.readByKeys(keys))) + XCTAssertTrue(checkTransactionDidReadItems(readTransaction.readWithMetadataByKeys(keys))) } func test__transaction__read_by_keys_with_data_2() { configureForReadingMultiple() - XCTAssertTrue(checkTransactionDidReadItems(readTransaction.readByKeys(Set(keys)))) + XCTAssertTrue(checkTransactionDidReadItems(readTransaction.readWithMetadataByKeys(Set(keys)))) } func test__transaction__read_by_keys_without_data() { - XCTAssertFalse(checkTransactionDidReadItems(readTransaction.readByKeys(keys))) + XCTAssertFalse(checkTransactionDidReadItems(readTransaction.readWithMetadataByKeys(keys))) } func test__transaction__read_all_with_data() { configureForReadingMultiple() - XCTAssertTrue(checkTransactionDidReadItems(readTransaction.readAll())) + XCTAssertTrue(checkTransactionDidReadItems(readTransaction.readWithMetadataAll())) } // Functional API - ConnectionType - Reading func test__connection__read_at_index_with_data() { configureForReadingSingle() - XCTAssertTrue(checkTransactionDidReadItem(connection.readAtIndex(index))) + XCTAssertTrue(checkTransactionDidReadItem(connection.readWithMetadataAtIndex(index))) XCTAssertTrue(connection.didRead) } func test__connection__read_at_index_without_data() { - XCTAssertFalse(checkTransactionDidReadItem(connection.readAtIndex(index))) + XCTAssertFalse(checkTransactionDidReadItem(connection.readWithMetadataAtIndex(index))) XCTAssertTrue(connection.didRead) } func test__connection__read_at_indexes_with_data() { configureForReadingMultiple() - XCTAssertTrue(checkTransactionDidReadItems(connection.readAtIndexes(indexes))) + XCTAssertTrue(checkTransactionDidReadItems(connection.readWithMetadataAtIndexes(indexes))) XCTAssertTrue(connection.didRead) } func test__connection__read_at_indexes_with_data_2() { configureForReadingMultiple() - XCTAssertTrue(checkTransactionDidReadItems(connection.readAtIndexes(Set(indexes)))) + XCTAssertTrue(checkTransactionDidReadItems(connection.readWithMetadataAtIndexes(Set(indexes)))) XCTAssertTrue(connection.didRead) } func test__connection__read_at_indexes_without_data() { - XCTAssertFalse(checkTransactionDidReadItems(connection.readAtIndexes(indexes))) + XCTAssertFalse(checkTransactionDidReadItems(connection.readWithMetadataAtIndexes(indexes))) XCTAssertTrue(connection.didRead) } func test__connection__read_by_key_with_data() { configureForReadingSingle() - XCTAssertTrue(checkTransactionDidReadItem(connection.readByKey(key))) + XCTAssertTrue(checkTransactionDidReadItem(connection.readWithMetadataByKey(key))) XCTAssertTrue(connection.didRead) } func test__connection__read_by_key_without_data() { - XCTAssertFalse(checkTransactionDidReadItem(connection.readByKey(key))) + XCTAssertFalse(checkTransactionDidReadItem(connection.readWithMetadataByKey(key))) XCTAssertTrue(connection.didRead) } func test__connection__read_by_keys_with_data() { configureForReadingMultiple() - XCTAssertTrue(checkTransactionDidReadItems(connection.readByKeys(keys))) + XCTAssertTrue(checkTransactionDidReadItems(connection.readWithMetadataByKeys(keys))) XCTAssertTrue(connection.didRead) } func test__connection__read_by_keys_with_data_2() { configureForReadingMultiple() - XCTAssertTrue(checkTransactionDidReadItems(connection.readByKeys(Set(keys)))) + XCTAssertTrue(checkTransactionDidReadItems(connection.readWithMetadataByKeys(Set(keys)))) XCTAssertTrue(connection.didRead) } func test__connection__read_by_keys_without_data() { - XCTAssertFalse(checkTransactionDidReadItems(connection.readByKeys(keys))) + XCTAssertFalse(checkTransactionDidReadItems(connection.readWithMetadataByKeys(keys))) XCTAssertTrue(connection.didRead) } func test__connection__read_all_with_data() { configureForReadingMultiple() - XCTAssertTrue(checkTransactionDidReadItems(connection.readAll())) + XCTAssertTrue(checkTransactionDidReadItems(connection.readWithMetadataAll())) XCTAssertTrue(connection.didRead) } } @@ -272,38 +282,29 @@ class Functional_Read_ObjectWithValueMetadataTests: ObjectWithValueMetadataTests class Functional_Write_ObjectWithValueMetadataTests: ObjectWithValueMetadataTests { func test__transaction__write_item() { - checkTransactionDidWriteItem(writeTransaction.write(item)) + checkTransactionDidWriteItem(writeTransaction.writeWithMetadata((item, metadata))) } func test__transaction__write_items() { - checkTransactionDidWriteItems(writeTransaction.write(items)) - } - - func test__transaction__write_items_2() { - checkTransactionDidWriteItems(writeTransaction.write(Set(items))) + checkTransactionDidWriteItems(writeTransaction.writeWithMetadata(zipToWrite(items, metadatas))) } // MARK: - Functional API - Connection - Writing func test__connection__write_item() { - checkTransactionDidWriteItem(connection.write(item)) + checkTransactionDidWriteItem(connection.writeWithMetadata((item, metadata))) XCTAssertTrue(connection.didWrite) } func test__connection__write_items() { - checkTransactionDidWriteItems(connection.write(items)) - XCTAssertTrue(connection.didWrite) - } - - func test__connection__write_items_2() { - checkTransactionDidWriteItems(connection.write(Set(items))) + checkTransactionDidWriteItems(connection.writeWithMetadata(zipToWrite(items, metadatas))) XCTAssertTrue(connection.didWrite) } func test__connection__async_write_item() { - var result: TypeUnderTest! + var result: (TypeUnderTest, MetadataTypeUnderTest?)! let expectation = expectationWithDescription("Test: \(#function)") - connection.asyncWrite(item) { tmp in + connection.asyncWriteWithMetadata((item, metadata)) { tmp in result = tmp expectation.fulfill() } @@ -313,9 +314,9 @@ class Functional_Write_ObjectWithValueMetadataTests: ObjectWithValueMetadataTest } func test__connection__async_write_items() { - var result: [TypeUnderTest] = [] + var result: [(TypeUnderTest, MetadataTypeUnderTest?)] = [] let expectation = expectationWithDescription("Test: \(#function)") - connection.asyncWrite(items) { received in + connection.asyncWriteWithMetadata(zipToWrite(items, metadatas)) { received in result = received expectation.fulfill() } @@ -382,45 +383,45 @@ class Curried_Read_ObjectWithValueMetadataTests: ObjectWithValueMetadataTests { func test__curried__read_at_index_with_data() { configureForReadingSingle() - XCTAssertTrue(checkTransactionDidReadItem(connection.read(TypeUnderTest.readAtIndex(index)))) + XCTAssertTrue(checkTransactionDidReadItem(connection.read(TypeUnderTest.readWithMetadataAtIndex(index)))) XCTAssertTrue(connection.didRead) } func test__curried__read_at_index_with_no_data() { - XCTAssertFalse(checkTransactionDidReadItem(connection.read(TypeUnderTest.readAtIndex(index)))) + XCTAssertFalse(checkTransactionDidReadItem(connection.read(TypeUnderTest.readWithMetadataAtIndex(index)))) XCTAssertTrue(connection.didRead) } func test__curried__read_at_indexes_with_data() { configureForReadingMultiple() - XCTAssertTrue(checkTransactionDidReadItems(connection.read(TypeUnderTest.readAtIndexes(indexes)))) + XCTAssertTrue(checkTransactionDidReadItems(connection.read(TypeUnderTest.readWithMetadataAtIndexes(indexes)))) XCTAssertTrue(connection.didRead) } func test__curried__read_at_indexes_with_no_data() { - XCTAssertFalse(checkTransactionDidReadItems(connection.read(TypeUnderTest.readAtIndexes(indexes)))) + XCTAssertFalse(checkTransactionDidReadItems(connection.read(TypeUnderTest.readWithMetadataAtIndexes(indexes)))) XCTAssertTrue(connection.didRead) } func test__curried__read_by_key_with_data() { configureForReadingSingle() - XCTAssertTrue(checkTransactionDidReadItem(connection.read(TypeUnderTest.readByKey(key)))) + XCTAssertTrue(checkTransactionDidReadItem(connection.read(TypeUnderTest.readWithMetadataByKey(key)))) XCTAssertTrue(connection.didRead) } func test__curried__read_by_key_with_no_data() { - XCTAssertFalse(checkTransactionDidReadItem(connection.read(TypeUnderTest.readByKey(key)))) + XCTAssertFalse(checkTransactionDidReadItem(connection.read(TypeUnderTest.readWithMetadataByKey(key)))) XCTAssertTrue(connection.didRead) } func test__curried__read_by_keys_with_data() { configureForReadingMultiple() - XCTAssertTrue(checkTransactionDidReadItems(connection.read(TypeUnderTest.readByKeys(keys)))) + XCTAssertTrue(checkTransactionDidReadItems(connection.read(TypeUnderTest.readWithMetadataByKeys(keys)))) XCTAssertTrue(connection.didRead) } func test__curried__read_by_keys_with_no_data() { - XCTAssertFalse(checkTransactionDidReadItems(connection.read(TypeUnderTest.readByKeys(keys)))) + XCTAssertFalse(checkTransactionDidReadItems(connection.read(TypeUnderTest.readWithMetadataByKeys(keys)))) XCTAssertTrue(connection.didRead) } } @@ -428,7 +429,7 @@ class Curried_Read_ObjectWithValueMetadataTests: ObjectWithValueMetadataTests { class Curried_Write_ObjectWithValueMetadataTests: ObjectWithValueMetadataTests { func test__curried__write() { - checkTransactionDidWriteItem(connection.write(item.write())) + checkTransactionDidWriteItem(connection.write(item.writeWithMetadata(metadata))) XCTAssertTrue(connection.didWrite) } } @@ -440,69 +441,69 @@ class Persistable_Read_ObjectWithValueMetadataTests: ObjectWithValueMetadataTest func test__reader__in_transaction_at_index() { configureForReadingSingle() reader = Read(readTransaction) - XCTAssertTrue(checkTransactionDidReadItem(reader.inTransaction(readTransaction, atIndex: index))) + XCTAssertTrue(checkTransactionDidReadItem(reader.withMetadataInTransaction(readTransaction, atIndex: index))) } func test__reader__in_transaction_at_index_2() { configureForReadingSingle() reader = Read(readTransaction) - let atIndex = reader.inTransactionAtIndex(readTransaction) + let atIndex: YapDB.Index -> (TypeUnderTest, MetadataTypeUnderTest?)? = reader.withMetadataInTransactionAtIndex(readTransaction) XCTAssertTrue(checkTransactionDidReadItem(atIndex(index))) } func test__reader__at_index_in_transaction() { configureForReadingSingle() reader = Read(readTransaction) - let inTransaction = reader.atIndexInTransaction(index) + let inTransaction: TestableReadTransaction -> (TypeUnderTest, MetadataTypeUnderTest?)? = reader.withMetadataAtIndexInTransaction(index) XCTAssertTrue(checkTransactionDidReadItem(inTransaction(readTransaction))) } func test__reader__at_indexes_in_transaction_with_items() { configureForReadingMultiple() reader = Read(readTransaction) - XCTAssertTrue(checkTransactionDidReadItems(reader.atIndexesInTransaction(indexes)(readTransaction))) + XCTAssertTrue(checkTransactionDidReadItems(reader.withMetadataAtIndexesInTransaction(indexes)(readTransaction))) } func test__reader__at_indexes_in_transaction_with_no_items() { reader = Read(readTransaction) - XCTAssertFalse(checkTransactionDidReadItems(reader.atIndexesInTransaction(indexes)(readTransaction))) + XCTAssertFalse(checkTransactionDidReadItems(reader.withMetadataAtIndexesInTransaction(indexes)(readTransaction))) } func test__reader__in_transaction_by_key() { configureForReadingSingle() reader = Read(readTransaction) - XCTAssertTrue(checkTransactionDidReadItem(reader.inTransaction(readTransaction, byKey: key))) + XCTAssertTrue(checkTransactionDidReadItem(reader.withMetadataInTransaction(readTransaction, byKey: key))) } func test__reader__in_transaction_by_key_2() { configureForReadingSingle() reader = Read(readTransaction) - let byKey = reader.inTransactionByKey(readTransaction) + let byKey: String -> (TypeUnderTest, MetadataTypeUnderTest?)? = reader.withMetadataInTransactionByKey(readTransaction) XCTAssertTrue(checkTransactionDidReadItem(byKey(key))) } func test__reader__by_key_in_transaction() { configureForReadingSingle() reader = Read(readTransaction) - let inTransaction = reader.byKeyInTransaction(key) + let inTransaction: TestableReadTransaction -> (TypeUnderTest, MetadataTypeUnderTest?)? = reader.withMetadataByKeyInTransaction(key) XCTAssertTrue(checkTransactionDidReadItem(inTransaction(readTransaction))) } func test__reader__by_keys_in_transaction_with_items() { configureForReadingMultiple() reader = Read(readTransaction) - XCTAssertTrue(checkTransactionDidReadItems(reader.byKeysInTransaction(keys)(readTransaction))) + XCTAssertTrue(checkTransactionDidReadItems(reader.withMetadataByKeysInTransaction(keys)(readTransaction))) } func test__reader__by_keys_in_transaction_with_items_with_keys() { configureForReadingMultiple() reader = Read(readTransaction) - XCTAssertTrue(checkTransactionDidReadItems(reader.byKeysInTransaction()(readTransaction))) + XCTAssertTrue(checkTransactionDidReadItems(reader.withMetadataByKeysInTransaction()(readTransaction))) } func test__reader__by_keys_in_transaction_with_no_items() { reader = Read(readTransaction) - XCTAssertFalse(checkTransactionDidReadItems(reader.byKeysInTransaction(keys)(readTransaction))) + XCTAssertFalse(checkTransactionDidReadItems(reader.withMetadataByKeysInTransaction(keys)(readTransaction))) } // Reading - With Transaction @@ -510,66 +511,66 @@ class Persistable_Read_ObjectWithValueMetadataTests: ObjectWithValueMetadataTest func test__reader_with_transaction__at_index_with_item() { configureForReadingSingle() reader = Read(readTransaction) - XCTAssertTrue(checkTransactionDidReadItem(reader.inTransaction(readTransaction, atIndex: index))) + XCTAssertTrue(checkTransactionDidReadItem(reader.withMetadataInTransaction(readTransaction, atIndex: index))) } func test__reader_with_transaction__at_index_with_no_item() { reader = Read(readTransaction) - XCTAssertFalse(checkTransactionDidReadItem(reader.atIndex(index))) + XCTAssertFalse(checkTransactionDidReadItem(reader.withMetadataAtIndex(index))) } func test__reader_with_transaction__at_indexes_with_items() { configureForReadingMultiple() reader = Read(readTransaction) - XCTAssertTrue(checkTransactionDidReadItems(reader.atIndexes(indexes))) + XCTAssertTrue(checkTransactionDidReadItems(reader.withMetadataAtIndexes(indexes))) } func test__reader_with_transaction__at_indexes_with_no_items() { reader = Read(readTransaction) - XCTAssertFalse(checkTransactionDidReadItems(reader.atIndexes(indexes))) + XCTAssertFalse(checkTransactionDidReadItems(reader.withMetadataAtIndexes(indexes))) } func test__reader_with_transaction__by_key_with_item() { configureForReadingSingle() reader = Read(readTransaction) - XCTAssertTrue(checkTransactionDidReadItem(reader.inTransaction(readTransaction, atIndex: index))) + XCTAssertTrue(checkTransactionDidReadItem(reader.withMetadataInTransaction(readTransaction, atIndex: index))) } func test__reader_with_transaction__by_key_with_no_item() { reader = Read(readTransaction) - XCTAssertFalse(checkTransactionDidReadItem(reader.byKey(key))) + XCTAssertFalse(checkTransactionDidReadItem(reader.withMetadataByKey(key))) } func test__reader_with_transaction__by_keys_with_items() { configureForReadingMultiple() reader = Read(readTransaction) - XCTAssertTrue(checkTransactionDidReadItems(reader.byKeys(keys))) + XCTAssertTrue(checkTransactionDidReadItems(reader.withMetadataByKeys(keys))) } func test__reader_with_transaction__by_keys_with_no_items() { reader = Read(readTransaction) - XCTAssertFalse(checkTransactionDidReadItems(reader.byKeys(keys))) + XCTAssertFalse(checkTransactionDidReadItems(reader.withMetadataByKeys(keys))) } func test__reader_with_transaction__all_with_items() { configureForReadingMultiple() reader = Read(readTransaction) - XCTAssertTrue(checkTransactionDidReadItems(reader.all())) + XCTAssertTrue(checkTransactionDidReadItems(reader.withMetadataAll())) XCTAssertEqual(readTransaction.didKeysInCollection, TypeUnderTest.collection) } func test__reader_with_transaction__all_with_no_items() { reader = Read(readTransaction) - XCTAssertFalse(checkTransactionDidReadItems(reader.all())) + XCTAssertFalse(checkTransactionDidReadItems(reader.withMetadataAll())) XCTAssertEqual(readTransaction.didKeysInCollection, TypeUnderTest.collection) } func test__reader_with_transaction__filter() { configureForReadingSingle() reader = Read(readTransaction) - let (items, missing) = reader.filterExisting(keys) + let (items, missing): ([(TypeUnderTest, MetadataTypeUnderTest?)], [String]) = reader.withMetadataFilterExisting(keys) XCTAssertEqual(readTransaction.didReadAtIndexes.first!, indexes.first!) - XCTAssertEqual(items.map { $0.identifier }, items.prefixUpTo(1).map { $0.identifier }) + XCTAssertEqual(items.map { $0.0.identifier }, items.prefixUpTo(1).map { $0.0.identifier }) XCTAssertEqual(missing, Array(keys.suffixFrom(1))) } @@ -578,66 +579,66 @@ class Persistable_Read_ObjectWithValueMetadataTests: ObjectWithValueMetadataTest func test__reader_with_connection__at_index_with_item() { configureForReadingSingle() reader = Read(connection) - XCTAssertTrue(checkTransactionDidReadItem(reader.atIndex(index))) + XCTAssertTrue(checkTransactionDidReadItem(reader.withMetadataAtIndex(index))) XCTAssertTrue(connection.didRead) } func test__reader_with_connection__at_index_with_no_item() { reader = Read(connection) - XCTAssertFalse(checkTransactionDidReadItem(reader.atIndex(index))) + XCTAssertFalse(checkTransactionDidReadItem(reader.withMetadataAtIndex(index))) XCTAssertTrue(connection.didRead) } func test__reader_with_connection__at_indexes_with_items() { configureForReadingMultiple() reader = Read(connection) - XCTAssertTrue(checkTransactionDidReadItems(reader.atIndexes(indexes))) + XCTAssertTrue(checkTransactionDidReadItems(reader.withMetadataAtIndexes(indexes))) XCTAssertTrue(connection.didRead) } func test__reader_with_connection__at_indexes_with_no_items() { reader = Read(connection) - XCTAssertFalse(checkTransactionDidReadItems(reader.atIndexes(indexes))) + XCTAssertFalse(checkTransactionDidReadItems(reader.withMetadataAtIndexes(indexes))) XCTAssertTrue(connection.didRead) } func test__reader_with_connection__by_key_with_item() { configureForReadingSingle() reader = Read(connection) - XCTAssertTrue(checkTransactionDidReadItem(reader.byKey(key))) + XCTAssertTrue(checkTransactionDidReadItem(reader.withMetadataByKey(key))) XCTAssertTrue(connection.didRead) } func test__reader_with_connection__by_key_with_no_item() { reader = Read(connection) - XCTAssertFalse(checkTransactionDidReadItem(reader.byKey(key))) + XCTAssertFalse(checkTransactionDidReadItem(reader.withMetadataByKey(key))) XCTAssertTrue(connection.didRead) } func test__reader_with_connection__by_keys_with_items() { configureForReadingMultiple() reader = Read(connection) - XCTAssertTrue(checkTransactionDidReadItems(reader.byKeys(keys))) + XCTAssertTrue(checkTransactionDidReadItems(reader.withMetadataByKeys(keys))) XCTAssertTrue(connection.didRead) } func test__reader_with_connection__by_keys_with_no_items() { reader = Read(connection) - XCTAssertFalse(checkTransactionDidReadItems(reader.byKeys(keys))) + XCTAssertFalse(checkTransactionDidReadItems(reader.withMetadataByKeys(keys))) XCTAssertTrue(connection.didRead) } func test__reader_with_connection__all_with_items() { configureForReadingMultiple() reader = Read(connection) - XCTAssertTrue(checkTransactionDidReadItems(reader.all())) + XCTAssertTrue(checkTransactionDidReadItems(reader.withMetadataAll())) XCTAssertTrue(connection.didRead) XCTAssertEqual(readTransaction.didKeysInCollection, TypeUnderTest.collection) } func test__reader_with_connection__all_with_no_items() { reader = Read(connection) - XCTAssertFalse(checkTransactionDidReadItems(reader.all())) + XCTAssertFalse(checkTransactionDidReadItems(reader.withMetadataAll())) XCTAssertTrue(connection.didRead) XCTAssertEqual(readTransaction.didKeysInCollection, TypeUnderTest.collection) } @@ -657,19 +658,19 @@ class Persistable_Read_ObjectWithValueMetadataTests: ObjectWithValueMetadataTest class Persistable_Write_ObjectWithValueMetadataTests: ObjectWithValueMetadataTests { func test__item_persistable__write_using_transaction() { - checkTransactionDidWriteItem(item.write(writeTransaction)) + checkTransactionDidWriteItem(item.writeWithMetadata(writeTransaction, metadata: metadata)) } func test__item_persistable__write_using_connection() { - checkTransactionDidWriteItem(item.write(connection)) + checkTransactionDidWriteItem(item.writeWithMetadata(connection, metadata: metadata)) XCTAssertTrue(connection.didWrite) } func test__item_persistable__write_async_using_connection() { let expectation = expectationWithDescription("Test: \(#function)") - var result: TypeUnderTest! = nil + var result: (TypeUnderTest, MetadataTypeUnderTest?)! = nil - item.asyncWrite(connection) { tmp in + item.asyncWriteWithMetadata(connection, metadata: metadata) { tmp in result = tmp expectation.fulfill() } @@ -681,7 +682,7 @@ class Persistable_Write_ObjectWithValueMetadataTests: ObjectWithValueMetadataTes func test__item_persistable__write_using_opertion() { let expectation = expectationWithDescription("Test: \(#function)") - let operation = item.writeOperation(connection) + let operation = item.writeWithMetadataOperation(connection, metadata: metadata) operation.completionBlock = { expectation.fulfill() } @@ -691,24 +692,24 @@ class Persistable_Write_ObjectWithValueMetadataTests: ObjectWithValueMetadataTes XCTAssertFalse(writeTransaction.didWriteAtIndexes.isEmpty) XCTAssertEqual(writeTransaction.didWriteAtIndexes[0].0, index) XCTAssertEqual(writeTransaction.didWriteAtIndexes[0].1.identifier, item.identifier) - XCTAssertEqual(TypeUnderTest.MetadataType.decode(writeTransaction.didWriteAtIndexes[0].2), item.metadata) + XCTAssertEqual(MetadataTypeUnderTest.decode(writeTransaction.didWriteAtIndexes[0].2), metadata) XCTAssertTrue(connection.didWrite) } func test__items_persistable__write_using_transaction() { - checkTransactionDidWriteItems(items.write(writeTransaction)) + checkTransactionDidWriteItems(items.writeWithMetadata(writeTransaction, metadata: metadatas)) } func test__items_persistable__write_using_connection() { - checkTransactionDidWriteItems(items.write(connection)) + checkTransactionDidWriteItems(items.writeWithMetadata(connection, metadata: metadatas)) XCTAssertTrue(connection.didWrite) } func test__items_persistable__write_async_using_connection() { let expectation = expectationWithDescription("Test: \(#function)") - var result: [TypeUnderTest] = [] + var result: [(TypeUnderTest, MetadataTypeUnderTest?)] = [] - items.asyncWrite(connection) { tmp in + items.asyncWriteWithMetadata(connection, metadata: metadatas) { tmp in result = tmp expectation.fulfill() } diff --git a/Tests/Shared/ReadWriteTests.swift b/Tests/Shared/ReadWriteTests.swift index ae0f920..f1c14c3 100644 --- a/Tests/Shared/ReadWriteTests.swift +++ b/Tests/Shared/ReadWriteTests.swift @@ -10,10 +10,12 @@ import YapDatabase class ReadWriteBaseTests: XCTestCase { var item: Employee! + var metadata: NSDate! var index: YapDB.Index! var key: String! var items: [Employee]! + var metadatas: [NSDate?]! var indexes: [YapDB.Index]! var keys: [String]! @@ -29,9 +31,11 @@ class ReadWriteBaseTests: XCTestCase { override func tearDown() { item = nil + metadata = nil index = nil key = nil items = nil + metadatas = nil indexes = nil keys = nil super.tearDown() @@ -39,18 +43,23 @@ class ReadWriteBaseTests: XCTestCase { func createPersistables() { item = Employee(id: "beatle-1", name: "John") - item.metadata = NSDate() + metadata = NSDate() items = [ item, Employee(id: "beatle-2", name: "Paul"), Employee(id: "beatle-3", name: "George"), Employee(id: "beatle-4", name: "Ringo") ] - items.suffixFrom(1).forEach { $0.metadata = NSDate() } + metadatas = [ + metadata, + NSDate(), + NSDate(), + NSDate() + ] } func writeItemsToDatabase(db: YapDatabase) { - db.makeNewConnection().write(items) + db.makeNewConnection().writeWithMetadata(zipToWrite(items, metadatas)) } } diff --git a/Tests/Shared/ValueWithNoMetadataTests.swift b/Tests/Shared/ValueWithNoMetadataTests.swift index 7c61aec..dc15538 100644 --- a/Tests/Shared/ValueWithNoMetadataTests.swift +++ b/Tests/Shared/ValueWithNoMetadataTests.swift @@ -114,7 +114,6 @@ class ValueWithNoMetadataTests: XCTestCase { } XCTAssertEqual(readTransaction.didReadAtIndex, index) XCTAssertEqual(result.identifier, item.identifier) - XCTAssertNil(result.metadata) return true } @@ -137,18 +136,6 @@ class ValueWithNoMetadataTests: XCTestCase { } } -class Base_ValueWithNoMetadataTests: ValueWithNoMetadataTests { - - func test__metadata_is_nil() { - XCTAssertNil(item.metadata) - } - - func test__metadata_cannot_be_set() { - item.metadata = Void() - XCTAssertNil(item.metadata) - } -} - class Functional_Read_ValueWithNoMetadataTests: ValueWithNoMetadataTests { // Functional API - ReadTransactionType - Reading diff --git a/Tests/Shared/ValueWithObjectMetadataTests.swift b/Tests/Shared/ValueWithObjectMetadataTests.swift index 185e02f..80e9b14 100644 --- a/Tests/Shared/ValueWithObjectMetadataTests.swift +++ b/Tests/Shared/ValueWithObjectMetadataTests.swift @@ -13,12 +13,15 @@ import XCTest class ValueWithObjectMetadataTests: XCTestCase { typealias TypeUnderTest = Inventory + typealias MetadataTypeUnderTest = NSNumber var item: TypeUnderTest! + var metadata: MetadataTypeUnderTest! var index: YapDB.Index! var key: String! var items: [TypeUnderTest]! + var metadatas: [MetadataTypeUnderTest?]! var indexes: [YapDB.Index]! var keys: [String]! @@ -56,9 +59,11 @@ class ValueWithObjectMetadataTests: XCTestCase { override func tearDown() { item = nil + metadata = nil index = nil key = nil items = nil + metadatas = nil indexes = nil keys = nil @@ -74,74 +79,77 @@ class ValueWithObjectMetadataTests: XCTestCase { func createPersistables() { let products = [ Product( - metadata: Product.Metadata(categoryIdentifier: 1), identifier: "vodka-123", name: "Belvidere", barcode: .UPCA(1, 2, 3, 4) ), Product( - metadata: Product.Metadata(categoryIdentifier: 2), identifier: "gin-123", name: "Boxer Gin", barcode: .UPCA(5, 10, 15, 20) ), Product( - metadata: Product.Metadata(categoryIdentifier: 3), identifier: "rum-123", name: "Mount Gay Rum", barcode: .UPCA(12, 24, 39, 48) ), Product( - metadata: Product.Metadata(categoryIdentifier: 2), identifier: "gin-234", name: "Monkey 47", barcode: .UPCA(31, 62, 93, 124) ) ] - items = products.map { TypeUnderTest(product: $0, metadata: NSNumber(integer: 12)) } + metadatas = [ + NSNumber(integer: 12), + NSNumber(integer: 13), + NSNumber(integer: 14), + NSNumber(integer: 15) + ] + items = products.map { TypeUnderTest(product: $0) } item = items[0] + metadata = metadatas[0] } func configureForReadingSingle() { readTransaction.object = item.encoded - readTransaction.metadata = item.metadata + readTransaction.metadata = metadata } func configureForReadingMultiple() { readTransaction.objects = items.encoded - readTransaction.metadatas = items.map { $0.metadata } + readTransaction.metadatas = metadatas.map { $0 } readTransaction.keys = keys } - func checkTransactionDidWriteItem(result: TypeUnderTest) { - XCTAssertEqual(result.identifier, item.identifier) + func checkTransactionDidWriteItem(result: (TypeUnderTest, MetadataTypeUnderTest?)) { + XCTAssertEqual(result.0.identifier, item.identifier) XCTAssertFalse(writeTransaction.didWriteAtIndexes.isEmpty) XCTAssertEqual(writeTransaction.didWriteAtIndexes[0].0, index) XCTAssertEqual(TypeUnderTest.decode(writeTransaction.didWriteAtIndexes[0].1)!, item) - XCTAssertEqual(writeTransaction.didWriteAtIndexes[0].2 as? NSNumber, item.metadata) + XCTAssertEqual(writeTransaction.didWriteAtIndexes[0].2 as? NSNumber, metadata) } - func checkTransactionDidWriteItems(result: [TypeUnderTest]) { + func checkTransactionDidWriteItems(result: [(TypeUnderTest, MetadataTypeUnderTest?)]) { XCTAssertFalse(writeTransaction.didWriteAtIndexes.isEmpty) XCTAssertEqual(writeTransaction.didWriteAtIndexes.map { $0.0.key }.sort(), indexes.map { $0.key }.sort()) XCTAssertEqual(writeTransaction.didWriteAtIndexes.map { $0.2 }.count, items.count) XCTAssertFalse(result.isEmpty) - XCTAssertEqual(Set(result), Set(items)) + XCTAssertEqual(Set(result.map({$0.0})), Set(items)) } - func checkTransactionDidReadItem(result: TypeUnderTest?) -> Bool { + func checkTransactionDidReadItem(result: (TypeUnderTest, MetadataTypeUnderTest?)?) -> Bool { XCTAssertEqual(readTransaction.didReadAtIndex, index) guard let result = result else { return false } XCTAssertEqual(readTransaction.didReadMetadataAtIndex, index) - XCTAssertEqual(result.identifier, item.identifier) - XCTAssertEqual(result.metadata, item.metadata) + XCTAssertEqual(result.0.identifier, item.identifier) + XCTAssertEqual(result.1, metadata) return true } - func checkTransactionDidReadItems(result: [TypeUnderTest]) -> Bool { + func checkTransactionDidReadItems(result: [(TypeUnderTest, MetadataTypeUnderTest?)]) -> Bool { if result.isEmpty { return false } @@ -150,17 +158,17 @@ class ValueWithObjectMetadataTests: XCTestCase { return true } - func checkTransactionDidReadMetadataItem(result: TypeUnderTest.MetadataType?) -> Bool { + func checkTransactionDidReadMetadataItem(result: MetadataTypeUnderTest?) -> Bool { XCTAssertNil(readTransaction.didReadAtIndex) guard let result = result else { return false } XCTAssertEqual(readTransaction.didReadMetadataAtIndex, index) - XCTAssertEqual(result, item.metadata) + XCTAssertEqual(result, metadata) return true } - func checkTransactionDidReadMetadataItems(result: [TypeUnderTest.MetadataType]) -> Bool { + func checkTransactionDidReadMetadataItems(result: [MetadataTypeUnderTest]) -> Bool { XCTAssertTrue(readTransaction.didReadAtIndexes.isEmpty) if result.isEmpty { return false @@ -186,7 +194,7 @@ class ValueWithObjectMetadataTests: XCTestCase { class Base_ValueWithObjectMetadataTests: ValueWithObjectMetadataTests { func test__metadata_is_not_nil() { - XCTAssertNotNil(item.metadata) + XCTAssertNotNil(metadata) } } @@ -196,116 +204,116 @@ class Functional_Read_ValueWithObjectMetadataTests: ValueWithObjectMetadataTests func test__transaction__read_at_index_with_data() { configureForReadingSingle() - XCTAssertTrue(checkTransactionDidReadItem(readTransaction.readAtIndex(index))) + XCTAssertTrue(checkTransactionDidReadItem(readTransaction.readWithMetadataAtIndex(index))) } func test__transaction__read_at_index_without_data() { - XCTAssertFalse(checkTransactionDidReadItem(readTransaction.readAtIndex(index))) + XCTAssertFalse(checkTransactionDidReadItem(readTransaction.readWithMetadataAtIndex(index))) } func test__transaction__read_at_indexes_with_data() { configureForReadingMultiple() - XCTAssertTrue(checkTransactionDidReadItems(readTransaction.readAtIndexes(indexes))) + XCTAssertTrue(checkTransactionDidReadItems(readTransaction.readWithMetadataAtIndexes(indexes))) } func test__transaction__read_at_indexes_with_data_2() { configureForReadingMultiple() - XCTAssertTrue(checkTransactionDidReadItems(readTransaction.readAtIndexes(Set(indexes)))) + XCTAssertTrue(checkTransactionDidReadItems(readTransaction.readWithMetadataAtIndexes(Set(indexes)))) } func test__transaction__read_at_indexes_without_data() { - XCTAssertFalse(checkTransactionDidReadItems(readTransaction.readAtIndexes(indexes))) + XCTAssertFalse(checkTransactionDidReadItems(readTransaction.readWithMetadataAtIndexes(indexes))) } func test__transaction__read_by_key_with_data() { configureForReadingSingle() - XCTAssertTrue(checkTransactionDidReadItem(readTransaction.readByKey(key))) + XCTAssertTrue(checkTransactionDidReadItem(readTransaction.readWithMetadataByKey(key))) } func test__transaction__read_by_key_without_data() { - XCTAssertFalse(checkTransactionDidReadItem(readTransaction.readByKey(key))) + XCTAssertFalse(checkTransactionDidReadItem(readTransaction.readWithMetadataByKey(key))) } func test__transaction__read_by_keys_with_data() { configureForReadingMultiple() - XCTAssertTrue(checkTransactionDidReadItems(readTransaction.readByKeys(keys))) + XCTAssertTrue(checkTransactionDidReadItems(readTransaction.readWithMetadataByKeys(keys))) } func test__transaction__read_by_keys_with_data_2() { configureForReadingMultiple() - XCTAssertTrue(checkTransactionDidReadItems(readTransaction.readByKeys(Set(keys)))) + XCTAssertTrue(checkTransactionDidReadItems(readTransaction.readWithMetadataByKeys(Set(keys)))) } func test__transaction__read_by_keys_without_data() { - XCTAssertFalse(checkTransactionDidReadItems(readTransaction.readByKeys(keys))) + XCTAssertFalse(checkTransactionDidReadItems(readTransaction.readWithMetadataByKeys(keys))) } func test__transaction__read_all_with_data() { configureForReadingMultiple() - XCTAssertTrue(checkTransactionDidReadItems(readTransaction.readAll())) + XCTAssertTrue(checkTransactionDidReadItems(readTransaction.readWithMetadataAll())) } // Functional API - ConnectionType - Reading func test__connection__read_at_index_with_data() { configureForReadingSingle() - XCTAssertTrue(checkTransactionDidReadItem(connection.readAtIndex(index))) + XCTAssertTrue(checkTransactionDidReadItem(connection.readWithMetadataAtIndex(index))) XCTAssertTrue(connection.didRead) } func test__connection__read_at_index_without_data() { - XCTAssertFalse(checkTransactionDidReadItem(connection.readAtIndex(index))) + XCTAssertFalse(checkTransactionDidReadItem(connection.readWithMetadataAtIndex(index))) XCTAssertTrue(connection.didRead) } func test__connection__read_at_indexes_with_data() { configureForReadingMultiple() - XCTAssertTrue(checkTransactionDidReadItems(connection.readAtIndexes(indexes))) + XCTAssertTrue(checkTransactionDidReadItems(connection.readWithMetadataAtIndexes(indexes))) XCTAssertTrue(connection.didRead) } func test__connection__read_at_indexes_with_data_2() { configureForReadingMultiple() - XCTAssertTrue(checkTransactionDidReadItems(connection.readAtIndexes(Set(indexes)))) + XCTAssertTrue(checkTransactionDidReadItems(connection.readWithMetadataAtIndexes(Set(indexes)))) XCTAssertTrue(connection.didRead) } func test__connection__read_at_indexes_without_data() { - XCTAssertFalse(checkTransactionDidReadItems(connection.readAtIndexes(indexes))) + XCTAssertFalse(checkTransactionDidReadItems(connection.readWithMetadataAtIndexes(indexes))) XCTAssertTrue(connection.didRead) } func test__connection__read_by_key_with_data() { configureForReadingSingle() - XCTAssertTrue(checkTransactionDidReadItem(connection.readByKey(key))) + XCTAssertTrue(checkTransactionDidReadItem(connection.readWithMetadataByKey(key))) XCTAssertTrue(connection.didRead) } func test__connection__read_by_key_without_data() { - XCTAssertFalse(checkTransactionDidReadItem(connection.readByKey(key))) + XCTAssertFalse(checkTransactionDidReadItem(connection.readWithMetadataByKey(key))) XCTAssertTrue(connection.didRead) } func test__connection__read_by_keys_with_data() { configureForReadingMultiple() - XCTAssertTrue(checkTransactionDidReadItems(connection.readByKeys(keys))) + XCTAssertTrue(checkTransactionDidReadItems(connection.readWithMetadataByKeys(keys))) XCTAssertTrue(connection.didRead) } func test__connection__read_by_keys_with_data_2() { configureForReadingMultiple() - XCTAssertTrue(checkTransactionDidReadItems(connection.readByKeys(Set(keys)))) + XCTAssertTrue(checkTransactionDidReadItems(connection.readWithMetadataByKeys(Set(keys)))) XCTAssertTrue(connection.didRead) } func test__connection__read_by_keys_without_data() { - XCTAssertFalse(checkTransactionDidReadItems(connection.readByKeys(keys))) + XCTAssertFalse(checkTransactionDidReadItems(connection.readWithMetadataByKeys(keys))) XCTAssertTrue(connection.didRead) } func test__connection__read_all_with_data() { configureForReadingMultiple() - XCTAssertTrue(checkTransactionDidReadItems(connection.readAll())) + XCTAssertTrue(checkTransactionDidReadItems(connection.readWithMetadataAll())) XCTAssertTrue(connection.didRead) } } @@ -356,38 +364,29 @@ class Functional_Read_Metadata_ValueWithObjectMetadataTests: ValueWithObjectMeta class Functional_Write_ValueWithObjectMetadataTests: ValueWithObjectMetadataTests { func test__transaction__write_item() { - checkTransactionDidWriteItem(writeTransaction.write(item)) + checkTransactionDidWriteItem(writeTransaction.writeWithMetadata((item, metadata))) } func test__transaction__write_items() { - checkTransactionDidWriteItems(writeTransaction.write(items)) - } - - func test__transaction__write_items_2() { - checkTransactionDidWriteItems(writeTransaction.write(Set(items))) + checkTransactionDidWriteItems(writeTransaction.writeWithMetadata(zipToWrite(items, metadatas))) } // MARK: - Functional API - Connection - Writing func test__connection__write_item() { - checkTransactionDidWriteItem(connection.write(item)) + checkTransactionDidWriteItem(connection.writeWithMetadata((item, metadata))) XCTAssertTrue(connection.didWrite) } func test__connection__write_items() { - checkTransactionDidWriteItems(connection.write(items)) - XCTAssertTrue(connection.didWrite) - } - - func test__connection__write_items_2() { - checkTransactionDidWriteItems(connection.write(Set(items))) + checkTransactionDidWriteItems(connection.writeWithMetadata(zipToWrite(items, metadatas))) XCTAssertTrue(connection.didWrite) } func test__connection__async_write_item() { - var result: TypeUnderTest! + var result: (TypeUnderTest, MetadataTypeUnderTest?)! let expectation = expectationWithDescription("Test: \(#function)") - connection.asyncWrite(item) { tmp in + connection.asyncWriteWithMetadata((item, metadata)) { tmp in result = tmp expectation.fulfill() } @@ -397,9 +396,9 @@ class Functional_Write_ValueWithObjectMetadataTests: ValueWithObjectMetadataTest } func test__connection__async_write_items() { - var result: [TypeUnderTest] = [] + var result: [(TypeUnderTest, MetadataTypeUnderTest?)] = [] let expectation = expectationWithDescription("Test: \(#function)") - connection.asyncWrite(items) { received in + connection.asyncWriteWithMetadata(zipToWrite(items, metadatas)) { received in result = received expectation.fulfill() } @@ -466,45 +465,45 @@ class Curried_Read_ValueWithObjectMetadataTests: ValueWithObjectMetadataTests { func test__curried__read_at_index_with_data() { configureForReadingSingle() - XCTAssertTrue(checkTransactionDidReadItem(connection.read(TypeUnderTest.readAtIndex(index)))) + XCTAssertTrue(checkTransactionDidReadItem(connection.read(TypeUnderTest.readWithMetadataAtIndex(index)))) XCTAssertTrue(connection.didRead) } func test__curried__read_at_index_with_no_data() { - XCTAssertFalse(checkTransactionDidReadItem(connection.read(TypeUnderTest.readAtIndex(index)))) + XCTAssertFalse(checkTransactionDidReadItem(connection.read(TypeUnderTest.readWithMetadataAtIndex(index)))) XCTAssertTrue(connection.didRead) } func test__curried__read_at_indexes_with_data() { configureForReadingMultiple() - XCTAssertTrue(checkTransactionDidReadItems(connection.read(TypeUnderTest.readAtIndexes(indexes)))) + XCTAssertTrue(checkTransactionDidReadItems(connection.read(TypeUnderTest.readWithMetadataAtIndexes(indexes)))) XCTAssertTrue(connection.didRead) } func test__curried__read_at_indexes_with_no_data() { - XCTAssertFalse(checkTransactionDidReadItems(connection.read(TypeUnderTest.readAtIndexes(indexes)))) + XCTAssertFalse(checkTransactionDidReadItems(connection.read(TypeUnderTest.readWithMetadataAtIndexes(indexes)))) XCTAssertTrue(connection.didRead) } func test__curried__read_by_key_with_data() { configureForReadingSingle() - XCTAssertTrue(checkTransactionDidReadItem(connection.read(TypeUnderTest.readByKey(key)))) + XCTAssertTrue(checkTransactionDidReadItem(connection.read(TypeUnderTest.readWithMetadataByKey(key)))) XCTAssertTrue(connection.didRead) } func test__curried__read_by_key_with_no_data() { - XCTAssertFalse(checkTransactionDidReadItem(connection.read(TypeUnderTest.readByKey(key)))) + XCTAssertFalse(checkTransactionDidReadItem(connection.read(TypeUnderTest.readWithMetadataByKey(key)))) XCTAssertTrue(connection.didRead) } func test__curried__read_by_keys_with_data() { configureForReadingMultiple() - XCTAssertTrue(checkTransactionDidReadItems(connection.read(TypeUnderTest.readByKeys(keys)))) + XCTAssertTrue(checkTransactionDidReadItems(connection.read(TypeUnderTest.readWithMetadataByKeys(keys)))) XCTAssertTrue(connection.didRead) } func test__curried__read_by_keys_with_no_data() { - XCTAssertFalse(checkTransactionDidReadItems(connection.read(TypeUnderTest.readByKeys(keys)))) + XCTAssertFalse(checkTransactionDidReadItems(connection.read(TypeUnderTest.readWithMetadataByKeys(keys)))) XCTAssertTrue(connection.didRead) } } @@ -512,7 +511,7 @@ class Curried_Read_ValueWithObjectMetadataTests: ValueWithObjectMetadataTests { class Curried_Write_ValueWithObjectMetadataTests: ValueWithObjectMetadataTests { func test__curried__write() { - checkTransactionDidWriteItem(connection.write(item.write())) + checkTransactionDidWriteItem(connection.write(item.writeWithMetadata(metadata))) XCTAssertTrue(connection.didWrite) } } @@ -524,69 +523,69 @@ class Persistable_Read_ValueWithObjectMetadataTests: ValueWithObjectMetadataTest func test__reader__in_transaction_at_index() { configureForReadingSingle() reader = Read(readTransaction) - XCTAssertTrue(checkTransactionDidReadItem(reader.inTransaction(readTransaction, atIndex: index))) + XCTAssertTrue(checkTransactionDidReadItem(reader.withMetadataInTransaction(readTransaction, atIndex: index))) } func test__reader__in_transaction_at_index_2() { configureForReadingSingle() reader = Read(readTransaction) - let atIndex = reader.inTransactionAtIndex(readTransaction) + let atIndex: YapDB.Index -> (TypeUnderTest, MetadataTypeUnderTest?)? = reader.withMetadataInTransactionAtIndex(readTransaction) XCTAssertTrue(checkTransactionDidReadItem(atIndex(index))) } func test__reader__at_index_in_transaction() { configureForReadingSingle() reader = Read(readTransaction) - let inTransaction = reader.atIndexInTransaction(index) + let inTransaction: TestableReadTransaction -> (TypeUnderTest, MetadataTypeUnderTest?)? = reader.withMetadataAtIndexInTransaction(index) XCTAssertTrue(checkTransactionDidReadItem(inTransaction(readTransaction))) } func test__reader__at_indexes_in_transaction_with_items() { configureForReadingMultiple() reader = Read(readTransaction) - XCTAssertTrue(checkTransactionDidReadItems(reader.atIndexesInTransaction(indexes)(readTransaction))) + XCTAssertTrue(checkTransactionDidReadItems(reader.withMetadataAtIndexesInTransaction(indexes)(readTransaction))) } func test__reader__at_indexes_in_transaction_with_no_items() { reader = Read(readTransaction) - XCTAssertFalse(checkTransactionDidReadItems(reader.atIndexesInTransaction(indexes)(readTransaction))) + XCTAssertFalse(checkTransactionDidReadItems(reader.withMetadataAtIndexesInTransaction(indexes)(readTransaction))) } func test__reader__in_transaction_by_key() { configureForReadingSingle() reader = Read(readTransaction) - XCTAssertTrue(checkTransactionDidReadItem(reader.inTransaction(readTransaction, byKey: key))) + XCTAssertTrue(checkTransactionDidReadItem(reader.withMetadataInTransaction(readTransaction, byKey: key))) } func test__reader__in_transaction_by_key_2() { configureForReadingSingle() reader = Read(readTransaction) - let byKey = reader.inTransactionByKey(readTransaction) + let byKey: String -> (TypeUnderTest, MetadataTypeUnderTest?)? = reader.withMetadataInTransactionByKey(readTransaction) XCTAssertTrue(checkTransactionDidReadItem(byKey(key))) } func test__reader__by_key_in_transaction() { configureForReadingSingle() reader = Read(readTransaction) - let inTransaction = reader.byKeyInTransaction(key) + let inTransaction: TestableReadTransaction -> (TypeUnderTest, MetadataTypeUnderTest?)? = reader.withMetadataByKeyInTransaction(key) XCTAssertTrue(checkTransactionDidReadItem(inTransaction(readTransaction))) } func test__reader__by_keys_in_transaction_with_items() { configureForReadingMultiple() reader = Read(readTransaction) - XCTAssertTrue(checkTransactionDidReadItems(reader.byKeysInTransaction(keys)(readTransaction))) + XCTAssertTrue(checkTransactionDidReadItems(reader.withMetadataByKeysInTransaction(keys)(readTransaction))) } func test__reader__by_keys_in_transaction_with_items_with_keys() { configureForReadingMultiple() reader = Read(readTransaction) - XCTAssertTrue(checkTransactionDidReadItems(reader.byKeysInTransaction()(readTransaction))) + XCTAssertTrue(checkTransactionDidReadItems(reader.withMetadataByKeysInTransaction()(readTransaction))) } func test__reader__by_keys_in_transaction_with_no_items() { reader = Read(readTransaction) - XCTAssertFalse(checkTransactionDidReadItems(reader.byKeysInTransaction(keys)(readTransaction))) + XCTAssertFalse(checkTransactionDidReadItems(reader.withMetadataByKeysInTransaction(keys)(readTransaction))) } // Reading - With Transaction @@ -594,66 +593,66 @@ class Persistable_Read_ValueWithObjectMetadataTests: ValueWithObjectMetadataTest func test__reader_with_transaction__at_index_with_item() { configureForReadingSingle() reader = Read(readTransaction) - XCTAssertTrue(checkTransactionDidReadItem(reader.inTransaction(readTransaction, atIndex: index))) + XCTAssertTrue(checkTransactionDidReadItem(reader.withMetadataInTransaction(readTransaction, atIndex: index))) } func test__reader_with_transaction__at_index_with_no_item() { reader = Read(readTransaction) - XCTAssertFalse(checkTransactionDidReadItem(reader.atIndex(index))) + XCTAssertFalse(checkTransactionDidReadItem(reader.withMetadataAtIndex(index))) } func test__reader_with_transaction__at_indexes_with_items() { configureForReadingMultiple() reader = Read(readTransaction) - XCTAssertTrue(checkTransactionDidReadItems(reader.atIndexes(indexes))) + XCTAssertTrue(checkTransactionDidReadItems(reader.withMetadataAtIndexes(indexes))) } func test__reader_with_transaction__at_indexes_with_no_items() { reader = Read(readTransaction) - XCTAssertFalse(checkTransactionDidReadItems(reader.atIndexes(indexes))) + XCTAssertFalse(checkTransactionDidReadItems(reader.withMetadataAtIndexes(indexes))) } func test__reader_with_transaction__by_key_with_item() { configureForReadingSingle() reader = Read(readTransaction) - XCTAssertTrue(checkTransactionDidReadItem(reader.inTransaction(readTransaction, atIndex: index))) + XCTAssertTrue(checkTransactionDidReadItem(reader.withMetadataInTransaction(readTransaction, atIndex: index))) } func test__reader_with_transaction__by_key_with_no_item() { reader = Read(readTransaction) - XCTAssertFalse(checkTransactionDidReadItem(reader.byKey(key))) + XCTAssertFalse(checkTransactionDidReadItem(reader.withMetadataByKey(key))) } func test__reader_with_transaction__by_keys_with_items() { configureForReadingMultiple() reader = Read(readTransaction) - XCTAssertTrue(checkTransactionDidReadItems(reader.byKeys(keys))) + XCTAssertTrue(checkTransactionDidReadItems(reader.withMetadataByKeys(keys))) } func test__reader_with_transaction__by_keys_with_no_items() { reader = Read(readTransaction) - XCTAssertFalse(checkTransactionDidReadItems(reader.byKeys(keys))) + XCTAssertFalse(checkTransactionDidReadItems(reader.withMetadataByKeys(keys))) } func test__reader_with_transaction__all_with_items() { configureForReadingMultiple() reader = Read(readTransaction) - XCTAssertTrue(checkTransactionDidReadItems(reader.all())) + XCTAssertTrue(checkTransactionDidReadItems(reader.withMetadataAll())) XCTAssertEqual(readTransaction.didKeysInCollection, TypeUnderTest.collection) } func test__reader_with_transaction__all_with_no_items() { reader = Read(readTransaction) - XCTAssertFalse(checkTransactionDidReadItems(reader.all())) + XCTAssertFalse(checkTransactionDidReadItems(reader.withMetadataAll())) XCTAssertEqual(readTransaction.didKeysInCollection, TypeUnderTest.collection) } func test__reader_with_transaction__filter() { configureForReadingSingle() reader = Read(readTransaction) - let (items, missing) = reader.filterExisting(keys) + let (items, missing): ([(TypeUnderTest, MetadataTypeUnderTest?)], [String]) = reader.withMetadataFilterExisting(keys) XCTAssertEqual(readTransaction.didReadAtIndexes.first!, indexes.first!) - XCTAssertEqual(items.map { $0.identifier }, items.prefixUpTo(1).map { $0.identifier }) + XCTAssertEqual(items.map { $0.0.identifier }, items.prefixUpTo(1).map { $0.0.identifier }) XCTAssertEqual(missing, Array(keys.suffixFrom(1))) } @@ -662,66 +661,66 @@ class Persistable_Read_ValueWithObjectMetadataTests: ValueWithObjectMetadataTest func test__reader_with_connection__at_index_with_item() { configureForReadingSingle() reader = Read(connection) - XCTAssertTrue(checkTransactionDidReadItem(reader.atIndex(index))) + XCTAssertTrue(checkTransactionDidReadItem(reader.withMetadataAtIndex(index))) XCTAssertTrue(connection.didRead) } func test__reader_with_connection__at_index_with_no_item() { reader = Read(connection) - XCTAssertFalse(checkTransactionDidReadItem(reader.atIndex(index))) + XCTAssertFalse(checkTransactionDidReadItem(reader.withMetadataAtIndex(index))) XCTAssertTrue(connection.didRead) } func test__reader_with_connection__at_indexes_with_items() { configureForReadingMultiple() reader = Read(connection) - XCTAssertTrue(checkTransactionDidReadItems(reader.atIndexes(indexes))) + XCTAssertTrue(checkTransactionDidReadItems(reader.withMetadataAtIndexes(indexes))) XCTAssertTrue(connection.didRead) } func test__reader_with_connection__at_indexes_with_no_items() { reader = Read(connection) - XCTAssertFalse(checkTransactionDidReadItems(reader.atIndexes(indexes))) + XCTAssertFalse(checkTransactionDidReadItems(reader.withMetadataAtIndexes(indexes))) XCTAssertTrue(connection.didRead) } func test__reader_with_connection__by_key_with_item() { configureForReadingSingle() reader = Read(connection) - XCTAssertTrue(checkTransactionDidReadItem(reader.byKey(key))) + XCTAssertTrue(checkTransactionDidReadItem(reader.withMetadataByKey(key))) XCTAssertTrue(connection.didRead) } func test__reader_with_connection__by_key_with_no_item() { reader = Read(connection) - XCTAssertFalse(checkTransactionDidReadItem(reader.byKey(key))) + XCTAssertFalse(checkTransactionDidReadItem(reader.withMetadataByKey(key))) XCTAssertTrue(connection.didRead) } func test__reader_with_connection__by_keys_with_items() { configureForReadingMultiple() reader = Read(connection) - XCTAssertTrue(checkTransactionDidReadItems(reader.byKeys(keys))) + XCTAssertTrue(checkTransactionDidReadItems(reader.withMetadataByKeys(keys))) XCTAssertTrue(connection.didRead) } func test__reader_with_connection__by_keys_with_no_items() { reader = Read(connection) - XCTAssertFalse(checkTransactionDidReadItems(reader.byKeys(keys))) + XCTAssertFalse(checkTransactionDidReadItems(reader.withMetadataByKeys(keys))) XCTAssertTrue(connection.didRead) } func test__reader_with_connection__all_with_items() { configureForReadingMultiple() reader = Read(connection) - XCTAssertTrue(checkTransactionDidReadItems(reader.all())) + XCTAssertTrue(checkTransactionDidReadItems(reader.withMetadataAll())) XCTAssertTrue(connection.didRead) XCTAssertEqual(readTransaction.didKeysInCollection, TypeUnderTest.collection) } func test__reader_with_connection__all_with_no_items() { reader = Read(connection) - XCTAssertFalse(checkTransactionDidReadItems(reader.all())) + XCTAssertFalse(checkTransactionDidReadItems(reader.withMetadataAll())) XCTAssertTrue(connection.didRead) XCTAssertEqual(readTransaction.didKeysInCollection, TypeUnderTest.collection) } @@ -792,19 +791,19 @@ class Persistable_Read_Metadata_ValueWithObjectMetadataTests: ValueWithObjectMet class Persistable_Write_ValueWithObjectMetadataTests: ValueWithObjectMetadataTests { func test__item_persistable__write_using_transaction() { - checkTransactionDidWriteItem(item.write(writeTransaction)) + checkTransactionDidWriteItem(item.writeWithMetadata(writeTransaction, metadata: metadata)) } func test__item_persistable__write_using_connection() { - checkTransactionDidWriteItem(item.write(connection)) + checkTransactionDidWriteItem(item.writeWithMetadata(connection, metadata: metadata)) XCTAssertTrue(connection.didWrite) } func test__item_persistable__write_async_using_connection() { let expectation = expectationWithDescription("Test: \(#function)") - var result: TypeUnderTest! = nil + var result: (TypeUnderTest, MetadataTypeUnderTest?)! = nil - item.asyncWrite(connection) { tmp in + item.asyncWriteWithMetadata(connection, metadata: metadata) { tmp in result = tmp expectation.fulfill() } @@ -816,7 +815,7 @@ class Persistable_Write_ValueWithObjectMetadataTests: ValueWithObjectMetadataTes func test__item_persistable__write_using_opertion() { let expectation = expectationWithDescription("Test: \(#function)") - let operation = item.writeOperation(connection) + let operation = item.writeWithMetadataOperation(connection, metadata: metadata) operation.completionBlock = { expectation.fulfill() } @@ -826,24 +825,24 @@ class Persistable_Write_ValueWithObjectMetadataTests: ValueWithObjectMetadataTes XCTAssertFalse(writeTransaction.didWriteAtIndexes.isEmpty) XCTAssertEqual(writeTransaction.didWriteAtIndexes[0].0, index) XCTAssertEqual(TypeUnderTest.decode(writeTransaction.didWriteAtIndexes[0].1)!, item) - XCTAssertEqual(writeTransaction.didWriteAtIndexes[0].2 as? NSNumber, item.metadata) + XCTAssertEqual(writeTransaction.didWriteAtIndexes[0].2 as? NSNumber, metadata) XCTAssertTrue(connection.didWrite) } func test__items_persistable__write_using_transaction() { - checkTransactionDidWriteItems(items.write(writeTransaction)) + checkTransactionDidWriteItems(items.writeWithMetadata(writeTransaction, metadata: metadatas)) } func test__items_persistable__write_using_connection() { - checkTransactionDidWriteItems(items.write(connection)) + checkTransactionDidWriteItems(items.writeWithMetadata(connection, metadata: metadatas)) XCTAssertTrue(connection.didWrite) } func test__items_persistable__write_async_using_connection() { let expectation = expectationWithDescription("Test: \(#function)") - var result: [TypeUnderTest] = [] + var result: [(TypeUnderTest, MetadataTypeUnderTest?)] = [] - items.asyncWrite(connection) { tmp in + items.asyncWriteWithMetadata(connection, metadata: metadatas) { tmp in result = tmp expectation.fulfill() } @@ -855,7 +854,7 @@ class Persistable_Write_ValueWithObjectMetadataTests: ValueWithObjectMetadataTes func test__items_persistable__write_using_opertion() { let expectation = expectationWithDescription("Test: \(#function)") - let operation = items.writeOperation(connection) + let operation = items.writeWithMetadataOperation(connection, metadata: metadatas) operation.completionBlock = { expectation.fulfill() } diff --git a/Tests/Shared/ValueWithValueMetadataTests.swift b/Tests/Shared/ValueWithValueMetadataTests.swift index a22b05d..d6b4fd6 100644 --- a/Tests/Shared/ValueWithValueMetadataTests.swift +++ b/Tests/Shared/ValueWithValueMetadataTests.swift @@ -13,12 +13,15 @@ import XCTest class ValueWithValueMetadataTests: XCTestCase { typealias TypeUnderTest = Product + typealias MetadataTypeUnderTest = Product.Metadata var item: TypeUnderTest! + var metadata: MetadataTypeUnderTest! var index: YapDB.Index! var key: String! var items: [TypeUnderTest]! + var metadatas: [MetadataTypeUnderTest?]! var indexes: [YapDB.Index]! var keys: [String]! @@ -56,9 +59,11 @@ class ValueWithValueMetadataTests: XCTestCase { override func tearDown() { item = nil + metadata = nil index = nil key = nil items = nil + metadatas = nil indexes = nil keys = nil @@ -73,73 +78,76 @@ class ValueWithValueMetadataTests: XCTestCase { func createPersistables() { item = TypeUnderTest( - metadata: TypeUnderTest.Metadata(categoryIdentifier: 1), identifier: "vodka-123", name: "Belvidere", barcode: .UPCA(1, 2, 3, 4) ) + metadata = TypeUnderTest.Metadata(categoryIdentifier: 1) items = [ item, TypeUnderTest( - metadata: TypeUnderTest.Metadata(categoryIdentifier: 2), identifier: "gin-123", name: "Boxer Gin", barcode: .UPCA(5, 10, 15, 20) ), TypeUnderTest( - metadata: TypeUnderTest.Metadata(categoryIdentifier: 3), identifier: "rum-123", name: "Mount Gay Rum", barcode: .UPCA(12, 24, 39, 48) ), TypeUnderTest( - metadata: TypeUnderTest.Metadata(categoryIdentifier: 2), identifier: "gin-234", name: "Monkey 47", barcode: .UPCA(31, 62, 93, 124) ) ] + metadatas = [ + metadata, + MetadataTypeUnderTest(categoryIdentifier: 2), + MetadataTypeUnderTest(categoryIdentifier: 3), + MetadataTypeUnderTest(categoryIdentifier: 4) + ] } func configureForReadingSingle() { readTransaction.object = item.encoded - readTransaction.metadata = item.metadata?.encoded + readTransaction.metadata = metadata?.encoded } func configureForReadingMultiple() { readTransaction.objects = items.encoded - readTransaction.metadatas = items.map { $0.metadata?.encoded } + readTransaction.metadatas = metadatas.map { $0?.encoded } readTransaction.keys = keys } - func checkTransactionDidWriteItem(result: TypeUnderTest) { - XCTAssertEqual(result.identifier, item.identifier) + func checkTransactionDidWriteItem(result: (TypeUnderTest, MetadataTypeUnderTest?)) { + XCTAssertEqual(result.0.identifier, item.identifier) XCTAssertFalse(writeTransaction.didWriteAtIndexes.isEmpty) XCTAssertEqual(writeTransaction.didWriteAtIndexes[0].0, index) XCTAssertEqual(TypeUnderTest.decode(writeTransaction.didWriteAtIndexes[0].1)!, item) - XCTAssertEqual(TypeUnderTest.MetadataType.decode(writeTransaction.didWriteAtIndexes[0].2), item.metadata) + XCTAssertEqual(MetadataTypeUnderTest.decode(writeTransaction.didWriteAtIndexes[0].2), metadata) } - func checkTransactionDidWriteItems(result: [TypeUnderTest]) { + func checkTransactionDidWriteItems(result: [(TypeUnderTest, MetadataTypeUnderTest?)]) { XCTAssertFalse(writeTransaction.didWriteAtIndexes.isEmpty) XCTAssertEqual(writeTransaction.didWriteAtIndexes.map { $0.0.key }.sort(), indexes.map { $0.key }.sort()) XCTAssertEqual(writeTransaction.didWriteAtIndexes.map { $0.2 }.count, items.count) XCTAssertFalse(result.isEmpty) - XCTAssertEqual(Set(result), Set(items)) + XCTAssertEqual(Set(result.map({$0.0})), Set(items)) } - func checkTransactionDidReadItem(result: TypeUnderTest?) -> Bool { + func checkTransactionDidReadItem(result: (TypeUnderTest, MetadataTypeUnderTest?)?) -> Bool { XCTAssertEqual(readTransaction.didReadAtIndex, index) guard let result = result else { return false } XCTAssertEqual(readTransaction.didReadMetadataAtIndex, index) - XCTAssertEqual(result.identifier, item.identifier) - XCTAssertEqual(result.metadata, item.metadata) + XCTAssertEqual(result.0.identifier, item.identifier) + XCTAssertEqual(result.1, metadata) return true } - func checkTransactionDidReadItems(result: [TypeUnderTest]) -> Bool { + func checkTransactionDidReadItems(result: [(TypeUnderTest, MetadataTypeUnderTest?)]) -> Bool { if result.isEmpty { return false } @@ -148,17 +156,17 @@ class ValueWithValueMetadataTests: XCTestCase { return true } - func checkTransactionDidReadMetadataItem(result: TypeUnderTest.MetadataType?) -> Bool { + func checkTransactionDidReadMetadataItem(result: MetadataTypeUnderTest?) -> Bool { XCTAssertNil(readTransaction.didReadAtIndex) guard let result = result else { return false } XCTAssertEqual(readTransaction.didReadMetadataAtIndex, index) - XCTAssertEqual(result, item.metadata) + XCTAssertEqual(result, metadata) return true } - func checkTransactionDidReadMetadataItems(result: [TypeUnderTest.MetadataType]) -> Bool { + func checkTransactionDidReadMetadataItems(result: [MetadataTypeUnderTest]) -> Bool { XCTAssertTrue(readTransaction.didReadAtIndexes.isEmpty) if result.isEmpty { return false @@ -183,7 +191,7 @@ class ValueWithValueMetadataTests: XCTestCase { class Base_ValueWithValueMetadataTests: ValueWithValueMetadataTests { func test__metadata_is_not_nil() { - XCTAssertNotNil(item.metadata) + XCTAssertNotNil(metadata) } } @@ -193,116 +201,116 @@ class Functional_Read_ValueWithValueMetadataTests: ValueWithValueMetadataTests { func test__transaction__read_at_index_with_data() { configureForReadingSingle() - XCTAssertTrue(checkTransactionDidReadItem(readTransaction.readAtIndex(index))) + XCTAssertTrue(checkTransactionDidReadItem(readTransaction.readWithMetadataAtIndex(index))) } func test__transaction__read_at_index_without_data() { - XCTAssertFalse(checkTransactionDidReadItem(readTransaction.readAtIndex(index))) + XCTAssertFalse(checkTransactionDidReadItem(readTransaction.readWithMetadataAtIndex(index))) } func test__transaction__read_at_indexes_with_data() { configureForReadingMultiple() - XCTAssertTrue(checkTransactionDidReadItems(readTransaction.readAtIndexes(indexes))) + XCTAssertTrue(checkTransactionDidReadItems(readTransaction.readWithMetadataAtIndexes(indexes))) } func test__transaction__read_at_indexes_with_data_2() { configureForReadingMultiple() - XCTAssertTrue(checkTransactionDidReadItems(readTransaction.readAtIndexes(Set(indexes)))) + XCTAssertTrue(checkTransactionDidReadItems(readTransaction.readWithMetadataAtIndexes(Set(indexes)))) } func test__transaction__read_at_indexes_without_data() { - XCTAssertFalse(checkTransactionDidReadItems(readTransaction.readAtIndexes(indexes))) + XCTAssertFalse(checkTransactionDidReadItems(readTransaction.readWithMetadataAtIndexes(indexes))) } func test__transaction__read_by_key_with_data() { configureForReadingSingle() - XCTAssertTrue(checkTransactionDidReadItem(readTransaction.readByKey(key))) + XCTAssertTrue(checkTransactionDidReadItem(readTransaction.readWithMetadataByKey(key))) } func test__transaction__read_by_key_without_data() { - XCTAssertFalse(checkTransactionDidReadItem(readTransaction.readByKey(key))) + XCTAssertFalse(checkTransactionDidReadItem(readTransaction.readWithMetadataByKey(key))) } func test__transaction__read_by_keys_with_data() { configureForReadingMultiple() - XCTAssertTrue(checkTransactionDidReadItems(readTransaction.readByKeys(keys))) + XCTAssertTrue(checkTransactionDidReadItems(readTransaction.readWithMetadataByKeys(keys))) } func test__transaction__read_by_keys_with_data_2() { configureForReadingMultiple() - XCTAssertTrue(checkTransactionDidReadItems(readTransaction.readByKeys(Set(keys)))) + XCTAssertTrue(checkTransactionDidReadItems(readTransaction.readWithMetadataByKeys(Set(keys)))) } func test__transaction__read_by_keys_without_data() { - XCTAssertFalse(checkTransactionDidReadItems(readTransaction.readByKeys(keys))) + XCTAssertFalse(checkTransactionDidReadItems(readTransaction.readWithMetadataByKeys(keys))) } func test__transaction__read_all_with_data() { configureForReadingMultiple() - XCTAssertTrue(checkTransactionDidReadItems(readTransaction.readAll())) + XCTAssertTrue(checkTransactionDidReadItems(readTransaction.readWithMetadataAll())) } // Functional API - ConnectionType - Reading func test__connection__read_at_index_with_data() { configureForReadingSingle() - XCTAssertTrue(checkTransactionDidReadItem(connection.readAtIndex(index))) + XCTAssertTrue(checkTransactionDidReadItem(connection.readWithMetadataAtIndex(index))) XCTAssertTrue(connection.didRead) } func test__connection__read_at_index_without_data() { - XCTAssertFalse(checkTransactionDidReadItem(connection.readAtIndex(index))) + XCTAssertFalse(checkTransactionDidReadItem(connection.readWithMetadataAtIndex(index))) XCTAssertTrue(connection.didRead) } func test__connection__read_at_indexes_with_data() { configureForReadingMultiple() - XCTAssertTrue(checkTransactionDidReadItems(connection.readAtIndexes(indexes))) + XCTAssertTrue(checkTransactionDidReadItems(connection.readWithMetadataAtIndexes(indexes))) XCTAssertTrue(connection.didRead) } func test__connection__read_at_indexes_with_data_2() { configureForReadingMultiple() - XCTAssertTrue(checkTransactionDidReadItems(connection.readAtIndexes(Set(indexes)))) + XCTAssertTrue(checkTransactionDidReadItems(connection.readWithMetadataAtIndexes(Set(indexes)))) XCTAssertTrue(connection.didRead) } func test__connection__read_at_indexes_without_data() { - XCTAssertFalse(checkTransactionDidReadItems(connection.readAtIndexes(indexes))) + XCTAssertFalse(checkTransactionDidReadItems(connection.readWithMetadataAtIndexes(indexes))) XCTAssertTrue(connection.didRead) } func test__connection__read_by_key_with_data() { configureForReadingSingle() - XCTAssertTrue(checkTransactionDidReadItem(connection.readByKey(key))) + XCTAssertTrue(checkTransactionDidReadItem(connection.readWithMetadataByKey(key))) XCTAssertTrue(connection.didRead) } func test__connection__read_by_key_without_data() { - XCTAssertFalse(checkTransactionDidReadItem(connection.readByKey(key))) + XCTAssertFalse(checkTransactionDidReadItem(connection.readWithMetadataByKey(key))) XCTAssertTrue(connection.didRead) } func test__connection__read_by_keys_with_data() { configureForReadingMultiple() - XCTAssertTrue(checkTransactionDidReadItems(connection.readByKeys(keys))) + XCTAssertTrue(checkTransactionDidReadItems(connection.readWithMetadataByKeys(keys))) XCTAssertTrue(connection.didRead) } func test__connection__read_by_keys_with_data_2() { configureForReadingMultiple() - XCTAssertTrue(checkTransactionDidReadItems(connection.readByKeys(Set(keys)))) + XCTAssertTrue(checkTransactionDidReadItems(connection.readWithMetadataByKeys(Set(keys)))) XCTAssertTrue(connection.didRead) } func test__connection__read_by_keys_without_data() { - XCTAssertFalse(checkTransactionDidReadItems(connection.readByKeys(keys))) + XCTAssertFalse(checkTransactionDidReadItems(connection.readWithMetadataByKeys(keys))) XCTAssertTrue(connection.didRead) } func test__connection__read_all_with_data() { configureForReadingMultiple() - XCTAssertTrue(checkTransactionDidReadItems(connection.readAll())) + XCTAssertTrue(checkTransactionDidReadItems(connection.readWithMetadataAll())) XCTAssertTrue(connection.didRead) } } @@ -353,38 +361,29 @@ class Functional_Read_Metadata_ValueWithValueMetadataTests: ValueWithValueMetada class Functional_Write_ValueWithValueMetadataTests: ValueWithValueMetadataTests { func test__transaction__write_item() { - checkTransactionDidWriteItem(writeTransaction.write(item)) + checkTransactionDidWriteItem(writeTransaction.writeWithMetadata((item, metadata))) } func test__transaction__write_items() { - checkTransactionDidWriteItems(writeTransaction.write(items)) - } - - func test__transaction__write_items_2() { - checkTransactionDidWriteItems(writeTransaction.write(Set(items))) + checkTransactionDidWriteItems(writeTransaction.writeWithMetadata(zipToWrite(items, metadatas))) } // MARK: - Functional API - Connection - Writing func test__connection__write_item() { - checkTransactionDidWriteItem(connection.write(item)) + checkTransactionDidWriteItem(connection.writeWithMetadata((item, metadata))) XCTAssertTrue(connection.didWrite) } func test__connection__write_items() { - checkTransactionDidWriteItems(connection.write(items)) - XCTAssertTrue(connection.didWrite) - } - - func test__connection__write_items_2() { - checkTransactionDidWriteItems(connection.write(Set(items))) + checkTransactionDidWriteItems(connection.writeWithMetadata(zipToWrite(items, metadatas))) XCTAssertTrue(connection.didWrite) } func test__connection__async_write_item() { - var result: TypeUnderTest! + var result: (TypeUnderTest, MetadataTypeUnderTest?)! let expectation = expectationWithDescription("Test: \(#function)") - connection.asyncWrite(item) { tmp in + connection.asyncWriteWithMetadata((item, metadata)) { tmp in result = tmp expectation.fulfill() } @@ -394,9 +393,9 @@ class Functional_Write_ValueWithValueMetadataTests: ValueWithValueMetadataTests } func test__connection__async_write_items() { - var result: [TypeUnderTest] = [] + var result: [(TypeUnderTest, MetadataTypeUnderTest?)] = [] let expectation = expectationWithDescription("Test: \(#function)") - connection.asyncWrite(items) { received in + connection.asyncWriteWithMetadata(zipToWrite(items, metadatas)) { received in result = received expectation.fulfill() } @@ -463,45 +462,45 @@ class Curried_Read_ValueWithValueMetadataTests: ValueWithValueMetadataTests { func test__curried__read_at_index_with_data() { configureForReadingSingle() - XCTAssertTrue(checkTransactionDidReadItem(connection.read(TypeUnderTest.readAtIndex(index)))) + XCTAssertTrue(checkTransactionDidReadItem(connection.read(TypeUnderTest.readWithMetadataAtIndex(index)))) XCTAssertTrue(connection.didRead) } func test__curried__read_at_index_with_no_data() { - XCTAssertFalse(checkTransactionDidReadItem(connection.read(TypeUnderTest.readAtIndex(index)))) + XCTAssertFalse(checkTransactionDidReadItem(connection.read(TypeUnderTest.readWithMetadataAtIndex(index)))) XCTAssertTrue(connection.didRead) } func test__curried__read_at_indexes_with_data() { configureForReadingMultiple() - XCTAssertTrue(checkTransactionDidReadItems(connection.read(TypeUnderTest.readAtIndexes(indexes)))) + XCTAssertTrue(checkTransactionDidReadItems(connection.read(TypeUnderTest.readWithMetadataAtIndexes(indexes)))) XCTAssertTrue(connection.didRead) } func test__curried__read_at_indexes_with_no_data() { - XCTAssertFalse(checkTransactionDidReadItems(connection.read(TypeUnderTest.readAtIndexes(indexes)))) + XCTAssertFalse(checkTransactionDidReadItems(connection.read(TypeUnderTest.readWithMetadataAtIndexes(indexes)))) XCTAssertTrue(connection.didRead) } func test__curried__read_by_key_with_data() { configureForReadingSingle() - XCTAssertTrue(checkTransactionDidReadItem(connection.read(TypeUnderTest.readByKey(key)))) + XCTAssertTrue(checkTransactionDidReadItem(connection.read(TypeUnderTest.readWithMetadataByKey(key)))) XCTAssertTrue(connection.didRead) } func test__curried__read_by_key_with_no_data() { - XCTAssertFalse(checkTransactionDidReadItem(connection.read(TypeUnderTest.readByKey(key)))) + XCTAssertFalse(checkTransactionDidReadItem(connection.read(TypeUnderTest.readWithMetadataByKey(key)))) XCTAssertTrue(connection.didRead) } func test__curried__read_by_keys_with_data() { configureForReadingMultiple() - XCTAssertTrue(checkTransactionDidReadItems(connection.read(TypeUnderTest.readByKeys(keys)))) + XCTAssertTrue(checkTransactionDidReadItems(connection.read(TypeUnderTest.readWithMetadataByKeys(keys)))) XCTAssertTrue(connection.didRead) } func test__curried__read_by_keys_with_no_data() { - XCTAssertFalse(checkTransactionDidReadItems(connection.read(TypeUnderTest.readByKeys(keys)))) + XCTAssertFalse(checkTransactionDidReadItems(connection.read(TypeUnderTest.readWithMetadataByKeys(keys)))) XCTAssertTrue(connection.didRead) } } @@ -509,7 +508,7 @@ class Curried_Read_ValueWithValueMetadataTests: ValueWithValueMetadataTests { class Curried_Write_ValueWithValueMetadataTests: ValueWithValueMetadataTests { func test__curried__write() { - checkTransactionDidWriteItem(connection.write(item.write())) + checkTransactionDidWriteItem(connection.write(item.writeWithMetadata(metadata))) XCTAssertTrue(connection.didWrite) } } @@ -521,69 +520,69 @@ class Persistable_Read_ValueWithValueMetadataTests: ValueWithValueMetadataTests func test__reader__in_transaction_at_index() { configureForReadingSingle() reader = Read(readTransaction) - XCTAssertTrue(checkTransactionDidReadItem(reader.inTransaction(readTransaction, atIndex: index))) + XCTAssertTrue(checkTransactionDidReadItem(reader.withMetadataInTransaction(readTransaction, atIndex: index))) } func test__reader__in_transaction_at_index_2() { configureForReadingSingle() reader = Read(readTransaction) - let atIndex = reader.inTransactionAtIndex(readTransaction) + let atIndex: YapDB.Index -> (TypeUnderTest, MetadataTypeUnderTest?)? = reader.withMetadataInTransactionAtIndex(readTransaction) XCTAssertTrue(checkTransactionDidReadItem(atIndex(index))) } func test__reader__at_index_in_transaction() { configureForReadingSingle() reader = Read(readTransaction) - let inTransaction = reader.atIndexInTransaction(index) + let inTransaction: TestableReadTransaction -> (TypeUnderTest, MetadataTypeUnderTest?)? = reader.withMetadataAtIndexInTransaction(index) XCTAssertTrue(checkTransactionDidReadItem(inTransaction(readTransaction))) } func test__reader__at_indexes_in_transaction_with_items() { configureForReadingMultiple() reader = Read(readTransaction) - XCTAssertTrue(checkTransactionDidReadItems(reader.atIndexesInTransaction(indexes)(readTransaction))) + XCTAssertTrue(checkTransactionDidReadItems(reader.withMetadataAtIndexesInTransaction(indexes)(readTransaction))) } func test__reader__at_indexes_in_transaction_with_no_items() { reader = Read(readTransaction) - XCTAssertFalse(checkTransactionDidReadItems(reader.atIndexesInTransaction(indexes)(readTransaction))) + XCTAssertFalse(checkTransactionDidReadItems(reader.withMetadataAtIndexesInTransaction(indexes)(readTransaction))) } func test__reader__in_transaction_by_key() { configureForReadingSingle() reader = Read(readTransaction) - XCTAssertTrue(checkTransactionDidReadItem(reader.inTransaction(readTransaction, byKey: key))) + XCTAssertTrue(checkTransactionDidReadItem(reader.withMetadataInTransaction(readTransaction, byKey: key))) } func test__reader__in_transaction_by_key_2() { configureForReadingSingle() reader = Read(readTransaction) - let byKey = reader.inTransactionByKey(readTransaction) + let byKey: String -> (TypeUnderTest, MetadataTypeUnderTest?)? = reader.withMetadataInTransactionByKey(readTransaction) XCTAssertTrue(checkTransactionDidReadItem(byKey(key))) } func test__reader__by_key_in_transaction() { configureForReadingSingle() reader = Read(readTransaction) - let inTransaction = reader.byKeyInTransaction(key) + let inTransaction: TestableReadTransaction -> (TypeUnderTest, MetadataTypeUnderTest?)? = reader.withMetadataByKeyInTransaction(key) XCTAssertTrue(checkTransactionDidReadItem(inTransaction(readTransaction))) } func test__reader__by_keys_in_transaction_with_items() { configureForReadingMultiple() reader = Read(readTransaction) - XCTAssertTrue(checkTransactionDidReadItems(reader.byKeysInTransaction(keys)(readTransaction))) + XCTAssertTrue(checkTransactionDidReadItems(reader.withMetadataByKeysInTransaction(keys)(readTransaction))) } func test__reader__by_keys_in_transaction_with_items_with_keys() { configureForReadingMultiple() reader = Read(readTransaction) - XCTAssertTrue(checkTransactionDidReadItems(reader.byKeysInTransaction()(readTransaction))) + XCTAssertTrue(checkTransactionDidReadItems(reader.withMetadataByKeysInTransaction()(readTransaction))) } func test__reader__by_keys_in_transaction_with_no_items() { reader = Read(readTransaction) - XCTAssertFalse(checkTransactionDidReadItems(reader.byKeysInTransaction(keys)(readTransaction))) + XCTAssertFalse(checkTransactionDidReadItems(reader.withMetadataByKeysInTransaction(keys)(readTransaction))) } // Reading - With Transaction @@ -591,66 +590,66 @@ class Persistable_Read_ValueWithValueMetadataTests: ValueWithValueMetadataTests func test__reader_with_transaction__at_index_with_item() { configureForReadingSingle() reader = Read(readTransaction) - XCTAssertTrue(checkTransactionDidReadItem(reader.inTransaction(readTransaction, atIndex: index))) + XCTAssertTrue(checkTransactionDidReadItem(reader.withMetadataInTransaction(readTransaction, atIndex: index))) } func test__reader_with_transaction__at_index_with_no_item() { reader = Read(readTransaction) - XCTAssertFalse(checkTransactionDidReadItem(reader.atIndex(index))) + XCTAssertFalse(checkTransactionDidReadItem(reader.withMetadataAtIndex(index))) } func test__reader_with_transaction__at_indexes_with_items() { configureForReadingMultiple() reader = Read(readTransaction) - XCTAssertTrue(checkTransactionDidReadItems(reader.atIndexes(indexes))) + XCTAssertTrue(checkTransactionDidReadItems(reader.withMetadataAtIndexes(indexes))) } func test__reader_with_transaction__at_indexes_with_no_items() { reader = Read(readTransaction) - XCTAssertFalse(checkTransactionDidReadItems(reader.atIndexes(indexes))) + XCTAssertFalse(checkTransactionDidReadItems(reader.withMetadataAtIndexes(indexes))) } func test__reader_with_transaction__by_key_with_item() { configureForReadingSingle() reader = Read(readTransaction) - XCTAssertTrue(checkTransactionDidReadItem(reader.inTransaction(readTransaction, atIndex: index))) + XCTAssertTrue(checkTransactionDidReadItem(reader.withMetadataInTransaction(readTransaction, atIndex: index))) } func test__reader_with_transaction__by_key_with_no_item() { reader = Read(readTransaction) - XCTAssertFalse(checkTransactionDidReadItem(reader.byKey(key))) + XCTAssertFalse(checkTransactionDidReadItem(reader.withMetadataByKey(key))) } func test__reader_with_transaction__by_keys_with_items() { configureForReadingMultiple() reader = Read(readTransaction) - XCTAssertTrue(checkTransactionDidReadItems(reader.byKeys(keys))) + XCTAssertTrue(checkTransactionDidReadItems(reader.withMetadataByKeys(keys))) } func test__reader_with_transaction__by_keys_with_no_items() { reader = Read(readTransaction) - XCTAssertFalse(checkTransactionDidReadItems(reader.byKeys(keys))) + XCTAssertFalse(checkTransactionDidReadItems(reader.withMetadataByKeys(keys))) } func test__reader_with_transaction__all_with_items() { configureForReadingMultiple() reader = Read(readTransaction) - XCTAssertTrue(checkTransactionDidReadItems(reader.all())) + XCTAssertTrue(checkTransactionDidReadItems(reader.withMetadataAll())) XCTAssertEqual(readTransaction.didKeysInCollection, TypeUnderTest.collection) } func test__reader_with_transaction__all_with_no_items() { reader = Read(readTransaction) - XCTAssertFalse(checkTransactionDidReadItems(reader.all())) + XCTAssertFalse(checkTransactionDidReadItems(reader.withMetadataAll())) XCTAssertEqual(readTransaction.didKeysInCollection, TypeUnderTest.collection) } func test__reader_with_transaction__filter() { configureForReadingSingle() reader = Read(readTransaction) - let (items, missing) = reader.filterExisting(keys) + let (items, missing): ([(TypeUnderTest, MetadataTypeUnderTest?)], [String]) = reader.withMetadataFilterExisting(keys) XCTAssertEqual(readTransaction.didReadAtIndexes.first!, indexes.first!) - XCTAssertEqual(items.map { $0.identifier }, items.prefixUpTo(1).map { $0.identifier }) + XCTAssertEqual(items.map { $0.0.identifier }, items.prefixUpTo(1).map { $0.0.identifier }) XCTAssertEqual(missing, Array(keys.suffixFrom(1))) } @@ -659,66 +658,66 @@ class Persistable_Read_ValueWithValueMetadataTests: ValueWithValueMetadataTests func test__reader_with_connection__at_index_with_item() { configureForReadingSingle() reader = Read(connection) - XCTAssertTrue(checkTransactionDidReadItem(reader.atIndex(index))) + XCTAssertTrue(checkTransactionDidReadItem(reader.withMetadataAtIndex(index))) XCTAssertTrue(connection.didRead) } func test__reader_with_connection__at_index_with_no_item() { reader = Read(connection) - XCTAssertFalse(checkTransactionDidReadItem(reader.atIndex(index))) + XCTAssertFalse(checkTransactionDidReadItem(reader.withMetadataAtIndex(index))) XCTAssertTrue(connection.didRead) } func test__reader_with_connection__at_indexes_with_items() { configureForReadingMultiple() reader = Read(connection) - XCTAssertTrue(checkTransactionDidReadItems(reader.atIndexes(indexes))) + XCTAssertTrue(checkTransactionDidReadItems(reader.withMetadataAtIndexes(indexes))) XCTAssertTrue(connection.didRead) } func test__reader_with_connection__at_indexes_with_no_items() { reader = Read(connection) - XCTAssertFalse(checkTransactionDidReadItems(reader.atIndexes(indexes))) + XCTAssertFalse(checkTransactionDidReadItems(reader.withMetadataAtIndexes(indexes))) XCTAssertTrue(connection.didRead) } func test__reader_with_connection__by_key_with_item() { configureForReadingSingle() reader = Read(connection) - XCTAssertTrue(checkTransactionDidReadItem(reader.byKey(key))) + XCTAssertTrue(checkTransactionDidReadItem(reader.withMetadataByKey(key))) XCTAssertTrue(connection.didRead) } func test__reader_with_connection__by_key_with_no_item() { reader = Read(connection) - XCTAssertFalse(checkTransactionDidReadItem(reader.byKey(key))) + XCTAssertFalse(checkTransactionDidReadItem(reader.withMetadataByKey(key))) XCTAssertTrue(connection.didRead) } func test__reader_with_connection__by_keys_with_items() { configureForReadingMultiple() reader = Read(connection) - XCTAssertTrue(checkTransactionDidReadItems(reader.byKeys(keys))) + XCTAssertTrue(checkTransactionDidReadItems(reader.withMetadataByKeys(keys))) XCTAssertTrue(connection.didRead) } func test__reader_with_connection__by_keys_with_no_items() { reader = Read(connection) - XCTAssertFalse(checkTransactionDidReadItems(reader.byKeys(keys))) + XCTAssertFalse(checkTransactionDidReadItems(reader.withMetadataByKeys(keys))) XCTAssertTrue(connection.didRead) } func test__reader_with_connection__all_with_items() { configureForReadingMultiple() reader = Read(connection) - XCTAssertTrue(checkTransactionDidReadItems(reader.all())) + XCTAssertTrue(checkTransactionDidReadItems(reader.withMetadataAll())) XCTAssertTrue(connection.didRead) XCTAssertEqual(readTransaction.didKeysInCollection, TypeUnderTest.collection) } func test__reader_with_connection__all_with_no_items() { reader = Read(connection) - XCTAssertFalse(checkTransactionDidReadItems(reader.all())) + XCTAssertFalse(checkTransactionDidReadItems(reader.withMetadataAll())) XCTAssertTrue(connection.didRead) XCTAssertEqual(readTransaction.didKeysInCollection, TypeUnderTest.collection) } @@ -789,19 +788,19 @@ class Persistable_Read_Metadata_ValueWithValueMetadataTests: ValueWithValueMetad class Persistable_Write_ValueWithValueMetadataTests: ValueWithValueMetadataTests { func test__item_persistable__write_using_transaction() { - checkTransactionDidWriteItem(item.write(writeTransaction)) + checkTransactionDidWriteItem(item.writeWithMetadata(writeTransaction, metadata: metadata)) } func test__item_persistable__write_using_connection() { - checkTransactionDidWriteItem(item.write(connection)) + checkTransactionDidWriteItem(item.writeWithMetadata(connection, metadata: metadata)) XCTAssertTrue(connection.didWrite) } func test__item_persistable__write_async_using_connection() { let expectation = expectationWithDescription("Test: \(#function)") - var result: TypeUnderTest! = nil + var result: (TypeUnderTest, MetadataTypeUnderTest?)! = nil - item.asyncWrite(connection) { tmp in + item.asyncWriteWithMetadata(connection, metadata: metadata) { tmp in result = tmp expectation.fulfill() } @@ -813,7 +812,7 @@ class Persistable_Write_ValueWithValueMetadataTests: ValueWithValueMetadataTests func test__item_persistable__write_using_opertion() { let expectation = expectationWithDescription("Test: \(#function)") - let operation = item.writeOperation(connection) + let operation = item.writeWithMetadataOperation(connection, metadata: metadata) operation.completionBlock = { expectation.fulfill() } @@ -823,24 +822,24 @@ class Persistable_Write_ValueWithValueMetadataTests: ValueWithValueMetadataTests XCTAssertFalse(writeTransaction.didWriteAtIndexes.isEmpty) XCTAssertEqual(writeTransaction.didWriteAtIndexes[0].0, index) XCTAssertEqual(TypeUnderTest.decode(writeTransaction.didWriteAtIndexes[0].1)!, item) - XCTAssertEqual(TypeUnderTest.MetadataType.decode(writeTransaction.didWriteAtIndexes[0].2), item.metadata) + XCTAssertEqual(MetadataTypeUnderTest.decode(writeTransaction.didWriteAtIndexes[0].2), metadata) XCTAssertTrue(connection.didWrite) } func test__items_persistable__write_using_transaction() { - checkTransactionDidWriteItems(items.write(writeTransaction)) + checkTransactionDidWriteItems(items.writeWithMetadata(writeTransaction, metadata: metadatas)) } func test__items_persistable__write_using_connection() { - checkTransactionDidWriteItems(items.write(connection)) + checkTransactionDidWriteItems(items.writeWithMetadata(connection, metadata: metadatas)) XCTAssertTrue(connection.didWrite) } func test__items_persistable__write_async_using_connection() { let expectation = expectationWithDescription("Test: \(#function)") - var result: [TypeUnderTest] = [] - - items.asyncWrite(connection) { tmp in + var result: [(TypeUnderTest, MetadataTypeUnderTest?)] = [] + + items.asyncWriteWithMetadata(connection, metadata: metadatas) { tmp in result = tmp expectation.fulfill() } @@ -852,7 +851,7 @@ class Persistable_Write_ValueWithValueMetadataTests: ValueWithValueMetadataTests func test__items_persistable__write_using_opertion() { let expectation = expectationWithDescription("Test: \(#function)") - let operation = items.writeOperation(connection) + let operation = items.writeWithMetadataOperation(connection, metadata: metadatas) operation.completionBlock = { expectation.fulfill() } diff --git a/Tests/Shared/YapDatabaseExtensionsTests.swift b/Tests/Shared/YapDatabaseExtensionsTests.swift index 90dfae7..d3b475c 100644 --- a/Tests/Shared/YapDatabaseExtensionsTests.swift +++ b/Tests/Shared/YapDatabaseExtensionsTests.swift @@ -106,19 +106,18 @@ class YapDatabaseReadWriteTransactionTests: ReadWriteBaseTests { } let written = Employee.read(db).atIndex(index) XCTAssertNotNil(written) - XCTAssertNil(written!.metadata) XCTAssertEqual(written!.identifier, item.identifier) } func test__write_at_index_with_metadata() { let db = YapDB.testDatabase() db.makeNewConnection().readWriteWithBlock { transaction in - transaction.writeAtIndex(self.index, object: self.item, metadata: self.item.metadata) + transaction.writeAtIndex(self.index, object: self.item, metadata: self.metadata) } - let written = Employee.read(db).atIndex(index) + let written: (Employee, NSDate?)? = Employee.read(db).withMetadataAtIndex(index) XCTAssertNotNil(written) - XCTAssertNotNil(written!.metadata) - XCTAssertEqual(written!.identifier, item.identifier) + XCTAssertNotNil(written!.1) + XCTAssertEqual(written!.0.identifier, item.identifier) } func test__remove_at_indexes() { @@ -172,7 +171,7 @@ class YapDatabaseConnectionTests: ReadWriteBaseTests { var written: Employee? = .None db.makeNewConnection().asyncWrite({ transaction -> Employee? in - transaction.writeAtIndex(self.index, object: self.item, metadata: self.item.metadata) + transaction.writeAtIndex(self.index, object: self.item, metadata: self.metadata) return self.item }, queue: dispatchQueue) { (result: Employee?) in written = result @@ -206,10 +205,12 @@ class YapDatabaseConnectionTests: ReadWriteBaseTests { class ValueCodingTests: XCTestCase { var item: Product! + var metadata: Product.Metadata! var index: YapDB.Index! var key: String! var items: [Product]! + var metadatas: [Product.Metadata?]! var indexes: [YapDB.Index]! var keys: [String]! @@ -225,9 +226,11 @@ class ValueCodingTests: XCTestCase { override func tearDown() { item = nil + metadata = nil index = nil key = nil items = nil + metadatas = nil indexes = nil keys = nil super.tearDown() @@ -235,32 +238,35 @@ class ValueCodingTests: XCTestCase { func createPersistables() { item = Product( - metadata: Product.Metadata(categoryIdentifier: 1), identifier: "vodka-123", name: "Belvidere", barcode: .UPCA(1, 2, 3, 4) ) + metadata = Product.Metadata(categoryIdentifier: 1) items = [ item, Product( - metadata: Product.Metadata(categoryIdentifier: 2), identifier: "gin-123", name: "Boxer Gin", barcode: .UPCA(5, 10, 15, 20) ), Product( - metadata: Product.Metadata(categoryIdentifier: 3), identifier: "rum-123", name: "Mount Gay Rum", barcode: .UPCA(12, 24, 39, 48) ), Product( - metadata: Product.Metadata(categoryIdentifier: 2), identifier: "gin-234", name: "Monkey 47", barcode: .UPCA(31, 62, 93, 124) ) ] + metadatas = [ + metadata, + Product.Metadata(categoryIdentifier: 2), + Product.Metadata(categoryIdentifier: 3), + Product.Metadata(categoryIdentifier: 2) + ] } func test__index_is_hashable() { From eb22d8e94bef34b95bc29096496f79b72b075f01 Mon Sep 17 00:00:00 2001 From: Jim Roepcke Date: Thu, 15 Sep 2016 21:31:33 -0700 Subject: [PATCH 13/14] use new YapItem struct instead of tuples, replace zipToWrite with an extension on SequenceType --- .../ObjectWithObjectMetadataTests.swift | 48 +++++------ .../Shared/ObjectWithValueMetadataTests.swift | 48 +++++------ Tests/Shared/ReadWriteTests.swift | 2 +- .../Shared/ValueWithObjectMetadataTests.swift | 48 +++++------ .../Shared/ValueWithValueMetadataTests.swift | 48 +++++------ Tests/Shared/YapDatabaseExtensionsTests.swift | 6 +- .../Curried_ObjectWithObjectMetadata.swift | 12 +-- .../Curried_ObjectWithValueMetadata.swift | 12 +-- .../Curried_ValueWithObjectMetadata.swift | 12 +-- .../Curried_ValueWithValueMetadata.swift | 12 +-- .../Functional_ObjectWithObjectMetadata.swift | 42 +++++----- .../Functional_ObjectWithValueMetadata.swift | 42 +++++----- .../Functional_ValueWIthObjectMetadata.swift | 42 +++++----- .../Functional_ValueWIthValueMetadata.swift | 42 +++++----- ...Persistable_ObjectWithObjectMetadata.swift | 77 ++++++++++-------- .../Persistable_ObjectWithValueMetadata.swift | 79 +++++++++++-------- .../Persistable_ValueWithObjectMetadata.swift | 77 ++++++++++-------- .../Persistable_ValueWithValueMetadata.swift | 79 +++++++++++-------- .../Shared/YapDatabaseExtensions.swift | 34 +++++--- 19 files changed, 420 insertions(+), 342 deletions(-) diff --git a/Tests/Shared/ObjectWithObjectMetadataTests.swift b/Tests/Shared/ObjectWithObjectMetadataTests.swift index a91ec71..ea690a2 100644 --- a/Tests/Shared/ObjectWithObjectMetadataTests.swift +++ b/Tests/Shared/ObjectWithObjectMetadataTests.swift @@ -100,34 +100,34 @@ class ObjectWithObjectMetadataTests: XCTestCase { readTransaction.keys = keys } - func checkTransactionDidWriteItem(result: (TypeUnderTest, MetadataTypeUnderTest?)) { - XCTAssertEqual(result.0.identifier, item.identifier) + func checkTransactionDidWriteItem(result: YapItem) { + XCTAssertEqual(result.value.identifier, item.identifier) XCTAssertFalse(writeTransaction.didWriteAtIndexes.isEmpty) XCTAssertEqual(writeTransaction.didWriteAtIndexes[0].0, index) XCTAssertEqual(writeTransaction.didWriteAtIndexes[0].1.identifier, item.identifier) XCTAssertEqual(writeTransaction.didWriteAtIndexes[0].2 as? NSDate, metadata) } - func checkTransactionDidWriteItems(result: [(TypeUnderTest, MetadataTypeUnderTest?)]) { + func checkTransactionDidWriteItems(result: [YapItem]) { XCTAssertFalse(writeTransaction.didWriteAtIndexes.isEmpty) XCTAssertEqual(writeTransaction.didWriteAtIndexes.map { $0.0.key }.sort(), indexes.map { $0.key }.sort()) XCTAssertEqual(writeTransaction.didWriteAtIndexes.map { $0.2 }.count, items.count) XCTAssertFalse(result.isEmpty) - XCTAssertEqual(Set(result.map({$0.0})), Set(items)) + XCTAssertEqual(Set(result.map({$0.value})), Set(items)) } - func checkTransactionDidReadItem(result: (TypeUnderTest, MetadataTypeUnderTest?)?) -> Bool { + func checkTransactionDidReadItem(result: YapItem?) -> Bool { guard let result = result else { return false } XCTAssertEqual(readTransaction.didReadAtIndex, index) - XCTAssertEqual(result.0.identifier, item.identifier) + XCTAssertEqual(result.value.identifier, item.identifier) XCTAssertEqual(readTransaction.didReadMetadataAtIndex, index) - XCTAssertEqual(result.1, metadata) + XCTAssertEqual(result.metadata, metadata) return true } - func checkTransactionDidReadItems(result: [(TypeUnderTest, MetadataTypeUnderTest?)]) -> Bool { + func checkTransactionDidReadItems(result: [YapItem]) -> Bool { if result.isEmpty { return false } @@ -278,29 +278,29 @@ class Functional_Read_ObjectWithObjectMetadataTests: ObjectWithObjectMetadataTes class Functional_Write_ObjectWithObjectMetadataTests: ObjectWithObjectMetadataTests { func test__transaction__write_item() { - checkTransactionDidWriteItem(writeTransaction.writeWithMetadata((item, metadata))) + checkTransactionDidWriteItem(writeTransaction.writeWithMetadata(YapItem(item, metadata))) } func test__transaction__write_items() { - checkTransactionDidWriteItems(writeTransaction.writeWithMetadata(zipToWrite(items, metadatas))) + checkTransactionDidWriteItems(writeTransaction.writeWithMetadata(items.yapItems(with: metadatas))) } // MARK: - Functional API - Connection - Writing func test__connection__write_item() { - checkTransactionDidWriteItem(connection.writeWithMetadata((item, metadata))) + checkTransactionDidWriteItem(connection.writeWithMetadata(YapItem(item, metadata))) XCTAssertTrue(connection.didWrite) } func test__connection__write_items() { - checkTransactionDidWriteItems(connection.writeWithMetadata(zipToWrite(items, metadatas))) + checkTransactionDidWriteItems(connection.writeWithMetadata(items.yapItems(with: metadatas))) XCTAssertTrue(connection.didWrite) } func test__connection__async_write_item() { - var result: (TypeUnderTest, MetadataTypeUnderTest?)! + var result: YapItem! let expectation = expectationWithDescription("Test: \(#function)") - connection.asyncWriteWithMetadata((item, metadata)) { tmp in + connection.asyncWriteWithMetadata(YapItem(item, metadata)) { tmp in result = tmp expectation.fulfill() } @@ -310,9 +310,9 @@ class Functional_Write_ObjectWithObjectMetadataTests: ObjectWithObjectMetadataTe } func test__connection__async_write_items() { - var result: [(TypeUnderTest, MetadataTypeUnderTest?)] = [] + var result: [YapItem] = [] let expectation = expectationWithDescription("Test: \(#function)") - connection.asyncWriteWithMetadata(zipToWrite(items, metadatas)) { received in + connection.asyncWriteWithMetadata(items.yapItems(with: metadatas)) { received in result = received expectation.fulfill() } @@ -443,14 +443,14 @@ class Persistable_Read_ObjectWithObjectMetadataTests: ObjectWithObjectMetadataTe func test__reader__in_transaction_at_index_2() { configureForReadingSingle() reader = Read(readTransaction) - let atIndex: YapDB.Index -> (TypeUnderTest, MetadataTypeUnderTest?)? = reader.withMetadataInTransactionAtIndex(readTransaction) + let atIndex: YapDB.Index -> YapItem? = reader.withMetadataInTransactionAtIndex(readTransaction) XCTAssertTrue(checkTransactionDidReadItem(atIndex(index))) } func test__reader__at_index_in_transaction() { configureForReadingSingle() reader = Read(readTransaction) - let inTransaction: TestableReadTransaction -> (TypeUnderTest, MetadataTypeUnderTest?)? = reader.withMetadataAtIndexInTransaction(index) + let inTransaction: TestableReadTransaction -> YapItem? = reader.withMetadataAtIndexInTransaction(index) XCTAssertTrue(checkTransactionDidReadItem(inTransaction(readTransaction))) } @@ -474,14 +474,14 @@ class Persistable_Read_ObjectWithObjectMetadataTests: ObjectWithObjectMetadataTe func test__reader__in_transaction_by_key_2() { configureForReadingSingle() reader = Read(readTransaction) - let byKey: String -> (TypeUnderTest, MetadataTypeUnderTest?)? = reader.withMetadataInTransactionByKey(readTransaction) + let byKey: String -> YapItem? = reader.withMetadataInTransactionByKey(readTransaction) XCTAssertTrue(checkTransactionDidReadItem(byKey(key))) } func test__reader__by_key_in_transaction() { configureForReadingSingle() reader = Read(readTransaction) - let inTransaction: TestableReadTransaction -> (TypeUnderTest, MetadataTypeUnderTest?)? = reader.withMetadataByKeyInTransaction(key) + let inTransaction: TestableReadTransaction -> YapItem? = reader.withMetadataByKeyInTransaction(key) XCTAssertTrue(checkTransactionDidReadItem(inTransaction(readTransaction))) } @@ -564,9 +564,9 @@ class Persistable_Read_ObjectWithObjectMetadataTests: ObjectWithObjectMetadataTe func test__reader_with_transaction__filter() { configureForReadingSingle() reader = Read(readTransaction) - let (items, missing): ([(TypeUnderTest, MetadataTypeUnderTest?)], [String]) = reader.withMetadataFilterExisting(keys) + let (items, missing): ([YapItem], [String]) = reader.withMetadataFilterExisting(keys) XCTAssertEqual(readTransaction.didReadAtIndexes.first!, indexes.first!) - XCTAssertEqual(items.map { $0.0.identifier }, items.prefixUpTo(1).map { $0.0.identifier }) + XCTAssertEqual(items.map { $0.value.identifier }, items.prefixUpTo(1).map { $0.value.identifier }) XCTAssertEqual(missing, Array(keys.suffixFrom(1))) } @@ -664,7 +664,7 @@ class Persistable_Write_ObjectWithObjectMetadataTests: ObjectWithObjectMetadataT func test__item_persistable__write_async_using_connection() { let expectation = expectationWithDescription("Test: \(#function)") - var result: (TypeUnderTest, MetadataTypeUnderTest?)! = nil + var result: YapItem! = nil item.asyncWriteWithMetadata(connection, metadata: metadata) { tmp in result = tmp @@ -703,7 +703,7 @@ class Persistable_Write_ObjectWithObjectMetadataTests: ObjectWithObjectMetadataT func test__items_persistable__write_async_using_connection() { let expectation = expectationWithDescription("Test: \(#function)") - var result: [(TypeUnderTest, MetadataTypeUnderTest?)] = [] + var result: [YapItem] = [] items.asyncWriteWithMetadata(connection, metadata: metadatas) { tmp in result = tmp diff --git a/Tests/Shared/ObjectWithValueMetadataTests.swift b/Tests/Shared/ObjectWithValueMetadataTests.swift index 919eec4..7fb12a4 100644 --- a/Tests/Shared/ObjectWithValueMetadataTests.swift +++ b/Tests/Shared/ObjectWithValueMetadataTests.swift @@ -104,34 +104,34 @@ class ObjectWithValueMetadataTests: XCTestCase { readTransaction.keys = keys } - func checkTransactionDidWriteItem(result: (TypeUnderTest, MetadataTypeUnderTest?)) { - XCTAssertEqual(result.0.identifier, item.identifier) + func checkTransactionDidWriteItem(result: YapItem) { + XCTAssertEqual(result.value.identifier, item.identifier) XCTAssertFalse(writeTransaction.didWriteAtIndexes.isEmpty) XCTAssertEqual(writeTransaction.didWriteAtIndexes[0].0, index) XCTAssertEqual(writeTransaction.didWriteAtIndexes[0].1.identifier, item.identifier) XCTAssertEqual(MetadataTypeUnderTest.decode(writeTransaction.didWriteAtIndexes[0].2), metadata) } - func checkTransactionDidWriteItems(result: [(TypeUnderTest, MetadataTypeUnderTest?)]) { + func checkTransactionDidWriteItems(result: [YapItem]) { XCTAssertFalse(writeTransaction.didWriteAtIndexes.isEmpty) XCTAssertEqual(writeTransaction.didWriteAtIndexes.map { $0.0.key }.sort(), indexes.map { $0.key }.sort()) XCTAssertEqual(writeTransaction.didWriteAtIndexes.map { $0.2 }.count, items.count) XCTAssertFalse(result.isEmpty) - XCTAssertEqual(Set(result.map({$0.0})), Set(items)) + XCTAssertEqual(Set(result.map({$0.value})), Set(items)) } - func checkTransactionDidReadItem(result: (TypeUnderTest, MetadataTypeUnderTest?)?) -> Bool { + func checkTransactionDidReadItem(result: YapItem?) -> Bool { guard let result = result else { return false } XCTAssertEqual(readTransaction.didReadAtIndex, index) - XCTAssertEqual(result.0.identifier, item.identifier) + XCTAssertEqual(result.value.identifier, item.identifier) XCTAssertEqual(readTransaction.didReadMetadataAtIndex, index) - XCTAssertEqual(result.1, metadata) + XCTAssertEqual(result.metadata, metadata) return true } - func checkTransactionDidReadItems(result: [(TypeUnderTest, MetadataTypeUnderTest?)]) -> Bool { + func checkTransactionDidReadItems(result: [YapItem]) -> Bool { if result.isEmpty { return false } @@ -282,29 +282,29 @@ class Functional_Read_ObjectWithValueMetadataTests: ObjectWithValueMetadataTests class Functional_Write_ObjectWithValueMetadataTests: ObjectWithValueMetadataTests { func test__transaction__write_item() { - checkTransactionDidWriteItem(writeTransaction.writeWithMetadata((item, metadata))) + checkTransactionDidWriteItem(writeTransaction.writeWithMetadata(YapItem(item, metadata))) } func test__transaction__write_items() { - checkTransactionDidWriteItems(writeTransaction.writeWithMetadata(zipToWrite(items, metadatas))) + checkTransactionDidWriteItems(writeTransaction.writeWithMetadata(items.yapItems(with: metadatas))) } // MARK: - Functional API - Connection - Writing func test__connection__write_item() { - checkTransactionDidWriteItem(connection.writeWithMetadata((item, metadata))) + checkTransactionDidWriteItem(connection.writeWithMetadata(YapItem(item, metadata))) XCTAssertTrue(connection.didWrite) } func test__connection__write_items() { - checkTransactionDidWriteItems(connection.writeWithMetadata(zipToWrite(items, metadatas))) + checkTransactionDidWriteItems(connection.writeWithMetadata(items.yapItems(with: metadatas))) XCTAssertTrue(connection.didWrite) } func test__connection__async_write_item() { - var result: (TypeUnderTest, MetadataTypeUnderTest?)! + var result: YapItem! let expectation = expectationWithDescription("Test: \(#function)") - connection.asyncWriteWithMetadata((item, metadata)) { tmp in + connection.asyncWriteWithMetadata(YapItem(item, metadata)) { tmp in result = tmp expectation.fulfill() } @@ -314,9 +314,9 @@ class Functional_Write_ObjectWithValueMetadataTests: ObjectWithValueMetadataTest } func test__connection__async_write_items() { - var result: [(TypeUnderTest, MetadataTypeUnderTest?)] = [] + var result: [YapItem] = [] let expectation = expectationWithDescription("Test: \(#function)") - connection.asyncWriteWithMetadata(zipToWrite(items, metadatas)) { received in + connection.asyncWriteWithMetadata(items.yapItems(with: metadatas)) { received in result = received expectation.fulfill() } @@ -447,14 +447,14 @@ class Persistable_Read_ObjectWithValueMetadataTests: ObjectWithValueMetadataTest func test__reader__in_transaction_at_index_2() { configureForReadingSingle() reader = Read(readTransaction) - let atIndex: YapDB.Index -> (TypeUnderTest, MetadataTypeUnderTest?)? = reader.withMetadataInTransactionAtIndex(readTransaction) + let atIndex: YapDB.Index -> YapItem? = reader.withMetadataInTransactionAtIndex(readTransaction) XCTAssertTrue(checkTransactionDidReadItem(atIndex(index))) } func test__reader__at_index_in_transaction() { configureForReadingSingle() reader = Read(readTransaction) - let inTransaction: TestableReadTransaction -> (TypeUnderTest, MetadataTypeUnderTest?)? = reader.withMetadataAtIndexInTransaction(index) + let inTransaction: TestableReadTransaction -> YapItem? = reader.withMetadataAtIndexInTransaction(index) XCTAssertTrue(checkTransactionDidReadItem(inTransaction(readTransaction))) } @@ -478,14 +478,14 @@ class Persistable_Read_ObjectWithValueMetadataTests: ObjectWithValueMetadataTest func test__reader__in_transaction_by_key_2() { configureForReadingSingle() reader = Read(readTransaction) - let byKey: String -> (TypeUnderTest, MetadataTypeUnderTest?)? = reader.withMetadataInTransactionByKey(readTransaction) + let byKey: String -> YapItem? = reader.withMetadataInTransactionByKey(readTransaction) XCTAssertTrue(checkTransactionDidReadItem(byKey(key))) } func test__reader__by_key_in_transaction() { configureForReadingSingle() reader = Read(readTransaction) - let inTransaction: TestableReadTransaction -> (TypeUnderTest, MetadataTypeUnderTest?)? = reader.withMetadataByKeyInTransaction(key) + let inTransaction: TestableReadTransaction -> YapItem? = reader.withMetadataByKeyInTransaction(key) XCTAssertTrue(checkTransactionDidReadItem(inTransaction(readTransaction))) } @@ -568,9 +568,9 @@ class Persistable_Read_ObjectWithValueMetadataTests: ObjectWithValueMetadataTest func test__reader_with_transaction__filter() { configureForReadingSingle() reader = Read(readTransaction) - let (items, missing): ([(TypeUnderTest, MetadataTypeUnderTest?)], [String]) = reader.withMetadataFilterExisting(keys) + let (items, missing): ([YapItem], [String]) = reader.withMetadataFilterExisting(keys) XCTAssertEqual(readTransaction.didReadAtIndexes.first!, indexes.first!) - XCTAssertEqual(items.map { $0.0.identifier }, items.prefixUpTo(1).map { $0.0.identifier }) + XCTAssertEqual(items.map { $0.value.identifier }, items.prefixUpTo(1).map { $0.value.identifier }) XCTAssertEqual(missing, Array(keys.suffixFrom(1))) } @@ -668,7 +668,7 @@ class Persistable_Write_ObjectWithValueMetadataTests: ObjectWithValueMetadataTes func test__item_persistable__write_async_using_connection() { let expectation = expectationWithDescription("Test: \(#function)") - var result: (TypeUnderTest, MetadataTypeUnderTest?)! = nil + var result: YapItem! = nil item.asyncWriteWithMetadata(connection, metadata: metadata) { tmp in result = tmp @@ -707,7 +707,7 @@ class Persistable_Write_ObjectWithValueMetadataTests: ObjectWithValueMetadataTes func test__items_persistable__write_async_using_connection() { let expectation = expectationWithDescription("Test: \(#function)") - var result: [(TypeUnderTest, MetadataTypeUnderTest?)] = [] + var result: [YapItem] = [] items.asyncWriteWithMetadata(connection, metadata: metadatas) { tmp in result = tmp diff --git a/Tests/Shared/ReadWriteTests.swift b/Tests/Shared/ReadWriteTests.swift index f1c14c3..f526e8d 100644 --- a/Tests/Shared/ReadWriteTests.swift +++ b/Tests/Shared/ReadWriteTests.swift @@ -59,7 +59,7 @@ class ReadWriteBaseTests: XCTestCase { } func writeItemsToDatabase(db: YapDatabase) { - db.makeNewConnection().writeWithMetadata(zipToWrite(items, metadatas)) + db.makeNewConnection().writeWithMetadata(items.yapItems(with: metadatas)) } } diff --git a/Tests/Shared/ValueWithObjectMetadataTests.swift b/Tests/Shared/ValueWithObjectMetadataTests.swift index 80e9b14..b954985 100644 --- a/Tests/Shared/ValueWithObjectMetadataTests.swift +++ b/Tests/Shared/ValueWithObjectMetadataTests.swift @@ -122,34 +122,34 @@ class ValueWithObjectMetadataTests: XCTestCase { readTransaction.keys = keys } - func checkTransactionDidWriteItem(result: (TypeUnderTest, MetadataTypeUnderTest?)) { - XCTAssertEqual(result.0.identifier, item.identifier) + func checkTransactionDidWriteItem(result: YapItem) { + XCTAssertEqual(result.value.identifier, item.identifier) XCTAssertFalse(writeTransaction.didWriteAtIndexes.isEmpty) XCTAssertEqual(writeTransaction.didWriteAtIndexes[0].0, index) XCTAssertEqual(TypeUnderTest.decode(writeTransaction.didWriteAtIndexes[0].1)!, item) XCTAssertEqual(writeTransaction.didWriteAtIndexes[0].2 as? NSNumber, metadata) } - func checkTransactionDidWriteItems(result: [(TypeUnderTest, MetadataTypeUnderTest?)]) { + func checkTransactionDidWriteItems(result: [YapItem]) { XCTAssertFalse(writeTransaction.didWriteAtIndexes.isEmpty) XCTAssertEqual(writeTransaction.didWriteAtIndexes.map { $0.0.key }.sort(), indexes.map { $0.key }.sort()) XCTAssertEqual(writeTransaction.didWriteAtIndexes.map { $0.2 }.count, items.count) XCTAssertFalse(result.isEmpty) - XCTAssertEqual(Set(result.map({$0.0})), Set(items)) + XCTAssertEqual(Set(result.map({$0.value})), Set(items)) } - func checkTransactionDidReadItem(result: (TypeUnderTest, MetadataTypeUnderTest?)?) -> Bool { + func checkTransactionDidReadItem(result: YapItem?) -> Bool { XCTAssertEqual(readTransaction.didReadAtIndex, index) guard let result = result else { return false } XCTAssertEqual(readTransaction.didReadMetadataAtIndex, index) - XCTAssertEqual(result.0.identifier, item.identifier) - XCTAssertEqual(result.1, metadata) + XCTAssertEqual(result.value.identifier, item.identifier) + XCTAssertEqual(result.metadata, metadata) return true } - func checkTransactionDidReadItems(result: [(TypeUnderTest, MetadataTypeUnderTest?)]) -> Bool { + func checkTransactionDidReadItems(result: [YapItem]) -> Bool { if result.isEmpty { return false } @@ -364,29 +364,29 @@ class Functional_Read_Metadata_ValueWithObjectMetadataTests: ValueWithObjectMeta class Functional_Write_ValueWithObjectMetadataTests: ValueWithObjectMetadataTests { func test__transaction__write_item() { - checkTransactionDidWriteItem(writeTransaction.writeWithMetadata((item, metadata))) + checkTransactionDidWriteItem(writeTransaction.writeWithMetadata(YapItem(item, metadata))) } func test__transaction__write_items() { - checkTransactionDidWriteItems(writeTransaction.writeWithMetadata(zipToWrite(items, metadatas))) + checkTransactionDidWriteItems(writeTransaction.writeWithMetadata(items.yapItems(with: metadatas))) } // MARK: - Functional API - Connection - Writing func test__connection__write_item() { - checkTransactionDidWriteItem(connection.writeWithMetadata((item, metadata))) + checkTransactionDidWriteItem(connection.writeWithMetadata(YapItem(item, metadata))) XCTAssertTrue(connection.didWrite) } func test__connection__write_items() { - checkTransactionDidWriteItems(connection.writeWithMetadata(zipToWrite(items, metadatas))) + checkTransactionDidWriteItems(connection.writeWithMetadata(items.yapItems(with: metadatas))) XCTAssertTrue(connection.didWrite) } func test__connection__async_write_item() { - var result: (TypeUnderTest, MetadataTypeUnderTest?)! + var result: YapItem! let expectation = expectationWithDescription("Test: \(#function)") - connection.asyncWriteWithMetadata((item, metadata)) { tmp in + connection.asyncWriteWithMetadata(YapItem(item, metadata)) { tmp in result = tmp expectation.fulfill() } @@ -396,9 +396,9 @@ class Functional_Write_ValueWithObjectMetadataTests: ValueWithObjectMetadataTest } func test__connection__async_write_items() { - var result: [(TypeUnderTest, MetadataTypeUnderTest?)] = [] + var result: [YapItem] = [] let expectation = expectationWithDescription("Test: \(#function)") - connection.asyncWriteWithMetadata(zipToWrite(items, metadatas)) { received in + connection.asyncWriteWithMetadata(items.yapItems(with: metadatas)) { received in result = received expectation.fulfill() } @@ -529,14 +529,14 @@ class Persistable_Read_ValueWithObjectMetadataTests: ValueWithObjectMetadataTest func test__reader__in_transaction_at_index_2() { configureForReadingSingle() reader = Read(readTransaction) - let atIndex: YapDB.Index -> (TypeUnderTest, MetadataTypeUnderTest?)? = reader.withMetadataInTransactionAtIndex(readTransaction) + let atIndex: YapDB.Index -> YapItem? = reader.withMetadataInTransactionAtIndex(readTransaction) XCTAssertTrue(checkTransactionDidReadItem(atIndex(index))) } func test__reader__at_index_in_transaction() { configureForReadingSingle() reader = Read(readTransaction) - let inTransaction: TestableReadTransaction -> (TypeUnderTest, MetadataTypeUnderTest?)? = reader.withMetadataAtIndexInTransaction(index) + let inTransaction: TestableReadTransaction -> YapItem? = reader.withMetadataAtIndexInTransaction(index) XCTAssertTrue(checkTransactionDidReadItem(inTransaction(readTransaction))) } @@ -560,14 +560,14 @@ class Persistable_Read_ValueWithObjectMetadataTests: ValueWithObjectMetadataTest func test__reader__in_transaction_by_key_2() { configureForReadingSingle() reader = Read(readTransaction) - let byKey: String -> (TypeUnderTest, MetadataTypeUnderTest?)? = reader.withMetadataInTransactionByKey(readTransaction) + let byKey: String -> YapItem? = reader.withMetadataInTransactionByKey(readTransaction) XCTAssertTrue(checkTransactionDidReadItem(byKey(key))) } func test__reader__by_key_in_transaction() { configureForReadingSingle() reader = Read(readTransaction) - let inTransaction: TestableReadTransaction -> (TypeUnderTest, MetadataTypeUnderTest?)? = reader.withMetadataByKeyInTransaction(key) + let inTransaction: TestableReadTransaction -> YapItem? = reader.withMetadataByKeyInTransaction(key) XCTAssertTrue(checkTransactionDidReadItem(inTransaction(readTransaction))) } @@ -650,9 +650,9 @@ class Persistable_Read_ValueWithObjectMetadataTests: ValueWithObjectMetadataTest func test__reader_with_transaction__filter() { configureForReadingSingle() reader = Read(readTransaction) - let (items, missing): ([(TypeUnderTest, MetadataTypeUnderTest?)], [String]) = reader.withMetadataFilterExisting(keys) + let (items, missing): ([YapItem], [String]) = reader.withMetadataFilterExisting(keys) XCTAssertEqual(readTransaction.didReadAtIndexes.first!, indexes.first!) - XCTAssertEqual(items.map { $0.0.identifier }, items.prefixUpTo(1).map { $0.0.identifier }) + XCTAssertEqual(items.map { $0.value.identifier }, items.prefixUpTo(1).map { $0.value.identifier }) XCTAssertEqual(missing, Array(keys.suffixFrom(1))) } @@ -801,7 +801,7 @@ class Persistable_Write_ValueWithObjectMetadataTests: ValueWithObjectMetadataTes func test__item_persistable__write_async_using_connection() { let expectation = expectationWithDescription("Test: \(#function)") - var result: (TypeUnderTest, MetadataTypeUnderTest?)! = nil + var result: YapItem! = nil item.asyncWriteWithMetadata(connection, metadata: metadata) { tmp in result = tmp @@ -840,7 +840,7 @@ class Persistable_Write_ValueWithObjectMetadataTests: ValueWithObjectMetadataTes func test__items_persistable__write_async_using_connection() { let expectation = expectationWithDescription("Test: \(#function)") - var result: [(TypeUnderTest, MetadataTypeUnderTest?)] = [] + var result: [YapItem] = [] items.asyncWriteWithMetadata(connection, metadata: metadatas) { tmp in result = tmp diff --git a/Tests/Shared/ValueWithValueMetadataTests.swift b/Tests/Shared/ValueWithValueMetadataTests.swift index d6b4fd6..4b1b257 100644 --- a/Tests/Shared/ValueWithValueMetadataTests.swift +++ b/Tests/Shared/ValueWithValueMetadataTests.swift @@ -120,34 +120,34 @@ class ValueWithValueMetadataTests: XCTestCase { readTransaction.keys = keys } - func checkTransactionDidWriteItem(result: (TypeUnderTest, MetadataTypeUnderTest?)) { - XCTAssertEqual(result.0.identifier, item.identifier) + func checkTransactionDidWriteItem(result: YapItem) { + XCTAssertEqual(result.value.identifier, item.identifier) XCTAssertFalse(writeTransaction.didWriteAtIndexes.isEmpty) XCTAssertEqual(writeTransaction.didWriteAtIndexes[0].0, index) XCTAssertEqual(TypeUnderTest.decode(writeTransaction.didWriteAtIndexes[0].1)!, item) XCTAssertEqual(MetadataTypeUnderTest.decode(writeTransaction.didWriteAtIndexes[0].2), metadata) } - func checkTransactionDidWriteItems(result: [(TypeUnderTest, MetadataTypeUnderTest?)]) { + func checkTransactionDidWriteItems(result: [YapItem]) { XCTAssertFalse(writeTransaction.didWriteAtIndexes.isEmpty) XCTAssertEqual(writeTransaction.didWriteAtIndexes.map { $0.0.key }.sort(), indexes.map { $0.key }.sort()) XCTAssertEqual(writeTransaction.didWriteAtIndexes.map { $0.2 }.count, items.count) XCTAssertFalse(result.isEmpty) - XCTAssertEqual(Set(result.map({$0.0})), Set(items)) + XCTAssertEqual(Set(result.map({$0.value})), Set(items)) } - func checkTransactionDidReadItem(result: (TypeUnderTest, MetadataTypeUnderTest?)?) -> Bool { + func checkTransactionDidReadItem(result: YapItem?) -> Bool { XCTAssertEqual(readTransaction.didReadAtIndex, index) guard let result = result else { return false } XCTAssertEqual(readTransaction.didReadMetadataAtIndex, index) - XCTAssertEqual(result.0.identifier, item.identifier) - XCTAssertEqual(result.1, metadata) + XCTAssertEqual(result.value.identifier, item.identifier) + XCTAssertEqual(result.metadata, metadata) return true } - func checkTransactionDidReadItems(result: [(TypeUnderTest, MetadataTypeUnderTest?)]) -> Bool { + func checkTransactionDidReadItems(result: [YapItem]) -> Bool { if result.isEmpty { return false } @@ -361,29 +361,29 @@ class Functional_Read_Metadata_ValueWithValueMetadataTests: ValueWithValueMetada class Functional_Write_ValueWithValueMetadataTests: ValueWithValueMetadataTests { func test__transaction__write_item() { - checkTransactionDidWriteItem(writeTransaction.writeWithMetadata((item, metadata))) + checkTransactionDidWriteItem(writeTransaction.writeWithMetadata(YapItem(item, metadata))) } func test__transaction__write_items() { - checkTransactionDidWriteItems(writeTransaction.writeWithMetadata(zipToWrite(items, metadatas))) + checkTransactionDidWriteItems(writeTransaction.writeWithMetadata(items.yapItems(with: metadatas))) } // MARK: - Functional API - Connection - Writing func test__connection__write_item() { - checkTransactionDidWriteItem(connection.writeWithMetadata((item, metadata))) + checkTransactionDidWriteItem(connection.writeWithMetadata(YapItem(item, metadata))) XCTAssertTrue(connection.didWrite) } func test__connection__write_items() { - checkTransactionDidWriteItems(connection.writeWithMetadata(zipToWrite(items, metadatas))) + checkTransactionDidWriteItems(connection.writeWithMetadata(items.yapItems(with: metadatas))) XCTAssertTrue(connection.didWrite) } func test__connection__async_write_item() { - var result: (TypeUnderTest, MetadataTypeUnderTest?)! + var result: YapItem! let expectation = expectationWithDescription("Test: \(#function)") - connection.asyncWriteWithMetadata((item, metadata)) { tmp in + connection.asyncWriteWithMetadata(YapItem(item, metadata)) { tmp in result = tmp expectation.fulfill() } @@ -393,9 +393,9 @@ class Functional_Write_ValueWithValueMetadataTests: ValueWithValueMetadataTests } func test__connection__async_write_items() { - var result: [(TypeUnderTest, MetadataTypeUnderTest?)] = [] + var result: [YapItem] = [] let expectation = expectationWithDescription("Test: \(#function)") - connection.asyncWriteWithMetadata(zipToWrite(items, metadatas)) { received in + connection.asyncWriteWithMetadata(items.yapItems(with: metadatas)) { received in result = received expectation.fulfill() } @@ -526,14 +526,14 @@ class Persistable_Read_ValueWithValueMetadataTests: ValueWithValueMetadataTests func test__reader__in_transaction_at_index_2() { configureForReadingSingle() reader = Read(readTransaction) - let atIndex: YapDB.Index -> (TypeUnderTest, MetadataTypeUnderTest?)? = reader.withMetadataInTransactionAtIndex(readTransaction) + let atIndex: YapDB.Index -> YapItem? = reader.withMetadataInTransactionAtIndex(readTransaction) XCTAssertTrue(checkTransactionDidReadItem(atIndex(index))) } func test__reader__at_index_in_transaction() { configureForReadingSingle() reader = Read(readTransaction) - let inTransaction: TestableReadTransaction -> (TypeUnderTest, MetadataTypeUnderTest?)? = reader.withMetadataAtIndexInTransaction(index) + let inTransaction: TestableReadTransaction -> YapItem? = reader.withMetadataAtIndexInTransaction(index) XCTAssertTrue(checkTransactionDidReadItem(inTransaction(readTransaction))) } @@ -557,14 +557,14 @@ class Persistable_Read_ValueWithValueMetadataTests: ValueWithValueMetadataTests func test__reader__in_transaction_by_key_2() { configureForReadingSingle() reader = Read(readTransaction) - let byKey: String -> (TypeUnderTest, MetadataTypeUnderTest?)? = reader.withMetadataInTransactionByKey(readTransaction) + let byKey: String -> YapItem? = reader.withMetadataInTransactionByKey(readTransaction) XCTAssertTrue(checkTransactionDidReadItem(byKey(key))) } func test__reader__by_key_in_transaction() { configureForReadingSingle() reader = Read(readTransaction) - let inTransaction: TestableReadTransaction -> (TypeUnderTest, MetadataTypeUnderTest?)? = reader.withMetadataByKeyInTransaction(key) + let inTransaction: TestableReadTransaction -> YapItem? = reader.withMetadataByKeyInTransaction(key) XCTAssertTrue(checkTransactionDidReadItem(inTransaction(readTransaction))) } @@ -647,9 +647,9 @@ class Persistable_Read_ValueWithValueMetadataTests: ValueWithValueMetadataTests func test__reader_with_transaction__filter() { configureForReadingSingle() reader = Read(readTransaction) - let (items, missing): ([(TypeUnderTest, MetadataTypeUnderTest?)], [String]) = reader.withMetadataFilterExisting(keys) + let (items, missing): ([YapItem], [String]) = reader.withMetadataFilterExisting(keys) XCTAssertEqual(readTransaction.didReadAtIndexes.first!, indexes.first!) - XCTAssertEqual(items.map { $0.0.identifier }, items.prefixUpTo(1).map { $0.0.identifier }) + XCTAssertEqual(items.map { $0.value.identifier }, items.prefixUpTo(1).map { $0.value.identifier }) XCTAssertEqual(missing, Array(keys.suffixFrom(1))) } @@ -798,7 +798,7 @@ class Persistable_Write_ValueWithValueMetadataTests: ValueWithValueMetadataTests func test__item_persistable__write_async_using_connection() { let expectation = expectationWithDescription("Test: \(#function)") - var result: (TypeUnderTest, MetadataTypeUnderTest?)! = nil + var result: YapItem! = nil item.asyncWriteWithMetadata(connection, metadata: metadata) { tmp in result = tmp @@ -837,7 +837,7 @@ class Persistable_Write_ValueWithValueMetadataTests: ValueWithValueMetadataTests func test__items_persistable__write_async_using_connection() { let expectation = expectationWithDescription("Test: \(#function)") - var result: [(TypeUnderTest, MetadataTypeUnderTest?)] = [] + var result: [YapItem] = [] items.asyncWriteWithMetadata(connection, metadata: metadatas) { tmp in result = tmp diff --git a/Tests/Shared/YapDatabaseExtensionsTests.swift b/Tests/Shared/YapDatabaseExtensionsTests.swift index d3b475c..b244ca2 100644 --- a/Tests/Shared/YapDatabaseExtensionsTests.swift +++ b/Tests/Shared/YapDatabaseExtensionsTests.swift @@ -114,10 +114,10 @@ class YapDatabaseReadWriteTransactionTests: ReadWriteBaseTests { db.makeNewConnection().readWriteWithBlock { transaction in transaction.writeAtIndex(self.index, object: self.item, metadata: self.metadata) } - let written: (Employee, NSDate?)? = Employee.read(db).withMetadataAtIndex(index) + let written: YapItem? = Employee.read(db).withMetadataAtIndex(index) XCTAssertNotNil(written) - XCTAssertNotNil(written!.1) - XCTAssertEqual(written!.0.identifier, item.identifier) + XCTAssertNotNil(written!.metadata) + XCTAssertEqual(written!.value.identifier, item.identifier) } func test__remove_at_indexes() { diff --git a/YapDatabaseExtensions/Shared/Curried/Curried_ObjectWithObjectMetadata.swift b/YapDatabaseExtensions/Shared/Curried/Curried_ObjectWithObjectMetadata.swift index 333c42e..77587cb 100644 --- a/YapDatabaseExtensions/Shared/Curried/Curried_ObjectWithObjectMetadata.swift +++ b/YapDatabaseExtensions/Shared/Curried/Curried_ObjectWithObjectMetadata.swift @@ -24,7 +24,7 @@ extension Persistable where public static func readWithMetadataAtIndex< ReadTransaction, Metadata where ReadTransaction: ReadTransactionType, - Metadata: NSCoding>(index: YapDB.Index) -> ReadTransaction -> (Self, Metadata?)? { + Metadata: NSCoding>(index: YapDB.Index) -> ReadTransaction -> YapItem? { return { $0.readWithMetadataAtIndex(index) } } @@ -40,7 +40,7 @@ extension Persistable where Indexes: SequenceType, Indexes.Generator.Element == YapDB.Index, ReadTransaction: ReadTransactionType, - Metadata: NSCoding>(indexes: Indexes) -> ReadTransaction -> [(Self, Metadata?)] { + Metadata: NSCoding>(indexes: Indexes) -> ReadTransaction -> [YapItem] { return { $0.readWithMetadataAtIndexes(indexes) } } @@ -54,7 +54,7 @@ extension Persistable where public static func readWithMetadataByKey< ReadTransaction, Metadata where ReadTransaction: ReadTransactionType, - Metadata: NSCoding>(key: String) -> ReadTransaction -> (Self, Metadata?)? { + Metadata: NSCoding>(key: String) -> ReadTransaction -> YapItem? { return { $0.readWithMetadataByKey(key) } } @@ -70,7 +70,7 @@ extension Persistable where Keys: SequenceType, Keys.Generator.Element == String, ReadTransaction: ReadTransactionType, - Metadata: NSCoding>(keys: Keys) -> ReadTransaction -> [(Self, Metadata?)] { + Metadata: NSCoding>(keys: Keys) -> ReadTransaction -> [YapItem] { return { $0.readWithMetadataAtIndexes(Self.indexesWithKeys(keys)) } } @@ -84,7 +84,7 @@ extension Persistable where public func writeWithMetadata< WriteTransaction, Metadata where WriteTransaction: WriteTransactionType, - Metadata: NSCoding>(metadata: Metadata? = nil) -> WriteTransaction -> (Self, Metadata?) { - return { $0.writeWithMetadata((self, metadata)) } + Metadata: NSCoding>(metadata: Metadata? = nil) -> WriteTransaction -> YapItem { + return { $0.writeWithMetadata(YapItem(self, metadata)) } } } diff --git a/YapDatabaseExtensions/Shared/Curried/Curried_ObjectWithValueMetadata.swift b/YapDatabaseExtensions/Shared/Curried/Curried_ObjectWithValueMetadata.swift index b9b29a4..9dd1702 100644 --- a/YapDatabaseExtensions/Shared/Curried/Curried_ObjectWithValueMetadata.swift +++ b/YapDatabaseExtensions/Shared/Curried/Curried_ObjectWithValueMetadata.swift @@ -26,7 +26,7 @@ extension Persistable where ReadTransaction: ReadTransactionType, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(index: YapDB.Index) -> ReadTransaction -> (Self, Metadata?)? { + Metadata.Coder.ValueType == Metadata>(index: YapDB.Index) -> ReadTransaction -> YapItem? { return { $0.readWithMetadataAtIndex(index) } } @@ -44,7 +44,7 @@ extension Persistable where ReadTransaction: ReadTransactionType, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(indexes: Indexes) -> ReadTransaction -> [(Self, Metadata?)] { + Metadata.Coder.ValueType == Metadata>(indexes: Indexes) -> ReadTransaction -> [YapItem] { return { $0.readWithMetadataAtIndexes(indexes) } } @@ -60,7 +60,7 @@ extension Persistable where ReadTransaction: ReadTransactionType, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(key: String) -> ReadTransaction -> (Self, Metadata?)? { + Metadata.Coder.ValueType == Metadata>(key: String) -> ReadTransaction -> YapItem? { return { $0.readWithMetadataByKey(key) } } @@ -78,7 +78,7 @@ extension Persistable where ReadTransaction: ReadTransactionType, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(keys: Keys) -> ReadTransaction -> [(Self, Metadata?)] { + Metadata.Coder.ValueType == Metadata>(keys: Keys) -> ReadTransaction -> [YapItem] { return { $0.readWithMetadataAtIndexes(Self.indexesWithKeys(keys)) } } @@ -94,7 +94,7 @@ extension Persistable where WriteTransaction: WriteTransactionType, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(metadata: Metadata? = nil) -> WriteTransaction -> (Self, Metadata?) { - return { $0.writeWithMetadata((self, metadata)) } + Metadata.Coder.ValueType == Metadata>(metadata: Metadata? = nil) -> WriteTransaction -> YapItem { + return { $0.writeWithMetadata(YapItem(self, metadata)) } } } diff --git a/YapDatabaseExtensions/Shared/Curried/Curried_ValueWithObjectMetadata.swift b/YapDatabaseExtensions/Shared/Curried/Curried_ValueWithObjectMetadata.swift index c4c86bd..6ff0779 100644 --- a/YapDatabaseExtensions/Shared/Curried/Curried_ValueWithObjectMetadata.swift +++ b/YapDatabaseExtensions/Shared/Curried/Curried_ValueWithObjectMetadata.swift @@ -26,7 +26,7 @@ extension Persistable where public static func readWithMetadataAtIndex< ReadTransaction, Metadata where ReadTransaction: ReadTransactionType, - Metadata: NSCoding>(index: YapDB.Index) -> ReadTransaction -> (Self, Metadata?)? { + Metadata: NSCoding>(index: YapDB.Index) -> ReadTransaction -> YapItem? { return { $0.readWithMetadataAtIndex(index) } } @@ -42,7 +42,7 @@ extension Persistable where Indexes: SequenceType, Indexes.Generator.Element == YapDB.Index, ReadTransaction: ReadTransactionType, - Metadata: NSCoding>(indexes: Indexes) -> ReadTransaction -> [(Self, Metadata?)] { + Metadata: NSCoding>(indexes: Indexes) -> ReadTransaction -> [YapItem] { return { $0.readWithMetadataAtIndexes(indexes) } } @@ -56,7 +56,7 @@ extension Persistable where public static func readWithMetadataByKey< ReadTransaction, Metadata where ReadTransaction: ReadTransactionType, - Metadata: NSCoding>(key: String) -> ReadTransaction -> (Self, Metadata?)? { + Metadata: NSCoding>(key: String) -> ReadTransaction -> YapItem? { return { $0.readWithMetadataByKey(key) } } @@ -72,7 +72,7 @@ extension Persistable where Keys: SequenceType, Keys.Generator.Element == String, ReadTransaction: ReadTransactionType, - Metadata: NSCoding>(keys: Keys) -> ReadTransaction -> [(Self, Metadata?)] { + Metadata: NSCoding>(keys: Keys) -> ReadTransaction -> [YapItem] { return { $0.readWithMetadataAtIndexes(Self.indexesWithKeys(keys)) } } @@ -86,7 +86,7 @@ extension Persistable where public func writeWithMetadata< WriteTransaction, Metadata where WriteTransaction: WriteTransactionType, - Metadata: NSCoding>(metadata: Metadata? = nil) -> WriteTransaction -> (Self, Metadata?) { - return { $0.writeWithMetadata((self, metadata)) } + Metadata: NSCoding>(metadata: Metadata? = nil) -> WriteTransaction -> YapItem { + return { $0.writeWithMetadata(YapItem(self, metadata)) } } } diff --git a/YapDatabaseExtensions/Shared/Curried/Curried_ValueWithValueMetadata.swift b/YapDatabaseExtensions/Shared/Curried/Curried_ValueWithValueMetadata.swift index a3e59fe..4525175 100644 --- a/YapDatabaseExtensions/Shared/Curried/Curried_ValueWithValueMetadata.swift +++ b/YapDatabaseExtensions/Shared/Curried/Curried_ValueWithValueMetadata.swift @@ -28,7 +28,7 @@ extension Persistable where ReadTransaction: ReadTransactionType, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(index: YapDB.Index) -> ReadTransaction -> (Self, Metadata?)? { + Metadata.Coder.ValueType == Metadata>(index: YapDB.Index) -> ReadTransaction -> YapItem? { return { $0.readWithMetadataAtIndex(index) } } @@ -46,7 +46,7 @@ extension Persistable where ReadTransaction: ReadTransactionType, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(indexes: Indexes) -> ReadTransaction -> [(Self, Metadata?)] { + Metadata.Coder.ValueType == Metadata>(indexes: Indexes) -> ReadTransaction -> [YapItem] { return { $0.readWithMetadataAtIndexes(indexes) } } @@ -62,7 +62,7 @@ extension Persistable where ReadTransaction: ReadTransactionType, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(key: String) -> ReadTransaction -> (Self, Metadata?)? { + Metadata.Coder.ValueType == Metadata>(key: String) -> ReadTransaction -> YapItem? { return { $0.readWithMetadataByKey(key) } } @@ -80,7 +80,7 @@ extension Persistable where ReadTransaction: ReadTransactionType, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(keys: Keys) -> ReadTransaction -> [(Self, Metadata?)] { + Metadata.Coder.ValueType == Metadata>(keys: Keys) -> ReadTransaction -> [YapItem] { return { $0.readWithMetadataAtIndexes(Self.indexesWithKeys(keys)) } } @@ -96,7 +96,7 @@ extension Persistable where WriteTransaction: WriteTransactionType, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(metadata: Metadata? = nil) -> WriteTransaction -> (Self, Metadata?) { - return { $0.writeWithMetadata((self, metadata)) } + Metadata.Coder.ValueType == Metadata>(metadata: Metadata? = nil) -> WriteTransaction -> YapItem { + return { $0.writeWithMetadata(YapItem(self, metadata)) } } } diff --git a/YapDatabaseExtensions/Shared/Functional/Functional_ObjectWithObjectMetadata.swift b/YapDatabaseExtensions/Shared/Functional/Functional_ObjectWithObjectMetadata.swift index eb92925..3a9d98d 100644 --- a/YapDatabaseExtensions/Shared/Functional/Functional_ObjectWithObjectMetadata.swift +++ b/YapDatabaseExtensions/Shared/Functional/Functional_ObjectWithObjectMetadata.swift @@ -24,10 +24,10 @@ extension ReadTransactionType { Object, Metadata where Object: Persistable, Object: NSCoding, - Metadata: NSCoding>(index: YapDB.Index) -> (Object, Metadata?)? { + Metadata: NSCoding>(index: YapDB.Index) -> YapItem? { guard let item: Object = readAtIndex(index) else { return nil } let metadata: Metadata? = readMetadataAtIndex(index) - return (item, metadata) + return YapItem(item, metadata) } /** @@ -42,7 +42,7 @@ extension ReadTransactionType { Indexes.Generator.Element == YapDB.Index, Object: Persistable, Object: NSCoding, - Metadata: NSCoding>(indexes: Indexes) -> [(Object, Metadata?)] { + Metadata: NSCoding>(indexes: Indexes) -> [YapItem] { // FIXME: using flatMap means the output length need not match the input length return indexes.flatMap(readWithMetadataAtIndex) } @@ -57,7 +57,7 @@ extension ReadTransactionType { Object, Metadata where Object: Persistable, Object: NSCoding, - Metadata: NSCoding>(key: String) -> (Object, Metadata?)? { + Metadata: NSCoding>(key: String) -> YapItem? { return readWithMetadataAtIndex(Object.indexWithKey(key)) } @@ -73,7 +73,7 @@ extension ReadTransactionType { Keys.Generator.Element == String, Object: Persistable, Object: NSCoding, - Metadata: NSCoding>(keys: Keys) -> [(Object, Metadata?)] { + Metadata: NSCoding>(keys: Keys) -> [YapItem] { return readWithMetadataAtIndexes(Object.indexesWithKeys(keys)) } @@ -86,7 +86,7 @@ extension ReadTransactionType { Object, Metadata where Object: Persistable, Object: NSCoding, - Metadata: NSCoding>() -> [(Object, Metadata?)] { + Metadata: NSCoding>() -> [YapItem] { return readWithMetadataByKeys(keysInCollection(Object.collection)) } } @@ -103,7 +103,7 @@ extension ConnectionType { Object, Metadata where Object: Persistable, Object: NSCoding, - Metadata: NSCoding>(index: YapDB.Index) -> (Object, Metadata?)? { + Metadata: NSCoding>(index: YapDB.Index) -> YapItem? { return read { $0.readWithMetadataAtIndex(index) } } @@ -119,7 +119,7 @@ extension ConnectionType { Indexes.Generator.Element == YapDB.Index, Object: Persistable, Object: NSCoding, - Metadata: NSCoding>(indexes: Indexes) -> [(Object, Metadata?)] { + Metadata: NSCoding>(indexes: Indexes) -> [YapItem] { return read { $0.readWithMetadataAtIndexes(indexes) } } @@ -133,7 +133,7 @@ extension ConnectionType { Object, Metadata where Object: Persistable, Object: NSCoding, - Metadata: NSCoding>(key: String) -> (Object, Metadata?)? { + Metadata: NSCoding>(key: String) -> YapItem? { return readWithMetadataAtIndex(Object.indexWithKey(key)) } @@ -149,7 +149,7 @@ extension ConnectionType { Keys.Generator.Element == String, Object: Persistable, Object: NSCoding, - Metadata: NSCoding>(keys: Keys) -> [(Object, Metadata?)] { + Metadata: NSCoding>(keys: Keys) -> [YapItem] { return readWithMetadataAtIndexes(Object.indexesWithKeys(keys)) } @@ -162,7 +162,7 @@ extension ConnectionType { Object, Metadata where Object: Persistable, Object: NSCoding, - Metadata: NSCoding>() -> [(Object, Metadata?)] { + Metadata: NSCoding>() -> [YapItem] { return read { $0.readWithMetadataAll() } } } @@ -180,8 +180,8 @@ extension WriteTransactionType { Object, Metadata where Object: Persistable, Object: NSCoding, - Metadata: NSCoding>(item: (Object, Metadata?)) -> (Object, Metadata?) { - writeAtIndex(item.0.index, object: item.0, metadata: item.1) + Metadata: NSCoding>(item: YapItem) -> YapItem { + writeAtIndex(item.value.index, object: item.value, metadata: item.metadata) return item } @@ -193,10 +193,10 @@ extension WriteTransactionType { public func writeWithMetadata< Items, Object, Metadata where Items: SequenceType, - Items.Generator.Element == (Object, Metadata?), + Items.Generator.Element == YapItem, Object: Persistable, Object: NSCoding, - Metadata: NSCoding>(items: Items) -> [(Object, Metadata?)] { + Metadata: NSCoding>(items: Items) -> [YapItem] { return items.map(writeWithMetadata) } } @@ -212,7 +212,7 @@ extension ConnectionType { Object, Metadata where Object: Persistable, Object: NSCoding, - Metadata: NSCoding>(item: (Object, Metadata?)) -> (Object, Metadata?) { + Metadata: NSCoding>(item: YapItem) -> YapItem { return write { $0.writeWithMetadata(item) } } @@ -224,10 +224,10 @@ extension ConnectionType { public func writeWithMetadata< Items, Object, Metadata where Items: SequenceType, - Items.Generator.Element == (Object, Metadata?), + Items.Generator.Element == YapItem, Object: Persistable, Object: NSCoding, - Metadata: NSCoding>(items: Items) -> [(Object, Metadata?)] { + Metadata: NSCoding>(items: Items) -> [YapItem] { return write { $0.writeWithMetadata(items) } } @@ -242,7 +242,7 @@ extension ConnectionType { Object, Metadata where Object: Persistable, Object: NSCoding, - Metadata: NSCoding>(item: (Object, Metadata?), queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ((Object, Metadata?) -> Void)? = .None) { + Metadata: NSCoding>(item: YapItem, queue: dispatch_queue_t = dispatch_get_main_queue(), completion: (YapItem -> Void)? = .None) { asyncWrite({ $0.writeWithMetadata(item) }, queue: queue, completion: completion) } @@ -256,10 +256,10 @@ extension ConnectionType { public func asyncWriteWithMetadata< Items, Object, Metadata where Items: SequenceType, - Items.Generator.Element == (Object, Metadata?), + Items.Generator.Element == YapItem, Object: Persistable, Object: NSCoding, - Metadata: NSCoding>(items: Items, queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ([(Object, Metadata?)] -> Void)? = .None) { + Metadata: NSCoding>(items: Items, queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ([YapItem] -> Void)? = .None) { asyncWrite({ $0.writeWithMetadata(items) }, queue: queue, completion: completion) } } diff --git a/YapDatabaseExtensions/Shared/Functional/Functional_ObjectWithValueMetadata.swift b/YapDatabaseExtensions/Shared/Functional/Functional_ObjectWithValueMetadata.swift index cd0b332..ca78eb5 100644 --- a/YapDatabaseExtensions/Shared/Functional/Functional_ObjectWithValueMetadata.swift +++ b/YapDatabaseExtensions/Shared/Functional/Functional_ObjectWithValueMetadata.swift @@ -26,10 +26,10 @@ extension ReadTransactionType { Object: NSCoding, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(index: YapDB.Index) -> (Object, Metadata?)? { + Metadata.Coder.ValueType == Metadata>(index: YapDB.Index) -> YapItem? { guard let item: Object = readAtIndex(index) else { return nil } let metadata: Metadata? = readMetadataAtIndex(index) - return (item, metadata) + return YapItem(item, metadata) } /** @@ -46,7 +46,7 @@ extension ReadTransactionType { Object: NSCoding, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(indexes: Indexes) -> [(Object, Metadata?)] { + Metadata.Coder.ValueType == Metadata>(indexes: Indexes) -> [YapItem] { // FIXME: using flatMap means the output length need not match the input length return indexes.flatMap(readWithMetadataAtIndex) } @@ -63,7 +63,7 @@ extension ReadTransactionType { Object: NSCoding, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(key: String) -> (Object, Metadata?)? { + Metadata.Coder.ValueType == Metadata>(key: String) -> YapItem? { return readWithMetadataAtIndex(Object.indexWithKey(key)) } @@ -81,7 +81,7 @@ extension ReadTransactionType { Object: NSCoding, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(keys: Keys) -> [(Object, Metadata?)] { + Metadata.Coder.ValueType == Metadata>(keys: Keys) -> [YapItem] { return readWithMetadataAtIndexes(Object.indexesWithKeys(keys)) } @@ -96,7 +96,7 @@ extension ReadTransactionType { Object: NSCoding, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>() -> [(Object, Metadata?)] { + Metadata.Coder.ValueType == Metadata>() -> [YapItem] { return readWithMetadataByKeys(keysInCollection(Object.collection)) } } @@ -115,7 +115,7 @@ extension ConnectionType { Object: NSCoding, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(index: YapDB.Index) -> (Object, Metadata?)? { + Metadata.Coder.ValueType == Metadata>(index: YapDB.Index) -> YapItem? { return read { $0.readWithMetadataAtIndex(index) } } @@ -133,7 +133,7 @@ extension ConnectionType { Object: NSCoding, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(indexes: Indexes) -> [(Object, Metadata?)] { + Metadata.Coder.ValueType == Metadata>(indexes: Indexes) -> [YapItem] { return read { $0.readWithMetadataAtIndexes(indexes) } } @@ -149,7 +149,7 @@ extension ConnectionType { Object: NSCoding, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(key: String) -> (Object, Metadata?)? { + Metadata.Coder.ValueType == Metadata>(key: String) -> YapItem? { return readWithMetadataAtIndex(Object.indexWithKey(key)) } @@ -167,7 +167,7 @@ extension ConnectionType { Object: NSCoding, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(keys: Keys) -> [(Object, Metadata?)] { + Metadata.Coder.ValueType == Metadata>(keys: Keys) -> [YapItem] { return readWithMetadataAtIndexes(Object.indexesWithKeys(keys)) } @@ -182,7 +182,7 @@ extension ConnectionType { Object: NSCoding, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>() -> [(Object, Metadata?)] { + Metadata.Coder.ValueType == Metadata>() -> [YapItem] { return read { $0.readWithMetadataAll() } } } @@ -202,8 +202,8 @@ extension WriteTransactionType { Object: NSCoding, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(item: (Object, Metadata?)) -> (Object, Metadata?) { - writeAtIndex(item.0.index, object: item.0, metadata: item.1?.encoded) + Metadata.Coder.ValueType == Metadata>(item: YapItem) -> YapItem { + writeAtIndex(item.value.index, object: item.value, metadata: item.metadata?.encoded) return item } @@ -215,12 +215,12 @@ extension WriteTransactionType { public func writeWithMetadata< Items, Object, Metadata where Items: SequenceType, - Items.Generator.Element == (Object, Metadata?), + Items.Generator.Element == YapItem, Object: Persistable, Object: NSCoding, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(items: Items) -> [(Object, Metadata?)] { + Metadata.Coder.ValueType == Metadata>(items: Items) -> [YapItem] { return items.map(writeWithMetadata) } } @@ -238,7 +238,7 @@ extension ConnectionType { Object: NSCoding, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(item: (Object, Metadata?)) -> (Object, Metadata?) { + Metadata.Coder.ValueType == Metadata>(item: YapItem) -> YapItem { return write { $0.writeWithMetadata(item) } } @@ -250,12 +250,12 @@ extension ConnectionType { public func writeWithMetadata< Items, Object, Metadata where Items: SequenceType, - Items.Generator.Element == (Object, Metadata?), + Items.Generator.Element == YapItem, Object: Persistable, Object: NSCoding, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(items: Items) -> [(Object, Metadata?)] { + Metadata.Coder.ValueType == Metadata>(items: Items) -> [YapItem] { return write { $0.writeWithMetadata(items) } } @@ -272,7 +272,7 @@ extension ConnectionType { Object: NSCoding, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(item: (Object, Metadata?), queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ((Object, Metadata?) -> Void)? = .None) { + Metadata.Coder.ValueType == Metadata>(item: YapItem, queue: dispatch_queue_t = dispatch_get_main_queue(), completion: (YapItem -> Void)? = .None) { asyncWrite({ $0.writeWithMetadata(item) }, queue: queue, completion: completion) } @@ -286,12 +286,12 @@ extension ConnectionType { public func asyncWriteWithMetadata< Items, Object, Metadata where Items: SequenceType, - Items.Generator.Element == (Object, Metadata?), + Items.Generator.Element == YapItem, Object: Persistable, Object: NSCoding, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(items: Items, queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ([(Object, Metadata?)] -> Void)? = .None) { + Metadata.Coder.ValueType == Metadata>(items: Items, queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ([YapItem] -> Void)? = .None) { asyncWrite({ $0.writeWithMetadata(items) }, queue: queue, completion: completion) } } diff --git a/YapDatabaseExtensions/Shared/Functional/Functional_ValueWIthObjectMetadata.swift b/YapDatabaseExtensions/Shared/Functional/Functional_ValueWIthObjectMetadata.swift index 1b14983..ce9f5b8 100644 --- a/YapDatabaseExtensions/Shared/Functional/Functional_ValueWIthObjectMetadata.swift +++ b/YapDatabaseExtensions/Shared/Functional/Functional_ValueWIthObjectMetadata.swift @@ -26,10 +26,10 @@ extension ReadTransactionType { Value: ValueCoding, Value.Coder: NSCoding, Value.Coder.ValueType == Value, - Metadata: NSCoding>(index: YapDB.Index) -> (Value, Metadata?)? { + Metadata: NSCoding>(index: YapDB.Index) -> YapItem? { guard let item: Value = Value.decode(readAtIndex(index)) else { return nil } let metadata: Metadata? = readMetadataAtIndex(index) - return (item, metadata) + return YapItem(item, metadata) } /** @@ -46,7 +46,7 @@ extension ReadTransactionType { Value: ValueCoding, Value.Coder: NSCoding, Value.Coder.ValueType == Value, - Metadata: NSCoding>(indexes: Indexes) -> [(Value, Metadata?)] { + Metadata: NSCoding>(indexes: Indexes) -> [YapItem] { // FIXME: using flatMap means the output length need not match the input length return indexes.flatMap(readWithMetadataAtIndex) } @@ -63,7 +63,7 @@ extension ReadTransactionType { Value: ValueCoding, Value.Coder: NSCoding, Value.Coder.ValueType == Value, - Metadata: NSCoding>(key: String) -> (Value, Metadata?)? { + Metadata: NSCoding>(key: String) -> YapItem? { return readWithMetadataAtIndex(Value.indexWithKey(key)) } @@ -81,7 +81,7 @@ extension ReadTransactionType { Value: ValueCoding, Value.Coder: NSCoding, Value.Coder.ValueType == Value, - Metadata: NSCoding>(keys: Keys) -> [(Value, Metadata?)] { + Metadata: NSCoding>(keys: Keys) -> [YapItem] { return readWithMetadataAtIndexes(Value.indexesWithKeys(keys)) } @@ -96,7 +96,7 @@ extension ReadTransactionType { Value: ValueCoding, Value.Coder: NSCoding, Value.Coder.ValueType == Value, - Metadata: NSCoding>() -> [(Value, Metadata?)] { + Metadata: NSCoding>() -> [YapItem] { return readWithMetadataByKeys(keysInCollection(Value.collection)) } } @@ -115,7 +115,7 @@ extension ConnectionType { Value: ValueCoding, Value.Coder: NSCoding, Value.Coder.ValueType == Value, - Metadata: NSCoding>(index: YapDB.Index) -> (Value, Metadata?)? { + Metadata: NSCoding>(index: YapDB.Index) -> YapItem? { return read { $0.readWithMetadataAtIndex(index) } } @@ -133,7 +133,7 @@ extension ConnectionType { Value: ValueCoding, Value.Coder: NSCoding, Value.Coder.ValueType == Value, - Metadata: NSCoding>(indexes: Indexes) -> [(Value, Metadata?)] { + Metadata: NSCoding>(indexes: Indexes) -> [YapItem] { return read { $0.readWithMetadataAtIndexes(indexes) } } @@ -149,7 +149,7 @@ extension ConnectionType { Value: ValueCoding, Value.Coder: NSCoding, Value.Coder.ValueType == Value, - Metadata: NSCoding>(key: String) -> (Value, Metadata?)? { + Metadata: NSCoding>(key: String) -> YapItem? { return readWithMetadataAtIndex(Value.indexWithKey(key)) } @@ -167,7 +167,7 @@ extension ConnectionType { Value: ValueCoding, Value.Coder: NSCoding, Value.Coder.ValueType == Value, - Metadata: NSCoding>(keys: Keys) -> [(Value, Metadata?)] { + Metadata: NSCoding>(keys: Keys) -> [YapItem] { return readWithMetadataAtIndexes(Value.indexesWithKeys(keys)) } @@ -182,7 +182,7 @@ extension ConnectionType { Value: ValueCoding, Value.Coder: NSCoding, Value.Coder.ValueType == Value, - Metadata: NSCoding>() -> [(Value, Metadata?)] { + Metadata: NSCoding>() -> [YapItem] { return read { $0.readWithMetadataAll() } } } @@ -202,8 +202,8 @@ extension WriteTransactionType { Value: ValueCoding, Value.Coder: NSCoding, Value.Coder.ValueType == Value, - Metadata: NSCoding>(item: (Value, Metadata?)) -> (Value, Metadata?) { - writeAtIndex(item.0.index, object: item.0.encoded, metadata: item.1) + Metadata: NSCoding>(item: YapItem) -> YapItem { + writeAtIndex(item.value.index, object: item.value.encoded, metadata: item.metadata) return item } @@ -215,12 +215,12 @@ extension WriteTransactionType { public func writeWithMetadata< Items, Value, Metadata where Items: SequenceType, - Items.Generator.Element == (Value, Metadata?), + Items.Generator.Element == YapItem, Value: Persistable, Value: ValueCoding, Value.Coder: NSCoding, Value.Coder.ValueType == Value, - Metadata: NSCoding>(items: Items) -> [(Value, Metadata?)] { + Metadata: NSCoding>(items: Items) -> [YapItem] { return items.map(writeWithMetadata) } } @@ -238,7 +238,7 @@ extension ConnectionType { Value: ValueCoding, Value.Coder: NSCoding, Value.Coder.ValueType == Value, - Metadata: NSCoding>(item: (Value, Metadata?)) -> (Value, Metadata?) { + Metadata: NSCoding>(item: YapItem) -> YapItem { return write { $0.writeWithMetadata(item) } } @@ -250,12 +250,12 @@ extension ConnectionType { public func writeWithMetadata< Items, Value, Metadata where Items: SequenceType, - Items.Generator.Element == (Value, Metadata?), + Items.Generator.Element == YapItem, Value: Persistable, Value: ValueCoding, Value.Coder: NSCoding, Value.Coder.ValueType == Value, - Metadata: NSCoding>(items: Items) -> [(Value, Metadata?)] { + Metadata: NSCoding>(items: Items) -> [YapItem] { return write { $0.writeWithMetadata(items) } } @@ -272,7 +272,7 @@ extension ConnectionType { Value: ValueCoding, Value.Coder: NSCoding, Value.Coder.ValueType == Value, - Metadata: NSCoding>(item: (Value, Metadata?), queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ((Value, Metadata?) -> Void)? = .None) { + Metadata: NSCoding>(item: YapItem, queue: dispatch_queue_t = dispatch_get_main_queue(), completion: (YapItem -> Void)? = .None) { asyncWrite({ $0.writeWithMetadata(item) }, queue: queue, completion: completion) } @@ -286,12 +286,12 @@ extension ConnectionType { public func asyncWriteWithMetadata< Items, Value, Metadata where Items: SequenceType, - Items.Generator.Element == (Value, Metadata?), + Items.Generator.Element == YapItem, Value: Persistable, Value: ValueCoding, Value.Coder: NSCoding, Value.Coder.ValueType == Value, - Metadata: NSCoding>(items: Items, queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ([(Value, Metadata?)] -> Void)? = .None) { + Metadata: NSCoding>(items: Items, queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ([YapItem] -> Void)? = .None) { asyncWrite({ $0.writeWithMetadata(items) }, queue: queue, completion: completion) } } diff --git a/YapDatabaseExtensions/Shared/Functional/Functional_ValueWIthValueMetadata.swift b/YapDatabaseExtensions/Shared/Functional/Functional_ValueWIthValueMetadata.swift index ea95096..19fb418 100644 --- a/YapDatabaseExtensions/Shared/Functional/Functional_ValueWIthValueMetadata.swift +++ b/YapDatabaseExtensions/Shared/Functional/Functional_ValueWIthValueMetadata.swift @@ -28,10 +28,10 @@ extension ReadTransactionType { Value.Coder.ValueType == Value, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(index: YapDB.Index) -> (Value, Metadata?)? { + Metadata.Coder.ValueType == Metadata>(index: YapDB.Index) -> YapItem? { guard let item: Value = Value.decode(readAtIndex(index)) else { return nil } let metadata: Metadata? = readMetadataAtIndex(index) - return (item, metadata) + return YapItem(item, metadata) } /** @@ -50,7 +50,7 @@ extension ReadTransactionType { Value.Coder.ValueType == Value, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(indexes: Indexes) -> [(Value, Metadata?)] { + Metadata.Coder.ValueType == Metadata>(indexes: Indexes) -> [YapItem] { // FIXME: using flatMap means the output length need not match the input length return indexes.flatMap(readWithMetadataAtIndex) } @@ -69,7 +69,7 @@ extension ReadTransactionType { Value.Coder.ValueType == Value, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(key: String) -> (Value, Metadata?)? { + Metadata.Coder.ValueType == Metadata>(key: String) -> YapItem? { return readWithMetadataAtIndex(Value.indexWithKey(key)) } @@ -89,7 +89,7 @@ extension ReadTransactionType { Value.Coder.ValueType == Value, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(keys: Keys) -> [(Value, Metadata?)] { + Metadata.Coder.ValueType == Metadata>(keys: Keys) -> [YapItem] { return readWithMetadataAtIndexes(Value.indexesWithKeys(keys)) } @@ -106,7 +106,7 @@ extension ReadTransactionType { Value.Coder.ValueType == Value, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>() -> [(Value, Metadata?)] { + Metadata.Coder.ValueType == Metadata>() -> [YapItem] { return readWithMetadataByKeys(keysInCollection(Value.collection)) } } @@ -127,7 +127,7 @@ extension ConnectionType { Value.Coder.ValueType == Value, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(index: YapDB.Index) -> (Value, Metadata?)? { + Metadata.Coder.ValueType == Metadata>(index: YapDB.Index) -> YapItem? { return read { $0.readWithMetadataAtIndex(index) } } @@ -147,7 +147,7 @@ extension ConnectionType { Value.Coder.ValueType == Value, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(indexes: Indexes) -> [(Value, Metadata?)] { + Metadata.Coder.ValueType == Metadata>(indexes: Indexes) -> [YapItem] { return read { $0.readWithMetadataAtIndexes(indexes) } } @@ -165,7 +165,7 @@ extension ConnectionType { Value.Coder.ValueType == Value, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(key: String) -> (Value, Metadata?)? { + Metadata.Coder.ValueType == Metadata>(key: String) -> YapItem? { return readWithMetadataAtIndex(Value.indexWithKey(key)) } @@ -185,7 +185,7 @@ extension ConnectionType { Value.Coder.ValueType == Value, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(keys: Keys) -> [(Value, Metadata?)] { + Metadata.Coder.ValueType == Metadata>(keys: Keys) -> [YapItem] { return readWithMetadataAtIndexes(Value.indexesWithKeys(keys)) } @@ -202,7 +202,7 @@ extension ConnectionType { Value.Coder.ValueType == Value, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>() -> [(Value, Metadata?)] { + Metadata.Coder.ValueType == Metadata>() -> [YapItem] { return read { $0.readWithMetadataAll() } } } @@ -224,8 +224,8 @@ extension WriteTransactionType { Value.Coder.ValueType == Value, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(item: (Value, Metadata?)) -> (Value, Metadata?) { - writeAtIndex(item.0.index, object: item.0.encoded, metadata: item.1?.encoded) + Metadata.Coder.ValueType == Metadata>(item: YapItem) -> YapItem { + writeAtIndex(item.value.index, object: item.value.encoded, metadata: item.metadata?.encoded) return item } @@ -237,14 +237,14 @@ extension WriteTransactionType { public func writeWithMetadata< Items, Value, Metadata where Items: SequenceType, - Items.Generator.Element == (Value, Metadata?), + Items.Generator.Element == YapItem, Value: Persistable, Value: ValueCoding, Value.Coder: NSCoding, Value.Coder.ValueType == Value, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(items: Items) -> [(Value, Metadata?)] { + Metadata.Coder.ValueType == Metadata>(items: Items) -> [YapItem] { return items.map(writeWithMetadata) } } @@ -264,7 +264,7 @@ extension ConnectionType { Value.Coder.ValueType == Value, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(item: (Value, Metadata?)) -> (Value, Metadata?) { + Metadata.Coder.ValueType == Metadata>(item: YapItem) -> YapItem { return write { $0.writeWithMetadata(item) } } @@ -276,14 +276,14 @@ extension ConnectionType { public func writeWithMetadata< Items, Value, Metadata where Items: SequenceType, - Items.Generator.Element == (Value, Metadata?), + Items.Generator.Element == YapItem, Value: Persistable, Value: ValueCoding, Value.Coder: NSCoding, Value.Coder.ValueType == Value, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(items: Items) -> [(Value, Metadata?)] { + Metadata.Coder.ValueType == Metadata>(items: Items) -> [YapItem] { return write { $0.writeWithMetadata(items) } } @@ -302,7 +302,7 @@ extension ConnectionType { Value.Coder.ValueType == Value, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(item: (Value, Metadata?), queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ((Value, Metadata?) -> Void)? = .None) { + Metadata.Coder.ValueType == Metadata>(item: YapItem, queue: dispatch_queue_t = dispatch_get_main_queue(), completion: (YapItem -> Void)? = .None) { asyncWrite({ $0.writeWithMetadata(item) }, queue: queue, completion: completion) } @@ -316,14 +316,14 @@ extension ConnectionType { public func asyncWriteWithMetadata< Items, Value, Metadata where Items: SequenceType, - Items.Generator.Element == (Value, Metadata?), + Items.Generator.Element == YapItem, Value: Persistable, Value: ValueCoding, Value.Coder: NSCoding, Value.Coder.ValueType == Value, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(items: Items, queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ([(Value, Metadata?)] -> Void)? = .None) { + Metadata.Coder.ValueType == Metadata>(items: Items, queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ([YapItem] -> Void)? = .None) { asyncWrite({ $0.writeWithMetadata(items) }, queue: queue, completion: completion) } } diff --git a/YapDatabaseExtensions/Shared/Persistable/Persistable_ObjectWithObjectMetadata.swift b/YapDatabaseExtensions/Shared/Persistable/Persistable_ObjectWithObjectMetadata.swift index 0d86004..abc3f22 100644 --- a/YapDatabaseExtensions/Shared/Persistable/Persistable_ObjectWithObjectMetadata.swift +++ b/YapDatabaseExtensions/Shared/Persistable/Persistable_ObjectWithObjectMetadata.swift @@ -24,8 +24,8 @@ extension Persistable where public func writeWithMetadata< WriteTransaction, Metadata where WriteTransaction: WriteTransactionType, - Metadata: NSCoding>(transaction: WriteTransaction, metadata: Metadata?) -> (Self, Metadata?) { - return transaction.writeWithMetadata((self, metadata)) + Metadata: NSCoding>(transaction: WriteTransaction, metadata: Metadata?) -> YapItem { + return transaction.writeWithMetadata(YapItem(self, metadata)) } /** @@ -37,8 +37,8 @@ extension Persistable where public func writeWithMetadata< Connection, Metadata where Connection: ConnectionType, - Metadata: NSCoding>(connection: Connection, metadata: Metadata?) -> (Self, Metadata?) { - return connection.writeWithMetadata((self, metadata)) + Metadata: NSCoding>(connection: Connection, metadata: Metadata?) -> YapItem { + return connection.writeWithMetadata(YapItem(self, metadata)) } /** @@ -50,8 +50,8 @@ extension Persistable where public func asyncWriteWithMetadata< Connection, Metadata where Connection: ConnectionType, - Metadata: NSCoding>(connection: Connection, metadata: Metadata?, queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ((Self, Metadata?) -> Void)? = .None) { - return connection.asyncWriteWithMetadata((self, metadata), queue: queue, completion: completion) + Metadata: NSCoding>(connection: Connection, metadata: Metadata?, queue: dispatch_queue_t = dispatch_get_main_queue(), completion: (YapItem -> Void)? = .None) { + return connection.asyncWriteWithMetadata(YapItem(self, metadata), queue: queue, completion: completion) } /** @@ -64,7 +64,7 @@ extension Persistable where Connection, Metadata where Connection: ConnectionType, Metadata: NSCoding>(connection: Connection, metadata: Metadata?) -> NSOperation { - return NSBlockOperation { connection.writeWithMetadata((self, metadata)) } + return NSBlockOperation { connection.writeWithMetadata(YapItem(self, metadata)) } } } @@ -72,6 +72,21 @@ extension SequenceType where Generator.Element: Persistable, Generator.Element: NSCoding { + /** + Zips the receiver with metadata into an array of YapItem. + Assumes `self` and `metadata` have the same `count`. + + - parameter metadata: a sequence of optional metadatas. + - returns: an array where Persistables and Metadata with corresponding indexes in `self` and `metadata` are joined in a `YapItem` + */ + public func yapItems< + Metadatas, Metadata where + Metadata: NSCoding, + Metadatas: SequenceType, + Metadatas.Generator.Element == Optional>(with metadata: Metadatas) -> [YapItem] { + return zip(self, metadata).map { YapItem($0, $1) } + } + /** Write the items using an existing transaction. @@ -81,8 +96,8 @@ extension SequenceType where public func writeWithMetadata< WriteTransaction, Metadata where WriteTransaction: WriteTransactionType, - Metadata: NSCoding>(transaction: WriteTransaction, metadata: [Metadata?]) -> [(Generator.Element, Metadata?)] { - let items = zipToWrite(self, metadata) + Metadata: NSCoding>(transaction: WriteTransaction, metadata: [Metadata?]) -> [YapItem] { + let items = yapItems(with: metadata) return transaction.writeWithMetadata(items) } @@ -95,8 +110,8 @@ extension SequenceType where public func writeWithMetadata< Connection, Metadata where Connection: ConnectionType, - Metadata: NSCoding>(connection: Connection, metadata: [Metadata?]) -> [(Generator.Element, Metadata?)] { - let items = zipToWrite(self, metadata) + Metadata: NSCoding>(connection: Connection, metadata: [Metadata?]) -> [YapItem] { + let items = yapItems(with: metadata) return connection.writeWithMetadata(items) } @@ -109,8 +124,8 @@ extension SequenceType where public func asyncWriteWithMetadata< Connection, Metadata where Connection: ConnectionType, - Metadata: NSCoding>(connection: Connection, metadata: [Metadata?], queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ([(Generator.Element, Metadata?)] -> Void)? = .None) { - let items = zipToWrite(self, metadata) + Metadata: NSCoding>(connection: Connection, metadata: [Metadata?], queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ([YapItem] -> Void)? = .None) { + let items = yapItems(with: metadata) return connection.asyncWriteWithMetadata(items, queue: queue, completion: completion) } @@ -124,7 +139,7 @@ extension SequenceType where Connection, Metadata where Connection: ConnectionType, Metadata: NSCoding>(connection: Connection, metadata: [Metadata?]) -> NSOperation { - let items = zipToWrite(self, metadata) + let items = yapItems(with: metadata) return NSBlockOperation { connection.writeWithMetadata(items) } } } @@ -136,15 +151,15 @@ extension Readable where ItemType: NSCoding, ItemType: Persistable { - func withMetadataInTransaction(transaction: Database.Connection.ReadTransaction, atIndex index: YapDB.Index) -> (ItemType, Metadata?)? { + func withMetadataInTransaction(transaction: Database.Connection.ReadTransaction, atIndex index: YapDB.Index) -> YapItem? { return transaction.readWithMetadataAtIndex(index) } - func withMetadataInTransactionAtIndex(transaction: Database.Connection.ReadTransaction) -> YapDB.Index -> (ItemType, Metadata?)? { + func withMetadataInTransactionAtIndex(transaction: Database.Connection.ReadTransaction) -> YapDB.Index -> YapItem? { return { self.withMetadataInTransaction(transaction, atIndex: $0) } } - func withMetadataAtIndexInTransaction(index: YapDB.Index) -> Database.Connection.ReadTransaction -> (ItemType, Metadata?)? { + func withMetadataAtIndexInTransaction(index: YapDB.Index) -> Database.Connection.ReadTransaction -> YapItem? { return { self.withMetadataInTransaction($0, atIndex: index) } } @@ -152,23 +167,23 @@ extension Readable where Indexes, Metadata where Indexes: SequenceType, Indexes.Generator.Element == YapDB.Index, - Metadata: NSCoding>(indexes: Indexes) -> Database.Connection.ReadTransaction -> [(ItemType, Metadata?)] { + Metadata: NSCoding>(indexes: Indexes) -> Database.Connection.ReadTransaction -> [YapItem] { return { indexes.flatMap(self.withMetadataInTransactionAtIndex($0)) } } - func withMetadataInTransaction(transaction: Database.Connection.ReadTransaction, byKey key: String) -> (ItemType, Metadata?)? { + func withMetadataInTransaction(transaction: Database.Connection.ReadTransaction, byKey key: String) -> YapItem? { return withMetadataInTransaction(transaction, atIndex: ItemType.indexWithKey(key)) } - func withMetadataInTransactionByKey(transaction: Database.Connection.ReadTransaction) -> String -> (ItemType, Metadata?)? { + func withMetadataInTransactionByKey(transaction: Database.Connection.ReadTransaction) -> String -> YapItem? { return { self.withMetadataInTransaction(transaction, byKey: $0) } } - func withMetadataByKeyInTransaction(key: String) -> Database.Connection.ReadTransaction -> (ItemType, Metadata?)? { + func withMetadataByKeyInTransaction(key: String) -> Database.Connection.ReadTransaction -> YapItem? { return { self.withMetadataInTransaction($0, byKey: key) } } - func withMetadataByKeysInTransaction(keys: [String]? = .None) -> Database.Connection.ReadTransaction -> [(ItemType, Metadata?)] { + func withMetadataByKeysInTransaction(keys: [String]? = .None) -> Database.Connection.ReadTransaction -> [YapItem] { return { transaction in let keys = keys ?? transaction.keysInCollection(ItemType.collection) return keys.flatMap(self.withMetadataInTransactionByKey(transaction)) @@ -181,7 +196,7 @@ extension Readable where - parameter index: a YapDB.Index - returns: an optional `ItemType` */ - public func withMetadataAtIndex(index: YapDB.Index) -> (ItemType, Metadata?)? { + public func withMetadataAtIndex(index: YapDB.Index) -> YapItem? { return sync(withMetadataAtIndexInTransaction(index)) } @@ -195,7 +210,7 @@ extension Readable where Indexes, Metadata where Indexes: SequenceType, Indexes.Generator.Element == YapDB.Index, - Metadata: NSCoding>(indexes: Indexes) -> [(ItemType, Metadata?)] { + Metadata: NSCoding>(indexes: Indexes) -> [YapItem] { return sync(withMetadataAtIndexesInTransaction(indexes)) } @@ -205,7 +220,7 @@ extension Readable where - parameter key: a String - returns: an optional `ItemType` */ - public func withMetadataByKey(key: String) -> (ItemType, Metadata?)? { + public func withMetadataByKey(key: String) -> YapItem? { return sync(withMetadataByKeyInTransaction(key)) } @@ -219,7 +234,7 @@ extension Readable where Keys, Metadata where Keys: SequenceType, Keys.Generator.Element == String, - Metadata: NSCoding>(keys: Keys) -> [(ItemType, Metadata?)] { + Metadata: NSCoding>(keys: Keys) -> [YapItem] { return sync(withMetadataByKeysInTransaction(Array(keys))) } @@ -228,7 +243,7 @@ extension Readable where - returns: an array of `ItemType` */ - public func withMetadataAll() -> [(ItemType, Metadata?)] { + public func withMetadataAll() -> [YapItem] { return sync(withMetadataByKeysInTransaction()) } @@ -238,11 +253,11 @@ extension Readable where - parameter keys: a SequenceType of String values - returns: a tuple of type `([ItemType], [String])` */ - public func withMetadataFilterExisting(keys: [String]) -> (existing: [(ItemType, Metadata?)], missing: [String]) { - let existingInTransaction: Database.Connection.ReadTransaction -> [(ItemType, Metadata?)] = withMetadataByKeysInTransaction(keys) - return sync { transaction -> ([(ItemType, Metadata?)], [String]) in + public func withMetadataFilterExisting(keys: [String]) -> (existing: [YapItem], missing: [String]) { + let existingInTransaction: Database.Connection.ReadTransaction -> [YapItem] = withMetadataByKeysInTransaction(keys) + return sync { transaction -> ([YapItem], [String]) in let existing = existingInTransaction(transaction) - let existingKeys = existing.map {keyForPersistable($0.0)} + let existingKeys = existing.map {keyForPersistable($0.value)} let missingKeys = keys.filter { !existingKeys.contains($0) } return (existing, missingKeys) } diff --git a/YapDatabaseExtensions/Shared/Persistable/Persistable_ObjectWithValueMetadata.swift b/YapDatabaseExtensions/Shared/Persistable/Persistable_ObjectWithValueMetadata.swift index 06a9570..3ca949f 100644 --- a/YapDatabaseExtensions/Shared/Persistable/Persistable_ObjectWithValueMetadata.swift +++ b/YapDatabaseExtensions/Shared/Persistable/Persistable_ObjectWithValueMetadata.swift @@ -27,8 +27,8 @@ extension Persistable where WriteTransaction: WriteTransactionType, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(transaction: WriteTransaction, metadata: Metadata?) -> (Self, Metadata?) { - return transaction.writeWithMetadata((self, metadata)) + Metadata.Coder.ValueType == Metadata>(transaction: WriteTransaction, metadata: Metadata?) -> YapItem { + return transaction.writeWithMetadata(YapItem(self, metadata)) } /** @@ -42,8 +42,8 @@ extension Persistable where Connection: ConnectionType, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(connection: Connection, metadata: Metadata?) -> (Self, Metadata?) { - return connection.writeWithMetadata((self, metadata)) + Metadata.Coder.ValueType == Metadata>(connection: Connection, metadata: Metadata?) -> YapItem { + return connection.writeWithMetadata(YapItem(self, metadata)) } /** @@ -57,8 +57,8 @@ extension Persistable where Connection: ConnectionType, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(connection: Connection, metadata: Metadata?, queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ((Self, Metadata?) -> Void)? = .None) { - return connection.asyncWriteWithMetadata((self, metadata), queue: queue, completion: completion) + Metadata.Coder.ValueType == Metadata>(connection: Connection, metadata: Metadata?, queue: dispatch_queue_t = dispatch_get_main_queue(), completion: (YapItem -> Void)? = .None) { + return connection.asyncWriteWithMetadata(YapItem(self, metadata), queue: queue, completion: completion) } /** @@ -73,7 +73,7 @@ extension Persistable where Metadata: ValueCoding, Metadata.Coder: NSCoding, Metadata.Coder.ValueType == Metadata>(connection: Connection, metadata: Metadata?) -> NSOperation { - return NSBlockOperation { connection.writeWithMetadata((self, metadata)) } + return NSBlockOperation { connection.writeWithMetadata(YapItem(self, metadata)) } } } @@ -81,6 +81,23 @@ extension SequenceType where Generator.Element: Persistable, Generator.Element: NSCoding { + /** + Zips the receiver with metadata into an array of YapItem. + Assumes `self` and `metadata` have the same `count`. + + - parameter metadata: a sequence of optional metadatas. + - returns: an array where Persistables and Metadata with corresponding indexes in `self` and `metadata` are joined in a `YapItem` + */ + public func yapItems< + Metadatas, Metadata where + Metadata: ValueCoding, + Metadata.Coder: NSCoding, + Metadata.Coder.ValueType == Metadata, + Metadatas: SequenceType, + Metadatas.Generator.Element == Optional>(with metadata: Metadatas) -> [YapItem] { + return zip(self, metadata).map { YapItem($0, $1) } + } + /** Write the items using an existing transaction. @@ -92,8 +109,8 @@ extension SequenceType where WriteTransaction: WriteTransactionType, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(transaction: WriteTransaction, metadata: [Metadata?]) -> [(Generator.Element, Metadata?)] { - let items = zipToWrite(self, metadata) + Metadata.Coder.ValueType == Metadata>(transaction: WriteTransaction, metadata: [Metadata?]) -> [YapItem] { + let items = yapItems(with: metadata) return transaction.writeWithMetadata(items) } @@ -108,8 +125,8 @@ extension SequenceType where Connection: ConnectionType, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(connection: Connection, metadata: [Metadata?]) -> [(Generator.Element, Metadata?)] { - let items = zipToWrite(self, metadata) + Metadata.Coder.ValueType == Metadata>(connection: Connection, metadata: [Metadata?]) -> [YapItem] { + let items = yapItems(with: metadata) return connection.writeWithMetadata(items) } @@ -124,8 +141,8 @@ extension SequenceType where Connection: ConnectionType, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(connection: Connection, metadata: [Metadata?], queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ([(Generator.Element, Metadata?)] -> Void)? = .None) { - let items = zipToWrite(self, metadata) + Metadata.Coder.ValueType == Metadata>(connection: Connection, metadata: [Metadata?], queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ([YapItem] -> Void)? = .None) { + let items = yapItems(with: metadata) return connection.asyncWriteWithMetadata(items, queue: queue, completion: completion) } @@ -141,7 +158,7 @@ extension SequenceType where Metadata: ValueCoding, Metadata.Coder: NSCoding, Metadata.Coder.ValueType == Metadata>(connection: Connection, metadata: [Metadata?]) -> NSOperation { - let items = zipToWrite(self, metadata) + let items = yapItems(with: metadata) return NSBlockOperation { connection.writeWithMetadata(items) } } } @@ -156,7 +173,7 @@ extension Readable where Metadata where Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(transaction: Database.Connection.ReadTransaction, atIndex index: YapDB.Index) -> (ItemType, Metadata?)? { + Metadata.Coder.ValueType == Metadata>(transaction: Database.Connection.ReadTransaction, atIndex index: YapDB.Index) -> YapItem? { return transaction.readWithMetadataAtIndex(index) } @@ -164,7 +181,7 @@ extension Readable where Metadata where Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(transaction: Database.Connection.ReadTransaction) -> YapDB.Index -> (ItemType, Metadata?)? { + Metadata.Coder.ValueType == Metadata>(transaction: Database.Connection.ReadTransaction) -> YapDB.Index -> YapItem? { return { self.withMetadataInTransaction(transaction, atIndex: $0) } } @@ -172,7 +189,7 @@ extension Readable where Metadata where Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(index: YapDB.Index) -> Database.Connection.ReadTransaction -> (ItemType, Metadata?)? { + Metadata.Coder.ValueType == Metadata>(index: YapDB.Index) -> Database.Connection.ReadTransaction -> YapItem? { return { self.withMetadataInTransaction($0, atIndex: index) } } @@ -182,7 +199,7 @@ extension Readable where Indexes.Generator.Element == YapDB.Index, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(indexes: Indexes) -> Database.Connection.ReadTransaction -> [(ItemType, Metadata?)] { + Metadata.Coder.ValueType == Metadata>(indexes: Indexes) -> Database.Connection.ReadTransaction -> [YapItem] { return { indexes.flatMap(self.withMetadataInTransactionAtIndex($0)) } } @@ -190,7 +207,7 @@ extension Readable where Metadata where Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(transaction: Database.Connection.ReadTransaction, byKey key: String) -> (ItemType, Metadata?)? { + Metadata.Coder.ValueType == Metadata>(transaction: Database.Connection.ReadTransaction, byKey key: String) -> YapItem? { return withMetadataInTransaction(transaction, atIndex: ItemType.indexWithKey(key)) } @@ -198,7 +215,7 @@ extension Readable where Metadata where Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(transaction: Database.Connection.ReadTransaction) -> String -> (ItemType, Metadata?)? { + Metadata.Coder.ValueType == Metadata>(transaction: Database.Connection.ReadTransaction) -> String -> YapItem? { return { self.withMetadataInTransaction(transaction, byKey: $0) } } @@ -206,7 +223,7 @@ extension Readable where Metadata where Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(key: String) -> Database.Connection.ReadTransaction -> (ItemType, Metadata?)? { + Metadata.Coder.ValueType == Metadata>(key: String) -> Database.Connection.ReadTransaction -> YapItem? { return { self.withMetadataInTransaction($0, byKey: key) } } @@ -214,7 +231,7 @@ extension Readable where Metadata where Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(keys: [String]? = .None) -> Database.Connection.ReadTransaction -> [(ItemType, Metadata?)] { + Metadata.Coder.ValueType == Metadata>(keys: [String]? = .None) -> Database.Connection.ReadTransaction -> [YapItem] { return { transaction in let keys = keys ?? transaction.keysInCollection(ItemType.collection) return keys.flatMap(self.withMetadataInTransactionByKey(transaction)) @@ -231,7 +248,7 @@ extension Readable where Metadata where Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(index: YapDB.Index) -> (ItemType, Metadata?)? { + Metadata.Coder.ValueType == Metadata>(index: YapDB.Index) -> YapItem? { return sync(withMetadataAtIndexInTransaction(index)) } @@ -247,7 +264,7 @@ extension Readable where Indexes.Generator.Element == YapDB.Index, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(indexes: Indexes) -> [(ItemType, Metadata?)] { + Metadata.Coder.ValueType == Metadata>(indexes: Indexes) -> [YapItem] { return sync(withMetadataAtIndexesInTransaction(indexes)) } @@ -261,7 +278,7 @@ extension Readable where Metadata where Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(key: String) -> (ItemType, Metadata?)? { + Metadata.Coder.ValueType == Metadata>(key: String) -> YapItem? { return sync(withMetadataByKeyInTransaction(key)) } @@ -277,7 +294,7 @@ extension Readable where Keys.Generator.Element == String, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(keys: Keys) -> [(ItemType, Metadata?)] { + Metadata.Coder.ValueType == Metadata>(keys: Keys) -> [YapItem] { return sync(withMetadataByKeysInTransaction(Array(keys))) } @@ -290,7 +307,7 @@ extension Readable where Metadata where Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>() -> [(ItemType, Metadata?)] { + Metadata.Coder.ValueType == Metadata>() -> [YapItem] { return sync(withMetadataByKeysInTransaction()) } @@ -304,11 +321,11 @@ extension Readable where Metadata where Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(keys: [String]) -> (existing: [(ItemType, Metadata?)], missing: [String]) { - let existingInTransaction: Database.Connection.ReadTransaction -> [(ItemType, Metadata?)] = withMetadataByKeysInTransaction(keys) - return sync { transaction -> ([(ItemType, Metadata?)], [String]) in + Metadata.Coder.ValueType == Metadata>(keys: [String]) -> (existing: [YapItem], missing: [String]) { + let existingInTransaction: Database.Connection.ReadTransaction -> [YapItem] = withMetadataByKeysInTransaction(keys) + return sync { transaction -> ([YapItem], [String]) in let existing = existingInTransaction(transaction) - let existingKeys = existing.map {keyForPersistable($0.0)} + let existingKeys = existing.map {keyForPersistable($0.value)} let missingKeys = keys.filter { !existingKeys.contains($0) } return (existing, missingKeys) } diff --git a/YapDatabaseExtensions/Shared/Persistable/Persistable_ValueWithObjectMetadata.swift b/YapDatabaseExtensions/Shared/Persistable/Persistable_ValueWithObjectMetadata.swift index 2a200dd..bbabc82 100644 --- a/YapDatabaseExtensions/Shared/Persistable/Persistable_ValueWithObjectMetadata.swift +++ b/YapDatabaseExtensions/Shared/Persistable/Persistable_ValueWithObjectMetadata.swift @@ -27,8 +27,8 @@ extension Persistable where public func writeWithMetadata< WriteTransaction, Metadata where WriteTransaction: WriteTransactionType, - Metadata: NSCoding>(transaction: WriteTransaction, metadata: Metadata?) -> (Self, Metadata?) { - return transaction.writeWithMetadata((self, metadata)) + Metadata: NSCoding>(transaction: WriteTransaction, metadata: Metadata?) -> YapItem { + return transaction.writeWithMetadata(YapItem(self, metadata)) } /** @@ -40,8 +40,8 @@ extension Persistable where public func writeWithMetadata< Connection, Metadata where Connection: ConnectionType, - Metadata: NSCoding>(connection: Connection, metadata: Metadata?) -> (Self, Metadata?) { - return connection.writeWithMetadata((self, metadata)) + Metadata: NSCoding>(connection: Connection, metadata: Metadata?) -> YapItem { + return connection.writeWithMetadata(YapItem(self, metadata)) } /** @@ -53,8 +53,8 @@ extension Persistable where public func asyncWriteWithMetadata< Connection, Metadata where Connection: ConnectionType, - Metadata: NSCoding>(connection: Connection, metadata: Metadata?, queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ((Self, Metadata?) -> Void)? = .None) { - return connection.asyncWriteWithMetadata((self, metadata), queue: queue, completion: completion) + Metadata: NSCoding>(connection: Connection, metadata: Metadata?, queue: dispatch_queue_t = dispatch_get_main_queue(), completion: (YapItem -> Void)? = .None) { + return connection.asyncWriteWithMetadata(YapItem(self, metadata), queue: queue, completion: completion) } /** @@ -67,7 +67,7 @@ extension Persistable where Connection, Metadata where Connection: ConnectionType, Metadata: NSCoding>(connection: Connection, metadata: Metadata?) -> NSOperation { - return NSBlockOperation { connection.writeWithMetadata((self, metadata)) } + return NSBlockOperation { connection.writeWithMetadata(YapItem(self, metadata)) } } } @@ -77,6 +77,21 @@ extension SequenceType where Generator.Element.Coder: NSCoding, Generator.Element.Coder.ValueType == Generator.Element { + /** + Zips the receiver with metadata into an array of YapItem. + Assumes `self` and `metadata` have the same `count`. + + - parameter metadata: a sequence of optional metadatas. + - returns: an array where Persistables and Metadata with corresponding indexes in `self` and `metadata` are joined in a `YapItem` + */ + public func yapItems< + Metadatas, Metadata where + Metadata: NSCoding, + Metadatas: SequenceType, + Metadatas.Generator.Element == Optional>(with metadata: Metadatas) -> [YapItem] { + return zip(self, metadata).map { YapItem($0, $1) } + } + /** Write the items using an existing transaction. @@ -86,8 +101,8 @@ extension SequenceType where public func writeWithMetadata< WriteTransaction, Metadata where WriteTransaction: WriteTransactionType, - Metadata: NSCoding>(transaction: WriteTransaction, metadata: [Metadata?]) -> [(Generator.Element, Metadata?)] { - let items = zipToWrite(self, metadata) + Metadata: NSCoding>(transaction: WriteTransaction, metadata: [Metadata?]) -> [YapItem] { + let items = yapItems(with: metadata) return transaction.writeWithMetadata(items) } @@ -100,8 +115,8 @@ extension SequenceType where public func writeWithMetadata< Connection, Metadata where Connection: ConnectionType, - Metadata: NSCoding>(connection: Connection, metadata: [Metadata?]) -> [(Generator.Element, Metadata?)] { - let items = zipToWrite(self, metadata) + Metadata: NSCoding>(connection: Connection, metadata: [Metadata?]) -> [YapItem] { + let items = yapItems(with: metadata) return connection.writeWithMetadata(items) } @@ -114,8 +129,8 @@ extension SequenceType where public func asyncWriteWithMetadata< Connection, Metadata where Connection: ConnectionType, - Metadata: NSCoding>(connection: Connection, metadata: [Metadata?], queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ([(Generator.Element, Metadata?)] -> Void)? = .None) { - let items = zipToWrite(self, metadata) + Metadata: NSCoding>(connection: Connection, metadata: [Metadata?], queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ([YapItem] -> Void)? = .None) { + let items = yapItems(with: metadata) return connection.asyncWriteWithMetadata(items, queue: queue, completion: completion) } @@ -129,7 +144,7 @@ extension SequenceType where Connection, Metadata where Connection: ConnectionType, Metadata: NSCoding>(connection: Connection, metadata: [Metadata?]) -> NSOperation { - let items = zipToWrite(self, metadata) + let items = yapItems(with: metadata) return NSBlockOperation { connection.writeWithMetadata(items) } } } @@ -143,15 +158,15 @@ extension Readable where ItemType.Coder: NSCoding, ItemType.Coder.ValueType == ItemType { - func withMetadataInTransaction(transaction: Database.Connection.ReadTransaction, atIndex index: YapDB.Index) -> (ItemType, Metadata?)? { + func withMetadataInTransaction(transaction: Database.Connection.ReadTransaction, atIndex index: YapDB.Index) -> YapItem? { return transaction.readWithMetadataAtIndex(index) } - func withMetadataInTransactionAtIndex(transaction: Database.Connection.ReadTransaction) -> YapDB.Index -> (ItemType, Metadata?)? { + func withMetadataInTransactionAtIndex(transaction: Database.Connection.ReadTransaction) -> YapDB.Index -> YapItem? { return { self.withMetadataInTransaction(transaction, atIndex: $0) } } - func withMetadataAtIndexInTransaction(index: YapDB.Index) -> Database.Connection.ReadTransaction -> (ItemType, Metadata?)? { + func withMetadataAtIndexInTransaction(index: YapDB.Index) -> Database.Connection.ReadTransaction -> YapItem? { return { self.withMetadataInTransaction($0, atIndex: index) } } @@ -159,23 +174,23 @@ extension Readable where Indexes, Metadata where Indexes: SequenceType, Indexes.Generator.Element == YapDB.Index, - Metadata: NSCoding>(indexes: Indexes) -> Database.Connection.ReadTransaction -> [(ItemType, Metadata?)] { + Metadata: NSCoding>(indexes: Indexes) -> Database.Connection.ReadTransaction -> [YapItem] { return { indexes.flatMap(self.withMetadataInTransactionAtIndex($0)) } } - func withMetadataInTransaction(transaction: Database.Connection.ReadTransaction, byKey key: String) -> (ItemType, Metadata?)? { + func withMetadataInTransaction(transaction: Database.Connection.ReadTransaction, byKey key: String) -> YapItem? { return withMetadataInTransaction(transaction, atIndex: ItemType.indexWithKey(key)) } - func withMetadataInTransactionByKey(transaction: Database.Connection.ReadTransaction) -> String -> (ItemType, Metadata?)? { + func withMetadataInTransactionByKey(transaction: Database.Connection.ReadTransaction) -> String -> YapItem? { return { self.withMetadataInTransaction(transaction, byKey: $0) } } - func withMetadataByKeyInTransaction(key: String) -> Database.Connection.ReadTransaction -> (ItemType, Metadata?)? { + func withMetadataByKeyInTransaction(key: String) -> Database.Connection.ReadTransaction -> YapItem? { return { self.withMetadataInTransaction($0, byKey: key) } } - func withMetadataByKeysInTransaction(keys: [String]? = .None) -> Database.Connection.ReadTransaction -> [(ItemType, Metadata?)] { + func withMetadataByKeysInTransaction(keys: [String]? = .None) -> Database.Connection.ReadTransaction -> [YapItem] { return { transaction in let keys = keys ?? transaction.keysInCollection(ItemType.collection) return keys.flatMap(self.withMetadataInTransactionByKey(transaction)) @@ -188,7 +203,7 @@ extension Readable where - parameter index: a YapDB.Index - returns: an optional `ItemType` */ - public func withMetadataAtIndex(index: YapDB.Index) -> (ItemType, Metadata?)? { + public func withMetadataAtIndex(index: YapDB.Index) -> YapItem? { return sync(withMetadataAtIndexInTransaction(index)) } @@ -202,7 +217,7 @@ extension Readable where Indexes, Metadata where Indexes: SequenceType, Indexes.Generator.Element == YapDB.Index, - Metadata: NSCoding>(indexes: Indexes) -> [(ItemType, Metadata?)] { + Metadata: NSCoding>(indexes: Indexes) -> [YapItem] { return sync(withMetadataAtIndexesInTransaction(indexes)) } @@ -212,7 +227,7 @@ extension Readable where - parameter key: a String - returns: an optional `ItemType` */ - public func withMetadataByKey(key: String) -> (ItemType, Metadata?)? { + public func withMetadataByKey(key: String) -> YapItem? { return sync(withMetadataByKeyInTransaction(key)) } @@ -226,7 +241,7 @@ extension Readable where Keys, Metadata where Keys: SequenceType, Keys.Generator.Element == String, - Metadata: NSCoding>(keys: Keys) -> [(ItemType, Metadata?)] { + Metadata: NSCoding>(keys: Keys) -> [YapItem] { return sync(withMetadataByKeysInTransaction(Array(keys))) } @@ -235,7 +250,7 @@ extension Readable where - returns: an array of `ItemType` */ - public func withMetadataAll() -> [(ItemType, Metadata?)] { + public func withMetadataAll() -> [YapItem] { return sync(withMetadataByKeysInTransaction()) } @@ -245,11 +260,11 @@ extension Readable where - parameter keys: a SequenceType of String values - returns: a tuple of type `([ItemType], [String])` */ - public func withMetadataFilterExisting(keys: [String]) -> (existing: [(ItemType, Metadata?)], missing: [String]) { - let existingInTransaction: Database.Connection.ReadTransaction -> [(ItemType, Metadata?)] = withMetadataByKeysInTransaction(keys) - return sync { transaction -> ([(ItemType, Metadata?)], [String]) in + public func withMetadataFilterExisting(keys: [String]) -> (existing: [YapItem], missing: [String]) { + let existingInTransaction: Database.Connection.ReadTransaction -> [YapItem] = withMetadataByKeysInTransaction(keys) + return sync { transaction -> ([YapItem], [String]) in let existing = existingInTransaction(transaction) - let existingKeys = existing.map {keyForPersistable($0.0)} + let existingKeys = existing.map {keyForPersistable($0.value)} let missingKeys = keys.filter { !existingKeys.contains($0) } return (existing, missingKeys) } diff --git a/YapDatabaseExtensions/Shared/Persistable/Persistable_ValueWithValueMetadata.swift b/YapDatabaseExtensions/Shared/Persistable/Persistable_ValueWithValueMetadata.swift index 5dea161..c7c4912 100644 --- a/YapDatabaseExtensions/Shared/Persistable/Persistable_ValueWithValueMetadata.swift +++ b/YapDatabaseExtensions/Shared/Persistable/Persistable_ValueWithValueMetadata.swift @@ -29,8 +29,8 @@ extension Persistable where WriteTransaction: WriteTransactionType, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(transaction: WriteTransaction, metadata: Metadata?) -> (Self, Metadata?) { - return transaction.writeWithMetadata((self, metadata)) + Metadata.Coder.ValueType == Metadata>(transaction: WriteTransaction, metadata: Metadata?) -> YapItem { + return transaction.writeWithMetadata(YapItem(self, metadata)) } /** @@ -44,8 +44,8 @@ extension Persistable where Connection: ConnectionType, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(connection: Connection, metadata: Metadata?) -> (Self, Metadata?) { - return connection.writeWithMetadata((self, metadata)) + Metadata.Coder.ValueType == Metadata>(connection: Connection, metadata: Metadata?) -> YapItem { + return connection.writeWithMetadata(YapItem(self, metadata)) } /** @@ -59,8 +59,8 @@ extension Persistable where Connection: ConnectionType, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(connection: Connection, metadata: Metadata?, queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ((Self, Metadata?) -> Void)? = .None) { - return connection.asyncWriteWithMetadata((self, metadata), queue: queue, completion: completion) + Metadata.Coder.ValueType == Metadata>(connection: Connection, metadata: Metadata?, queue: dispatch_queue_t = dispatch_get_main_queue(), completion: (YapItem -> Void)? = .None) { + return connection.asyncWriteWithMetadata(YapItem(self, metadata), queue: queue, completion: completion) } /** @@ -75,7 +75,7 @@ extension Persistable where Metadata: ValueCoding, Metadata.Coder: NSCoding, Metadata.Coder.ValueType == Metadata>(connection: Connection, metadata: Metadata?) -> NSOperation { - return NSBlockOperation { connection.writeWithMetadata((self, metadata)) } + return NSBlockOperation { connection.writeWithMetadata(YapItem(self, metadata)) } } } @@ -85,6 +85,23 @@ extension SequenceType where Generator.Element.Coder: NSCoding, Generator.Element.Coder.ValueType == Generator.Element { + /** + Zips the receiver with metadata into an array of YapItem. + Assumes `self` and `metadata` have the same `count`. + + - parameter metadata: a sequence of optional metadatas. + - returns: an array where Persistables and Metadata with corresponding indexes in `self` and `metadata` are joined in a `YapItem` + */ + public func yapItems< + Metadatas, Metadata where + Metadata: ValueCoding, + Metadata.Coder: NSCoding, + Metadata.Coder.ValueType == Metadata, + Metadatas: SequenceType, + Metadatas.Generator.Element == Optional>(with metadata: Metadatas) -> [YapItem] { + return zip(self, metadata).map { YapItem($0, $1) } + } + /** Write the items using an existing transaction. @@ -96,8 +113,8 @@ extension SequenceType where WriteTransaction: WriteTransactionType, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(transaction: WriteTransaction, metadata: [Metadata?]) -> [(Generator.Element, Metadata?)] { - let items = zipToWrite(self, metadata) + Metadata.Coder.ValueType == Metadata>(transaction: WriteTransaction, metadata: [Metadata?]) -> [YapItem] { + let items = yapItems(with: metadata) return transaction.writeWithMetadata(items) } @@ -112,8 +129,8 @@ extension SequenceType where Connection: ConnectionType, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(connection: Connection, metadata: [Metadata?]) -> [(Generator.Element, Metadata?)] { - let items = zipToWrite(self, metadata) + Metadata.Coder.ValueType == Metadata>(connection: Connection, metadata: [Metadata?]) -> [YapItem] { + let items = yapItems(with: metadata) return connection.writeWithMetadata(items) } @@ -128,8 +145,8 @@ extension SequenceType where Connection: ConnectionType, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(connection: Connection, metadata: [Metadata?], queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ([(Generator.Element, Metadata?)] -> Void)? = .None) { - let items = zipToWrite(self, metadata) + Metadata.Coder.ValueType == Metadata>(connection: Connection, metadata: [Metadata?], queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ([YapItem] -> Void)? = .None) { + let items = yapItems(with: metadata) return connection.asyncWriteWithMetadata(items, queue: queue, completion: completion) } @@ -145,7 +162,7 @@ extension SequenceType where Metadata: ValueCoding, Metadata.Coder: NSCoding, Metadata.Coder.ValueType == Metadata>(connection: Connection, metadata: [Metadata?]) -> NSOperation { - let items = zipToWrite(self, metadata) + let items = yapItems(with: metadata) return NSBlockOperation { connection.writeWithMetadata(items) } } } @@ -162,7 +179,7 @@ extension Readable where Metadata where Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(transaction: Database.Connection.ReadTransaction, atIndex index: YapDB.Index) -> (ItemType, Metadata?)? { + Metadata.Coder.ValueType == Metadata>(transaction: Database.Connection.ReadTransaction, atIndex index: YapDB.Index) -> YapItem? { return transaction.readWithMetadataAtIndex(index) } @@ -170,7 +187,7 @@ extension Readable where Metadata where Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(transaction: Database.Connection.ReadTransaction) -> YapDB.Index -> (ItemType, Metadata?)? { + Metadata.Coder.ValueType == Metadata>(transaction: Database.Connection.ReadTransaction) -> YapDB.Index -> YapItem? { return { self.withMetadataInTransaction(transaction, atIndex: $0) } } @@ -178,7 +195,7 @@ extension Readable where Metadata where Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(index: YapDB.Index) -> Database.Connection.ReadTransaction -> (ItemType, Metadata?)? { + Metadata.Coder.ValueType == Metadata>(index: YapDB.Index) -> Database.Connection.ReadTransaction -> YapItem? { return { self.withMetadataInTransaction($0, atIndex: index) } } @@ -188,7 +205,7 @@ extension Readable where Indexes.Generator.Element == YapDB.Index, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(indexes: Indexes) -> Database.Connection.ReadTransaction -> [(ItemType, Metadata?)] { + Metadata.Coder.ValueType == Metadata>(indexes: Indexes) -> Database.Connection.ReadTransaction -> [YapItem] { return { indexes.flatMap(self.withMetadataInTransactionAtIndex($0)) } } @@ -196,7 +213,7 @@ extension Readable where Metadata where Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(transaction: Database.Connection.ReadTransaction, byKey key: String) -> (ItemType, Metadata?)? { + Metadata.Coder.ValueType == Metadata>(transaction: Database.Connection.ReadTransaction, byKey key: String) -> YapItem? { return withMetadataInTransaction(transaction, atIndex: ItemType.indexWithKey(key)) } @@ -204,7 +221,7 @@ extension Readable where Metadata where Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(transaction: Database.Connection.ReadTransaction) -> String -> (ItemType, Metadata?)? { + Metadata.Coder.ValueType == Metadata>(transaction: Database.Connection.ReadTransaction) -> String -> YapItem? { return { self.withMetadataInTransaction(transaction, byKey: $0) } } @@ -212,7 +229,7 @@ extension Readable where Metadata where Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(key: String) -> Database.Connection.ReadTransaction -> (ItemType, Metadata?)? { + Metadata.Coder.ValueType == Metadata>(key: String) -> Database.Connection.ReadTransaction -> YapItem? { return { self.withMetadataInTransaction($0, byKey: key) } } @@ -220,7 +237,7 @@ extension Readable where Metadata where Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(keys: [String]? = .None) -> Database.Connection.ReadTransaction -> [(ItemType, Metadata?)] { + Metadata.Coder.ValueType == Metadata>(keys: [String]? = .None) -> Database.Connection.ReadTransaction -> [YapItem] { return { transaction in let keys = keys ?? transaction.keysInCollection(ItemType.collection) return keys.flatMap(self.withMetadataInTransactionByKey(transaction)) @@ -237,7 +254,7 @@ extension Readable where Metadata where Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(index: YapDB.Index) -> (ItemType, Metadata?)? { + Metadata.Coder.ValueType == Metadata>(index: YapDB.Index) -> YapItem? { return sync(withMetadataAtIndexInTransaction(index)) } @@ -253,7 +270,7 @@ extension Readable where Indexes.Generator.Element == YapDB.Index, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(indexes: Indexes) -> [(ItemType, Metadata?)] { + Metadata.Coder.ValueType == Metadata>(indexes: Indexes) -> [YapItem] { return sync(withMetadataAtIndexesInTransaction(indexes)) } @@ -267,7 +284,7 @@ extension Readable where Metadata where Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(key: String) -> (ItemType, Metadata?)? { + Metadata.Coder.ValueType == Metadata>(key: String) -> YapItem? { return sync(withMetadataByKeyInTransaction(key)) } @@ -283,7 +300,7 @@ extension Readable where Keys.Generator.Element == String, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(keys: Keys) -> [(ItemType, Metadata?)] { + Metadata.Coder.ValueType == Metadata>(keys: Keys) -> [YapItem] { return sync(withMetadataByKeysInTransaction(Array(keys))) } @@ -296,7 +313,7 @@ extension Readable where Metadata where Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>() -> [(ItemType, Metadata?)] { + Metadata.Coder.ValueType == Metadata>() -> [YapItem] { return sync(withMetadataByKeysInTransaction()) } @@ -310,11 +327,11 @@ extension Readable where Metadata where Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(keys: [String]) -> (existing: [(ItemType, Metadata?)], missing: [String]) { - let existingInTransaction: Database.Connection.ReadTransaction -> [(ItemType, Metadata?)] = withMetadataByKeysInTransaction(keys) - return sync { transaction -> ([(ItemType, Metadata?)], [String]) in + Metadata.Coder.ValueType == Metadata>(keys: [String]) -> (existing: [YapItem], missing: [String]) { + let existingInTransaction: Database.Connection.ReadTransaction -> [YapItem] = withMetadataByKeysInTransaction(keys) + return sync { transaction -> ([YapItem], [String]) in let existing = existingInTransaction(transaction) - let existingKeys = existing.map {keyForPersistable($0.0)} + let existingKeys = existing.map {keyForPersistable($0.value)} let missingKeys = keys.filter { !existingKeys.contains($0) } return (existing, missingKeys) } diff --git a/YapDatabaseExtensions/Shared/YapDatabaseExtensions.swift b/YapDatabaseExtensions/Shared/YapDatabaseExtensions.swift index aa3a1b8..b6cf121 100644 --- a/YapDatabaseExtensions/Shared/YapDatabaseExtensions.swift +++ b/YapDatabaseExtensions/Shared/YapDatabaseExtensions.swift @@ -119,16 +119,6 @@ public struct YapDB { } } -public func zipToWrite< - Value, Values, Metadata, Metadatas where - Values: SequenceType, - Values.Generator.Element == Value, - Metadatas: SequenceType, - Metadatas.Generator.Element == Metadata - >(values: Values, _ metadatas: Metadatas) -> [(Value, Metadata)] { - return zip(values, metadatas).map { ($0, $1) } -} - extension YapDB { /** @@ -155,6 +145,30 @@ extension YapDB { } } +/** +A pairing (effectively a tuple) of a value and a metadata. +Used when values and metadatas are read or written together. +*/ +public struct YapItem { + + /// The item's value + let value: Value + + /// The item's metadata + let metadata: Metadata? + + /** + Create a new YapItem value. + + - parameter value: the value associated with a `YapDB.Index` + - parameter metadata: an optional metadata associated with a `YapDB.Index` + */ + public init(_ value: Value, _ metadata: Metadata?) { + self.value = value + self.metadata = metadata + } +} + // MARK: - Identifiable /** From 1ed4c10556c63b61f978f6a536f4355d9474fb6f Mon Sep 17 00:00:00 2001 From: Jim Roepcke Date: Thu, 15 Sep 2016 23:38:33 -0700 Subject: [PATCH 14/14] some updates to README.md --- README.md | 42 +++++++++++++++++------------------------- 1 file changed, 17 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 9292a00..59847f1 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,6 @@ public protocol Identifiable { public protocol Persistable: Identifiable { static var collection: String { get } - var metadata: MetadataType? { get set } } ``` @@ -41,26 +40,11 @@ While not a requirement of YapDatabase, for these extensions, it is required tha There is also a `YapDB.Index` struct which composes the key and collection into a single type. This is used internally for all access methods. Properties defined in an extension on `Persistable` provide access to `key` and `index`. ### Metadata -YapDatabase supports storing metadata alongside the primary object. YapDatabaseExtensions supports automatic reading and writing of metadata as an optional property of the `Persistable` type. +YapDatabase supports storing metadata alongside the primary object. YapDatabaseExtensions supports optional reading and writing of metadata alongside a `Persistable` type. -By default, all types which conform to `Persistable`, will get a `MetadataType` of `Void` which is synthesized by default. Therefore if you do not want or need a metadata type, there is nothing to do. +Your custom metadata types must conform to either `NSCoding` or `ValueCoding`. -To support a custom metadata type, just add the following to your `Persistable` type, e.g.: - -```swift -struct MyCustomValue: Persistable, ValueCoding { - typealias Coder = MyCustomValueCoder - static let collection = “MyCustomValues” - var metadata: MyCustomMetadata? = .None - let identifier: NSUUID -} -``` - -where the type (`MyCustomMetadata` in the above snippet) implements either `NSCoding` or `ValueCoding`. - -When creating a new item, set the metadata property before saving the item to the database. YapDatabaseExtensions will then save the metadata inside YapDatabase correctly. *There is no need to encode the metadata inside the primary object*. When reading objects which have a valid `MetadataType`, YapDatabaseExtensions will automatically read, decode and set the item’s metadata before returning the item. - -Note that previous metadata protocols `ObjectMetadataPersistable` and `ValueMetadataPersistable` have been deprecated in favor of `Persistable`. +In this version of YapDatabaseExtensions, metadata has been removed from `Persistable`, and all reads and writes of metadata must be done explicitly. Additionally, since the `MetadataType` is decoupled from the `Persistable` type, a single `Persistable` type can use many different types of metadata, as appropriate. When you want to read or write a value and it's metadata together, you use the "withMetadata" variants of the API, which accept and return `YapItem` values. `YapItem` is basically a slightly nicer wrapper than a Swift tuple, which can be extended, unlike anonymous tuple types. ## “Correct” Type Patterns Because the generic protocols, `ValueCoding` and `CodingType` have self-reflective properties, they must be correctly implemented for the APIs to be available. This means that the equality `ValueCoding.Coder.ValueType == Self` must be met. The APIs are all composed with this represented in their generic where clauses. This means that if your `ValueCoding` type is not the `ValueType` of its `Coder`, your code will not compile. @@ -69,10 +53,10 @@ Therefore, there are six valid `Persistable` type patterns as described in the t Item encoding | Metadata encoding | Pattern --------------|-------------------|------------------ -`NSCoding` | `Void` Metadata | Object +`NSCoding` | No Metadata | Object `NSCoding` | `NSCoding` | ObjectWithObjectMetadata `NSCoding` | `ValueCoding` | ObjectWithValueMetadata -`ValueCoding` | `Void` Metadata | Value +`ValueCoding` | No Metadata | Value `ValueCoding` | `NSCoding` | ValueWithObjectMetadata `ValueCoding` | `ValueCoding` | ValueWithValueMetadata @@ -118,7 +102,7 @@ if let item: Item? = connection.readAtIndex(index) { // etc } -if let meta: Item.MetadataType? = connection.readMetadataAtIndex(index) { +if let meta: MetadataType? = connection.readMetadataAtIndex(index) { // etc } @@ -138,7 +122,7 @@ connection.read { transaction in let c: [Item] = transaction.readAtIndexes(indexes) let d: [Item] = transaction.readByKeys(keys) let all: [Item] = transaction.readAll() - let meta: [Item.MetadataType] = transaction.readMetadataAtIndexes(indexes) + let meta: [MetadataType] = transaction.readMetadataAtIndexes(indexes) } ``` @@ -166,10 +150,17 @@ Reading items from the database is a little different. ```swift // Read using a YapDB.Index. -if let item = Item.read(transaction).byIndex(index) { +if let item = Item.read(transaction).atIndex(index) { // etc - item is correct type, no casting required. } +// Read value and metadata using a YapDB.Index. +if let item: YapItem? = Item.read(transaction).withMetadataAtIndex(index) { + // etc - item is a correct type, no casting required. + // item.value contains the value + // item.metadata contains the metadata, wrapped in an Optional +} + // Read an array of items from an array of YapDB.Index(s) let items = Item.read(transaction).atIndexes(indexes) @@ -192,7 +183,7 @@ let (items, missingKeys) = Item.read(transaction).filterExisting(someKeys) Similarly, to work directly on a `YapDatabaseConnection`, use the following: ```swift -if let item = Item.read(connection).byIndex(index) { +if let item = Item.read(connection).atIndex(index) { // etc - item is correct type, no casting required. } @@ -233,6 +224,7 @@ To start working in this repository’s `YapDatabaseExtensions.xcodeproj`, you ## Author Daniel Thorpe, [@danthorpe](https://twitter.com/danthorpe) +Jim Roepcke, [@JimRoepcke](https://twitter.com/JimRoepcke) ## License