import { Code, Tabs, TabItem } from '@astrojs/starlight/components';
import fullExample from '/../examples/src/full_example.rs?raw'
SlateDB is implemented in Rust and ships official bindings for Go, Java, Node.js, and Python. Pick your language below to install the library and run a minimal example backed by an in-memory object store.
Installation#
```bash cargo add slatedb tokio --features tokio/macros,tokio/rt-multi-thread ``` ```bash go get slatedb.io/slatedb-go ```The Go binding uses cgo and links against the `slatedb_uniffi` shared library. You need Go 1.25+, `CGO_ENABLED=1`, a C toolchain, and the `slatedb_uniffi` library on your loader path.
```gradle
dependencies {
implementation("io.slatedb:slatedb-uniffi:<version>")
}
```
Maven:
```xml
<dependency>
<groupId>io.slatedb</groupId>
<artifactId>slatedb-uniffi</artifactId>
<version><version></version>
</dependency>
```
Requires Java 22 or newer.
Requires Node.js 20 or newer.
Requires Python 3.10 or newer.
Usage#
SlateDB reads and writes through an object store. Every binding exposes ObjectStore.resolve(...) which accepts any URL supported by Rust's object_store crate, including memory:///, file:///..., s3://, gs://, and az://. Pass the database path separately to Db::open, DbBuilder, or NewDbBuilder, as the examples below do.
<Code code={fullExample} lang="rust" title="main.rs" />
import (
"bytes"
"fmt"
slatedb "slatedb.io/slatedb-go/uniffi"
)
func main() {
store, err := slatedb.ObjectStoreResolve("memory:///")
if err != nil {
panic(err)
}
defer store.Destroy()
builder := slatedb.NewDbBuilder("example-db", store)
defer builder.Destroy()
db, err := builder.Build()
if err != nil {
panic(err)
}
defer db.Destroy()
if _, err := db.Put([]byte("hello"), []byte("world")); err != nil {
panic(err)
}
value, err := db.Get([]byte("hello"))
if err != nil {
panic(err)
}
if value == nil || !bytes.Equal(*value, []byte("world")) {
panic("unexpected value")
}
fmt.Println(string(*value))
if err := db.Shutdown(); err != nil {
panic(err)
}
}
```
Handle types own Rust-side resources: call `Shutdown()` on databases and readers, and `Destroy()` on handles when you are done with them.
```java
import io.slatedb.uniffi.Db;
import io.slatedb.uniffi.DbBuilder;
import io.slatedb.uniffi.ObjectStore;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
public final class Main {
private static <T> T await(CompletableFuture<T> future) throws Exception {
return future.get(30, TimeUnit.SECONDS);
}
public static void main(String[] args) throws Exception {
byte[] key = "hello".getBytes(StandardCharsets.UTF_8);
byte[] value = "world".getBytes(StandardCharsets.UTF_8);
try (ObjectStore store = ObjectStore.resolve("memory:///");
DbBuilder builder = new DbBuilder("demo-db", store)) {
Db db = await(builder.build());
try (db) {
await(db.put(key, value));
byte[] read = await(db.get(key));
if (read == null || !Arrays.equals(read, value)) {
throw new IllegalStateException("unexpected value");
}
System.out.println(new String(read, StandardCharsets.UTF_8));
await(db.shutdown());
}
}
}
}
```
```js
import assert from "node:assert/strict";
import { DbBuilder, ObjectStore } from "@slatedb/uniffi";
async function main() {
const store = ObjectStore.resolve("memory:///");
let db;
try {
const builder = new DbBuilder("demo-db", store);
try {
db = await builder.build();
} finally {
builder.dispose();
}
const key = Buffer.from("hello");
const value = Buffer.from("world");
await db.put(key, value);
const read = await db.get(key);
assert.deepEqual(read, value);
console.log(Buffer.from(read).toString("utf8"));
} finally {
if (db != null) {
await db.shutdown();
db.dispose();
}
store.dispose();
}
}
main().catch((error) => {
console.error(error);
process.exitCode = 1;
});
```
```python
import asyncio
from slatedb.uniffi import (
DbBuilder,
IsolationLevel,
KeyRange,
ObjectStore,
WriteBatch,
)
async def main() -> None:
store = ObjectStore.resolve("memory:///")
builder = DbBuilder("demo-db", store)
db = await builder.build()
try:
await db.put(b"user:1", b"Alice")
value = await db.get(b"user:1")
assert value == b"Alice"
batch = WriteBatch()
batch.put(b"user:2", b"Bob")
batch.put(b"user:3", b"Carol")
await db.write(batch)
scan = await db.scan_prefix(
b"user:",
KeyRange(
start=None,
start_inclusive=False,
end=None,
end_inclusive=False,
),
)
while (row := await scan.next()) is not None:
print(row.key, row.value)
tx = await db.begin(IsolationLevel.SERIALIZABLE_SNAPSHOT)
await tx.put(b"user:4", b"Dora")
await tx.commit()
finally:
await db.shutdown()
asyncio.run(main())
```