/[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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1363 - (show annotations)
Mon Apr 11 17:29:59 2011 UTC (13 years ago) by dpavlin
File size: 2132 byte(s)
use _num suffix for numeric fields
1 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 bucket
12 ));
13
14 use Data::Dump qw/dump/;
15 use URI;
16 use Net::Riak;
17
18 =head1 NAME
19
20 WebPAC::Output::Riak - feed data into Riak Search
21
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 my $bucket = $self->bucket || join('.', $self->database, $self->input || 'webpac2' );
37
38 $self->{_riak} = Net::Riak->new( host => $self->url );
39 $self->{_bucket} = $self->{_riak}->bucket( $bucket );
40 $self->{_bucket}->set_properties({
41 precommit => [ { mod => 'riak_search_kv_hook', fun => 'precommit' } ],
42 });
43
44 $log->info( $self->url,"/riak/$bucket" );
45 # warn dump($self->{_bucket}->get_properties);
46
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 sub add {
62 my ($self,$id,$ds) = @_;
63 my $log = $self->_get_logger;
64
65 # $log->debug( 'ds = ', $id, sub { dump($ds) } );
66
67 my $data;
68 $data->{$_->[0]} = $_->[1] foreach
69 map {
70 my $v = join(' ', @{ $ds->{$_}->{search} });
71 my $k = $_;
72 if ( $v =~ m/^\d+([-\d+]*\d)?$/ ) {
73 $v =~ s/-//g;
74 $v *= 1;
75 # _num suffix for riak search https://wiki.basho.com/display/RIAK/Riak+Search+-+Schema
76 $k .= '_num';
77 }
78 [ $k, $v ]
79 }
80 grep { exists $ds->{$_}->{search} }
81 keys %$ds;
82
83 my $obj = $self->{_bucket}->new_object( $id, $data );
84 $obj->store;
85
86 $log->debug( 'json = ', $id, sub { dump($data) } );
87
88 $self->{_count}++;
89
90 return 1;
91 }
92
93 =head2 finish
94
95 $out->finish;
96
97 =cut
98
99 sub finish {
100 my $self = shift;
101
102 my $log = $self->_get_logger();
103
104 $log->info('finish ', $self->{_count}, ' documents');
105
106 1;
107 }
108
109 =head1 SEE ALSO
110
111 L<https://wiki.basho.com/display/RIAK/Riak+Search>
112
113 =head1 AUTHOR
114
115 Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>
116
117 =head1 COPYRIGHT & LICENSE
118
119 Copyright 2010 Dobrica Pavlinusic, All Rights Reserved.
120
121 This program is free software; you can redistribute it and/or modify it
122 under the same terms as Perl itself.
123
124 =cut
125
126 1; # End of WebPAC::Output::Riak

  ViewVC Help
Powered by ViewVC 1.1.26