souffle  2.0.2-371-g6315b36
SerialisationStream.h
Go to the documentation of this file.
1 /*
2  * Souffle - A Datalog Compiler
3  * Copyright (c) 2020, The Souffle Developers. All rights reserved.
4  * Licensed under the Universal Permissive License v 1.0 as shown at:
5  * - https://opensource.org/licenses/UPL
6  * - <souffle root>/licenses/SOUFFLE-UPL.txt
7  */
8 
9 /************************************************************************
10  *
11  * @file SerialisationStream.h
12  *
13  * Defines a common base class for relation serialisation streams.
14  *
15  ***********************************************************************/
16 
17 #pragma once
18 
19 #include "souffle/RamTypes.h"
20 
21 #include "souffle/utility/json11.h"
22 #include <cassert>
23 #include <cstddef>
24 #include <map>
25 #include <string>
26 #include <utility>
27 #include <vector>
28 
29 namespace souffle {
30 
31 class RecordTable;
32 class SymbolTable;
33 
34 using json11::Json;
35 
36 template <bool readOnlyTables>
37 class SerialisationStream {
38 public:
39  virtual ~SerialisationStream() = default;
40 
41 protected:
42  template <typename A>
43  using RO = std::conditional_t<readOnlyTables, const A, A>;
44 
46  std::vector<std::string> relTypes, size_t auxArity = 0)
47  : symbolTable(symTab), recordTable(recTab), types(std::move(types)),
48  typeAttributes(std::move(relTypes)), arity(typeAttributes.size() - auxArity),
49  auxiliaryArity(auxArity) {}
50 
52  : symbolTable(symTab), recordTable(recTab), types(std::move(types)) {
53  setupFromJson();
54  }
55 
56  SerialisationStream(RO<SymbolTable>& symTab, RO<RecordTable>& recTab,
57  const std::map<std::string, std::string>& rwOperation)
58  : symbolTable(symTab), recordTable(recTab) {
59  std::string parseErrors;
60  types = Json::parse(rwOperation.at("types"), parseErrors);
61  assert(parseErrors.size() == 0 && "Internal JSON parsing failed.");
63  }
64 
65  RO<SymbolTable>& symbolTable;
66  RO<RecordTable>& recordTable;
67  Json types;
68  std::vector<std::string> typeAttributes;
69 
70  size_t arity = 0;
71  size_t auxiliaryArity = 0;
72 
73 private:
74  void setupFromJson() {
75  auto&& relInfo = types["relation"];
76  arity = static_cast<size_t>(relInfo["arity"].long_value());
77  auxiliaryArity = static_cast<size_t>(relInfo["auxArity"].long_value());
78 
79  assert(relInfo["types"].is_array());
80  auto&& relTypes = relInfo["types"].array_items();
81  assert(relTypes.size() == (arity + auxiliaryArity));
82 
83  for (size_t i = 0; i < arity + auxiliaryArity; ++i) {
84  auto&& type = relTypes[i].string_value();
85  assert(!type.empty() && "malformed types tag");
86  typeAttributes.push_back(type);
87  }
88  }
89 };
90 
91 } // namespace souffle
TCB_SPAN_NAMESPACE_NAME::detail::size
constexpr auto size(const C &c) -> decltype(c.size())
Definition: span.h:198
souffle::SerialisationStream::recordTable
RO< RecordTable > & recordTable
Definition: SerialisationStream.h:72
json11::Json::parse
static Json parse(const std::string &in, std::string &err, JsonParse strategy=JsonParse::STANDARD)
Definition: json11.h:1071
souffle::SerialisationStream::~SerialisationStream
virtual ~SerialisationStream()=default
souffle::SerialisationStream::RO
std::conditional_t< readOnlyTables, const A, A > RO
Definition: SerialisationStream.h:49
json11.h
souffle::SerialisationStream::symbolTable
RO< SymbolTable > & symbolTable
Definition: SerialisationStream.h:71
souffle::SerialisationStream::typeAttributes
std::vector< std::string > typeAttributes
Definition: SerialisationStream.h:74
souffle::SerialisationStream::types
Json types
Definition: SerialisationStream.h:73
i
size_t i
Definition: json11.h:663
json11::Json
Definition: json11.h:87
souffle::SerialisationStream::SerialisationStream
SerialisationStream(RO< SymbolTable > &symTab, RO< RecordTable > &recTab, Json types, std::vector< std::string > relTypes, size_t auxArity=0)
Definition: SerialisationStream.h:51
std
Definition: Brie.h:3053
RamTypes.h
souffle
Definition: AggregateOp.h:25
souffle::SerialisationStream::auxiliaryArity
size_t auxiliaryArity
Definition: SerialisationStream.h:77
souffle::SerialisationStream::arity
size_t arity
Definition: SerialisationStream.h:76
std::type
ElementType type
Definition: span.h:640
souffle::SerialisationStream::setupFromJson
void setupFromJson()
Definition: SerialisationStream.h:80