/[Search-Estraier]/trunk/lib/Search/Estraier.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /trunk/lib/Search/Estraier.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 111 by dpavlin, Tue Feb 21 15:41:57 2006 UTC revision 135 by dpavlin, Tue May 9 12:42:39 2006 UTC
# Line 4  use 5.008; Line 4  use 5.008;
4  use strict;  use strict;
5  use warnings;  use warnings;
6    
7  our $VERSION = '0.04_3';  our $VERSION = '0.06_1';
8    
9  =head1 NAME  =head1 NAME
10    
# Line 599  sub options { Line 599  sub options {
599  }  }
600    
601    
602    =head2 set_skip
603    
604    Set number of skipped documents from beginning of results
605    
606      $cond->set_skip(42);
607    
608    Similar to C<offset> in RDBMS.
609    
610    =cut
611    
612    sub set_skip {
613            my $self = shift;
614            $self->{skip} = shift;
615    }
616    
617    =head2 skip
618    
619    Return skip for this condition.
620    
621      print $cond->skip;
622    
623    =cut
624    
625    sub skip {
626            my $self = shift;
627            return $self->{skip};
628    }
629    
630    
631  package Search::Estraier::ResultDocument;  package Search::Estraier::ResultDocument;
632    
633  use Carp qw/croak/;  use Carp qw/croak/;
# Line 843  or in more verbose form Line 872  or in more verbose form
872    
873    my $node = new Search::HyperEstraier::Node(    my $node = new Search::HyperEstraier::Node(
874          url => 'http://localhost:1978/node/test',          url => 'http://localhost:1978/node/test',
875            user => 'admin',
876            passwd => 'admin'
877          debug => 1,          debug => 1,
878          croak_on_error => 1          croak_on_error => 1
879    );    );
# Line 855  with following arguments: Line 886  with following arguments:
886    
887  URL to node  URL to node
888    
889    =item user
890    
891    specify username for node server authentication
892    
893    =item passwd
894    
895    password for authentication
896    
897  =item debug  =item debug
898    
899  dumps a B<lot> of debugging output  dumps a B<lot> of debugging output
# Line 885  sub new { Line 924  sub new {
924          if ($#_ == 0) {          if ($#_ == 0) {
925                  $self->{url} = shift;                  $self->{url} = shift;
926          } else {          } else {
                 my $args = {@_};  
   
927                  %$self = ( %$self, @_ );                  %$self = ( %$self, @_ );
928    
929                    $self->set_auth( $self->{user}, $self->{passwd} ) if ($self->{user});
930    
931                  warn "## Node debug on\n" if ($self->{debug});                  warn "## Node debug on\n" if ($self->{debug});
932          }          }
933    
# Line 1360  sub search { Line 1399  sub search {
1399          );          );
1400          return if ($rv != 200);          return if ($rv != 200);
1401    
1402          my (@docs, $hints);          my @records     = split /--------\[.*?\]--------(?::END)?\r?\n/, $resbody;
1403            my $hintsText   = splice @records, 0, 2; # starts with empty record
1404          my @lines = split(/\n/, $resbody);          my $hints               = { $hintsText =~ m/^(.*?)\t(.*?)$/gsm };
1405          return unless (@lines);  
1406            # process records
1407          my $border = $lines[0];          my $docs = [];
1408          my $isend = 0;          foreach my $record (@records)
1409          my $lnum = 1;          {
1410                    # split into keys and snippets
1411          while ( $lnum <= $#lines ) {                  my ($keys, $snippet) = $record =~ m/^(.*?)\n\n(.*?)$/s;
1412                  my $line = $lines[$lnum];  
1413                  $lnum++;                  # create document hash
1414                    my $doc                         = { $keys =~ m/^(.*?)=(.*?)$/gsm };
1415                  #warn "## $line\n";                  $doc->{'@keywords'}     = $doc->{keywords};
1416                  if ($line && $line =~ m/^\Q$border\E(:END)*$/) {                  ($doc->{keywords})      = $keys =~ m/^%VECTOR\t(.*?)$/gm;
1417                          $isend = $1;                  $doc->{snippet}         = $snippet;
1418                          last;  
1419                  }                  push @$docs, new Search::Estraier::ResultDocument(
1420                            attrs           => $doc,
1421                  if ($line =~ /\t/) {                          uri             => $doc->{'@uri'},
1422                          my ($k,$v) = split(/\t/, $line, 2);                          snippet         => $snippet,
1423                          $hints->{$k} = $v;                          keywords        => $doc->{'keywords'},
1424                  }                  );
1425          }          }
1426    
1427          my $snum = $lnum;          return new Search::Estraier::NodeResult( docs => $docs, hints => $hints );
   
         while( ! $isend && $lnum <= $#lines ) {  
                 my $line = $lines[$lnum];  
                 #warn "# $lnum: $line\n";  
                 $lnum++;  
   
                 if ($line && $line =~ m/^\Q$border\E/) {  
                         if ($lnum > $snum) {  
                                 my $rdattrs;  
                                 my $rdvector;  
                                 my $rdsnippet;  
                                   
                                 my $rlnum = $snum;  
                                 while ($rlnum < $lnum - 1 ) {  
                                         #my $rdline = $self->_s($lines[$rlnum]);  
                                         my $rdline = $lines[$rlnum];  
                                         $rlnum++;  
                                         last unless ($rdline);  
                                         if ($rdline =~ /^%/) {  
                                                 $rdvector = $1 if ($rdline =~ /^%VECTOR\t(.+)$/);  
                                         } elsif($rdline =~ /=/) {  
                                                 $rdattrs->{$1} = $2 if ($rdline =~ /^(.+)=(.+)$/);  
                                         } else {  
                                                 confess "invalid format of response";  
                                         }  
                                 }  
                                 while($rlnum < $lnum - 1) {  
                                         my $rdline = $lines[$rlnum];  
                                         $rlnum++;  
                                         $rdsnippet .= "$rdline\n";  
                                 }  
                                 #warn Dumper($rdvector, $rdattrs, $rdsnippet);  
                                 if (my $rduri = $rdattrs->{'@uri'}) {  
                                         push @docs, new Search::Estraier::ResultDocument(  
                                                 uri => $rduri,  
                                                 attrs => $rdattrs,  
                                                 snippet => $rdsnippet,  
                                                 keywords => $rdvector,  
                                         );  
                                 }  
                         }  
                         $snum = $lnum;  
                         #warn "### $line\n";  
                         $isend = 1 if ($line =~ /:END$/);  
                 }  
   
         }  
   
         if (! $isend) {  
                 warn "received result doesn't have :END\n$resbody";  
                 return;  
         }  
   
         #warn Dumper(\@docs, $hints);  
   
         return new Search::Estraier::NodeResult( docs => \@docs, hints => $hints );  
