souffle  2.0.2-371-g6315b36
Protected Member Functions | Protected Attributes
souffle::WriteStreamJSON Class Reference

#include <WriteStreamJSON.h>

Inheritance diagram for souffle::WriteStreamJSON:
Inheritance graph
Collaboration diagram for souffle::WriteStreamJSON:
Collaboration graph

Protected Member Functions

void writeNextTupleJSON (std::ostream &destination, const RamDomain *tuple)
 
void writeNextTupleList (std::ostream &destination, const std::string &name, const RamDomain value)
 
void writeNextTupleObject (std::ostream &destination, const std::string &name, const RamDomain value)
 
 WriteStreamJSON (const std::map< std::string, std::string > &rwOperation, const SymbolTable &symbolTable, const RecordTable &recordTable)
 
- Protected Member Functions inherited from souffle::WriteStream
void outputADT (std::ostream &destination, const RamDomain value, const std::string &name)
 
void outputRecord (std::ostream &destination, const RamDomain value, const std::string &name)
 
template<typename Tuple >
void writeNext (const Tuple tuple)
 
virtual void writeNextTuple (const RamDomain *tuple)=0
 
virtual void writeNullary ()=0
 
virtual void writeSize (std::size_t)
 
- Protected Member Functions inherited from souffle::SerialisationStream< true >
 SerialisationStream (RO< SymbolTable > &symTab, RO< RecordTable > &recTab, const std::map< std::string, std::string > &rwOperation)
 
 SerialisationStream (RO< SymbolTable > &symTab, RO< RecordTable > &recTab, Json types)
 
 SerialisationStream (RO< SymbolTable > &symTab, RO< RecordTable > &recTab, Json types, std::vector< std::string > relTypes, size_t auxArity=0)
 

Protected Attributes

Json params
 
const bool useObjects
 
- Protected Attributes inherited from souffle::WriteStream
const bool summary
 
- Protected Attributes inherited from souffle::SerialisationStream< true >
size_t arity
 
size_t auxiliaryArity
 
RO< RecordTable > & recordTable
 
RO< SymbolTable > & symbolTable
 
std::vector< std::string > typeAttributes
 
Json types
 

Additional Inherited Members

- Public Member Functions inherited from souffle::WriteStream
template<typename T >
void writeAll (const T &relation)
 
template<>
void writeNext (const RamDomain *tuple)
 
template<typename T >
void writeSize (const T &relation)
 
 WriteStream (const std::map< std::string, std::string > &rwOperation, const SymbolTable &symbolTable, const RecordTable &recordTable)
 
- Public Member Functions inherited from souffle::SerialisationStream< true >
virtual ~SerialisationStream ()=default
 
- Protected Types inherited from souffle::SerialisationStream< true >
using RO = std::conditional_t< readOnlyTables, const A, A >
 

Detailed Description

Definition at line 37 of file WriteStreamJSON.h.

Constructor & Destructor Documentation

◆ WriteStreamJSON()

souffle::WriteStreamJSON::WriteStreamJSON ( const std::map< std::string, std::string > &  rwOperation,
const SymbolTable symbolTable,
const RecordTable recordTable 
)
inlineprotected

Definition at line 43 of file WriteStreamJSON.h.

