@@ -287,7 +287,14 @@ final class SQLitePackageCollectionsStorage: PackageCollectionsStorage, Closable
287
287
func searchPackages( identifiers: [ Model . CollectionIdentifier ] ? = nil ,
288
288
query: String ,
289
289
callback: @escaping ( Result < Model . PackageSearchResult , Error > ) -> Void ) {
290
- if self . useSearchIndices. get ( ) ?? false {
290
+ let useSearchIndices : Bool
291
+ do {
292
+ useSearchIndices = try self . shouldUseSearchIndices ( )
293
+ } catch {
294
+ return callback ( . failure( error) )
295
+ }
296
+
297
+ if useSearchIndices {
291
298
var matches = [ ( collection: Model . CollectionIdentifier, package : PackageIdentity) ] ( )
292
299
var matchingCollections = Set < Model . CollectionIdentifier > ( )
293
300
@@ -397,7 +404,14 @@ final class SQLitePackageCollectionsStorage: PackageCollectionsStorage, Closable
397
404
func findPackage( identifier: PackageIdentity ,
398
405
collectionIdentifiers: [ Model . CollectionIdentifier ] ? ,
399
406
callback: @escaping ( Result < Model . PackageSearchResult . Item , Error > ) -> Void ) {
400
- if self . useSearchIndices. get ( ) ?? false {
407
+ let useSearchIndices : Bool
408
+ do {
409
+ useSearchIndices = try self . shouldUseSearchIndices ( )
410
+ } catch {
411
+ return callback ( . failure( error) )
412
+ }
413
+
414
+ if useSearchIndices {
401
415
var matches = [ ( collection: Model . CollectionIdentifier, package : PackageIdentity) ] ( )
402
416
var matchingCollections = Set < Model . CollectionIdentifier > ( )
403
417
@@ -499,7 +513,14 @@ final class SQLitePackageCollectionsStorage: PackageCollectionsStorage, Closable
499
513
callback ( . success( result) )
500
514
}
501
515
502
- if self . useSearchIndices. get ( ) ?? false {
516
+ let useSearchIndices : Bool
517
+ do {
518
+ useSearchIndices = try self . shouldUseSearchIndices ( )
519
+ } catch {
520
+ return callback ( . failure( error) )
521
+ }
522
+
523
+ if useSearchIndices {
503
524
var matches = [ ( collection: Model . CollectionIdentifier, package : PackageIdentity, targetName: String) ] ( )
504
525
var matchingCollections = Set < Model . CollectionIdentifier > ( )
505
526
@@ -662,7 +683,7 @@ final class SQLitePackageCollectionsStorage: PackageCollectionsStorage, Closable
662
683
}
663
684
664
685
private func insertToSearchIndices( collection: Model . Collection ) throws {
665
- guard self . useSearchIndices . get ( ) ?? false else { return }
686
+ guard try self . shouldUseSearchIndices ( ) else { return }
666
687
667
688
try self . ftsLock. withLock {
668
689
// First delete existing data
@@ -730,7 +751,7 @@ final class SQLitePackageCollectionsStorage: PackageCollectionsStorage, Closable
730
751
}
731
752
732
753
private func removeFromSearchIndices( identifier: Model . CollectionIdentifier ) throws {
733
- guard self . useSearchIndices . get ( ) ?? false else { return }
754
+ guard try self . shouldUseSearchIndices ( ) else { return }
734
755
735
756
let identifierBase64 = try self . encoder. encode ( identifier. databaseKey ( ) ) . base64EncodedString ( )
736
757
@@ -760,6 +781,13 @@ final class SQLitePackageCollectionsStorage: PackageCollectionsStorage, Closable
760
781
self . targetTrie. remove { $0. collection == identifier }
761
782
}
762
783
784
+ private func shouldUseSearchIndices( ) throws -> Bool {
785
+ // Make sure createSchemaIfNecessary is called and useSearchIndices is set before reading it
786
+ try self . withDB { _ in
787
+ self . useSearchIndices. get ( ) ?? false
788
+ }
789
+ }
790
+
763
791
internal func populateTargetTrie( callback: @escaping ( Result < Void , Error > ) -> Void = { _ in } ) {
764
792
DispatchQueue . sharedConcurrent. async ( group: nil , qos: . background, flags: . assignCurrentContext) {
765
793
do {
@@ -893,7 +921,7 @@ final class SQLitePackageCollectionsStorage: PackageCollectionsStorage, Closable
893
921
#if os(Android)
894
922
// FTS queries for strings containing hyphens isn't working in SQLite on
895
923
// Android, so disable for now.
896
- useSearchIndices. put ( false )
924
+ self . useSearchIndices. put ( false )
897
925
#else
898
926
do {
899
927
let ftsPackages = """
@@ -914,12 +942,12 @@ final class SQLitePackageCollectionsStorage: PackageCollectionsStorage, Closable
914
942
"""
915
943
try db. exec ( query: ftsTargets)
916
944
917
- useSearchIndices. put ( true )
945
+ self . useSearchIndices. put ( true )
918
946
} catch {
919
947
// We can use FTS3 tables but queries yield different results when run on different
920
948
// platforms. This could be because of SQLite version perhaps? But since we can't get
921
949
// consistent results we will not fallback to FTS3 and just give up if FTS4 is not available.
922
- useSearchIndices. put ( false )
950
+ self . useSearchIndices. put ( false )
923
951
}
924
952
#endif
925
953
0 commit comments