22// CollectionManager.swift
33// CbliteSwiftJsLib
44//
5- // Created by Aaron LaBeau on 4/7 /24.
5+ // Created by Aaron LaBeau on 07/04 /24.
66//
77
88import Foundation
99
1010import CouchbaseLiteSwift
1111
1212enum CollectionError : Error {
13- case unableToFindCollection( collectionName: String , scopeName: String , databaseName: String )
14- case getCollection( message: String , collectionName: String , scopeName: String , databaseName: String )
13+ case unableToFindCollection( collectionName: String , scopeName: String , databaseName: String )
14+ case getCollection( message: String , collectionName: String , scopeName: String , databaseName: String )
1515 case cannotCreateIndex( indexName: String )
1616 case createIndex( indexName: String , message: String )
1717 case unknownIndexType( indexType: String )
1818 case documentError( message: String , collectionName: String , scopeName: String , databaseName: String )
1919}
2020
2121public class CollectionManager {
22-
23- private var defaultCollectionName : String = " _default "
24- private var defaultScopeName : String = " _default "
25-
22+
23+ private var defaultCollectionName : String = " _default "
24+ private var defaultScopeName : String = " _default "
25+
2626 // MARK: - Private for management of state
27-
28- //index is based on databaseName.scopeName.collectionName
27+
28+ // index is based on databaseName.scopeName.collectionName
2929 var collections = [ String: Collection] ( )
3030 var documentChangeListeners = [ String: Any] ( )
31-
31+
3232 // MARK: - Singleton
3333 static let shared = CollectionManager ( )
34-
35- //MARK: - Private initializer to prevent external instatiation
34+
35+ // MARK: - Private initializer to prevent external instatiation
3636 private init ( ) {
37-
37+
3838 }
39-
39+
4040 // MARK: - Helper Functions
41-
41+
4242 private func getCollectionKey( _ collectionName: String ,
4343 scopeName: String ,
4444 databaseName: String ) -> String {
4545 return " \( databaseName) . \( scopeName) . \( collectionName) "
4646 }
47-
47+
4848 public func getCollection( _ collectionName: String ,
4949 scopeName: String ,
5050 databaseName: String ) throws -> Collection ? {
5151 guard let database = DatabaseManager . shared. getDatabase ( databaseName) else {
5252 throw DatabaseError . invalidDatabaseName ( databaseName: databaseName)
5353 }
54-
54+
5555 do {
5656 let key = getCollectionKey (
5757 collectionName,
@@ -72,23 +72,23 @@ public class CollectionManager {
7272 throw CollectionError . getCollection ( message: error. localizedDescription, collectionName: collectionName, scopeName: scopeName, databaseName: databaseName)
7373 }
7474 }
75-
75+
7676 // MARK: Index Functions
77-
77+
7878 public func createIndex( _ indexName: String ,
7979 indexType: String ,
8080 items: [ [ Any ] ] ,
8181 collectionName: String ,
8282 scopeName: String ,
8383 databaseName: String ) throws {
84-
84+
8585 guard let collection = try self . getCollection (
8686 collectionName,
8787 scopeName: scopeName,
8888 databaseName: databaseName) else {
8989 throw CollectionError . unableToFindCollection ( collectionName: collectionName, scopeName: scopeName, databaseName: databaseName)
9090 }
91-
91+
9292 let index : Index
9393 switch indexType {
9494 case " value " :
@@ -98,19 +98,19 @@ public class CollectionManager {
9898 default :
9999 throw CollectionError . unknownIndexType ( indexType: indexType)
100100 }
101-
101+
102102 do {
103103 try collection. createIndex ( index, name: indexName)
104104 } catch {
105105 throw CollectionError . createIndex ( indexName: indexName, message: error. localizedDescription)
106106 }
107107 }
108-
108+
109109 func deleteIndex( _ indexName: String ,
110110 collectionName: String ,
111111 scopeName: String ,
112112 databaseName: String ) throws {
113-
113+
114114 guard let collection = try self . getCollection (
115115 collectionName,
116116 scopeName: scopeName,
@@ -126,11 +126,11 @@ public class CollectionManager {
126126 throw error
127127 }
128128 }
129-
129+
130130 func indexes( _ collectionName: String ,
131131 scopeName: String ,
132132 databaseName: String ) throws -> [ String ] {
133-
133+
134134 guard let collection = try self . getCollection (
135135 collectionName,
136136 scopeName: scopeName,
@@ -140,13 +140,13 @@ public class CollectionManager {
140140 }
141141 return indexes
142142 }
143-
143+
144144 // MARK: Document Functions
145-
145+
146146 func documentsCount( _ collectionName: String ,
147147 scopeName: String ,
148148 databaseName: String ) throws -> UInt64 {
149-
149+
150150 guard let collection = try self . getCollection (
151151 collectionName,
152152 scopeName: scopeName,
@@ -158,15 +158,14 @@ public class CollectionManager {
158158 }
159159 return collection. count
160160 }
161-
161+
162162 func saveDocument( _ documentId: String ,
163163 document: [ String : Any ] ,
164164 concurrencyControl: ConcurrencyControl ? ,
165165 collectionName: String ,
166166 scopeName: String ,
167167 databaseName: String ) throws -> String {
168-
169-
168+
170169 guard let collection = try self . getCollection (
171170 collectionName,
172171 scopeName: scopeName,
@@ -182,9 +181,9 @@ public class CollectionManager {
182181 } else {
183182 mutableDocument = MutableDocument ( data: MapHelper . toMap ( document) )
184183 }
185-
184+
186185 do {
187-
186+
188187 if let concurrencyControlValue = concurrencyControl {
189188 let results = try collection. save ( document: mutableDocument, concurrencyControl: concurrencyControlValue)
190189 if results {
@@ -203,12 +202,12 @@ public class CollectionManager {
203202 databaseName: databaseName)
204203 }
205204 }
206-
205+
207206 func document( _ documentId: String ,
208207 collectionName: String ,
209208 scopeName: String ,
210209 databaseName: String ) throws -> Document ? {
211-
210+
212211 guard let collection = try self . getCollection (
213212 collectionName,
214213 scopeName: scopeName,
@@ -229,7 +228,7 @@ public class CollectionManager {
229228 databaseName: databaseName)
230229 }
231230 }
232-
231+
233232 func getBlobContent( _ key: String , documentId: String , collectionName: String , scopeName: String , databaseName: String ) throws -> [ Int ] ? {
234233 guard let collection = try self . getCollection (
235234 collectionName,
@@ -240,15 +239,15 @@ public class CollectionManager {
240239 scopeName: scopeName,
241240 databaseName: databaseName)
242241 }
243-
242+
244243 guard let document = try collection. document ( id: documentId) else {
245244 throw CollectionError . documentError ( message: " can't find document " , collectionName: collectionName, scopeName: scopeName, databaseName: databaseName)
246245 }
247-
246+
248247 guard let blob = document. blob ( forKey: key) else {
249248 return [ ]
250249 }
251-
250+
252251 if let data = blob. content {
253252 var content : [ Int ] = [ ]
254253 data. regions. forEach { region in
@@ -260,12 +259,12 @@ public class CollectionManager {
260259 }
261260 return [ ]
262261 }
263-
262+
264263 func deleteDocument( _ documentId: String ,
265264 collectionName: String ,
266265 scopeName: String ,
267266 databaseName: String ) throws {
268-
267+
269268 guard let collection = try self . getCollection (
270269 collectionName,
271270 scopeName: scopeName,
@@ -292,13 +291,13 @@ public class CollectionManager {
292291 databaseName: databaseName)
293292 }
294293 }
295-
294+
296295 func deleteDocument( _ documentId: String ,
297296 concurrencyControl: ConcurrencyControl ,
298297 collectionName: String ,
299298 scopeName: String ,
300299 databaseName: String ) throws -> String {
301-
300+
302301 guard let collection = try self . getCollection (
303302 collectionName,
304303 scopeName: scopeName,
@@ -317,7 +316,7 @@ public class CollectionManager {
317316 databaseName: databaseName)
318317 }
319318 let result = try collection. delete ( document: document, concurrencyControl: concurrencyControl)
320- if ( result) {
319+ if result {
321320 return " true "
322321 }
323322 return " false "
@@ -329,12 +328,12 @@ public class CollectionManager {
329328 databaseName: databaseName)
330329 }
331330 }
332-
331+
333332 func purgeDocument( _ documentId: String ,
334333 collectionName: String ,
335334 scopeName: String ,
336335 databaseName: String ) throws {
337-
336+
338337 guard let collection = try self . getCollection (
339338 collectionName,
340339 scopeName: scopeName,
@@ -354,13 +353,13 @@ public class CollectionManager {
354353 databaseName: databaseName)
355354 }
356355 }
357-
356+
358357 func setDocumentExpiration( _ documentId: String ,
359358 expiration: Date ? ,
360359 collectionName: String ,
361360 scopeName: String ,
362361 databaseName: String ) throws {
363-
362+
364363 guard let collection = try self . getCollection (
365364 collectionName,
366365 scopeName: scopeName,
@@ -381,14 +380,14 @@ public class CollectionManager {
381380 scopeName: scopeName,
382381 databaseName: databaseName)
383382 }
384-
383+
385384 }
386-
385+
387386 func getDocumentExpiration( _ documentId: String ,
388387 collectionName: String ,
389388 scopeName: String ,
390- databaseName: String ) throws -> Date ? {
391-
389+ databaseName: String ) throws -> Date ? {
390+
392391 guard let collection = try self . getCollection (
393392 collectionName,
394393 scopeName: scopeName,
@@ -408,13 +407,13 @@ public class CollectionManager {
408407 scopeName: scopeName,
409408 databaseName: databaseName)
410409 }
411-
410+
412411 }
413-
412+
414413 // MARK: Replicator Functions
415-
414+
416415 func replicator( _ replicatorId: String , replicatorConfig: [ String : Any ] , collectionName: String , scopeName: String , databaseName: String ) throws {
417-
416+
418417 }
419-
418+
420419}
0 commit comments