SDK support

CTable_.h

Row descriptors, field selections, row concepts, and supporting typed-table declarations.

C++23 mc/database/CTable_.h
#include <mc/database/CTable_.h>

Use CField, CFields, and CRow32 or CRow64 to describe row data. Cosmic schemas normally generate these descriptors for you.

Unique, Optional, and Hash are field flags. A hashed field stores its index representation instead of a recoverable original value.

The support templates are listed for completeness; applications normally access tables through CDatabase and CTable<R>.

Jump to a declaration · 109

CField

template<class V, size_t I, uint64_t G = 0> struct CField

Types, constants & data

using type = V;
static constexpr size_t Index = I;
static constexpr uint64_t Flags = G;

Free functions & types

Types, constants & data

template<class T> concept CCField = std::same_as<T, CField<typename T::type, T::Index, T::Flags>>;
template<class T> concept CCRowId = CSame<T, uint32_t> || CSame<T, uint64_t>;
using CRow32 = CRow<uint32_t>;
using CRow64 = CRow<uint64_t>;
template<class T> concept CCRow = std::is_base_of_v<CRow32, T> || std::is_base_of_v<CRow64, T> || std::is_same_v<T, cvec>;

CFields

class CFields

Methods

CFields

CFields();

Creates an empty field selection. Add field indices with operator<<, or use all() to request all fields.

operator<<

CFields& operator<<(size_t index);

Adds a field index to the selection and grows the mask if necessary. Repeated additions leave the field selected.

operator[]

bool operator[](size_t i) const;

Tests the stored selection mask at a valid index. Check isAll() separately for the all-fields marker; that marker does not populate the mask.

size

size_t size() const;

Returns the length of the selection mask, not the number of selected fields. The all-fields marker can have an empty mask.

all

static CFields all();

Creates the special selection meaning every field. It is interpreted by table retrieval rather than expanded into individual indices.

isAll

bool isAll() const;

Reports whether the selection is the special all-fields marker.

CFieldsCreate_

template<size_t I> struct CFieldsCreate_

Methods

create

template<class T> static bool create(const T& t, CFields& f);

Adds the field indices from a tuple starting at compile-time position I to an existing selection. This supports generated row fields() helpers.

CFieldGetType_

template<class F> struct CFieldGetType_
template<size_t J, uint64_t G> struct CFieldGetType_<CField<cstr, J, G>>
template<size_t J, uint64_t G> struct CFieldGetType_<CField<cvar, J, G>>

Types, constants & data

using type = typename F::type;
using type = const cstr&;
using type = const cvar&;

CRow

template<CCRowId RI> class CRow

Types, constants & data

using RowId = RI;
using RowSet = CHashSet<RowId>;
using RowVector = CVector<RowId>;
static const uint64_t Unique = 0x01;
static const uint64_t Optional = 0x02;
static const uint64_t Hash = 0x04;

Methods

CRow

CRow(size_t fieldCount) noexcept;
CRow(const cvec& v) noexcept;
CRow(cvec&& v) noexcept;

Creates a row from a field count or existing value vector. Slot zero holds the row ID; a new count-based row starts with ID zero and requires room for that slot.

setRowId

void setRowId(RowId rowId) noexcept;

Sets the row’s stored ID without changing any database record. IDs used for update or erase must identify an existing row.

rowId

RowId rowId() const noexcept;

Returns the row ID in slot zero. Zero denotes a new or unassigned row.

vec

const cvec& vec() const noexcept;
cvec& vec() noexcept;

Returns the backing field vector by reference. Changes affect this row immediately, but still require table insertion/update and commit to reach the database.

get

template<class F> decltype(auto) get();

Directly reads the requested field type. The field must be loaded, present, and have the matching stored type; use safeGet() for checked access.

safeGet

template<class F> decltype(auto) safeGet();

Reads a field through checked conversion, except cvar fields which are returned directly. Missing or incompatible typed fields raise CError.

has

template<class F> decltype(auto) has() const;

Reports whether a field slot is non-Null. It does not prove the row exists in a table or that an unloaded field is absent on disk.

set

template<class F, class T> void set(T&& x);

Assigns a value to a field slot in this row. It does not stage a database update by itself.

erase

template<class V, size_t J, uint64_t G> void erase(CField<V, J, G> f);

Sets the selected field slot to Null in this row. The row still needs a table update and commit to persist the change.

operator<<

friend std::ostream& operator<<(std::ostream& ostr, const CRow& r);

init_

void init_(cvec&& v) noexcept;

Replaces the complete field vector by moving the supplied values. This is the row-adapter hook used by table retrieval.

fields

template<class... Args> static CFields fields();

Builds a field selection from the supplied field-descriptor template arguments. Use it to avoid reading unneeded columns.

CMultiIndexValue__

CMultiIndexValue_

template<class T> struct CMultiIndexValue_ : public CMultiIndexValue__

