forked from posulliv/libcassandra
/
cassandra.cc
767 lines (628 loc) · 21.8 KB
/
cassandra.cc
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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
/*
* 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.
*/
#include <time.h>
#include <netinet/in.h>
#include <string>
#include <set>
#include <sstream>
#include <iostream>
#include "libgenthrift/Cassandra.h"
#include "libcassandra/cassandra.h"
#include "libcassandra/exception.h"
#include "libcassandra/indexed_slices_query.h"
#include "libcassandra/keyspace.h"
#include "libcassandra/keyspace_definition.h"
#include "libcassandra/util_functions.h"
using namespace std;
using namespace org::apache::cassandra;
using namespace libcassandra;
Cassandra::Cassandra()
:
thrift_client(NULL),
host(),
port(0),
cluster_name(),
server_version(),
current_keyspace(),
key_spaces(),
token_map()
{
}
Cassandra::Cassandra(CassandraClient *in_thrift_client,
const string &in_host,
int in_port)
:
thrift_client(in_thrift_client),
host(in_host),
port(in_port),
cluster_name(),
server_version(),
current_keyspace(),
key_spaces(),
token_map()
{}
Cassandra::Cassandra(CassandraClient *in_thrift_client,
const string &in_host,
int in_port,
const string& keyspace)
:
thrift_client(in_thrift_client),
host(in_host),
port(in_port),
cluster_name(),
server_version(),
current_keyspace(keyspace),
key_spaces(),
token_map()
{}
Cassandra::~Cassandra()
{
delete thrift_client;
}
CassandraClient *Cassandra::getCassandra()
{
return thrift_client;
}
void Cassandra::login(const string& user, const string& password)
{
AuthenticationRequest req;
req.credentials["username"]= user;
req.credentials["password"]= password;
thrift_client->login(req);
}
void Cassandra::setKeyspace(const string& ks_name)
{
current_keyspace.assign(ks_name);
thrift_client->set_keyspace(ks_name);
}
string Cassandra::getCurrentKeyspace() const
{
return current_keyspace;
}
void Cassandra::insertColumn(const string& key,
const string& column_family,
const string& super_column_name,
const string& column_name,
const string& value,
ConsistencyLevel::type level,
int32_t ttl= 0)
{
ColumnParent col_parent;
col_parent.column_family.assign(column_family);
if (! super_column_name.empty())
{
col_parent.super_column.assign(super_column_name);
col_parent.__isset.super_column= true;
}
Column col;
col.name.assign(column_name);
col.value.assign(value);
col.timestamp= createTimestamp();
if (ttl)
{
col.ttl=ttl;
col.__isset.ttl=true;
}
if ((int)key.size() ==0){
level = ConsistencyLevel::QUORUM;
}
/*
* actually perform the insert
* TODO - validate the ColumnParent before the insert
*/
thrift_client->insert(key, col_parent, col, level);
}
void Cassandra::insertColumn(const string& key,
const string& column_family,
const string& super_column_name,
const string& column_name,
const string& value)
{
insertColumn(key, column_family, super_column_name, column_name, value, ConsistencyLevel::QUORUM);
}
void Cassandra::insertColumn(const string& key,
const string& column_family,
const string& super_column_name,
const string& column_name,
const int64_t value)
{
insertColumn(key, column_family, super_column_name, column_name, serializeLong(value), ConsistencyLevel::QUORUM);
}
void Cassandra::insertColumn(const string& key,
const string& column_family,
const string& column_name,
const string& value)
{
insertColumn(key, column_family, "", column_name, value, ConsistencyLevel::QUORUM);
}
void Cassandra::insertColumn(const string& key,
const string& column_family,
const string& column_name,
const int64_t value)
{
//int64_t store_value= htonll(value);
insertColumn(key, column_family, "", column_name, serializeLong(value), ConsistencyLevel::QUORUM);
}
void Cassandra::remove(const string &key,
const ColumnPath &col_path,
ConsistencyLevel::type level)
{
thrift_client->remove(key, col_path, createTimestamp(), level);
}
void Cassandra::remove(const string &key,
const ColumnPath &col_path)
{
thrift_client->remove(key, col_path, createTimestamp(), ConsistencyLevel::QUORUM);
}
void Cassandra::remove(const string& key,
const string& column_family,
const string& super_column_name,
const string& column_name)
{
ColumnPath col_path;
col_path.column_family.assign(column_family);
if (! super_column_name.empty())
{
col_path.super_column.assign(super_column_name);
col_path.__isset.super_column= true;
}
if (! column_name.empty())
{
col_path.column.assign(column_name);
col_path.__isset.column= true;
}
remove(key, col_path);
}
void Cassandra::removeColumn(const string& key,
const string& column_family,
const string& super_column_name,
const string& column_name)
{
remove(key, column_family, super_column_name, column_name);
}
void Cassandra::removeSuperColumn(const string& key,
const string& column_family,
const string& super_column_name)
{
remove(key, column_family, super_column_name, "");
}
Column Cassandra::getColumn(const string& key,
const string& column_family,
const string& super_column_name,
const string& column_name,
ConsistencyLevel::type level)
{
ColumnPath col_path;
col_path.column_family.assign(column_family);
if (! super_column_name.empty())
{
col_path.super_column.assign(super_column_name);
col_path.__isset.super_column= true;
}
col_path.column.assign(column_name);
col_path.__isset.column= true;
ColumnOrSuperColumn cosc;
/* TODO - validate column path */
thrift_client->get(cosc, key, col_path, level);
if (cosc.column.name.empty())
{
/* throw an exception */
throw(InvalidRequestException());
}
return cosc.column;
}
Column Cassandra::getColumn(const string& key,
const string& column_family,
const string& super_column_name,
const string& column_name)
{
return getColumn(key, column_family, super_column_name, column_name, ConsistencyLevel::QUORUM);
}
Column Cassandra::getColumn(const string& key,
const string& column_family,
const string& column_name)
{
return getColumn(key, column_family, "", column_name, ConsistencyLevel::QUORUM);
}
string Cassandra::getColumnValue(const string& key,
const string& column_family,
const string& super_column_name,
const string& column_name)
{
return getColumn(key, column_family, super_column_name, column_name).value;
}
string Cassandra::getColumnValue(const string& key,
const string& column_family,
const string& column_name)
{
return getColumn(key, column_family, column_name).value;
}
int64_t Cassandra::getIntegerColumnValue(const string& key,
const string& column_family,
const string& column_name)
{
string ret= getColumn(key, column_family, column_name).value;
return deserializeLong(ret);
}
SuperColumn Cassandra::getSuperColumn(const string& key,
const string& column_family,
const string& super_column_name,
ConsistencyLevel::type level)
{
ColumnPath col_path;
col_path.column_family.assign(column_family);
col_path.super_column.assign(super_column_name);
/* this is ugly but thanks to thrift is needed */
col_path.__isset.super_column= true;
ColumnOrSuperColumn cosc;
/* TODO - validate super column path */
thrift_client->get(cosc, key, col_path, level);
if (cosc.super_column.name.empty())
{
/* throw an exception */
throw(InvalidRequestException());
}
return cosc.super_column;
}
SuperColumn Cassandra::getSuperColumn(const string& key,
const string& column_family,
const string& super_column_name)
{
return getSuperColumn(key, column_family, super_column_name, ConsistencyLevel::QUORUM);
}
vector<Column> Cassandra::getSliceNames(const string& key,
const ColumnParent& col_parent,
SlicePredicate& pred,
ConsistencyLevel::type level)
{
vector<ColumnOrSuperColumn> ret_cosc;
vector<Column> result;
/* damn you thrift! */
// pred.__isset.column_names= true;
thrift_client->get_slice(ret_cosc, key, col_parent, pred, level);
for (vector<ColumnOrSuperColumn>::iterator it= ret_cosc.begin();
it != ret_cosc.end();
++it)
{
if (! (*it).column.name.empty())
{
result.push_back((*it).column);
}
}
return result;
}
vector<Column> Cassandra::getSliceNames(const string& key,
const ColumnParent& col_parent,
SlicePredicate& pred)
{
return getSliceNames(key, col_parent, pred, ConsistencyLevel::QUORUM);
}
vector<Column> Cassandra::getSliceRange(const string& key,
const ColumnParent& col_parent,
SlicePredicate& pred,
ConsistencyLevel::type level)
{
vector<ColumnOrSuperColumn> ret_cosc;
vector<Column> result;
/* damn you thrift! */
pred.__isset.slice_range= true;
thrift_client->get_slice(ret_cosc, key, col_parent, pred, level);
for (vector<ColumnOrSuperColumn>::iterator it= ret_cosc.begin();
it != ret_cosc.end();
++it)
{
if (! (*it).column.name.empty())
{
result.push_back((*it).column);
}
}
return result;
}
vector<Column> Cassandra::getSliceRange(const string& key,
const ColumnParent& col_parent,
SlicePredicate& pred)
{
return getSliceRange(key, col_parent, pred, ConsistencyLevel::QUORUM);
}
vector<pair<string, vector<Column> > >
Cassandra::getRangeSlice(const ColumnParent& col_parent,
const SlicePredicate& pred,
const string& start,
const string& finish,
const int32_t row_count,
ConsistencyLevel::type level)
{
vector<pair<string, vector<Column> > > ret;
vector<KeySlice> key_slices;
KeyRange key_range;
key_range.start_key.assign(start);
key_range.end_key.assign(finish);
key_range.count= row_count;
key_range.__isset.start_key= true;
key_range.__isset.end_key= true;
thrift_client->get_range_slices(key_slices,
col_parent,
pred,
key_range,
level);
if (! key_slices.empty())
{
for (vector<KeySlice>::iterator it= key_slices.begin();
it != key_slices.end();
++it)
{
ret.push_back(make_pair((*it).key, getColumnList((*it).columns)));
}
}
return ret;
}
vector<pair<string, vector<Column> > >
Cassandra::getRangeSlice(const ColumnParent& col_parent,
const SlicePredicate& pred,
const string& start,
const string& finish,
const int32_t row_count)
{
return getRangeSlice(col_parent, pred, start, finish, row_count, ConsistencyLevel::QUORUM);
}
vector<pair<string, vector<SuperColumn> > >
Cassandra::getSuperRangeSlice(const ColumnParent& col_parent,
const SlicePredicate& pred,
const string& start,
const string& finish,
const int32_t row_count,
ConsistencyLevel::type level)
{
vector<pair<string, vector<SuperColumn> > > ret;
vector<KeySlice> key_slices;
KeyRange key_range;
key_range.start_key.assign(start);
key_range.end_key.assign(finish);
key_range.count= row_count;
key_range.__isset.start_key= true;
key_range.__isset.end_key= true;
thrift_client->get_range_slices(key_slices,
col_parent,
pred,
key_range,
level);
if (! key_slices.empty())
{
for (vector<KeySlice>::iterator it= key_slices.begin();
it != key_slices.end();
++it)
{
ret.push_back(make_pair((*it).key, getSuperColumnList((*it).columns)));
}
}
return ret;
}
vector<pair<string, vector<SuperColumn> > >
Cassandra::getSuperRangeSlice(const ColumnParent& col_parent,
const SlicePredicate& pred,
const string& start,
const string& finish,
const int32_t row_count)
{
return getSuperRangeSlice(col_parent, pred, start, finish, row_count, ConsistencyLevel::QUORUM);
}
vector<pair<string, vector<Column> > >
Cassandra::getIndexedSlices(const IndexedSlicesQuery& query)
{
vector<KeySlice> key_slices;
SlicePredicate thrift_slice_pred= createSlicePredicateObject(query);
ColumnParent thrift_col_parent;
thrift_col_parent.column_family.assign(query.getColumnFamily());
thrift_client->get_indexed_slices(key_slices,
thrift_col_parent,
query.getIndexClause(),
thrift_slice_pred,
query.getConsistencyLevel());
vector<pair<string, vector<Column> > > ret;
for(vector<KeySlice>::iterator it= key_slices.begin();
it != key_slices.end();
++it)
{
ret.push_back(make_pair((*it).key, getColumnList((*it).columns)));
}
return ret;
}
int32_t Cassandra::getCount(const string& key,
const ColumnParent& col_parent,
const SlicePredicate& pred,
ConsistencyLevel::type level)
{
return (thrift_client->get_count(key, col_parent, pred, level));
}
int32_t Cassandra::getCount(const string& key,
const ColumnParent& col_parent,
const SlicePredicate& pred)
{
return (getCount(key, col_parent, pred, ConsistencyLevel::QUORUM));
}
vector<KeyspaceDefinition> Cassandra::getKeyspaces()
{
vector<KsDef> thrift_ks_defs;
thrift_client->describe_keyspaces(thrift_ks_defs);
key_spaces.clear();
for (vector<KsDef>::iterator it= thrift_ks_defs.begin();
it != thrift_ks_defs.end();
++it)
{
KsDef thrift_entry= *it;
KeyspaceDefinition entry(thrift_entry.name,
thrift_entry.strategy_class,
thrift_entry.strategy_options,
thrift_entry.replication_factor,
thrift_entry.cf_defs);
key_spaces.push_back(entry);
}
return key_spaces;
}
string Cassandra::createColumnFamily(const ColumnFamilyDefinition& cf_def)
{
string schema_id;
CfDef thrift_cf_def= createCfDefObject(cf_def);
thrift_client->system_add_column_family(schema_id, thrift_cf_def);
return schema_id;
}
string Cassandra::updateColumnFamily(const ColumnFamilyDefinition& cf_def)
{
string schema_id;
CfDef thrift_cf_def= createCfDefObject(cf_def);
thrift_client->system_update_column_family(schema_id, thrift_cf_def);
return schema_id;
}
string Cassandra::dropColumnFamily(const string& cf_name)
{
string schema_id;
thrift_client->system_drop_column_family(schema_id, cf_name);
return schema_id;
}
string Cassandra::createKeyspace(const KeyspaceDefinition& ks_def)
{
string ret;
KsDef thrift_ks_def= createKsDefObject(ks_def);
thrift_client->system_add_keyspace(ret, thrift_ks_def);
return ret;
}
string Cassandra::updateKeyspace(const KeyspaceDefinition& ks_def)
{
string ret;
KsDef thrift_ks_def= createKsDefObject(ks_def);
thrift_client->system_update_keyspace(ret, thrift_ks_def);
return ret;
}
string Cassandra::dropKeyspace(const string& ks_name)
{
string ret;
thrift_client->system_drop_keyspace(ret, ks_name);
return ret;
}
string Cassandra::getClusterName()
{
if (cluster_name.empty())
{
thrift_client->describe_cluster_name(cluster_name);
}
return cluster_name;
}
string Cassandra::getServerVersion()
{
if (server_version.empty())
{
thrift_client->describe_version(server_version);
}
return server_version;
}
string Cassandra::getHost()
{
return host;
}
int Cassandra::getPort() const
{
return port;
}
std::vector<org::apache::cassandra::TokenRange> Cassandra::describeRing(const std::string &keyspace) {
vector<org::apache::cassandra::TokenRange> ret;
thrift_client->describe_ring(ret, keyspace);
return ret;
}
void Cassandra::batchInsert(const std::vector<ColumnInsertTuple> &columns,
const std::vector<SuperColumnInsertTuple> &super_columns,
org::apache::cassandra::ConsistencyLevel::type level) {
MutationsMap mutations;
for (std::vector<ColumnInsertTuple>::const_iterator column = columns.begin();
column != columns.end(); column++) {
addToMap(*column, mutations);
}
for (std::vector<SuperColumnInsertTuple>::const_iterator super_column = super_columns.begin();
super_column != super_columns.end(); super_column++) {
addToMap(*super_column, mutations);
}
thrift_client->batch_mutate(mutations, level);
}
void Cassandra::batchInsert(const std::vector<ColumnInsertTuple> &columns,
const std::vector<SuperColumnInsertTuple> &super_columns) {
batchInsert(columns, super_columns, ConsistencyLevel::QUORUM);
}
void Cassandra::addToMap(const ColumnInsertTuple &tuple, MutationsMap &mutations) {
std::string column_family = std::tr1::get<0>(tuple);
std::string key = std::tr1::get<1>(tuple);
std::string name = std::tr1::get<2>(tuple);
std::string value = std::tr1::get<3>(tuple);
org::apache::cassandra::Mutation mutation;
mutation.column_or_supercolumn.column.name = name;
mutation.column_or_supercolumn.column.value = value;
mutation.column_or_supercolumn.column.timestamp = createTimestamp();
mutation.column_or_supercolumn.__isset.column = true;
mutation.__isset.column_or_supercolumn = true;
if (mutations.find(key) == mutations.end()) {
mutations[key] = std::map<std::string,
std::vector<org::apache::cassandra::Mutation> >();
}
std::map<std::string,
std::vector<org::apache::cassandra::Mutation>
> &mutations_per_cf = mutations[key];
if (mutations_per_cf.find(column_family) == mutations_per_cf.end()) {
mutations_per_cf[column_family] = std::vector<org::apache::cassandra::Mutation>();
}
std::vector<org::apache::cassandra::Mutation> &mutation_list = mutations_per_cf[column_family];
mutation_list.push_back(mutation);
}
void Cassandra::addToMap(const SuperColumnInsertTuple &tuple, MutationsMap &mutations) {
std::string column_family = std::tr1::get<0>(tuple);
std::string key = std::tr1::get<1>(tuple);
std::string super_column = std::tr1::get<2>(tuple);
std::string name = std::tr1::get<3>(tuple);
std::string value = std::tr1::get<4>(tuple);
if (mutations.find(key) == mutations.end()) {
mutations[key] = std::map<std::string, std::vector<org::apache::cassandra::Mutation> >();
}
std::map<std::string,
std::vector<org::apache::cassandra::Mutation>
> &mutations_per_cf = mutations[key];
if (mutations_per_cf.find(column_family) == mutations_per_cf.end()) {
mutations_per_cf[column_family] = std::vector<org::apache::cassandra::Mutation>();
}
std::vector<org::apache::cassandra::Mutation> &mutation_list = mutations_per_cf[column_family];
org::apache::cassandra::Mutation mutation;
//this is awful, is there a better way?
for (std::vector<org::apache::cassandra::Mutation>::iterator it = mutation_list.begin();
it != mutation_list.end(); it++) {
if (it->column_or_supercolumn.__isset.super_column &&
it->column_or_supercolumn.super_column.name == super_column) {
mutation = *it;
mutation_list.erase(it);
break;
}
}
mutation.column_or_supercolumn.super_column.name = super_column;
mutation.column_or_supercolumn.__isset.super_column = true;
mutation.__isset.column_or_supercolumn = true;
Column column;
column.name = name;
column.value = value;
column.timestamp = createTimestamp();
mutation.column_or_supercolumn.super_column.columns.push_back(column);
mutation_list.push_back(mutation);
}
bool Cassandra::findKeyspace(const string& name)
{
for (vector<KeyspaceDefinition>::iterator it= key_spaces.begin();
it != key_spaces.end();
++it)
{
if (name == it->getName())
{
return true;
}
}
return false;
}