souffle  2.0.2-371-g6315b36
Public Member Functions | Protected Member Functions
souffle::ReadStream Class Referenceabstract

#include <ReadStream.h>

Inheritance diagram for souffle::ReadStream:
Inheritance graph
Collaboration diagram for souffle::ReadStream:
Collaboration graph

Public Member Functions

template<typename T >
void readAll (T &relation)
 
- Public Member Functions inherited from souffle::SerialisationStream< false >
virtual ~SerialisationStream ()=default
 

Protected Member Functions

void consumeChar (const std::string &str, char c, size_t &pos)
 Read past given character, consuming any preceding whitespace. More...
 
void consumeWhiteSpace (const std::string &str, size_t &pos)
 Advance position in the string until first non-whitespace character. More...
 
RamDomain readADT (const std::string &source, const std::string &adtName, size_t pos=0, size_t *charactersRead=nullptr)
 
std::string readAlphanumeric (const std::string &source, size_t &pos)
 Read the next alphanumeric sequence (corresponding to IDENT). More...
 
virtual Own< RamDomain[]> readNextTuple ()=0
 
RamDomain readRecord (const std::string &source, const std::string &recordTypeName, size_t pos=0, size_t *charactersRead=nullptr)
 Read a record from a string. More...
 
 ReadStream (const std::map< std::string, std::string > &rwOperation, SymbolTable &symTab, RecordTable &recTab)
 
std::string readUntil (const std::string &source, const std::string stopChars, const size_t pos, size_t *charactersRead)
 
- Protected Member Functions inherited from souffle::SerialisationStream< false >
 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)
 

Additional Inherited Members

- Protected Types inherited from souffle::SerialisationStream< false >
using RO = std::conditional_t< readOnlyTables, const A, A >
 
- Protected Attributes inherited from souffle::SerialisationStream< false >
size_t arity
 
size_t auxiliaryArity
 
RO< RecordTable > & recordTable
 
RO< SymbolTable > & symbolTable
 
std::vector< std::string > typeAttributes
 
Json types
 

Detailed Description

Definition at line 40 of file ReadStream.h.

Constructor & Destructor Documentation

◆ ReadStream()

souffle::ReadStream::ReadStream ( const std::map< std::string, std::string > &  rwOperation,
SymbolTable symTab,
RecordTable recTab 
)
inlineprotected

Definition at line 46 of file ReadStream.h.

