Connectors

Ruby connector

Install, connect, query and handle errors with the Raft DB Ruby connector (standard library only).

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

Ruby 3.2 or newer (tested on 3.3), standard library only (socket, json).

Install#

Download the gem from the Download page and install it:

Shell
gem install ./raftdb-ruby-connector-<version>.gem

Then require "raftdb".

Connect#

Ruby
require "raftdb"

# user:, password: or token: if the server needs them; timeout: 30.0 by default
RaftDB::Client.connect("127.0.0.1:7480") do |db|
  db.ping
end

Query#

Ruby
RaftDB::Client.connect("127.0.0.1:7480") do |db|
  db.put("u1", { "name" => "Asha", "age" => 31 })  # Hash or JSON text
  p db.get("u1")                                   # Hash, or nil if absent
  p db.count({ "age" => { "$gte" => 30 } })
  db.scan("SELECT name FROM documents ORDER BY name").each { |row| p row }
end

One page of rows (from examples.rb)#

Ruby
db.create_index("age", true)

page = db.query({}, limit: 1)
p page["rows"], page["next"]

Errors#

RaftDB::Error (a StandardError) has #code and #message. Network problems are ordinary SystemCallError / IOError / IO::TimeoutError; a closed client raises IOError. After an AUTH error or any I/O error the client closes itself.

Ruby
begin
  db.count('{"age":')
rescue RaftDB::Error => e
  puts "#{e.code} #{e.message}"   # 4 ...
end

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#

  • With a block, connect yields the client and closes it afterwards.
  • scan returns an Enumerator.
  • Not supported: TLS, the optional UDP transport, pipelining.
  • A client is not thread-safe: one connection per thread.

Conformance status#

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

See also#