     /**
 
     A database index value type.
-    
+
     :param: collection A String
     :param: key A String
     */

    }
}

public protocol Identifiable {
    typealias IdentifierType: Printable
    var identifier: IdentifierType { get }
}

public protocol Persistable: Identifiable {
    static var collection: String { get }
}

public protocol ObjectMetadataPersistable: Persistable {
    typealias MetadataType: NSCoding
    var metadata: MetadataType { get }
}

public protocol ValueMetadataPersistable: Persistable {
    typealias MetadataType: Saveable
    var metadata: MetadataType { get }
}

public func indexForPersistable<P: Persistable>(persistable: P) -> YapDB.Index {
    return YapDB.Index(collection: persistable.dynamicType.collection, key: "\(persistable.identifier)")
}

internal func map<S: SequenceType, T>(source: S, transform: (S.Generator.Element) -> T?) -> [T] {


extension YapDatabaseConnection {

    /**
    Synchronously reads from the database on the connection. The closure receives
    the read transaction, and the function returns the result of the closure. This
    makes it very suitable as a building block for more functional methods.
    
    The majority of the wrapped functions provided by these extensions use this as 
    their basis.

    :param: block A closure which receives YapDatabaseReadTransaction and returns T
    :returns: An instance of T
    */
    public func read<T>(block: (YapDatabaseReadTransaction) -> T) -> T {
        var result: T! = .None
        readWithBlock { result = block($0) }
        return result
    }

    /**
    Synchronously writes to the database on the connection. The closure receives
    the read write transaction, and the function returns the result of the closure.
    This makes it very suitable as a building block for more functional methods.

    The majority of the wrapped functions provided by these extensions use this as
    their basis.

    :param: block A closure which receives YapDatabaseReadWriteTransaction and returns T
    :returns: An instance of T
    */
    public func write<T>(block: (YapDatabaseReadWriteTransaction) -> T) -> T {
        var result: T! = .None
        readWriteWithBlock { result = block($0) }
        return result
    }

    /**
    Asynchronously reads from the database on the connection. The closure receives
    the read transaction, and completion block receives the result of the closure.
    This makes it very suitable as a building block for more functional methods.

    The majority of the wrapped functions provided by these extensions use this as
    their basis.

    :param: block A closure which receives YapDatabaseReadTransaction and returns T
    :param: queue A dispatch_queue_t, defaults to main queue, can be ommitted in most cases.
    :param: completion A closure which receives T and returns Void.
    */
    public func asyncRead<T>(block: (YapDatabaseReadTransaction) -> T, queue: dispatch_queue_t = dispatch_get_main_queue(), completion: (T) -> Void) {
        var result: T! = .None
        asyncReadWithBlock({ result = block($0) }, completionQueue: queue) { completion(result) }
    }

    /**
    Asynchronously writes to the database on the connection. The closure receives
    the read write transaction, and completion block receives the result of the closure.
    This makes it very suitable as a building block for more functional methods.

    The majority of the wrapped functions provided by these extensions use this as
    their basis.

    :param: block A closure which receives YapDatabaseReadWriteTransaction and returns T
    :param: queue A dispatch_queue_t, defaults to main queue, can be ommitted in most cases.
    :param: completion A closure which receives T and returns Void.
    */
    public func asyncWrite<T>(block: (YapDatabaseReadWriteTransaction) -> T, queue: dispatch_queue_t = dispatch_get_main_queue(), completion: (T) -> Void) {
        var result: T! = .None
        asyncReadWriteWithBlock({ result = block($0) }, completionQueue: queue) { completion(result) }
    }
}


