summary refs log tree commit diff
path: root/lib/Net/Riak/Role/REST/MapReduce.pm
diff options
context:
space:
mode:
authorRobin Edwards <robin.ge@gmail.com>2011-04-20 14:38:43 +0100
committerRobin Edwards <robin.ge@gmail.com>2011-04-20 14:38:43 +0100
commit79bea382fd2c0753ca9ace79a11bb74c9a1d722b (patch)
treebde42a47792a27e0a863ee527b88c8c24258f7e9 /lib/Net/Riak/Role/REST/MapReduce.pm
parentMerge remote branch 'simon/fix_link_encoding' (diff)
downloadnet-riak-79bea382fd2c0753ca9ace79a11bb74c9a1d722b.tar.gz
merged pbc branch to master
Diffstat (limited to '')
-rw-r--r--lib/Net/Riak/Role/REST/MapReduce.pm40
1 files changed, 40 insertions, 0 deletions
diff --git a/lib/Net/Riak/Role/REST/MapReduce.pm b/lib/Net/Riak/Role/REST/MapReduce.pm
new file mode 100644
index 0000000..e987a21
--- /dev/null
+++ b/lib/Net/Riak/Role/REST/MapReduce.pm
@@ -0,0 +1,40 @@
+package Net::Riak::Role::REST::MapReduce;
+use Moose::Role;
+use JSON;
+use Data::Dumper;
+
+sub execute_job {
+    my ($self, $job, $timeout) = @_;
+
+    # save existing timeout value.
+    my $ua_timeout = $self->useragent->timeout();
+
+    if ($timeout) {
+        if ($ua_timeout < ($timeout/1000)) {
+            $self->useragent->timeout(int($timeout/1000));
+        }
+        $job->{timeout} = $timeout;
+    }
+
+    my $content = JSON::encode_json($job);
+
+    my $request = $self->new_request(
+        'POST', [$self->mapred_prefix]
+    );
+    $request->content($content);
+
+    my $response = $self->send_request($request);
+
+    # restore time out value
+    if ( $timeout && ( $ua_timeout != $self->useragent->timeout() ) ) {
+        $self->useragent->timeout($ua_timeout);
+    }
+
+    unless ($response->is_success) {
+        die "MapReduce query failed: ".$response->status_line;
+    }
+
+    return JSON::decode_json($response->content);
+}
+
+1;