Skip to content

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 15 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
[package]
name = "couchbase_lite"
version = "3.1.10-0"
version = "3.2.1-0"
# The first three numbers correspond to the Couchbase Lite C release, the fourth number corresponds to the Doctolib release
edition = "2021"

[dependencies]
enum_primitive = "*"
Expand Down
9 changes: 9 additions & 0 deletions c_playground/CMakeLists.txt
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)
22 changes: 22 additions & 0 deletions c_playground/doc.json
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"]
}
144 changes: 144 additions & 0 deletions c_playground/main.c
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 modified libcblite-3.0.3/.DS_Store
Binary file not shown.
Binary file modified libcblite-3.0.3/include/.DS_Store
Binary file not shown.
1 change: 1 addition & 0 deletions libcblite-3.0.3/include/cbl++/Base.hh
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ namespace cbl {

CBLRefCounted* _cbl_nullable _ref;

friend class Extension;
friend class Transaction;
};

Expand Down
60 changes: 56 additions & 4 deletions libcblite-3.0.3/include/cbl++/Collection.hh
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#pragma once
#include "cbl++/Base.hh"
#include "cbl++/Database.hh"
#include "cbl/CBLCollection.h"
#include "cbl/CBLScope.h"
#include "fleece/Mutable.hh"
Expand All @@ -35,6 +36,8 @@ namespace cbl {
class MutableDocument;
class CollectionChange;
class DocumentChange;
class QueryIndex;
class VectorIndexConfiguration;

/** Conflict handler used when saving a document. */
using CollectionConflictHandler = std::function<bool(MutableDocument documentBeingSaved,
Expand All @@ -61,19 +64,25 @@ namespace cbl {
public:
// Accessors:

/** The collection name. */
std::string name() const {return asString(CBLCollection_Name(ref()));}
/** The collection's name. */
std::string name() const {return asString(CBLCollection_Name(ref()));}

/** The scope name. */
/** The collection's fully qualified name in the '<scope-name>.<collection-name>' format. */
std::string fullName() const {return asString(CBLCollection_FullName(ref()));}

/** The scope's name. */
std::string scopeName() const {
auto scope = CBLCollection_Scope(ref());
auto scopeName = asString(CBLScope_Name(scope));
CBLScope_Release(scope);
return scopeName;
}

/** The collection's database. */
Database database() const {return Database(CBLCollection_Database(ref()));}

/** The number of documents in the collection. */
uint64_t count() const {return CBLCollection_Count(ref());}
uint64_t count() const {return CBLCollection_Count(ref());}

// Documents:

Expand Down Expand Up @@ -190,6 +199,28 @@ namespace cbl {
CBLError error;
check(CBLCollection_CreateFullTextIndex(ref(), name, config, &error), error);
}

/** Creates an array index for use with UNNEST queries in the collection.
Indexes are persistent.
If an identical index with that name already exists, nothing happens (and no error is returned.)
If a non-identical index with that name already exists, it is deleted and re-created.
@param name The index name.
@param config The array index config. */
void createArrayIndex(slice name, CBLArrayIndexConfiguration config) {
CBLError error;
check(CBLCollection_CreateArrayIndex(ref(), name, config, &error), error);
}

#ifdef COUCHBASE_ENTERPRISE
/** ENTERPRISE EDITION ONLY

Creatres a vector index in the collection.
If an identical index with that name already exists, nothing happens (and no error is returned.)
If a non-identical index with that name already exists, it is deleted and re-created.
@param name The index name.
@param config The vector index config. */
inline void createVectorIndex(slice name, const VectorIndexConfiguration &config);
#endif

/** Deletes an index given its name from the collection. */
void deleteIndex(slice name) {
Expand All @@ -207,6 +238,9 @@ namespace cbl {
return names;
}

/** Get an index by name. If the index doesn't exist, the NULL QueryIndex object will be returned. */
inline QueryIndex getIndex(slice name);

// Listeners:

/** Collection Change Listener Token */
Expand Down Expand Up @@ -250,6 +284,7 @@ namespace cbl {

friend class Database;
friend class Document;
friend class QueryIndex;

CBL_REFCOUNTED_BOILERPLATE(Collection, RefCounted, CBLCollection);

Expand Down Expand Up @@ -311,6 +346,23 @@ namespace cbl {
Collection _collection;
slice _docID;
};

// Database method bodies:

inline Collection Database::getCollection(slice collectionName, slice scopeName) const {
CBLError error {};
return Collection::adopt(CBLDatabase_Collection(ref(), collectionName, scopeName, &error), &error) ;
}

inline Collection Database::createCollection(slice collectionName, slice scopeName) {
CBLError error {};
return Collection::adopt(CBLDatabase_CreateCollection(ref(), collectionName, scopeName, &error), &error) ;
}

inline Collection Database::getDefaultCollection() const {
CBLError error {};
return Collection::adopt(CBLDatabase_DefaultCollection(ref(), &error), &error) ;
}
}

/** Hash function for Collection. */
Expand Down
3 changes: 3 additions & 0 deletions libcblite-3.0.3/include/cbl++/CouchbaseLite.hh
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,8 @@
#include "Collection.hh"
#include "Database.hh"
#include "Document.hh"
#include "Prediction.hh"
#include "Query.hh"
#include "QueryIndex.hh"
#include "Replicator.hh"
#include "VectorIndex.hh"
Loading