libcorm โ€” Complete API Reference

This document provides the exhaustive API reference, types, macros, callbacks, and function tables for libcorm. For a high-level overview, quickstart, and ecosystem context, see the main README.

Type System

libcorm supports both built-in and custom types for keys and values.

Built-in Type Constants

ConstantDescription
CM_U3232-bit unsigned integer (4 bytes, ordered)
CM_STRNull-terminated string (variable length, ordered lexicographically)
CM_HNDLMap handle (uint32_t, used for maps-of-maps)
CM_PTRRaw pointer (fixed-size, ordered by address)

Custom Type Registration

Register fixed-length or variable-length types for use as keys or values:

uint32_t qm_point = corm_reg(sizeof(struct point));      // fixed-length
uint32_t qm_blob = corm_mreg(measure_blob);              // variable-length
FunctionSignatureDescription
corm_reguint32_t corm_reg(size_t len)Register a fixed-length type. Returns type ID or CM_MISS.
corm_mreguint32_t corm_mreg(corm_measure_t *measure)Register a variable-length type. measure callback returns byte size.
corm_type_lensize_t corm_type_len(uint32_t type_id)Get fixed byte-length of a type, or 0 if variable-length.
corm_lensize_t corm_len(uint32_t type_id, const void *data)Get byte length of a specific element.
corm_cmp_setvoid corm_cmp_set(uint32_t ref, corm_cmp_t *cmp)Override comparison function for a type.

Callback typedefs:

TypeSignatureDescription
corm_measure_tsize_t (*)(const void *data)Returns byte size of a variable-length element.
corm_cmp_tint (*)(const void *a, const void *b, size_t len)Comparison: returns <0, 0, >0.

Flags

FlagValueApplies toDescription
CM_SORTEDโ€”corm_openKeep entries sorted by key (required for CM_MULTIVALUE).
CM_MULTIVALUEโ€”corm_openAllow multiple values per key (requires CM_SORTED).
CM_MIRRORโ€”corm_openCreate bidirectional reverse-lookup mirror (handle + 1).
CM_AINDEXโ€”corm_openAuto-index: assign sequential integer IDs for each unique key.
CM_NOGROWโ€”corm_openDisallow auto-growth beyond initial mask capacity.
CM_RANGEโ€”corm_iterEnable ordered range scan over sorted keys.
CM_RECORD()โ€”corm_openDeclare vtype as a record type for field-level access.

Sentinel: CM_MISS (UINT32_MAX) is returned by corm_open, corm_reg, and corm_iter on failure.


Iteration

Iterate over all entries in a map, or over entries matching a key:

// Iterate all entries
uint32_t cur = corm_iter(hd, NULL, 0);
const void *k, *v;
while (corm_next(&k, &v, cur)) {
    printf("key=%u val=%s\n", *(uint32_t *)k, (const char *)v);
}
corm_fin(cur);

// Range scan: iterate entries with keys in [2, 5)
uint32_t key_start = 2;
uint32_t cur2 = corm_iter(hd, &key_start, CM_RANGE);
while (corm_next(&k, &v, cur2)) {
    uint32_t k32 = *(uint32_t *)k;
    if (k32 >= 5) break;  // past range end
    printf("key=%u val=%s\n", k32, (const char *)v);
}
corm_fin(cur2);
FunctionSignatureDescription
corm_iteruint32_t corm_iter(uint32_t hd, const void *key, uint32_t flags)Start iteration. key=NULL iterates all entries; CM_RANGE enables range scan. Returns cursor handle or CM_MISS.
corm_nextint corm_next(const void **key, const void **value, uint32_t cur_id)Fetch next key/value from cursor. Returns 1 if valid, 0 if done.
corm_finvoid corm_fin(uint32_t cur_id)End iteration early and free cursor.

Multi-Value Support (CM_MULTIVALUE)

For scenarios where multiple values need to share the same key (such as secondary indexes), use the CM_MULTIVALUE flag.

Requirements:

Behavior:

Deletion Patterns:

time_t max_time = 9999;
corm_del(by_time, &max_time);      // Delete first occurrence
corm_del_all(by_time, &max_time);  // Delete all occurrences
corm_drop(by_time);                // Drop all entries (clear entire map)

Associations (Secondary Indexes)

Link a secondary (index) map to a primary map. Puts and deletes on the primary auto-update the secondary via a callback that extracts the secondary key:

static void index_by_time(const void **skey, const void *pkey,
                           const void *value, void *ud)
{
    (void)pkey; (void)ud;
    const struct interval *iv = value;
    *(const time_t **)skey = &iv->max;  // secondary key = interval.max
}

corm_assoc(by_time, primary, index_by_time, NULL);
FunctionSignatureDescription
corm_assocvoid corm_assoc(uint32_t hd, uint32_t link, corm_assoc_t cb, void *userdata)Link secondary map to primary. Callback extracts a single secondary key.
corm_assoc_multivoid corm_assoc_multi(uint32_t hd, uint32_t link, corm_assoc_multi_t cb, void *userdata)Like corm_assoc but callback produces multiple secondary keys per primary entry.

Record-Aware Maps

For structured data with named fields, register a record layout and open maps with CM_RECORD() for field-level get/put and automatic reference resolution.