43  : %s", err);
44  }
45  }
46  };
47 
48  const bool useObjects;
49  Json params;
50 
51  void writeNextTupleJSON(std::ostream& destination, const RamDomain* tuple) {
52  std::vector<Json> result;
53 
54  if (useObjects)

Member Function Documentation

◆ writeNextTupleJSON()

void souffle::WriteStreamJSON::writeNextTupleJSON ( std::ostream &  destination,
const RamDomain tuple 
)
inlineprotected

Definition at line 59 of file WriteStreamJSON.h.

59  {
60  if (col > 0) {
61  destination << ", ";
62  }
63 
64  if (useObjects) {
65  destination << params["relation"]["params"][col].dump() << ": ";
66  writeNextTupleObject(destination, typeAttributes.at(col), tuple[col]);
67  } else {
68  writeNextTupleList(destination, typeAttributes.at(col), tuple[col]);
69  }
70  }
71 
72  if (useObjects)
73  destination << "}";
74  else
75  destination << "]";
76  }
77 
78  void writeNextTupleList(std::ostream& destination, const std::string& name, const RamDomain value) {
79  using ValueTuple = std::pair<const std::string, const RamDomain>;
80  std::stack<std::variant<ValueTuple, std::string>> worklist;
81  worklist.push(std::make_pair(name, value));
82 
83  // the Json11 output is not tail recursive, therefore highly inefficient for recursive record
84  // in addition the JSON object is immutable, so has memory overhead

References json11::Json::dump(), params, souffle::SerialisationStream< true >::typeAttributes, useObjects, writeNextTupleList(), and writeNextTupleObject().

Here is the call graph for this function:

◆ writeNextTupleList()

void souffle::WriteStreamJSON::writeNextTupleList ( std::ostream &  destination,
const std::string &  name,
const RamDomain  value 
)
inlineprotected

Definition at line 86 of file WriteStreamJSON.h.

89  {
90  destination << std::get<std::string>(curr);
91  continue;
92  }
93 
94  const std::string& currType = std::get<ValueTuple>(curr).first;
95  const RamDomain currValue = std::get<ValueTuple>(curr).second;
96  assert(currType.length() > 2 && "Invalid type length");
97  switch (currType[0]) {
98  // since some strings may need to be escaped, we use dump here
99  case 's': destination << Json(symbolTable.unsafeResolve(currValue)).dump(); break;
100  case 'i': destination << currValue; break;
101  case 'u': destination << (int)ramBitCast<RamUnsigned>(currValue); break;
102  case 'f': destination << ramBitCast<RamFloat>(currValue); break;
103  case 'r': {
104  auto&& recordInfo = types["records"][currType];
105  assert(!recordInfo.is_null() && "Missing record type information");
106  if (currValue == 0) {
107  destination << "null";
108  break;
109  }
110 
111  auto&& recordTypes = recordInfo["types"];
112  const size_t recordArity = recordInfo["arity"].long_value();
113  const RamDomain* tuplePtr = recordTable.unpack(currValue, recordArity);
114  worklist.push("]");
115  for (auto i = (long long)(recordArity - 1); i >= 0; --i) {
116  if (i != (long long)(recordArity - 1)) {
117  worklist.push(", ");
118  }
119  const std::string& recordType = recordTypes[i].string_value();
120  const RamDomain recordValue = tuplePtr[i];
121  worklist.push(std::make_pair(recordType, recordValue));
122  }
123 
124  worklist.push("[");
125  break;
126  }
127  default: fatal("unsupported type attribute: `%c`", currType[0]);
128  }
129  }
130  }
131 
132  void writeNextTupleObject(std::ostream& destination, const std::string& name, const RamDomain value) {
133  using ValueTuple = std::pair<const std::string, const RamDomain>;
134  std::stack<std::variant<ValueTuple, std::string>> worklist;
135  worklist.push(std::make_pair(name, value));
136 
137  // the Json11 output is not tail recursive, therefore highly inefficient for recursive record
138  // in addition the JSON object is immutable, so has memory overhead

Referenced by writeNextTupleJSON().

◆ writeNextTupleObject()

void souffle::WriteStreamJSON::writeNextTupleObject ( std::ostream &  destination,
const std::string &  name,
const RamDomain  value 
)
inlineprotected

Definition at line 140 of file WriteStreamJSON.h.

143  {
144  destination << std::get<std::string>(curr);
145  continue;
146  }
147 
148  const std::string& currType = std::get<ValueTuple>(curr).first;
149  const RamDomain currValue = std::get<ValueTuple>(curr).second;
150  const std::string& typeName = currType.substr(2);
151  assert(currType.length() > 2 && "Invalid type length");
152  switch (currType[0]) {
153  // since some strings may need to be escaped, we use dump here
154  case 's': destination << Json(symbolTable.unsafeResolve(currValue)).dump(); break;
155  case 'i': destination << currValue; break;
156  case 'u': destination << (int)ramBitCast<RamUnsigned>(currValue); break;
157  case 'f': destination << ramBitCast<RamFloat>(currValue); break;
158  case 'r': {
159  auto&& recordInfo = types["records"][currType];
160  assert(!recordInfo.is_null() && "Missing record type information");
161  if (currValue == 0) {
162  destination << "null";
163  break;
164  }
165 
166  auto&& recordTypes = recordInfo["types"];
167  const size_t recordArity = recordInfo["arity"].long_value();
168  const RamDomain* tuplePtr = recordTable.unpack(currValue, recordArity);
169  worklist.push("}");
170  for (auto i = (long long)(recordArity - 1); i >= 0; --i) {
171  if (i != (long long)(recordArity - 1)) {
172  worklist.push(", ");
173  }
174  const std::string& recordType = recordTypes[i].string_value();
175  const RamDomain recordValue = tuplePtr[i];
176  worklist.push(std::make_pair(recordType, recordValue));
177  worklist.push(": ");
178 
179  auto&& recordParam = params["records"][typeName]["params"][i];
180  assert(recordParam.is_string());
181  worklist.push(recordParam.dump());
182  }
183 
184  worklist.push("{");
185  break;
186  }
187  default: fatal("unsupported type attribute: `%c`", currType[0]);
188  }
189  }
190  }
191 };
192 
193 class WriteFileJSON : public WriteStreamJSON {
194 public:
195  WriteFileJSON(const std::map<std::string, std::string>& rwOperation, const SymbolTable& symbolTable,
196  const RecordTable& recordTable)
197  : WriteStreamJSON(rwOperation, symbolTable, recordTable), isFirst(true),
198  file(getFileName(rwOperation), std::ios::out | std::ios::binary) {

Referenced by writeNextTupleJSON().

Field Documentation

◆ params

Json souffle::WriteStreamJSON::params
protected

Definition at line 57 of file WriteStreamJSON.h.

Referenced by writeNextTupleJSON().

◆ useObjects

const bool souffle::WriteStreamJSON::useObjects
protected

Definition at line 54 of file WriteStreamJSON.h.

Referenced by writeNextTupleJSON().


The documentation for this class was generated from the following file:
souffle::SerialisationStream< true >::recordTable
RO< RecordTable > & recordTable
Definition: SerialisationStream.h:72
json11::Json::dump
void dump(std::string &out) const
Definition: json11.h:370
souffle::RamDomain
int32_t RamDomain
Definition: RamTypes.h:56
souffle::WriteStreamJSON::writeNextTupleObject
void writeNextTupleObject(std::ostream &destination, const std::string &name, const RamDomain value)
Definition: WriteStreamJSON.h:140
souffle::SerialisationStream< true >::symbolTable
RO< SymbolTable > & symbolTable
Definition: SerialisationStream.h:71
souffle::WriteStreamJSON::writeNextTupleList
void writeNextTupleList(std::ostream &destination, const std::string &name, const RamDomain value)
Definition: WriteStreamJSON.h:86
souffle::SerialisationStream< true >::typeAttributes
std::vector< std::string > typeAttributes
Definition: SerialisationStream.h:74
souffle::WriteStreamJSON::useObjects
const bool useObjects
Definition: WriteStreamJSON.h:54
souffle::SerialisationStream< true >::types
Json types
Definition: SerialisationStream.h:73
i
size_t i
Definition: json11.h:663
souffle::WriteStreamJSON::params
Json params
Definition: WriteStreamJSON.h:57
std
Definition: Brie.h:3053
souffle::WriteStreamJSON::WriteStreamJSON
WriteStreamJSON(const std::map< std::string, std::string > &rwOperation, const SymbolTable &symbolTable, const RecordTable &recordTable)
Definition: WriteStreamJSON.h:43
souffle::fatal
void fatal(const char *format, const Args &... args)
Definition: MiscUtil.h:198