Types, constants & data

using type = T;
chash h;
T v;

Methods

operator<

bool operator<(const CMultiIndexValue_& x) const;

Orders composite-index values by their equality-key hash first and their ordered component second. This groups equal composite keys for range traversal.

operator<=

bool operator<=(const CMultiIndexValue_& x) const;

Orders composite-index values by their equality-key hash first and their ordered component second. This groups equal composite keys for range traversal.

operator>

bool operator>(const CMultiIndexValue_& x) const;

Orders composite-index values by their equality-key hash first and their ordered component second. This groups equal composite keys for range traversal.

operator cvar

operator cvar() const;

Returns the two words of the equality-key hash followed by the ordered component as a three-element vector.

CTableIndexType_

template<class T> struct CTableIndexType_

Types, constants & data

using type = std::conditional_t< std::same_as<T, uint32_t> || std::same_as<T, int32_t> || std::same_as<T, uint64_t> || std::same_as<T, int64_t> || std::same_as<T, float> || std::same_as<T, double>, T, cvar>;

CTableFieldValueType_

template<CCField F> struct CTableFieldValueType_

Types, constants & data

using type = std::conditional_t<std::same_as<typename F::type, cstr>, cvar, typename F::type>;

CTableIndex_

template<CCRowId RI, class T> class CTableIndex_
template<CCRowId RI> class CTableIndex_<RI, cvar>
template<CCRowId RI, class T> class CTableIndex_<RI, CMultiIndexValue_<T>>
template<CCRowId RI> class CTableIndex_<RI, CMultiIndexValue_<cvar>>

Types, constants & data

using value_type = T;
using value_type = chash;
using value_type = CMultiIndexValue_<T>;
using value_type = CMultiIndexValue_<uint64_t>;
using RowId = RI;
using RowSet = CHashSet<RowId>;
using RowVector = CVector<RowId>;
using QueryFunc_ = std::function<bool(RowId rowId, cvec& r)>;
using UpdateMap = CHashMap<RowId, RowId>;

Methods

CTableIndex_

CTableIndex_() noexcept;

Creates an uninitialized index adapter. Generated table construction calls init() before using its operations.

init

void init(CTable_<RowId>* t, size_t indexId, uint64_t flags) noexcept;

Attaches the adapter to a borrowed table and an assigned index ID with field flags. The table must outlive the adapter.

insert

void insert(RowId rowId, T x) noexcept;
void insert(RowId rowId, const cstr& x) noexcept;
void insert(RowId rowId, const cvar& x) noexcept;
void insert(RowId rowId, value_type x) noexcept;

Stages an index entry for a row. It is prepared and published with the table’s commit sequence.

get

template<CCRow R> bool get(const CFields& fs, T value, R& row) noexcept;
template<CCRow R> bool get(const CFields& fs, const cvar& v, R& row) noexcept;
template<CCRow R> bool get(const CFields& fs, const chash& h, T value, R& row) noexcept;
template<CCRow R> bool get(const CFields& fs, const chash& h, const cvar& value, R& row) noexcept;

Looks up an equal indexed value and fills the first matching row, returning false when none exists. The field selection controls which row fields are loaded.

has

bool has(T value);
bool has(const cvar& value);
bool has(const chash& h, T value);
bool has(const chash& h, const cvar& value);

Tests for an equal indexed value without returning a row.

query

void query(const CFields& fs, T start, T end, QueryFunc_ f);
void query(const CFields& fs, value_type start, value_type end, QueryFunc_ f);
void query(const CFields& fs, const chash& h, T start, T end, QueryFunc_ f);
void query(const CFields& fs, const chash& h, const cvar& start, const cvar& end, QueryFunc_ f);

Visits an indexed value interval, including equal endpoint matches. A callback returning false stops traversal; compound-index overloads additionally select an equality-key group.

setQuery

void setQuery(T start, T end, RowSet& rs);
void setQuery(value_type start, value_type end, RowSet&);
void setQuery(const chash& h, T start, T end, RowSet& rs);
void setQuery(value_type start, value_type end, RowSet& rs);
void setQuery(const chash& h, const cvar& start, const cvar& end, RowSet& rs);

Adds matching row IDs to an existing result set without clearing it.

vectorQuery

void vectorQuery(T start, T end, RowVector& rs);
void vectorQuery(value_type start, value_type end, RowVector&);
void vectorQuery(const chash& h, T start, T end, RowVector& rs);
void vectorQuery(value_type start, value_type end, RowVector& rs);

Appends matching row IDs to an existing result vector without clearing it.

traverseForward

void traverseForward(const CFields& fs, QueryFunc_ f);
void traverseForward(const CFields& fs, QueryFunc_ f, const chash& h);

Visits rows in forward index order, optionally within a composite-key group. Return false from the callback to stop.

traverseBackward