FunctionSignatureDescription
corm_record_registeruint32_t corm_record_register(const char *name, size_t struct_size, const corm_record_field_t *fields, size_t field_count)Register record layout. Returns record ID or CM_MISS.
corm_record_type_iduint32_t corm_record_type_id(uint32_t record_id)Get type ID for use as vtype in corm_open.
corm_get_keyconst char *corm_get_key(uint32_t hd, uint32_t pos)Get string key at position.
corm_posuint32_t corm_pos(uint32_t hd, const char *key)Get position for key.
corm_field_putuint32_t corm_field_put(uint32_t hd, const char *item_id, const char *field_name, const char *value)Set field value.
corm_field_getconst char *corm_field_get(uint32_t hd, const char *item_id, const char *field_name)Get field value.
corm_inv_getsize_t corm_inv_get(uint32_t hd, const char *field_name, uint32_t target_pos, uint32_t *out, size_t max)Inverse index query.
corm_record_field_set_target_hdvoid corm_record_field_set_target_hd(uint32_t record_id, const char *field_name, uint32_t target_hd)Set reference field target.

ID Management

libcorm provides a free-list ID allocator (idm_t) for managing reusable integer IDs alongside CM_AINDEX maps:

FunctionSignatureDescription
idm_initidm_t idm_init(void)Initialize empty ID manager.
idm_newuint32_t idm_new(idm_t *idm)Allocate ID (reuses freed IDs first).
idm_delint idm_del(idm_t *idm, uint32_t id)Free ID.
idm_pushuint32_t idm_push(idm_t *idm, uint32_t n)Bulk-push IDs.
idm_dropvoid idm_drop(idm_t *idm)Free all managed IDs.
ids_initids_t ids_init(void)Init raw ID list.
ids_pushvoid ids_push(ids_t *list, uint32_t id)Push ID.
ids_popuint32_t ids_pop(ids_t *list)Pop ID.
ids_peekuint32_t ids_peek(ids_t *list)Peek at top ID.
ids_dropvoid ids_drop(ids_t *list)Free all IDs in list.
ids_iteridsi_t *ids_iter(ids_t *list)Start iteration.
ids_nextint ids_next(uint32_t *id, idsi_t **cur)Next ID.

Recall Kernel (cormr.h)

FunctionSignatureDescription
rec_set_newrec_set_t *rec_set_new(void)New empty, unsorted set (arena-backed).
rec_set_pushvoid rec_set_push(rec_set_t *s, rec_ref_t r)Append a ref (unseals).
rec_set_sealvoid rec_set_seal(rec_set_t *s)Sort + dedup in place; required before joins.
rec_set_fill_corm_iterint rec_set_fill_corm_iter(rec_set_t *s, uint32_t hd)Drain a fixed-key corm handle into the set.
rec_set_intersectint rec_set_intersect(rec_set_t *dst, const rec_set_t *a, const rec_set_t *b)dst := a โˆฉ b (sealed, merge-join).
rec_set_subtractint rec_set_subtract(rec_set_t *dst, const rec_set_t *a, const rec_set_t *b)dst := a โˆ’ b (sealed).
rec_set_unionint rec_set_union(rec_set_t *dst, const rec_set_t *a, const rec_set_t *b)dst := a โˆช b (sealed).
rec_set_countsize_t rec_set_count(const rec_set_t *s)Number of refs held.
rec_set_atconst rec_ref_t *rec_set_at(const rec_set_t *s)Backing array (sealed โ†’ sorted, deduped).
rec_set_freevoid rec_set_free(rec_set_t *s)Release storage.
rec_rank_newrec_rank_t *rec_rank_new(size_t top_k, float min_score)New bounded top-k buffer (top_k 0 โ†’ NULL).
rec_rank_pushvoid rec_rank_push(rec_rank_t *rk, rec_ref_t r, float score)Stream a scored ref; O(log top_k).
rec_rank_sortedsize_t rec_rank_sorted(const rec_rank_t *rk, rec_ref_t *refs, float *scores)Retained results best-first; returns count.
rec_rank_freevoid rec_rank_free(rec_rank_t *rk)Release the buffer.

Complete Core API Reference Table

CategoryFunctionSignatureDescription
Lifecyclecorm_openuint32_t corm_open(const char *filename, const char *database, uint32_t ktype, uint32_t vtype, uint32_t mask, uint32_t flags)Open/create a map.
corm_savevoid corm_save(void)Write all file-backed maps to disk.
corm_closevoid corm_close(uint32_t hd)Close a map and free entries.
corm_dropvoid corm_drop(uint32_t hd)Remove all entries (keep map open).
corm_get_vtypeuint32_t corm_get_vtype(uint32_t hd)Get value type ID for a map.
CRUDcorm_getconst void *corm_get(uint32_t hd, const void *key)Get value by key.
corm_putuint32_t corm_put(uint32_t hd, const void *key, const void *value)Insert/update key-value.
corm_delvoid corm_del(uint32_t hd, const void *key)Delete entry by key (first match for MULTIVALUE).
corm_del_allvoid corm_del_all(uint32_t hd, const void *key)Delete all entries matching key.
Iterationcorm_iteruint32_t corm_iter(uint32_t hd, const void *key, uint32_t flags)Start iteration over entries.
corm_nextint corm_next(const void **key, const void **value, uint32_t cur_id)Next key/value from cursor.
corm_finvoid corm_fin(uint32_t cur_id)End iteration.
corm_get_multiuint32_t corm_get_multi(uint32_t hd, const void *key)Iterate all values for a MULTIVALUE key.
corm_countuint32_t corm_count(uint32_t hd, const void *key)Count entries matching key.
Typescorm_reguint32_t corm_reg(size_t len)Register fixed-length type.
corm_mreguint32_t corm_mreg(corm_measure_t *measure)Register variable-length type.
corm_type_lensize_t corm_type_len(uint32_t type_id)Get type byte length.
corm_lensize_t corm_len(uint32_t type_id, const void *data)Get element byte length.
corm_cmp_setvoid corm_cmp_set(uint32_t ref, corm_cmp_t *cmp)Override comparison for a type.