Permalink
Browse files

Added batch_add call to allow bulk update counters. fixes #8

  • Loading branch information...
1 parent 519527e commit aa56600cee60e1f14efb22d613a28449e37e8b76 Filipe Gonçalves committed Feb 29, 2012
Showing with 71 additions and 2 deletions.
  1. +10 −0 README
  2. +2 −2 examples/counter_column_test.pm
  3. +59 −0 lib/Cassandra/Simple.pm
View
@@ -149,6 +149,16 @@ SYNOPSYS
super_column, consistency_level_write
+ batch_add
+ Usage: "batch_add($column_family, $rows[, opt])"
+
+ $rows is an *HASH* of the form "{ key => { column => value , column =>
+ value }, key => { column => value , column => value } }"
+
+ $opt is an *HASH* and can have the following keys:
+
+ consistency_level_write
+
remove_counter
Usage: "remove_counter($column_family, $key, $column [, opt])"
@@ -52,8 +52,8 @@ print "\$conn->get($column_family, 'ChaveA', {columns => ['ColunaA']})";
print Dumper $conn->get($column_family, 'ChaveA', {columns => ['ColunaA']});
#Expected result: ColunaA -> 1
-print "\$conn->add($column_family, 'ChaveA', 'ColunaA',10)";
-print Dumper $conn->add($column_family, 'ChaveA', 'ColunaA', 10);
+print "\$conn->batch_add($column_family, {'ChaveA'=> {'ColunaA'=> 10}})";
+print Dumper $conn->batch_add($column_family, {'ChaveA'=> {'ColunaA'=> 10}});
print "\$conn->get($column_family, 'ChaveA', {columns => ['ColunaA']})";
print Dumper $conn->get($column_family, 'ChaveA', {columns => ['ColunaA']});
@@ -822,6 +822,65 @@ sub add {
return $res;
}
+=head2 batch_add
+
+Usage: C<batch_add($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
+
+consistency_level_write
+
+=back
+
+=cut
+
+sub batch_add {
+ 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 {
+ Cassandra::Mutation->new(
+ {
+ column_or_supercolumn =>
+ Cassandra::ColumnOrSuperColumn->new(
+ {
+ counter_column =>
+ Cassandra::CounterColumn->new(
+ {
+ name => $_,
+ value => $columns->{$_}
+ }
+ )
+ }
+ )
+ }
+ );
+ } keys %$columns
+ ]
+ }
+ } keys %$rows;
+ my $cl = $self->pool->get();
+ my $res = eval { $cl->batch_mutate( \%mutation_map, $level ); };
+ if ($@) { print Dumper $@; $self->pool->fail($cl) }
+ else { $self->pool->put($cl) }
+ return $res;
+}
+
=head2 remove_counter
Usage: C<remove_counter($column_family, $key, $column [, opt])>

0 comments on commit aa56600

Please sign in to comment.