Connectors

Elixir connector

Install, connect, query and handle errors with the Raft DB Elixir connector (OTP and the built-in JSON module).

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

Elixir 1.18 or newer. Only OTP :gen_tcp and Elixir's built-in JSON module; no Hex packages.

Install#

Download the package from the Download page and unpack it into a folder. No package registry is needed. Add a path dependency in your mix.exs:

Elixir
def deps do
  [{:raftdb, path: "../raftdb-elixir-connector"}]
end

Connect#

Elixir
{:ok, db} = RaftDB.connect("127.0.0.1:7480")                         # open server
{:ok, db} = RaftDB.connect("host:7480", user: "admin", password: "pw") # accounts
{:ok, db} = RaftDB.connect("host:7480", token: "secret")              # shared token

Query#

Elixir
{:ok, false} = RaftDB.put(db, "u1", %{"name" => "Asha", "age" => 31})   # existed?
{:ok, %{"name" => "Asha"}} = RaftDB.get(db, "u1")                       # {:ok, nil} if absent
{:ok, 1} = RaftDB.count(db, %{"age" => %{"$gte" => 30}})
{:ok, %{rows: rows, next: next}} = RaftDB.query(db, %{}, limit: 50)
RaftDB.scan(db, "SELECT id FROM documents ORDER BY id") |> Enum.take(10)
RaftDB.close(db)

Bang variants and a lazy scan (from examples.exs)#

Elixir
%{rows: rows, next: next} = RaftDB.query!(db, %{}, limit: 1)
IO.inspect({rows, next}, label: "first page")

db
|> RaftDB.scan("SELECT name FROM documents ORDER BY name", page_size: 100)
|> Enum.each(&IO.inspect(&1, label: "row"))

Errors#

Server errors are {:error, %RaftDB.Error{code: code, message: message}}. Network failures are {:error, {:io, reason}} and close the connection; after close/1 every call returns {:error, {:io, :closed}}. Bang variants (get!/2, ...) return the bare value or raise.

Elixir
{:error, %RaftDB.Error{code: code}} = RaftDB.count(db, ~s({"age":))
IO.puts("broken filter -> error code #{code}")

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#

  • The socket belongs to the process that called connect/2 and is used directly from it; there is no GenServer. A connection is not safe for concurrent use: open one per process.
  • scan returns a lazy Stream; every function except scan and close has a bang variant.
  • Not supported: the optional UDP transport.

Conformance status#

24/24 conformance scenarios against a running Raft DB server (Elixir 1.18.3 / OTP 27, Linux); ExUnit reports 25 tests, 0 failures, because the file also has a dataset-loading test. The scenarios are listed in the specification page. The package's README.txt has the install steps and a short example.

See also#