/[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 132 by dpavlin, Mon May 8 21:33:37 2006 UTC revision 191 by dpavlin, Sun Nov 5 16:26:57 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.06_1';  our $VERSION = '0.08';
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 117  our @ISA = qw/Search::Estraier/; Line 120  our @ISA = qw/Search::Estraier/;
120    
121  =head1 Search::Estraier::Document  =head1 Search::Estraier::Document
122    
123  This class implements Document which is collection of attributes  This class implements Document which is single item in Hyper Estraier.
124  (key=value), vectors (also key value) display text and hidden text.  
125    It's is collection of:
126    
127    =over 4
128    
129    =item attributes
130    
131    C<< 'key' => 'value' >> pairs which can later be used for filtering of results
132    
133    You can add common filters to C<attrindex> in estmaster's C<_conf>
134    file for better performance. See C<attrindex> in
135    L<Hyper Estraier P2P Guide|http://hyperestraier.sourceforge.net/nguide-en.html>.
136    
137    =item vectors
138    
139    also C<< 'key' => 'value' >> pairs
140    
141    =item display text
142    
143    Text which will be used to create searchable corpus of your index and
144    included in snippet output.
145    
146    =item hidden text
147    
148    Text which will be searchable, but will not be included in snippet.
149    
150    =back
151    
152  =head2 new  =head2 new
153    
# Line 154  sub new { Line 182  sub new {
182    
183                          if ($line =~ m/^%VECTOR\t(.+)$/) {                          if ($line =~ m/^%VECTOR\t(.+)$/) {
184                                  my @fields = split(/\t/, $1);                                  my @fields = split(/\t/, $1);
185                                  for my $i ( 0 .. ($#fields - 1) ) {                                  if ($#fields % 2 == 1) {
186                                          $self->{kwords}->{ $fields[ $i ] } = $fields[ $i + 1 ];                                          $self->{kwords} = { @fields };
187                                          $i++;                                  } else {
188                                            warn "can't decode $line\n";
189                                  }                                  }
190                                  next;                                  next;
191                            } elsif ($line =~ m/^%SCORE\t(.+)$/) {
192                                $self->{score} = $1;
193                                next;
194                          } elsif ($line =~ m/^%/) {                          } elsif ($line =~ m/^%/) {
195                                  # What is this? comment?                                  # What is this? comment?
196                                  #warn "$line\n";                                  #warn "$line\n";
# Line 240  sub add_hidden_text { Line 272  sub add_hidden_text {
272          push @{ $self->{htexts} }, $self->_s($text);          push @{ $self->{htexts} }, $self->_s($text);
273  }  }
274    
275    =head2 add_vectors
276    
277    Add a vectors
278    
279      $doc->add_vector(
280            'vector_name' => 42,
281            'another' => 12345,
282      );
283    
284    =cut
285    
286    sub add_vectors {
287            my $self = shift;
288            return unless (@_);
289    
290            # this is ugly, but works
291            die "add_vector needs HASH as argument" unless ($#_ % 2 == 1);
292    
293            $self->{kwords} = {@_};
294    }
295    
296    =head2 set_score
297    
298    Set the substitute score
299    
300      $doc->set_score(12345);
301    
302    =cut
303    
304    sub set_score {
305        my $self = shift;
306        my $score = shift;
307        return unless (defined($score));
308        $self->{score} = $score;
309    }
310    
311    =head2 score
312    
313    Get the substitute score
314    
315    =cut
316    
317    sub score {
318        my $self = shift;
319        return -1 unless (defined($self->{score}));
320        return $self->{score};
321    }
322    
323  =head2 id  =head2 id
324    
# Line 334  sub dump_draft { Line 413  sub dump_draft {
413          }          }
414    
415          if ($self->{kwords}) {          if ($self->{kwords}) {
416                  $draft .= '%%VECTOR';                  $draft .= '%VECTOR';
417                  while (my ($key, $value) = each %{ $self->{kwords} }) {                  while (my ($key, $value) = each %{ $self->{kwords} }) {
418                          $draft .= "\t$key\t$value";                          $draft .= "\t$key\t$value";
419                  }                  }
420                  $draft .= "\n";                  $draft .= "\n";
421          }          }
422    
423            if (defined($self->{score}) && $self->{score} >= 0) {
424                $draft .= "%SCORE\t" . $self->{score} . "\n";
425            }
426    
427          $draft .= "\n";          $draft .= "\n";
428    
429          $draft .= join("\n", @{ $self->{dtexts} }) . "\n" if ($self->{dtexts});          $draft .= join("\n", @{ $self->{dtexts} }) . "\n" if ($self->{dtexts});
# Line 628  sub skip { Line 711  sub skip {
711  }  }
712    
713    
714    =head2 set_distinct
715    
716      $cond->set_distinct('@author');
717    
718    =cut
719    
720    sub set_distinct {
721            my $self = shift;
722            $self->{distinct} = shift;
723    }
724    
725    =head2 distinct
726    
727    Return distinct attribute
728    
729      print $cond->distinct;
730    
731    =cut
732    
733    sub distinct {
734            my $self = shift;
735            return $self->{distinct};
736    }
737    
738    =head2 set_mask
739    
740    Filter out some links when searching.
741    
742    Argument array of link numbers, starting with 0 (current node).
743    
744      $cond->set_mask(qw/0 1 4/);
745    
746    =cut
747    
748    sub set_mask {
749            my $self = shift;
750            return unless (@_);
751            $self->{mask} = \@_;
752    }
753    
754    
755  package Search::Estraier::ResultDocument;  package Search::Estraier::ResultDocument;
756    
757  use Carp qw/croak/;  use Carp qw/croak/;
# Line 874  or in more verbose form Line 998  or in more verbose form
998          url => 'http://localhost:1978/node/test',          url => 'http://localhost:1978/node/test',
999          user => 'admin',          user => 'admin',
1000          passwd => 'admin'          passwd => 'admin'
1001            create => 1,
1002            label => 'optional node label',
1003          debug => 1,          debug => 1,
1004          croak_on_error => 1          croak_on_error => 1
1005    );    );
# Line 894  specify username for node server authent Line 1020  specify username for node server authent
1020    
1021  password for authentication  password for authentication
1022    
1023    =item create
1024    
1025    create node if it doesn't exists
1026    
1027    =item label
1028    
1029    optional label for new node if C<create> is used
1030    
1031  =item debug  =item debug
1032    
1033  dumps a B<lot> of debugging output  dumps a B<lot> of debugging output
# Line 937  sub new { Line 1071  sub new {
1071                  size => -1.0,                  size => -1.0,
1072          };          };
1073    
1074            if ($self->{create}) {
1075                    if (! eval { $self->name } || $@) {
1076                            my $name = $1 if ($self->{url} =~ m#/node/([^/]+)/*#);
1077                            croak "can't find node name in '$self->{url}'" unless ($name);
1078                            my $label = $self->{label} || $name;
1079                            $self->master(
1080                                    action => 'nodeadd',
1081                                    name => $name,
1082                                    label => $label,
1083                            ) || croak "can't create node $name ($label)";
1084                    }
1085            }
1086    
1087          $self ? return $self : return undef;          $self ? return $self : return undef;
1088  }  }
1089    
# Line 1027  Add a document Line 1174  Add a document
1174    
1175    $node->put_doc( $document_draft ) or die "can't add document";    $node->put_doc( $document_draft ) or die "can't add document";
1176    
1177  Return true on success or false on failture.  Return true on success or false on failure.
1178    
1179  =cut  =cut
1180    
# Line 1035  sub put_doc { Line 1182  sub put_doc {
1182          my $self = shift;          my $self = shift;
1183          my $doc = shift || return;          my $doc = shift || return;
1184          return unless ($self->{url} && $doc->isa('Search::Estraier::Document'));          return unless ($self->{url} && $doc->isa('Search::Estraier::Document'));
1185          $self->shuttle_url( $self->{url} . '/put_doc',          if ($self->shuttle_url( $self->{url} . '/put_doc',
1186                  'text/x-estraier-draft',                  'text/x-estraier-draft',
1187                  $doc->dump_draft,                  $doc->dump_draft,
1188                  undef                  undef
1189          ) == 200;          ) == 200) {
1190                    $self->_clear_info;
1191                    return 1;
1192            }
1193            return undef;
1194  }  }
1195    
1196    
# Line 1058  sub out_doc { Line 1209  sub out_doc {
1209          my $id = shift || return;          my $id = shift || return;
1210          return unless ($self->{url});          return unless ($self->{url});
1211          croak "id must be number, not '$id'" unless ($id =~ m/^\d+$/);          croak "id must be number, not '$id'" unless ($id =~ m/^\d+$/);
1212          $self->shuttle_url( $self->{url} . '/out_doc',          if ($self->shuttle_url( $self->{url} . '/out_doc',
1213                  'application/x-www-form-urlencoded',                  'application/x-www-form-urlencoded',
1214                  "id=$id",                  "id=$id",
1215                  undef                  undef
1216          ) == 200;          ) == 200) {
1217                    $self->_clear_info;
1218                    return 1;
1219            }
1220            return undef;
1221  }  }
1222    
1223    
# Line 1080  sub out_doc_by_uri { Line 1235  sub out_doc_by_uri {
1235          my $self = shift;          my $self = shift;
1236          my $uri = shift || return;          my $uri = shift || return;
1237          return unless ($self->{url});          return unless ($self->{url});
1238          $self->shuttle_url( $self->{url} . '/out_doc',          if ($self->shuttle_url( $self->{url} . '/out_doc',
1239                  'application/x-www-form-urlencoded',                  'application/x-www-form-urlencoded',
1240                  "uri=" . uri_escape($uri),                  "uri=" . uri_escape($uri),
1241                  undef                  undef
1242          ) == 200;          ) == 200) {
1243                    $self->_clear_info;
1244                    return 1;
1245            }
1246            return undef;
1247  }  }
1248    
1249    
# Line 1102  sub edit_doc { Line 1261  sub edit_doc {
1261          my $self = shift;          my $self = shift;
1262          my $doc = shift || return;          my $doc = shift || return;
1263          return unless ($self->{url} && $doc->isa('Search::Estraier::Document'));          return unless ($self->{url} && $doc->isa('Search::Estraier::Document'));
1264          $self->shuttle_url( $self->{url} . '/edit_doc',          if ($self->shuttle_url( $self->{url} . '/edit_doc',
1265                  'text/x-estraier-draft',                  'text/x-estraier-draft',
1266                  $doc->dump_draft,                  $doc->dump_draft,
1267                  undef                  undef
1268          ) == 200;          ) == 200) {
1269                    $self->_clear_info;
1270                    return 1;
1271            }
1272            return undef;
1273  }  }
1274    
1275    
# Line 1264  sub _fetch_doc { Line 1427  sub _fetch_doc {
1427          $path = '/etch_doc' if ($a->{etch});          $path = '/etch_doc' if ($a->{etch});
1428    
1429          if ($a->{id}) {          if ($a->{id}) {
1430                  croak "id must be numberm not '$a->{id}'" unless ($a->{id} =~ m/^\d+$/);                  croak "id must be number not '$a->{id}'" unless ($a->{id} =~ m/^\d+$/);
1431                  $arg = 'id=' . $a->{id};                  $arg = 'id=' . $a->{id};
1432          } elsif ($a->{uri}) {          } elsif ($a->{uri}) {
1433                  $arg = 'uri=' . uri_escape($a->{uri});                  $arg = 'uri=' . uri_escape($a->{uri});
# Line 1473  sub cond_to_query { Line 1636  sub cond_to_query {
1636          push @args, 'wwidth=' . $self->{wwidth};          push @args, 'wwidth=' . $self->{wwidth};
1637          push @args, 'hwidth=' . $self->{hwidth};          push @args, 'hwidth=' . $self->{hwidth};
1638          push @args, 'awidth=' . $self->{awidth};          push @args, 'awidth=' . $self->{awidth};
1639          push @args, 'skip=' . $self->{skip} if ($self->{skip});          push @args, 'skip=' . $cond->{skip} if ($cond->{skip});
1640    
1641            if (my $distinct = $cond->distinct) {
1642                    push @args, 'distinct=' . uri_escape($distinct);
1643            }
1644    
1645            if ($cond->{mask}) {
1646                    my $mask = 0;
1647                    map { $mask += ( 2 ** $_ ) } @{ $cond->{mask} };
1648    
1649                    push @args, 'mask=' . $mask if ($mask);
1650            }
1651    
1652          return join('&', @args);          return join('&', @args);
1653  }  }
# Line 1620  sub set_user { Line 1794  sub set_user {
1794          croak "mode must be number, not '$mode'" unless ($mode =~ m/^\d+$/);          croak "mode must be number, not '$mode'" unless ($mode =~ m/^\d+$/);
1795    
1796          $self->shuttle_url( $self->{url} . '/_set_user',          $self->shuttle_url( $self->{url} . '/_set_user',
1797                  'text/plain',                  'application/x-www-form-urlencoded',
1798                  'name=' . uri_escape($name) . '&mode=' . $mode,                  'name=' . uri_escape($name) . '&mode=' . $mode,
1799                  undef                  undef
1800          ) == 200;          ) == 200;
# Line 1653  sub set_link { Line 1827  sub set_link {
1827                  undef                  undef
1828          ) == 200) {          ) == 200) {
1829                  # refresh node info after adding link                  # refresh node info after adding link
1830                  $self->_set_info;                  $self->_clear_info;
1831                  return 1;                  return 1;
1832          }          }
1833            return undef;
1834  }  }
1835    
1836  =head2 admins  =head2 admins
# Line 1700  sub links { Line 1875  sub links {
1875          return $self->{inform}->{links};          return $self->{inform}->{links};
1876  }  }
1877    
1878    =head2 cacheusage
1879    
1880    Return cache usage for a node
1881    
1882      my $cache = $node->cacheusage;
1883    
1884    =cut
1885    
1886    sub cacheusage {
1887            my $self = shift;
1888    
1889            return unless ($self->{url});
1890    
1891            my $resbody;
1892            my $rv = $self->shuttle_url( $self->{url} . '/cacheusage',
1893                    'text/plain',
1894                    undef,
1895                    \$resbody,
1896            );
1897    
1898            return if ($rv != 200 || !$resbody);
1899    
1900            return $resbody;
1901    }
1902    
1903    =head2 master
1904    
1905    Set actions on Hyper Estraier node master (C<estmaster> process)
1906    
1907      $node->master(
1908            action => 'sync'
1909      );
1910    
1911    All available actions are documented in
1912    L<http://hyperestraier.sourceforge.net/nguide-en.html#protocol>
1913    
1914    =cut
1915    
1916    my $estmaster_rest = {
1917            shutdown => {
1918                    status => 202,
1919            },
1920            sync => {
1921                    status => 202,
1922            },
1923            backup => {
1924                    status => 202,
1925            },
1926            userlist => {
1927                    status => 200,
1928                    returns => [ qw/name passwd flags fname misc/ ],
1929            },
1930            useradd => {
1931                    required => [ qw/name passwd flags/ ],
1932                    optional => [ qw/fname misc/ ],
1933                    status => 200,
1934            },
1935            userdel => {
1936                    required => [ qw/name/ ],
1937                    status => 200,
1938            },
1939            nodelist => {
1940                    status => 200,
1941                    returns => [ qw/name label doc_num word_num size/ ],
1942            },
1943            nodeadd => {
1944                    required => [ qw/name/ ],
1945                    optional => [ qw/label/ ],
1946                    status => 200,
1947            },
1948            nodedel => {
1949                    required => [ qw/name/ ],
1950                    status => 200,
1951            },
1952            nodeclr => {
1953                    required => [ qw/name/ ],
1954                    status => 200,
1955            },
1956            nodertt => {
1957                    status => 200,  
1958            },
1959    };
1960    
1961    sub master {
1962            my $self = shift;
1963    
1964            my $args = {@_};
1965    
1966            # have action?
1967            my $action = $args->{action} || croak "need action, available: ",
1968                    join(", ",keys %{ $estmaster_rest });
1969    
1970            # check if action is valid
1971            my $rest = $estmaster_rest->{$action};
1972            croak "action '$action' is not supported, available actions: ",
1973                    join(", ",keys %{ $estmaster_rest }) unless ($rest);
1974    
1975            croak "BUG: action '$action' needs return status" unless ($rest->{status});
1976    
1977            my @args;
1978    
1979            if ($rest->{required} || $rest->{optional}) {
1980    
1981                    map {
1982                            croak "need parametar '$_' for action '$action'" unless ($args->{$_});
1983                            push @args, $_ . '=' . uri_escape( $args->{$_} );
1984                    } ( @{ $rest->{required} } );
1985    
1986                    map {
1987                            push @args, $_ . '=' . uri_escape( $args->{$_} ) if ($args->{$_});
1988                    } ( @{ $rest->{optional} } );
1989    
1990            }
1991    
1992            my $uri = new URI( $self->{url} );
1993    
1994            my $resbody;
1995    
1996            my $status = $self->shuttle_url(
1997                    'http://' . $uri->host_port . '/master?action=' . $action ,
1998                    'application/x-www-form-urlencoded',
1999                    join('&', @args),
2000                    \$resbody,
2001                    1,
2002            ) or confess "shuttle_url failed";
2003    
2004            if ($status == $rest->{status}) {
2005    
2006                    # refresh node info after sync
2007                    $self->_clear_info if ($action eq 'sync' || $action =~ m/^node(?:add|del|clr)$/);
2008    
2009                    if ($rest->{returns} && wantarray) {
2010    
2011                            my @results;
2012                            my $fields = $#{$rest->{returns}};
2013    
2014                            foreach my $line ( split(/[\r\n]/,$resbody) ) {
2015                                    my @e = split(/\t/, $line, $fields + 1);
2016                                    my $row;
2017                                    foreach my $i ( 0 .. $fields) {
2018                                            $row->{ $rest->{returns}->[$i] } = $e[ $i ];
2019                                    }
2020                                    push @results, $row;
2021                            }
2022    
2023                            return @results;
2024    
2025                    } elsif ($resbody) {
2026                            chomp $resbody;
2027                            return $resbody;
2028                    } else {
2029                            return 0E0;
2030                    }
2031            }
2032    
2033            carp "expected status $rest->{status}, but got $status";
2034            return undef;
2035    }
2036    
2037  =head1 PRIVATE METHODS  =head1 PRIVATE METHODS
2038    
# Line 1730  sub _set_info { Line 2063  sub _set_info {
2063    
2064          my @lines = split(/[\r\n]/,$resbody);          my @lines = split(/[\r\n]/,$resbody);
2065    
2066          $self->{inform} = {};          $self->_clear_info;
2067    
2068          ( $self->{inform}->{name}, $self->{inform}->{label}, $self->{inform}->{dnum},          ( $self->{inform}->{name}, $self->{inform}->{label}, $self->{inform}->{dnum},
2069                  $self->{inform}->{wnum}, $self->{inform}->{size} ) = split(/\t/, shift @lines, 5);                  $self->{inform}->{wnum}, $self->{inform}->{size} ) = split(/\t/, shift @lines, 5);
# Line 1755  sub _set_info { Line 2088  sub _set_info {
2088    
2089  }  }
2090    
2091    =head2 _clear_info
2092    
2093    Clear information for node
2094    
2095      $node->_clear_info;
2096    
2097    On next call to C<name>, C<label>, C<doc_num>, C<word_num> or C<size> node
2098    info will be fetch again from Hyper Estraier.
2099    
2100    =cut
2101    sub _clear_info {
2102            my $self = shift;
2103            $self->{inform} = {
2104                    dnum => -1,
2105                    wnum => -1,
2106                    size => -1.0,
2107            };
2108    }
2109    
2110  ###  ###
2111    
2112  =head1 EXPORT  =head1 EXPORT
# Line 1767  L<http://hyperestraier.sourceforge.net/> Line 2119  L<http://hyperestraier.sourceforge.net/>
2119    
2120  Hyper Estraier Ruby interface on which this module is based.  Hyper Estraier Ruby interface on which this module is based.
2121    
2122    Hyper Estraier now also has pure-perl binding included in distribution. It's
2123    a faster way to access databases directly if you are not running
2124    C<estmaster> P2P server.
2125    
2126  =head1 AUTHOR  =head1 AUTHOR
2127    
2128  Dobrica Pavlinusic, E<lt>dpavlin@rot13.orgE<gt>  Dobrica Pavlinusic, E<lt>dpavlin@rot13.orgE<gt>

Legend:
Removed from v.132  
changed lines
  Added in v.191

  ViewVC Help
Powered by ViewVC 1.1.26