Client libraries and protocols¶
OliviaDB speaks two protocols:
- HTTP (the network API): everything, JSON in and out.
- The native protocol (specification, server side in the network API): one long-lived connection (TCP, TLS or a Unix socket) carrying many requests at once. Reads come back in rows (each node's stored key and value bytes, described once per connection and decoded only when a field is read), writes are acknowledged in JSON (the HTTP route's status and answer), and agent memory and agent state stay on HTTP.
| Language | Package | Repository | HTTP client | Native protocol |
|---|---|---|---|---|
| Rust | oliviadb on crates.io |
OliviaDB/oliviadb-rust | oliviadb::Client |
oliviadb::native::Native |
| Java (17+) | com.olivia-db:oliviadb-client on Maven Central |
OliviaDB/oliviadb-java | OliviaDbClient |
NativeClient |
| JavaScript / TypeScript | @olivia-db/client on npm |
OliviaDB/oliviadb-js | OliviaDbClient (any fetch) |
@olivia-db/client/native (Node.js) |
| Python (3.10+) | oliviadb on PyPI |
OliviaDB/oliviadb-python | oliviadb.OliviaDbClient |
oliviadb.native.NativeClient |
cargo add oliviadb
npm install @olivia-db/client
pip install oliviadb
<dependency>
<groupId>com.olivia-db</groupId>
<artifactId>oliviadb-client</artifactId>
<version>0.1.0</version>
</dependency>
Each library's contract tests run against a server built from the core; the native clients'
tests check that a read's rows, given as JSON, are the HTTP answer to the same read. The
DL/I layer is embedded through its C ABI, the Rust API or the Java binding:
embedding the DL/I layer. DL/I calls from Rust, Java and Python programs: the
packages oliviadb-dli of each, in DL/I from COBOL, C, Rust, Java and Python. Editors: editors.
Examples¶
Each example connects over the native protocol (aggregates: a write, acknowledged in JSON,
and a tree read in rows with typed accessors) and over HTTP (agent memory). They read the
server from OLIVIADB_URL, OLIVIADB_KEY, OLIVIADB_NATIVE_HOST, OLIVIADB_NATIVE_PORT and
OLIVIADB_CA_FILE (the server's CA certificate; for the image's self-signed one,
docker cp oliviadb:/data/tls/server.pem oliviadb-ca.pem). The native protocol is TLS, with ALPN
oliviadb/1 (the image's native port, OLIVIADB_NATIVE=1); each client verifies the chain
(the platform's roots, or the CA file) and the server name. Over HTTP, the Rust client takes the
CA file itself; Java, Node.js and Python take it from their trust settings (a trust store,
NODE_EXTRA_CA_CERTS, SSL_CERT_FILE).
use the schema of the HTTP examples, and are run by
tools/docs-snippets.py against a checkout of each library.
Rust¶
use oliviadb::memory::Remember;
use oliviadb::native::{Native, TlsOptions};
use oliviadb::Client;
use serde_json::json;
fn main() -> oliviadb::Result<()> {
let env = |k: &str| std::env::var(k).unwrap();
let key = env("OLIVIADB_KEY");
let addr = format!("{}:{}", env("OLIVIADB_NATIVE_HOST"), env("OLIVIADB_NATIVE_PORT"));
let tls = TlsOptions { ca_file: Some(env("OLIVIADB_CA_FILE").into()), server_name: None };
let db = Native::connect_tls(&addr, &tls, "token", &key)?;
// Writes: acknowledged with the HTTP route's status and JSON answer.
let (status, _) = db.write("POST", "Shelf/rs1", &json!({"fields": {"label": "Rust shelf", "count": 1}}))?;
assert_eq!(status, 201);
db.write("POST", "Shelf/rs1/Book/9780000000031", &json!({"fields": {"title": "Ferns", "price": "4.20"}}))?;
// Reads: rows, each field decoded when it is read.
let rows = db.get("Shelf/rs1", &[("tree", "true")])?;
for n in rows.nodes() {
let n = n?;
match n.desc().type_name() {
"Shelf" => println!("shelf {:?}", n.str(&n.desc().field("label").unwrap())),
"Book" => println!("book {:?}", n.decimal(&n.desc().field("price").unwrap())),
_ => {}
}
}
// Agent memory over HTTP.
let http = Client::builder([env("OLIVIADB_URL")]).api_key(&key).ca_file(env("OLIVIADB_CA_FILE"))?.build()?;
http.remember(&Remember {
thread: "rs".into(),
text: "Ann works at Acme.".into(),
step_id: "rs-1".into(),
entities: vec!["Ann".into()],
..Default::default()
})?;
Ok(())
}
Java¶
import com.fasterxml.jackson.databind.ObjectMapper;
import com.oliviadb.client.NativeClient;
import com.oliviadb.client.OliviaDbClient;
import java.net.URI;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
public class Main {
public static void main(String[] args) throws Exception {
String key = System.getenv("OLIVIADB_KEY");
ObjectMapper json = new ObjectMapper();
NativeClient.TlsOptions tls = new NativeClient.TlsOptions().caFile(Path.of(System.getenv("OLIVIADB_CA_FILE")));
try (NativeClient db = NativeClient.connectTls(
System.getenv("OLIVIADB_NATIVE_HOST"), Integer.parseInt(System.getenv("OLIVIADB_NATIVE_PORT")), tls, "token", key)) {
// Writes: acknowledged with the HTTP route's status and JSON answer.
NativeClient.Ack ack = db.write("POST", "Shelf/java1",
json.readTree("{\"fields\": {\"label\": \"Java shelf\", \"count\": 1}}"));
if (ack.status != 201) throw new IllegalStateException("insert: " + ack.status);
db.write("POST", "Shelf/java1/Book/9780000000048",
json.readTree("{\"fields\": {\"title\": \"Moss\", \"price\": \"3.10\"}}"));
// Reads: rows, each field decoded when it is read.
for (NativeClient.Node n : db.get("Shelf/java1", Map.of("tree", "true")).nodes()) {
switch (n.desc().typeName()) {
case "Shelf" -> System.out.println("shelf " + n.str(n.desc().field("label")));
case "Book" -> System.out.println("book " + n.decimal(n.desc().field("price")));
default -> { }
}
}
}
// Agent memory over HTTP.
OliviaDbClient http = OliviaDbClient.builder(URI.create(System.getenv("OLIVIADB_URL"))).apiKey(key).build();
http.remember("java", "Ann works at Acme.", "java-1", List.of("Ann"), List.of());
}
}
JavaScript (Node.js)¶
import { NativeClient } from "@olivia-db/client/native";
import { OliviaDbClient } from "@olivia-db/client";
const env = process.env;
const db = await NativeClient.connectTls(env.OLIVIADB_NATIVE_HOST, Number(env.OLIVIADB_NATIVE_PORT), "token", env.OLIVIADB_KEY, {
caFile: env.OLIVIADB_CA_FILE,
});
// Writes: acknowledged with the HTTP route's status and JSON answer.
const [status] = await db.write("POST", "Shelf/js1", { fields: { label: "JS shelf", count: 1 } });
if (status !== 201) throw new Error(`insert: ${status}`);
await db.write("POST", "Shelf/js1/Book/9780000000055", { fields: { title: "Ivy", price: "2.30" } });
// Reads: rows, each field decoded when it is read.
const rows = await db.get("Shelf/js1", { tree: "true" });
for (const n of rows.nodes()) {
if (n.desc.typeName === "Shelf") console.log("shelf", n.str(n.desc.field("label")));
if (n.desc.typeName === "Book") console.log("book", n.decimal(n.desc.field("price")));
}
db.close();
// Agent memory over HTTP.
const http = new OliviaDbClient({ baseUrl: env.OLIVIADB_URL, apiKey: env.OLIVIADB_KEY });
await http.remember({ thread: "js", text: "Ann works at Acme.", step_id: "js-1", entities: ["Ann"] });
Python¶
import os
from oliviadb import OliviaDbClient
from oliviadb.native import NativeClient
env = os.environ
host, port = env["OLIVIADB_NATIVE_HOST"], int(env["OLIVIADB_NATIVE_PORT"])
with NativeClient.connect_tls(host, port, "token", env["OLIVIADB_KEY"], ca_file=env["OLIVIADB_CA_FILE"]) as db:
# Writes: acknowledged with the HTTP route's status and JSON answer.
status, _ = db.write("POST", "Shelf/py1", {"fields": {"label": "Python shelf", "count": 1}})
assert status == 201, status
db.write("POST", "Shelf/py1/Book/9780000000062", {"fields": {"title": "Fern", "price": "1.90"}})
# Reads: rows, each field decoded when it is read.
for n in db.get("Shelf/py1", {"tree": "true"}).nodes():
if n.desc.type_name == "Shelf":
print("shelf", n.str(n.desc.field("label")))
elif n.desc.type_name == "Book":
print("book", n.decimal(n.desc.field("price")))
# Agent memory over HTTP.
http = OliviaDbClient(env["OLIVIADB_URL"], api_key=env["OLIVIADB_KEY"])
http.remember("py", "Ann works at Acme.", step_id="py-1", entities=["Ann"])
Specifications¶
The open formats and protocols, from OliviaDB/oliviadb-spec: native protocol, schema language, path keys, storage format, unload format, memory index, compatibility.