Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix database pool use of FTS5 tokenizers #414

Merged
merged 3 commits into from
Sep 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
Release Notes
=============

## Next Version

### Fixed

- [#414](https://github.com/groue/GRDB.swift/pull/414): Fix database pool use of FTS5 tokenizers


## 3.3.0

Released September 16, 2018 • [diff](https://github.com/groue/GRDB.swift/compare/v3.3.0-beta1...v3.3.0)
Expand Down
55 changes: 33 additions & 22 deletions GRDB/Core/DatabasePool.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public final class DatabasePool: DatabaseWriter {

private var functions = Set<DatabaseFunction>()
private var collations = Set<DatabaseCollation>()
private var tokenizerRegistrations: [(Database) -> Void] = []

var snapshotCount = ReadWriteBox(0)

Expand Down Expand Up @@ -105,12 +106,22 @@ public final class DatabasePool: DatabaseWriter {
#endif

private func setupDatabase(_ db: Database) {
for function in self.functions {
for function in functions {
db.add(function: function)
}
for collation in self.collations {
for collation in collations {
db.add(collation: collation)
}
for registration in tokenizerRegistrations {
registration(db)
}
}

/// Blocks the current thread until all database connections have
/// executed the *body* block.
fileprivate func forEachConnection(_ body: (Database) -> Void) {
writer.sync(body)
readerPool.forEach { $0.sync(body) }
}
}

Expand Down Expand Up @@ -148,11 +159,7 @@ extension DatabasePool {
///
/// See also setupMemoryManagement(application:)
public func releaseMemory() {
// TODO: test that this method blocks the current thread until all database accesses are completed.
writer.sync { $0.releaseMemory() }
readerPool.forEach { reader in
reader.sync { $0.releaseMemory() }
}
forEachConnection { $0.releaseMemory() }
readerPool.clear()
}

Expand Down Expand Up @@ -722,19 +729,13 @@ extension DatabasePool : DatabaseReader {
/// }
public func add(function: DatabaseFunction) {
functions.update(with: function)
writer.sync { $0.add(function: function) }
readerPool.forEach { reader in
reader.sync { $0.add(function: function) }
}
forEachConnection { $0.add(function: function) }
}

/// Remove an SQL function.
public func remove(function: DatabaseFunction) {
functions.remove(function)
writer.sync { $0.remove(function: function) }
readerPool.forEach { reader in
reader.sync { $0.remove(function: function) }
}
forEachConnection { $0.remove(function: function) }
}

// MARK: - Collations
Expand All @@ -750,20 +751,30 @@ extension DatabasePool : DatabaseReader {
/// }
public func add(collation: DatabaseCollation) {
collations.update(with: collation)
writer.sync { $0.add(collation: collation) }
readerPool.forEach { reader in
reader.sync { $0.add(collation: collation) }
}
forEachConnection { $0.add(collation: collation) }
}

/// Remove a collation.
public func remove(collation: DatabaseCollation) {
collations.remove(collation)
writer.sync { $0.remove(collation: collation) }
readerPool.forEach { reader in
reader.sync { $0.remove(collation: collation) }
forEachConnection { $0.remove(collation: collation) }
}

// MARK: - Custom FTS5 Tokenizers

#if SQLITE_ENABLE_FTS5
/// Add a custom FTS5 tokenizer.
///
/// class MyTokenizer : FTS5CustomTokenizer { ... }
/// dbPool.add(tokenizer: MyTokenizer.self)
public func add<Tokenizer: FTS5CustomTokenizer>(tokenizer: Tokenizer.Type) {
func registerTokenizer(db: Database) {
db.add(tokenizer: Tokenizer.self)
}
tokenizerRegistrations.append(registerTokenizer)
forEachConnection(registerTokenizer)
}
#endif
}

extension DatabasePool {
Expand Down
15 changes: 0 additions & 15 deletions GRDB/FTS/FTS5CustomTokenizer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -150,19 +150,4 @@
}
}
}

extension DatabasePool {

// MARK: - Custom FTS5 Tokenizers

/// Add a custom FTS5 tokenizer.
///
/// class MyTokenizer : FTS5CustomTokenizer { ... }
/// dbPool.add(tokenizer: MyTokenizer.self)
public func add<Tokenizer: FTS5CustomTokenizer>(tokenizer: Tokenizer.Type) {
writeWithoutTransaction { db in
db.add(tokenizer: Tokenizer.self)
}
}
}
#endif
2 changes: 1 addition & 1 deletion Tests/GRDBTests/FTS5CustomTokenizerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ class FTS5CustomTokenizerTests: GRDBTestCase {
}

func testStopWordsTokenizerDatabasePool() throws {
let dbPool = try makeDatabaseQueue()
let dbPool = try makeDatabasePool()
dbPool.add(tokenizer: StopWordsTokenizer.self)

try dbPool.write { db in
Expand Down