From 4411dd91a80b5c3830542605e4ff1787c3e5f88d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sat, 22 Sep 2018 08:59:50 +0200 Subject: [PATCH 1/3] Failing test for custom FTS5 tokenizer in DatabasePool --- Tests/GRDBTests/FTS5CustomTokenizerTests.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/GRDBTests/FTS5CustomTokenizerTests.swift b/Tests/GRDBTests/FTS5CustomTokenizerTests.swift index eeeee2fbe4..6c8c6425a3 100644 --- a/Tests/GRDBTests/FTS5CustomTokenizerTests.swift +++ b/Tests/GRDBTests/FTS5CustomTokenizerTests.swift @@ -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 From c99794e1a86e1fd5e7080064881fd999b386f85a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sat, 22 Sep 2018 09:22:05 +0200 Subject: [PATCH 2/3] Fix failing test for custom FTS5 tokenizer in DatabasePool Tokenizer are now added to new reader connections. --- GRDB/Core/DatabasePool.swift | 55 ++++++++++++++++++------------ GRDB/FTS/FTS5CustomTokenizer.swift | 15 -------- 2 files changed, 33 insertions(+), 37 deletions(-) diff --git a/GRDB/Core/DatabasePool.swift b/GRDB/Core/DatabasePool.swift index 6a5df7bb81..eb940bae1e 100644 --- a/GRDB/Core/DatabasePool.swift +++ b/GRDB/Core/DatabasePool.swift @@ -17,6 +17,7 @@ public final class DatabasePool: DatabaseWriter { private var functions = Set() private var collations = Set() + private var tokenizerRegistrations: [(Database) -> Void] = [] var snapshotCount = ReadWriteBox(0) @@ -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) } } } @@ -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() } @@ -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 @@ -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: Tokenizer.Type) { + func registerTokenizer(db: Database) { + db.add(tokenizer: Tokenizer.self) } + tokenizerRegistrations.append(registerTokenizer) + forEachConnection(registerTokenizer) } + #endif } extension DatabasePool { diff --git a/GRDB/FTS/FTS5CustomTokenizer.swift b/GRDB/FTS/FTS5CustomTokenizer.swift index fb604300b8..b7834e2253 100644 --- a/GRDB/FTS/FTS5CustomTokenizer.swift +++ b/GRDB/FTS/FTS5CustomTokenizer.swift @@ -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: Tokenizer.Type) { - writeWithoutTransaction { db in - db.add(tokenizer: Tokenizer.self) - } - } - } #endif From bce49acb980f40ca2325286453c084166ebb07fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sat, 22 Sep 2018 09:30:53 +0200 Subject: [PATCH 3/3] CHANGELOG --- CHANGELOG.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index de22c0fc8c..bbfc3d3a3d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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)