GPUdb C++ API  Version 6.2.0.3
GPUdb.hpp
Go to the documentation of this file.
1 #ifndef __GPUDB_HPP__
2 #define __GPUDB_HPP__
3 
5 
6 #ifndef GPUDB_NO_HTTPS
7 #include <boost/asio/ssl.hpp>
8 #endif
9 
10 #include <stdint.h>
11 
12 #include <boost/thread/mutex.hpp>
13 
14 namespace gpudb
15 {
16  class GPUdb;
17 }
18 
19 #include "gpudb/Avro.hpp"
20 #include "gpudb/GenericRecord.hpp"
21 #include "gpudb/GPUdbException.hpp"
22 #include "gpudb/Http.hpp"
23 #include "gpudb/Type.hpp"
25 
26 
53 namespace gpudb
54 {
55 class GPUdb : private boost::noncopyable
56 {
57 public:
58 
59  class Options
60  {
61  public:
62  Options();
63 
64 #ifndef GPUDB_NO_HTTPS
65  boost::asio::ssl::context* getSslContext() const;
66 #endif
67 
68  std::string getUsername() const;
69  std::string getPassword() const;
70  bool getUseSnappy() const;
71  size_t getThreadCount() const;
72  avro::ExecutorPtr getExecutor() const;
73  std::map<std::string, std::string>& getHttpHeaders();
74  const std::map<std::string, std::string>& getHttpHeaders() const;
75  size_t getTimeout() const;
76  uint16_t getHostManagerPort() const;
77 
78 #ifndef GPUDB_NO_HTTPS
79  Options& setSslContext(boost::asio::ssl::context* value);
80 #endif
81 
82  Options& setUsername(const std::string& value);
83  Options& setPassword(const std::string& value);
84  Options& setUseSnappy(const bool value);
85  Options& setThreadCount(const size_t value);
86  Options& setExecutor(const avro::ExecutorPtr value);
87  Options& setHttpHeaders(const std::map<std::string, std::string>& value);
88  Options& addHttpHeader(const std::string& header, const std::string& value);
89  Options& setTimeout(const size_t value);
90  Options& setHostManagerPort(const uint16_t value);
91 
92  private:
93 
94 #ifndef GPUDB_NO_HTTPS
95  boost::asio::ssl::context* m_sslContext;
96 #endif
97 
98  std::string m_username;
99  std::string m_password;
100  bool m_useSnappy;
101  size_t m_threadCount;
102  avro::ExecutorPtr m_executor;
103  std::map<std::string, std::string> m_httpHeaders;
104  size_t m_timeout;
105  uint16_t m_hmPort;
106  };
107 
108  static const int64_t END_OF_SET = -9999;
109 
110  static inline std::string getApiVersion() { return GPUdb::API_VERSION; }
111 
112  GPUdb(const HttpUrl& url, const Options& options = Options());
113  GPUdb(const std::string& url, const Options& options = Options());
114  GPUdb(const std::vector<HttpUrl>& urls, const Options& options = Options());
115  GPUdb(const std::vector<std::string>& urls, const Options& options = Options());
116 
118  const HttpUrl& getUrl() const;
119  const std::vector<HttpUrl>& getUrls() const;
120  const HttpUrl& getHmUrl() const;
121  const std::vector<HttpUrl>& getHmUrls() const;
122 
123 #ifndef GPUDB_NO_HTTPS
124  boost::asio::ssl::context* getSslContext() const;
125 #endif
126 
127  const std::string& getUsername() const;
128  const std::string& getPassword() const;
129  bool getUseSnappy() const;
130  size_t getThreadCount() const;
131  avro::ExecutorPtr getExecutor() const;
132  const std::map<std::string, std::string>& getHttpHeaders() const;
133  size_t getTimeout() const;
134 
136  void updateHostManagerPort();
137 
138  template<typename TRequest, typename TResponse>
139  TResponse& submitRequest(const HttpUrl& url,
140  const TRequest& request,
141  TResponse& response,
142  const bool enableCompression = false) const
143  {
144  std::vector<uint8_t> requestBytes;
145  avro::encode(requestBytes, request);
146  RawGpudbResponse gpudbResponse;
147  submitRequestRaw(url, requestBytes, gpudbResponse, enableCompression);
148  avro::decode(response, gpudbResponse.data);
149  return response;
150  }
151 
152  template<typename TRequest, typename TResponse>
153  TResponse& submitRequest(const std::string& endpoint,
154  const TRequest& request,
155  TResponse& response,
156  const bool enableCompression = false) const
157  {
158  std::vector<uint8_t> requestBytes;
159  avro::encode(requestBytes, request);
160  RawGpudbResponse gpudbResponse;
161  submitRequestRaw(endpoint, requestBytes, gpudbResponse, enableCompression);
162  avro::decode(response, gpudbResponse.data);
163  return response;
164  }
165 
166  template<typename TRequest, typename TResponse>
167  TResponse& submitRequest(const char* endpoint,
168  const TRequest& request,
169  TResponse& response,
170  const bool enableCompression = false) const
171  {
172  submitRequest( (std::string) endpoint, request, response, enableCompression );
173  return response;
174  }
175 
176 
190  template<typename TRequest, typename TResponse>
191  TResponse& submitRequestToHostManager(const std::string& endpoint,
192  const TRequest& request,
193  TResponse& response,
194  const bool enableCompression = false) const
195  {
196  // Handle host manager stuff here
197  std::vector<uint8_t> requestBytes;
198  avro::encode(requestBytes, request);
199  RawGpudbResponse gpudbResponse;
200  submitRequestToHostManagerRaw(endpoint, requestBytes, gpudbResponse, enableCompression);
201  avro::decode(response, gpudbResponse.data);
202  return response;
203  } // end submitRequestToHostManager
204 
205 
219  template<typename TRequest, typename TResponse>
220  TResponse& submitRequestToHostManager(const char* endpoint,
221  const TRequest& request,
222  TResponse& response,
223  const bool enableCompression = false) const
224  {
225  submitRequestToHostManager( (std::string) endpoint, request, response, enableCompression );
226  return response;
227  } // end submitRequestToHostManager
228 
229 
230 #include "gpudb/GPUdbFunctions.hpp"
231 
232  void addKnownType(const std::string& typeId, const avro::DecoderPtr& decoder);
233 
234  template<typename T>
235  void addKnownType(const std::string& typeId)
236  {
237  addKnownType(typeId, avro::createDecoder<T>());
238  }
239 
240  template<typename T>
241  void addKnownType(const std::string& typeId, const std::string& schemaString)
242  {
243  addKnownType(typeId, avro::createDecoder<T>(schemaString));
244  }
245 
246  template<typename T>
247  void addKnownType(const std::string& typeId, const ::avro::ValidSchema& schema)
248  {
249  addKnownType(typeId, avro::createDecoder<T>(schema));
250  }
251 
252  void addKnownTypeFromTable(const std::string& tableName, const avro::DecoderPtr& decoder);
253 
254  template<typename T>
255  void addKnownTypeFromTable(const std::string& tableName)
256  {
257  addKnownTypeFromTable(tableName, avro::createDecoder<T>());
258  }
259 
260  template<typename T>
261  void addKnownTypeFromTable(const std::string& tableName, const std::string& schemaString)
262  {
263  addKnownTypeFromTable(tableName, avro::createDecoder<T>(schemaString));
264  }
265 
266  template<typename T>
267  void addKnownTypeFromTable(const std::string& tableName, const ::avro::ValidSchema& schema)
268  {
269  addKnownTypeFromTable(tableName, avro::createDecoder<T>(schema));
270  }
271 
272 private:
273  static const std::string API_VERSION;
274 
275  std::vector<HttpUrl> m_urls;
276  std::vector<HttpUrl> m_hmUrls;
277  mutable boost::mutex m_urlMutex;
278  mutable size_t m_currentUrl;
279  mutable size_t m_currentHmUrl;
280 
281 #ifndef GPUDB_NO_HTTPS
282  boost::asio::ssl::context* m_sslContext;
283 #endif
284 
285  std::string m_username;
286  std::string m_password;
287  std::string m_authorization;
288  bool m_useSnappy;
289  size_t m_threadCount;
290  avro::ExecutorPtr m_executor;
291  std::map<std::string, std::string> m_httpHeaders;
292  size_t m_timeout;
293 
294  mutable std::map<std::string, avro::DecoderPtr> m_knownTypes;
295  mutable boost::mutex m_knownTypesMutex;
296 
298  const HttpUrl* getUrlPointer() const;
299  const HttpUrl* getHmUrlPointer() const;
300  const HttpUrl* switchUrl(const HttpUrl* oldUrl) const;
301  const HttpUrl* switchHmUrl(const HttpUrl* oldUrl) const;
302 
304  void initHttpRequest(HttpRequest& httpRequest) const;
305  void submitRequestRaw(const std::string& endpoint,
306  const std::vector<uint8_t>& request,
307  RawGpudbResponse& response,
308  const bool enableCompression) const;
309  void submitRequestToHostManagerRaw(const std::string& endpoint,
310  const std::vector<uint8_t>& request,
311  RawGpudbResponse& response,
312  const bool enableCompression) const;
313  void submitRequestRaw(const HttpUrl& url,
314  const std::vector<uint8_t>& request,
315  RawGpudbResponse& response,
316  const bool enableCompression,
317  const bool throwOnError = true) const;
318 
320  avro::DecoderPtr getDecoder(const std::string& typeId) const;
321  void setDecoderIfMissing(const std::string& typeId,
322  const std::string& label,
323  const std::string& schemaString,
324  const std::map<std::string, std::vector<std::string> >& properties) const;
325 
327  void createHostManagerUrl( const HttpUrl& url, uint16_t hostManagerPort );
328  void createHostManagerUrls( const std::vector<HttpUrl>& urls, uint16_t hostManagerPort );
329  void setHostManagerPort(uint16_t value);
330 };
331 
332 #include "gpudb/GPUdbTemplates.hpp"
333 
334 } // end namespace gpudb
335 
336 #endif
Options & setSslContext(boost::asio::ssl::context *value)
void addKnownType(const std::string &typeId, const avro::DecoderPtr &decoder)
std::string getPassword() const
void addKnownTypeFromTable(const std::string &tableName, const ::avro::ValidSchema &schema)
Definition: GPUdb.hpp:267
Options & setHttpHeaders(const std::map< std::string, std::string > &value)
boost::asio::ssl::context * getSslContext() const
std::string getUsername() const
TResponse & submitRequest(const HttpUrl &url, const TRequest &request, TResponse &response, const bool enableCompression=false) const
Definition: GPUdb.hpp:139
GPUdb(const HttpUrl &url, const Options &options=Options())
const std::vector< HttpUrl > & getUrls() const
void updateHostManagerPort()
Update the host manager port by inquiring the server.
void addKnownType(const std::string &typeId)
Definition: GPUdb.hpp:235
void addKnownTypeFromTable(const std::string &tableName)
Definition: GPUdb.hpp:255
TResponse & submitRequest(const std::string &endpoint, const TRequest &request, TResponse &response, const bool enableCompression=false) const
Definition: GPUdb.hpp:153
avro::ExecutorPtr getExecutor() const
const std::vector< HttpUrl > & getHmUrls() const
Options & setHostManagerPort(const uint16_t value)
size_t getThreadCount() const
bool getUseSnappy() const
size_t getTimeout() const
Options & setUsername(const std::string &value)
uint16_t getHostManagerPort() const
void addKnownTypeFromTable(const std::string &tableName, const avro::DecoderPtr &decoder)
void addKnownType(const std::string &typeId, const ::avro::ValidSchema &schema)
Definition: GPUdb.hpp:247
const HttpUrl & getHmUrl() const
static std::string getApiVersion()
Definition: GPUdb.hpp:110
Options & setExecutor(const avro::ExecutorPtr value)
TResponse & submitRequestToHostManager(const char *endpoint, const TRequest &request, TResponse &response, const bool enableCompression=false) const
Submit an HTTP request to the host manager.
Definition: GPUdb.hpp:220
std::map< std::string, std::string > & getHttpHeaders()
static const int64_t END_OF_SET
Definition: GPUdb.hpp:108
Options & setTimeout(const size_t value)
A set of output parameters forendpoint /gpudbresponse}.
Definition: gpudb_wrapper.h:17
Options & setThreadCount(const size_t value)
void addKnownTypeFromTable(const std::string &tableName, const std::string &schemaString)
Definition: GPUdb.hpp:261
std::vector< uint8_t > data
Definition: gpudb_wrapper.h:35
const HttpUrl & getUrl() const
Some getters.
Options & addHttpHeader(const std::string &header, const std::string &value)
Options & setUseSnappy(const bool value)
TResponse & submitRequest(const char *endpoint, const TRequest &request, TResponse &response, const bool enableCompression=false) const
Definition: GPUdb.hpp:167
Options & setPassword(const std::string &value)
TResponse & submitRequestToHostManager(const std::string &endpoint, const TRequest &request, TResponse &response, const bool enableCompression=false) const
Submit an HTTP request to the host manager.
Definition: GPUdb.hpp:191
void addKnownType(const std::string &typeId, const std::string &schemaString)
Definition: GPUdb.hpp:241