Expand description
libobj — C ABI surface for the obj embedded document database.
This crate is the unsafe boundary of obj. Every C entry
point is unsafe extern "C"; internally each shim is a short
piece of glue that validates pointers, calls into the safe-Rust
[obj] API, and converts the obj_engine::Error result into an
obj_error_t code + out-pointer.
§Power-of-ten posture
- Rule 7: every fallible call inside this crate is matched
or
?-propagated. Nounwrap()/expect()on the C boundary; an unexpected pointer condition becomesOBJ_ERR_INVALID_ARG. - Rule 8: the crate enables
unsafe_codeand turnsunsafe_op_in_unsafe_fninto a hard error so every singleunsafeoperation lives inside an explicitunsafe { ... }block with a// SAFETY:comment justifying it against the function’s documented C contract. - Rule 9: no
dyn Traitat the C boundary. The opaqueobj_db_tis a concrete struct; iterators introduced later are concrete-typed. - Rule 10: clippy pedantic on; every
#[allow(...)]carries an adjacent justification.
§Pointer / lifetime conventions
- All opaque handle pointers (
obj_db_t*,obj_write_txn_t*, etc.) returned by this library are obtained by leaking aBox<T>. The correspondingobj_*_close/_free/_rollbackcalls reclaim ownership viaBox::from_raw. - All byte buffers returned from this library are allocated as
Box<[u8]>and MUST be freed viaobj_free_buffer. Mixing Rust’s allocator with C’smallocis undefined. - Null handles are treated specifically per function: close /
free / rollback are null-tolerant (no-op); every other entry
point returns
OBJ_ERR_INVALID_ARGwhen given a null handle / out-pointer.
Structs§
- obj_
config_ t - Flat C struct mirroring
obj_engine::Config’s public knobs. - obj_
db_ t - Opaque database handle.
- obj_
integrity_ report_ t - Opaque integrity report handle. Buffers the failure list so per-failure access by index is cheap.
- obj_
iter_ t - Opaque iterator handle returned by
obj_iter_all/obj_iter_index_range. Drives one element at a time viaobj_iter_nextand is freed byobj_iter_free. - obj_
read_ txn_ t - Opaque read-transaction handle. Same lifetime erasure pattern
as
obj_write_txn_t. - obj_
stat_ t - Flat C struct returned by
obj_stat. - obj_
write_ txn_ t - Opaque write-transaction handle.
Constants§
- OBJ_
ERR_ BUSY - A lock could not be acquired inside the configured timeout — another transaction holds the writer mutex / cross-process writer byte, or the in-process pager mutex was poisoned.
- OBJ_
ERR_ CORRUPTION - Content-level corruption: a page CRC mismatch, malformed catalog row, schema-version-from-future, B-tree invariant violated, etc.
- OBJ_
ERR_ INTEGRITY - An integrity check completed and found violations. Distinct
from
OBJ_ERR_CORRUPTIONwhich is the hard fail-closed shape emitted by individual operations;OBJ_ERR_INTEGRITYis the soft “the report has failures” shape returned by aggregate diagnostic calls. - OBJ_
ERR_ INVALID_ ARG - Caller passed an invalid argument: a null handle / out-pointer where one is required, a non-UTF-8 path or collection name, a length that exceeds the documented maximum, etc.
- OBJ_
ERR_ IO - Underlying I/O / syscall failure: file open, read, write, fsync, directory enumeration, lock acquisition syscall.
- OBJ_
ERR_ NOT_ FOUND - The requested record / index entry / collection was not found.
Iterators also return this when exhausted (see
obj_iter_nextdocs). - OBJ_
ERR_ RESERVED_ MAX - Highest reserved code in the obj-defined range. Future error
codes must stay
<= OBJ_ERR_RESERVED_MAX; this allows downstream callers to reliably distinguish obj-emitted codes from any they may layer on top. - OBJ_
ERR_ UNSUPPORTED - Operation is not supported in this build / for this database
(e.g.
obj_backup_toon an in-memory database, an attempt to mutate an attached read-only attachment, an unimplemented schema migration). - OBJ_
ERR_ UTF8 - A C string handed across the FFI boundary was not valid UTF-8.
Distinct from
OBJ_ERR_INVALID_ARGbecause UTF-8 issues are frequent enough on user paths / index names to merit their own signal. - OBJ_OK
- Operation succeeded.
- OBJ_
SYNC_ MODE_ FULL - Strongest durability — survives system-wide power loss.
- OBJ_
SYNC_ MODE_ NORMAL - Process-crash / kernel-panic durability.
- OBJ_
SYNC_ MODE_ OFF - No durability call. Tests and benchmarks only.
Functions§
- obj_
backup_ ⚠to - Take a hot backup of
dbtodest. The destination MUST NOT already exist. - obj_
close ⚠ - Close a database handle. Null-tolerant.
- obj_
count_ ⚠all - Count every doc in
collection. - obj_
count_ ⚠index_ range - Count index B-tree entries inside
[lower, upper]. Bounds use the same(ptr, len, inclusive)shape asobj_iter_index_range. - obj_
doc_ ⚠delete - Delete the document at
idincollection. ReturnsOBJ_ERR_NOT_FOUNDif the id is absent. - obj_
doc_ ⚠get - Fetch the document at
idincollection. OnOBJ_OKthe caller owns*out_payload(length*out_payload_len) and MUST free it viaobj_free_buffer. ReturnsOBJ_ERR_NOT_FOUNDif the document is absent. - obj_
doc_ ⚠insert - Insert a raw-bytes document into
collection. The collection is lazy-created if absent. On success*out_idis set to the freshly-allocated id. - obj_
doc_ ⚠update - Update the document at
idincollectionwithpayload. ReturnsOBJ_ERR_NOT_FOUNDif the id is absent. - obj_
doc_ ⚠upsert - Insert-or-replace the document at
idincollectionwithpayload. The collection is lazy-created if absent. - obj_
find_ ⚠unique - Look up a single doc by index key on a
Uniqueindex. The caller pre-encodeskey_bytesper M7’s order-preserving scheme. ReturnsOBJ_ERR_NOT_FOUNDwhen no doc matches. - obj_
free_ ⚠buffer - Free a buffer returned by an
obj_doc_get/ iteration call. Pairs with theBox<[u8]>::into_rawallocation done inside [box_bytes]. Null-tolerant; passing a(ptr, len)pair that did NOT come from libobj is undefined behaviour. - obj_
integrity_ ⚠check - Run the full bidirectional integrity walk against
db. Returns the report through*out_report. OnOBJ_OKthe report holds the walk’s outcome (the caller checksis_okviaobj_integrity_report_is_ok); onOBJ_ERR_INTEGRITYthe walk completed but found at least one failure. Any I/O / engine error during the walk surfaces as a different error code with*out_reportset to NULL. - obj_
integrity_ ⚠report_ failure_ at - Copy the Debug-formatted text of failure
indexinto a fresh libobj-owned buffer. Caller pairs withobj_free_buffer. ReturnsOBJ_ERR_NOT_FOUNDfor an out-of-range index. - obj_
integrity_ ⚠report_ failure_ count - Number of failure entries in the report.
- obj_
integrity_ ⚠report_ free - Free an integrity report. Null-tolerant.
- obj_
integrity_ ⚠report_ is_ ok trueiff the report carries no failures. Null-tolerant — returnsfalsefor a NULL report (a NULL report is by definition not a clean one).- obj_
integrity_ ⚠report_ pages_ checked - Number of pages the walker visited.
- obj_
iter_ ⚠all - Construct an iterator over every doc in
collection, snapshot- consistent againsttxn. Caller pairs withobj_iter_nextandobj_iter_free. - obj_
iter_ ⚠free - Free an iterator handle. Null-tolerant.
- obj_
iter_ ⚠index_ range - Construct an iterator over the index range
[lower, upper](bounds applied per*_inclusiveflag) onindex_nameincollection.NULLlower / upper means unbounded. - obj_
iter_ ⚠next - Step the iterator. On
OBJ_OKthe caller owns*out_payload(length*out_payload_len) and MUST free it withobj_free_buffer. ReturnsOBJ_ERR_NOT_FOUNDat end-of- iteration; a real engine failure surfaces as the corresponding error code. - obj_
open ⚠ - Open or create a file-backed database at
path. Default configuration (obj_engine::Config::default). - obj_
open_ ⚠with_ config - As
obj_openbut with caller-supplied configuration. - obj_
stat ⚠ - Populate
out_statwith a snapshot of the db’s header + collection-count summary. - obj_
strerror ⚠ - Return a static C string describing
code. Never returns NULL. - obj_
txn_ ⚠begin_ read - Begin a read transaction against
db. Pins a snapshot at the current writer LSN. - obj_
txn_ ⚠begin_ write - Begin a write transaction against
db. Acquires the writer slot (subject to the configured busy timeout). - obj_
txn_ ⚠commit - Commit a write transaction. Consumes the handle — the C
caller MUST NOT touch
txnagain after this returns. - obj_
txn_ ⚠end_ read - End a read transaction. Releases the reader slot + the pinned-snapshot WAL anchor. Null-tolerant.
- obj_
txn_ ⚠rollback - Roll back a write transaction. Null-tolerant.
Type Aliases§
- obj_
error_ t - Numeric type carried by
obj_error_t. Aliased so the cbindgen header emitstypedef int32_t obj_error_t;rather than a bareint32_teverywhere. - obj_
sync_ mode_ t SyncModeselector forobj_config_t::sync_mode.