-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDatabaseManager.swift
More file actions
739 lines (595 loc) · 33.1 KB
/
DatabaseManager.swift
File metadata and controls
739 lines (595 loc) · 33.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
//
// DatabaseManager.swift
// CBL-Tests-iOS
//
// Created by Callum Birks on 01/08/2023.
//
import CouchbaseLiteSwift
import ZipArchive
import CryptoKit
class DatabaseManager {
private let kDatasetBaseURL = "https://media.githubusercontent.com/media/couchbaselabs/couchbase-lite-tests/refs/heads/main/dataset/server/"
private let kDatasetDownloadDirectory = "downloads"
private let kDatasetExtractedDirectory = "extracted"
private let databaseDirectory: String
private let datasetVersion: String
private var databases : [ String : Database ] = [:]
private var replicators : [ UUID : Replicator ] = [:]
private var replicatorDocuments : [ UUID : [ ContentTypes.DocumentReplication ] ] = [:]
private var replicatorDocumentsToken : [ UUID : ListenerToken ] = [:]
private var multipeerReplicators : [ UUID : MultipeerReplicator ] = [:]
private var peerReplicatorStatus : [ UUID : [ PeerID: (status: Replicator.Status, transport: MultipeerTransport) ] ] = [:]
private var peerReplicatorTransport : [ UUID : [ PeerID: ContentTypes.MultipeerTransport ] ] = [:]
private var peerReplicatorStatusToken : [ UUID : ListenerToken ] = [:]
private var peerReplicatorDocuments : [ UUID : [ PeerID: [ ContentTypes.DocumentReplication ] ] ] = [:]
private var peerReplicatorDocumentsToken : [ UUID : ListenerToken ] = [:]
private var listeners: [ UUID : URLEndpointListener ] = [:]
private let listenerQueue = DispatchQueue(label: "DatabaseManagerListenerQueue")
public init(directory: String, datasetVersion: String) {
self.databaseDirectory = directory
self.datasetVersion = datasetVersion
}
deinit {
do {
try reset()
} catch {
Log.log(level: .error, message: "Failed to reset database manager : \(error)")
}
}
@discardableResult
public func addCollection(dbName: String, scope: String, name: String) throws -> Collection {
Log.log(level: .debug, message: "Creating collection: \(scope).\(name) in database: \(dbName)")
guard let database = databases[dbName]
else {
Log.log(level: .error, message: "Failed to create collection, database '\(dbName)' does not exist")
throw TestServerError.cblDBNotOpen
}
do {
let coll = try database.createCollection(name: name, scope: scope)
Log.log(level: .debug, message: "Collection \(scope).\(name) successfully created in database \(dbName)")
return coll
} catch(let error as NSError) {
Log.log(level: .error, message: "Failed to create collection due to CBL error: \(error)")
throw TestServerError(domain: .CBL, code: error.code, message: error.localizedDescription)
}
}
public func runQuery(dbName: String, queryString: String) throws -> ResultSet {
Log.log(level: .debug, message: "Running query: `\(queryString)` in database: \(dbName)")
guard let database = databases[dbName]
else {
Log.log(level: .error, message: "Failed to run query, database '\(dbName)' does not exist")
throw TestServerError.cblDBNotOpen
}
do {
let query = try database.createQuery(queryString)
let result = try query.execute()
Log.log(level: .debug, message: "Query successfully executed.")
return result
} catch(let error as NSError) {
Log.log(level: .error, message: "Failed to run query due to CBL error: \(error)")
throw TestServerError(domain: .CBL, code: error.code, message: error.localizedDescription)
}
}
public func startListener(dbName: String, collections: [String], port: UInt16?, disableTLS: Bool = false) throws -> UUID {
var collectionsArr: [Collection] = []
guard let database = databases[dbName]
else {
Log.log(level: .error, message: "Failed to start Listener, database '\(dbName)' does not exist")
throw TestServerError.cblDBNotOpen
}
for collName in collections {
guard let coll = try collection(collName, inDB: database)
else {
Log.log(level: .error, message: "Failed to start Listener, Collection '\(collName)' does not exist in \(dbName).")
throw TestServerError.badRequest("Collection '\(collName)' does not exist in \(dbName).")
}
collectionsArr.append(coll)
}
var listenerConfig = URLEndpointListenerConfiguration(collections: collectionsArr)
listenerConfig.port = port
listenerConfig.disableTLS = disableTLS
let listener = URLEndpointListener(config: listenerConfig)
let listenerID = UUID()
listeners[listenerID] = listener
try listener.start()
Log.log(level: .debug, message: "EndpointListener started successfully with ID \(listenerID)")
return listenerID
}
public func stopListener(forID listenerID: UUID) throws {
Log.log(level: .debug, message: "Stop EndpointListener for ID \(listenerID) is requested.")
guard let listener = listeners[listenerID] else {
throw TestServerError.badRequest("EndpointListener with ID '\(listenerID)' does not exist.")
}
listener.stop()
Log.log(level: .debug, message: "Stop EndpointListener for ID \(listenerID) is successfully requested.")
}
public func startReplicator(config: ContentTypes.ReplicatorConfiguration, reset: Bool) throws -> UUID {
Log.log(level: .debug, message: "Starting Replicator with config: \(config.description)")
guard let database = databases[config.database]
else {
Log.log(level: .error, message: "Failed to start Replicator, database '\(config.database)' does not exist")
throw TestServerError.cblDBNotOpen
}
guard let endpointURL = URL(string: config.endpoint)
else {
Log.log(level: .error, message: "Failed to start Replicator, invalid endpoint URL.")
throw TestServerError.badRequest("Endpoint URL is not a valid URL.")
}
var replConfig: ReplicatorConfiguration
var collectionsConfig: [CollectionConfiguration] = []
if config.collections.count > 0 {
for configColl in config.collections {
let _ = try configColl.names.map({ collName in
guard let collection = try collection(collName, inDB: database)
else {
Log.log(level: .error, message: "Failed to start Replicator, Collection '\(collName)' does not exist in \(config.database).")
throw TestServerError.badRequest("Collection '\(collName)' does not exist in \(config.database).")
}
var collConfig = CollectionConfiguration(collection: collection)
collConfig.channels = configColl.channels
collConfig.documentIDs = configColl.documentIDs
if let pullFilter = configColl.pullFilter {
collConfig.pullFilter = try DatabaseManager.getCBLReplicationFilter(from: pullFilter).toReplicationFilter()
}
if let pushFilter = configColl.pushFilter {
collConfig.pushFilter = try DatabaseManager.getCBLReplicationFilter(from: pushFilter).toReplicationFilter()
}
if let resolver = configColl.conflictResolver {
collConfig.conflictResolver = try DatabaseManager.getCBLReplicationConflictResolver(from: resolver)
}
collectionsConfig.append(collConfig)
return collConfig
})
}
replConfig = ReplicatorConfiguration(collections: collectionsConfig, target: URLEndpoint(url: endpointURL))
} else {
let defaultColConfig = CollectionConfiguration(collection: try database.defaultCollection())
replConfig = ReplicatorConfiguration(collections: [defaultColConfig], target: URLEndpoint(url: endpointURL))
}
switch(config.replicatorType) {
case .push:
replConfig.replicatorType = .push
case .pull:
replConfig.replicatorType = .pull
case .pushpull:
replConfig.replicatorType = .pushAndPull
}
replConfig.continuous = config.continuous
replConfig.enableAutoPurge = config.enableAutoPurge
if let auth = config.authenticator {
replConfig.authenticator = try DatabaseManager.getCBLAuthenticator(from: auth)
}
if let headersString = config.headers {
// Decode as [String: String] as per CBL type
if let data = headersString.data(using: .utf8) {
do {
let decoded = try JSONDecoder().decode([String: String].self, from: data)
replConfig.headers = decoded
} catch {
throw TestServerError.badRequest("Invalid headers. Must be a JSON object with string key-value pairs with single quotes." )
}
} else {
replConfig.headers = nil
}
}
if let pinnedCert = config.pinnedServerCert {
guard let cert = CertUtil.certificate(from: pinnedCert) else {
throw TestServerError.badRequest("Pinned server cert has invalid format.")
}
replConfig.pinnedServerCertificate = cert
}
let replicatorID = UUID()
let replicator = Replicator(config: replConfig)
replicators[replicatorID] = replicator
// Whenever a document is replicated, add it to the replicatorDocuments dict
if(config.enableDocumentListener) {
replicatorDocuments[replicatorID] = []
let listenerToken = replicator.addDocumentReplicationListener(withQueue: listenerQueue) { [weak self] docChange in
guard let strongSelf = self else { return }
for doc in docChange.documents {
var docFlags: [ContentTypes.DocumentReplicationFlags] = []
switch doc.flags {
case .accessRemoved:
docFlags.append(.accessRemoved)
case .deleted:
docFlags.append(.deleted)
default:
break
}
var error: TestServerError? = nil
if let docError = doc.error as NSError? {
error = TestServerError(domain: .CBL, code: docError.code, message: docError.localizedDescription)
}
strongSelf.replicatorDocuments[replicatorID]?.append(
ContentTypes.DocumentReplication(
collection: "\(doc.scope).\(doc.collection)",
documentID: doc.id,
isPush: docChange.isPush,
flags: docFlags,
error: error)
)
}
}
replicatorDocumentsToken[replicatorID] = listenerToken
}
replicator.start(reset: reset)
Log.log(level: .debug, message: "Replicator started successfully with ID \(replicatorID)")
return replicatorID
}
public func stopReplicator(forID replID: UUID) throws {
Log.log(level: .debug, message: "Stop Replicator for ID \(replID) is requested.")
guard let replicator = replicators[replID] else {
throw TestServerError.badRequest("Replicator with ID '\(replID)' does not exist.")
}
replicator.stop()
Log.log(level: .debug, message: "Stop Replicator for ID \(replID) is successfully requested.")
}
public func replicatorStatus(forID replID: UUID) -> ContentTypes.ReplicatorStatus? {
Log.log(level: .debug, message: "Fetching Replicator status for ID \(replID)")
var status: ContentTypes.ReplicatorStatus?
listenerQueue.sync {
guard let replicator = replicators[replID] else {
Log.log(level: .debug, message: "Failed to fetch Replicator status, Replicator with ID \(replID) not found.")
return
}
let docs: [ContentTypes.DocumentReplication] = replicatorDocuments[replID] ?? []
replicatorDocuments[replID] = [] // Reset after return per spec
status = ContentTypes.ReplicatorStatus(status: replicator.status, docs: docs)
Log.log(level: .debug, message: "Succeessfully fetched Replicator status for ID \(replID): \(status!.description)")
}
return status
}
public func startMultipeerReplicator(config: ContentTypes.MultipeerReplicatorConfiguration) throws -> UUID {
Log.log(level: .debug, message: "Starting Multipeer Replicator")
let identity = try DatabaseManager.multipeerReplicatorIdentity(for: config)
let authenticator = try DatabaseManager.multipeerAuthenticator(for: config.authenticator)
var collectionConfigs: [MultipeerCollectionConfiguration] = []
for replColl in config.collections {
for name in replColl.names {
guard let collection = try self.collection(name, inDB: config.database) else {
Log.log(level: .error, message: "Failed to start Multipeer Replicator as collection '\(name)' does not exist in \(config.database).")
throw TestServerError.badRequest("Collection '\(name)' does not exist in \(config.database).")
}
var collConfig = MultipeerCollectionConfiguration(collection: collection)
collConfig.documentIDs = replColl.documentIDs
if let pullFilter = replColl.pullFilter {
collConfig.pullFilter = try DatabaseManager.getCBLReplicationFilter(from: pullFilter).toMultipeerReplicationFilter()
}
if let pushFilter = replColl.pushFilter {
collConfig.pushFilter = try DatabaseManager.getCBLReplicationFilter(from: pushFilter).toMultipeerReplicationFilter()
}
if let resolver = replColl.conflictResolver {
collConfig.conflictResolver = try DatabaseManager.getCBLReplicationConflictResolver(from: resolver)
}
collectionConfigs.append(collConfig)
}
}
var conf = MultipeerReplicatorConfiguration(
peerGroupID: config.peerGroupID,
identity: identity,
authenticator: authenticator,
collections: collectionConfigs)
if let transports = config.transports, !transports.isEmpty {
let transportMap = transports.map { tr in
switch tr {
case ContentTypes.MultipeerTransport.wifi:
return CouchbaseLiteSwift.MultipeerTransport.wifi
case ContentTypes.MultipeerTransport.bluetooth:
return CouchbaseLiteSwift.MultipeerTransport.bluetooth
}
}
conf.transports = Set(transportMap)
}
let id = UUID()
let multipeerReplicator = try MultipeerReplicator(config: conf)
let listenerToken = multipeerReplicator.addPeerReplicatorStatusListener(on: listenerQueue) { [weak self] status in
guard let strongSelf = self else { return }
strongSelf.peerReplicatorStatus[id, default: [:]][status.peerID] = (status.status,status.transport)
}
let docReplToken = multipeerReplicator.addPeerDocumentReplicationListener(on: listenerQueue) { [weak self] docRepl in
guard let strongSelf = self else { return }
var docs: [ContentTypes.DocumentReplication] = []
docRepl.documents.forEach { doc in
docs.append(ContentTypes.DocumentReplication.init(doc: doc, isPush: docRepl.isPush))
}
strongSelf.peerReplicatorDocuments[id, default: [:]][docRepl.peerID, default: []] += docs
}
multipeerReplicator.start()
multipeerReplicators[id] = multipeerReplicator
peerReplicatorStatusToken[id] = listenerToken
peerReplicatorDocumentsToken[id] = docReplToken
return id
}
public func stopMultipeerReplicator(forID id: UUID) throws {
Log.log(level: .debug, message: "Stop Multipeer Replicator for ID \(id) is requested.")
guard let replicator = multipeerReplicators[id] else {
throw TestServerError.badRequest("Multipeer Replicator ID'\(id)' does not exist.")
}
replicator.stop()
Log.log(level: .debug, message: "Stop Multipeer Replicator for ID \(id) is successfully requested.")
}
public func multipeerReplicatorStatus(forID id: UUID) -> ContentTypes.MultipeerReplicatorStatus? {
Log.log(level: .debug, message: "Getting MultipeerReplicator Status for ID \(id)")
var status: ContentTypes.MultipeerReplicatorStatus?
listenerQueue.sync {
if multipeerReplicators[id] == nil {
Log.log(level: .debug, message: "Failed to get MultipeerReplicator Status, MultipeerReplicator with ID \(id) not found.")
return
}
var replicators: [ContentTypes.PeerReplicatorStatus] = []
if let statuses = peerReplicatorStatus[id] {
for (peerID, tuple) in statuses {
let status = tuple.status
let transport = tuple.transport
let docs = peerReplicatorDocuments[id]?[peerID] ?? []
let replStatus = ContentTypes.ReplicatorStatus.init(status: status, docs: docs)
let transport_type = ContentTypes.MultipeerTransport.init(transportType: transport)
replicators.append(ContentTypes.PeerReplicatorStatus(peerID: "\(peerID)", status: replStatus, transport:transport_type ))
peerReplicatorDocuments[id]?[peerID] = [] // Reset after return per spec
}
// Remove disconected peers with stopped replicators so their statuses are not included next time.
peerReplicatorStatus[id] = statuses.filter { $0.value.status.activity != .stopped }
}
status = ContentTypes.MultipeerReplicatorStatus.init(replicators: replicators)
}
return status
}
public func collection(_ name: String, inDB dbName: String) throws -> Collection? {
Log.log(level: .debug, message: "Fetching collecting '\(name)' in database '\(dbName)'")
guard let database = databases[dbName]
else {
Log.log(level: .error, message: "Failed to fetch collection, database '\(dbName)' not open.")
throw TestServerError.cblDBNotOpen
}
return try collection(name, inDB: database)
}
public func collection(_ name: String, inDB database: Database) throws -> Collection? {
Log.log(level: .debug, message: "Fetching collection with DB: \(database.name), collection: \(name)")
do {
let spec = try CollectionSpec(name)
let collection = try database.collection(name: spec.collection, scope: spec.scope)
Log.log(level: .debug, message: "Fetched collection, result: \(collection.debugDescription)")
return collection
} catch(let error as NSError) {
Log.log(level: .error, message: "Failed to fetch collection due to CBL error: \(error)")
throw TestServerError(domain: .CBL, code: error.code, message: error.localizedDescription)
}
}
public func getDocument(_ id: String, fromCollection collName: String, inDB dbName: String) throws -> Document? {
Log.log(level: .debug, message: "Getting document '\(id)' from collection '\(collName)' in database '\(dbName)'")
guard let collection = try collection(collName, inDB: dbName) else {
throw TestServerError.badRequest("Cannot find collection '\(collName)' in db '\(dbName)'")
}
return try collection.document(id: id)
}
// Returns [scope_name.collection_name]
public func getQualifiedCollections(fromDB dbName: String) throws -> Array<String> {
Log.log(level: .debug, message: "Fetching all collection names from DB '\(dbName)'")
guard let database = databases[dbName]
else {
Log.log(level: .error, message: "Failed to fetch collections, DB \(dbName) not open")
throw TestServerError.cblDBNotOpen
}
do {
var result: [String] = []
for scope in try database.scopes() {
for collection in try scope.collections() {
result.append("\(scope.name).\(collection.name)")
}
}
Log.log(level: .debug, message: "Fetched all collections: \(result)")
return result
} catch(let error as NSError) {
Log.log(level: .error, message: "Failed to fetch collections due to CBL error: \(error)")
throw TestServerError(domain: .CBL, code: error.code, message: error.localizedDescription)
}
}
public func performMaintenance(type: MaintenanceType, onDB dbName: String) throws {
guard let db = databases[dbName]
else { throw TestServerError.cblDBNotOpen }
do {
try db.performMaintenance(type: type)
} catch(let error as NSError) {
throw TestServerError(domain: .CBL, code: error.code, message: error.localizedDescription)
}
}
public func closeDatabase(withName dbName: String) throws {
Log.log(level: .debug, message: "Closing database '\(dbName)'")
guard let database = databases[dbName]
else {
Log.log(level: .debug, message: "Database \(dbName) was already closed.")
return
}
do {
try database.close()
databases.removeValue(forKey: dbName)
} catch(let error as NSError) {
Log.log(level: .error, message: "Failed to close database due to CBL error: \(error)")
throw TestServerError(domain: .CBL, code: error.code, message: error.localizedDescription)
}
Log.log(level: .debug, message: "Database '\(dbName)' closed successfully.")
}
public func createDatabase(dbName: String, dataset: String) throws {
Log.log(level: .debug, message: "Create Database \(dbName) with dataset \(dataset)")
// Load database with the dataset
try loadDataset(withName: dataset, dbName: dbName)
// Open database
do {
databases[dbName] = try Database(name: dbName)
} catch(let error as NSError) {
Log.log(level: .error, message: "CBL Error while re-opening DB: \(error)")
throw TestServerError(domain: .CBL, code: error.code, message: error.localizedDescription)
}
Log.log(level: .debug, message: "Database '\(dbName)' has been created with the dataset \(dataset).")
}
public func createDatabase(dbName: String, collections: [String] = []) throws {
Log.log(level: .debug, message: "Create Database \(dbName) with collections \(collections)")
// For any reasons if the database exists, delete it.
if Database.exists(withName: dbName) {
try Database.delete(withName: dbName)
}
// Open database
do {
let db = try Database(name: dbName)
for collName in collections {
let spec = try CollectionSpec(collName)
try _ = db.createCollection(name: spec.collection, scope: spec.scope)
}
databases[dbName] = db
} catch(let error as NSError) {
Log.log(level: .error, message: "CBL Error while re-opening DB: \(error)")
throw TestServerError(domain: .CBL, code: error.code, message: error.localizedDescription)
}
Log.log(level: .debug, message: "Database '\(dbName)' has been created with the collections \(collections).")
}
public func reset() throws {
Log.log(level: .debug, message: "Resetting all databases")
for dbName in databases.keys {
try closeDatabase(withName: dbName)
try? Database.delete(withName: dbName)
}
databases.removeAll()
// Reset all replicators:
replicators.removeAll()
replicatorDocuments.removeAll()
replicatorDocumentsToken.removeAll()
// Reset all listeners:
listeners.removeAll()
// Reset all multipeer replicators:
multipeerReplicators.values.forEach { $0.stop() } // Temporary until all close database also stops multipeer replicator
multipeerReplicators.removeAll()
peerReplicatorStatus.removeAll()
peerReplicatorTransport.removeAll()
peerReplicatorStatusToken.removeAll()
peerReplicatorDocuments.removeAll()
peerReplicatorDocumentsToken.removeAll()
}
private static func getCBLAuthenticator(from auth: ReplicatorAuthenticator) throws -> Authenticator {
switch auth {
case let auth as ContentTypes.ReplicatorBasicAuthenticator:
return BasicAuthenticator(username: auth.username, password: auth.password)
case let auth as ContentTypes.ReplicatorSessionAuthenticator:
return SessionAuthenticator(sessionID: auth.sessionID, cookieName: auth.cookieName)
default:
throw TestServerError.badRequest("'authenticator' parameter did not match a valid authenticator.")
}
}
private static func getCBLReplicationFilter(from filter: ContentTypes.ReplicationFilter) throws -> AnyReplicationFilter {
return try ReplicationFilterFactory.getFilter(withName: filter.name, params: filter.params)
}
private static func getCBLReplicationConflictResolver(from resolver: ContentTypes.ReplicationConflictResolver) throws -> ConflictResolver {
return try ReplicationConflictResolverFactory.getResolver(withName: resolver.name, params: resolver.params)
}
private func loadDataset(withName name: String, dbName: String) throws {
Log.log(level: .debug, message: "Loading dataset '\(name)' into DB '\(dbName)'")
let datasetRelativePath = URL(fileURLWithPath: "dbs")
.appendingPathComponent(datasetVersion)
.appendingPathComponent("\(name).cblite2.zip")
.relativePath
let datasetZipURL = try downloadDatasetFileIfNecessary(relativePath: datasetRelativePath)
Log.log(level: .debug, message: "Load dataset at \(datasetZipURL.path)")
let fm = FileManager()
let extractedDatasetDir = URL(fileURLWithPath: databaseDirectory)
.appendingPathComponent(kDatasetExtractedDirectory)
let extractedDatasetPath = extractedDatasetDir
.appendingPathComponent(datasetZipURL.deletingPathExtension().lastPathComponent)
.path
if(!fm.fileExists(atPath: extractedDatasetPath)) {
Log.log(level: .debug, message: "Unzipping dataset \(datasetZipURL.lastPathComponent)")
guard SSZipArchive.unzipFile(atPath: datasetZipURL.path, toDestination: extractedDatasetDir.path) else {
Log.log(level: .error, message: "Error while unzipping dataset at \(datasetZipURL.absoluteString)")
throw TestServerError(domain: .CBL, code: CBLError.cantOpenFile, message: "Couldn't unzip dataset archive.")
}
}
// For any reasons if the database exists, delete it.
if Database.exists(withName: dbName) {
try Database.delete(withName: dbName)
}
do {
Log.log(level: .debug, message: "Attempting to copy dataset from \(extractedDatasetPath)")
try Database.copy(fromPath: extractedDatasetPath, toDatabase: dbName, withConfig: nil)
Log.log(level: .debug, message: "Dataset '\(name)' successfully copied to DB '\(dbName)'")
} catch(let error as NSError) {
Log.log(level: .error, message: "Failed to copy dataset due to CBL error: \(error)")
throw TestServerError(domain: .CBL, code: error.code, message: error.localizedDescription)
}
}
private func datasetRelativePath(for name: String, version: String) -> String {
return "dbs/\(version)/\(name).cblite2.zip"
}
private func downloadDatasetFileIfNecessary(relativePath: String) throws -> URL {
let datasetPath = URL(fileURLWithPath: databaseDirectory)
.appendingPathComponent(kDatasetDownloadDirectory)
.appendingPathComponent(relativePath)
let fm = FileManager.default
if (fm.fileExists(atPath: datasetPath.path)) {
Log.log(level: .debug, message: "Skipping download, dataset already exists at pat \(datasetPath.path)")
return datasetPath
}
let parentDir = datasetPath.deletingLastPathComponent()
if (!fm.fileExists(atPath: parentDir.path)) {
try fm.createDirectory(at: parentDir, withIntermediateDirectories: true)
}
guard let url = URL(string: relativePath, relativeTo: URL(string: kDatasetBaseURL)) else {
throw TestServerError.badRequest("Invalid dataset path : \(relativePath)")
}
Log.log(level: .info, message: "Downloading dataset from \(url.absoluteString)")
try FileDownloader.download(url: url, to: datasetPath.path)
return datasetPath
}
public func loadBlob(filename: String) throws -> Blob {
let blobRelativePath = URL(fileURLWithPath: "blobs")
.appendingPathComponent(filename)
.relativePath
let blobFileURL = try downloadDatasetFileIfNecessary(relativePath: blobRelativePath)
Log.log(level: .debug, message: "Load blob from \(blobFileURL.path)")
let contentType: String = {
switch blobFileURL.pathExtension {
case "jpeg", "jpg": return "image/jpeg"
default: return "application/octet-stream"
}
}()
do {
return try Blob(contentType: contentType, fileURL: blobFileURL)
} catch(let error as NSError) {
throw TestServerError(domain: .CBL, code: error.code, message: error.localizedDescription)
}
}
public func blobFileExists(forBlob blob: Blob, inDB dbName: String) throws -> Bool {
guard let db = databases[dbName]
else { throw TestServerError.cblDBNotOpen }
do {
return try db.getBlob(properties: blob.properties) != nil
} catch(let error as NSError) {
throw TestServerError(domain: .CBL, code: error.code, message: error.localizedDescription)
}
}
private static func multipeerReplicatorIdentity(for config: ContentTypes.MultipeerReplicatorConfiguration) throws -> TLSIdentity {
let label = "ios-multipeer-\(config.peerGroupID)"
guard let data = Data(base64Encoded: config.identity.data) else {
throw TestServerError.badRequest("Invalid multipeer replictor's identity data")
}
try TLSIdentity.deleteIdentity(withLabel: label)
return try TLSIdentity.importIdentity(withData: data, password: config.identity.password, label: label)
}
private static func multipeerAuthenticator(for config: ContentTypes.MultipeerReplicatorCAAuthenticator?) throws -> MultipeerCertificateAuthenticator {
guard let auth = config else {
return MultipeerCertificateAuthenticator { peer, certs in
return true
}
}
let lines = auth.certificate
.components(separatedBy: .newlines)
.filter { !$0.contains("-----BEGIN CERTIFICATE-----") &&
!$0.contains("-----END CERTIFICATE-----") &&
!$0.isEmpty }
let certData = lines.joined()
guard let derData = Data(base64Encoded: certData) else {
throw TestServerError.badRequest("Failed to convert multipeer authenticator's root certificate from PEM to DER")
}
guard let rootCert = SecCertificateCreateWithData(nil, derData as CFData) else {
throw TestServerError.badRequest("Invalid multipeer authenticator's root certificate")
}
return MultipeerCertificateAuthenticator(rootCerts: [rootCert])
}
}