Vector Index Class
gxVectorIndex keeps vectors in a .vix file built on gxDatabase, with exact search and an HNSW graph for fast approximate search (DataReel 7.4).
Overview
Files
Nodes
Searches
Vector Members
Maintenance
Format Fields (Phase D)
Overview
A .vix file holds nodes of a fixed size: an id (a long long, often the address of the record the vector belongs to), four unsigned tags, the vector in its stored form and the node's graph links. Exact() compares the query with every live node; Search() walks the graph (HNSW) and is exact for small indexes and for filters that pass fewer than about 5% of the nodes.
Element types: gxvFLOAT32, gxvFLOAT16 (IEEE binary16, half the size) and gxvINT8 (signed bytes and a float scale per vector). Metrics: gxvL2 (squared distance), gxvCOSINE (the vectors kept at unit length) and gxvIP (inner product, largest first). A spec string names what the vectors are (a model, a schema); Open() with a spec refuses a file of another.
Tags are four unsigned values per node; a gxvFilter keeps nodes whose tags are in ranges (and an optional accept function). By convention tag 2 holds the day (yyyymmdd): the file keeps the smallest and largest tag 2 so a gxVectorSet can skip it.
Transactions: attach Database() to a gxdJournal to make writes all or nothing. After Rollback() call Reload(): the file is restored and the nodes in memory must be read again.
Vector members: a record can keep its vector in the index's stored form (MakeMember()), so the index can be made again from the records alone (gxvmember.h, vix_rebuild).
Every function returns gxv_NO_ERROR (0) or a gxvError; ErrorMessage() describes it. The examples: vector_basics, vector_records.
gxVectorIndex vix;
vix.Create("notes.vix", 384, gxvFLOAT16, gxvCOSINE, 16, "notes/v1");
unsigned tags[gxvTagCount] = { 0, 0, 20261115, 0 }; // tag 2: the day
vix.Insert(record_address, vec, tags);
std::vector<gxvResult> r;
vix.Search(query, 10, r, 64); // the 10 nearest, closest first
Files
gxvError Create(const char *fname, unsigned dim, gxvElementType type = gxvFLOAT32, gxvMetric metric = gxvL2, unsigned M = 16, const char *spec = 0, unsigned ef_construction = 200)Creates a new index. M is the number of graph links per node (twice that on the bottom level); ef_construction is the search width used while inserting.
gxvError Open(const char *fname, gxDatabaseAccessMode mode = gxDBASE_READWRITE, const char *spec = 0)Opens an index; a spec, when given, must equal the file's. A phase A file (no saved graph) is searched after the graph is built in memory: GraphSaved() is 0 until Repair().
gxvError Close() / Flush()Writes the header and closes, or writes the header only.
gxvError Reload()Reads the nodes from the file again (after a gxdJournal Rollback(), or a file another process changed).
gxDatabase *Database()The file, to attach to a gxdJournal. Write to it only through this class.
Nodes
gxvError Insert(long long id, const float *vec, const unsigned *tags = 0)Adds a node. gxv_DUPLICATE_ID if the id is live; gxv_OUT_OF_RANGE for a float16 value beyond 65504; gxv_NOT_FINITE for a NaN or infinite value.
gxvError Update(long long id, const float *vec, const unsigned *tags = 0)The same vector: the tags are rewritten in place. A new vector: the old node is deleted and a new one inserted.
gxvError Delete(long long id)A tombstone: the node stays in the file, is never found, and is counted by Deleted(). Repair() drops tombstones from the graph.
int Contains(long long id) const / gxvError Get(long long id, float *vec, unsigned *tags = 0) constWhether an id is live; its stored vector (unit length for cosine) and tags.
gxvError Rename(long long old_id, long long new_id)The record moved to a new address: the node gets the new id.
unsigned long long Count() const / Deleted() const / ChangeCount() constLive nodes, tombstones, and 1 + the number of writes since Open() (lets a reader see a change).
Searches
gxvError Exact(const float *query, unsigned k, std::vector<gxvResult> &results, const gxvFilter *filter = 0) constThe k nearest live nodes that pass the filter, closest first, by comparing every node.
gxvError Search(const float *query, unsigned k, std::vector<gxvResult> &results, unsigned ef = 64, const gxvFilter *filter = 0) constApproximate search over the graph; a larger ef is closer to Exact() and slower.
struct gxvFilter: void Tag(unsigned i, unsigned value) / void Range(unsigned i, unsigned lo, unsigned hi)Keep nodes whose tag i equals value, or is in [lo, hi]; accept(id, tags, data), when set, is asked last.
Vector Members
gxvError MakeMember(const float *vec, std::vector<unsigned char> &member) constThe vector in the stored form for a record: a 12-byte head (type, dimension, spec CRC) and the code bytes (MemberSize() in all).
gxvError InsertMember(long long id, const unsigned char *member, unsigned len, const unsigned *tags = 0) / UpdateMember(...)Puts a member's code in the index unchanged, so record and node hold the same bytes; a member of another type, dimension or spec is refused.
gxvError MemberVector(const unsigned char *member, unsigned len, float *vec) const / int MemberMatches(long long id, const unsigned char *member, unsigned len) constDecodes a member; whether node id holds exactly this member's code.
Maintenance
gxvError Repair()Makes the graph again from the live nodes and writes it (attach the journal first to make it all or nothing).
unsigned long Verify(int (*resolve)(long long id, void *data) = 0, void *data = 0, std::vector<long long> *bad = 0, std::string *report = 0)Checks the file against memory and, with a resolver, whether each node's object still exists. Returns the number of problems.
static gxvError Merge(const char *out_fname, const std::vector<std::string> &inputs, ...)Builds one index from the live nodes of several (the day files of a month), every link written once.
unsigned long long LogicalDigest(unsigned long long *nodes = 0) constA hash of the live nodes in id order (id, stored code, tags): the same for two indexes that hold the same nodes, whatever their graphs.
Format Fields (Phase D)
unsigned FeaturesRequired() const / FeaturesOptional() constFeature bits: a reader refuses a file with a required bit it does not know; optional bits gxvOptTagRanges (tag ranges kept) and gxvOptFromMembers (derived from record members).
unsigned Codec() const / GraphLayout() const / PartitionId() const / PartitionCount() constThe stored code's codec (0: plain), the graph layout, and the file's partition of a set.
int TagRange(unsigned i, unsigned &lo, unsigned &hi) const / int TagRangeExcludes(unsigned i, unsigned lo, unsigned hi) constThe smallest and largest value tag i ever had; 1 when no node can have tag i in [lo, hi].
static const char *ErrorMessage(gxvError err)A description of an error code.