-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
84 lines (69 loc) · 2.83 KB
/
main.cpp
File metadata and controls
84 lines (69 loc) · 2.83 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
#include <unistd.h>
#include "cbl/CouchbaseLite.h"
char app_service_url[] = "wss://YOUR_CAPELLA_APP_SERVICE_URL";
char app_service_user[] = "YOUR_APP_USER_NAME";
char app_service_password[] = "YOUR_APP_USER_PASSWORD";
int main() {
// Set logging level
CBLLog_SetConsoleLevel(kCBLLogVerbose);
// Error handling
CBLError err;
memset(&err, 0, sizeof(err));
// Connect to CBLite DB
CBLDatabase* database = CBLDatabase_Open(FLSTR("mydb"), NULL, &err);
if (!database) {
fprintf(stderr, "Error opening database (%d / %d)\n", err.domain, err.code);
FLSliceResult msg = CBLError_Message(&err);
fprintf(stderr, "%.*s\n", (int)msg.size, (const char *)msg.buf);
FLSliceResult_Release(msg);
return -1;
}
// Set collection
CBLCollection* collection = CBLDatabase_DefaultCollection(database, &err);
// Create a document with key "test::01"
CBLDocument* mutableDoc = CBLDocument_CreateWithID(FLSTR("test::01"));
FLMutableDict properties = CBLDocument_MutableProperties(mutableDoc);
FLMutableDict_SetFloat(properties, FLSTR("oldVersion"), 3.0);
if (!CBLCollection_SaveDocument(collection, mutableDoc, &err)) {
return -1;
}
// Update a document with key "test::01"
properties = CBLDocument_MutableProperties(mutableDoc);
FLMutableDict_SetString(properties, FLSTR("name"), FLSTR("apples"));
CBLCollection_SaveDocument(collection, mutableDoc, &err);
// Connection to remote endpoint
CBLEndpoint* targetEndpoint = CBLEndpoint_CreateWithURL(FLStr(app_service_url), &err);
if (!targetEndpoint) {
return -1;
}
// Configure the authentication
CBLReplicatorConfiguration replConfig;
CBLAuthenticator* basicAuth = CBLAuth_CreatePassword(FLStr(app_service_user), FLStr(app_service_password));
memset(&replConfig, 0, sizeof(replConfig));
replConfig.database = database;
replConfig.endpoint = targetEndpoint;
replConfig.authenticator = basicAuth;
// Create the replicator
CBLReplicator* replicator = CBLReplicator_Create(&replConfig, &err);
CBLAuth_Free(basicAuth);
CBLEndpoint_Free(targetEndpoint);
if (!replicator) {
return-1;
}
// Start the replicator
CBLReplicator_Start(replicator, false);
while (true) {
CBLReplicatorStatus status = CBLReplicator_Status(replicator);
if (status.activity == kCBLReplicatorStopped) {
break;
}
sleep(5);
}
// Read the document with the key "test::01" and print it
FLSliceResult json = CBLDocument_CreateJSON(mutableDoc);
printf("Document in JSON : %.*s\n", (int)json.size, (const char *)json.buf);
// Delete the document with the key "test::01" and finally release it
CBLCollection_DeleteDocument(collection, mutableDoc, &err);
CBLDocument_Release(mutableDoc);
return 0;
}