Skip to content
Browse files

Added Counters support

  • Loading branch information...
1 parent 544ecc3 commit cafd939ec6a09507b46cb3a4bd0c71ae4cf6a47d @fmgoncalves committed
Showing with 163 additions and 10 deletions.
  1. +5 −5 README
  2. +56 −0 examples/counter_column_test.pm
  3. +102 −5 lib/Cassandra/Simple.pm
View
10 README
@@ -165,13 +165,13 @@ SYNOPSYS
comparator_type, etc.).
create_index
- Usage: "create_index($keyspace, $column_family, $column,
+ Usage: "create_index($keyspace, $column_family, $columns,
[$validation_class])"
- Creates an index on $column of $column_family.
-
- $validation_class only applies when $column doesn't yet exist, and even
- then it is optional (defaults to *BytesType*).
+ Creates an index on $columns of $column_family. $columns is an ARRAY of
+ column names to be indexed. $validation_class only applies when $column
+ doesn't yet exist, and even then it is optional (defaults to
+ *BytesType*).
ring
Usage: "ring($keyspace)"
View
56 examples/counter_column_test.pm
@@ -0,0 +1,56 @@
+use strict;
+use warnings;
+
+use Data::Dumper;
+
+use Cassandra::Simple;
+use Cassandra::Composite qw/composite composite_to_array/;
+
+use Sys::Hostname qw/hostname/;
+
+sub println {
+ print @_, "\n";
+}
+
+my ( $keyspace, $column_family) = qw/simple simplecounter/;
+
+my $conn = Cassandra::Simple->new( server_name => '127.0.0.1',
+ keyspace => $keyspace, );
+
+my $present =
+ grep { $_ eq $column_family } @{ [ $conn->list_keyspace_cfs($keyspace) ] };
+
+unless ($present) {
+ println "Creating $column_family in $keyspace";
+ $conn->create_column_family( $keyspace, $column_family,
+ {
+ comparator_type => 'UTF8Type',
+ key_validation_class => 'UTF8Type',
+ default_validation_class => 'CounterColumnType',
+ } );
+}
+
+
+#Method to test code here success
+#add 100% 100%
+#remove_counter 100% 100%
+#get 100% 100%
+
+
+println "\$conn->add($column_family, 'ChaveA', 'ColunaA')";
+println Dumper $conn->add($column_family, 'ChaveA', 'ColunaA');
+
+println "\$conn->get($column_family, 'ChaveA', {columns => ['ColunaA']})";
+println Dumper $conn->get($column_family, 'ChaveA', {columns => ['ColunaA']});
+#Expected result: ColunaA -> 1
+
+println "\$conn->add($column_family, 'ChaveA', 'ColunaA')";
+println Dumper $conn->add($column_family, 'ChaveA', 'ColunaA', 10);
+
+println "\$conn->get($column_family, 'ChaveA', {columns => ['ColunaA']})";
+println Dumper $conn->get($column_family, 'ChaveA', {columns => ['ColunaA']});
+#Expected result: ColunaA -> 11
+
+println "\$conn->remove_counter($column_family, 'ChaveA', 'ColunaA')";
+println Dumper $conn->remove_counter($column_family, 'ChaveA', 'ColunaA');
+
View
107 lib/Cassandra/Simple.pm
@@ -142,7 +142,7 @@ sub _column_or_supercolumn_to_hash {
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} ) {
+ } elsif ( exists $c_or_sc->{super_column} and $c_or_sc->{supercolumn} ) {
@result = (
$c_or_sc->{super_column}->{name},
{
@@ -150,6 +150,17 @@ sub _column_or_supercolumn_to_hash {
@{ $c_or_sc->{super_column}->{columns} }
}
);
+ }elsif ( exists $c_or_sc->{counter_column} and $c_or_sc->{counter_column} ) {
+ @result = ( $_->{counter_column}->{name}, $_->{counter_column}->{value} );
+ }
+ elsif ( exists $c_or_sc->{counter_super_column} and $c_or_sc->{counter_super_column} ) {
+ @result = (
+ $c_or_sc->{counter_super_column}->{name},
+ {
+ map { $_->{name} => $_->{value} }
+ @{ $c_or_sc->{counter_super_column}->{columns} }
+ }
+ );
}
return \@result;
@@ -747,6 +758,91 @@ sub batch_insert {
else { $self->pool->put($cl) }
}
+=head2 add
+
+Usage: C<add($column_family, $key, $column, [$value [, opt]])>
+
+Increment or decrement counter C<$column> by C<$value>. C<$value> is 1 by default.
+
+C<$opt> is a I<HASH> and can have the following keys:
+
+=over 2
+
+super_column, consistency_level_write
+
+=back
+
+=cut
+
+sub add {
+ my $self = shift;
+
+ my $column_family = shift;
+ my $key = shift;
+ my $column = shift;
+ my $value = shift || 1;
+ my $opt = shift || {};
+
+ my $level = $self->_consistency_level_write($opt);
+ my $columnParent =
+ Cassandra::ColumnParent->new(
+ {
+ column_family => $column_family,
+ super_column => $opt->{super_column} // undef,
+ }
+ );
+ my $col =
+ Cassandra::CounterColumn->new( { name => $column, value => $value } );
+
+ my $cl = $self->pool->get();
+ my $res = eval { $cl->add( $key, $columnParent, $col, $level ) };
+ if ($@) { $self->pool->fail($cl) }
+ else { $self->pool->put($cl) }
+ return $res;
+}
+
+=head2 remove_counter
+
+Usage: C<remove_counter($column_family, $key, $column [, opt])>
+
+Remove counter C<$column> on C<$key>.
+
+C<$opt> is a I<HASH> and can have the following keys:
+
+=over 2
+
+super_column, consistency_level_write
+
+=back
+
+=cut
+
+sub remove_counter {
+ my $self = shift;
+
+ my $column_family = shift;
+ my $key = shift;
+ my $column = shift;
+ my $opt = shift || {};
+
+ my $level = $self->_consistency_level_write($opt);
+
+ my $columnPath =
+ new Cassandra::ColumnPath(
+ {
+ column_family => $column_family,
+ super_column => $opt->{super_column} // undef,
+ column => $column
+ }
+ );
+
+ my $cl = $self->pool->get();
+ my $res = eval { $cl->remove_counter( $key, $columnPath, $level ) };
+ if ($@) { $self->pool->fail($cl) }
+ else { $self->pool->put($cl) }
+ return $res;
+}
+
=head2 remove
Usage: C<< remove($column_family[, $keys][, opt]) >>
@@ -879,6 +975,7 @@ sub create_index {
my $keyspace = shift;
my $column_family = shift;
my $columns = shift;
+ my $validation_class = shift || 'org.apache.cassandra.db.marshal.BytesType';
if ( !UNIVERSAL::isa( $columns, 'ARRAY' ) ) {
$columns = [$columns];
@@ -905,19 +1002,19 @@ sub create_index {
$newmetadata->{$col} // new Cassandra::ColumnDef(
{
name => $col,
- validation_class => 'org.apache.cassandra.db.marshal.BytesType',
+ validation_class => $validation_class,
}
);
$newmetadata->{$col}->{index_type} = 0;
$newmetadata->{$col}->{index_name} = $col . "_idx";
}
- $cfdef->{column_metadata} = [values %$newmetadata];
+ $cfdef->{column_metadata} = [ values %$newmetadata ];
#print Dumper $cfdef;
my $res = eval { $cl->system_update_column_family($cfdef) };
- if ($@) { print Dumper $@ ;$self->pool->fail($cl) }
- else { $self->pool->put($cl) }
+ if ($@) { print Dumper $@; $self->pool->fail($cl) }
+ else { $self->pool->put($cl) }
return $res;
}

0 comments on commit cafd939

Please sign in to comment.
Something went wrong with that request. Please try again.