1 /* Copyright (C) 2014 InfiniDB, Inc.
2 
3    This program is free software; you can redistribute it and/or
4    modify it under the terms of the GNU General Public License
5    as published by the Free Software Foundation; version 2 of
6    the License.
7 
8    This program is distributed in the hope that it will be useful,
9    but WITHOUT ANY WARRANTY; without even the implied warranty of
10    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11    GNU General Public License for more details.
12 
13    You should have received a copy of the GNU General Public License
14    along with this program; if not, write to the Free Software
15    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
16    MA 02110-1301, USA. */
17 
18 /******************************************************************************
19  * $Id: objectreader.h 9633 2013-06-19 13:36:01Z rdempsey $
20  *
21  *****************************************************************************/
22 
23 /** @file
24  * class ObjectReader interface
25  */
26 
27 #ifndef EXECPLAN_OBJECTREADER_H
28 #define EXECPLAN_OBJECTREADER_H
29 
30 #include <exception>
31 #include <string>
32 #include <stdint.h>
33 
34 namespace messageqcpp
35 {
36 class ByteStream;
37 }
38 
39 namespace execplan
40 {
41 
42 class TreeNode;
43 class ParseTree;
44 class CalpontExecutionPlan;
45 
46 /** @brief A class for creating execplan classes from ByteStreams.
47  *
48  * A class currently used for recreating execplan polymorphic classes
49  * and dynamic objects from ByteStreams.
50  */
51 
52 class ObjectReader
53 {
54 
55 public:
56 
57     class UnserializeException : public std::exception
58     {
59     public:
60         UnserializeException(std::string) throw();
61         virtual ~UnserializeException() throw();
62         virtual const char* what() const throw();
63     private:
64         std::string fWhat;
65     };
66 
67     /** @brief Enumerates classes supporting serialization
68      *
69      * This defines one constant for each class that supports
70      * serialization.
71      */
72     enum CLASSID
73     {
74         ZERO,      // an appropriate initializer
75         NULL_CLASS,		// to denote that some member is NULL
76 
77         /**** TreeNodes */
78         TREENODE,
79         TREENODEIMPL,
80         RETURNEDCOLUMN,
81         AGGREGATECOLUMN,
82         GROUPCONCATCOLUMN,
83         ARITHMETICCOLUMN,
84         CONSTANTCOLUMN,
85         FUNCTIONCOLUMN,
86         ROWCOLUMN,
87         WINDOWFUNCTIONCOLUMN,
88         PSEUDOCOLUMN,
89 
90         SIMPLECOLUMN,
91         SIMPLECOLUMN_INT1,
92         SIMPLECOLUMN_INT2,
93         SIMPLECOLUMN_INT4,
94         SIMPLECOLUMN_INT8,
95         SIMPLECOLUMN_UINT1,
96         SIMPLECOLUMN_UINT2,
97         SIMPLECOLUMN_UINT4,
98         SIMPLECOLUMN_UINT8,
99         SIMPLECOLUMN_DECIMAL1,
100         SIMPLECOLUMN_DECIMAL2,
101         SIMPLECOLUMN_DECIMAL4,
102         SIMPLECOLUMN_DECIMAL8,
103 
104         FILTER,
105         CONDITIONFILTER,
106         EXISTSFILTER,
107         SELECTFILTER,
108         SIMPLEFILTER,
109         SIMPLESCALARFILTER,
110 
111         OPERATOR,
112         ARITHMETICOPERATOR,
113         PREDICATEOPERATOR,
114         LOGICOPERATOR,
115 
116         /**** /TreeNodes */
117 
118         PARSETREE,
119         CALPONTSELECTEXECUTIONPLAN,
120         CONSTANTFILTER,
121         OUTERJOINONFILTER,
122 
123         /** UDAF SDK */
124         MCSV1_CONTEXT,
125         UDAFCOLUMN,
126     };
127 
128     typedef uint8_t id_t;    //expand as necessary
129 
130     /** @brief Creates a new TreeNode object from the ByteStream
131      *
132      * @param b The ByteStream to create it from
133      * @return A newly allocated TreeNode
134      */
135     static TreeNode* createTreeNode(messageqcpp::ByteStream& b);
136 
137     /** @brief Creates a new ParseTree from the ByteStream
138      *
139      * @param b The ByteStream to create it from
140      * @return A newly allocated ParseTree
141      */
142     static ParseTree* createParseTree(messageqcpp::ByteStream& b);
143 
144     /** @brief Creates a new CalpontExecutionPlan from the ByteStream
145      *
146      * @param b The ByteStream to create it from
147      * @return A newly allocated CalpontExecutionPlan
148      */
149     static CalpontExecutionPlan* createExecutionPlan(messageqcpp::ByteStream& b);
150 
151     /** @brief Serialize() for ParseTrees
152      *
153      * This function effectively serializes a ParseTree.
154      * @param tree The ParseTree to write out
155      * @param b The ByteStream to write tree to
156      */
157     static void writeParseTree(const ParseTree* tree,
158                                messageqcpp::ByteStream& b);
159 
160     /** @brief Verify the type of the next object in the ByteStream
161      *
162      * @param b The ByteStream to read from
163      * @param type The type it should be
164      * @throw UnserializeException if the type does not match; this is a fatal error.
165      */
166     static void checkType(messageqcpp::ByteStream& b, const CLASSID type);
167 };
168 
169 }
170 #endif // EXECPLAN_OBJECTREADER_H
171 
172