Skip to content

Commit 4530fdc

Browse files
authored
couchbase-lite-C 3.2.1 (#19)
This pull request upgrades the repo to `couchbase-lite-C` 3.2.1. > [!WARNING] > This release is not backward compatible with `couchbase-lite-C` 3.1. Mainly, this new versions adds support for [Array Indexes](https://docs.couchbase.com/couchbase-lite/current/c/indexing.html#array-indexing).
1 parent 0302d50 commit 4530fdc

File tree

139 files changed

+4132
-1107
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

139 files changed

+4132
-1107
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
/target/
22
Cargo.lock
3+
4+
.vscode

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
[package]
22
name = "couchbase_lite"
3-
version = "3.1.10-0"
3+
version = "3.2.1-0"
44
# The first three numbers correspond to the Couchbase Lite C release, the fourth number corresponds to the Doctolib release
5+
edition = "2021"
56

67
[dependencies]
78
enum_primitive = "*"

c_playground/.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
CMakeCache.txt
2+
CMakeFiles
3+
Makefile
4+
cmake_install.cmake
5+
6+
Main

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/README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
## Purpose of the c_playground
2+
3+
Couchbase does not provide support for Rust bindings, so we cannot communicate code from this repo in support tickets.
4+
5+
With the c_playground, you can code whatever you want in C with full access to the couchbase-lite-C API.
6+
7+
## How to use it
8+
9+
The code in main.c will be compiled.
10+
11+
In the c_payground repository, you need to run the command once:
12+
13+
```
14+
cmake CMakeLists.txt
15+
```
16+
17+
You will then be able to compile the code anytime you want with the command:
18+
19+
```
20+
make
21+
```
22+
23+
The file Main is the executable, do not forget to set the execution right for yourself:
24+
25+
```
26+
chmod u+x ./Main
27+
```
28+
29+
Execute your code:
30+
31+
```
32+
./Main
33+
```

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+
}

libcblite-3.0.3/.DS_Store

0 Bytes
Binary file not shown.

libcblite-3.0.3/include/.DS_Store

0 Bytes
Binary file not shown.

libcblite-3.0.3/include/cbl++/Base.hh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ namespace cbl {
9191

9292
CBLRefCounted* _cbl_nullable _ref;
9393

94+
friend class Extension;
9495
friend class Transaction;
9596
};
9697

libcblite-3.0.3/include/cbl++/Collection.hh

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#pragma once
2020
#include "cbl++/Base.hh"
21+
#include "cbl++/Database.hh"
2122
#include "cbl/CBLCollection.h"
2223
#include "cbl/CBLScope.h"
2324
#include "fleece/Mutable.hh"
@@ -35,6 +36,8 @@ namespace cbl {
3536
class MutableDocument;
3637
class CollectionChange;
3738
class DocumentChange;
39+
class QueryIndex;
40+
class VectorIndexConfiguration;
3841

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

64-
/** The collection name. */
65-
std::string name() const {return asString(CBLCollection_Name(ref()));}
67+
/** The collection's name. */
68+
std::string name() const {return asString(CBLCollection_Name(ref()));}
6669

67-
/** The scope name. */
70+
/** The collection's fully qualified name in the '<scope-name>.<collection-name>' format. */
71+
std::string fullName() const {return asString(CBLCollection_FullName(ref()));}
72+
73+
/** The scope's name. */
6874
std::string scopeName() const {
6975
auto scope = CBLCollection_Scope(ref());
7076
auto scopeName = asString(CBLScope_Name(scope));
7177
CBLScope_Release(scope);
7278
return scopeName;
7379
}
7480

81+
/** The collection's database. */
82+
Database database() const {return Database(CBLCollection_Database(ref()));}
83+
7584
/** The number of documents in the collection. */
76-
uint64_t count() const {return CBLCollection_Count(ref());}
85+
uint64_t count() const {return CBLCollection_Count(ref());}
7786

7887
// Documents:
7988

@@ -190,6 +199,28 @@ namespace cbl {
190199
CBLError error;
191200
check(CBLCollection_CreateFullTextIndex(ref(), name, config, &error), error);
192201
}
202+
203+
/** Creates an array index for use with UNNEST queries in the collection.
204+
Indexes are persistent.
205+
If an identical index with that name already exists, nothing happens (and no error is returned.)
206+
If a non-identical index with that name already exists, it is deleted and re-created.
207+
@param name The index name.
208+
@param config The array index config. */
209+
void createArrayIndex(slice name, CBLArrayIndexConfiguration config) {
210+
CBLError error;
211+
check(CBLCollection_CreateArrayIndex(ref(), name, config, &error), error);
212+
}
213+
214+
#ifdef COUCHBASE_ENTERPRISE
215+
/** ENTERPRISE EDITION ONLY
216+
217+
Creatres a vector index in the collection.
218+
If an identical index with that name already exists, nothing happens (and no error is returned.)
219+
If a non-identical index with that name already exists, it is deleted and re-created.
220+
@param name The index name.
221+
@param config The vector index config. */
222+
inline void createVectorIndex(slice name, const VectorIndexConfiguration &config);
223+
#endif
193224

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

241+
/** Get an index by name. If the index doesn't exist, the NULL QueryIndex object will be returned. */
242+
inline QueryIndex getIndex(slice name);
243+
210244
// Listeners:
211245

212246
/** Collection Change Listener Token */
@@ -250,6 +284,7 @@ namespace cbl {
250284

251285
friend class Database;
252286
friend class Document;
287+
friend class QueryIndex;
253288

254289
CBL_REFCOUNTED_BOILERPLATE(Collection, RefCounted, CBLCollection);
255290

@@ -311,6 +346,23 @@ namespace cbl {
311346
Collection _collection;
312347
slice _docID;
313348
};
349+
350+
// Database method bodies:
351+
352+
inline Collection Database::getCollection(slice collectionName, slice scopeName) const {
353+
CBLError error {};
354+
return Collection::adopt(CBLDatabase_Collection(ref(), collectionName, scopeName, &error), &error) ;
355+
}
356+
357+
inline Collection Database::createCollection(slice collectionName, slice scopeName) {
358+
CBLError error {};
359+
return Collection::adopt(CBLDatabase_CreateCollection(ref(), collectionName, scopeName, &error), &error) ;
360+
}
361+
362+
inline Collection Database::getDefaultCollection() const {
363+
CBLError error {};
364+
return Collection::adopt(CBLDatabase_DefaultCollection(ref(), &error), &error) ;
365+
}
314366
}
315367

316368
/** Hash function for Collection. */

libcblite-3.0.3/include/cbl++/CouchbaseLite.hh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,8 @@
2424
#include "Collection.hh"
2525
#include "Database.hh"
2626
#include "Document.hh"
27+
#include "Prediction.hh"
2728
#include "Query.hh"
29+
#include "QueryIndex.hh"
2830
#include "Replicator.hh"
31+
#include "VectorIndex.hh"

0 commit comments

Comments
 (0)