OILS / vendor / souffle / io / IOSystem.h View on Github | oilshell.org

98 lines, 60 significant
1/*
2 * Souffle - A Datalog Compiler
3 * Copyright (c) 2021, 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 IOSystem.h
12 *
13 ***********************************************************************/
14
15#pragma once
16
17#include "souffle/RamTypes.h"
18#include "souffle/RecordTable.h"
19#include "souffle/SymbolTable.h"
20#include "souffle/io/ReadStream.h"
21#include "souffle/io/ReadStreamCSV.h"
22#include "souffle/io/ReadStreamJSON.h"
23#include "souffle/io/WriteStream.h"
24#include "souffle/io/WriteStreamCSV.h"
25#include "souffle/io/WriteStreamJSON.h"
26
27#ifdef USE_SQLITE
28#include "souffle/io/ReadStreamSQLite.h"
29#include "souffle/io/WriteStreamSQLite.h"
30#endif
31
32#include <map>
33#include <memory>
34#include <stdexcept>
35#include <string>
36
37namespace souffle {
38
39class IOSystem {
40public:
41 static IOSystem& getInstance() {
42 static IOSystem singleton;
43 return singleton;
44 }
45
46 void registerWriteStreamFactory(const std::shared_ptr<WriteStreamFactory>& factory) {
47 outputFactories[factory->getName()] = factory;
48 }
49
50 void registerReadStreamFactory(const std::shared_ptr<ReadStreamFactory>& factory) {
51 inputFactories[factory->getName()] = factory;
52 }
53
54 /**
55 * Return a new WriteStream
56 */
57 Own<WriteStream> getWriter(const std::map<std::string, std::string>& rwOperation,
58 const SymbolTable& symbolTable, const RecordTable& recordTable) const {
59 std::string ioType = rwOperation.at("IO");
60 if (outputFactories.count(ioType) == 0) {
61 throw std::invalid_argument("Requested output type <" + ioType + "> is not supported.");
62 }
63 return outputFactories.at(ioType)->getWriter(rwOperation, symbolTable, recordTable);
64 }
65 /**
66 * Return a new ReadStream
67 */
68 Own<ReadStream> getReader(const std::map<std::string, std::string>& rwOperation, SymbolTable& symbolTable,
69 RecordTable& recordTable) const {
70 std::string ioType = rwOperation.at("IO");
71 if (inputFactories.count(ioType) == 0) {
72 throw std::invalid_argument("Requested input type <" + ioType + "> is not supported.");
73 }
74 return inputFactories.at(ioType)->getReader(rwOperation, symbolTable, recordTable);
75 }
76 ~IOSystem() = default;
77
78private:
79 IOSystem() {
80 registerReadStreamFactory(std::make_shared<ReadFileCSVFactory>());
81 registerReadStreamFactory(std::make_shared<ReadCinCSVFactory>());
82 registerReadStreamFactory(std::make_shared<ReadFileJSONFactory>());
83 registerReadStreamFactory(std::make_shared<ReadCinJSONFactory>());
84 registerWriteStreamFactory(std::make_shared<WriteFileCSVFactory>());
85 registerWriteStreamFactory(std::make_shared<WriteCoutCSVFactory>());
86 registerWriteStreamFactory(std::make_shared<WriteCoutPrintSizeFactory>());
87 registerWriteStreamFactory(std::make_shared<WriteFileJSONFactory>());
88 registerWriteStreamFactory(std::make_shared<WriteCoutJSONFactory>());
89#ifdef USE_SQLITE
90 registerReadStreamFactory(std::make_shared<ReadSQLiteFactory>());
91 registerWriteStreamFactory(std::make_shared<WriteSQLiteFactory>());
92#endif
93 };
94 std::map<std::string, std::shared_ptr<WriteStreamFactory>> outputFactories;
95 std::map<std::string, std::shared_ptr<ReadStreamFactory>> inputFactories;
96};
97
98} /* namespace souffle */