Skip to content

Commit 0b53484

Browse files
committed
Setup c_playground
1 parent 5c1b045 commit 0b53484

File tree

3 files changed

+175
-0
lines changed

3 files changed

+175
-0
lines changed

c_playground/CMakeLists.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
cmake_minimum_required(VERSION 3.15)
2+
3+
project(c_playground)
4+
5+
include_directories(${CMAKE_SOURCE_DIR}/../libcblite/include)
6+
7+
add_executable(Main main.c)
8+
9+
target_link_libraries(Main PUBLIC ${CMAKE_SOURCE_DIR}/../libcblite/lib/macos/libcblite.3.dylib)

c_playground/doc.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name":"Sam",
3+
"contacts":[
4+
{
5+
"type":"primary",
6+
"address":{"street":"1 St","city":"San Pedro","state":"CA"},
7+
"phones":[
8+
{"type":"home","number":"310-123-4567"},
9+
{"type":"mobile","number":"310-123-6789"}
10+
]
11+
},
12+
{
13+
"type":"secondary",
14+
"address":{"street":"5 St","city":"Seattle","state":"WA"},
15+
"phones":[
16+
{"type":"home","number":"206-123-4567"},
17+
{"type":"mobile","number":"206-123-6789"}
18+
]
19+
}
20+
],
21+
"likes":["soccer","travel"]
22+
}

c_playground/main.c

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
#include <assert.h>
2+
#include <stdio.h>
3+
#include <string.h>
4+
5+
#include <cbl/CBLCollection.h>
6+
#include <cbl/CBLDatabase.h>
7+
#include <cbl/CBLDocument.h>
8+
#include <cbl/CBLLog.h>
9+
#include <cbl/CBLQuery.h>
10+
11+
const char* DOMAINS[] = { "Database", "Query", "Replicator", "Network" };
12+
const char* LEVEL_PREFIX[] = { "((", "_", "", "WARNING: ", "***ERROR: " };
13+
const char* LEVEL_SUFFIX[] = { "))", "_", "", "", " ***" };
14+
15+
void log_callback(CBLLogDomain domain, CBLLogLevel level, FLString message) {
16+
printf(
17+
"CBL %s: %s%s%s\n",
18+
DOMAINS[domain],
19+
LEVEL_PREFIX[level],
20+
(char*) message.buf,
21+
LEVEL_SUFFIX[level]
22+
);
23+
}
24+
25+
int main(void) {
26+
CBLLog_SetCallbackLevel(kCBLLogVerbose);
27+
CBLLog_SetConsoleLevel(kCBLLogVerbose);
28+
CBLLog_SetCallback(log_callback);
29+
30+
// Open database
31+
CBLError error;
32+
CBLDatabaseConfiguration config = {FLSTR("/tmp")};
33+
CBLDatabase* db = CBLDatabase_Open(FLSTR("my_db"), &config, &error);
34+
assert(db);
35+
36+
CBLCollection* default_collection = CBLDatabase_DefaultCollection(db, &error);
37+
assert(default_collection);
38+
39+
// Create a document
40+
CBLDocument* doc = CBLDocument_Create();
41+
42+
FILE* fp = fopen("doc.json", "r");
43+
44+
char json_format[4096];
45+
int len = fread(json_format, 1, 4096, fp);
46+
47+
fclose(fp);
48+
49+
FLString json = {};
50+
json.buf = &json_format;
51+
json.size = len;
52+
bool set_doc_content = CBLDocument_SetJSON(doc, json, &error);
53+
54+
// Save the document
55+
bool saved = CBLDatabase_SaveDocument(db, doc, &error);
56+
assert(saved);
57+
58+
CBLDocument_Release(doc);
59+
60+
// Simple array index
61+
CBLArrayIndexConfiguration array_index_config = {};
62+
array_index_config.expressionLanguage = kCBLN1QLLanguage;
63+
array_index_config.path = FLSTR("likes");
64+
array_index_config.expressions = FLSTR("");
65+
66+
bool array_index_created = CBLCollection_CreateArrayIndex(
67+
default_collection,
68+
FLSTR("one_level"),
69+
array_index_config,
70+
&error
71+
);
72+
assert(array_index_created);
73+
74+
int error_pos = 0;
75+
CBLQuery* query = CBLDatabase_CreateQuery(
76+
db,
77+
kCBLN1QLLanguage,
78+
FLSTR("SELECT _.name, _like FROM _ UNNEST _.likes as _like WHERE _like = 'travel'"),
79+
&error_pos,
80+
&error
81+
);
82+
assert(query);
83+
84+
FLSliceResult explain_result = CBLQuery_Explain(query);
85+
assert(strstr(explain_result.buf, "USING INDEX one_level"));
86+
87+
CBLResultSet* query_result = CBLQuery_Execute(query, &error);
88+
assert(query_result);
89+
90+
assert(CBLResultSet_Next(query_result));
91+
92+
FLArray row = CBLResultSet_ResultArray(query_result);
93+
FLValue name = FLArray_Get(row, 0);
94+
assert(strcmp(FLValue_AsString(name).buf, "Sam") == 0);
95+
96+
assert(!CBLResultSet_Next(query_result));
97+
98+
CBLResultSet_Release(query_result);
99+
CBLQuery_Release(query);
100+
101+
// Complex array index
102+
array_index_config.expressionLanguage = kCBLN1QLLanguage;
103+
array_index_config.path = FLSTR("contacts[].phones");
104+
array_index_config.expressions = FLSTR("type");
105+
106+
array_index_created = CBLCollection_CreateArrayIndex(
107+
default_collection,
108+
FLSTR("two_level"),
109+
array_index_config,
110+
&error
111+
);
112+
assert(array_index_created);
113+
114+
query = CBLDatabase_CreateQuery(
115+
db,
116+
kCBLN1QLLanguage,
117+
FLSTR("SELECT _.name, contact.type, phone.number FROM _ UNNEST _.contacts as contact UNNEST contact.phones as phone WHERE phone.type = 'mobile'"),
118+
&error_pos,
119+
&error
120+
);
121+
assert(query);
122+
123+
explain_result = CBLQuery_Explain(query);
124+
assert(strstr(explain_result.buf, "USING INDEX two_level"));
125+
126+
query_result = CBLQuery_Execute(query, &error);
127+
assert(query_result);
128+
129+
assert(CBLResultSet_Next(query_result));
130+
131+
row = CBLResultSet_ResultArray(query_result);
132+
name = FLArray_Get(row, 0);
133+
assert(strcmp(FLValue_AsString(name).buf, "Sam") == 0);
134+
135+
assert(CBLResultSet_Next(query_result));
136+
assert(!CBLResultSet_Next(query_result));
137+
138+
CBLResultSet_Release(query_result);
139+
CBLQuery_Release(query);
140+
141+
// Cleanup
142+
bool closed = CBLDatabase_Delete(db, &error);
143+
assert(closed);
144+
}

0 commit comments

Comments
 (0)