forked from couchbase/couchbase-lite-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CBLDatabase.h
425 lines (324 loc) · 14.2 KB
/
CBLDatabase.h
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
//
// CBLDatabase.h
// CouchbaseLite
//
// Copyright (c) 2016 Couchbase, Inc All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#import <Foundation/Foundation.h>
#import "CBLLogger.h"
@class CBLDatabaseConfiguration;
@class CBLDocument, CBLMutableDocument, CBLDocumentFragment;
@class CBLDatabaseChange, CBLDocumentChange;
@class CBLIndex;
@class CBLLog;
@protocol CBLConflictResolver;
@protocol CBLListenerToken;
NS_ASSUME_NONNULL_BEGIN
/**
Concurruncy control type used when saving or deleting a document.
*/
typedef NS_ENUM(uint32_t, CBLConcurrencyControl) {
kCBLConcurrencyControlLastWriteWins, ///< The last write operation will win if there is a conflict.
kCBLConcurrencyControlFailOnConflict ///< The operation will fail if there is a conflict.
};
/** A Couchbase Lite database. */
@interface CBLDatabase : NSObject
/** The database's name. */
@property (readonly, nonatomic) NSString* name;
/** The database's path. If the database is closed or deleted, nil value will be returned. */
@property (readonly, atomic, nullable) NSString* path;
/** The number of documents in the database. */
@property (readonly, atomic) uint64_t count;
/**
The database's configuration. The returned configuration object is readonly;
an NSInternalInconsistencyException exception will be thrown if
the configuration object is modified.
*/
@property (readonly, nonatomic) CBLDatabaseConfiguration *config;
#pragma mark - Initializers
/**
Initializes a database object with a given name and the default database configuration.
If the database does not yet exist, it will be created.
@param name The name of the database.
@param error On return, the error if any.
*/
- (nullable instancetype) initWithName: (NSString*)name
error: (NSError**)error;
/**
Initializes a Couchbase Lite database with a given name and database configuration.
If the database does not yet exist, it will be created.
@param name The name of the database.
@param config The database configuration, or nil for the default configuration.
@param error On return, the error if any.
*/
- (nullable instancetype) initWithName: (NSString*)name
config: (nullable CBLDatabaseConfiguration*)config
error: (NSError**)error;
/** Not available */
- (instancetype) init NS_UNAVAILABLE;
#pragma mark - Get Existing Document
/**
Gets an existing document with the given ID. If a document with the given ID
doesn't exist in the database, the value returned will be nil.
@param id The document ID.
@return The CBLDocument object.
*/
- (nullable CBLDocument*) documentWithID: (NSString*)id;
#pragma mark - Subscript
/**
Gets a document fragment with the given document ID.
@param documentID The document ID.
@return The CBLDocumentFragment object.
*/
- (CBLDocumentFragment*) objectForKeyedSubscript: (NSString*)documentID;
#pragma mark - Save, Delete, Purge
/**
Saves a document to the database. When write operations are executed
concurrently, the last writer will overwrite all other written values.
Calling this method is the same as calling the -saveDocument:concurrencyControl:error:
method with kCBLConcurrencyControlLastWriteWins concurrency control.
@param document The document.
@param error On return, the error if any.
@return True on success, false on failure.
*/
- (BOOL) saveDocument: (CBLMutableDocument*)document error: (NSError**)error;
/**
Saves a document to the database. When used with kCBLConcurrencyControlLastWriteWins
concurrency control, the last write operation will win if there is a conflict.
When used with kCBLConcurrencyControlFailOnConflict concurrency control,
save will fail with 'CBLErrorConflict' error code returned.
@param document The document.
@param concurrencyControl The concurrency control.
@param error On return, the error if any.
@return True on success, false on failure.
*/
- (BOOL) saveDocument: (CBLMutableDocument*)document
concurrencyControl: (CBLConcurrencyControl)concurrencyControl
error: (NSError**)error;
/**
Saves a document to the database. When write operations are executed concurrently and if conflicts
occur, the conflict handler will be called. Use the conflict handler to directly edit the document
to resolve the conflict. When the conflict handler returns 'true', the save method will save the
edited document as the resolved document. If the conflict handler returns 'false', the save
operation will be canceled with 'false' value returned as the conflict wasn't resolved.
@param document The document.
@param conflictHandler The conflict handler block which can be used to resolve it.
@param error On return, error if any.
@return True if successful. False if there is a conflict, but the conflict wasn't resolved as the
conflict handler returns 'false' value.
*/
- (BOOL) saveDocument: (CBLMutableDocument*)document
conflictHandler: (BOOL (^)(CBLMutableDocument*, CBLDocument* nullable))conflictHandler
error: (NSError**)error;
/**
Deletes a document from the database. When write operations are executed
concurrently, the last writer will overwrite all other written values.
Calling this method is the same as calling the -deleteDocument:concurrencyControl:error:
method with kCBLConcurrencyControlLastWriteWins concurrency control.
@param document The document.
@param error On return, the error if any.
@return /True on success, false on failure.
*/
- (BOOL) deleteDocument: (CBLDocument*)document error: (NSError**)error;
/**
Deletes a document from the database. When used with kCBLConcurrencyControlLastWriteWins
concurrency control, the last write operation will win if there is a conflict.
When used with kCBLConcurrencyControlFailOnConflict concurrency control,
delete will fail with 'CBLErrorConflict' error code returned.
@param document The document.
@param concurrencyControl The concurrency control.
@param error On return, the error if any.
@return True on success, false on failure.
*/
- (BOOL) deleteDocument: (CBLDocument*)document
concurrencyControl: (CBLConcurrencyControl)concurrencyControl
error: (NSError**)error;
/**
Purges the given document from the database.
This is more drastic than deletion: it removes all traces of the document.
The purge will NOT be replicated to other databases.
@param document The document to be purged.
@param error On return, the error if any.
@return True on success, false on failure.
*/
- (BOOL) purgeDocument: (CBLDocument*)document error: (NSError**)error;
/**
Purges the document for the given documentID from the database.
This is more drastic than deletion: it removes all traces of the document.
The purge will NOT be replicated to other databases.
@param documentID The ID of the document to be purged.
@param error On return, the error if any.
@return True on success, false on failure.
*/
- (BOOL) purgeDocumentWithID: (NSString*)documentID error: (NSError**)error;
#pragma mark - Batch Operation
/**
Runs a group of database operations in a batch. Use this when performing bulk write operations
like multiple inserts/updates; it saves the overhead of multiple database commits, greatly
improving performance.
@param error On return, the error if any.
@param block The block to execute a group of database operations.
@return True on success, false on failure.
*/
- (BOOL) inBatch: (NSError**)error usingBlock: (void (NS_NOESCAPE ^)(void))block;
#pragma mark - Databaes Maintenance
/**
Closes a database.
@param error On return, the error if any.
@return True on success, false on failure.
*/
- (BOOL) close: (NSError**)error;
/**
Deletes a database.
@param error On return, the error if any.
@return True on success, false on failure.
*/
- (BOOL) delete: (NSError**)error;
/**
Compacts the database file by deleting unused attachment files and vacuuming
the SQLite database
@param error On return, the error if any.
@return True on success, false on failure.
*/
- (BOOL) compact: (NSError**)error;
/**
Deletes a database of the given name in the given directory.
@param name The database name.
@param directory The directory where the database is located at.
@param error On return, the error if any.
@return True on success, false on failure.
*/
+ (BOOL) deleteDatabase: (NSString*)name
inDirectory: (nullable NSString*)directory
error: (NSError**)error;
/**
Checks whether a database of the given name exists in the given directory or not.
@param name The database name.
@param directory The directory where the database is located at.
@return True on success, false on failure.
*/
+ (BOOL) databaseExists: (NSString*)name
inDirectory: (nullable NSString*)directory;
/**
Copies a canned databaes from the given path to a new database with the given name and
the configuration. The new database will be created at the directory specified in the
configuration. Without given the database configuration, the default configuration that
is equivalent to setting all properties in the configuration to nil will be used.
@param path The source database path.
@param name The name of the new database to be created.
@param config The database configuration for the new database.
@param error On return, the error if any.
@return True on success, false on failure.
*/
+ (BOOL) copyFromPath: (NSString*)path
toDatabase: (NSString*)name
withConfig: (nullable CBLDatabaseConfiguration*)config
error: (NSError**)error;
#pragma mark - Logging
/**
This function is deprecated. Use CBLDatabase.log.console to set log level and domains instead.
@param level The log level.
@param domain The log domain.
*/
+ (void) setLogLevel: (CBLLogLevel)level domain: (CBLLogDomain)domain
__attribute__((deprecated("Use CBLDatabase.log.console instead.")));
/**
Log object used for configuring console, file, and custom logger.
@return log object
*/
+ (CBLLog*) log;
#pragma mark - Change Listener
/**
Adds a database change listener. Changes will be posted on the main queue.
@param listener The listener to post the changes.
@return An opaque listener token object for removing the listener.
*/
- (id<CBLListenerToken>) addChangeListener: (void (^)(CBLDatabaseChange*))listener;
/**
Adds a database change listener with the dispatch queue on which changes
will be posted. If the dispatch queue is not specified, the changes will be
posted on the main queue.
@param queue The dispatch queue.
@param listener The listener to post changes.
@return An opaque listener token object for removing the listener.
*/
- (id<CBLListenerToken>) addChangeListenerWithQueue: (nullable dispatch_queue_t)queue
listener: (void (^)(CBLDatabaseChange*))listener;
/**
Adds a document change listener for the document with the given ID. Changes
will be posted on the main queue.
@param id The document ID.
@param listener The listener to post changes.
@return An opaque listener token object for removing the listener.
*/
- (id<CBLListenerToken>) addDocumentChangeListenerWithID: (NSString*)id
listener: (void (^)(CBLDocumentChange*))listener;
/**
Adds a document change listener for the document with the given ID and the
dispatch queue on which changes will be posted. If the dispatch queue
is not specified, the changes will be posted on the main queue.
@param id The document ID.
@param queue The dispatch queue.
@param listener The listener to post changes.
@return An opaque listener token object for removing the listener.
*/
- (id<CBLListenerToken>) addDocumentChangeListenerWithID: (NSString*)id
queue: (nullable dispatch_queue_t)queue
listener: (void (^)(CBLDocumentChange*))listener;
/**
Removes a change listener with the given listener token.
@param token The listener token.
*/
- (void) removeChangeListenerWithToken: (id<CBLListenerToken>)token;
#pragma mark - Index
/** All index names. */
@property (atomic, readonly) NSArray<NSString*>* indexes;
/**
Creates an index which could be a value index or a full-text search index with the given name.
The name can be used for deleting the index. Creating a new different index with an existing
index name will replace the old index; creating the same index with the same name will be no-ops.
@param index The index.
@param name The index name.
@param error error On return, the error if any.
@return True on success, false on failure.
*/
- (BOOL) createIndex: (CBLIndex*)index withName: (NSString*)name error: (NSError**)error;
/**
Deletes the index of the given index name.
@param name The index name.
@param error error On return, the error if any.
@return True on success, false on failure.
*/
- (BOOL) deleteIndexForName: (NSString*)name error: (NSError**)error;
#pragma mark - DOCUMENT EXPIRATION
/**
Sets an expiration date on a document.
After this time the document will be purged from the database.
@param documentID The ID of the document to set the expiration date for
@param date The expiration date. Set nil date will reset the document expiration.
@param error error On return, the error if any.
@return True on success, false on failure.
*/
- (BOOL) setDocumentExpirationWithID: (NSString*)documentID
expiration: (nullable NSDate*)date
error: (NSError**)error;
/**
Returns the expiration time of a document, if exists, else nil.
@param documentID The ID of the document to set the expiration date for
@return the expiration time of a document, if one has been set, else nil.
*/
- (nullable NSDate*) getDocumentExpirationWithID: (NSString*)documentID;
@end
NS_ASSUME_NONNULL_END