<commit_before>private let connectionQueueKey = "connectionQueueKey".UTF8String

public final class Connection {
    // MARK: Public properties

    /// Reference to parent database
    public unowned let database: Database

    /// Default value cahce size when a collection does not provide its own size
    public let defaultValueCacheSize: Int

    // MARK: Internal properties

    /// Id for tracking connections
    internal let id: Int

    /// Wrapper around an open sqlite3 connection
    internal var sqlite: SQLiteAdapter!

    /// Has the sqlite connection been closed
    internal var isClosed: Bool { return sqlite?.isClosed ?? true }

    /// Non-thread safe snapshot number
    internal private(set) var localSnapshot: UInt64

    internal let connectionQueue: Dispatch.Queue

    // MARK: Private properties

    private let databaseWriteQueue: Dispatch.Queue
    private var connectionModificationLock: OSSpinLock = OS_SPINLOCK_INIT

    private var connectionState: ConnectionState = .Inactive
    private var extensionConnections: [String: ExtensionConnection]
    private var collectionsLocalStorage: [String: TypeErasedCollectionLocalStorage]
    private var modifiedCollections: [String: TypeErasedCollection]

    // MARK: Object lifecycle

    /**
     Opens a new connection to the sqlite database.
     - note:
        - This does not have to be instantiated in a thread safe manor
     - parameter id: Unique id for the connection
     - parameter database: Reference (unowned) to the parent Turf database
     - parameter databaseWriteQueue: A common serial queue used for write transactions
     - parameter defaultValueCacheSize: The default when a collection does not provide its own cache size. Default = 50.
     - throws: SQLiteError.FailedToOpenDatabase or SQLiteError.Error(code, reason)
     */
    internal init(id: Int, database: Database, databaseWriteQueue: Dispatch.Queue, defaultValueCacheSize: Int = 50) throws {
        self.id = id
        self.database = database
        self.databaseWriteQueue = databaseWriteQueue
        self.connectionQueue = Dispatch.Queues.create(.SerialQueue, name: "turf.connection[\(id)]")
        self.localSnapshot = 0
        self.defaultValueCacheSize = defaultValueCacheSize
        self.extensionConnections = [:]
        self.collectionsLocalStorage = [:]
        self.modifiedCollections = [:]

        Dispatch.Queues.setContext(
            Dispatch.Queues.makeContext(self.connectionQueue),
            key: connectionQueueKey,
            forQueue: self.connectionQueue)

        do {
            self.sqlite = try SQLiteAdapter(sqliteDatabaseUrl: database.url)
            try self.createExtensionConnections()
        } catch {
            self.sqlite = nil
            throw error
        }
    }

    /**
     - note:
        - Thread safe
            - Will not close the sqlite connection until any read/writes have completed
     */
    deinit {
        Dispatch.synchronouslyOn(connectionQueue) {
            self.sqlite?.close()
        }
        database.removeConnection(self)
    }

    // MARK: Public methods

    /**
     Pass a new read transaction into `closure` that will be executed asynchronously on a read queue.
     - note:<commit_msg>Yay for Swift 2.2<commit_after>private let connectionQueueKey = "connectionQueueKey".UTF8String

public final class Connection {
    // MARK: Public properties

    /// Reference to parent database
    public unowned let database: Database

    /// Default value cahce size when a collection does not provide its own size
    public let defaultValueCacheSize: Int

    // MARK: Internal properties

    /// Id for tracking connections
    internal let id: Int

    /// Wrapper around an open sqlite3 connection
    internal var sqlite: SQLiteAdapter

    /// Has the sqlite connection been closed
    internal var isClosed: Bool { return sqlite.isClosed }

    /// Non-thread safe snapshot number
    internal private(set) var localSnapshot: UInt64

    internal let connectionQueue: Dispatch.Queue

    // MARK: Private properties

    private let databaseWriteQueue: Dispatch.Queue
    private var connectionModificationLock: OSSpinLock = OS_SPINLOCK_INIT

    private var connectionState: ConnectionState = .Inactive
    private var extensionConnections: [String: ExtensionConnection]
    private var collectionsLocalStorage: [String: TypeErasedCollectionLocalStorage]
    private var modifiedCollections: [String: TypeErasedCollection]

    // MARK: Object lifecycle

    /**
     Opens a new connection to the sqlite database.
     - note:
        - This does not have to be instantiated in a thread safe manor
     - parameter id: Unique id for the connection
     - parameter database: Reference (unowned) to the parent Turf database
     - parameter databaseWriteQueue: A common serial queue used for write transactions
     - parameter defaultValueCacheSize: The default when a collection does not provide its own cache size. Default = 50.
     - throws: SQLiteError.FailedToOpenDatabase or SQLiteError.Error(code, reason)
     */
    internal init(id: Int, database: Database, databaseWriteQueue: Dispatch.Queue, defaultValueCacheSize: Int = 50) throws {
        self.id = id
        self.database = database
        self.databaseWriteQueue = databaseWriteQueue
        self.connectionQueue = Dispatch.Queues.create(.SerialQueue, name: "turf.connection[\(id)]")
        self.localSnapshot = 0
        self.defaultValueCacheSize = defaultValueCacheSize
        self.extensionConnections = [:]
        self.collectionsLocalStorage = [:]
        self.modifiedCollections = [:]

        Dispatch.Queues.setContext(
            Dispatch.Queues.makeContext(self.connectionQueue),
            key: connectionQueueKey,
            forQueue: self.connectionQueue)

        self.sqlite = try SQLiteAdapter(sqliteDatabaseUrl: database.url)
        try self.createExtensionConnections()
    }

    /**
     - note:
        - Thread safe
            - Will not close the sqlite connection until any read/writes have completed
     */
    deinit {
        Dispatch.synchronouslyOn(connectionQueue) {
            self.sqlite.close()
        }
        database.removeConnection(self)
    }

    // MARK: Public methods

    /**
     Pass a new read transaction into `closure` that will be executed asynchronously on a read queue.
     - note:
{'fraction_non_alphanumeric': 0.06863052334691319, 'fraction_numerical': 0.0031338138514572234, 'mean_word_length': 2.806201550387597, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3595302', 'n_tokens_mistral': 1632, 'n_tokens_neox': 1531, 'n_words': 537}