/[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 150 by dpavlin, Mon May 15 22:26:08 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.07_1';
8    
9  =head1 NAME  =head1 NAME
10    
# Line 20  Search::Estraier - pure perl module to u Line 20  Search::Estraier - pure perl module to u
20          my $node = new Search::Estraier::Node(          my $node = new Search::Estraier::Node(
21                  url => 'http://localhost:1978/node/test',                  url => 'http://localhost:1978/node/test',
22                  user => 'admin',                  user => 'admin',
23                  passwd => 'admin'                  passwd => 'admin',
24                    create => 1,
25                    label => 'Label for node',
26                    croak_on_error => 1,
27          );          );
28    
29          # create document          # create document
# Line 599  sub options { Line 602  sub options {
602  }  }
603    
604    
605    =head2 set_skip
606    
607    Set number of skipped documents from beginning of results
608    
609      $cond->set_skip(42);
610    
611    Similar to C<offset> in RDBMS.
612    
613    =cut
614    
615    sub set_skip {
616            my $self = shift;
617            $self->{skip} = shift;
618    }
619    
620    =head2 skip
621    
622    Return skip for this condition.
623    
624      print $cond->skip;
625    
626    =cut
627    
628    sub skip {
629            my $self = shift;
630            return $self->{skip};
631    }
632    
633    
634  package Search::Estraier::ResultDocument;  package Search::Estraier::ResultDocument;
635    
636  use Carp qw/croak/;  use Carp qw/croak/;
# Line 843  or in more verbose form Line 875  or in more verbose form
875    
876    my $node = new Search::HyperEstraier::Node(    my $node = new Search::HyperEstraier::Node(
877          url => 'http://localhost:1978/node/test',          url => 'http://localhost:1978/node/test',
878            user => 'admin',
879            passwd => 'admin'
880            create => 1,
881            label => 'optional node label',
882          debug => 1,          debug => 1,
883          croak_on_error => 1          croak_on_error => 1
884    );    );
# Line 855  with following arguments: Line 891  with following arguments:
891    
892  URL to node  URL to node
893    
894    =item user
895    
896    specify username for node server authentication
897    
898    =item passwd
899    
900    password for authentication
901    
902    =item create
903    
904    create node if it doesn't exists
905    
906    =item label
907    
908    optional label for new node if C<create> is used
909    
910  =item debug  =item debug
911    
912  dumps a B<lot> of debugging output  dumps a B<lot> of debugging output
# Line 885  sub new { Line 937  sub new {
937          if ($#_ == 0) {          if ($#_ == 0) {
938                  $self->{url} = shift;                  $self->{url} = shift;
939          } else {          } else {
                 my $args = {@_};  
   
940                  %$self = ( %$self, @_ );                  %$self = ( %$self, @_ );
941    
942                    $self->set_auth( $self->{user}, $self->{passwd} ) if ($self->{user});
943    
944                  warn "## Node debug on\n" if ($self->{debug});                  warn "## Node debug on\n" if ($self->{debug});
945          }          }
946    
# Line 898  sub new { Line 950  sub new {
950                  size => -1.0,                  size => -1.0,
951          };          };
952    
953            if ($self->{create}) {
954                    if (! eval { $self->name } || $@) {
955                            my $name = $1 if ($self->{url} =~ m#/node/([^/]+)/*#);
956                            croak "can't find node name in '$self->{url}'" unless ($name);
957                            my $label = $self->{label} || $name;
958                            $self->master(
959                                    action => 'nodeadd',
960                                    name => $name,
961                                    label => $label,
962                            ) || croak "can't create node $name ($label)";
963                    }
964            }
965    
966          $self ? return $self : return undef;          $self ? return $self : return undef;
967  }  }
968    
# Line 1019  sub out_doc { Line 1084  sub out_doc {
1084          my $id = shift || return;          my $id = shift || return;
1085          return unless ($self->{url});          return unless ($self->{url});
1086          croak "id must be number, not '$id'" unless ($id =~ m/^\d+$/);          croak "id must be number, not '$id'" unless ($id =~ m/^\d+$/);
1087          $self->shuttle_url( $self->{url} . '/out_doc',          if ($self->shuttle_url( $self->{url} . '/out_doc',
1088                  'application/x-www-form-urlencoded',                  'application/x-www-form-urlencoded',
1089                  "id=$id",                  "id=$id",
1090                  undef                  undef
1091          ) == 200;          ) == 200) {
1092                    $self->_set_info;
1093                    return $id;
1094            }
1095            return undef;
1096  }  }
1097    
1098    
# Line 1041  sub out_doc_by_uri { Line 1110  sub out_doc_by_uri {
1110          my $self = shift;          my $self = shift;
1111          my $uri = shift || return;          my $uri = shift || return;
1112          return unless ($self->{url});          return unless ($self->{url});
1113          $self->shuttle_url( $self->{url} . '/out_doc',          if ($self->shuttle_url( $self->{url} . '/out_doc',
1114                  'application/x-www-form-urlencoded',                  'application/x-www-form-urlencoded',
1115                  "uri=" . uri_escape($uri),                  "uri=" . uri_escape($uri),
1116                  undef                  undef
1117          ) == 200;          ) == 200) {
1118                    $self->_set_info;
1119                    return $uri;
1120            }
1121            return undef;
1122  }  }
1123    
1124    
# Line 1360  sub search { Line 1433  sub search {
1433          );          );
1434          return if ($rv != 200);          return if ($rv != 200);
1435    
1436          my (@docs, $hints);          my @records     = split /--------\[.*?\]--------(?::END)?\r?\n/, $resbody;
1437            my $hintsText   = splice @records, 0, 2; # starts with empty record
1438          my @lines = split(/\n/, $resbody);          my $hints               = { $hintsText =~ m/^(.*?)\t(.*?)$/gsm };
1439          return unless (@lines);  
1440            # process records
1441          my $border = $lines[0];          my $docs = [];
1442          my $isend = 0;          foreach my $record (@records)
1443          my $lnum = 1;          {
1444                    # split into keys and snippets
1445          while ( $lnum <= $#lines ) {                  my ($keys, $snippet) = $record =~ m/^(.*?)\n\n(.*?)$/s;
1446                  my $line = $lines[$lnum];  
1447                  $lnum++;                  # create document hash
1448                    my $doc                         = { $keys =~ m/^(.*?)=(.*?)$/gsm };
1449                  #warn "## $line\n";                  $doc->{'@keywords'}     = $doc->{keywords};
1450                  if ($line && $line =~ m/^\Q$border\E(:END)*$/) {                  ($doc->{keywords})      = $keys =~ m/^%VECTOR\t(.*?)$/gm;
1451                          $isend = $1;                  $doc->{snippet}         = $snippet;
1452                          last;  
1453                  }                  push @$docs, new Search::Estraier::ResultDocument(
1454                            attrs           => $doc,
1455                  if ($line =~ /\t/) {                          uri             => $doc->{'@uri'},
1456                          my ($k,$v) = split(/\t/, $line, 2);                          snippet         => $snippet,
1457                          $hints->{$k} = $v;                          keywords        => $doc->{'keywords'},
1458                  }                  );
         }  
   
         my $snum = $lnum;  
   
         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$/);  
                 }  
   
