This guide helps you get started with vix::kv.
kv is a durable, local-first key-value engine for Vix, built on top of Softadastra.
It is designed to be:
- simple to use
- durable by default
- ready for real-world conditions
Before using kv, you need:
- a C++20 compiler
- a Vix project
vixinstalled on your system
Add kv to your project:
vix add @vix/kv
vix installIn your C++ file:
#include <vix/kv/kv.hpp>This gives you access to the public kv API.
The simplest way:
auto db = vix::kv::open();This opens a local database with default options.
By default, kv uses a WAL-backed durable store.
db.set({"users", "42"}, "Alice");This stores the value "Alice" under the structured key {"users", "42"}.
auto value = db.get({"users", "42"});
if (value)
{
std::cout << value->to_string() << std::endl;
}If the key exists, you get a KvValue.
If not, you get std::nullopt.
db.erase({"users", "42"});This removes the key from the materialized state and records the delete durably.
You can fetch all entries under a prefix:
auto users = db.list({"users"});
for (const auto& [key, value] : users)
{
std::cout << value.to_string() << std::endl;
}This is useful for grouped data like:
- users
- sessions
- products
- settings
#include <iostream>
#include <vix/kv/kv.hpp>
int main()
{
auto db = vix::kv::open();
db.set({"users", "1"}, "Alice");
db.set({"users", "2"}, "Bob");
auto user = db.get({"users", "1"});
if (user)
{
std::cout << "User 1: " << user->to_string() << std::endl;
}
auto users = db.list({"users"});
for (const auto& [key, value] : users)
{
std::cout << value.to_string() << std::endl;
}
db.erase({"users", "2"});
return 0;
}You can configure the database with KvOptions:
vix::kv::api::KvOptions options;
options.path = "data/app.wal";
options.auto_flush = true;
auto db = vix::kv::open(options);Useful fields include:
pathenable_walauto_flushinitial_capacity
When you call:
db.set({"users", "42"}, "Alice");kv does this:
- encodes the structured key
- encodes the value
- appends the mutation to the WAL
- updates the in-memory state
This means a successful write is already durable.
Think of kv like this:
- WAL = source of truth
- memory = fast read layer
- API = simple developer interface
Write locally. Persist first. Read instantly.
db.set({"settings", "theme"}, "dark");
db.set({"settings", "language"}, "en");db.set({"sessions", "abc123"}, "user:42");db.set({"users", "42", "name"}, "Alice");
db.set({"users", "42", "city"}, "Kampala");Available now:
open()open(options)set()get()erase()list()
Planned next:
- transactions
- watch
- sync
- conflict handling
After this guide, read:
docs/api.mddocs/concepts.mddocs/architecture.mddocs/softadastra.md
These documents explain how kv works and why it is built this way.
With kv, you can start small:
- open a database
- write values
- read values
- list by prefix
- rely on durable local persistence
That is the foundation.