1 |
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 |
input |
9 |
url |
10 |
database |
11 |
)); |
12 |
|
13 |
use Data::Dump qw/dump/; |
14 |
use Net::CouchDb; |
15 |
use URI; |
16 |
|
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 |
my $u = URI->new( $self->url ); |
36 |
|
37 |
my $cdb = Net::CouchDb->new( |
38 |
host => $u->host || "localhost", |
39 |
port => $u->port || 5984, |
40 |
); |
41 |
|
42 |
my $database = $self->database || 'webpac2'; |
43 |
|
44 |
$log->info("CouchDB database $database info ", dump( $cdb->server_info ) ); |
45 |
|
46 |
$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 |
sub add_row { |
64 |
my $self = shift; |
65 |
|
66 |
my ( $id, $ds ) = @_; |
67 |
|
68 |
my $log = $self->_get_logger; |
69 |
|
70 |
$id = $self->input . '-' . $id if $self->input; |
71 |
|
72 |
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 |