--- trunk/PlusPlus.pm 2004/12/03 15:23:23 3 +++ trunk/PlusPlus.pm 2004/12/03 19:35:02 4 @@ -7,6 +7,7 @@ our $VERSION = '0.02'; use Carp; +use File::Temp qw/ tempdir /; =head1 NAME @@ -121,6 +122,101 @@ } +=head2 index_document + +Quick way to add simple data to index. + + $i->index_document($key, $data); + $i->index_document( 42 => 'meaning of life' ); + +=cut + +sub index_document { + my $self = shift; + + my %doc = @_; + + foreach my $id (keys %doc) { + $self->_create_doc( + path => $id, + body => $doc{$id}, + ); + } + + return 1; +} + +=head1 PRIVATE METHODS + +Private methods implement internals for creating temporary file needed for +swish++. You should have no need to call them directly, and they are here +just to have documentation. + +=head2 _init_index + +Create temporary directory in which files for indexing will be created and +start index process. + + my $i->_init_index || die "can't start indexer"; + +=cut + +sub _init_index { + my $self = shift; + + $self->{'tmp_dir'} = tempdir( CLEANUP => 1 ) || confess "can't create temporary directory: $!"; + + my $opt = "-v 4"; + + my $open_cmd = '| index '.$opt.' -e "html:*" -i '.$self->{'index_dir'}.'/index -'; + + chdir $self->{'tmp_dir'} || confess "can't chdir to ".$self->{'tmp_dir'}.": $!"; + + CORE::open($self->{'index_fh'}, $open_cmd) || confess "can't start index with $open_cmd: $!"; + + return $self->{'index_fh'}; +} + +=head2 _create_doc + +Create temporary file and pass it's name to swish++ + + $i->_create_doc( + path => 'path/to/store/in/index', + body => 'data to story in body tag', + meta => { + 'meta name' => 'data for this meta', + 'another' => 'again more data', + } + ); + +=cut + +sub _create_doc { + my $self = shift; + + my $arg = {@_}; + + # open indexer if needed + $self->{'index_fh'} ||= $self->_init_index; + + my $path = $self->{'tmp_dir'} || confess "no tmp_dir?"; + + CORE::open(TMP, '>', $arg->{'path'}) || die "can't create temp file ".$arg->{'path'}.": $!"; + + print TMP ''; + + if ($arg->{'meta'}) { + confess "not yet implemented"; + } + + print TMP '' . ($arg->{'body'} || '') . ''; + + close(TMP) || confess "can't close tmp file ".$arg->{'path'}.": $!"; + + print { $self->{'index_fh'} } $arg->{'path'}."\n"; +} + 1; __END__