void traverseBackward(const CFields& fs, QueryFunc_ f);
void traverseBackward(const CFields& fs, QueryFunc_ f, const chash& h);

Visits rows in reverse index order, optionally within a composite-key group. Return false from the callback to stop.

prepare

void prepare();

Validates and orders staged index values before commit. Floating-point NaN is rejected as an index value.

commit

void commit(const UpdateMap& updateMap, RowId startRowId);

Publishes prepared entries using the table’s row-ID allocation and update map. This is part of the coordinated table commit, not a standalone index transaction.

clear

void clear() noexcept;

Discards staged index entries. Already committed table entries remain unchanged.

insert_

void insert_(RowId rowId, const chash& h) noexcept;

Stages a hashed index key using the supplied row offset. This supports index specializations for nonnumeric field types.

CTableData_

template<CCRowId RI> class CTableData_

Types, constants & data

using RowId = RI;
using RowSet = CHashSet<RowId>;
using RowVector = CVector<RowId>;
using QueryFunc_ = std::function<bool(RowId rowId, cvec& r)>;
using UpdateMap = CHashMap<RowId, RowId>;

Methods

CTableData_

CTableData_(CTable_<RowId>* x);

Creates staging state for a borrowed table. The table must remain alive throughout use of this adapter.

dump

void dump();

Writes the underlying table’s diagnostic contents.

getCommitRowId_

RowId getCommitRowId_();

Reserves the persistent row-ID range needed for staged data. Used during the coordinated, locked table commit.

get_

bool get_(const CFields& fs, RowId rowId, cvec& r);
void get_(const RowSet& rs, QueryFunc_ f);
void get_(const RowVector& rs, QueryFunc_ f);

Retrieves committed row data by ID or an ID collection. Callback forms stop when the callback returns false; use the table adapter for typed rows.

getResolved_

bool getResolved_(const CFields& fs, RowId rowId, cvec& r);
void getResolved_(const RowSet& rs, QueryFunc_ f);
void getResolved_(const RowVector& rs, QueryFunc_ f);

Retrieves row data while resolving update mappings. This is a table-integration helper rather than a separate user transaction API.

query_

void query_(size_t indexId, uint32_t start, uint32_t end, QueryFunc_ f);

Visits the specified internal index range with row IDs and values. Return false from the callback to stop traversal.

setQuery_

void setQuery_(size_t indexId, uint32_t start, uint32_t end, RowSet& rs);

Adds row IDs from an internal index range to the supplied set.

vectorQuery_

void vectorQuery_(size_t indexId, uint32_t start, uint32_t end, RowVector& rs);

Appends row IDs from an internal index range to the supplied vector.

traverseForward_

void traverseForward_(size_t indexId, QueryFunc_ f);
void traverseForward_(const CFields& fs, QueryFunc_ f);

Traverses committed rows or the specified index in forward order. Callback rows are valid for the duration of the call.

traverseBackward_

void traverseBackward_(size_t indexId, QueryFunc_ f);
void traverseBackward_(const CFields& fs, QueryFunc_ f);

Traverses committed rows or the specified index in reverse order. Return false from the callback to stop.

insert_

void insert_(cvec& r) noexcept;
void insert_(cvec&& r) noexcept;

Adds a copied or moved row vector to this handle’s staging buffer. It does not publish the row to persistent table data.

commit_

void commit_(RowId startRowId) noexcept;

Commits staged row data starting at the allocated persistent row ID. Table adapters coordinate this with index commits.

rollback_

void rollback_(RowId rowId) noexcept;

Rolls back a reserved commit range after commit preparation fails. The table’s rollback() instead discards pending work before publication.

erase_

void erase_(RowId rowId) noexcept;

Marks a row for deletion in the pending update map. No persistent change is made until commit.

update_

void update_(RowId rowId, RowId newRowId) noexcept;

Maps an existing row ID to its staged replacement. Used together with insertion of the replacement row.

rowId

RowId rowId() const;

Returns the next staging offset, not a newly allocated persistent row ID.

clear_

void clear_() noexcept;

Discards staged rows and update mappings and resets the staging offset.

reserve_

void reserve_(size_t numRows);

Reserves capacity for pending row vectors without changing their number.

readLock_

void readLock_();

Acquires shared access to the underlying table. Pair with readUnlock_().

writeLock_

void writeLock_();

Acquires exclusive access to the underlying table. Pair with writeUnlock_().

readUnlock_

void readUnlock_();

Releases a shared table lock held by the caller.

writeUnlock_

void writeUnlock_();

Releases an exclusive table lock held by the caller.

table

CTable_<RowId>* table();

Returns the borrowed underlying table pointer.

updateMap

const UpdateMap& updateMap() const;

Returns a borrowed view of pending deletion and replacement mappings for the coordinated commit.

count_

size_t count_() const noexcept;

Returns the committed row count of the underlying table.