§ Building & Searching
A single basic::ram_index is a complete index on its own.
It needs no SafeGraphs files, no sharded index around it and no UUIDs.
Keys may be any hashable type, node IDs may be 32 or 64 bits wide, and every record keeps its own key.
The sharded indexes in ram_indexes.hpp, vram_nvidia_indexes.hpp, vram_amd_indexes.hpp and vram_apple_indexes.hpp are what the compiled library builds on top.
Only they narrow the choice, to 32-bit shards whose nodes a shard table locates in 8 bytes.
An index is a template over the payload, the key, the node ID width and the distance type.
It allocates nothing until its first reserve or insert, then grows on demand in chunks of node records that never move, so reserve is only a hint that covers ahead.
Every record builds its value from the index’s allocator by uses-allocator construction, so the values a caller inserts are best built from index.get_allocator() too.
Each thread brings its own metric instance and its own traversal context, which holds the walk’s scratch, sits on any allocator such as a per-thread arena, and receives the matches:
using index_t = us::basic::ram_index<std::vector<float>, us::util::u64_t>;
index_t index;
if (!index.reserve(1000)) return 1;
manhattan_metric_t metric;
index_t::traversal_context_t context {/* seed */ 42};
std::vector<std::vector<float>> values = {{0, 0, 1}, {0, 1, 0}, {1, 0, 0}};
std::vector<us::util::u64_t> keys = {10, 20, 30};
auto const [inserted, insert_status] = index.insert(std::span {values}, std::span<us::util::u64_t const> {keys},
metric, std::span<us::util::u64_t> {}, context,
us::basic::keep_diverse_t {16});
std::vector<float> const query = {0, 0, 0.9f};
auto const [found, search_status] = index.search(query, metric, context);
for (std::size_t i = 0; i != found.count; ++i) {
auto const [key, key_status] = index.key_of(context.results[i].node_id);
// `context.results[i].distance` is the match's score, ascending.
}insertmoves a batch of payloads in under their keys and is all-or-nothing: capacity and scratch for the whole batch are secured before the first value moves.plan_insertspreads one batch over threads: it claims, covers and dispenses the whole ID range, each thread places a disjoint slice through the plan’s owninsertwith a contextreserve_traversalsized, andfinishpublishes the batch at once. Nothing can fail past the dispense, so every position of a plan must be placed, and a refused fan-out places on the calling thread.- The selector decides which candidates become neighbors, and holds the neighbors-per-node budget:
basic::keep_diverse_tprunes by diversity,basic::keep_closest_tkeeps the nearest. Both are per-call values, so heuristics and budgets can change between batches. searchleaves its matches incontext.results, nearest first, andkey_of,value_of,findandcounttranslate between node IDs and keys.- A search may score with a different metric than the one the graph was built with, and a
foldpassed tosearchreads every scored round, for walks that reduce to something other than the nearest matches.