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 il.relocate(il.begin(),p.first);
32 return true;
33 } else {
34 current_size+=infos.size();
35 while(current_size>MAX_CIRCUIT_CACHE_SIZE && !il.empty()) {
36 current_size -= (*il.end()).size();
37 il.pop_back();
38 }
39 return false;
40 }
41}
42
43std::optional<CircuitCacheInfos> CircuitCache::get(pg_uuid_t token) const
44{
45 auto it = il.get<1>().find(token);
46 if(it!=il.get<1>().end())
47 return *it;
48 else
49 return {};
50}
51
52/** @brief Process-local singleton circuit gate cache. */
54
55bool circuit_cache_create_gate(pg_uuid_t token, gate_type type, unsigned nb_children, pg_uuid_t *children)
56{
57 return cache.insert({token, type, std::vector<pg_uuid_t>(children, children+nb_children)});
58}
59
61{
62 auto opt = cache.get(token);
63
64 if(opt) {
65 auto nb_children = opt.value().children.size();
66 *children=reinterpret_cast<pg_uuid_t*>(calloc(nb_children, sizeof(pg_uuid_t)));
67 for(unsigned i=0; i<nb_children; ++i)
68 (*children)[i] = opt.value().children[i];
69 return nb_children;
70 } else {
71 *children = nullptr;
72 return 0;
73 }
74}
75
77{
78 auto opt = cache.get(token);
79 if(opt)
80 return opt.value().type;
81 else
82 return gate_invalid;
83}
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.
gate_type
Possible gate type in the provenance circuit.
@ gate_invalid
Invalid gate type.
All information stored for a single gate in the circuit cache.
unsigned size() const
Estimated memory footprint of this entry in bytes.
UUID structure.