Connectors

OCaml connector

Build, connect, query and handle errors with the Raft DB OCaml connector (Unix only).

The OCaml 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#

OCaml 4.14 or newer (tested on 5.4.0); the standard distribution (Unix). No opam packages. POSIX only.

Install#

Download the archive from the Download page and unpack it. It holds raftdb.ml, raftdb.mli, a dune file and an opam file:

opam pin add raftdb ./raftdb-ocaml-connector-<version>
ocamlfind ocamlopt -package unix -linkpkg raftdb.mli raftdb.ml myprog.ml -o myprog      # without dune

Connect#

OCaml
(* ~user ~password, or ~token, if the server needs them; ~timeout in seconds, default 30. *)
let db = Raftdb.connect "127.0.0.1:7480"

Query#

OCaml
let () =
  let db = Raftdb.connect "127.0.0.1:7480" in
  ignore (Raftdb.put db "u1" {|{"name":"Asha","age":31}|});
  (match Raftdb.get db "u1" with Some json -> print_endline json | None -> ());
  Printf.printf "%d\n" (Raftdb.count db {|{"age":{"$gte":30}}|});
  Raftdb.scan db "SELECT name FROM documents ORDER BY name" print_endline;
  Raftdb.close db

One page of rows (from example.ml)#

OCaml
let rows, next = Raftdb.query ~limit:1 db "{}" in
Printf.printf "page of %d row(s), next cursor %Ld\n" (List.length rows) next

Errors#

Raftdb.Error (code, message) is a server refusal. Raftdb.Io_error message is a socket problem, a timeout, a closed connection or a malformed reply; the connection is closed afterwards. Invalid_argument is raised for a bad address, a negative limit or a request over 16 MiB.

OCaml
try ignore (Raftdb.count db {|{"age":|})
with Raftdb.Error (code, msg) -> Printf.printf "%d %s\n" code msg   (* 4 ... *)

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#

  • Documents, filters and rows are JSON text (string): the library ships no JSON parser.
  • A connection is not thread-safe: use one per thread. SIGPIPE is set to ignore on connect.
  • next = 0L means no more rows. Not supported: UDP transport; Windows is not a target.

Conformance status#

24/24 conformance scenarios on Linux (OCaml 5.4.0). The scenarios are listed in the specification page. The package's README.txt has the install steps and a short example.

See also#