ProvSQL C/C++ API
Adding support for provenance and uncertainty management to PostgreSQL databases
Loading...
Searching...
No Matches
circuit_cache.h
Go to the documentation of this file.
1/**
2 * @file circuit_cache.h
3 * @brief C-linkage interface to the in-process provenance circuit cache.
4 *
5 * The circuit cache is a bounded, LRU-evicting in-memory store that
6 * avoids repeated round-trips to the mmap-backed persistent circuit
7 * storage for recently accessed gates. It is implemented in C++ (see
8 * @c CircuitCache.h / @c CircuitCache.cpp) but exposed to the C parts of
9 * the extension through this header.
10 *
11 * The cache is a pure read accelerator on top of a write-through design:
12 * @c create_gate (in @c provsql_mmap.c) populates the cache and
13 * unconditionally also forwards the IPC to the background worker, so the
14 * worker is always at least as up-to-date as any backend's cache.
15 * Evicted entries are therefore simply discarded (no flush hook is
16 * needed: the worker already has them). Lookups that miss the cache
17 * fall back to an IPC fetch from the worker.
18 *
19 * Two cache-poisoning hazards motivated the call-site skip rules in
20 * @c provsql_mmap.c (search for "Skip caching" there): @c MMappedCircuit
21 * returns @c gate_input as a default for unknown tokens, and reports
22 * zero children for both real zero-child gates and unknown tokens, so
23 * caching such ambiguous results would short-circuit subsequent real
24 * @c create_gate calls in the same session. The cache itself is
25 * agnostic to that policy.
26 */
27#ifndef CIRCUIT_CACHE_H
28#define CIRCUIT_CACHE_H
29
30#include "provsql_utils.h"
31
32/**
33 * @brief Insert a new gate into the circuit cache.
34 *
35 * Records the gate identified by @p token with the given @p type and
36 * @p nb_children children. If the cache is full the oldest entry is
37 * dropped from memory to make room (the worker already holds it under
38 * the write-through invariant, so no flush is performed). If an entry
39 * for @p token is already present, its @p type and @p children are
40 * overwritten when the incoming call carries strictly more information
41 * (a real @c gate_type replacing a previously stored @c gate_invalid,
42 * or a non-empty children list replacing an empty one); otherwise the
43 * entry is left untouched and just refreshed in LRU order.
44 *
45 * @param token UUID identifying the new gate.
46 * @param type Gate type (e.g. @c gate_input, @c gate_plus).
47 * @param nb_children Number of child gates.
48 * @param children Array of @p nb_children child UUIDs.
49 * @return @c true if the gate was newly inserted, @c false if it was
50 * already present in the cache (regardless of whether its
51 * contents were upgraded).
52 */
53bool circuit_cache_create_gate(pg_uuid_t token, gate_type type, unsigned nb_children, pg_uuid_t *children);
54
55/**
56 * @brief Retrieve the children of a cached gate.
57 *
58 * Looks up @p token in the cache and, on a hit with at least one
59 * child, allocates an array of child UUIDs (via @c calloc) and writes
60 * its address to @p *children.
61 *
62 * @param token UUID of the gate to look up.
63 * @param children Output parameter: set to a freshly @c calloc'd
64 * array of child UUIDs on a hit with children; set to
65 * @c NULL on a miss or on a hit for a zero-child
66 * entry (the two are deliberately indistinguishable
67 * to callers, see the file-level note).
68 * @return Number of children found, or @c 0 on a miss or zero-child
69 * hit. Callers should detect a miss via @c *children==NULL,
70 * not via the return value.
71 */
72unsigned circuit_cache_get_children(pg_uuid_t token, pg_uuid_t **children);
73
74/**
75 * @brief Retrieve the type of a cached gate.
76 *
77 * @param token UUID of the gate to look up.
78 * @return The @c gate_type of the gate, or @c gate_invalid on a miss.
79 */
81
82#endif /* CIRCUIT_CACHE_H */
gate_type circuit_cache_get_type(pg_uuid_t token)
Retrieve the type of a cached gate.
unsigned circuit_cache_get_children(pg_uuid_t token, pg_uuid_t **children)
Retrieve the children of a cached gate.
bool circuit_cache_create_gate(pg_uuid_t token, gate_type type, unsigned nb_children, pg_uuid_t *children)
Insert a new gate into the circuit cache.
Core types, constants, and utilities shared across ProvSQL.
UUID structure.