1 |
dpavlin |
1196 |
package WebPAC::Output::CouchDB; |
2 |
|
|
|
3 |
|
|
use warnings; |
4 |
|
|
use strict; |
5 |
|
|
|
6 |
|
|
use base qw/WebPAC::Common WebPAC::Output Class::Accessor/; |
7 |
|
|
__PACKAGE__->mk_accessors(qw( |
8 |
dpavlin |
1197 |
input |
9 |
|
|
url |
10 |
dpavlin |
1196 |
database |
11 |
|
|
)); |
12 |
|
|
|
13 |
|
|
use Data::Dump qw/dump/; |
14 |
|
|
use Net::CouchDb; |
15 |
dpavlin |
1197 |
use URI; |
16 |
dpavlin |
1196 |
|
17 |
|
|
=head1 NAME |
18 |
|
|
|
19 |
|
|
WebPAC::Output::CouchDB - feed data into CouchDB |
20 |
|
|
|
21 |
|
|
=head1 FUNCTIONS |
22 |
|
|
|
23 |
|
|
=head2 init |
24 |
|
|
|
25 |
|
|
$out->init; |
26 |
|
|
|
27 |
|
|
=cut |
28 |
|
|
|
29 |
|
|
sub init { |
30 |
|
|
my $self = shift; |
31 |
|
|
my $log = $self->_get_logger; |
32 |
|
|
|
33 |
|
|
$log->debug('init'); |
34 |
|
|
|
35 |
dpavlin |
1197 |
my $u = URI->new( $self->url ); |
36 |
|
|
|
37 |
dpavlin |
1196 |
my $cdb = Net::CouchDb->new( |
38 |
dpavlin |
1197 |
host => $u->host || "localhost", |
39 |
|
|
port => $u->port || 5984, |
40 |
dpavlin |
1196 |
); |
41 |
|
|
|
42 |
|
|
my $database = $self->database || 'webpac2'; |
43 |
|
|
|
44 |
dpavlin |
1197 |
$log->info("CouchDB database $database info ", dump( $cdb->server_info ) ); |
45 |
|
|
|
46 |
dpavlin |
1196 |
$cdb->debug( $self->debug ); |
47 |
|
|
eval { $cdb->delete_db( $self->database ) }; |
48 |
|
|
$cdb->create_db( $self->database ); |
49 |
|
|
$self->{_cdb} = $cdb->db( $self->database ); |
50 |
|
|
|
51 |
|
|
return 1; |
52 |
|
|
} |
53 |
|
|
|
54 |
|
|
|
55 |
|
|
=head2 add |
56 |
|
|
|
57 |
|
|
Adds one entry to database. |
58 |
|
|
|
59 |
|
|
$out->add( 42, $ds ); |
60 |
|
|
|
61 |
|
|
=cut |
62 |
|
|
|
63 |
dpavlin |
1198 |
sub add_row { |
64 |
dpavlin |
1196 |
my $self = shift; |
65 |
|
|
|
66 |
|
|
my ( $id, $ds ) = @_; |
67 |
|
|
|
68 |
|
|
my $log = $self->_get_logger; |
69 |
|
|
|
70 |
dpavlin |
1197 |
$id = $self->input . '-' . $id if $self->input; |
71 |
|
|
|
72 |
dpavlin |
1196 |
my $doc = Net::CouchDb::Document->new( $id, $ds ); |
73 |
|
|
$self->{_cdb}->put( $doc ); |
74 |
|
|
|
75 |
|
|
return 1; |
76 |
|
|
} |
77 |
|
|
|
78 |
|
|
=head2 finish |
79 |
|
|
|
80 |
|
|
$out->finish; |
81 |
|
|
|
82 |
|
|
=cut |
83 |
|
|
|
84 |
|
|
sub finish { |
85 |
|
|
my $self = shift; |
86 |
|
|
|
87 |
|
|
my $log = $self->_get_logger(); |
88 |
|
|
|
89 |
|
|
$log->info('finish'); |
90 |
|
|
1; |
91 |
|
|
|
92 |
|
|
} |
93 |
|
|
|
94 |
|
|
=head1 AUTHOR |
95 |
|
|
|
96 |
|
|
Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >> |
97 |
|
|
|
98 |
|
|
=head1 COPYRIGHT & LICENSE |
99 |
|
|
|
100 |
|
|
Copyright 2009 Dobrica Pavlinusic, All Rights Reserved. |
101 |
|
|
|
102 |
|
|
This program is free software; you can redistribute it and/or modify it |
103 |
|
|
under the same terms as Perl itself. |
104 |
|
|
|
105 |
|
|
=cut |
106 |
|
|
|
107 |
|
|
1; # End of WebPAC::Output::CouchDB |