/* * Souffle - A Datalog Compiler * Copyright (c) 2020, The Souffle Developers. All rights reserved. * Licensed under the Universal Permissive License v 1.0 as shown at: * - https://opensource.org/licenses/UPL * - /licenses/SOUFFLE-UPL.txt */ /************************************************************************ * * @file SerialisationStream.h * * Defines a common base class for relation serialisation streams. * ***********************************************************************/ #pragma once #include "RamTypes.h" #include "json11.h" #include #include #include #include #include #include namespace souffle { class RecordTable; class SymbolTable; using json11::Json; template class SerialisationStream { public: virtual ~SerialisationStream() = default; protected: template using RO = std::conditional_t; SerialisationStream(RO& symTab, RO& recTab, Json types, std::vector relTypes, size_t auxArity = 0) : symbolTable(symTab), recordTable(recTab), types(std::move(types)), typeAttributes(std::move(relTypes)), arity(typeAttributes.size() - auxArity), auxiliaryArity(auxArity) {} SerialisationStream(RO& symTab, RO& recTab, Json types) : symbolTable(symTab), recordTable(recTab), types(std::move(types)) { setupFromJson(); } SerialisationStream(RO& symTab, RO& recTab, const std::map& rwOperation) : symbolTable(symTab), recordTable(recTab) { std::string parseErrors; types = Json::parse(rwOperation.at("types"), parseErrors); assert(parseErrors.size() == 0 && "Internal JSON parsing failed."); setupFromJson(); } RO& symbolTable; RO& recordTable; Json types; std::vector typeAttributes; size_t arity = 0; size_t auxiliaryArity = 0; private: void setupFromJson() { auto&& relInfo = types["relation"]; arity = static_cast(relInfo["arity"].long_value()); auxiliaryArity = static_cast(relInfo["auxArity"].long_value()); assert(relInfo["types"].is_array()); auto&& relTypes = relInfo["types"].array_items(); assert(relTypes.size() == (arity + auxiliaryArity)); for (size_t i = 0; i < arity + auxiliaryArity; ++i) { auto&& type = relTypes[i].string_value(); assert(!type.empty() && "malformed types tag"); typeAttributes.push_back(type); } } }; } // namespace souffle