1459          }          }
1460    
1461          if (! $isend) {          return new Search::Estraier::NodeResult( docs => $docs, hints => $hints );
                 warn "received result doesn't have :END\n$resbody";  
                 return;  
         }  
   
         #warn Dumper(\@docs, $hints);  
   
         return new Search::Estraier::NodeResult( docs => \@docs, hints => $hints );  
1462  }  }
1463    
1464    
# Line 1490  sub cond_to_query { Line 1507  sub cond_to_query {
1507          push @args, 'wwidth=' . $self->{wwidth};          push @args, 'wwidth=' . $self->{wwidth};
1508          push @args, 'hwidth=' . $self->{hwidth};          push @args, 'hwidth=' . $self->{hwidth};
1509          push @args, 'awidth=' . $self->{awidth};          push @args, 'awidth=' . $self->{awidth};
1510            push @args, 'skip=' . $self->{skip} if ($self->{skip});
1511    
1512          return join('&', @args);          return join('&', @args);
1513  }  }
# Line 1672  sub set_link { Line 1690  sub set_link {
1690                  $self->_set_info;                  $self->_set_info;
1691                  return 1;                  return 1;
1692          }          }
1693            return undef;
1694  }  }
1695    
1696  =head2 admins  =head2 admins
# Line 1716  sub links { Line 1735  sub links {
1735          return $self->{inform}->{links};          return $self->{inform}->{links};
1736  }  }
1737    
1738    =head2 master
1739    
1740    Set actions on Hyper Estraier node master (C<estmaster> process)
1741    
1742      $node->master(
1743            action => 'sync'
1744      );
1745    
1746    All available actions are documented in
1747    L<http://hyperestraier.sourceforge.net/nguide-en.html#protocol>
1748    
1749    =cut
1750    
1751    my $estmaster_rest = {
1752            shutdown => {
1753                    status => 202,
1754            },
1755            sync => {
1756                    status => 202,
1757            },
1758            backup => {
1759                    status => 202,
1760            },
1761            userlist => {
1762                    status => 200,
1763                    returns => [ qw/name passwd flags fname misc/ ],
1764            },
1765            useradd => {
1766                    required => [ qw/name passwd flags/ ],
1767                    optional => [ qw/fname misc/ ],
1768                    status => 200,
1769            },
1770            userdel => {
1771                    required => [ qw/name/ ],
1772                    status => 200,
1773            },
1774            nodelist => {
1775                    status => 200,
1776                    returns => [ qw/name label doc_num word_num size/ ],
1777            },
1778            nodeadd => {
1779                    required => [ qw/name/ ],
1780                    optional => [ qw/label/ ],
1781                    status => 200,
1782            },
1783            nodedel => {
1784                    required => [ qw/name/ ],
1785                    status => 200,
1786            },
1787            nodeclr => {
1788                    required => [ qw/name/ ],
1789                    status => 200,
1790            },
1791            nodertt => {
1792                    status => 200,  
1793            },
1794    };
1795    
1796    sub master {
1797            my $self = shift;
1798    
1799            my $args = {@_};
1800    
1801            # have action?
1802            my $action = $args->{action} || croak "need action, available: ",
1803                    join(", ",keys %{ $estmaster_rest });
1804    
1805            # check if action is valid
1806            my $rest = $estmaster_rest->{$action};
1807            croak "action '$action' is not supported, available actions: ",
1808                    join(", ",keys %{ $estmaster_rest }) unless ($rest);
1809    
1810            croak "BUG: action '$action' needs return status" unless ($rest->{status});
1811    
1812            my @args;
1813    
1814            if ($rest->{required} || $rest->{optional}) {
1815    
1816                    map {
1817                            croak "need parametar '$_' for action '$action'" unless ($args->{$_});
1818                            push @args, $_ . '=' . uri_escape( $args->{$_} );
1819                    } ( @{ $rest->{required} } );
1820    
1821                    map {
1822                            push @args, $_ . '=' . uri_escape( $args->{$_} ) if ($args->{$_});
1823                    } ( @{ $rest->{optional} } );
1824    
1825            }
1826    
1827            my $uri = new URI( $self->{url} );
1828    
1829            my $resbody;
1830    
1831            my $status = $self->shuttle_url(
1832                    'http://' . $uri->host_port . '/master?action=' . $action ,
1833                    'application/x-www-form-urlencoded',
1834                    join('&', @args),
1835                    \$resbody,
1836                    1,
1837            ) or confess "shuttle_url failed";
1838    
1839            if ($status == $rest->{status}) {
1840    
1841                    # refresh node info after sync
1842                    $self->_set_info if ($action eq 'sync');
1843    
1844                    if ($rest->{returns} && wantarray) {
1845    
1846                            my @results;
1847                            my $fields = $#{$rest->{returns}};
1848    
1849                            foreach my $line ( split(/[\r\n]/,$resbody) ) {
1850                                    my @e = split(/\t/, $line, $fields + 1);
1851                                    my $row;
1852                                    foreach my $i ( 0 .. $fields) {
1853                                            $row->{ $rest->{returns}->[$i] } = $e[ $i ];
1854                                    }
1855                                    push @results, $row;
1856                            }
1857    
1858                            return @results;
1859    
1860                    } elsif ($resbody) {
1861                            chomp $resbody;
1862                            return $resbody;
1863                    } else {
1864                            return 0E0;
1865                    }
1866            }
1867    
1868            carp "expected status $rest->{status}, but got $status";
1869            return undef;
1870    }
1871    
1872  =head1 PRIVATE METHODS  =head1 PRIVATE METHODS
1873    
# Line 1787  Hyper Estraier Ruby interface on which t Line 1939  Hyper Estraier Ruby interface on which t
1939    
1940  Dobrica Pavlinusic, E<lt>dpavlin@rot13.orgE<gt>  Dobrica Pavlinusic, E<lt>dpavlin@rot13.orgE<gt>
1941    
1942    Robert Klep E<lt>robert@klep.nameE<gt> contributed refactored search code
1943    
1944  =head1 COPYRIGHT AND LICENSE  =head1 COPYRIGHT AND LICENSE
1945    

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

  ViewVC Help
Powered by ViewVC 1.1.26