souffle  2.0.2-371-g6315b36
IOSystem.h
Go to the documentation of this file.
1 /*
2  * Souffle - A Datalog Compiler
3  * Copyright (c) 2013, 2014, Oracle and/or its affiliates. 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/SymbolTable.h"
19 #include "souffle/io/ReadStream.h"
22 #include "souffle/io/WriteStream.h"
25 
26 #ifdef USE_SQLITE
29 #endif
30 
31 #include <map>
32 #include <memory>
33 #include <stdexcept>
34 #include <string>
35 
36 namespace souffle {
37 class RecordTable;
38 
39 class IOSystem {
40 public:
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 
78 private:
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 */
souffle::IOSystem::inputFactories
std::map< std::string, std::shared_ptr< ReadStreamFactory > > inputFactories
Definition: IOSystem.h:99
WriteStreamCSV.h
SymbolTable.h
WriteStreamSQLite.h
souffle::RecordTable
Definition: RecordTable.h:114
souffle::Own
std::unique_ptr< A > Own
Definition: ContainerUtil.h:42
souffle::IOSystem::registerWriteStreamFactory
void registerWriteStreamFactory(const std::shared_ptr< WriteStreamFactory > &factory)
Definition: IOSystem.h:50
souffle::IOSystem::~IOSystem
~IOSystem()=default
souffle::IOSystem::getInstance
static IOSystem & getInstance()
Definition: IOSystem.h:45
ReadStreamCSV.h
souffle::IOSystem::registerReadStreamFactory
void registerReadStreamFactory(const std::shared_ptr< ReadStreamFactory > &factory)
Definition: IOSystem.h:54
ReadStreamJSON.h
ReadStreamSQLite.h
ReadStream.h
souffle::SymbolTable
Definition: SymbolTable.h:48
WriteStreamJSON.h
souffle::IOSystem::outputFactories
std::map< std::string, std::shared_ptr< WriteStreamFactory > > outputFactories
Definition: IOSystem.h:97
souffle::IOSystem::getWriter
Own< WriteStream > getWriter(const std::map< std::string, std::string > &rwOperation, const SymbolTable &symbolTable, const RecordTable &recordTable) const
Return a new WriteStream.
Definition: IOSystem.h:61
WriteStream.h
RamTypes.h
souffle
Definition: AggregateOp.h:25
souffle::IOSystem::IOSystem
IOSystem()
Definition: IOSystem.h:83
souffle::IOSystem::getReader
Own< ReadStream > getReader(const std::map< std::string, std::string > &rwOperation, SymbolTable &symbolTable, RecordTable &recordTable) const
Return a new ReadStream.
Definition: IOSystem.h:72