hce-node application  1.4.3
HCE Hierarchical Cluster Engine node application
 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 
28 
29 namespace HCE {
30 
32  //citrusleaf_init ();
34  if (!_clc) {
35  throw Poco::Exception("clc is not created");
36  }
37  cl_write_parameters_set_default(&_clwp);
38  if(_writeUnique) {
39  //cl_write_parameters_set_unique(&_clwp); // not declared
40  _clwp.unique = true;
41  }
42 }
43 
45  if (_clc) {
47  }
48  //citrusleaf_shutdown();
49 }
50 
51 void AerospikeDB::setDriverUrl( const std::string & dbUrl) {
52  Poco::URI url(dbUrl);
53  std::string host = url.getHost();
54  unsigned short port = url.getPort();
55  citrusleaf_cluster_add_host(_clc, host.c_str(), port, TIMEOUT);
56 }
57 
58 void AerospikeDB::put(const std::string& key, const std::string& value) {
60  cl_object key_obj;
61  citrusleaf_object_init_str(&key_obj, key.c_str());
62  /*
63  cl_bin values;
64  strcpy( values.bin_name, docs[DEFAULT_DOCUMENT].c_str());
65  citrusleaf_object_init_str(&values.object, values.c_str());
66  int return_value = citrusleaf_put(_clc, _dbName.c_str(), sets[DEFAULT_SET].c_str(), &key_obj, &values, 1, &_clwp);
67  */
71  cl_bin values[2];
72  strcpy( values[0].bin_name, "key");
73  strcpy( values[1].bin_name, "value");
74  citrusleaf_object_init_str(&values[0].object, key.c_str());
75  citrusleaf_object_init_str(&values[1].object, value.c_str());
76  int return_value = citrusleaf_put(_clc, _dbName.c_str(), sets[DEFAULT_SET].c_str(), &key_obj, values, 2, &_clwp);
77 
78  if( return_value != CITRUSLEAF_OK ) {
79  std::cerr << "Failure setting values " << return_value << std::endl;
81  }
82 }
83 
84 std::string AerospikeDB::get(const std::string& key) {
86  cl_object key_obj;
87  citrusleaf_object_init_str(&key_obj, key.c_str());
88  cl_bin *cl_v = NULL;
89  uint32_t generation;
90  int cl_v_len;
91  int rv = citrusleaf_get_all(_clc, _dbName.c_str(), sets[DEFAULT_SET].c_str(), &key_obj, &cl_v, &cl_v_len, TIMEOUT, &generation);
92  if (rv != CITRUSLEAF_OK) {
93  std::cerr << "get failed - " << rv << std::endl;
94  if (cl_v) free(cl_v);
95  return std::string ("");
96  }
97  std::string s;
98  for (int i=0;i< cl_v_len;i++) {
99  #ifdef _DEBUG_
100  fprintf(stderr, "%d: bin %s ",i,cl_v[i].bin_name);
101  #endif
102  switch (cl_v[i].object.type) {
103  case CL_STR:
104  #ifdef _DEBUG_
105  fprintf(stderr, "type string: value %s\n", cl_v[i].object.u.str);
106  #endif
107  s = cl_v[i].object.u.str;
109  break;
110  case CL_INT:
111  #ifdef _DEBUG_
112  fprintf(stderr, "type int: value %ld64\n",cl_v[i].object.u.i64);
113  #endif
114  break;
115  default:
116  #ifdef _DEBUG_
117  fprintf(stderr, "type unknown! (%d)\n",(int)cl_v[i].object.type);
118  #endif
119  break;
120  }
121  }
122  for (int i = 0; i < cl_v_len; i++) {
123  citrusleaf_object_free(&cl_v[i].object);
124  }
125  if (cl_v) free(cl_v);
126  return s;
127 }
128 
129 std::vector<std::string> AerospikeDB::documents;
130 int cb (char *ns, cf_digest *keyd,
131  char *set, uint32_t generation, uint32_t record_ttl,
132  cl_bin *bins, int n_bins, bool is_last, void *udata) {
133  /*
134  std::cout << "Set: " << set << std::endl
135  << "Namespace: " << ns << std::endl
136  << "Generation: " << generation << std::endl
137  << "record_ttl: " << record_ttl << std::endl
138  << "n_bins: " << n_bins << std::endl
139  << "cl_bin: " << bins->bin_name << std::endl
140  << "cl_bin: " << bins->object.u.str << std::endl
141  << "udata: " << udata << std::endl
142  << "============================================" << std::endl;
143  AerospikeDB::count++;
144  */
145  AerospikeDB::documents.push_back(bins->object.u.str);
146 }
147 
148 std::vector<std::string> AerospikeDB::get_all() {
149  //AerospikeDB::count = 0;
150  AerospikeDB::documents.clear();
151  cf_vector* pcfv = citrusleaf_scan_all_nodes(_clc, (char*)_dbName.c_str(), (char*)sets[DEFAULT_SET].c_str(), 0, 0, false, 100, cb, 0, 0);
152  //std::cout << "Count: " << AerospikeDB::count << std::endl;
153  return AerospikeDB::documents;
154 }
155 
156 void AerospikeDB::del(const std::string& key) {
158  cl_object key_obj;
159  citrusleaf_object_init_str(&key_obj, key.c_str());
160  int return_value = citrusleaf_delete(_clc, _dbName.c_str(), sets[DEFAULT_SET].c_str(), &key_obj, NULL);
161  if( return_value != CITRUSLEAF_OK ) {
163  }
164 }
165 
166 
167 }
168 
169 
170 #endif