Connectors

Lua connector

Install, connect, query and handle errors with the Raft DB Lua connector (Lua 5.4 and LuaSocket).

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

Lua 5.4 and LuaSocket (require("socket")), the one dependency: Lua's standard library has no sockets. Ubuntu/Debian: sudo apt install lua5.4 lua-socket (LuaRocks: luarocks install luasocket).

Install#

Download the package from the Download page and unzip it. It holds raftdb.lua, raftdbjson.lua (a small JSON encoder and decoder, only needed if you want one) and a rockspec. Install with luarocks make raftdb-lua-connector-<version>-1.rockspec, or copy raftdb.lua next to your program or onto LUA_PATH.

Connect#

Lua
local raftdb = require("raftdb")
-- addr is "host:port" (the port defaults to 7480); {user=, password=}, {token=}, or none for an open server
local c = raftdb.connect("127.0.0.1:7480", {user = "admin", password = "pw"})

Query#

Lua
c:put("u1", '{"name":"Asha","age":31}')      -- -> existed (boolean)
local text = c:get("u1")                      -- JSON text, or nil if absent
local n = c:count('{"age":{"$gte":30}}')      -- "{}" = all
local rows, nxt = c:query('{"age":{"$gte":30}}', {limit = 50})   -- rows = array of JSON text; nxt 0 = done
for row in c:scan("SELECT name FROM documents ORDER BY id", {page = 500}) do print(row) end
c:close()

A page and an iterator (from examples.lua)#

Lua
local rows, nxt = c:query('{"city":"Pune"}', { limit = 10 })
print("page:", #rows, "next:", nxt)

c:create_index("age", true)
for row in c:scan("SELECT name, age FROM documents ORDER BY age", { page = 50 }) do
  print(row)
end

Errors#

Server errors are raised with error() of a table {code=, message=} with a __tostring ("Raft DB error 4: ..."); test with raftdb.is_raftdb_error(e); the codes are in raftdb.codes. Local I/O problems raise plain strings. After an I/O or AUTH error the connection is closed.

Lua
local ok, err = pcall(c.count, c, '{"age":')
if not ok and raftdb.is_raftdb_error(err) then print(err.code, err.message) end   -- 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; raftdb.lua contains no JSON code.
  • A connection is not thread-safe; do not interleave calls on one connection.
  • Not implemented: TLS, UDP transport, pipelining.

Conformance status#

24/24 conformance scenarios plus the dataset step on Lua 5.4 with LuaSocket 3.0.0 (Ubuntu); tested on Linux. The scenarios are listed in the specification page. The package's README.txt has the install steps and a short example.

See also#