/[webpac2]/trunk/lib/WebPAC/Output/Riak.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Annotation of /trunk/lib/WebPAC/Output/Riak.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1357 - (hide annotations)
Sat Nov 13 15:29:18 2010 UTC (13 years, 6 months ago) by dpavlin
File size: 1949 byte(s)
finish riak search support

1 dpavlin 1354 package WebPAC::Output::Riak;
2    
3     use warnings;
4     use strict;
5    
6     use base qw/WebPAC::Common WebPAC::Output Class::Accessor/;
7     __PACKAGE__->mk_accessors(qw(
8     input
9     url
10     database
11 dpavlin 1357 bucket
12 dpavlin 1354 ));
13    
14     use Data::Dump qw/dump/;
15     use URI;
16     use Net::Riak;
17    
18     =head1 NAME
19    
20 dpavlin 1357 WebPAC::Output::Riak - feed data into Riak Search
21 dpavlin 1354
22     =head1 FUNCTIONS
23    
24     =head2 init
25    
26     $out->init;
27    
28     =cut
29    
30     sub init {
31     my $self = shift;
32     my $log = $self->_get_logger;
33    
34     $log->debug('init');
35    
36 dpavlin 1357 my $bucket = $self->bucket || join('.', $self->database, $self->input || 'webpac2' );
37    
38 dpavlin 1354 $self->{_riak} = Net::Riak->new( host => $self->url );
39 dpavlin 1357 $self->{_bucket} = $self->{_riak}->bucket( $bucket );
40     $self->{_bucket}->set_properties({
41     precommit => [ { mod => 'riak_search_kv_hook', fun => 'precommit' } ],
42     });
43 dpavlin 1354
44 dpavlin 1357 $log->info( $self->url,"/riak/$bucket" );
45     # warn dump($self->{_bucket}->get_properties);
46 dpavlin 1354
47     $self->{_count} = 0;
48    
49     return 1;
50     }
51    
52    
53     =head2 add
54    
55     Adds one entry to database.
56    
57     $out->add( 42, $ds );
58    
59     =cut
60    
61 dpavlin 1357 sub add {
62     my ($self,$id,$ds) = @_;
63     my $log = $self->_get_logger;
64 dpavlin 1354
65 dpavlin 1357 # $log->debug( 'ds = ', $id, sub { dump($ds) } );
66 dpavlin 1354
67 dpavlin 1357 my $data;
68     $data->{$_} = join(' ', @{ $ds->{$_}->{search} }) foreach
69     map { m/^\d+$/ ? $_ = $_ . '_num' : $_ }
70     # add _num suffix for riak search https://wiki.basho.com/display/RIAK/Riak+Search+-+Schema
71     grep { exists $ds->{$_}->{search} }
72     keys %$ds;
73 dpavlin 1354
74 dpavlin 1357 my $obj = $self->{_bucket}->new_object( $id, $data );
75 dpavlin 1354 $obj->store;
76    
77 dpavlin 1357 $log->debug( 'json = ', $id, sub { dump($data) } );
78 dpavlin 1354
79     $self->{_count}++;
80    
81     return 1;
82     }
83    
84     =head2 finish
85    
86     $out->finish;
87    
88     =cut
89    
90     sub finish {
91     my $self = shift;
92    
93     my $log = $self->_get_logger();
94    
95     $log->info('finish ', $self->{_count}, ' documents');
96    
97     1;
98     }
99    
100     =head1 AUTHOR
101    
102     Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>
103    
104     =head1 COPYRIGHT & LICENSE
105    
106     Copyright 2010 Dobrica Pavlinusic, All Rights Reserved.
107    
108     This program is free software; you can redistribute it and/or modify it
109     under the same terms as Perl itself.
110    
111     =cut
112    
113     1; # End of WebPAC::Output::Riak

  ViewVC Help
Powered by ViewVC 1.1.26