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