Opening a database
This guide shows how to open a Vix KV database.
Use this page when you want to choose between memory-only storage, durable storage, and simple direct access.
Public header
#include <vix/kv/kv.hpp>
#include <vix/print.hpp>Opening modes
Vix KV provides three common opening modes.
| API | Purpose |
|---|---|
vix::kv::open_memory() | Opens a memory-only database. |
vix::kv::open(path) | Opens a durable database at a filesystem path. |
vix::kv::open_durable(path) | Opens a durable database with explicit Result-style error handling. |
Memory-only database
Use open_memory() when you want temporary data.
The data is lost when the database is closed or when the process exits.
#include <vix/kv/kv.hpp>
#include <vix/print.hpp>
int main()
{
auto opened = vix::kv::open_memory();
if (opened.is_err())
{
vix::eprint(opened.error().message());
return 1;
}
auto kv = opened.move_value();
auto written = kv.set({"session", "token"}, "abc123");
if (written.is_err())
{
vix::eprint(written.error().message());
return 1;
}
auto value = kv.get({"session", "token"});
if (value.is_ok())
{
vix::print("session/token =", value.value().to_string());
}
(void)kv.close();
return 0;
}Run:
vix run main.cppExpected output:
session/token = abc123Durable database
Use open(path) when values must survive after restart.
#include <vix/kv/kv.hpp>
#include <vix/print.hpp>
int main()
{
auto kv = vix::kv::open("data/app");
kv.put("hello", "world");
(void)kv.flush();
(void)kv.close();
return 0;
}The database files are stored under the directory you pass to open.
data/appReopen a durable database
A durable database can be reopened later.
#include <vix/kv/kv.hpp>
#include <vix/print.hpp>
int main()
{
{
auto kv = vix::kv::open("data/app");
kv.put("name", "Ada");
(void)kv.flush();
(void)kv.close();
}
{
auto kv = vix::kv::open("data/app");
const auto value = kv.get("name");
if (value.has_value())
{
vix::print("name =", *value);
}
(void)kv.close();
}
return 0;
}Expected output:
name = AdaResult-style durable opening
Use open_durable(path) when you want explicit error handling while opening a durable database.
#include <vix/kv/kv.hpp>
#include <vix/print.hpp>
int main()
{
auto opened = vix::kv::open_durable("data/app");
if (opened.is_err())
{
vix::eprint(opened.error().message());
return 1;
}
auto kv = opened.move_value();
auto written = kv.set({"users", "1", "name"}, "Ada");
if (written.is_err())
{
vix::eprint(written.error().message());
return 1;
}
(void)kv.flush();
(void)kv.close();
return 0;
}Default opening
vix::kv::open() opens a database with default configuration.
#include <vix/kv/kv.hpp>
#include <vix/print.hpp>
int main()
{
auto opened = vix::kv::open();
if (opened.is_err())
{
vix::eprint(opened.error().message());
return 1;
}
auto kv = opened.move_value();
vix::print("open =", kv.is_open() ? "yes" : "no");
vix::print("empty =", kv.empty() ? "yes" : "no");
vix::print("size =", kv.size());
(void)kv.close();
return 0;
}Direct API vs Result API
There are two common opening styles.
Direct style
Use direct style when you want a compact local database API.
auto kv = vix::kv::open("data/app");
kv.put("hello", "world");
const auto value = kv.get("hello");
if (value.has_value())
{
vix::print("hello =", *value);
}
(void)kv.close();This style is simple and useful for small examples.
Result style
Use Result style when you want to handle errors explicitly.
auto opened = vix::kv::open_memory();
if (opened.is_err())
{
vix::eprint(opened.error().message());
return 1;
}
auto kv = opened.move_value();
auto written = kv.set({"hello"}, "world");
if (written.is_err())
{
vix::eprint(written.error().message());
return 1;
}
auto value = kv.get({"hello"});
if (value.is_ok())
{
vix::print("hello =", value.value().to_string());
}
(void)kv.close();Close the database
Close the database when you are done.
auto closed = kv.close();
if (closed.is_err())
{
vix::eprint(closed.error().message());
return 1;
}For simple examples, this is also accepted:
(void)kv.close();Flush durable writes
Use flush() when you want to force pending durable writes to disk.
auto flushed = kv.flush();
if (flushed.is_err())
{
vix::eprint(flushed.error().message());
return 1;
}Typical durable flow:
auto kv = vix::kv::open("data/app");
kv.put("name", "Ada");
(void)kv.flush();
(void)kv.close();Check database state
After opening a database, you can inspect its state.
vix::print("open =", kv.is_open() ? "yes" : "no");
vix::print("empty =", kv.empty() ? "yes" : "no");
vix::print("size =", kv.size());For more details, use stats().
const auto stats = kv.stats();
vix::print("memory only =", stats.memory_only ? "yes" : "no");
vix::print("live keys =", stats.key_count);
vix::print("last sequence =", stats.last_sequence);Common workflows
Open a temporary store
auto opened = vix::kv::open_memory();
if (opened.is_err())
{
vix::eprint(opened.error().message());
return 1;
}
auto kv = opened.move_value();Open a durable store
auto kv = vix::kv::open("data/app");Open a durable store with explicit errors
auto opened = vix::kv::open_durable("data/app");
if (opened.is_err())
{
vix::eprint(opened.error().message());
return 1;
}
auto kv = opened.move_value();Write, flush, and close
kv.put("hello", "world");
(void)kv.flush();
(void)kv.close();Reopen and read
auto kv = vix::kv::open("data/app");
const auto value = kv.get("hello");
if (value.has_value())
{
vix::print("hello =", *value);
}
(void)kv.close();Common mistakes
Using memory storage when data must persist
Memory-only data does not survive restart.
auto opened = vix::kv::open_memory();Use durable storage instead:
auto kv = vix::kv::open("data/app");Forgetting to check open errors
Wrong:
auto opened = vix::kv::open_memory();
auto kv = opened.move_value();Correct:
auto opened = vix::kv::open_memory();
if (opened.is_err())
{
vix::eprint(opened.error().message());
return 1;
}
auto kv = opened.move_value();Forgetting to close the database
auto kv = vix::kv::open("data/app");
kv.put("hello", "world");
(void)kv.close();Forgetting to flush durable writes in examples
When writing durable examples, call flush() before close().
(void)kv.flush();
(void)kv.close();Confusing direct open and Result open
This returns a database directly:
auto kv = vix::kv::open("data/app");This returns a Result:
auto opened = vix::kv::open_memory();
auto opened = vix::kv::open_durable("data/app");Related pages
| Page | Purpose |
|---|---|
| Keys | Learn how to structure keys. |
| Values | Learn how to store and read values. |
| Persistence | Learn how data survives restart. |
| Stats | Learn how to inspect database state. |
Next step
Continue with keys.