summary refs log tree commit diff
path: root/lib/AnyEvent/Riak.pm
blob: f1bf60b36f7392f4a8154e714d318f01d9f36a7c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
package AnyEvent::Riak;

use strict;
use warnings;
use Carp;
use URI;
use JSON;
use AnyEvent;
use AnyEvent::HTTP;
use MIME::Base64;
use YAML::Syck;

our $VERSION = '0.02';

sub new {
    my ( $class, %args ) = @_;

    my $host        = delete $args{host}        || 'http://127.0.0.1:8098';
    my $path        = delete $args{path}        || 'riak';
    my $mapred_path = delete $args{mapred_path} || 'mapred';
    my $r           = delete $args{r}           || 2;
    my $d           = delete $args{w}           || 2;
    my $dw          = delete $args{dw}          || 2;

    my $client_id
        = "perl_anyevent_riak_" . encode_base64( int( rand(10737411824) ) );

    bless {
        host        => $host,
        path        => $path,
        mapred_path => $mapred_path,
        client_id   => $client_id,
        r           => $r,
        d           => $d,
        dw          => $dw,
        %args,
    }, $class;
}

sub _build_uri {
    my ( $self, $path, $query ) = @_;
    my $uri = URI->new( $self->{host} );
    $uri->path(  join( "/", @$path ) );
    $uri->query_form(%$query) if $query;
    return $uri->as_string;
}

sub _build_headers {
    my ( $self, $options) = @_;
    my $headers = {};
    $headers = {
        'Content-Type'    => 'application/json',
        'X-Riak-ClientId' => $self->{client_id},
    };
    return $headers;
}

sub _init_callback {
    my $self = shift;
    $self->all_cv->begin();

    my ( $cv, $cb );
    if (@_) {
        $cv = pop if UNIVERSAL::isa( $_[-1], 'AnyEvent::CondVar' );
        $cb = pop if ref $_[-1] eq 'CODE';
    }
    $cv ||= AE::cv;

    $cv->cb(
        sub {
            my $cv  = shift;
            my $res = $cv->recv;
            $cb->($res);
        }
    ) if $cb;

    return ( $cv, $cb );
}

sub all_cv {
    my $self = shift;
    $self->{all_cv} = AE::cv unless $self->{all_cv};
    return $self->{all_cv};
}

sub default_cb {
    my ( $self, $options ) = @_;
    return sub {
        my ( $body, $headers ) = @_;
        my $status = 200;
        if ( $headers->{Status} == $status ) {
            if ( $options->{json} ) {
                return JSON::decode_json( $_[0] );
            }
            else {
                return $_[0];
            }
        }else{
            return 1;
        }
    };
}

sub is_alive {
    my $self = shift;

    my ( $cv, $cb ) = $self->_init_callback(@_);
    $cb = $self->default_cb( { json => 0 } ) if !$cb;

    http_request(
        GET => $self->_build_uri( [qw/ping/] ),
        headers => { 'Content-Type' => 'application/json', },
        sub {
            $cv->send( $cb->(@_) );
        },
    );
    return $cv;
}

sub list_bucket {
    my $self        = shift;
    my $bucket_name = shift;
    my $options     = shift;

    my ( $cv, $cb ) = $self->_init_callback(@_);
    $cb = $self->default_cb( { json => 1 } ) if !$cb;

    http_request(
        GET => $self->_build_uri( [ $self->{path}, $bucket_name ] ),
        headers => { 'Content-Type' => 'application/json', },
        sub {
            $cv->send( $cb->(@_) );
        }
    );
    return $cv;
}

sub set_bucket {
    my $self   = shift;
    my $bucket = shift;
    my $schema = shift;

    my ( $cv, $cb ) = $self->_init_callback(@_);
    $cb = $self->default_cb( { json => 1 } ) if !$cb;

    http_request(
        PUT => $self->_build_uri( [ $self->{path}, 'bucket' ] ),
        headers => { 'Content-Type' => 'application/json' },
        body    => JSON::encode_json($schema),
        sub {
            $cv->send( $cb->(@_) );
        }
    );
    $cv;
}

sub fetch {
    my $self   = shift;
    my $bucket = shift;
    my $key    = shift;
    my $r      = shift;

    $r = $self->{r} if !$r;

    my ( $cv, $cb ) = $self->_init_callback(@_);
    $cb = $self->default_cb( { json => 1 } ) if !$cb;
    http_request(
        GET => $self->_build_uri( [ $self->{path}, $bucket, $key ] ),
        headers => { 'Content-Type' => 'application/json' },
        sub {
            $cv->send( $cb->(@_) );
        }
    );
    $cv;
}

sub store {
    my $self   = shift;
    my $object = shift;
    my $w      = shift;
    my $dw     = shift;

    $w  = $self->{w}  if !$w;
    $dw = $self->{dw} if !$dw;

    my $bucket = $object->{bucket};
    my $key    = $object->{key};
    $object->{links} = [] if !exists $object->{links};

    my ( $cv, $cb ) = $self->_init_callback(@_);
    $cb = $self->default_cb( { json => 1 } ) if !$cb;

    http_request(
        POST => $self->_build_uri( [ $self->{path}, $bucket, $key ] ),
        headers => { 'Content-Type' => 'application/json' },
        body    => JSON::encode_json($object),
        sub {
            $cv->send( $cb->(@_) );
        }
    );
    $cv;
}

