Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

allow to setup indexing on a bucket via precommit

add tests for riaksearch
  • Loading branch information...
commit ade1fc13ec613d2318a9c3517bc40837d99f4d99 1 parent 4fe4d90
gmaurice authored
2  lib/Net/Riak.pm
View
@@ -14,7 +14,7 @@ has client => (
is => 'rw',
isa => Client_T,
required => 1,
- handles => [qw/is_alive all_buckets server_info stats search/]
+ handles => [qw/is_alive all_buckets server_info stats search setup_indexing/]
);
sub BUILDARGS {
59 lib/Net/Riak/Role/REST/Search.pm
View
@@ -3,17 +3,18 @@ use Moose::Role;
use JSON;
sub search {
- my ( $self, $params) = @_;
-
+ my $self = shift;
+ my %params = @_;
my $request;
+
$request =
$self->new_request( 'GET',
- [ $self->search_prefix, "select" ], $params ) unless $params->{index};
- if ( $params->{index} ){
- my $index = delete $params->{index};
+ [ $self->search_prefix, "select" ], \%params ) unless $params{index};
+ if ( $params{index} ){
+ my $index = delete $params{index};
$request =
$self->new_request( 'GET',
- [ $self->search_prefix, $index, "select" ], $params );
+ [ $self->search_prefix, $index, "select" ], \%params );
}
my $http_response = $self->send_request($request);
@@ -24,10 +25,50 @@ sub search {
if ($status == 404) {
return;
}
-use YAML::Syck;
-warn Dump $http_response;
- JSON::decode_json($http_response->content);
+
+ return JSON::decode_json($http_response->content) if $params{wt} =~ /json/i;
+ $http_response->content;
};
+sub setup_indexing {
+ my ( $self, $bucket ) = @_;
+ my $request =
+ $self->new_request( 'GET',
+ [ $self->prefix, $bucket ] );
+
+ my $http_response = $self->send_request($request);
+
+ return if (!$http_response);
+ my $status = $http_response->code;
+ if ($status == 404) {
+ return;
+ }
+
+ my $precommits = JSON::decode_json($http_response->content)->{props}->{precommit};
+
+ for (@$precommits){
+ return JSON::decode_json($http_response->content) if $_->{mod} eq "riak_search_kv_hook";
+ }
+ push ( @$precommits, { mod => "riak_search_kv_hook" , fun => "precommit" } );
+
+ $request = $self->new_request( 'PUT', [ $self->prefix, $bucket ] );
+ $request->content( JSON::encode_json({ props => { precommit => $precommits } } ) );
+ $request->header('Content-Type' => "application/json" );
+
+ $http_response = $self->send_request($request);
+
+ return if (!$http_response);
+ $status = $http_response->code;
+ if ($status == 404) {
+ return;
+ }
+ $request =
+ $self->new_request( 'GET',
+ [ $self->prefix, $bucket ] );
+
+ $http_response = $self->send_request($request);
+
+ JSON::decode_json($http_response->content);
+}
1;
7 lib/Net/Riak/Search.pm
View
@@ -1,7 +1,5 @@
package Net::Riak::Search;
-# ABSTRACT: the riaklink object represents a link from one Riak object to another
-
use Moose;
with 'Net::Riak::Role::Base' => {classes =>
@@ -12,4 +10,9 @@ sub search {
$self->client->search($params);
};
+sub setup_indexing {
+ my ($self, $bucket) = @_;
+ $self->client->setup_indexing($bucket);
+};
+
1;
30 t/20_search.t
View
@@ -0,0 +1,30 @@
+use lib 't/lib';
+use Test::More;
+use Test::Riak;
+
+test_riak_rest {
+ my ($client, $bucket_name) = @_;
+ ok $client->setup_indexing($bucket_name), 'setup indexing ok';
+
+ ok my $bucket = $client->bucket($bucket_name), 'got bucket test';
+ my $content = { field => "indexed" };
+
+ ok my $obj = $bucket->new_object(undef, $content),
+ 'created a new riak object without a key';
+ ok $obj->store, 'store object without key';
+ ok $obj->key, 'key created';
+
+ is $client->search(
+ index => $bucket_name,
+ wt => "json",
+ q => "field:indexed")->{response}->{docs}[0]->{id},
+ $obj->key,
+ 'search with index in path';
+
+ is $client->search(
+ wt => "json",
+ q => "$bucket_name.field:indexed")->{response}->{docs}[0]->{id},
+ $obj->key,
+ 'search with index prefixes in query';
+ $obj->delete;
+}
Please sign in to comment.
Something went wrong with that request. Please try again.