jett / couchbase-lite-C

C language bindings for the Couchbase Lite embedded NoSQL database engine

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Couchbase Lite For C

This is a cross-platform version of the Couchbase Lite embedded NoSQL syncable database, with a plain C API. The API can be used directly, or as the substrate for binding to other languages like Python, JavaScript or Rust.

What's New (May 2020)

This project is close to beta status. The API is nearly complete and almost all of the functionality is implemented, but there are still missing pieces and only limited testing.

  • New Rust and Nim language bindings! They're in the new top-level bindings directory. The Python binding has been moved there too.
  • Added variants of many API functions, which take slices (FLSlice) instead of C strings. These are more efficient to call from languages whose native string type is not NUL-terminated, such as Rust.
  • Added CBLResultSet_RowArray(), CBLResultSet_RowDict(), and CBLResultSet_GetQuery().
  • More of the logging API (in CBLLog.h) is implemented, including custom log callbacks.
  • Updated to latest Couchbase Lite Core (LiteCore).

Goals

  • C API
    • Similar to to other Couchbase Lite platforms (Java, C#, Swift, Objective-C)
    • Clean and regular design
    • Comes with a C++ wrapper API, implemented as inline calls to C
    • Experimental Python binding (made using cffi)
    • Can be bound to other languages like Go or JavaScript
  • Same feature set as other Couchbase Lite platforms
    • Schemaless JSON data model
      • Standard CRUD operations
      • Efficient binary blob support
      • Timed document expiration
      • Database encryption (Enterprise Edition only)
    • Powerful query language based on Couchbase's N1QL
      • Index arbitrary JSON properties or derived expression values
      • Full-text search (FTS)
    • Multi-master bidirectional replication (sync) with Couchbase Server
      • Fast WebSocket-based protocol
      • Transfers document deltas for minimal bandwidth
      • Replicator event listeners
      • Replicator online/offline support and retry behavior
      • Replicator TLS/SSL support
      • Peer-to-peer replication
  • Minimal platform dependencies: C++ standard library, filesystem, TCP/IP
  • Broad OS support
    • macOS, for ease of development
    • Common Linux distros, esp. Ubuntu, Fedora, Raspbian (q.v.)
    • Windows
    • iOS
    • Android (but we have a Couchbase Lite For Android already, with a Java API)
  • Runs on Raspberry-Pi-level embedded platforms with…
    • 32-bit or 64-bit CPU
    • ARM or x86
    • Hundreds of MB RAM, hundreds of MHz CPU, tens of MB storage
    • Linux-based OS
    • Stretch goal: Simpler embedded kernels like mbedOS or ESP-IDF.

Examples

C

// Open a database:
CBLError error;
CBLDatabaseConfiguration config = {"/tmp", kCBLDatabase_Create};
CBLDatabase* db = CBLDatabase_Open("my_db", &config, &error);

// Create a document:
CBLDocument* doc = CBLDocument_New("foo");
FLMutableDict props = CBLDocument_MutableProperties(doc);
FLSlot_SetString(FLMutableDict_Set(dict, FLStr("greeting")), FLStr("Howdy!"));

// Save the document:
const CBLDocument *saved = CBLDatabase_SaveDocument(db, doc,
                                         kCBLConcurrencyControlFailOnConflict,
                                         &error);
CBLDocument_Release(saved);
CBLDocument_Release(doc);

// Read it back:
const CBLDocument *readDoc = CBLDatabase_GetDocument(db, "foo");
FLDict readProps = CBLDocument_Properties(readDoc);
FLSlice greeting = FLValue_AsString( FLDict_Get(readProps, FLStr("greeting")) );
CBLDocument_Release(readDoc);

C++

// Open a database:
cbl::Database db(kDatabaseName, {"/tmp", kCBLDatabase_Create});

// Create a document:
cbl::MutableDocument doc("foo");
doc["greeting"] = "Howdy!";
db.saveDocument(doc);

// Read it back:
cbl::Document doc = db.getMutableDocument("foo");
fleece::Dict readProps = doc->properties();
fleece::slice greeting = readProps["greeting"].asString();

Nim

# Open a database:
let config = DatabaseConfiguration(directory: "/tmp", flags: {DatabaseFlag.create})
var db = openDatabase("nim_db", config)

# Create a document:
var doc = newDocument("foo")
doc["greeting"] = "Howdy!"
db.saveDocument(doc)

# Read it back:
let readDoc = db.getDocument("foo")
let readProps = readDoc.properties
let greeting = readProps["greeting"]

Python

# Open a database:
db = Database("python_db", DatabaseConfiguration("/tmp"));

# Create a document:
doc = MutableDocument("foo")
doc["greeting"] = "Howdy!"
db.saveDocument(doc)

# Read it back:
readDoc = db.getDocument("foo")
readProps = readDoc.properties
greeting = readProps["greeting"]

Rust

// Open a database:
let cfg = DatabaseConfiguration{directory: tmp_dir.path(), flags: CREATE};
let mut db = Database::open("rust_db, Some(cfg)).expect("opening db");

// Create a document:
let mut doc = Document::new_with_id("foo");
let mut props = doc.mutable_properties();
props.at("greeting").put_string("Howdy!");
db.save_document(&mut doc, ConcurrencyControl::FailOnConflict).expect("saving");

// Read it back:
let doc = db.get_document("foo").expect("reload document");
let props = doc.properties();
let greeting = props.get("greeting");

Documentation

Building It

With CMake on Unix (now including Raspberry Pi!)

Dependencies:

  • GCC 7+ or Clang
  • CMake 3.9+
  • ICU libraries (apt-get install icu-dev)
  1. Clone the repo
  2. Check out submodules (recursively), i.e. git submodule update --init --recursive
  3. Run the shellscript build.sh
  4. Run the unit tests, with test.sh

The library is at build_cmake/libCouchbaseLiteC.so. (Or .DLL or .dylib)

With CMake on Windows

(Much like building on Unix. Details TBD)

With Xcode on macOS

  1. Clone the repo
  2. Check out submodules (recursively)
  3. Open the Xcode project in the Xcode subfolder
  4. Select scheme CBL_C Framework
  5. Build

The result is CouchbaseLite.framework in your Xcode Build Products/Debug directory (the path depends on your Xcode settings.)

To run the unit tests:

  1. Select scheme CBL_Tests
  2. Run

Using It

Generic instructions

  • In your build settings, add the paths include and vendor/couchbase-lite-core/vendor/fleece/API (relative to this repo) to your header search paths.
  • In your linker settings, add the dynamic library.
  • In source files, add #include "cbl/CouchbaseLite.h".

With Xcode on macOS

  • Add CouchbaseLite.framework (see instructions above) to your project.
  • In source files, add #include "CouchbaseLite/CouchbaseLite.h".
  • You may need to add a build step to your target, to copy the framework into your app bundle.

Other Language Bindings

About

C language bindings for the Couchbase Lite embedded NoSQL database engine

License:Apache License 2.0


Languages

Language:C++ 34.1%Language:Nim 26.4%Language:C 14.6%Language:Rust 14.6%Language:Python 6.9%Language:CMake 1.9%Language:Shell 0.9%Language:Objective-C++ 0.4%Language:PowerShell 0.2%