# sub delete {
#     my ( $self, $bucket, $key, $rw ) = @_;

#     $rw = $self->{rw} || 2 if !$rw;
#     return $self->_request( 'DELETE',
#         $self->_build_uri( [ $bucket, $key ], { dw => $rw } ), 204 );
# }

# sub walk {
#     my ( $self, $bucket, $key, $spec ) = @_;
#     my $path = $self->_build_uri( [ $bucket, $key ] );
#     $path .= $self->_build_spec($spec);
#     return $self->_request( 'GET', $path, 200 );
# }

# sub _build_spec {
#     my ( $self, $spec ) = @_;
#     my $acc = '/';
#     foreach my $item (@$spec) {
#         $acc
#             .= ( $item->{bucket} || '_' ) . ','
#             . ( $item->{tag} || '_' ) . ','
#             . ( $item->{acc} || '_' ) . '/';
#     }
#     return $acc;
# }


# sub _build_query {
#     my ($self, $options) = @_;
# }

# sub _request {
#     my ( $self, $method, $uri, $expected, $body ) = @_;
#     my $cv = AnyEvent->condvar;
#     my $cb = sub {
#         my ( $body, $headers ) = @_;
#         if ( $headers->{Status} == $expected ) {
#             if ( $body && $headers->{'content-type'} eq 'application/json' ) {
#                 return $cv->send( decode_json($body) );
#             }
#             else {
#                 return $cv->send(1);
#             }
#         }
#         else {
#             return $cv->croak(
#                 encode_json( [ $headers->{Status}, $headers->{Reason} ] ) );
#         }
#     };

#     if ($body) {
#         http_request(
#             $method => $uri,
#             headers => { 'Content-Type' => 'application/json', },
#             body    => $body,
#             $cb
#         );
#     }
#     else {
#         http_request(
#             $method => $uri,
#             headers => { 'Content-Type' => 'application/json', },
#             $cb
#         );
#     }
#     $cv;
# }

sub head {
}

sub get {
}

sub put {
}

sub post {
}

1;
__END__

=head1 NAME

AnyEvent::Riak - Non-blocking Riak client

=head1 SYNOPSIS

  use AnyEvent::Riak;

  my $riak = AnyEvent::Riak->new(
    host => 'http://127.0.0.1:8098',
    path => 'riak',
  );

  if ( $riak->is_alive->recv ) {
    my $buckets = $riak->list_bucket('bucketname')->recv;
    my $new_bucket
        = $riak->set_bucket( 'foo', { allowed_fields => '*' } )->recv;
    my $store
        = $riak->store(
        { bucket => 'foo', key => 'bar', object => { baz => 1 }, link => [] }
        )->recv;
    my $fetch = $riak->fetch( 'foo', 'bar' )->recv;
    my $delete = $riak->delete( 'foo', 'bar' )->recv;
  }

=head1 DESCRIPTION

AnyEvent::Riak is a non-blocking riak client using C<AnyEvent>. This client allows you to connect to a Riak instance, create, modify and delete Riak objects.

=head2 METHODS

=over 4

=item B<is_alive>

Check if the Riak server is alive.

=item B<list_bucket>

Get the schema and key list for 'bucket'

    $riak->list_bucket('bucketname')->recv;

=item B<set_bucket>

Set the schema for 'bucket'. The schema parameter must be a hash with at least
an 'allowed_fields' field. Other valid fields are 'requried_fields',
'read_mask', and 'write_mask'.

    $riak->new_bucket('bucketname', {allowed_fields => '*'})->recv;

=item B<fetch>

Get the object stored in 'bucket' at 'key'.

    $riak->fetch('bucketname', 'key')->recv;

=item B<store>

Store 'object' in Riak. If the object has not defined its 'key' field, a key
will be chosen for it by the server.

    $riak->store({
        bucket => 'bucketname',
        key    => 'key',
        object => { foo => "bar", baz => 2 },
        links  => [],
    })->recv;

=item B<delete>

Delete the data stored in 'bucket' at 'key'.

    $riak->delete('bucketname', 'key')->recv;

=item B<walk>

Follow links from the object stored in 'bucket' at 'key' to other objects.
The 'spec' parameter should be an array of hashes, each hash optinally
defining 'bucket', 'tag', and 'acc' fields.  If a field is not defined in a
spec hash, the wildcard '_' will be used instead.

    ok $res = $jiak->walk(
        'bucketname',
        'key',
        [ { bucket => 'bucketname', key => '_' } ]
    )->recv;

=back

=head1 AUTHOR

franck cuny E<lt>franck@lumberjaph.netE<gt>

=head1 SEE ALSO

=head1 LICENSE

Copyright 2009 by linkfluence.

L<http://linkfluence.net>

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.

=cut