ProvSQL C/C++ API
Adding support for provenance and uncertainty management to PostgreSQL databases
Loading...
Searching...
No Matches
CircuitFromMMap.cpp
Go to the documentation of this file.
1/**
2 * @file CircuitFromMMap.cpp
3 * @brief Build in-memory circuits from the mmap-backed store.
4 *
5 * Implements the free functions declared in @c CircuitFromMMap.h:
6 * - @c getBooleanCircuit(): reads the mmap store (via the background
7 * worker IPC channel) and constructs a @c BooleanCircuit.
8 * - @c getGenericCircuit(): same but constructs a @c GenericCircuit.
9 *
10 * The internal @c getCircuitFromMMap<C>() template handles the IPC
11 * protocol: it sends a request through the shared-memory pipe,
12 * receives a Boost-serialised circuit blob from the background worker,
13 * and deserialises it into the appropriate circuit type.
14 */
15#include <cmath>
16
17#include <boost/archive/binary_iarchive.hpp>
18#include <boost/iostreams/device/array.hpp>
19#include <boost/iostreams/stream.hpp>
20
21#include "CircuitFromMMap.h"
22#include "having_semantics.hpp"
23#include "semiring/BoolExpr.h"
24#include "provsql_utils_cpp.h"
25
26extern "C" {
27#include "provsql_shmem.h"
28#include "provsql_mmap.h"
29}
30
31/**
32 * @brief Read and deserialise a circuit rooted at @p token from the mmap worker.
33 * @tparam C Circuit type to deserialise (@c BooleanCircuit or @c GenericCircuit).
34 * @param token UUID of the root gate to retrieve.
35 * @param message_char IPC message-type byte sent to the background worker.
36 * @return Deserialised circuit of type @c C.
37 */
38template<typename C>
39static C getCircuitFromMMap(pg_uuid_t token, char message_char)
40{
42 if(!WRITEM(&message_char, char) || !WRITEM(&token, pg_uuid_t))
43 provsql_error("Cannot write to pipe (message type %c)", message_char);
44
45 unsigned long size;
46 if(!READB(size, unsigned long))
47 provsql_error("Cannot read from pipe (message type %c)", message_char);
48
49 char *buf = new char[size], *p = buf;
50 ssize_t actual_read, remaining_size=size;
51 while((actual_read=read(provsql_shared_state->pipembr, p, remaining_size))<remaining_size) {
52 if(actual_read<=0) {
54 delete [] buf;
55 provsql_error("Cannot read from pipe (message type %c)", message_char);
56 } else {
57 remaining_size-=actual_read;
58 p+=actual_read;
59 }
60 }
62
63 boost::iostreams::stream<boost::iostreams::array_source> stream(buf, size);
64 boost::archive::binary_iarchive ia(stream);
65 C c;
66 ia >> c;
67
68 delete [] buf;
69
70 return c;
71}
72
74{
76 auto ggate = gc.getGate(uuid2string(token));
78 std::unordered_map<gate_t, gate_t> mapping;
79 for(gate_t u: gc.getInputs()) {
80 mapping[u]=c.setGate(gc.getUUID(u), BooleanGate::IN, gc.getProb(u));
81 }
82 for(size_t i=0; i<gc.getNbGates(); ++i) {
83 auto u=static_cast<gate_t>(i);
84 if(gc.getGateType(u)==gate_mulinput) {
85 mapping[u]=c.setGate(gc.getUUID(u), BooleanGate::MULIN, gc.getProb(u));
86 c.setInfo(mapping[u], gc.getInfos(u).first);
87 c.addWire(
88 mapping[u],
89 mapping[gc.getWires(u)[0]]);
90 }
91 }
94 gate=gc.evaluate(ggate, mapping, semiring);
95
96 return c;
97}
98
100{
101 return getCircuitFromMMap<GenericCircuit>(token, 'g');
102}
Boolean-expression (lineage formula) semiring.
@ IN
Input (variable) gate representing a base tuple.
@ MULIN
Multivalued-input gate (one of several options)
BooleanCircuit getBooleanCircuit(pg_uuid_t token, gate_t &gate)
Build a BooleanCircuit from the mmap store rooted at token.
static C getCircuitFromMMap(pg_uuid_t token, char message_char)
Read and deserialise a circuit rooted at token from the mmap worker.
GenericCircuit getGenericCircuit(pg_uuid_t token)
Build a GenericCircuit from the mmap store rooted at token.
Build in-memory circuits from the mmap-backed persistent store.
gate_t
Strongly-typed gate identifier.
Definition Circuit.h:48
Boolean circuit for provenance formula evaluation.
gate_t setGate(BooleanGate type) override
Allocate a new gate with type type and no UUID.
void setInfo(gate_t g, unsigned info)
Store an integer annotation on gate g.
std::vector< gate_t > & getWires(gate_t g)
Return a mutable reference to the child-wire list of gate g.
Definition Circuit.h:139
gateType getGateType(gate_t g) const
Return the type of gate g.
Definition Circuit.h:129
void addWire(gate_t f, gate_t t)
Add a directed wire from gate f (parent) to gate t (child).
Definition Circuit.hpp:81
uuid getUUID(gate_t g) const
Return the UUID string associated with gate g.
Definition Circuit.hpp:46
gate_t getGate(const uuid &u)
Return (or create) the gate associated with UUID u.
Definition Circuit.hpp:33
std::vector< gate_t >::size_type getNbGates() const
Return the total number of gates in the circuit.
Definition Circuit.h:102
In-memory provenance circuit with semiring-generic evaluation.
S::value_type evaluate(gate_t g, std::unordered_map< gate_t, typename S::value_type > &provenance_mapping, S semiring) const
Evaluate the sub-circuit rooted at gate g over semiring semiring.
double getProb(gate_t g) const
Return the probability for gate g.
const std::set< gate_t > & getInputs() const
Return the set of input (leaf) gates.
std::pair< unsigned, unsigned > getInfos(gate_t g) const
Return the integer annotation pair for gate g.
Provenance-as-Boolean-circuit semiring.
Definition BoolExpr.h:42
void provsql_try_having_boolexpr(GenericCircuit &c, semiring::BoolExpr &be, gate_t g, std::unordered_map< gate_t, gate_t > &mapping)
Evaluate the HAVING sub-circuit at g over the BoolExpr semiring.
Provenance evaluation helpers for HAVING-clause circuits.
#define provsql_error(fmt,...)
Report a fatal ProvSQL error and abort the current transaction.
Background worker and IPC primitives for mmap-backed circuit storage.
#define WRITEM(pvar, type)
Write one value of type to the background-to-main pipe.
#define READB(var, type)
Read one value of type from the main-to-background pipe.
void provsql_shmem_unlock(void)
Release the ProvSQL LWLock.
void provsql_shmem_lock_exclusive(void)
Acquire the ProvSQL LWLock in exclusive mode.
provsqlSharedState * provsql_shared_state
Pointer to the ProvSQL shared-memory segment (set in provsql_shmem_startup).
Shared-memory segment and inter-process pipe management.
@ gate_mulinput
Multivalued input (for Boolean provenance)
string uuid2string(pg_uuid_t uuid)
Format a pg_uuid_t as a std::string.
C++ utility functions for UUID manipulation.
UUID structure.
long pipembr
Main-to-background pipe: read end (worker reads)