HCE project C++ developers source code library  1.1.1
HCE project developer library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Aerospike.cpp
Go to the documentation of this file.
1 
15 #ifdef _USE_AEROSPIKE_DB_
16 
17 
21 #include "Aerospike.hpp"
22 
26 #include <string.h>
27 
29  //citrusleaf_init ();
30  _clc = citrusleaf_cluster_create();
31  if (!_clc) {
32  throw Poco::Exception("clc is not created");
33  }
34 }
35 
37  if (_clc) {
38  citrusleaf_cluster_destroy(_clc);
39  }
40  //citrusleaf_shutdown();
41 }
42 
43 void HCE::AerospikeDB::setDriverUrl( const std::string & dbUrl) {
44  Poco::URI url(dbUrl);
45  std::string host = url.getHost();
46  unsigned short port = url.getPort();
47  citrusleaf_cluster_add_host(_clc, host.c_str(), port, TIMEOUT);
48 }
49 
50 void HCE::AerospikeDB::put(const std::string& key, const std::string& _value) {
52  cl_object key_obj;
53  citrusleaf_object_init_str(&key_obj, key.c_str());
54  cl_bin value;
55  strcpy( value.bin_name, docs[DEFAULT_DOCUMENT].c_str());
56  citrusleaf_object_init_str(&value.object, _value.c_str());
57  int return_value = citrusleaf_put(_clc, _dbName.c_str(), sets[DEFAULT_SET].c_str(), &key_obj, &value, 1, NULL);
58  if( return_value != CITRUSLEAF_OK ) {
59  std::cerr << "Failure setting values " << return_value << std::endl;
60  _error = ACTION_GENERAL_FAILURE;
61  }
62 }
63 
64 std::string HCE::AerospikeDB::get(const std::string& key) {
66  cl_object key_obj;
67  citrusleaf_object_init_str(&key_obj, key.c_str());
68  cl_bin *cl_v = NULL;
69  uint32_t generation;
70  int cl_v_len;
71  int rv = citrusleaf_get_all(_clc, _dbName.c_str(), sets[DEFAULT_SET].c_str(), &key_obj, &cl_v, &cl_v_len, TIMEOUT, &generation);
72  if (rv != CITRUSLEAF_OK) {
73  std::cerr << "get failed - " << rv << std::endl;
74  if (cl_v) free(cl_v);
75  return std::string ("");
76  }
77  std::string s;
78  for (int i=0;i< cl_v_len;i++) {
79  #ifdef _DEBUG_
80  fprintf(stderr, "%d: bin %s ",i,cl_v[i].bin_name);
81  #endif
82  switch (cl_v[i].object.type) {
83  case CL_STR:
84  #ifdef _DEBUG_
85  fprintf(stderr, "type string: value %s\n", cl_v[i].object.u.str);
86  #endif
87  s = cl_v[i].object.u.str;
88  _error = DOCUMENT_PRESENT;
89  break;
90  case CL_INT:
91  #ifdef _DEBUG_
92  fprintf(stderr, "type int: value %ld64\n",cl_v[i].object.u.i64);
93  #endif
94  break;
95  default:
96  #ifdef _DEBUG_
97  fprintf(stderr, "type unknown! (%d)\n",(int)cl_v[i].object.type);
98  #endif
99  break;
100  }
101  }
102  for (int i = 0; i < cl_v_len; i++) {
103  citrusleaf_object_free(&cl_v[i].object);
104  }
105  if (cl_v) free(cl_v);
106  return s;
107 }
108 
109 std::vector<std::string> HCE::AerospikeDB::get_all() {
110  std::vector<std::string> data;
111  return data;
112 }
113 
114 void HCE::AerospikeDB::del(const std::string& key) {
116  cl_object key_obj;
117  citrusleaf_object_init_str(&key_obj, key.c_str());
118  int return_value = citrusleaf_delete(_clc, _dbName.c_str(), sets[DEFAULT_SET].c_str(), &key_obj, NULL);
119  if( return_value != CITRUSLEAF_OK ) {
120  _error = ACTION_GENERAL_FAILURE;
121  }
122 }
123 
124 #endif