/
cassandra.h
448 lines (387 loc) · 15.7 KB
/
cassandra.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
/*
* LibCassandra
* Copyright (C) 2010 Padraig O'Sullivan
* All rights reserved.
*
* Use and distribution licensed under the BSD license. See
* the COPYING file in the parent directory for full text.
*/
#ifndef __LIBCASSANDRA_CASSANDRA_H
#define __LIBCASSANDRA_CASSANDRA_H
#include <string>
#include <vector>
#include <set>
#include <map>
#include <tr1/memory>
#include "libgenthrift/cassandra_types.h"
#include "libcassandra/indexed_slices_query.h"
#include "libcassandra/keyspace_definition.h"
namespace org
{
namespace apache
{
namespace cassandra
{
class CassandraClient;
}
}
}
namespace libcassandra
{
class Keyspace;
class Cassandra
{
public:
Cassandra();
Cassandra(org::apache::cassandra::CassandraClient *in_thrift_client,
const std::string &in_host,
int in_port);
Cassandra(org::apache::cassandra::CassandraClient *in_thrift_client,
const std::string &in_host,
int in_port,
const std::string& keyspace);
~Cassandra();
enum FailoverPolicy
{
FAIL_FAST= 0, /* return error as is to user */
ON_FAIL_TRY_ONE_NEXT_AVAILABLE, /* try 1 random server before returning to user */
ON_FAIL_TRY_ALL_AVAILABLE /* try all available servers in cluster before return to user */
};
/**
* @return the underlying cassandra thrift client.
*/
org::apache::cassandra::CassandraClient *getCassandra();
/**
* Log for the current session
* @param[in] user to use for authentication
* @param[in] password to use for authentication
*/
void login(const std::string& user, const std::string& password);
/**
* @return the keyspace associated with this session
*/
std::string getCurrentKeyspace() const;
/**
* set the keyspace for the current connection
* @param[in] ks_name name of the keyspace to specify for current session
*/
void setKeyspace(const std::string& ks_name);
/**
* @return all the keyspace definitions.
*/
std::vector<KeyspaceDefinition> getKeyspaces();
/**
* Insert a column, possibly inside a supercolumn
*
* @param[in] key the column key
* @param[in] column_family the column family
* @param[in] super_column_name the super column name (optional)
* @param[in] column_name the column name
* @param[in] value the column value
* @param[in] level consistency level
* @param[in] ttl time to live
*/
void insertColumn(const std::string& key,
const std::string& column_family,
const std::string& super_column_name,
const std::string& column_name,
const std::string& value,
org::apache::cassandra::ConsistencyLevel::type level,
int32_t ttl);
/**
* Insert a column, possibly inside a supercolumn
*
* @param[in] key the column key
* @param[in] column_family the column family
* @param[in] super_column_name the super column name (optional)
* @param[in] column_name the column name
* @param[in] value the column value
*/
void insertColumn(const std::string& key,
const std::string& column_family,
const std::string& super_column_name,
const std::string& column_name,
const std::string& value);
/**
* Insert a column, directly in a columnfamily
*
* @param[in] key the column key
* @param[in] column_family the column family
* @param[in] column_name the column name
* @param[in] value the column value
*/
void insertColumn(const std::string& key,
const std::string& column_family,
const std::string& column_name,
const std::string& value);
/**
* Insert a column, directly in a columnfamily
*
* @param[in] key the column key
* @param[in] column_family the column family
* @param[in] column_name the column name
* @param[in] value the column value
*/
void insertColumn(const std::string& key,
const std::string& column_family,
const std::string& column_name,
const int64_t value);
/**
* Removes all the columns that match the given column path
*
* @param[in] key the column or super column key
* @param[in] col_path the path to the column or super column
* @param[in] level consistency level
*/
void remove(const std::string& key,
const org::apache::cassandra::ColumnPath& col_path,
org::apache::cassandra::ConsistencyLevel::type level);
/**
* Removes all the columns that match the given column path
*
* @param[in] key the column or super column key
* @param[in] col_path the path to the column or super column
*/
void remove(const std::string& key,
const org::apache::cassandra::ColumnPath& col_path);
/**
* Removes all the columns that match the given arguments
* Can remove all under a column family, an individual column or supercolumn under a column family, or an individual column under a supercolumn
*
* @param[in] key the column key
* @param[in] column_family the column family
* @param[in] super_column_name the super column name (optional)
* @param[in] column_name the column name (optional)
*/
void remove(const std::string& key,
const std::string& column_family,
const std::string& super_column_name,
const std::string& column_name);
/**
* Remove a column, possibly inside a supercolumn
*
* @param[in] key the column key
* @param[in] column_family the column family
* @param[in] super_column_name the super column name (optional)
* @param[in] column_name the column name (optional)
*/
void removeColumn(const std::string& key,
const std::string& column_family,
const std::string& super_column_name,
const std::string& column_name);
/**
* Remove a super column and all columns under it
*
* @param[in] key the column key
* @param[in] column_family the column family
* @param[in] super_column_name the super column name
*/
void removeSuperColumn(const std::string& key,
const std::string& column_family,
const std::string& super_column_name);
/**
* Rertieve a column.
*
* @param[in] key the column key
* @param[in] column_family the column family
* @param[in] super_column_name the super column name (optional)
* @param[in] column_name the column name (optional)
* @param[in] level consistency level
* @return a column
*/
org::apache::cassandra::Column getColumn(const std::string& key,
const std::string& column_family,
const std::string& super_column_name,
const std::string& column_name,
org::apache::cassandra::ConsistencyLevel::type level);
/**
* Rertieve a column.
*
* @param[in] key the column key
* @param[in] column_family the column family
* @param[in] super_column_name the super column name (optional)
* @param[in] column_name the column name (optional)
* @return a column
*/
org::apache::cassandra::Column getColumn(const std::string& key,
const std::string& column_family,
const std::string& super_column_name,
const std::string& column_name);
/**
* Retrieve a column
*
* @param[in] key the column key
* @param[in] column_family the column family
* @param[in] column_name the column name (optional)
* @return a column
*/
org::apache::cassandra::Column getColumn(const std::string& key,
const std::string& column_family,
const std::string& column_name);
/**
* Retrieve a column value
*
* @param[in] key the column key
* @param[in] column_family the column family
* @param[in] super_column_name the super column name (optional)
* @param[in] column_name the column name (optional)
* @return the value for the column that corresponds to the given parameters
*/
std::string getColumnValue(const std::string& key,
const std::string& column_family,
const std::string& super_column_name,
const std::string& column_name);
/**
* Retrieve a column value
*
* @param[in] key the column key
* @param[in] column_family the column family
* @param[in] column_name the column name (optional)
* @return the value for the column that corresponds to the given parameters
*/
std::string getColumnValue(const std::string& key,
const std::string& column_family,
const std::string& column_name);
/**
* Retrieve a column value
*
* @param[in] key the column key
* @param[in] column_family the column family
* @param[in] column_name the column name (optional)
* @return the value for the column that corresponds to the given parameters
* but as an integer
*/
int64_t getIntegerColumnValue(const std::string& key,
const std::string& column_family,
const std::string& column_name);
org::apache::cassandra::SuperColumn getSuperColumn(const std::string& key,
const std::string& column_family,
const std::string& super_column_name,
org::apache::cassandra::ConsistencyLevel::type level);
org::apache::cassandra::SuperColumn getSuperColumn(const std::string& key,
const std::string& column_family,
const std::string& super_column_name);
std::vector<org::apache::cassandra::Column> getSliceNames(const std::string& key,
const org::apache::cassandra::ColumnParent& col_parent,
org::apache::cassandra::SlicePredicate& pred,
org::apache::cassandra::ConsistencyLevel::type level);
std::vector<org::apache::cassandra::Column> getSliceNames(const std::string& key,
const org::apache::cassandra::ColumnParent& col_parent,
org::apache::cassandra::SlicePredicate& pred);
std::vector<org::apache::cassandra::Column> getSliceRange(const std::string& key,
const org::apache::cassandra::ColumnParent& col_parent,
org::apache::cassandra::SlicePredicate& pred,
org::apache::cassandra::ConsistencyLevel::type level);
std::vector<org::apache::cassandra::Column> getSliceRange(const std::string& key,
const org::apache::cassandra::ColumnParent& col_parent,
org::apache::cassandra::SlicePredicate& pred);
std::map<std::string, std::vector<org::apache::cassandra::Column> >
getRangeSlice(const org::apache::cassandra::ColumnParent& col_parent,
const org::apache::cassandra::SlicePredicate& pred,
const std::string& start,
const std::string& finish,
const int32_t row_count,
org::apache::cassandra::ConsistencyLevel::type level);
std::map<std::string, std::vector<org::apache::cassandra::Column> >
getRangeSlice(const org::apache::cassandra::ColumnParent& col_parent,
const org::apache::cassandra::SlicePredicate& pred,
const std::string& start,
const std::string& finish,
const int32_t row_count);
std::map<std::string, std::vector<org::apache::cassandra::SuperColumn> >
getSuperRangeSlice(const org::apache::cassandra::ColumnParent& col_parent,
const org::apache::cassandra::SlicePredicate& pred,
const std::string& start,
const std::string& finish,
const int32_t count,
org::apache::cassandra::ConsistencyLevel::type level);
std::map<std::string, std::vector<org::apache::cassandra::SuperColumn> >
getSuperRangeSlice(const org::apache::cassandra::ColumnParent& col_parent,
const org::apache::cassandra::SlicePredicate& pred,
const std::string& start,
const std::string& finish,
const int32_t count);
/**
* Return a list of slices using the given query object
* @param[in] query object that encapuslates everything needed
* for a query using secondary indexes
* @return a map of row keys to column names and values
*/
std::map<std::string, std::map<std::string, std::string> >
getIndexedSlices(const IndexedSlicesQuery& query);
/**
* @return number of columns in a row or super column
*/
int32_t getCount(const std::string& key,
const org::apache::cassandra::ColumnParent& col_parent,
const org::apache::cassandra::SlicePredicate& pred,
org::apache::cassandra::ConsistencyLevel::type level);
/**
* @return number of columns in a row or super column
*/
int32_t getCount(const std::string& key,
const org::apache::cassandra::ColumnParent& col_parent,
const org::apache::cassandra::SlicePredicate& pred);
/**
* Create a column family
* @param[in] cf_def object representing defintion for column family to create
* @return the schema ID for the keyspace created
*/
std::string createKeyspace(const KeyspaceDefinition& ks_def);
/**
* drop a keyspace
* @param[in] ks_name the name of the keyspace to drop
* @return the schema ID for the keyspace dropped
*/
std::string dropKeyspace(const std::string& ks_name);
/**
* Create a keyspace
* @param[in] ks_def object representing defintion for keyspace to create
* @return the schema ID for the column family created
*/
std::string createColumnFamily(const ColumnFamilyDefinition& cf_def);
/**
* drop a column family
* @param[in] cf_name the name of the column family to drop
* @return the schema ID for the column family dropped
*/
std::string dropColumnFamily(const std::string& cf_name);
/**
* @return the target server cluster name.
*/
std::string getClusterName();
/**
* @return the server version.
*/
std::string getServerVersion();
/**
* @return a string property from the server
*/
void getStringProperty(std::string &return_val, const std::string &property);
/**
* @return hostname
*/
std::string getHost();
/**
* @return port number
*/
int getPort() const;
private:
/**
* Finds the given keyspace in the list of keyspace definitions
* @return true if found; false otherwise
*/
bool findKeyspace(const std::string& name);
org::apache::cassandra::CassandraClient *thrift_client;
std::string host;
int port;
std::string cluster_name;
std::string server_version;
std::string current_keyspace;
std::vector<KeyspaceDefinition> key_spaces;
std::map<std::string, std::string> token_map;
Cassandra(const Cassandra&);
Cassandra &operator=(const Cassandra&);
};
} /* end namespace libcassandra */
#endif /* __LIBCASSANDRA_CASSANDRA_H */