Skip to content

Commit

Permalink
Fix failing test for custom FTS5 tokenizer in DatabasePool
Browse files Browse the repository at this point in the history
Tokenizer are now added to new reader connections.
  • Loading branch information
groue committed Sep 22, 2018
1 parent bdcbee3 commit a5a8b2a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 37 deletions.
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

0 comments on commit a5a8b2a

Please sign in to comment.