package WebPAC::Output::Riak;

use warnings;
use strict;

use base qw/WebPAC::Common WebPAC::Output Class::Accessor/;
__PACKAGE__->mk_accessors(qw(
	input
	url
	database
));

use Data::Dump qw/dump/;
use URI;
use Net::Riak;

=head1 NAME

WebPAC::Output::Riak - feed data into Riak

=head1 FUNCTIONS

=head2 init

  $out->init;

=cut

sub init {
	my $self = shift;
	my $log = $self->_get_logger;

	$log->debug('init');

	$self->{_riak} = Net::Riak->new( host => $self->url );
	$self->{_bucket} = $self->{_riak}->bucket( $self->database );

	$log->info("Riak ",$self->url, " bucket ", $self->database);

	$self->{_count} = 0;

	return 1;
}


=head2 add

Adds one entry to database.

  $out->add( 42, $ds );

=cut

sub add_row {
	my $self = shift;

	my ( $id, $ds ) = @_;

	my $log = $self->_get_logger;

	my $obj = $self->{_bucket}->new_object( $id, $ds );
	$obj->store;

	$log->debug( $id, sub { dump($ds) } );

	$self->{_count}++;

	return 1;
}

=head2 finish

 $out->finish;

=cut

sub finish {
	my $self = shift;

	my $log = $self->_get_logger();

	$log->info('finish ', $self->{_count}, ' documents');

	1;
}

=head1 AUTHOR

Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>

=head1 COPYRIGHT & LICENSE

Copyright 2010 Dobrica Pavlinusic, All Rights Reserved.

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

=cut

1; # End of WebPAC::Output::Riak
