/[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 142 by dpavlin, Wed May 10 14:57:50 2006 UTC revision 176 by dpavlin, Sun Aug 6 18:43:58 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';  our $VERSION = '0.07';
8    
9  =head1 NAME  =head1 NAME
10    
# Line 120  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.
 (key=value), vectors (also key value) display text and hidden text.  
124    
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 157  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/^%/) {                          } elsif ($line =~ m/^%/) {
# Line 243  sub add_hidden_text { Line 269  sub add_hidden_text {
269          push @{ $self->{htexts} }, $self->_s($text);          push @{ $self->{htexts} }, $self->_s($text);
270  }  }
271    
272    =head2 add_vectors
273    
274    Add a vectors
275    
276      $doc->add_vector(
277            'vector_name' => 42,
278            'another' => 12345,
279      );
280    
281    =cut
282    
283    sub add_vectors {
284            my $self = shift;
285            return unless (@_);
286    
287            # this is ugly, but works
288            die "add_vector needs HASH as argument" unless ($#_ % 2 == 1);
289    
290            $self->{kwords} = {@_};
291    }
292    
293    
294  =head2 id  =head2 id
295    
# Line 337  sub dump_draft { Line 384  sub dump_draft {
384          }          }
385    
386          if ($self->{kwords}) {          if ($self->{kwords}) {
387                  $draft .= '%%VECTOR';                  $draft .= '%VECTOR';
388                  while (my ($key, $value) = each %{ $self->{kwords} }) {                  while (my ($key, $value) = each %{ $self->{kwords} }) {
389                          $draft .= "\t$key\t$value";                          $draft .= "\t$key\t$value";
390                  }                  }
# Line 630  sub skip { Line 677  sub skip {
677          return $self->{skip};          return $self->{skip};
678  }  }
679    
680    =head2 set_mask
681    
682    Filter out some links when searching.
683    
684    Argument array of link numbers, starting with 0 (current node).
685    
686      $cond->set_mask(qw/0 1 4/);
687    
688    =cut
689    
690    sub set_mask {
691            my $self = shift;
692            return unless (@_);
693            $self->{mask} = \@_;
694    }
695    
696    
697  package Search::Estraier::ResultDocument;  package Search::Estraier::ResultDocument;
698    
# Line 1053  Add a document Line 1116  Add a document
1116    
1117    $node->put_doc( $document_draft ) or die "can't add document";    $node->put_doc( $document_draft ) or die "can't add document";
1118    
1119  Return true on success or false on failture.  Return true on success or false on failure.
1120    
1121  =cut  =cut
1122    
# Line 1061  sub put_doc { Line 1124  sub put_doc {
1124          my $self = shift;          my $self = shift;
1125          my $doc = shift || return;          my $doc = shift || return;
1126          return unless ($self->{url} && $doc->isa('Search::Estraier::Document'));          return unless ($self->{url} && $doc->isa('Search::Estraier::Document'));
1127          $self->shuttle_url( $self->{url} . '/put_doc',          if ($self->shuttle_url( $self->{url} . '/put_doc',
1128                  'text/x-estraier-draft',                  'text/x-estraier-draft',
1129                  $doc->dump_draft,                  $doc->dump_draft,
1130                  undef                  undef
1131          ) == 200;          ) == 200) {
1132                    $self->_clear_info;
1133                    return 1;
1134            }
1135            return undef;
1136  }  }
1137    
1138    
# Line 1084  sub out_doc { Line 1151  sub out_doc {
1151          my $id = shift || return;          my $id = shift || return;
1152          return unless ($self->{url});          return unless ($self->{url});
1153          croak "id must be number, not '$id'" unless ($id =~ m/^\d+$/);          croak "id must be number, not '$id'" unless ($id =~ m/^\d+$/);
1154          $self->shuttle_url( $self->{url} . '/out_doc',          if ($self->shuttle_url( $self->{url} . '/out_doc',
1155                  'application/x-www-form-urlencoded',                  'application/x-www-form-urlencoded',
1156                  "id=$id",                  "id=$id",
1157                  undef                  undef
1158          ) == 200;          ) == 200) {
1159                    $self->_clear_info;
1160                    return 1;
1161            }
1162            return undef;
1163  }  }
1164    
1165    
# Line 1106  sub out_doc_by_uri { Line 1177  sub out_doc_by_uri {
1177          my $self = shift;          my $self = shift;
1178          my $uri = shift || return;          my $uri = shift || return;
1179          return unless ($self->{url});          return unless ($self->{url});
1180          $self->shuttle_url( $self->{url} . '/out_doc',          if ($self->shuttle_url( $self->{url} . '/out_doc',
1181                  'application/x-www-form-urlencoded',                  'application/x-www-form-urlencoded',
1182                  "uri=" . uri_escape($uri),                  "uri=" . uri_escape($uri),
1183                  undef                  undef
1184          ) == 200;          ) == 200) {
1185                    $self->_clear_info;
1186                    return 1;
1187            }
1188            return undef;
1189  }  }
1190    
1191    
# Line 1128  sub edit_doc { Line 1203  sub edit_doc {
1203          my $self = shift;          my $self = shift;
1204          my $doc = shift || return;          my $doc = shift || return;
1205          return unless ($self->{url} && $doc->isa('Search::Estraier::Document'));          return unless ($self->{url} && $doc->isa('Search::Estraier::Document'));
1206          $self->shuttle_url( $self->{url} . '/edit_doc',          if ($self->shuttle_url( $self->{url} . '/edit_doc',
1207                  'text/x-estraier-draft',                  'text/x-estraier-draft',
1208                  $doc->dump_draft,                  $doc->dump_draft,
1209                  undef                  undef
1210          ) == 200;          ) == 200) {
1211                    $self->_clear_info;
1212                    return 1;
1213            }
1214            return undef;
1215  }  }
1216    
1217    
# Line 1290  sub _fetch_doc { Line 1369  sub _fetch_doc {
1369          $path = '/etch_doc' if ($a->{etch});          $path = '/etch_doc' if ($a->{etch});
1370    
1371          if ($a->{id}) {          if ($a->{id}) {
1372                  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+$/);
1373                  $arg = 'id=' . $a->{id};                  $arg = 'id=' . $a->{id};
1374          } elsif ($a->{uri}) {          } elsif ($a->{uri}) {
1375                  $arg = 'uri=' . uri_escape($a->{uri});                  $arg = 'uri=' . uri_escape($a->{uri});
# Line 1499  sub cond_to_query { Line 1578  sub cond_to_query {
1578          push @args, 'wwidth=' . $self->{wwidth};          push @args, 'wwidth=' . $self->{wwidth};
1579          push @args, 'hwidth=' . $self->{hwidth};          push @args, 'hwidth=' . $self->{hwidth};
1580          push @args, 'awidth=' . $self->{awidth};          push @args, 'awidth=' . $self->{awidth};
1581          push @args, 'skip=' . $self->{skip} if ($self->{skip});          push @args, 'skip=' . $cond->{skip} if ($cond->{skip});
1582    
1583            if ($cond->{mask}) {
1584                    my $mask = 0;
1585                    map { $mask += ( 2 ** $_ ) } @{ $cond->{mask} };
1586    
1587                    push @args, 'mask=' . $mask if ($mask);
1588            }
1589    
1590          return join('&', @args);          return join('&', @args);
1591  }  }
# Line 1646  sub set_user { Line 1732  sub set_user {
1732          croak "mode must be number, not '$mode'" unless ($mode =~ m/^\d+$/);          croak "mode must be number, not '$mode'" unless ($mode =~ m/^\d+$/);
1733    
1734          $self->shuttle_url( $self->{url} . '/_set_user',          $self->shuttle_url( $self->{url} . '/_set_user',
1735                  'text/plain',                  'application/x-www-form-urlencoded',
1736                  'name=' . uri_escape($name) . '&mode=' . $mode,                  'name=' . uri_escape($name) . '&mode=' . $mode,
1737                  undef                  undef
1738          ) == 200;          ) == 200;
# Line 1679  sub set_link { Line 1765  sub set_link {
1765                  undef                  undef
1766          ) == 200) {          ) == 200) {
1767                  # refresh node info after adding link                  # refresh node info after adding link
1768                  $self->_set_info;                  $self->_clear_info;
1769                  return 1;                  return 1;
1770          }          }
1771            return undef;
1772  }  }
1773    
1774  =head2 admins  =head2 admins
# Line 1726  sub links { Line 1813  sub links {
1813          return $self->{inform}->{links};          return $self->{inform}->{links};
1814  }  }
1815    
1816    =head2 cacheusage
1817    
1818    Return cache usage for a node
1819    
1820      my $cache = $node->cacheusage;
1821    
1822    =cut
1823    
1824    sub cacheusage {
1825            my $self = shift;
1826    
1827            return unless ($self->{url});
1828    
1829            my $resbody;
1830            my $rv = $self->shuttle_url( $self->{url} . '/cacheusage',
1831                    'text/plain',
1832                    undef,
1833                    \$resbody,
1834            );
1835    
1836            return if ($rv != 200 || !$resbody);
1837    
1838            return $resbody;
1839    }
1840    
1841  =head2 master  =head2 master
1842    
1843  Set actions on Hyper Estraier node master (C<estmaster> process)  Set actions on Hyper Estraier node master (C<estmaster> process)
# Line 1828  sub master { Line 1940  sub master {
1940          ) or confess "shuttle_url failed";          ) or confess "shuttle_url failed";
1941    
1942          if ($status == $rest->{status}) {          if ($status == $rest->{status}) {
1943    
1944                    # refresh node info after sync
1945                    $self->_clear_info if ($action eq 'sync' || $action =~ m/^node(?:add|del|clr)$/);
1946    
1947                  if ($rest->{returns} && wantarray) {                  if ($rest->{returns} && wantarray) {
1948    
1949                          my @results;                          my @results;
# Line 1885  sub _set_info { Line 2001  sub _set_info {
2001    
2002          my @lines = split(/[\r\n]/,$resbody);          my @lines = split(/[\r\n]/,$resbody);
2003    
2004          $self->{inform} = {};          $self->_clear_info;
2005    
2006          ( $self->{inform}->{name}, $self->{inform}->{label}, $self->{inform}->{dnum},          ( $self->{inform}->{name}, $self->{inform}->{label}, $self->{inform}->{dnum},
2007                  $self->{inform}->{wnum}, $self->{inform}->{size} ) = split(/\t/, shift @lines, 5);                  $self->{inform}->{wnum}, $self->{inform}->{size} ) = split(/\t/, shift @lines, 5);
# Line 1910  sub _set_info { Line 2026  sub _set_info {
2026    
2027  }  }
2028    
2029    =head2 _clear_info
2030    
2031    Clear information for node
2032    
2033      $node->_clear_info;
2034    
2035    On next call to C<name>, C<label>, C<doc_num>, C<word_num> or C<size> node
2036    info will be fetch again from Hyper Estraier.
2037    
2038    =cut
2039    sub _clear_info {
2040            my $self = shift;
2041            $self->{inform} = {
2042                    dnum => -1,
2043                    wnum => -1,
2044                    size => -1.0,
2045            };
2046    }
2047    
2048  ###  ###
2049    
2050  =head1 EXPORT  =head1 EXPORT
# Line 1922  L<http://hyperestraier.sourceforge.net/> Line 2057  L<http://hyperestraier.sourceforge.net/>
2057    
2058  Hyper Estraier Ruby interface on which this module is based.  Hyper Estraier Ruby interface on which this module is based.
2059    
2060    Hyper Estraier now also has pure-perl binding included in distribution. It's
2061    a faster way to access databases directly if you are not running
2062    C<estmaster> P2P server.
2063    
2064  =head1 AUTHOR  =head1 AUTHOR
2065    
2066  Dobrica Pavlinusic, E<lt>dpavlin@rot13.orgE<gt>  Dobrica Pavlinusic, E<lt>dpavlin@rot13.orgE<gt>

Legend:
Removed from v.142  
changed lines
  Added in v.176

  ViewVC Help
Powered by ViewVC 1.1.26