Skip to content

Commit

Permalink
Updated create index to allow multiple index creation.
Browse files Browse the repository at this point in the history
  • Loading branch information
fmgoncalves committed Oct 17, 2011
1 parent 5cdb72d commit 544ecc3
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 54 deletions.
7 changes: 6 additions & 1 deletion examples/cassandra_simple_test.pm
Expand Up @@ -22,7 +22,12 @@ my $present =


unless ($present) { unless ($present) {
println "Creating $column_family in $keyspace"; println "Creating $column_family in $keyspace";
$conn->create_column_family( $keyspace, $column_family ); $conn->create_column_family( $keyspace, $column_family,
{
comparator_type => 'UTF8Type',
key_validation_class => 'UTF8Type',
default_validation_class => 'UTF8Type',
} );
} }


$present = $present =
Expand Down
104 changes: 51 additions & 53 deletions lib/Cassandra/Simple.pm
Expand Up @@ -195,10 +195,7 @@ sub get {


if ( exists $opt->{columns} ) if ( exists $opt->{columns} )
{ #TODO extra case for when only 1 column is requested, use thrift api's get { #TODO extra case for when only 1 column is requested, use thrift api's get
$predicate->{column_names} = [ $predicate->{column_names} = [ map { $_ } @{ $opt->{columns} } ];
map {$_
} @{ $opt->{columns} }
];
} else { } else {
my $sliceRange = Cassandra::SliceRange->new($opt); my $sliceRange = Cassandra::SliceRange->new($opt);
$sliceRange->{start} = $opt->{column_start} // ''; $sliceRange->{start} = $opt->{column_start} // '';
Expand Down Expand Up @@ -579,6 +576,7 @@ timestamp, ttl, consistency_level_write
=back =back
=cut =cut

sub insert { sub insert {
my $self = shift; my $self = shift;


Expand All @@ -593,22 +591,22 @@ sub insert {


my @mutations = map { my @mutations = map {
new Cassandra::Mutation( new Cassandra::Mutation(
{ {
column_or_supercolumn => column_or_supercolumn =>
Cassandra::ColumnOrSuperColumn->new( Cassandra::ColumnOrSuperColumn->new(
{ {
column => column =>
new Cassandra::Column( new Cassandra::Column(
{ {
name => $_, name => $_,
value => $columns->{$_}, value => $columns->{$_},
timestamp => $opt->{timestamp} // time, timestamp => $opt->{timestamp} // time,
ttl => $opt->{ttl} // undef, ttl => $opt->{ttl} // undef,
} }
) )
} }
) )
} }
) )
} keys %$columns; } keys %$columns;


Expand Down Expand Up @@ -726,15 +724,15 @@ sub batch_insert {
column_or_supercolumn => column_or_supercolumn =>
Cassandra::ColumnOrSuperColumn->new( Cassandra::ColumnOrSuperColumn->new(
{ {
column => new Cassandra::Column( column =>
new Cassandra::Column(
{ {
name => name => $_,
$_,
value => $columns->{$_}, value => $columns->{$_},
timestamp => $opt->{timestamp} // time, timestamp => $opt->{timestamp} // time,
ttl => $opt->{ttl} // undef, ttl => $opt->{ttl} // undef,
} }
) )
} }
) )
} }
Expand Down Expand Up @@ -835,7 +833,7 @@ Returns an HASH of C<< { column_family_name => column_family_type } >> where col


sub list_keyspace_cfs { sub list_keyspace_cfs {
my $self = shift; my $self = shift;
return keys % {$self->ksdef}; return keys %{ $self->ksdef };
} }


=head2 create_column_family =head2 create_column_family
Expand All @@ -853,24 +851,24 @@ sub create_column_family {
my $keyspace = shift; my $keyspace = shift;
my $column_family = shift; my $column_family = shift;
my $opt = shift // {}; my $opt = shift // {};

$opt->{name} = $column_family; $opt->{name} = $column_family;
$opt->{keyspace} = $keyspace; $opt->{keyspace} = $keyspace;

my $cfdef = Cassandra::CfDef->new($opt); my $cfdef = Cassandra::CfDef->new($opt);
print Dumper $cfdef; print Dumper $cfdef;
my $cl = $self->pool->get(); my $cl = $self->pool->get();
print Dumper $cl->system_add_column_family($cfdef); print Dumper $cl->system_add_column_family($cfdef);
if ($@) { $self->pool->fail($cl) } if ($@) { $self->pool->fail($cl) }
else { $self->pool->put($cl) } else { $self->pool->put($cl) }
} }


=head2 create_index =head2 create_index
Usage: C<< create_index($keyspace, $column_family, $column, [$validation_class]) >> Usage: C<< create_index($keyspace, $column_family, $columns, [$validation_class]) >>
Creates an index on C<$column> of C<$column_family>.
Creates an index on C<$columns> of C<$column_family>.
C<$columns> is an ARRAY of column names to be indexed.
C<$validation_class> only applies when C<$column> doesn't yet exist, and even then it is optional (defaults to I<BytesType>). C<$validation_class> only applies when C<$column> doesn't yet exist, and even then it is optional (defaults to I<BytesType>).
=cut =cut
Expand All @@ -880,7 +878,11 @@ sub create_index {


my $keyspace = shift; my $keyspace = shift;
my $column_family = shift; my $column_family = shift;
my $column = shift; my $columns = shift;

if ( !UNIVERSAL::isa( $columns, 'ARRAY' ) ) {
$columns = [$columns];
}


#get column family definition, substitute the target column with itself but indexed. #get column family definition, substitute the target column with itself but indexed.


Expand All @@ -895,32 +897,28 @@ sub create_index {
die 'Cassandra Request Failed ' . $@; die 'Cassandra Request Failed ' . $@;
} }


my $cdef; my $newmetadata =
if ( @{ $cfdef->{column_metadata} } { map { $_->{name} => $_ } @{ $cfdef->{column_metadata} } };
and grep { $_->{name} eq $column } @{ $cfdef->{column_metadata} } )
{ foreach my $col ( @{$columns} ) {
$cdef = $newmetadata->{$col} =
[ grep { $_->{name} eq $column } @{ $cfdef->{column_metadata} } ] $newmetadata->{$col} // new Cassandra::ColumnDef(
->[0];
} else {
$cdef = new Cassandra::ColumnDef(
{ {
name => $column, name => $col,
validation_class => 'org.apache.cassandra.db.marshal.BytesType', validation_class => 'org.apache.cassandra.db.marshal.BytesType',
} }
); );
$newmetadata->{$col}->{index_type} = 0;
$newmetadata->{$col}->{index_name} = $col . "_idx";
} }
$cdef->{index_type} = 0;
$cdef->{index_name} = $column . "_idx";


$cfdef->{column_metadata} = $cfdef->{column_metadata} = [values %$newmetadata];
[ grep { $_->{name} ne $column } @{ $cfdef->{column_metadata} } ];
push @{ $cfdef->{column_metadata} }, $cdef;


#print Dumper $cfdef; #print Dumper $cfdef;
eval { $cl->system_update_column_family($cfdef) }; my $res = eval { $cl->system_update_column_family($cfdef) };
if ($@) { $self->pool->fail($cl) } if ($@) { print Dumper $@ ;$self->pool->fail($cl) }
else { $self->pool->put($cl) } else { $self->pool->put($cl) }
return $res;
} }


=head2 ring =head2 ring
Expand Down

0 comments on commit 544ecc3

Please sign in to comment.