--- lib/CouchDB/Estraier.pm 2008/08/08 19:20:13 11 +++ lib/CouchDB/Estraier.pm 2008/08/09 16:10:05 12 @@ -9,6 +9,8 @@ use Data::Dump qw/dump/; use Getopt::Long; use JSON; +#use IO::Handle; +use IO::File; =head1 NAME @@ -20,12 +22,29 @@ our $c = { node_url => 'http://localhost:1978/node/', - debug => 0, estuser => 'admin', estpasswd => 'admin', - quiet => 0, + debug => 0, }; +my $log = $0; +$log =~ s{^.*/([^/]+)(\.\w+)$}{/tmp/$1.log} or die "can't generate log name from $0"; + +warn "$0 log $log\n"; + +GetOptions($c, qw/node_url=s debug+ estuser=s estpasswd=s dbuser=s dbpasswd=s/) or die $!; + +#my $log = IO::Handle->new; +#my $log = IO::Handle->new_from_fd(\*STDERR, 'w'); + +$log = IO::File->new( "> $log" ) || die "can't open $log: $!"; + +$log->autoflush( 1 ); +$log->print("c: ", dump($c), "\n" ); # if $c->{debug}; + +my $in = IO::Handle->new_from_fd(\*STDIN, 'r'); +my $out = IO::Handle->new_from_fd(\*STDOUT, 'w'); +$out->autoflush( 1 ); =head2 run_search @@ -35,47 +54,52 @@ =cut -GetOptions($c, qw/node_url=s debug+ quiet+ estuser=s estpasswd=s dbuser=s dbpasswd=s/) or die $!; -warn "# c: ", dump($c) if ($c->{debug}); - -open(my $log, '>', '/tmp/couchdb-estraier.log'); - sub run_search { while ( 1 ) { - my $database = ; - die unless defined $database; - my $query_string = ; - chomp $database; - chomp $query_string; - print $log "run_search $database\t$query_string\n"; - search( $database, $query_string ); + $log->print("search ready\n"); + my $json = $in->getline; + die unless defined $json; + $log->print( "run_search $json\n" ); + $out->print( + encode_json( + search( decode_json( $json ) ) + ) ,"\n" + ); } } sub run_update { while ( 1 ) { - my $database = ; - die unless defined $database; - my $json = ; - chomp $database; - chomp $json; - print $log "run_update $database\t$json\n"; - add( $database, from_json( $json ) ); + $log->print("update ready\n"); + my $json = $in->getline; + die unless defined $json; + $log->print( "run_update $json\n" ); + update( decode_json( $json ) ) } } -=head2 add +=head2 update - CouchDB::Estraier::add( $database, $data ); + CouchDB::Estraier::update( { db => $database, type => $type } ); =cut -sub add { - my ( $database, $data ) = @_; +sub update { + my $args = shift or die "no args"; + $log->print( "add ",dump( $args ),"\n" ); + + my $ret = { + code => 200, + json => { + args => $args, + }, + }; - print $log "add $database ",dump( $data ),"\n"; - return; + return $ret; + + my $database = $args->{db} or die "no db in ",dump( $args ); + my $data = $args->{data} or die "no data in ",dump( $args ); # create and configure node my $node = new Search::Estraier::Node( @@ -98,7 +122,7 @@ while (my ($col,$val) = each %{$data}) { - if ($val) { + if ( defined $val ) { # add attributes (make column usable from attribute search) $doc->add_attr($col, $val); @@ -108,10 +132,11 @@ } - warn "# doc draft: ",$doc->dump_draft, "\n" if ($c->{debug} >= 2); + $log->print("doc draft: ",$doc->dump_draft ) if ($c->{debug} >= 2); die "error: ", $node->status,"\n" unless (eval { $node->put_doc($doc) }); + return $ret; } =head2 search @@ -123,7 +148,10 @@ =cut sub search { - my ( $database, $query ) = @_; + my $args = shift or die "no args"; + $log->print( "search ",dump( $args ),"\n" ); + my $database = $args->{db} or die "no db in ",dump( $args ); + my $query = $args->{query} or die "no query in ",dump( $args ); # create and configure node my $node = new Search::Estraier::Node( @@ -131,7 +159,7 @@ user => $c->{estuser}, passwd => $c->{estpasswd}, croak_on_error => 1, - create => 1, +# create => 1, debug => $c->{debug} >= 4 ? 1 : 0, ); @@ -141,18 +169,17 @@ if ( defined($nres) ) { - print "ok\n"; + $out->print( "ok\n" ); for my $i ( 0 ... $nres->doc_num - 1 ) { my $rdoc = $nres->get_doc($i); print $rdoc->attr('@uri'),"\n",$i,"\n"; } - print "\n\n"; + $out->print( "\n\n" ); } else { - print "error\n", $node->status, "\n"; + $out->print( "error\n", $node->status, "\n" ); } - }