/
Simple.pm
970 lines (673 loc) · 22.6 KB
/
Simple.pm
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
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
package Cassandra::Simple;
=pod
=encoding utf8
=head1 NAME
Cassandra::Simple
=head1 VERSION
version 0.1
=head1 DESCRIPTION
Easy to use, Perl oriented client interface to Apache Cassandra.
This module attempts to abstract the underlying Thrift methods as much as possible to allow any Perl developer a small learning curve when using Cassandra.
=head1 SYNOPSYS
my ($keyspace, $column_family) = qw/simple simple/;
my $conn = Cassandra::Simple->new(keyspace => $keyspace,);
$conn->create_column_family( $keyspace, $column_family);
$conn->insert($column_family, 'KeyA', [ [ 'ColumnA' => 'AA' ], [ 'ColumnB' => 'AB' ] ] );
$conn->get($column_family, 'KeyA');
$conn->get($column_family, 'KeyA', { columns => [ qw/ColumnA/ ] });
$conn->get($column_family, 'KeyA', { column_count => 1, column_reversed => 1 });
$conn->batch_insert($column_family, { 'KeyB' => [ [ 'ColumnA' => 'BA' ] , [ 'ColumnB' => 'BB' ] ], 'KeyC' => [ [ 'ColumnA' => 'CA' ] , [ 'ColumnD' => 'CD' ] ] });
$conn->multiget($column_family, [qw/KeyA KeyC/]);
$conn->get_range($column_family, { start=> 'KeyA', finish => 'KeyB', column_count => 1 });
$conn->get_range($column_family);
$conn->get_indexed_slices($column_family, { expression_list => [ [ 'ColumnA' => 'BA' ] ] });
$conn->remove($column_family, [ 'KeyA' ], { columns => [ 'ColumnA' ]});
$conn->remove($column_family, [ 'KeyA' ]);
$conn->remove($column_family);
$conn->get_count($column_family, 'KeyA');
$conn->multiget_count($column_family, [ 'KeyB', 'KeyC' ]);
=cut
use strict;
use warnings;
use Data::Dumper;
use Any::Moose;
has 'pool' => ( is => 'rw', isa => 'Cassandra::Pool', lazy_build => 1 );
has 'consistency_level_read' => ( is => 'rw', isa => 'Str', default => 'ONE' );
has 'consistency_level_write' => ( is => 'rw', isa => 'Str', default => 'ONE' );
has 'keyspace' => ( is => 'rw', isa => 'Str' );
has 'password' => ( is => 'rw', isa => 'Str', default => '' );
has 'server_name' => ( is => 'rw', isa => 'Str', default => '127.0.0.1' );
has 'server_port' => ( is => 'rw', isa => 'Int', default => 9160 );
has 'username' => ( is => 'rw', isa => 'Str', default => '' );
use 5.010;
use Cassandra::Cassandra;
use Cassandra::Pool;
use strict;
use warnings;
### Thrift Protocol/Client methods ###
sub _build_pool {
my $self = shift;
#print "BBBBB\n";
return new Cassandra::Pool(
$self->keyspace,
{
server_name => $self->server_name,
server_port => $self->server_port,
username => $self->username,
password => $self->password
}
);
}
sub _consistency_level_read {
my $self = shift;
my $opt = shift // {};
my $level = $opt->{consistency_level_read} // $self->consistency_level_read;
eval "\$level = Cassandra::ConsistencyLevel::$level;";
$level;
}
sub _consistency_level_write {
my $self = shift;
my $opt = shift // {};
my $level = $opt->{consistency_level_write}
// $self->consistency_level_write;
eval "\$level = Cassandra::ConsistencyLevel::$level;";
$level;
}
sub _column_or_supercolumn_to_hash {
my $self = shift;
my $c_or_sc = shift;
my @result;
if ( exists $c_or_sc->{column} and $c_or_sc->{column} ) {
@result = ( $_->{column}->{name}, $_->{column}->{value} );
} elsif ( exists $c_or_sc->{super_column} ) {
@result = (
$c_or_sc->{super_column}->{name},
{
map { $_->{name} => $_->{value} }
@{ $c_or_sc->{super_column}->{columns} }
}
);
}
return \@result;
}
#### API methods ####
=head2 get
Usage: C<get($column_family, $key[, opt])>
C<$opt> is a I<HASH> and can have the following keys:
=over 2
columns, column_start, column_finish, column_count, column_reversed, super_column, consistency_level_read
=back
Returns an HASH of the form C<< { column => value, column => value } >>
=cut
sub get {
my $self = shift;
my $column_family = shift;
my $key = shift;
my $opt = shift // {};
my $columnParent =
Cassandra::ColumnParent->new(
{
column_family => $column_family,
super_column => $opt->{super_column} // undef,
}
);
my $predicate = Cassandra::SlicePredicate->new;
#Cases
#1 - columns -> getslice with slicepredicate only
#2 - column_start and end -> getslice with slicerange
if ( exists $opt->{columns} )
{ #TODO extra case for when only 1 column is requested, use thrift api's get
$predicate->{column_names} = $opt->{columns};
} else {
my $sliceRange = Cassandra::SliceRange->new($opt);
$sliceRange->{start} = $opt->{column_start} // '';
$sliceRange->{finish} = $opt->{column_finish} // '';
$sliceRange->{reversed} = $opt->{column_reversed} // 0;
$sliceRange->{count} = $opt->{column_count} // 100;
$predicate->{slice_range} = $sliceRange;
}
my $level = $self->_consistency_level_read($opt);
my $result =
$self->pool->get()->get_slice( $key, $columnParent, $predicate, $level );
my %result_columns =
map {
my $a = $self->_column_or_supercolumn_to_hash($_);
$a->[0] => $a->[1]
} @{$result};
return \%result_columns;
}
=head2 multiget
Usage: C<< multiget($column_family, $keys[, opt]) >>
C<$keys> should be an I<ARRAYREF> of keys to fetch.
All parameters in C<$opt> are the same as in C<get()>
Returns an HASH of the form C<< { key => { column => value, column => value }, key => { column => value, column => value } } >>
=cut
sub multiget {
my $self = shift;
my $column_family = shift;
my $keys = shift;
my $opt = shift // {};
my $columnParent =
Cassandra::ColumnParent->new( { column_family => $column_family } );
my $predicate = Cassandra::SlicePredicate->new;
#Cases
#1 - columns -> getslice with slicepredicate only
#2 - column_start and end -> getslice with slicerange
if ( exists $opt->{columns} ) {
$predicate->{column_names} = $opt->{columns};
} else {
my $sliceRange = Cassandra::SliceRange->new($opt);
$sliceRange->{start} = $opt->{column_start} // '';
$sliceRange->{finish} = $opt->{column_finish} // '';
$sliceRange->{reversed} = $opt->{column_reversed} // 0;
$sliceRange->{count} = $opt->{column_count} // 100;
$predicate->{slice_range} = $sliceRange;
}
my $level = $self->_consistency_level_read($opt);
my $result =
$self->pool->get()->multiget_slice( $keys, $columnParent, $predicate, $level );
my %result_columns = map {
$_ => { map { $_->{column}->{name} => $_->{column}->{value} }
@{ $result->{$_} } }
} keys %$result;
return \%result_columns;
}
=head2 get_count
Usage: C<< get_count($column_family, $key[, opt]) >>
C<$opt> is a I<HASH> and can have the following keys:
=over 2
columns, column_start, column_finish, super_column, consistency_level_read
=back
Returns the count as an int
=cut
sub get_count {
my $self = shift;
my $column_family = shift;
my $key = shift;
my $opt = shift // {};
my $columnParent =
Cassandra::ColumnParent->new(
{
column_family => $column_family,
super_column => $opt->{super_column} // undef,
}
);
my $predicate = Cassandra::SlicePredicate->new;
#Cases
#1 - columns -> getslice with slicepredicate only
#2 - column_start and end -> getslice with slicerange
if ( exists $opt->{columns} ) {
$predicate->{column_names} = $opt->{columns};
} else {
my $sliceRange = Cassandra::SliceRange->new($opt);
$sliceRange->{start} = $opt->{column_start} // '';
$sliceRange->{finish} = $opt->{column_finish} // '';
$sliceRange->{reversed} = $opt->{column_reversed} // 0;
$sliceRange->{count} = $opt->{column_count} // 100;
$predicate->{slice_range} = $sliceRange;
}
my $level = $self->_consistency_level_read($opt);
return $self->pool->get()->get_count( $key, $columnParent, $predicate, $level );
}
=head2 multiget_count
Usage: C<< multiget_count($column_family, $keys[, opt]) >>
C<$keys> should be an I<ARRAYREF> of keys.
All parameters in C<$opt> are the same as in C<get_count()>
Returns a mapping of C<< key -> count >>
=cut
sub multiget_count {
my $self = shift;
my $column_family = shift;
my $keys = shift;
my $opt = shift // {};
my $columnParent =
Cassandra::ColumnParent->new( { column_family => $column_family } );
my $predicate = Cassandra::SlicePredicate->new;
#Cases
#1 - columns -> getslice with slicepredicate only
#2 - column_start and end -> getslice with slicerange
if ( exists $opt->{columns} ) {
$predicate->{column_names} = $opt->{columns};
} else {
my $sliceRange = Cassandra::SliceRange->new($opt);
$sliceRange->{start} = $opt->{column_start} // '';
$sliceRange->{finish} = $opt->{column_finish} // '';
$sliceRange->{reversed} = $opt->{column_reversed} // 0;
$sliceRange->{count} = $opt->{column_count} // 100;
$predicate->{slice_range} = $sliceRange;
}
my $level = $self->_consistency_level_read($opt);
return $self->pool->get()->multiget_count( $keys, $columnParent, $predicate,
$level );
}
=head2 get_range
Usage: C<get_range( $column_family[, opt])>
C<$opt> is a I<HASH> and can have the following keys:
=over 2
start, finish, columns, column_start, column_finish, column_reversed, column_count, row_count, super_column, consistency_level_read
=back
Returns an I<HASH> of the form C<< { key => { column => value, column => value }, key => { column => value, column => value } } >>
=cut
sub get_range {
my $self = shift;
my $column_family = shift;
my $opt = shift;
my $columnParent =
Cassandra::ColumnParent->new(
{
column_family => $column_family,
super_column => $opt->{super_column} // undef,
}
);
my $predicate = Cassandra::SlicePredicate->new;
#Cases
#1 - columns -> getslice with slicepredicate only
#2 - column_start and end -> getslice with slicerange
if ( exists $opt->{columns} ) {
$predicate->{column_names} = $opt->{columns};
} else {
my $sliceRange = Cassandra::SliceRange->new($opt);
$sliceRange->{start} = $opt->{column_start} // '';
$sliceRange->{finish} = $opt->{column_finish} // '';
$sliceRange->{reversed} = $opt->{column_reversed} // 0;
$sliceRange->{count} = $opt->{column_count} // 100;
$predicate->{slice_range} = $sliceRange;
}
my $keyRange =
Cassandra::KeyRange->new(
{
start_key => $opt->{start} // '',
end_key => $opt->{finish} // '',
count => $opt->{row_count} // 100,
}
);
my $level = $self->_consistency_level_read($opt);
my $result =
$self->pool->get()->get_range_slices( $columnParent, $predicate,
$keyRange, $level );
my %result_columns = map {
$_->{key} => {
map {
my $a = $self->_column_or_supercolumn_to_hash($_);
$a->[0] => $a->[1]
} @{ $_->{columns} }
}
} @{$result};
return \%result_columns;
}
=head2 get_indexed_slices
Usage: C<get_indexed_slices($column_family, $index_clause[, opt])>
C<$index_clause> is an I<HASH> containing the following keys:
=over 2
expression_list, start_key, row_count
The I<expression_list> is an I<ARRAYREF> of the form C<< [ [ column => value ] ] >>
=back
C<$opt> is an I<HASH> and can have the following keys:
=over 2
columns, column_start, column_finish, column_reversed, column_count, consistency_level_read
=back
Returns an I<HASH> of the form C<< { key => { column => value, column => value }, key => { column => value, column => value } } >>
=cut
sub get_indexed_slices {
my $self = shift;
my $column_family = shift;
my $index_clause = shift;
my $opt = shift // {};
my $expr_list;
my $predicate_args;
my $columnParent =
Cassandra::ColumnParent->new( { column_family => $column_family } );
my @index_expr = map {
Cassandra::IndexExpression->new(
{
column_name => $_->[0],
op => Cassandra::IndexOperator::EQ,
value => $_->[1]
}
);
} @{ $index_clause->{'expression_list'} };
my $index_clause_thrift =
Cassandra::IndexClause->new(
{
expressions => \@index_expr,
start_key => $index_clause->{start_key} // '',
count => $index_clause->{row_count} // 100,
}
);
my $predicate = Cassandra::SlicePredicate->new;
#Cases
#1 - columns -> getslice with slicepredicate only
#2 - column_start and end -> getslice with slicerange
if ( exists $opt->{columns} ) {
$predicate->{column_names} = $opt->{columns};
} else {
my $sliceRange = Cassandra::SliceRange->new($opt);
$sliceRange->{start} = $opt->{column_start} // '';
$sliceRange->{finish} = $opt->{column_finish} // '';
$sliceRange->{reversed} = $opt->{column_reversed} // 0;
$sliceRange->{count} = $opt->{column_count} // 100;
$predicate->{slice_range} = $sliceRange;
}
my $level = $self->_consistency_level_read($opt);
my $result =
$self->pool->get()->get_indexed_slices( $columnParent, $index_clause_thrift,
$predicate, $level );
my %result_keys = map {
$_->{key} => { map { $_->{column}->{name} => $_->{column}->{value} }
@{ $_->{columns} } }
} @{$result};
return \%result_keys;
}
=head2 insert
Usage: C<< insert($column_family, $key, $columns[, opt]) >>
The C<$columns> is an I<HASHREF> of the form C<< { column => value, column => value } >>
C<$opt> is an I<HASH> and can have the following keys:
=over 2
timestamp, ttl, consistency_level_write
=back
=cut
sub insert {
my $self = shift;
my $column_family = shift;
my $key = shift;
my $columns = shift;
my $opt = shift // {};
my $columnParent =
Cassandra::ColumnParent->new( { column_family => $column_family } );
my $level = $self->_consistency_level_write($opt);
my @mutations = map {
new Cassandra::Mutation(
{
column_or_supercolumn =>
Cassandra::ColumnOrSuperColumn->new(
{
column =>
new Cassandra::Column(
{
name => $_,
value => $columns->{$_},
timestamp => $opt->{timestamp} // time,
ttl => $opt->{ttl} // undef,
}
)
}
)
}
)
} keys %$columns;
$self->pool->get()->batch_mutate( { $key => { $column_family => \@mutations } },
$level );
}
=head2 insert_super
Usage: C<< insert_super($column_family, $key, $columns[, opt]) >>
The C<$columns> is an I<HASH> of the form C<< { super_column => { column => value, column => value } } >>
C<$opt> is an I<HASH> and can have the following keys:
=over 2
timestamp, ttl, consistency_level_write
=back
=cut
sub insert_super {
my $self = shift;
my $column_family = shift;
my $key = shift;
my $columns = shift;
my $opt = shift // {};
my $columnParent =
Cassandra::ColumnParent->new( { column_family => $column_family } );
my $level = $self->_consistency_level_write($opt);
my @mutations = map {
my $arg = $_;
new Cassandra::Mutation(
{
column_or_supercolumn => Cassandra::ColumnOrSuperColumn->new(
{
super_column => Cassandra::SuperColumn->new(
{
name => $_,
columns => [
map {
new Cassandra::Column(
{
name => $_,
value => $columns->{$arg}->{$_},
timestamp => $opt->{timestamp}
// time,
ttl => $opt->{ttl} // undef,
}
)
} keys %{ $columns->{$arg} }
],
}
)
}
)
}
)
} keys %$columns;
$self->pool->get()->batch_mutate( { $key => { $column_family => \@mutations } },
$level );
}
=head2 batch_insert
Usage: C<batch_insert($column_family, $rows[, opt])>
C<$rows> is an I<HASH> of the form C<< { key => { column => value , column => value }, key => { column => value , column => value } } >>
C<$opt> is an I<HASH> and can have the following keys:
=over 2
timestamp, ttl, consistency_level_write
=back
=cut
sub batch_insert {
my $self = shift;
my $column_family = shift;
my $rows = shift;
my $opt = shift // {};
my $columnParent =
Cassandra::ColumnParent->new( { column_family => $column_family } );
my $level = $self->_consistency_level_write($opt);
my %mutation_map = map {
my $columns = $rows->{$_};
$_ => {
$column_family => [
map {
new Cassandra::Mutation(
{
column_or_supercolumn =>
Cassandra::ColumnOrSuperColumn->new(
{
column =>
new Cassandra::Column(
{
name => $_,
value => $columns->{$_},
timestamp => $opt->{timestamp} // time,
ttl => $opt->{ttl} // undef,
}
)
}
)
}
);
} keys %$columns
]
}
} keys %$rows;
$self->pool->get()->batch_mutate( \%mutation_map, $level );
}
=head2 remove
Usage: C<< remove($column_family[, $keys][, opt]) >>
C<$keys> is a key or an I<ARRAY> of keys to be deleted.
A removal whitout keys truncates the whole column_family.
C<$opt> is an I<HASH> and can have the following keys:
=over 2
columns, super_column, write_consistency_level
=back
The timestamp used for remove is returned.
=cut
sub remove {
my $self = shift;
my $column_family = shift;
my $keys = shift;
my $opt = shift // {};
my $timestamp = time;
my $level = $self->_consistency_level_write($opt);
if ($keys) {
$keys = [$keys] unless ref($keys) eq 'ARRAY';
my $deletion =
Cassandra::Deletion->new(
{
timestamp => $timestamp,
super_column => $opt->{super_column} // undef,
}
);
if ( exists $opt->{columns} ) {
$deletion->{predicate} = Cassandra::SlicePredicate->new(
{ column_names => $opt->{columns} } );
}
# else {#Unsupported by Cassandra yet
# my $predicate = Cassandra::SlicePredicate->new;
# my $sliceRange = Cassandra::SliceRange->new($opt);
# $sliceRange->{start} = $opt->{column_start} // '';
# $sliceRange->{finish} = $opt->{column_finish} // '';
# $sliceRange->{reversed} = $opt->{column_reversed} // 0;
# $sliceRange->{count} = $opt->{column_count} // 100;
# $predicate->{slice_range} = $sliceRange;
# $deletion->{predicate} = $predicate;
# }
my %mutation_map = map {
$_ => { $column_family =>
[ new Cassandra::Mutation( { deletion => $deletion, } ) ] }
} @{$keys};
$self->pool->get()->batch_mutate( \%mutation_map, $level );
return $timestamp;
} else {
$self->pool->get()->truncate($column_family);
}
}
=head2 list_keyspace_cfs
Usage: C<< list_keyspace_cfs($keyspace) >>
Returns an HASH of C<< { column_family_name => column_family_type } >> where column family type is either C<Standard> or C<Super>
=cut
sub list_keyspace_cfs {
my ( $self, $keyspace ) = @_;
my $result = $self->pool->get()->describe_keyspace($keyspace);
return map { $_->{name} => $_->{column_type} } @{ $result->{cf_defs} };
}
=head2 create_column_family
Usage C<< create_column_family($keyspace, $column_family[, $is_super][, $comment]) >>
C<$is_super> is a boolean indicating if this is a Standard or Super Column Family.
=cut
sub create_column_family {
my $self = shift;
my $keyspace = shift;
my $column_family = shift;
my $is_super = shift // 0;
my $comment = shift // undef;
my $cfdef = Cassandra::CfDef->new();
$cfdef->{name} = $column_family;
$cfdef->{keyspace} = $keyspace;
$cfdef->{comment} = $comment;
$cfdef->{column_type} = $is_super ? 'Super' : 'Standard';
$self->pool->get()->system_add_column_family($cfdef);
}
=head2 create_index
Usage: C<< create_index($keyspace, $column_family, $column, [$validation_class]) >>
Creates an index on C<$column> of C<$column_family>.
C<$validation_class> only applies when C<$column> doesn't yet exist, and even then it is optional (defaults to I<BytesType>).
=cut
sub create_index {
my $self = shift;
my $keyspace = shift;
my $column_family = shift;
my $column = shift;
#get column family definition, substitute the target column with itself but indexed.
my $cfdef =
[ grep { $_->{name} eq $column_family }
@{ $self->pool->get()->describe_keyspace($keyspace)->{cf_defs} } ]->[0];
my $cdef;
if ( @{ $cfdef->{column_metadata} }
and grep { $_->{name} eq $column } @{ $cfdef->{column_metadata} } )
{
$cdef =
[ grep { $_->{name} eq $column } @{ $cfdef->{column_metadata} } ]
->[0];
} else {
$cdef = new Cassandra::ColumnDef(
{
name => $column,
validation_class => 'org.apache.cassandra.db.marshal.BytesType',
}
);
}
$cdef->{index_type} = 0;
$cdef->{index_name} = $column . "_idx";
$cfdef->{column_metadata} =
[ grep { $_->{name} ne $column } @{ $cfdef->{column_metadata} } ];
push @{ $cfdef->{column_metadata} }, $cdef;
#print Dumper $cfdef;
$self->pool->get()->system_update_column_family($cfdef);
}
=head2 ring
Usage: C<< ring($keyspace) >>
Lists the addresses of all nodes on the cluster associated with the keyspace C<<$keyspace>>.
=cut
sub ring {
my $self = shift;
my $keyspace = shift;
return
map { $_->{endpoints}->[0] } @{ $self->pool->get()->describe_ring($keyspace) };
}
=head1 BUGS
Bugs should be reported on github at L<https://github.com/fmgoncalves/p5-cassandra-simple>.
=cut
#TODO TODOs
=head1 TODO
B<Unit Tests>
=over 2
Sort of done in the examples folder
L<https://github.com/fmgoncalves/p5-cassandra-simple/tree/master/examples>
=back
B<Tombstones>
get, get_range and get_indexed_slices should probably filter out tombstones, even if it means returning less than the requested count.
Ideally it would retry until it got enough results.
B<Methods>
The following are Thrift methods left unimplemented.
Not all of these will be implemented, since some aren't useful to the common developer.
Priority will be given to live schema updating methods.
=over 2
describe_cluster_name
string describe_cluster_name()
describe_keyspace
KsDef describe_keyspace(string keyspace)
describe_keyspaces
list<KsDef> describe_keyspaces()
describe_partitioner
string describe_partitioner()
describe_snitch
string describe_snitch()
describe_version
string describe_version()
system_drop_column_family
string system_drop_column_family(ColumnFamily column_family)
system_add_keyspace
string system_add_keyspace(KSDef ks_def)
system_drop_keyspace
string system_drop_keyspace(string keyspace)
=back
=cut
=head1 ACKNOWLEDGEMENTS
Implementation based on Cassandra::Lite.
=over 2
L<http://search.cpan.org/~gslin/Cassandra-Lite-0.0.4/lib/Cassandra/Lite.pm>
=back
API based on Pycassa.
=over 2
L<http://pycassa.github.com/pycassa/>
=back
=cut
=head1 AUTHOR
Filipe Gonçalves C<< <the.wa.syndrome@gmail> >>
=cut
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2011 by Filipe Gonçalves.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
=cut
1;