ProvSQL C/C++ API
Adding support for provenance and uncertainty management to PostgreSQL databases
Loading...
Searching...
No Matches
CircuitCache.h
Go to the documentation of this file.
1/**
2 * @file CircuitCache.h
3 * @brief LRU in-process cache for recently created provenance circuit gates.
4 *
5 * Reading gates from the mmap-backed persistent storage is relatively
6 * expensive. The @c CircuitCache stores the most recently created gates
7 * in an in-memory Boost multi-index container so that subsequent lookups
8 * within the same backend transaction can skip the IPC round-trip to the
9 * background worker.
10 *
11 * The cache is bounded by a fixed byte budget. When inserting a new gate
12 * would exceed the budget the oldest entry (FIFO order) is evicted.
13 * Evicted entries must be flushed to the background worker by the caller.
14 *
15 * The C-linkage wrapper functions in @c circuit_cache.h provide the
16 * interface used by the C portions of the extension.
17 */
18#ifndef CIRCUIT_CACHE_CPP_H
19#define CIRCUIT_CACHE_CPP_H
20
21#include "provsql_utils_cpp.h"
22
23#include <optional>
24#include <utility>
25#include <vector>
26
27#include <boost/multi_index_container.hpp>
28#include <boost/multi_index/hashed_index.hpp>
29#include <boost/multi_index/member.hpp>
30#include <boost/multi_index/sequenced_index.hpp>
31
32using namespace boost::multi_index;
33
34/**
35 * @brief All information stored for a single gate in the circuit cache.
36 *
37 * Each cache entry tracks the gate's UUID token, its type, and the list
38 * of its children (also as UUIDs).
39 */
41{
42 pg_uuid_t token; ///< UUID identifying this gate
43 gate_type type; ///< Kind of gate (input, plus, times, …)
44 std::vector<pg_uuid_t> children; ///< Ordered list of child gate UUIDs
45
46 /**
47 * @brief Estimated memory footprint of this entry in bytes.
48 *
49 * Used by @c CircuitCache to track the total size of the cache so that
50 * it can enforce its byte budget.
51 *
52 * @return Size of @c CircuitCacheInfos plus the children array.
53 */
54 inline unsigned size() const {
55 return sizeof(CircuitCacheInfos)+children.size()*sizeof(pg_uuid_t);
56 }
57};
58
59/**
60 * @brief Bounded LRU cache mapping gate UUIDs to their @c CircuitCacheInfos.
61 *
62 * Internally a Boost @c multi_index_container provides two views of the
63 * same data:
64 * - A sequenced (insertion-order) index used to evict the oldest entry
65 * when the cache is full.
66 * - A hashed-unique index keyed on @c token for O(1) lookup.
67 *
68 * The cache is not thread-safe; each backend process maintains its own
69 * instance.
70 */
72{
73/** @brief Boost multi-index container type for cache entries. */
74typedef multi_index_container<
76 indexed_by<
77 sequenced<>,
78 hashed_unique<member<CircuitCacheInfos, pg_uuid_t, &CircuitCacheInfos::token> >
79 >
81item_list il; ///< The container holding cached entries
82unsigned current_size; ///< Current total byte usage of cached entries
83
84public:
85/** @brief The value type stored in the cache. */
87/** @brief Iterator type for iterating over cache entries in FIFO order. */
88typedef typename item_list::iterator iterator;
89
90/** @brief Construct an empty cache. */
93
94/**
95 * @brief Insert a new gate into the cache, evicting the oldest if necessary.
96 *
97 * If @p infos.token is already present in the cache the insert is a
98 * no-op and the function returns @c false. Otherwise the entry is
99 * added and, if the cache exceeds its size budget, the oldest entry is
100 * removed.
101 *
102 * @param infos Gate information to cache.
103 * @return @c true if the entry was newly inserted, @c false if it was
104 * already present.
105 */
106bool insert(const CircuitCacheInfos& infos);
107
108/**
109 * @brief Look up a gate by UUID.
110 *
111 * @param token UUID of the gate to find.
112 * @return An @c std::optional containing the @c CircuitCacheInfos if
113 * found, or @c std::nullopt on a cache miss.
114 */
115std::optional<CircuitCacheInfos> get(pg_uuid_t token) const;
116
117/**
118 * @brief Iterator to the first cached entry (oldest).
119 * @return Iterator pointing to the oldest cache entry.
120 */
122 return il.begin();
123}
124/**
125 * @brief Past-the-end iterator for the cache.
126 * @return Iterator one past the last cache entry.
127 */
128inline iterator end(){
129 return il.end();
130}
131};
132
133#endif /* CIRCUIT_CACHE_CPP_H */
Bounded LRU cache mapping gate UUIDs to their CircuitCacheInfos.
CircuitCache()
Construct an empty cache.
multi_index_container< CircuitCacheInfos, indexed_by< sequenced<>, hashed_unique< member< CircuitCacheInfos, pg_uuid_t, &CircuitCacheInfos::token > > > > item_list
Boost multi-index container type for cache entries.
item_list il
The container holding cached entries.
item_list::iterator iterator
Iterator type for iterating over cache entries in FIFO order.
unsigned current_size
Current total byte usage of cached entries.
CircuitCacheInfos item_type
The value type stored in the cache.
iterator end()
Past-the-end iterator for the cache.
iterator begin()
Iterator to the first cached entry (oldest).
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.
C++ utility functions for UUID manipulation.
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, …)
pg_uuid_t token
UUID identifying this gate.
UUID structure.