47  {
48  const RamDomain* ramDomain = next.get();

References relation.

Member Function Documentation

◆ consumeChar()

void souffle::ReadStream::consumeChar ( const std::string &  str,
char  c,
size_t &  pos 
)
inlineprotected

Read past given character, consuming any preceding whitespace.

Definition at line 290 of file ReadStream.h.

298  {
299  while (pos < str.length() && std::isspace(static_cast<unsigned char>(str[pos]))) {
300  ++pos;
301  }

◆ consumeWhiteSpace()

void souffle::ReadStream::consumeWhiteSpace ( const std::string &  str,
size_t &  pos 
)
inlineprotected

Advance position in the string until first non-whitespace character.

Definition at line 306 of file ReadStream.h.

307  {
308 public:
309  virtual Own<ReadStream> getReader(
310  const std::map<std::string, std::string>&, SymbolTable&, RecordTable&) = 0;

References souffle::ReadStreamFactory::getReader().

Here is the call graph for this function:

◆ readADT()

RamDomain souffle::ReadStream::readADT ( const std::string &  source,
const std::string &  adtName,
size_t  pos = 0,
size_t *  charactersRead = nullptr 
)
inlineprotected

Definition at line 143 of file ReadStream.h.

148  {
149  throw std::invalid_argument("Missing ADT information: " + adtName);
150  }
151 
152  // Consume initial character
153  consumeChar(source, '$', pos);
154  std::string constructor = readAlphanumeric(source, pos);
155 
156  json11::Json branchInfo = [&]() -> json11::Json {
157  for (auto branch : branches.array_items()) {
158  ++branchIdx;
159  if (branch["name"].string_value() == constructor) {
160  return branch;
161  }
162  }
163 
164  throw std::invalid_argument("Missing branch information: " + constructor);
165  }();
166 
167  assert(branchInfo["types"].is_array());
168  auto branchTypes = branchInfo["types"].array_items();
169 
170  // Handle a branch without arguments.
171  if (branchTypes.empty()) {
172  if (charactersRead != nullptr) {
173  *charactersRead = pos - initial_position;
174  }
175 
176  if (adtInfo["enum"].bool_value()) {
177  return branchIdx;
178  }
179 
180  RamDomain emptyArgs = recordTable.pack(toVector<RamDomain>().data(), 0);
181  return recordTable.pack(toVector<RamDomain>(branchIdx, emptyArgs).data(), 2);
182  }
183 
184  consumeChar(source, '(', pos);
185 
186  std::vector<RamDomain> branchArgs(branchTypes.size());
187 
188  for (size_t i = 0; i < branchTypes.size(); ++i) {
189  auto argType = branchTypes[i].string_value();
190  assert(!argType.empty());
191 
192  size_t consumed = 0;
193 
194  if (i > 0) {
195  consumeChar(source, ',', pos);
196  }
197  consumeWhiteSpace(source, pos);
198 
199  switch (argType[0]) {
200  case 's': {
201  branchArgs[i] = symbolTable.unsafeLookup(readUntil(source, ",)", pos, &consumed));
202  break;
203  }
204  case 'i': {
205  branchArgs[i] = RamSignedFromString(source.substr(pos), &consumed);
206  break;
207  }
208  case 'u': {
209  branchArgs[i] = ramBitCast(RamUnsignedFromString(source.substr(pos), &consumed));
210  break;
211  }
212  case 'f': {
213  branchArgs[i] = ramBitCast(RamFloatFromString(source.substr(pos), &consumed));
214  break;
215  }
216  case 'r': {
217  branchArgs[i] = readRecord(source, argType, pos, &consumed);
218  break;
219  }
220  case '+': {
221  branchArgs[i] = readADT(source, argType, pos, &consumed);
222  break;
223  }
224  default: fatal("Invalid type attribute");
225  }
226  pos += consumed;
227  }
228 
229  consumeChar(source, ')', pos);
230 
231  if (charactersRead != nullptr) {
232  *charactersRead = pos - initial_position;
233  }
234 
235  // Store branch either as [branch_id, [arguments]] or [branch_id, argument].
236  RamDomain branchValue = [&]() -> RamDomain {
237  if (branchArgs.size() != 1) {
238  return recordTable.pack(branchArgs.data(), branchArgs.size());
239  } else {
240  return branchArgs[0];
241  }
242  }();
243 
244  return recordTable.pack(toVector<RamDomain>(branchIdx, branchValue).data(), 2);
245  }
246 
247  /**
248  * Read the next alphanumeric sequence (corresponding to IDENT).
249  * Consume preceding whitespace.
250  * TODO (darth_tytus): use std::string_view?
251  */
252  std::string readAlphanumeric(const std::string& source, size_t& pos) {
253  consumeWhiteSpace(source, pos);

◆ readAll()

template<typename T >
void souffle::ReadStream::readAll ( T &  relation)
inline

Definition at line 52 of file ReadStream.h.

53  :
54  /**
55  * Read a record from a string.
56  *
57  * @param source - string containing a record
58  * @param recordTypeName - record type.
59  * @parem pos - start parsing from this position.

References souffle::SerialisationStream< false >::types.

◆ readAlphanumeric()

std::string souffle::ReadStream::readAlphanumeric ( const std::string &  source,
size_t &  pos 
)
inlineprotected

Read the next alphanumeric sequence (corresponding to IDENT).

Consume preceding whitespace. TODO (darth_tytus): use std::string_view?

Definition at line 260 of file ReadStream.h.

267  {
268  size_t endOfSymbol = source.find_first_of(stopChars, pos);
269 
270  if (endOfSymbol == std::string::npos) {
271  throw std::invalid_argument("Unexpected end of input");
272  }

◆ readNextTuple()

virtual Own<RamDomain[]> souffle::ReadStream::readNextTuple ( )
protectedpure virtual

◆ readRecord()

RamDomain souffle::ReadStream::readRecord ( const std::string &  source,
const std::string &  recordTypeName,
size_t  pos = 0,
size_t *  charactersRead = nullptr 
)
inlineprotected

Read a record from a string.

Parameters
source- string containing a record
recordTypeName- record type. @parem pos - start parsing from this position.
consumed- if not nullptr: number of characters read.

Definition at line 71 of file ReadStream.h.

75  {
76  if (charactersRead != nullptr) {
77  *charactersRead = 3;
78  }
79  return 0;
80  }
81 
82  auto&& recordTypes = recordInfo["types"];
83  const size_t recordArity = recordInfo["arity"].long_value();
84 
85  std::vector<RamDomain> recordValues(recordArity);
86 
87  consumeChar(source, '[', pos);
88 
89  for (size_t i = 0; i < recordArity; ++i) {
90  const std::string& recordType = recordTypes[i].string_value();
91  size_t consumed = 0;
92 
93  if (i > 0) {
94  consumeChar(source, ',', pos);
95  }
96  consumeWhiteSpace(source, pos);
97  switch (recordType[0]) {
98  case 's': {
99  recordValues[i] = symbolTable.unsafeLookup(readUntil(source, ",]", pos, &consumed));
100  break;
101  }
102  case 'i': {
103  recordValues[i] = RamSignedFromString(source.substr(pos), &consumed);
104  break;
105  }
106  case 'u': {
107  recordValues[i] = ramBitCast(RamUnsignedFromString(source.substr(pos), &consumed));
108  break;
109  }
110  case 'f': {
111  recordValues[i] = ramBitCast(RamFloatFromString(source.substr(pos), &consumed));
112  break;
113  }
114  case 'r': {
115  recordValues[i] = readRecord(source, recordType, pos, &consumed);
116  break;
117  }
118  case '+': {
119  recordValues[i] = readADT(source, recordType, pos, &consumed);
120  break;
121  }
122  default: fatal("Invalid type attribute");
123  }
124  pos += consumed;
125  }
126  consumeChar(source, ']', pos);
127 
128  if (charactersRead != nullptr) {
129  *charactersRead = pos - initial_position;
130  }
131 
132  return recordTable.pack(recordValues.data(), recordValues.size());
133  }
134 
135  RamDomain readADT(const std::string& source, const std::string& adtName, size_t pos = 0,
136  size_t* charactersRead = nullptr) {
137  const size_t initial_position = pos;
138 
139  // Branch will are encoded as one of the:
140  // [branchIdx, [branchValues...]]
141  // [branchIdx, branchValue]

◆ readUntil()

std::string souffle::ReadStream::readUntil ( const std::string &  source,
const std::string  stopChars,
const size_t  pos,
size_t *  charactersRead 
)
inlineprotected

Definition at line 274 of file ReadStream.h.

282  {
283  consumeWhiteSpace(str, pos);
284  if (pos >= str.length()) {
285  throw std::invalid_argument("Unexpected end of input");

The documentation for this class was generated from the following file:
souffle::RamSignedFromString
RamSigned RamSignedFromString(const std::string &str, std::size_t *position=nullptr, const int base=10)
Converts a string to a RamSigned.
Definition: StringUtil.h:51
souffle::SerialisationStream< false >::recordTable
RO< RecordTable > & recordTable
Definition: SerialisationStream.h:72
souffle::RamDomain
int32_t RamDomain
Definition: RamTypes.h:56
souffle::SerialisationStream< false >::symbolTable
RO< SymbolTable > & symbolTable
Definition: SerialisationStream.h:71
str
const std::string & str
Definition: json11.h:662
json11::Json::array_items
const array & array_items() const
Definition: json11.h:554
i
size_t i
Definition: json11.h:663
souffle::ReadStream::consumeWhiteSpace
void consumeWhiteSpace(const std::string &str, size_t &pos)
Advance position in the string until first non-whitespace character.
Definition: ReadStream.h:306
souffle::ReadStream::readUntil
std::string readUntil(const std::string &source, const std::string stopChars, const size_t pos, size_t *charactersRead)
Definition: ReadStream.h:274
json11::Json
Definition: json11.h:87
souffle::ReadStream::readAlphanumeric
std::string readAlphanumeric(const std::string &source, size_t &pos)
Read the next alphanumeric sequence (corresponding to IDENT).
Definition: ReadStream.h:260
souffle::fatal
void fatal(const char *format, const Args &... args)
Definition: MiscUtil.h:198
souffle::RamUnsignedFromString
RamUnsigned RamUnsignedFromString(const std::string &str, std::size_t *position=nullptr, const int base=10)
Converts a string to a RamUnsigned.
Definition: StringUtil.h:110
TCB_SPAN_NAMESPACE_NAME::detail::data
constexpr auto data(C &c) -> decltype(c.data())
Definition: span.h:210
souffle::ramBitCast
To ramBitCast(From source)
In C++20 there will be a new way to cast between types by reinterpreting bits (std::bit_cast),...
Definition: RamTypes.h:87
souffle::RamFloatFromString
RamFloat RamFloatFromString(const std::string &str, std::size_t *position=nullptr)
Converts a string to a RamFloat.
Definition: StringUtil.h:93
souffle::ReadStream::readADT
RamDomain readADT(const std::string &source, const std::string &adtName, size_t pos=0, size_t *charactersRead=nullptr)
Definition: ReadStream.h:143
souffle::ReadStream::readRecord
RamDomain readRecord(const std::string &source, const std::string &recordTypeName, size_t pos=0, size_t *charactersRead=nullptr)
Read a record from a string.
Definition: ReadStream.h:71
souffle::ReadStream::consumeChar
void consumeChar(const std::string &str, char c, size_t &pos)
Read past given character, consuming any preceding whitespace.
Definition: ReadStream.h:290