GPUdb C++ API  Version 7.0.19.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GenericRecord.hpp
Go to the documentation of this file.
1 #ifndef __GPUDB__GENERICRECORD_HPP__
2 #define __GPUDB__GENERICRECORD_HPP__
3 
4 #include "gpudb/Type.hpp"
5 
6 #include <avro/Schema.hh>
7 #include <avro/Specific.hh>
8 
9 #include <boost/core/demangle.hpp>
10 #include <boost/lexical_cast.hpp>
11 
12 namespace gpudb
13 {
14 
15  typedef boost::shared_ptr<Type> gpudb_type_ptr_t;
16 
18  {
19  friend class GPUdb;
20 
21  friend struct ::avro::codec_traits<GenericRecord>;
22 
23  public:
25  GenericRecord(const Type& type);
26 
27  const Type& getType() const;
28  const ::avro::ValidSchema& getSchema() const;
29 
30  template<typename T> T& value(const size_t index)
31  {
32  T* o = boost::any_cast<T>(&m_values.at(index));
33  if (!o) throw std::runtime_error("Error converting GenericRecord column " + boost::lexical_cast<std::string>(index) + " to type " + boost::core::demangle(typeid(T).name() ));
34  return *o;
35  }
36 
37  template<typename T> const T& value(const size_t index) const
38  {
39  const T* o = boost::any_cast<T>(&m_values.at(index));
40  if (!o) throw std::runtime_error("Error converting GenericRecord column " + boost::lexical_cast<std::string>(index) + " to type " + boost::core::demangle(typeid(T).name() ));
41  return *o;
42  }
43 
44  template<typename T> T& value(const std::string& name)
45  {
46  T* o = boost::any_cast<T>(&m_values[m_type.getColumnIndex(name)]);
47  if (!o) throw std::runtime_error("Error converting GenericRecord column '" + name + "' to type " + boost::core::demangle(typeid(T).name() ));
48  return *o;
49  }
50 
51  template<typename T> const T& value(const std::string& name) const
52  {
53  const T* o = boost::any_cast<T>(&m_values[m_type.getColumnIndex(name)]);
54  if (!o) throw std::runtime_error("Error converting GenericRecord column '" + name + "' to type " + boost::core::demangle(typeid(T).name() ));
55  return *o;
56  }
57 
58  std::vector<uint8_t>& bytesValue(const size_t index);
59  const std::vector<uint8_t>& bytesValue(const size_t index) const;
60  std::vector<uint8_t>& bytesValue(const std::string& name);
61  const std::vector<uint8_t>& bytesValue(const std::string& name) const;
62 
63  double& doubleValue(const size_t index);
64  const double& doubleValue(const size_t index) const;
65  double& doubleValue(const std::string& name);
66  const double& doubleValue(const std::string& name) const;
67 
68  float& floatValue(const size_t index);
69  const float& floatValue(const size_t index) const;
70  float& floatValue(const std::string& name);
71  const float& floatValue(const std::string& name) const;
72 
73  int32_t& intValue(const size_t index);
74  const int32_t& intValue(const size_t index) const;
75  int32_t& intValue(const std::string& name);
76  const int32_t& intValue(const std::string& name) const;
77 
78  int64_t& longValue(const size_t index);
79  const int64_t& longValue(const size_t index) const;
80  int64_t& longValue(const std::string& name);
81  const int64_t& longValue(const std::string& name) const;
82 
83  std::string& stringValue(const size_t index);
84  const std::string& stringValue(const size_t index) const;
85  std::string& stringValue(const std::string& name);
86  const std::string& stringValue(const std::string& name) const;
87 
88  boost::optional<std::vector<uint8_t> >& nullableBytesValue(const size_t index);
89  const boost::optional<std::vector<uint8_t> >& nullableBytesValue(const size_t index) const;
90  boost::optional<std::vector<uint8_t> >& nullableBytesValue(const std::string& name);
91  const boost::optional<std::vector<uint8_t> >& nullableBytesValue(const std::string& name) const;
92 
93  boost::optional<double>& nullableDoubleValue(const size_t index);
94  const boost::optional<double>& nullableDoubleValue(const size_t index) const;
95  boost::optional<double>& nullableDoubleValue(const std::string& name);
96  const boost::optional<double>& nullableDoubleValue(const std::string& name) const;
97 
98  boost::optional<float>& nullableFloatValue(const size_t index);
99  const boost::optional<float>& nullableFloatValue(const size_t index) const;
100  boost::optional<float>& nullableFloatValue(const std::string& name);
101  const boost::optional<float>& nullableFloatValue(const std::string& name) const;
102 
103  boost::optional<int32_t>& nullableIntValue(const size_t index);
104  const boost::optional<int32_t>& nullableIntValue(const size_t index) const;
105  boost::optional<int32_t>& nullableIntValue(const std::string& name);
106  const boost::optional<int32_t>& nullableIntValue(const std::string& name) const;
107 
108  boost::optional<int64_t>& nullableLongValue(const size_t index);
109  const boost::optional<int64_t>& nullableLongValue(const size_t index) const;
110  boost::optional<int64_t>& nullableLongValue(const std::string& name);
111  const boost::optional<int64_t>& nullableLongValue(const std::string& name) const;
112 
113  boost::optional<std::string>& nullableStringValue(const size_t index);
114  const boost::optional<std::string>& nullableStringValue(const size_t index) const;
115  boost::optional<std::string>& nullableStringValue(const std::string& name);
116  const boost::optional<std::string>& nullableStringValue(const std::string& name) const;
117 
118  bool isNull(const size_t index) const;
119  bool isNull(const std::string& name) const;
120 
121  void getAsBytes(const size_t index, std::vector<uint8_t>& result) const;
122  void getAsBytes(const std::string& name, std::vector<uint8_t>& result) const;
123  std::vector<uint8_t> getAsBytes(const size_t index) const;
124  std::vector<uint8_t> getAsBytes(const std::string& name) const;
125 
126  void getAsDouble(const size_t index, double& result) const;
127  void getAsDouble(const std::string& name, double& result) const;
128  double getAsDouble(const size_t index) const;
129  double getAsDouble(const std::string& name) const;
130 
131  void getAsFloat(const size_t index, float& result) const;
132  void getAsFloat(const std::string& name, float& result) const;
133  float getAsFloat(const size_t index) const;
134  float getAsFloat(const std::string& name) const;
135 
136  void getAsInt(const size_t index, int32_t& result) const;
137  void getAsInt(const std::string& name, int32_t& result) const;
138  int32_t getAsInt(const size_t index) const;
139  int32_t getAsInt(const std::string& name) const;
140 
141  void getAsLong(const size_t index, int64_t& result) const;
142  void getAsLong(const std::string& name, int64_t& result) const;
143  int64_t getAsLong(const size_t index) const;
144  int64_t getAsLong(const std::string& name) const;
145 
146  void getAsString(const size_t index, std::string& result) const;
147  void getAsString(const std::string& name, std::string& result) const;
148  std::string getAsString(const size_t index) const;
149  std::string getAsString(const std::string& name) const;
150 
151  void getAsNullableBytes(const size_t index, boost::optional<std::vector<uint8_t> >& result) const;
152  void getAsNullableBytes(const std::string& name, boost::optional<std::vector<uint8_t> >& result) const;
153  boost::optional<std::vector<uint8_t> > getAsNullableBytes(const size_t index) const;
154  boost::optional<std::vector<uint8_t> > getAsNullableBytes(const std::string& name) const;
155 
156  void getAsNullableDouble(const size_t index, boost::optional<double>& result) const;
157  void getAsNullableDouble(const std::string& name, boost::optional<double>& result) const;
158  boost::optional<double> getAsNullableDouble(const size_t index) const;
159  boost::optional<double> getAsNullableDouble(const std::string& name) const;
160 
161  void getAsNullableFloat(const size_t index, boost::optional<float>& result) const;
162  void getAsNullableFloat(const std::string& name, boost::optional<float>& result) const;
163  boost::optional<float> getAsNullableFloat(const size_t index) const;
164  boost::optional<float> getAsNullableFloat(const std::string& name) const;
165 
166  void getAsNullableInt(const size_t index, boost::optional<int32_t>& result) const;
167  void getAsNullableInt(const std::string& name, boost::optional<int32_t>& result) const;
168  boost::optional<int32_t> getAsNullableInt(const size_t index) const;
169  boost::optional<int32_t> getAsNullableInt(const std::string& name) const;
170 
171  void getAsNullableLong(const size_t index, boost::optional<int64_t>& result) const;
172  void getAsNullableLong(const std::string& name, boost::optional<int64_t>& result) const;
173  boost::optional<int64_t> getAsNullableLong(const size_t index) const;
174  boost::optional<int64_t> getAsNullableLong(const std::string& name) const;
175 
176  void getAsNullableString(const size_t index, boost::optional<std::string>& result) const;
177  void getAsNullableString(const std::string& name, boost::optional<std::string>& result) const;
178  boost::optional<std::string> getAsNullableString(const size_t index) const;
179  boost::optional<std::string> getAsNullableString(const std::string& name) const;
180 
181  void setNull(const size_t index);
182  void setNull(const std::string& name);
183 
184  void setAsBytes(const size_t index, const std::vector<uint8_t>& newValue);
185  void setAsBytes(const std::string& name, const std::vector<uint8_t>& newValue);
186 
187  void setAsDouble(const size_t index, const double& newValue);
188  void setAsDouble(const std::string& name, const double& newValue);
189 
190  void setAsFloat(const size_t index, const float& newValue);
191  void setAsFloat(const std::string& name, const float& newValue);
192 
193  void setAsInt(const size_t index, const int32_t& newValue);
194  void setAsInt(const std::string& name, const int32_t& newValue);
195 
196  void setAsLong(const size_t index, const int64_t& newValue);
197  void setAsLong(const std::string& name, const int64_t& newValue);
198 
199  void setAsString(const size_t index, const std::string& newValue);
200  void setAsString(const std::string& name, const std::string& newValue);
201 
202  void setAsNullableBytes(const size_t index, const boost::optional<std::vector<uint8_t> >& newValue);
203  void setAsNullableBytes(const std::string& name, const boost::optional<std::vector<uint8_t> >& newValue);
204 
205  void setAsNullableDouble(const size_t index, const boost::optional<double>& newValue);
206  void setAsNullableDouble(const std::string& name, const boost::optional<double>& newValue);
207 
208  void setAsNullableFloat(const size_t index, const boost::optional<float>& newValue);
209  void setAsNullableFloat(const std::string& name, const boost::optional<float>& newValue);
210 
211  void setAsNullableInt(const size_t index, const boost::optional<int32_t>& newValue);
212  void setAsNullableInt(const std::string& name, const boost::optional<int32_t>& newValue);
213 
214  void setAsNullableLong(const size_t index, const boost::optional<int64_t>& newValue);
215  void setAsNullableLong(const std::string& name, const boost::optional<int64_t>& newValue);
216 
217  void setAsNullableString(const size_t index, const boost::optional<std::string>& newValue);
218  void setAsNullableString(const std::string& name, const boost::optional<std::string>& newValue);
219 
220  void toString(const size_t index, std::string& result) const;
221  void toString(const std::string& name, std::string& result) const;
222  std::string toString(const size_t index) const;
223  std::string toString(const std::string& name) const;
224 
225  friend std::ostream &operator << (std::ostream &os, GenericRecord &gr);
226  friend std::ostream &operator << (std::ostream &os, const GenericRecord &gr);
227 
236  static void decode(const std::string& schemaString, const std::vector<std::vector<uint8_t> >& encodedData, std::vector<GenericRecord>& data );
237 
245  static void decode(const std::string& schemaString, const std::vector<uint8_t>& encodedData, GenericRecord& data );
246 
247  private:
248 
249  Type m_type;
250  std::vector<boost::any> m_values;
251 
252  GenericRecord(const std::vector<std::pair<Type::Column::ColumnType, bool> >& columnTypes);
253  void initializeColumn(const Type::Column::ColumnType type, const bool isNullable);
254 
255  static void transpose(const std::string& schemaString, const std::vector<uint8_t>& encodedData, std::vector<GenericRecord>& data, gpudb_type_ptr_t &dataTypePtr );
256 
257  };
258 
260 }
261 
262 namespace avro
263 {
264  template<> struct codec_traits<gpudb::GenericRecord>
265  {
266  static void encode(Encoder& e, const gpudb::GenericRecord& v);
267  static void decode(Decoder& d, gpudb::GenericRecord& v);
268  };
269 }
270 
271 #endif
void setAsBytes(const size_t index, const std::vector< uint8_t > &newValue)
void setAsFloat(const size_t index, const float &newValue)
void setAsNullableFloat(const size_t index, const boost::optional< float > &newValue)
GenericRecord(const Type &type)
Create a blank GenericRecord object from a given type.
T & value(const size_t index)
boost::shared_ptr< Type > gpudb_type_ptr_t
void getAsNullableLong(const size_t index, boost::optional< int64_t > &result) const
boost::optional< double > & nullableDoubleValue(const size_t index)
void toString(const size_t index, std::string &result) const
int32_t & intValue(const size_t index)
boost::optional< int32_t > & nullableIntValue(const size_t index)
void setAsDouble(const size_t index, const double &newValue)
const T & value(const size_t index) const
size_t getColumnIndex(const std::string &name) const
GenericRecord DynamicTableRecord
bool isNull(const size_t index) const
void setAsNullableLong(const size_t index, const boost::optional< int64_t > &newValue)
void getAsNullableInt(const size_t index, boost::optional< int32_t > &result) const
std::string & stringValue(const size_t index)
void getAsLong(const size_t index, int64_t &result) const
void getAsNullableFloat(const size_t index, boost::optional< float > &result) const
static void decode(const std::string &schemaString, const std::vector< std::vector< uint8_t > > &encodedData, std::vector< GenericRecord > &data)
Decodes avro encoded data (given the schema string) into GenericRecord objects.
void setAsNullableInt(const size_t index, const boost::optional< int32_t > &newValue)
void setAsNullableDouble(const size_t index, const boost::optional< double > &newValue)
void setAsString(const size_t index, const std::string &newValue)
boost::optional< int64_t > & nullableLongValue(const size_t index)
void getAsNullableString(const size_t index, boost::optional< std::string > &result) const
boost::optional< float > & nullableFloatValue(const size_t index)
float & floatValue(const size_t index)
int64_t & longValue(const size_t index)
friend std::ostream & operator<<(std::ostream &os, GenericRecord &gr)
void getAsNullableDouble(const size_t index, boost::optional< double > &result) const
void getAsBytes(const size_t index, std::vector< uint8_t > &result) const
void getAsFloat(const size_t index, float &result) const
const Type & getType() const
void setAsLong(const size_t index, const int64_t &newValue)
const T & value(const std::string &name) const
double & doubleValue(const size_t index)
void getAsInt(const size_t index, int32_t &result) const
void setAsNullableBytes(const size_t index, const boost::optional< std::vector< uint8_t > > &newValue)
boost::optional< std::string > & nullableStringValue(const size_t index)
void setNull(const size_t index)
void getAsNullableBytes(const size_t index, boost::optional< std::vector< uint8_t > > &result) const
const ::avro::ValidSchema & getSchema() const
T & value(const std::string &name)
boost::optional< std::vector< uint8_t > > & nullableBytesValue(const size_t index)
void getAsString(const size_t index, std::string &result) const
void setAsNullableString(const size_t index, const boost::optional< std::string > &newValue)
void getAsDouble(const size_t index, double &result) const
void setAsInt(const size_t index, const int32_t &newValue)
std::vector< uint8_t > & bytesValue(const size_t index)