Connectors

C++ connector

Use the single-header Raft DB C++ connector: connect, query, scan and handle errors.

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#

C++17, standard library and POSIX sockets. POSIX only (Linux, macOS, BSD); Windows is not a target.

Install#

Download the package from the Download page and unzip it. It holds the single header include/raftdb.hpp and a README.txt; nothing needs linking (c++ -std=c++17 -Iinclude myprog.cpp).

Connect#

C++
#include "raftdb.hpp"

raftdb::Options opt;            // user+password, or token, or nothing (open server)
opt.user = "admin";
opt.password = "secret";     // timeout_ms defaults to 30000
raftdb::Client db = raftdb::Client::connect("127.0.0.1:7480", opt);

Query#

C++
db.put("u1", R"({"name":"Asha","age":31})");            // true if the key already existed
if (auto row = db.get("u1")) std::cout << *row << "\n";  // std::optional<std::string> of JSON text

std::cout << db.count(R"({"age":{"$gte":30}})") << "\n";
for (const std::string& r : db.scan("SELECT name FROM documents ORDER BY name"))  // follows `next`
  std::cout << r << "\n";

Error handling around a session (from example.cpp)#

C++
try {
  raftdb::Client db = raftdb::Client::connect(argc > 1 ? argv[1] : "127.0.0.1:7480");
  db.put("example1", R"({"name":"Asha","age":31})");
  std::cout << "adults: " << db.count(R"({"age":{"$gte":18}})") << "\n";
  db.erase("example1");
} catch (const raftdb::Error& e) {
  std::cerr << "error " << e.code() << ": " << e.what() << "\n";
}

Errors#

After code 7 the connection is closed; after other server errors it stays usable. After a raftdb::IoError the connection is closed: connect again.

C++
// raftdb::Error (derives from std::runtime_error): code() is the server code, what() the message
// raftdb::IoError (derives from raftdb::Error, code() == 0): socket error, timeout, malformed reply, closed client

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#

  • Rows and documents are JSON text (std::string): the library contains no JSON parser.
  • Client is move-only and closes the socket in its destructor. It is not thread-safe.
  • erase(key) deletes; for_each_row(filter_or_sql, callback, page_size) is the callback form of scan.
  • Under -Wpedantic -std=c++17 designated initializers are not allowed: assign the fields of raftdb::Options as shown. Not supported: UDP transport.

Conformance status#

24/24 conformance scenarios on Linux, plain and under ASan and UBSan (g++). The scenarios are listed in the specification page. The package's README.txt has the install steps and a short example.

See also#