1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
//!
//! # Common components
//!
//! This module provides common components and utilities used throughout the VSDB framework.
//! It includes type definitions, constants, macros, and functions for managing the
//! underlying database environment.
//!
pub
/// Structured error types for the VSDB public API.
/// Namespaces: anonymous placement groups (independent engine instances).
pub use BatchTrait;
use ;
pub use ;
use Mutex;
use *;
use ;
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
/// A type alias for a vector of bytes, commonly used for raw data.
pub type RawBytes = ;
/// A type alias for a raw key, represented as a vector of bytes.
pub type RawKey = RawBytes;
/// A type alias for a raw value, represented as a vector of bytes.
pub type RawValue = RawBytes;
/// A type alias for a prefix, represented as a `u64`.
pub type Pre = u64;
/// The size of a prefix in bytes.
pub const PREFIX_SIZE: usize = ;
/// A type alias for a prefix represented as a byte array.
pub type PreBytes = ;
/// A constant representing 1 kilobyte in bytes.
pub const KB: u64 = 1 << 10;
/// A constant representing 1 megabyte in bytes.
pub const MB: u64 = 1 << 20;
/// A constant representing 1 gigabyte in bytes.
pub const GB: u64 = 1 << 30;
/// The first allocatable prefix: everything below this value is reserved
/// (never issued by the allocator). Doubles as the allocator's initial
/// persisted value.
const PREFIX_ALLOC_START: Pre = 4096_0000;
/// The biggest reserved ID.
pub const BIGGEST_RESERVED_ID: Pre = PREFIX_ALLOC_START - 1;
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
const BASE_DIR_VAR: &str = "VSDB_BASE_DIR";
static VSDB_BASE_DIR: =
new;
static VSDB_CUSTOM_DIR: = new;
static VSDB_SYSTEM_DIR: = new;
static VSDB_META_DIR: = new;
/// Returns the instance-meta directory path of the **default
/// namespace** (its root is the base dir, so this equals
/// `Namespace::default_ns().meta_dir()`; non-default namespaces keep
/// their metas under their own roots — use [`Namespace::meta_dir`]).
///
/// This directory (`{system_dir}/__instance_meta__/`) is used to persist
/// lightweight metadata (e.g. serialized handles) for individual VSDB
/// instances, keyed by their unique `instance_id`.
/// Returns the **default-namespace** meta file path for a given map ID
/// (a bare `u64` can only ever address a default-namespace instance;
/// namespace-resident metas live under [`Namespace::meta_dir`]).
/// Atomically replaces the file at `path` with `bytes`.
///
/// Writes to a sibling `*.tmp` file, fsyncs it, then renames it over the
/// target — a crash mid-write can never leave a truncated file at `path`
/// (POSIX `rename` is atomic within a filesystem). Instance metas are
/// written under the SWMR contract, so the fixed tmp name cannot race.
/// The global instance of the VsDB database.
///
/// This static variable is lazily initialized and provides a single point of
/// access to the underlying database.
pub static VSDB: = new;
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
/// A macro to parse a byte slice into a specified integer type.
///
/// # Arguments
///
/// * `$bytes` - The byte slice to parse.
/// * `$ty` - The integer type to parse the bytes into.
///
/// # Panics
///
/// This macro will panic if the byte slice cannot be converted into the specified integer type.
pub use parse_int;
/// A macro to parse a byte slice into a `Pre` type.
///
/// # Arguments
///
/// * `$bytes` - The byte slice to parse.
///
/// # Panics
///
/// This macro will panic if the byte slice cannot be converted into a `Pre` type.
pub use parse_prefix;
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
/// A struct representing the VsDB database.
///
/// This struct provides a high-level interface for interacting with the
/// database. It is a thin delegate over the default namespace, which
/// owns the actual default engine (see `namespace::DEFAULT_NS`).
///
/// The storage engine is MMDB, a pure-Rust LSM-Tree engine.
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
/// Returns the custom directory path for VSDB — the **app-level
/// bootstrap anchor**, deliberately one per universe (NOT per
/// namespace).
///
/// This directory (`{base_dir}/__CUSTOM__/`) is available for users to
/// store application-specific files alongside the VSDB data directory —
/// typically the serialized app state holding the top-level collection
/// handles. Those handles may live in *any* namespace (their metas
/// embed the owning `ns_id`, and deserialization auto-opens it), so
/// this blob is the app's index over the whole universe. That is why
/// the dir does not split per namespace: namespaces are reached
/// *through* handles, and the handles are bootstrapped from here — a
/// per-namespace location would be circular, and `vsdb_ns_destroy`
/// would silently take the app's root pointer with it. Users need to
/// remember exactly one thing: the base dir.
///
/// # Returns
///
/// A `&'static Path` to the custom directory.
/// Returns the internal system directory path of the **default
/// namespace** (≡ `Namespace::default_ns().system_dir()`; each
/// namespace has its own `__SYSTEM__` under its root — see
/// [`Namespace::system_dir`]). Also home to the registry-wide state
/// that deliberately stays global: the namespace registry, the prefix
/// allocator ceiling, and the DagMap ID counter.
///
/// This directory (`{base_dir}/__SYSTEM__/`) is reserved for VSDB internal use
/// (instance metadata, trie caches, ID counters). Not intended for external use.
/// Returns the base directory path for VSDB.
///
/// This function returns the path of the base directory, which is determined
/// by the `VSDB_BASE_DIR` environment variable, the `HOME` environment variable
/// (`$HOME/.vsdb`), or a process-private temporary directory as a last resort.
///
/// # Returns
///
/// A `PathBuf` to the base directory.
/// Whether the base directory has been frozen (set manually or locked in
/// by the first database initialization).
static BASE_DIR_FROZEN: AtomicBool = new;
/// Freezes the base directory without touching the process environment.
///
/// Called by the engine when the database is first opened so that any
/// later [`vsdb_set_base_dir`] call fails instead of silently diverging
/// from the directory already in use. This deliberately performs no
/// `env::set_var` — it can run at an arbitrary point in a multithreaded
/// program, where mutating the environment would be unsound.
pub
/// Sets the base directory path for VSDB manually.
///
/// This function allows you to programmatically set the base directory for VSDB.
/// It can only be called once, before the database is initialized.
///
/// It also publishes the directory through the `VSDB_BASE_DIR`
/// environment variable (for child processes). Because `env::set_var`
/// is unsound while other threads may be reading the environment, call
/// this **early in `main`, before spawning any threads**. If you cannot
/// guarantee that, set the `VSDB_BASE_DIR` environment variable before
/// process start instead of calling this function.
///
/// # Arguments
///
/// * `dir` - An object that can be converted into a `Path`.
///
/// # Errors
///
/// This function will return an error if the base directory has already been initialized.
/// Flushes all data to disk — the default namespace and every open
/// non-default namespace.
///
/// This function triggers a flush operation on the underlying database,
/// ensuring that all pending writes are persisted to disk. This operation
/// may take a long time to complete, depending on the amount of data to be flushed.