4 |
use strict; |
use strict; |
5 |
use warnings; |
use warnings; |
6 |
|
|
7 |
our $VERSION = '0.04'; |
our $VERSION = '0.06_1'; |
8 |
|
|
9 |
=head1 NAME |
=head1 NAME |
10 |
|
|
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/; |
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 |
); |
); |
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 |
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 |
|
|
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 |
|
|
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 |
} |
} |
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 |
|
} ( @{ $rest->{required} } ); |
1785 |
|
|
1786 |
|
map { |
1787 |
|
push @args, $_ . '=' . uri_escape( $args->{$_} ) if ($args->{$_}); |
1788 |
|
} ( @{ $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 |
|
chomp $resbody; |
1823 |
|
return $resbody; |
1824 |
|
} else { |
1825 |
|
return 0E0; |
1826 |
|
} |
1827 |
|
} |
1828 |
|
|
1829 |
|
carp "expected status $rest->{status}, but got $status"; |
1830 |
|
return undef; |
1831 |
|
} |
1832 |
|
|
1833 |
=head1 PRIVATE METHODS |
=head1 PRIVATE METHODS |
1834 |
|
|
1900 |
|
|
1901 |
Dobrica Pavlinusic, E<lt>dpavlin@rot13.orgE<gt> |
Dobrica Pavlinusic, E<lt>dpavlin@rot13.orgE<gt> |
1902 |
|
|
1903 |
|
Robert Klep E<lt>robert@klep.nameE<gt> contributed refactored search code |
1904 |
|
|
1905 |
=head1 COPYRIGHT AND LICENSE |
=head1 COPYRIGHT AND LICENSE |
1906 |
|
|