C connector
Build, connect, query, free memory and handle errors with the Raft DB C connector (C99, POSIX).
The C connector speaks the Raft DB binary protocol (specification) and offers the same operations as every other connector. Download the package from the Download page.
No TLS yet. User names, passwords and tokens travel in clear text. Use only on trusted networks.
Requirements#
C99 and POSIX sockets, libc only: no other library, no JSON code. POSIX only (Linux, macOS, BSD); Windows is not a target.
Install#
Download the package from the Download page and unzip it. It holds include/raftdb.h and src/raftdb.c (about 600 lines) and a README.txt; there is no compiled library because the connector uses POSIX sockets and is compiled with your program (cc -std=c99 -Iinclude -c src/raftdb.c).
Connect#
#include "raftdb.h"
#include <stdio.h>
raftdb_client *db = NULL;
int rc = raftdb_connect("127.0.0.1:7480", NULL, NULL, NULL, &db); /* user, password, token */
if (rc) { fprintf(stderr, "%d: %s\n", rc, raftdb_error_message(db)); raftdb_close(db); return 1; }Query#
int existed;
raftdb_put_json(db, "u1", "{\"name\": \"Asha\", \"age\": 31}", &existed);
char *json;
if (raftdb_get_json(db, "u1", &json, NULL) == 0 && json) { puts(json); raftdb_free(json); }
uint64_t n;
raftdb_count(db, "{\"age\": {\"$gte\": 30}}", &n);Paging through a SQL query (from example.c)#
char *rows; uint64_t next = 0, cursor = 0;
do { /* page through a SQL query */
if (raftdb_query_sql(db, "SELECT id FROM documents ORDER BY id", 100, cursor, &rows, &next)) break;
puts(rows); /* "[{...},{...}]" */
raftdb_free(rows);
cursor = next;
} while (next != 0);
raftdb_close(db);Errors#
After RAFTDB_L_IO, RAFTDB_L_PROTO and RAFTDB_E_AUTH the connection is closed and later calls return RAFTDB_L_CLOSED; other server errors leave it usable. The text of the last failure is raftdb_error_message(c).
/* every function returns an int: 0 = success; positive = the server's error code
(RAFTDB_E_BAD_FILTER = 4, RAFTDB_E_SCHEMA = 2, RAFTDB_E_AUTH = 7, RAFTDB_E_NOT_FOUND = 10, ...);
negative = a local failure (RAFTDB_L_IO, RAFTDB_L_PROTO, RAFTDB_L_NOMEM, RAFTDB_L_CLOSED, RAFTDB_L_ARG, RAFTDB_L_TOOBIG) */
int rc = raftdb_count(db, "{\"age\":", &n);
if (rc > 0) printf("server code %d: %s\n", raftdb_server_code(db), raftdb_error_message(db));Transactions and prepared statements#
The connector has no transaction call and no prepared-statement call: the protocol has none (every operation is one request and one reply, see the specification), and none of the connector's README, tests or examples uses one. Use the MySQL or MariaDB front-end with a standard driver for those: see Standard MySQL and MariaDB drivers.
Notes#
- Every
char *the library hands out (raftdb_get_json,raftdb_query_*) is malloc'd: free it withraftdb_free(). Callraftdb_close()on the client exactly once; ifraftdb_connectfails,*outis a dead client carrying the message, so read it and close it. - Keys, filters, SQL and documents are NUL-terminated UTF-8 C strings (a key cannot contain a zero byte). Rows come back as JSON text.
raftdb_scan(c, filter_or_sql, page_size, fn, user)callsfn(row, len, user)for every row; rows are not NUL-terminated.- Not supported: UDP transport, thread safety (one
raftdb_clientper thread).
Conformance status#
24/24 conformance scenarios on Linux, also under ASan and UBSan. The scenarios are listed in the specification page. The package's README.txt has the install steps and a short example.