package BackupPC::Search::Estraier;
use warnings;
use strict;

use Search::Estraier;

my $debug = $ENV{DEBUG} || 0;

sub new {
	my ( $class, $index_node_url ) = @_;

	warn "# using $index_node_url";

	my $self = bless {
		node => Search::Estraier::Node->new(
			url => $index_node_url,
			user => 'admin',
			passwd => 'admin',
			croak_on_error => 1,
		),
	}, $class;
	return $self;
}

sub node { $_[0]->{node} };

sub exists {
	my ( $self, $row ) = @_;

	my $uri = $row->{hname} . ':' . $row->{sname} . '#' . $row->{backupnum} . ' ' . $row->{filepath};
	my $id = $self->node->uri_to_id($uri);
	return $id && $id != -1;
}

sub add_doc {
	my ( $self, $row ) = @_;

	# create a document object 
	my $doc = Search::Estraier::Document->new;

	my $uri = $row->{hname} . ':' . $row->{sname} . '#' . $row->{backupnum} . ' ' . $row->{filepath};
	# add attributes to the document object 
	$doc->add_attr('@uri', $uri);

	foreach my $c (keys %$row) {
		print STDERR "attr $c = $row->{$c}\n" if ($debug > 2);
		$doc->add_attr($c, $row->{$c}) if defined($row->{$c});
	}

	#$doc->add_attr('@cdate', fmt_date($row->{'date'}));

	# add the body text to the document object 
	my $path = $row->{'filepath'};
	$doc->add_text($path);
	$path =~ s/(.)/$1 /g;
	$doc->add_hidden_text($path);

	print STDERR $doc->dump_draft,"\n" if ($debug > 1);

	# register the document object to the database
	$self->node->put_doc($doc);

}

sub commit {
	my $self = shift;
	warn "# commit not needed";
}

1;
