4 |
use strict; |
use strict; |
5 |
use warnings; |
use warnings; |
6 |
|
|
7 |
our $VERSION = '0.01'; |
our $VERSION = '0.03'; |
8 |
|
|
9 |
use SWISH::API; |
use SWISH::API; |
10 |
use Text::Iconv; |
use Text::Iconv; |
12 |
use Carp; |
use Carp; |
13 |
use Digest::MD5 qw(md5_hex); |
use Digest::MD5 qw(md5_hex); |
14 |
use Memoize; |
use Memoize; |
|
use IPC::Run qw(start timeout pump finish); |
|
15 |
use File::Which; |
use File::Which; |
16 |
|
|
17 |
use Data::Dumper; |
use Data::Dumper; |
46 |
|
|
47 |
=head1 Methods used for indexing |
=head1 Methods used for indexing |
48 |
|
|
49 |
=head2 open |
=head2 open_index |
50 |
|
|
51 |
Create new object for index. |
Create new object for index. |
52 |
|
|
53 |
my $i = SWISH::Split->open({ |
my $i = SWISH::Split->open_index({ |
54 |
index => '/path/to/index', |
index => '/path/to/index', |
55 |
slice_name => \&slice_on_path, |
slice_name => \&slice_on_path, |
56 |
slices => 30, |
slices => 30, |
68 |
return shift split(/\//,$_[0]); |
return shift split(/\//,$_[0]); |
69 |
} |
} |
70 |
|
|
71 |
Options to open are following: |
Options to C<open_index> are following: |
72 |
|
|
73 |
=over 5 |
=over 5 |
74 |
|
|
97 |
=item C<swish_config> |
=item C<swish_config> |
98 |
|
|
99 |
additional parametars which will be inserted into |
additional parametars which will be inserted into |
100 |
C<swish-e> configuration file. See L<swish-config>. |
C<swish-e> configuration file. See C<swish-config>. |
101 |
|
|
102 |
=item C<memoize_to_xml> |
=item C<memoize_to_xml> |
103 |
|
|
109 |
|
|
110 |
my $iso2utf = Text::Iconv->new('ISO-8859-1','UTF-8'); |
my $iso2utf = Text::Iconv->new('ISO-8859-1','UTF-8'); |
111 |
|
|
112 |
sub open { |
sub open_index { |
113 |
my $class = shift; |
my $class = shift; |
114 |
my $self = {@_}; |
my $self = {@_}; |
115 |
bless($self, $class); |
bless($self, $class); |
150 |
|
|
151 |
my $slice = $self->put_slice($swishpath, $self->to_xml($data)); |
my $slice = $self->put_slice($swishpath, $self->to_xml($data)); |
152 |
|
|
|
# if ($err) { |
|
|
# carp "$swishpath: $err"; |
|
|
# return undef; |
|
|
# } |
|
|
|
|
153 |
return $slice; |
return $slice; |
154 |
} |
} |
155 |
|
|
159 |
|
|
160 |
$i->delete(@swishpath); |
$i->delete(@swishpath); |
161 |
|
|
162 |
|
B<This function is not implemented.> |
163 |
|
|
164 |
=cut |
=cut |
165 |
|
|
166 |
sub delete { |
sub delete { |
172 |
$self->{'paths'}->{$path} = DELETED; |
$self->{'paths'}->{$path} = DELETED; |
173 |
} |
} |
174 |
|
|
175 |
|
die "delete is not yet implemented"; |
176 |
|
|
177 |
return 42; |
return 42; |
178 |
} |
} |
179 |
|
|
200 |
my $ret = 0; |
my $ret = 0; |
201 |
|
|
202 |
foreach my $s (keys %{$self->{'slice'}}) { |
foreach my $s (keys %{$self->{'slice'}}) { |
203 |
print STDERR "closing slice $s\n"; |
$self->_debug("closing slice $s"); |
204 |
$ret += $self->close_slice($s); |
$ret += $self->close_slice($s); |
205 |
} |
} |
206 |
|
|
278 |
|
|
279 |
my $s = $i->in_slice('path/to/document/in/index'); |
my $s = $i->in_slice('path/to/document/in/index'); |
280 |
|
|
281 |
If there are C<slices> parametar to L<"open"> it will use |
If there are C<slices> parametar to L<"open_index"> it will use |
282 |
MD5 hash to spread documents across slices. That will produce random |
MD5 hash to spread documents across slices. That will produce random |
283 |
distribution of your documents in slices, which might or might not be best |
distribution of your documents in slices, which might or might not be best |
284 |
for your data. If you have to re-index large number of slices on each |
for your data. If you have to re-index large number of slices on each |
302 |
# first, pass path through slice_name function |
# first, pass path through slice_name function |
303 |
my $slice = &{$self->{'slice_name'}}($path); |
my $slice = &{$self->{'slice_name'}}($path); |
304 |
# then calculate MD5 hash |
# then calculate MD5 hash |
305 |
$slice = md5_hex($slice); |
my $hash = md5_hex($slice); |
306 |
# take first 8 chars to produce number |
# take first 8 chars to produce number |
307 |
# FIXME how random is this? |
# FIXME how random is this? |
308 |
$slice = hex(substr($slice,0,8)); |
$hash = hex(substr($hash,0,8)); |
309 |
|
|
310 |
$slice = ($slice % $self->{'slices'}) + 1; |
$slice = ($hash % $self->{'slices'}) + 1; |
311 |
print "hash: $slice / ",$self->{'slices'}," => $slice\n"; |
$self->_debug("hash: $hash / ",$self->{'slices'}," => $slice"); |
312 |
return $slice; |
return $slice; |
313 |
} else { |
} else { |
314 |
return &{$self->{'split'}}($path); |
return &{$self->{'split'}}($path); |
339 |
my $config_filename = $i->make_config('slice name'); |
my $config_filename = $i->make_config('slice name'); |
340 |
|
|
341 |
It returns configuration filename. If no C<swish_config> was defined in |
It returns configuration filename. If no C<swish_config> was defined in |
342 |
L<"open">, default swish-e configuration will be used. It will index all data for |
L<"open_index">, default swish-e configuration will be used. It will index all data for |
343 |
searching, but none for properties. |
searching, but none for properties. |
344 |
|
|
345 |
If you want to see what is allready defined for swish-e in configuration |
If you want to see what is allready defined for swish-e in configuration |
399 |
|
|
400 |
=head2 create_slice |
=head2 create_slice |
401 |
|
|
402 |
On first run, starts C<swish-e> using L<IPC::Run>. On subsequent calls just return |
On first run, starts C<swish-e>. On subsequent calls just return |
403 |
it's handles using L<Memoize>. |
it's handles using C<Memoize>. |
404 |
|
|
405 |
my $s = create_slice('/path/to/document'); |
my $s = create_slice('/path/to/document'); |
406 |
|
|
420 |
|
|
421 |
my $swish_config = $self->make_config($s); |
my $swish_config = $self->make_config($s); |
422 |
|
|
423 |
print STDERR "creating slice $s\n"; # FIXME |
my $swish = qq{| swish-e }; |
424 |
|
if (-f $self->{'index'}.'/'.$s) { |
425 |
|
$swish .= qq{ -u }; |
426 |
|
$self->{'slice'}->{$s}->{'update_mode'}++; |
427 |
|
} |
428 |
|
$swish .= qq{ -S prog -c } . $swish_config; |
429 |
|
|
430 |
my @swish = qw(swish-e -u -S prog -c); |
$self->_debug("creating slice $s using $swish"); |
|
push @swish, $swish_config; |
|
431 |
|
|
432 |
## Build the harness, open all pipes, and launch the subprocesses |
## Build the harness, open all pipes, and launch the subprocesses |
433 |
$self->{'slice'}->{$s}->{'h'} = start \@swish, |
open(my $fh, $swish) || croak "can't open $swish: $!"; |
|
\$self->{'slice'}->{$s}->{'in'}, |
|
|
\$self->{'slice'}->{$s}->{'out'}, |
|
|
\$self->{'slice'}->{$s}->{'err'}, |
|
|
timeout( 90 ); # FIXME |
|
434 |
|
|
435 |
$self->{'slice'}->{$s}->{'out_len'} = 0; |
$self->{'slice'}->{$s}->{'h'} = $fh; |
|
$self->{'slice'}->{$s}->{'err_len'} = 0; |
|
436 |
|
|
437 |
$self->slice_output($s); |
$self->slice_output($s); |
438 |
|
|
460 |
my $s = $self->create_slice($path) || confess "create_slice returned null"; |
my $s = $self->create_slice($path) || confess "create_slice returned null"; |
461 |
|
|
462 |
confess "no slice $s" unless(exists($self->{'slice'}) && exists($self->{'slice'}->{$s})); |
confess "no slice $s" unless(exists($self->{'slice'}) && exists($self->{'slice'}->{$s})); |
|
confess "no 'in' in slice $s: ".Dumper($s) unless (exists($self->{'slice'}->{$s}->{'in'})); |
|
463 |
confess "no 'h' in slice $s: ".Dumper($s) unless (exists($self->{'slice'}->{$s}->{'h'})); |
confess "no 'h' in slice $s: ".Dumper($s) unless (exists($self->{'slice'}->{$s}->{'h'})); |
464 |
|
|
465 |
$self->slice_output($s); |
$self->slice_output($s); |
466 |
|
|
467 |
use bytes; # as opposed to chars |
use bytes; # as opposed to chars |
468 |
$self->{'slice'}->{$s}->{'in'} .= |
my $fh = $self->{'slice'}->{$s}->{'h'} || confess "handle for slice $s undefined"; |
469 |
"Path-Name: $path\n". |
|
470 |
"Content-Length: ".(length($xml)+1)."\n". |
my $update_header = "Update-Mode: Index\n"; |
471 |
"Update-Mode: Index\n". |
$update_header = '' unless ($self->{'slice'}->{$s}->{'update_mode'}); |
|
"Document-Type: XML\n\n$xml\n"; |
|
472 |
|
|
473 |
# do I/O |
print { $fh } "Path-Name: $path\n". |
474 |
$self->{'slice'}->{$s}->{'h'}->pump while length $self->{'slice'}->{$s}->{'in'} ; # wait for all input to go |
"Content-Length: ".(length($xml)+1)."\n" . $update_header . |
475 |
|
"Document-Type: XML\n\n$xml\n"; |
476 |
|
|
477 |
$self->slice_output($s); |
$self->slice_output($s); |
478 |
|
|
479 |
|
$self->_debug("dumping in slice $s: $path"); |
480 |
|
|
481 |
$self->{'paths'}->{$path} = ADDED; |
$self->{'paths'}->{$path} = ADDED; |
482 |
|
|
483 |
return $s; |
return $s; |
491 |
|
|
492 |
Normally, you don't need to call it. |
Normally, you don't need to call it. |
493 |
|
|
494 |
|
B<This is dummy placeholder function for very old code that assumes this |
495 |
|
module is using C<IPC::Run> which it isn't any more.> |
496 |
|
|
497 |
=cut |
=cut |
498 |
|
|
499 |
sub slice_output { |
sub slice_output { |
502 |
my $s = shift || confess "slice_output needs slice"; |
my $s = shift || confess "slice_output needs slice"; |
503 |
|
|
504 |
confess "no slice $s" unless(exists($self->{'slice'}) && exists($self->{'slice'}->{$s})); |
confess "no slice $s" unless(exists($self->{'slice'}) && exists($self->{'slice'}->{$s})); |
|
confess "no 'in' in slice $s: ".Dumper($s) unless (exists($self->{'slice'}->{$s}->{'in'})); |
|
|
confess "no 'out' in slice $s: ".Dumper($s) unless (exists($self->{'slice'}->{$s}->{'out'})); |
|
505 |
|
|
506 |
if (length $self->{'slice'}->{$s}->{'out'} > $self->{'slice'}->{$s}->{'out_len'}) { |
# FIXME |
|
#print STDERR "swish-e OUT: ",$self->{'slice'}->{$s}->{'out'},"\n" if ($self->{'slice'}->{$s}->{'out'}); |
|
|
$self->{'slice'}->{$s}->{'out_len'} = length $self->{'slice'}->{$s}->{'out'}; |
|
|
return $s; |
|
|
} elsif (length $self->{'slice'}->{$s}->{'err'} > $self->{'slice'}->{$s}->{'err_len'}) { |
|
|
print STDERR "swish-e ERR: ",$self->{'slice'}->{$s}->{'err'},"\n" if ($self->{'slice'}->{$s}->{'err'}); |
|
|
$self->{'slice'}->{$s}->{'err_len'} = length $self->{'slice'}->{$s}->{'err'}; |
|
|
# this is fatal |
|
|
return undef; |
|
|
} |
|
507 |
|
|
508 |
return $s; |
return $s; |
509 |
} |
} |
527 |
confess "no 'h' in slice $s: ".Dumper($s) unless (exists($self->{'slice'}->{$s}->{'h'})); |
confess "no 'h' in slice $s: ".Dumper($s) unless (exists($self->{'slice'}->{$s}->{'h'})); |
528 |
|
|
529 |
# pump rest of content (if any) |
# pump rest of content (if any) |
530 |
$self->{'slice'}->{$s}->{'h'}->pump while length $self->{'slice'}->{$s}->{'in'}; |
close $self->{'slice'}->{$s}->{'h'} || carp "can't close slice $s: $!"; |
531 |
|
|
532 |
$self->slice_output($s); |
$self->slice_output($s); |
533 |
|
|
534 |
# clean up |
undef $self->{'slice'}->{$s}->{'h'}; |
|
$self->{'slice'}->{$s}->{'h'}->finish or confess "finish on slice $s returned $?: $! -- ",$self->{'slice'}->{$s}->{'err'}; |
|
535 |
|
|
536 |
delete($self->{'slice'}->{$s}) && return 1; |
delete($self->{'slice'}->{$s}) && return 1; |
537 |
return 0; |
return 0; |
544 |
|
|
545 |
my $xml = $i->to_xml({ foo => 'bar' }); |
my $xml = $i->to_xml({ foo => 'bar' }); |
546 |
|
|
547 |
This function is extracted from L<"add"> method so that you can L<Memoize> it. |
This function is extracted from L<"add"> method so that you can C<Memoize> it. |
548 |
If your data set has a lot of repeatable data, and memory is not a problem, you |
If your data set has a lot of repeatable data, and memory is not a problem, you |
549 |
can add C<memoize_to_xml> option to L<"open">. |
can add C<memoize_to_xml> option to L<"open_index">. |
550 |
|
|
551 |
=cut |
=cut |
552 |
|
|
571 |
$xml .= qq{</xml>}; |
$xml .= qq{</xml>}; |
572 |
} |
} |
573 |
|
|
574 |
|
sub _debug { |
575 |
|
my $self = shift; |
576 |
|
print STDERR "## ",@_,"\n" if ($self->{'debug'}); |
577 |
|
return; |
578 |
|
} |
579 |
|
|
580 |
1; |
1; |
581 |
__END__ |
__END__ |
582 |
|
|