-
Notifications
You must be signed in to change notification settings - Fork 0
couchbase-lite-C 3.2.1 #19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 12 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
8a81078
Android
Nao-ris f78b0e2
iOS
Nao-ris 4a92df4
macOS & Windows
Nao-ris 37dfd28
Ubuntu
Nao-ris ecce13a
include
Nao-ris b2c1960
Strip libraries
Nao-ris 5255342
Scope & Collection: add new api & fix unit tests
Nao-ris 5c1b045
Add support for array indexes
Nao-ris 0b53484
Setup c_playground
Nao-ris 3a9a098
Unit test passing locally
Nao-ris 3dc1841
Better ownership & Remove unwrap
Nao-ris 4da5c2d
Change package version
Nao-ris 9152929
c_playground README & .gitignore update
Nao-ris 30296eb
Better documentation
Nao-ris 7337b3e
Fix ut compilation error
Nao-ris File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
cmake_minimum_required(VERSION 3.15) | ||
|
||
project(c_playground) | ||
|
||
include_directories(${CMAKE_SOURCE_DIR}/../libcblite/include) | ||
|
||
add_executable(Main main.c) | ||
|
||
target_link_libraries(Main PUBLIC ${CMAKE_SOURCE_DIR}/../libcblite/lib/macos/libcblite.3.dylib) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"name":"Sam", | ||
"contacts":[ | ||
{ | ||
"type":"primary", | ||
"address":{"street":"1 St","city":"San Pedro","state":"CA"}, | ||
"phones":[ | ||
{"type":"home","number":"310-123-4567"}, | ||
{"type":"mobile","number":"310-123-6789"} | ||
] | ||
}, | ||
{ | ||
"type":"secondary", | ||
"address":{"street":"5 St","city":"Seattle","state":"WA"}, | ||
"phones":[ | ||
{"type":"home","number":"206-123-4567"}, | ||
{"type":"mobile","number":"206-123-6789"} | ||
] | ||
} | ||
], | ||
"likes":["soccer","travel"] | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
#include <assert.h> | ||
#include <stdio.h> | ||
#include <string.h> | ||
|
||
#include <cbl/CBLCollection.h> | ||
#include <cbl/CBLDatabase.h> | ||
#include <cbl/CBLDocument.h> | ||
#include <cbl/CBLLog.h> | ||
#include <cbl/CBLQuery.h> | ||
|
||
const char* DOMAINS[] = { "Database", "Query", "Replicator", "Network" }; | ||
const char* LEVEL_PREFIX[] = { "((", "_", "", "WARNING: ", "***ERROR: " }; | ||
const char* LEVEL_SUFFIX[] = { "))", "_", "", "", " ***" }; | ||
|
||
void log_callback(CBLLogDomain domain, CBLLogLevel level, FLString message) { | ||
printf( | ||
"CBL %s: %s%s%s\n", | ||
DOMAINS[domain], | ||
LEVEL_PREFIX[level], | ||
(char*) message.buf, | ||
LEVEL_SUFFIX[level] | ||
); | ||
} | ||
|
||
int main(void) { | ||
CBLLog_SetCallbackLevel(kCBLLogVerbose); | ||
CBLLog_SetConsoleLevel(kCBLLogVerbose); | ||
CBLLog_SetCallback(log_callback); | ||
|
||
// Open database | ||
CBLError error; | ||
CBLDatabaseConfiguration config = {FLSTR("/tmp")}; | ||
CBLDatabase* db = CBLDatabase_Open(FLSTR("my_db"), &config, &error); | ||
assert(db); | ||
|
||
CBLCollection* default_collection = CBLDatabase_DefaultCollection(db, &error); | ||
assert(default_collection); | ||
|
||
// Create a document | ||
CBLDocument* doc = CBLDocument_Create(); | ||
|
||
FILE* fp = fopen("doc.json", "r"); | ||
|
||
char json_format[4096]; | ||
int len = fread(json_format, 1, 4096, fp); | ||
|
||
fclose(fp); | ||
|
||
FLString json = {}; | ||
json.buf = &json_format; | ||
json.size = len; | ||
bool set_doc_content = CBLDocument_SetJSON(doc, json, &error); | ||
|
||
// Save the document | ||
bool saved = CBLDatabase_SaveDocument(db, doc, &error); | ||
assert(saved); | ||
|
||
CBLDocument_Release(doc); | ||
|
||
// Simple array index | ||
CBLArrayIndexConfiguration array_index_config = {}; | ||
array_index_config.expressionLanguage = kCBLN1QLLanguage; | ||
array_index_config.path = FLSTR("likes"); | ||
array_index_config.expressions = FLSTR(""); | ||
|
||
bool array_index_created = CBLCollection_CreateArrayIndex( | ||
default_collection, | ||
FLSTR("one_level"), | ||
array_index_config, | ||
&error | ||
); | ||
assert(array_index_created); | ||
|
||
int error_pos = 0; | ||
CBLQuery* query = CBLDatabase_CreateQuery( | ||
db, | ||
kCBLN1QLLanguage, | ||
FLSTR("SELECT _.name, _like FROM _ UNNEST _.likes as _like WHERE _like = 'travel'"), | ||
&error_pos, | ||
&error | ||
); | ||
assert(query); | ||
|
||
FLSliceResult explain_result = CBLQuery_Explain(query); | ||
assert(strstr(explain_result.buf, "USING INDEX one_level")); | ||
|
||
CBLResultSet* query_result = CBLQuery_Execute(query, &error); | ||
assert(query_result); | ||
|
||
assert(CBLResultSet_Next(query_result)); | ||
|
||
FLArray row = CBLResultSet_ResultArray(query_result); | ||
FLValue name = FLArray_Get(row, 0); | ||
assert(strcmp(FLValue_AsString(name).buf, "Sam") == 0); | ||
|
||
assert(!CBLResultSet_Next(query_result)); | ||
|
||
CBLResultSet_Release(query_result); | ||
CBLQuery_Release(query); | ||
|
||
// Complex array index | ||
array_index_config.expressionLanguage = kCBLN1QLLanguage; | ||
array_index_config.path = FLSTR("contacts[].phones"); | ||
array_index_config.expressions = FLSTR("type"); | ||
|
||
array_index_created = CBLCollection_CreateArrayIndex( | ||
default_collection, | ||
FLSTR("two_level"), | ||
array_index_config, | ||
&error | ||
); | ||
assert(array_index_created); | ||
|
||
query = CBLDatabase_CreateQuery( | ||
db, | ||
kCBLN1QLLanguage, | ||
FLSTR("SELECT _.name, contact.type, phone.number FROM _ UNNEST _.contacts as contact UNNEST contact.phones as phone WHERE phone.type = 'mobile'"), | ||
&error_pos, | ||
&error | ||
); | ||
assert(query); | ||
|
||
explain_result = CBLQuery_Explain(query); | ||
assert(strstr(explain_result.buf, "USING INDEX two_level")); | ||
|
||
query_result = CBLQuery_Execute(query, &error); | ||
assert(query_result); | ||
|
||
assert(CBLResultSet_Next(query_result)); | ||
|
||
row = CBLResultSet_ResultArray(query_result); | ||
name = FLArray_Get(row, 0); | ||
assert(strcmp(FLValue_AsString(name).buf, "Sam") == 0); | ||
|
||
assert(CBLResultSet_Next(query_result)); | ||
assert(!CBLResultSet_Next(query_result)); | ||
|
||
CBLResultSet_Release(query_result); | ||
CBLQuery_Release(query); | ||
|
||
// Cleanup | ||
bool closed = CBLDatabase_Delete(db, &error); | ||
assert(closed); | ||
} |
Binary file not shown.
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.