GPUdb C++ API  Version 6.0.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends
GPUdb.hpp
Go to the documentation of this file.
1 #ifndef __GPUDB_HPP__
2 #define __GPUDB_HPP__
3 
5 
6 #include <boost/thread/mutex.hpp>
7 
8 namespace gpudb
9 {
10  class GPUdb;
11 }
12 
13 #include "gpudb/Avro.hpp"
14 #include "gpudb/GenericRecord.hpp"
15 #include "gpudb/GPUdbException.hpp"
16 #include "gpudb/Type.hpp"
18 
19 namespace gpudb
20 {
21  class GPUdb : private boost::noncopyable
22  {
23  public:
24  class Options
25  {
26  public:
27  Options();
28  std::string getUsername() const;
29  std::string getPassword() const;
30  bool getUseSnappy() const;
31  size_t getThreadCount() const;
32  avro::ExecutorPtr getExecutor() const;
33  Options& setUsername(const std::string& value);
34  Options& setPassword(const std::string& value);
35  Options& setUseSnappy(const bool value);
36  Options& setThreadCount(const size_t value);
37  Options& setExecutor(const avro::ExecutorPtr value);
38 
39  private:
40  std::string username;
41  std::string password;
42  bool useSnappy;
43  size_t threadCount;
44  avro::ExecutorPtr executor;
45  };
46 
47  static const int64_t END_OF_SET = -9999;
48 
49  static inline std::string getApiVersion() { return GPUdb::API_VERSION; }
50 
51  GPUdb(const std::string& url, const Options& options = Options());
52  GPUdb(const std::vector<std::string>& urls, const Options& options = Options());
53  std::string getUrl() const;
54  std::string getUsername() const;
55  std::string getPassword() const;
56  bool getUseSnappy() const;
57  size_t getThreadCount() const;
58  avro::ExecutorPtr getExecutor() const;
59 
60  template<typename TRequest, typename TResponse> TResponse& submitRequest(const std::string& endpoint, const TRequest& request, TResponse& response, const bool enableCompression = false) const
61  {
62  std::vector<uint8_t> requestBytes;
63  avro::encode(requestBytes, request);
64  GpudbResponse gpudbResponse;
65  submitRequestRaw(endpoint, requestBytes, gpudbResponse, enableCompression);
66  avro::decode(response, gpudbResponse.data);
67  return response;
68  }
69 
70  #include "gpudb/GPUdbFunctions.hpp"
71 
72  void addKnownType(const std::string& typeId, const avro::DecoderPtr& decoder);
73 
74  template<typename T> void addKnownType(const std::string& typeId)
75  {
76  addKnownType(typeId, avro::createDecoder<T>());
77  }
78 
79  template<typename T> void addKnownType(const std::string& typeId, const std::string& schemaString)
80  {
81  addKnownType(typeId, avro::createDecoder<T>(schemaString));
82  }
83 
84  template<typename T> void addKnownType(const std::string& typeId, const ::avro::ValidSchema& schema)
85  {
86  addKnownType(typeId, avro::createDecoder<T>(schema));
87  }
88 
89  void addKnownTypeFromTable(const std::string& tableName, const avro::DecoderPtr& decoder);
90 
91  template<typename T> void addKnownTypeFromTable(const std::string& tableName)
92  {
93  addKnownTypeFromTable(tableName, avro::createDecoder<T>());
94  }
95 
96  template<typename T> void addKnownTypeFromTable(const std::string& tableName, const std::string& schemaString)
97  {
98  addKnownTypeFromTable(tableName, avro::createDecoder<T>(schemaString));
99  }
100 
101  template<typename T> void addKnownTypeFromTable(const std::string& tableName, const ::avro::ValidSchema& schema)
102  {
103  addKnownTypeFromTable(tableName, avro::createDecoder<T>(schema));
104  }
105 
106  private:
107  static const std::string API_VERSION;
108 
109  /*
110  * Wraps connection params together for cycling in the HA logic
111  */
112  struct ConnectionToken
113  {
114  ConnectionToken(const std::string& url);
115  bool operator == (const ConnectionToken &rhs) const;
116  bool operator != (const ConnectionToken &rhs) const;
117 
118  std::string url;
119  std::string host;
120  std::string port;
121  std::string path;
122  bool secure;
123  };
124 
125  /*
126  * Manages the list of available hosts for HA. Randomly selects
127  */
128  struct ConnectionTokenManager
129  {
130  /*
131  * Populates the list and randomly assigns the currentIndex
132  */
133  void initialize(const std::vector<ConnectionToken>& tokens);
134 
135  /*
136  * @return The currently available connection information
137  */
138  ConnectionToken getCurrentToken() const;
139 
140  /*
141  * Cycles the index to the next entry (wrapping around if
142  * necessary)
143  * @return The new current token
144  */
145  ConnectionToken getNextToken();
146 
147  private:
148  std::size_t currentIndex;
149  std::vector<ConnectionToken> tokens;
150  mutable boost::mutex tokensMutex;
151  };
152 
153  void initializeConnectionTokens(
154  const std::vector<std::string>& urls);
155 
156  // Mutable for usage in submitRequestRaw
157  mutable ConnectionTokenManager tokenManager;
158 
159  std::string username;
160  std::string password;
161  std::string authorization;
162  bool useSnappy;
163  size_t threadCount;
164  avro::ExecutorPtr executor;
165  mutable std::map<std::string, avro::DecoderPtr> knownTypes;
166  mutable boost::mutex knownTypesMutex;
167 
168  void submitRequestRaw(const std::string& endpoint, const std::vector<uint8_t>& request, GpudbResponse& response, const bool enableCompression) const;
169  avro::DecoderPtr getDecoder(const std::string& typeId) const;
170  void setDecoderIfMissing(const std::string& typeId, const std::string& label, const std::string& schemaString, const std::map<std::string, std::vector<std::string> >& properties) const;
171  };
172 
173  #include "gpudb/GPUdbTemplates.hpp"
174 }
175 
176 #endif
void addKnownType(const std::string &typeId, const avro::DecoderPtr &decoder)
void addKnownTypeFromTable(const std::string &tableName, const ::avro::ValidSchema &schema)
Definition: GPUdb.hpp:101
void addKnownType(const std::string &typeId)
Definition: GPUdb.hpp:74
void addKnownTypeFromTable(const std::string &tableName)
Definition: GPUdb.hpp:91
avro::ExecutorPtr getExecutor() const
std::string getUsername() const
std::string getPassword() const
Options & setUsername(const std::string &value)
GPUdb(const std::string &url, const Options &options=Options())
size_t getThreadCount() const
A set of output parameters forendpoint /gpudbresponse}.
Definition: gpudb_wrapper.h:17
void addKnownTypeFromTable(const std::string &tableName, const avro::DecoderPtr &decoder)
TResponse & submitRequest(const std::string &endpoint, const TRequest &request, TResponse &response, const bool enableCompression=false) const
Definition: GPUdb.hpp:60
void addKnownType(const std::string &typeId, const ::avro::ValidSchema &schema)
Definition: GPUdb.hpp:84
size_t getThreadCount() const
std::string getPassword() const
static std::string getApiVersion()
Definition: GPUdb.hpp:49
Options & setExecutor(const avro::ExecutorPtr value)
std::string getUsername() const
static const int64_t END_OF_SET
Definition: GPUdb.hpp:47
avro::ExecutorPtr getExecutor() const
Options & setThreadCount(const size_t value)
void addKnownTypeFromTable(const std::string &tableName, const std::string &schemaString)
Definition: GPUdb.hpp:96
bool getUseSnappy() const
std::string getUrl() const
Options & setUseSnappy(const bool value)
Options & setPassword(const std::string &value)
void addKnownType(const std::string &typeId, const std::string &schemaString)
Definition: GPUdb.hpp:79
bool getUseSnappy() const
std::vector< uint8_t > data
Definition: gpudb_wrapper.h:35