Skip to content

Commit 3f4fb7b

Browse files
committed
updated to add collection manager, query and replicator helper for dealing with replication config
1 parent 142b84f commit 3f4fb7b

File tree

6 files changed

+311
-42
lines changed

6 files changed

+311
-42
lines changed

CbliteSwiftJsLib/Classes/CollectionManager.swift

Lines changed: 53 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public class CollectionManager {
3737

3838
}
3939

40-
// MARK: - Helper methods
40+
// MARK: - Helper Functions
4141

4242
private func getCollectionKey(_ collectionName: String,
4343
scopeName: String,
@@ -73,7 +73,7 @@ public class CollectionManager {
7373
}
7474
}
7575

76-
// MARK: Index Methods
76+
// MARK: Index Functions
7777

7878
public func createIndex(_ indexName: String,
7979
indexType: String,
@@ -135,13 +135,13 @@ public class CollectionManager {
135135
collectionName,
136136
scopeName: scopeName,
137137
databaseName: databaseName),
138-
let indexes = try? collection.indexes() else {
138+
let indexes = try? collection.indexes() else {
139139
throw CollectionError.unableToFindCollection(collectionName: collectionName, scopeName: scopeName, databaseName: databaseName)
140140
}
141141
return indexes
142142
}
143143

144-
// MARK: Document methods
144+
// MARK: Document Functions
145145

146146
func documentsCount(_ collectionName: String,
147147
scopeName: String,
@@ -204,12 +204,10 @@ public class CollectionManager {
204204
}
205205
}
206206

207-
208-
209207
func document(_ documentId: String,
210-
collectionName: String,
211-
scopeName: String,
212-
databaseName: String) throws -> Document? {
208+
collectionName: String,
209+
scopeName: String,
210+
databaseName: String) throws -> Document? {
213211

214212
guard let collection = try self.getCollection(
215213
collectionName,
@@ -232,10 +230,41 @@ public class CollectionManager {
232230
}
233231
}
234232

233+
func getBlobContent(_ key: String, documentId: String, collectionName: String, scopeName: String, databaseName: String) throws -> [Int]? {
234+
guard let collection = try self.getCollection(
235+
collectionName,
236+
scopeName: scopeName,
237+
databaseName: databaseName) else {
238+
throw CollectionError.unableToFindCollection(
239+
collectionName: collectionName,
240+
scopeName: scopeName,
241+
databaseName: databaseName)
242+
}
243+
244+
guard let document = try collection.document(id: documentId) else {
245+
throw CollectionError.documentError(message: "can't find document", collectionName: collectionName, scopeName: scopeName, databaseName: databaseName)
246+
}
247+
248+
guard let blob = document.blob(forKey: key) else {
249+
return []
250+
}
251+
252+
if let data = blob.content {
253+
var content: [Int] = []
254+
data.regions.forEach { region in
255+
for byte in region {
256+
content.append(Int(byte))
257+
}
258+
}
259+
return content
260+
}
261+
return []
262+
}
263+
235264
func deleteDocument(_ documentId: String,
236-
collectionName: String,
237-
scopeName: String,
238-
databaseName: String) throws {
265+
collectionName: String,
266+
scopeName: String,
267+
databaseName: String) throws {
239268

240269
guard let collection = try self.getCollection(
241270
collectionName,
@@ -265,10 +294,10 @@ public class CollectionManager {
265294
}
266295

267296
func deleteDocument(_ documentId: String,
268-
concurrencyControl: ConcurrencyControl,
269-
collectionName: String,
270-
scopeName: String,
271-
databaseName: String) throws -> String {
297+
concurrencyControl: ConcurrencyControl,
298+
collectionName: String,
299+
scopeName: String,
300+
databaseName: String) throws -> String {
272301

273302
guard let collection = try self.getCollection(
274303
collectionName,
@@ -302,9 +331,9 @@ public class CollectionManager {
302331
}
303332

304333
func purgeDocument(_ documentId: String,
305-
collectionName: String,
306-
scopeName: String,
307-
databaseName: String) throws {
334+
collectionName: String,
335+
scopeName: String,
336+
databaseName: String) throws {
308337

309338
guard let collection = try self.getCollection(
310339
collectionName,
@@ -381,6 +410,11 @@ public class CollectionManager {
381410
}
382411

383412
}
413+
414+
// MARK: Replicator Functions
384415

416+
func replicator(_ replicatorId: String, replicatorConfig: [String: Any], collectionName: String, scopeName: String, databaseName: String) throws {
417+
418+
}
385419

386420
}

CbliteSwiftJsLib/Classes/DatabaseManager.swift

Lines changed: 153 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ enum DatabaseError: Error {
1414
case databaseLocked(databaseName: String)
1515
case copyError(message: String)
1616
case maintenanceError(message: String)
17+
case unknownError(message: String)
1718
}
1819

1920
public class DatabaseManager {
@@ -48,7 +49,7 @@ public class DatabaseManager {
4849
// Initialization code here
4950
}
5051

51-
// MARK: - Helper methods
52+
// MARK: - Helper Functions
5253

5354
public func getDatabase(_ name: String) -> Database? {
5455
objc_sync_enter(openDatabases)
@@ -73,7 +74,7 @@ public class DatabaseManager {
7374
return databaseConfiguration
7475
}
7576

76-
// MARK: Database Methods
77+
// MARK: Database Functions
7778

7879
public func open(_ databaseName: String, databaseConfig: [AnyHashable: Any]?) throws {
7980
do {
@@ -137,7 +138,7 @@ public class DatabaseManager {
137138
}
138139
}
139140

140-
// MARK: Database Maintenance methods
141+
// MARK: Database Maintenance Functions
141142

142143
func performMaintenance(_ databaseName: String, maintenanceType: MaintenanceType) throws {
143144
guard let database = self.getDatabase(databaseName) else {
@@ -157,7 +158,99 @@ public class DatabaseManager {
157158
}
158159
}
159160

160-
// MARK: Index Methods
161+
// MARK: Scope Functions
162+
163+
func scopes(_ databaseName: String) throws -> [Scope]? {
164+
do {
165+
guard let database = self.getDatabase(databaseName) else {
166+
throw DatabaseError.invalidDatabaseName(databaseName: databaseName)
167+
}
168+
return try database.scopes()
169+
} catch {
170+
throw DatabaseError.unknownError(message: error.localizedDescription)
171+
}
172+
}
173+
174+
func defaultScope(_ databaseName: String) throws -> Scope? {
175+
do {
176+
guard let database = self.getDatabase(databaseName) else {
177+
throw DatabaseError.invalidDatabaseName(databaseName: databaseName)
178+
}
179+
return try database.defaultScope()
180+
} catch {
181+
throw DatabaseError.unknownError(message: error.localizedDescription)
182+
}
183+
}
184+
185+
func scope(_ scopeName: String, databaseName: String) throws -> Scope? {
186+
do {
187+
guard let database = self.getDatabase(databaseName) else {
188+
throw DatabaseError.invalidDatabaseName(databaseName: databaseName)
189+
}
190+
return try database.scope(name: scopeName)
191+
} catch {
192+
throw DatabaseError.unknownError(message: error.localizedDescription)
193+
}
194+
}
195+
196+
// MARK: Collection Functions
197+
198+
func defaultCollection(_ databaseName: String) throws -> Collection? {
199+
do {
200+
guard let database = self.getDatabase(databaseName) else {
201+
throw DatabaseError.invalidDatabaseName(databaseName: databaseName)
202+
}
203+
return try database.defaultCollection()
204+
} catch {
205+
throw DatabaseError.unknownError(message: error.localizedDescription)
206+
}
207+
}
208+
209+
func collections(_ scopeName: String, databaseName: String) throws -> [Collection] {
210+
do {
211+
guard let database = self.getDatabase(databaseName) else {
212+
throw DatabaseError.invalidDatabaseName(databaseName: databaseName)
213+
}
214+
return try database.collections(scope: scopeName)
215+
} catch {
216+
throw DatabaseError.unknownError(message: error.localizedDescription)
217+
}
218+
}
219+
220+
func createCollection(_ collectionName: String, scopeName: String, databaseName:String) throws -> Collection {
221+
do {
222+
guard let database = self.getDatabase(databaseName) else {
223+
throw DatabaseError.invalidDatabaseName(databaseName: databaseName)
224+
}
225+
return try database.createCollection(name: collectionName, scope: scopeName)
226+
} catch {
227+
throw DatabaseError.unknownError(message: error.localizedDescription)
228+
}
229+
}
230+
231+
func collection(_ collectionName: String, scopeName: String, databaseName: String) throws -> Collection? {
232+
do {
233+
guard let database = self.getDatabase(databaseName) else {
234+
throw DatabaseError.invalidDatabaseName(databaseName: databaseName)
235+
}
236+
return try database.collection(name: collectionName, scope: scopeName)
237+
} catch {
238+
throw DatabaseError.unknownError(message: error.localizedDescription)
239+
}
240+
}
241+
242+
func deleteCollection(_ collectionName: String, scopeName: String, databaseName: String) throws {
243+
do {
244+
guard let database = self.getDatabase(databaseName) else {
245+
throw DatabaseError.invalidDatabaseName(databaseName: databaseName)
246+
}
247+
try database.deleteCollection(name: collectionName, scope: scopeName)
248+
} catch {
249+
throw DatabaseError.unknownError(message: error.localizedDescription)
250+
}
251+
}
252+
253+
// MARK: Index Functions
161254

162255
func createIndex(_ indexName: String,
163256
indexType: String,
@@ -202,7 +295,7 @@ public class DatabaseManager {
202295
}
203296
}
204297

205-
// MARK: Document methods
298+
// MARK: Document Functions
206299

207300
func getDocumentsCount(_ databaseName: String)
208301
throws -> UInt64 {
@@ -284,8 +377,61 @@ public class DatabaseManager {
284377
throw error
285378
}
286379
}
287-
288-
//TODO add blob support
289380

381+
func getBlobContent(_ key:String,
382+
documentId: String,
383+
databaseName: String) throws -> [Int]? {
384+
do {
385+
return try CollectionManager.shared.getBlobContent(
386+
key,
387+
documentId: documentId,
388+
collectionName: defaultCollectionName,
389+
scopeName: defaultScopeName,
390+
databaseName: databaseName)
391+
} catch {
392+
throw error
393+
}
394+
}
395+
396+
// MARK: SQL++ Query Functions
397+
398+
func executeQuery(_ query: String,
399+
parameters: [String:Any]? = nil,
400+
databaseName: String) throws -> String {
401+
do {
402+
guard let database = self.getDatabase(databaseName) else {
403+
throw DatabaseError.invalidDatabaseName(databaseName: databaseName)
404+
}
405+
let query = try database.createQuery(query)
406+
if let params = parameters {
407+
let queryParams = try QueryHelper.getParamatersFromJson(params)
408+
query.parameters = queryParams
409+
}
410+
let results = try query.execute()
411+
let resultJSONs = results.map { $0.toJSON() }
412+
let jsonArray = "[" + resultJSONs.joined(separator: ",") + "]"
413+
return jsonArray
414+
} catch {
415+
throw QueryError.unknownError(message: error.localizedDescription)
416+
}
417+
}
290418

419+
func queryExplain(_ query: String,
420+
parameters: [String:Any]? = nil,
421+
databaseName: String) throws -> String {
422+
do {
423+
guard let database = self.getDatabase(databaseName) else {
424+
throw DatabaseError.invalidDatabaseName(databaseName: databaseName)
425+
}
426+
let query = try database.createQuery(query)
427+
if let params = parameters {
428+
let queryParams = try QueryHelper.getParamatersFromJson(params)
429+
query.parameters = queryParams
430+
}
431+
let results = try query.explain()
432+
return results
433+
} catch {
434+
throw QueryError.unknownError(message: error.localizedDescription)
435+
}
436+
}
291437
}

0 commit comments

Comments
 (0)