A platform for high-performance distributed tool and library development written in C++. It can be deployed in two different cluster modes: standalone or distributed. API for v0.5.0, released on June 13, 2018.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
QueryBase.h
Go to the documentation of this file.
1 /*****************************************************************************
2  * *
3  * Copyright 2018 Rice University *
4  * *
5  * Licensed under the Apache License, Version 2.0 (the "License"); *
6  * you may not use this file except in compliance with the License. *
7  * You may obtain a copy of the License at *
8  * *
9  * http://www.apache.org/licenses/LICENSE-2.0 *
10  * *
11  * Unless required by applicable law or agreed to in writing, software *
12  * distributed under the License is distributed on an "AS IS" BASIS, *
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
14  * See the License for the specific language governing permissions and *
15  * limitations under the License. *
16  * *
17  *****************************************************************************/
18 
19 #ifndef QUERY_BASE_H
20 #define QUERY_BASE_H
21 
22 #include <functional>
23 
24 #include "Object.h"
25 #include "PDBString.h"
26 #include "PDBVector.h"
27 #include "PDBDebug.h"
28 
29 using std::function;
30 
31 namespace pdb {
32 
33 // all queries are descended from this
34 class QueryBase : public Object {
35 
36 public:
38  whichSet = "";
39  whichDB = "";
40  }
41 
42  size_t hash() const {
43  return (size_t)this;
44  }
45 
46  void print() {
47  PDB_COUT << whichDB << "." << whichSet << ": " << getQueryType() << "(" << getOutputType()
48  << ")\n";
49  for (int i = 0; i < getNumInputs(); i++) {
50  if (getIthInput(i) == nullptr)
51  PDB_COUT << "<nullptr>";
52  else
53  getIthInput(i)->print();
54  }
55  }
56 
57  // gets the output type of this query as a string
58  virtual std::string getOutputType() = 0;
59 
60  // gets the number of intputs to this query type
61  virtual int getNumInputs() = 0;
62 
63  // gets the name of the i^th input type...
64  virtual std::string getIthInputType(int i) = 0;
65 
70  // virtual void execute(QueryAlgo& algo) = 0;
71 
72  virtual void match(function<void(QueryBase&)> forSelection,
73  function<void(QueryBase&)> forSet,
74  function<void(QueryBase&)> forQueryOutput) = 0;
75 
76  // gets the name of this particular query type ("selection", "join", etc.)
77  virtual std::string getQueryType() = 0;
78 
79  // getters/setters for the output database name and set name for this query
80  void setDBName(std::string toMe) {
81  whichDB = toMe;
82  }
83 
84  // used to mark this query as bad
85  void setError() {
86  isError = true;
87  }
88 
89  void setSetName(std::string toMe) {
90  whichSet = toMe;
91  }
92 
93  bool wasError() {
94  return isError;
95  }
96 
97  std::string getDBName() {
98  return whichDB;
99  }
100 
101  std::string getSetName() {
102  return whichSet;
103  }
104 
105  // gets a handle to the i^th input to this query, which is also a query
107  return (*inputs)[i];
108  }
109  bool hasInput() {
110  return !inputs.isNullPtr();
111  }
112 
113  // set the first slot, by default
115  return setInput(0, toMe);
116  }
117 
118  // sets the i^th input to be the output of a specific query... returns
119  // true if this is OK, false if it is not
120  bool setInput(int whichSlot, Handle<QueryBase> toMe) {
121 
122  // set the array of inputs if it is a nullptr
123  if (inputs == nullptr) {
124  inputs = makeObject<Vector<Handle<QueryBase>>>(getNumInputs());
125  for (int i = 0; i < getNumInputs(); i++) {
126  inputs->push_back(nullptr);
127  }
128  }
129 
130  // if we are adding this query to a valid slot
131  if (whichSlot < getNumInputs()) {
132 
133  // make sure the output type of the guy we are accepting meets the input type
134  if (getIthInputType(whichSlot) != toMe->getOutputType()) {
135  std::cout << "Cannot set output of query node with output of type "
136  << toMe->getOutputType() << " to be the input";
137  std::cout << " of a query with input type " << getIthInputType(whichSlot) << ".\n";
138  isError = true;
139  return false;
140  }
141 
142  (*inputs)[whichSlot] = toMe;
143 
144  // carry throgh an error
145  if (toMe->isError)
146  isError = true;
147 
148  // make sure that the database names match up
149  if (getDBName() != "" && getDBName() != toMe->getDBName()) {
150  std::cout
151  << "This is bad; you seem to be combining inputs from different databases.\n";
152  PDB_COUT << "DBs used are " << getDBName() << " and " << toMe->getDBName() << ".\n";
153  isError = true;
154  return false;
155  } else {
156 
157  // if we have not yet gotten the DB name, then set it here
158  setDBName(toMe->getDBName());
159  }
160 
161  return true;
162  }
163 
164  return false;
165  }
166 
167  virtual bool operator==(const QueryBase& other) {
168  return this == &other;
169  }
170 
171 private:
172  // this is the name of the database/set combo where the answer of this query is stored
175 
176  // if there was an error
177  bool isError = false;
178 
179  // all of the queries that are input into this one
181 };
182 
184 }
185 
186 #endif
virtual void match(function< void(QueryBase &)> forSelection, function< void(QueryBase &)> forSet, function< void(QueryBase &)> forQueryOutput)=0
String whichSet
Definition: QueryBase.h:174
std::string getSetName()
Definition: QueryBase.h:101
bool wasError()
Definition: QueryBase.h:93
void setSetName(std::string toMe)
Definition: QueryBase.h:89
virtual bool operator==(const QueryBase &other)
Definition: QueryBase.h:167
bool hasInput()
Definition: QueryBase.h:109
virtual int getNumInputs()=0
Handle< QueryBase > QueryBaseHdl
Definition: QueryBase.h:183
String whichDB
Definition: QueryBase.h:173
bool setInput(int whichSlot, Handle< QueryBase > toMe)
Definition: QueryBase.h:120
bool setInput(Handle< QueryBase > toMe)
Definition: QueryBase.h:114
virtual std::string getOutputType()=0
#define PDB_COUT
Definition: PDBDebug.h:31
Handle< Vector< Handle< QueryBase > > > inputs
Definition: QueryBase.h:180
void setDBName(std::string toMe)
Definition: QueryBase.h:80
void setError()
Definition: QueryBase.h:85
Handle< QueryBase > & getIthInput(int i) const
Definition: QueryBase.h:106
void print()
Definition: QueryBase.h:46
virtual std::string getQueryType()=0
size_t hash() const
Definition: QueryBase.h:42
std::string getDBName()
Definition: QueryBase.h:97
virtual std::string getIthInputType(int i)=0