ProvSQL C/C++ API
Adding support for provenance and uncertainty management to PostgreSQL databases
Loading...
Searching...
No Matches
CircuitCache.cpp
Go to the documentation of this file.
1/**
2 * @file CircuitCache.cpp
3 * @brief LRU circuit-gate cache implementation and C-linkage wrappers.
4 *
5 * Implements @c CircuitCache::insert() and @c CircuitCache::get(), and
6 * the three C-linkage wrapper functions declared in @c circuit_cache.h:
7 * - @c circuit_cache_create_gate()
8 * - @c circuit_cache_get_children()
9 * - @c circuit_cache_get_type()
10 *
11 * The cache is a process-local Boost multi-index container bounded by
12 * @c MAX_CIRCUIT_CACHE_SIZE bytes. On overflow, the oldest (FIFO) entry
13 * is evicted. The C wrappers manage a singleton @c CircuitCache instance
14 * and translate between @c pg_uuid_t / @c gate_type (C types) and the
15 * C++ @c CircuitCacheInfos structure.
16 */
17#include <src/CircuitCache.h>
18
19extern "C" {
20#include "circuit_cache.h"
21}
22
23/** @brief Maximum total byte size of the in-process circuit gate cache (1 MiB). */
24constexpr unsigned MAX_CIRCUIT_CACHE_SIZE = 1 << 20;
25
27{
28 std::pair<iterator,bool> p=il.push_front(infos);
29
30 if(!p.second) {
31 /* Key collision: an entry for this token already exists. If the
32 * incoming entry carries more information (i.e. a real type
33 * replacing a placeholder gate_invalid stored by get_children),
34 * overwrite it; otherwise just touch the LRU position. The
35 * eviction loop is not re-run here: a replace can only grow an
36 * entry by (delta children count) * sizeof(pg_uuid_t), which is
37 * negligible against MAX_CIRCUIT_CACHE_SIZE and self-corrects on
38 * the next true insert. */
39 auto current_size_delta = static_cast<long>(infos.size())
40 - static_cast<long>(p.first->size());
41 bool replace = (p.first->type == gate_invalid && infos.type != gate_invalid)
42 || (p.first->children.empty() && !infos.children.empty());
43 if(replace) {
44 il.replace(p.first, infos);
45 current_size = static_cast<unsigned>(
46 static_cast<long>(current_size) + current_size_delta);
47 }
48 il.relocate(il.begin(),p.first);
49 return false;
50 } else {
51 current_size+=infos.size();
52 while(current_size>MAX_CIRCUIT_CACHE_SIZE && !il.empty()) {
53 /* Evict the LRU tail. Use back() rather than *il.end() to avoid
54 * dereferencing the past-the-end iterator (undefined behaviour). */
55 current_size -= il.back().size();
56 il.pop_back();
57 }
58 return true;
59 }
60}
61
62std::optional<CircuitCacheInfos> CircuitCache::get(pg_uuid_t token) const
63{
64 auto it = il.get<1>().find(token);
65 if(it!=il.get<1>().end())
66 return *it;
67 else
68 return {};
69}
70
71/** @brief Process-local singleton circuit gate cache. */
73
74bool circuit_cache_create_gate(pg_uuid_t token, gate_type type, unsigned nb_children, pg_uuid_t *children)
75{
76 return cache.insert({token, type, std::vector<pg_uuid_t>(children, children+nb_children)});
77}
78
80{
81 auto opt = cache.get(token);
82
83 if(opt) {
84 auto nb_children = opt.value().children.size();
85 /* Avoid calloc(0, ...): on glibc this returns a non-null pointer,
86 * which would defeat the caller's `if(!children)` cache-miss
87 * check. Treat zero-children cache entries as nullptr/0. */
88 if(nb_children == 0) {
89 *children = nullptr;
90 return 0;
91 }
92 *children=reinterpret_cast<pg_uuid_t*>(calloc(nb_children, sizeof(pg_uuid_t)));
93 for(unsigned i=0; i<nb_children; ++i)
94 (*children)[i] = opt.value().children[i];
95 return nb_children;
96 } else {
97 *children = nullptr;
98 return 0;
99 }
100}
101
103{
104 auto opt = cache.get(token);
105 if(opt)
106 return opt.value().type;
107 else
108 return gate_invalid;
109}
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.
constexpr unsigned MAX_CIRCUIT_CACHE_SIZE
Maximum total byte size of the in-process circuit gate cache (1 MiB).
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.
static CircuitCache cache
Process-local singleton circuit gate cache.
LRU in-process cache for recently created provenance circuit gates.
C-linkage interface to the in-process provenance circuit cache.
Bounded LRU cache mapping gate UUIDs to their CircuitCacheInfos.
item_list il
The container holding cached entries.
unsigned current_size
Current total byte usage of cached entries.
bool insert(const CircuitCacheInfos &infos)
Insert a new gate into the cache, evicting the oldest if necessary.
std::optional< CircuitCacheInfos > get(pg_uuid_t token) const
Look up a gate by UUID.
All information stored for a single gate in the circuit cache.
std::vector< pg_uuid_t > children
Ordered list of child gate UUIDs.
unsigned size() const
Estimated memory footprint of this entry in bytes.
gate_type type
Kind of gate (input, plus, times, …).
UUID structure.