1428  }  }
1429    
1430    
# Line 1490  sub cond_to_query { Line 1473  sub cond_to_query {
1473          push @args, 'wwidth=' . $self->{wwidth};          push @args, 'wwidth=' . $self->{wwidth};
1474          push @args, 'hwidth=' . $self->{hwidth};          push @args, 'hwidth=' . $self->{hwidth};
1475          push @args, 'awidth=' . $self->{awidth};          push @args, 'awidth=' . $self->{awidth};
1476            push @args, 'skip=' . $self->{skip} if ($self->{skip});
1477    
1478          return join('&', @args);          return join('&', @args);
1479  }  }
# Line 1716  sub links { Line 1700  sub links {
1700          return $self->{inform}->{links};          return $self->{inform}->{links};
1701  }  }
1702    
1703    =head2 master
1704    
1705    Set actions on Hyper Estraier node master (C<estmaster> process)
1706    
1707      $node->master(
1708            action => 'sync'
1709      );
1710    
1711    All available actions are documented in
1712    L<http://hyperestraier.sourceforge.net/nguide-en.html#protocol>
1713    
1714    =cut
1715    
1716    my $estmaster_rest = {
1717            shutdown => {
1718                    status => 202,
1719            },
1720            sync => {
1721                    status => 202,
1722            },
1723            backup => {
1724                    status => 202,
1725            },
1726            userlist => {
1727                    status => 200,
1728                    returns => [ qw/name passwd flags fname misc/ ],
1729            },
1730            useradd => {
1731                    required => [ qw/name passwd flags/ ],
1732                    optional => [ qw/fname misc/ ],
1733                    status => 200,
1734            },
1735            userdel => {
1736                    required => [ qw/name/ ],
1737                    status => 200,
1738            },
1739            nodelist => {
1740                    status => 200,
1741                    returns => [ qw/name label doc_num word_num size/ ],
1742            },
1743            nodeadd => {
1744                    required => [ qw/name/ ],
1745                    optional => [ qw/label/ ],
1746                    status => 200,
1747            },
1748            nodedel => {
1749                    required => [ qw/name/ ],
1750                    status => 200,
1751            },
1752            nodeclr => {
1753                    required => [ qw/name/ ],
1754                    status => 200,
1755            },
1756            nodertt => {
1757                    status => 200,  
1758            },
1759    };
1760    
1761    sub master {
1762            my $self = shift;
1763    
1764            my $args = {@_};
1765    
1766            # have action?
1767            my $action = $args->{action} || croak "need action, available: ",
1768                    join(", ",keys %{ $estmaster_rest });
1769    
1770            # check if action is valid
1771            my $rest = $estmaster_rest->{$action};
1772            croak "action '$action' is not supported, available actions: ",
1773                    join(", ",keys %{ $estmaster_rest }) unless ($rest);
1774    
1775            croak "BUG: action '$action' needs return status" unless ($rest->{status});
1776    
1777            my @args;
1778    
1779            if ($rest->{required} || $rest->{optional}) {
1780    
1781                    map {
1782                            croak "need parametar '$_' for action '$action'" unless ($args->{$_});
1783                            push @args, $_ . '=' . uri_escape( $args->{$_} );
1784                    } ( keys %{ $rest->{required} } );
1785    
1786                    map {
1787                            push @args, $_ . '=' . uri_escape( $args->{$_} ) if ($args->{$_});
1788                    } ( keys %{ $rest->{optional} } );
1789    
1790            }
1791    
1792            my $uri = new URI( $self->{url} );
1793    
1794            my $resbody;
1795    
1796            my $status = $self->shuttle_url(
1797                    'http://' . $uri->host_port . '/master?action=' . $action ,
1798                    'application/x-www-form-urlencoded',
1799                    join('&', @args),
1800                    \$resbody,
1801                    1,
1802            ) or confess "shuttle_url failed";
1803    
1804            if ($status == $rest->{status}) {
1805                    if ($rest->{returns} && wantarray) {
1806    
1807                            my @results;
1808                            my $fields = $#{$rest->{returns}};
1809    
1810                            foreach my $line ( split(/[\r\n]/,$resbody) ) {
1811                                    my @e = split(/\t/, $line, $fields + 1);
1812                                    my $row;
1813                                    foreach my $i ( 0 .. $fields) {
1814                                            $row->{ $rest->{returns}->[$i] } = $e[ $i ];
1815                                    }
1816                                    push @results, $row;
1817                            }
1818    
1819                            return @results;
1820    
1821                    } elsif ($resbody) {
1822                            return $resbody;
1823                    } else {
1824                            return 0E0;
1825                    }
1826            }
1827    
1828            carp "expected status $rest->{status}, but got $status";
1829            return undef;
1830    }
1831    
1832  =head1 PRIVATE METHODS  =head1 PRIVATE METHODS
1833    
# Line 1787  Hyper Estraier Ruby interface on which t Line 1899  Hyper Estraier Ruby interface on which t
1899    
1900  Dobrica Pavlinusic, E<lt>dpavlin@rot13.orgE<gt>  Dobrica Pavlinusic, E<lt>dpavlin@rot13.orgE<gt>
1901    
1902    Robert Klep E<lt>robert@klep.nameE<gt> contributed refactored search code
1903    
1904  =head1 COPYRIGHT AND LICENSE  =head1 COPYRIGHT AND LICENSE
1905    

Legend:
Removed from v.111  
changed lines
  Added in v.135

  ViewVC Help
Powered by ViewVC 1.1.26