1 |
package jsFind; |
package jsFind; |
2 |
|
|
3 |
use 5.008004; |
use 5.005; |
4 |
use strict; |
use strict; |
5 |
use warnings; |
use warnings; |
6 |
|
use HTML::Entities; |
7 |
|
|
8 |
our $VERSION = '0.01'; |
our $VERSION = '0.03'; |
9 |
|
|
10 |
|
use Exporter 'import'; |
11 |
|
use Carp; |
12 |
|
|
13 |
|
our @ISA = qw(Exporter); |
14 |
|
|
15 |
|
BEGIN { |
16 |
|
import 'jsFind::Node'; |
17 |
|
} |
18 |
|
|
19 |
=head1 NAME |
=head1 NAME |
20 |
|
|
23 |
=head1 SYNOPSIS |
=head1 SYNOPSIS |
24 |
|
|
25 |
use jsFind; |
use jsFind; |
26 |
|
my $t = new jsFind(B => 4); |
27 |
|
my $f = 1; |
28 |
|
foreach my $k (qw{minima ut dolorem sapiente voluptatem}) { |
29 |
|
$t->B_search(Key => $k, |
30 |
|
Data => { |
31 |
|
"path" => { |
32 |
|
t => "word $k", |
33 |
|
f => $f }, |
34 |
|
}, |
35 |
|
Insert => 1, |
36 |
|
Append => 1, |
37 |
|
); |
38 |
|
} |
39 |
|
|
40 |
=head1 DESCRIPTION |
=head1 DESCRIPTION |
41 |
|
|
57 |
|
|
58 |
=back |
=back |
59 |
|
|
60 |
=head1 METHODS |
You can also examine examples which come as tests with this module, |
61 |
|
for example C<t/04words.t>. |
|
This module contains two packages C<jsFind> and C<jsFind::Node>. |
|
|
|
|
|
=head2 jsFind methods |
|
|
|
|
|
=cut |
|
|
|
|
|
use Exporter 'import'; |
|
|
use Carp; |
|
62 |
|
|
63 |
our @ISA = qw(Exporter); |
=head1 jsFind methods |
64 |
|
|
65 |
BEGIN { |
C<jsFind> is mode implementing methods which you, the user, are going to |
66 |
import 'jsFind::Node'; |
use to create indexes. |
|
} |
|
67 |
|
|
68 |
=head3 new |
=head2 new |
69 |
|
|
70 |
Create new tree. Arguments are C<B> which is maximum numbers of keys in |
Create new tree. Arguments are C<B> which is maximum numbers of keys in |
71 |
each node and optional C<Root> node. Each root node may have child nodes. |
each node and optional C<Root> node. Each root node may have child nodes. |
94 |
bless { B => $B, Root => $Root } => $package; |
bless { B => $B, Root => $Root } => $package; |
95 |
} |
} |
96 |
|
|
97 |
=head3 B_search |
=head2 B_search |
98 |
|
|
99 |
Search, insert, append or replace data in B-Tree |
Search, insert, append or replace data in B-Tree |
100 |
|
|
101 |
|
$t->B_search( |
102 |
|
Key => 'key value', |
103 |
|
Data => { "path" => { |
104 |
|
"t" => "title of document", |
105 |
|
"f" => 99, |
106 |
|
}, |
107 |
|
}, |
108 |
|
Insert => 1, |
109 |
|
Append => 1, |
110 |
|
); |
111 |
|
|
112 |
Semantics: |
Semantics: |
113 |
|
|
236 |
} |
} |
237 |
} |
} |
238 |
|
|
239 |
=head3 B |
=head2 B |
240 |
|
|
241 |
Return B (maximum number of keys) |
Return B (maximum number of keys) |
242 |
|
|
248 |
$_[0]{B}; |
$_[0]{B}; |
249 |
} |
} |
250 |
|
|
251 |
=head3 root |
=head2 root |
252 |
|
|
253 |
Returns root node |
Returns root node |
254 |
|
|
262 |
$self->{Root}; |
$self->{Root}; |
263 |
} |
} |
264 |
|
|
265 |
=head3 node_overfull |
=head2 node_overfull |
266 |
|
|
267 |
Returns if node is overfull |
Returns if node is overfull |
268 |
|
|
276 |
$node->size > $self->B; |
$node->size > $self->B; |
277 |
} |
} |
278 |
|
|
279 |
=head3 to_string |
=head2 to_string |
280 |
|
|
281 |
Returns your tree as formatted string. |
Returns your tree as formatted string. |
282 |
|
|
290 |
$_[0]->root->to_string; |
$_[0]->root->to_string; |
291 |
} |
} |
292 |
|
|
293 |
=head3 to_dot |
=head2 to_dot |
294 |
|
|
295 |
Create Graphviz graph of your tree |
Create Graphviz graph of your tree |
296 |
|
|
308 |
return $dot; |
return $dot; |
309 |
} |
} |
310 |
|
|
311 |
=head3 to_jsfind |
=head2 to_jsfind |
312 |
|
|
313 |
Create xml index files for jsFind. This should be called after |
Create xml index files for jsFind. This should be called after |
314 |
your B-Tree has been filled with data. |
your B-Tree has been filled with data. |
317 |
|
|
318 |
Returns number of nodes in created tree. |
Returns number of nodes in created tree. |
319 |
|
|
320 |
|
There is also longer version if you want to recode your data charset |
321 |
|
into different one (probably UTF-8): |
322 |
|
|
323 |
|
$root->to_jsfind('/full/path/to/index/dir/','ISO-8859-2','UTF-8'); |
324 |
|
|
325 |
|
Destination encoding is UTF-8 by default, so you don't have to specify it. |
326 |
|
|
327 |
|
$root->to_jsfind('/full/path/to/index/dir/','WINDOWS-1250'); |
328 |
|
|
329 |
=cut |
=cut |
330 |
|
|
331 |
|
my $iconv; |
332 |
|
my $iconv_l1; |
333 |
|
|
334 |
sub to_jsfind { |
sub to_jsfind { |
335 |
my $self = shift; |
my $self = shift; |
336 |
|
|
337 |
my $path = shift || confess "to_jsfind need path to your index!"; |
my $path = shift || confess "to_jsfind need path to your index!"; |
338 |
|
|
339 |
|
my ($from_cp,$to_cp) = @_; |
340 |
|
|
341 |
|
$to_cp ||= 'UTF-8'; |
342 |
|
|
343 |
|
if ($from_cp && $to_cp) { |
344 |
|
$iconv = Text::Iconv->new($from_cp,$to_cp); |
345 |
|
} |
346 |
|
$iconv_l1 = Text::Iconv->new('ISO-8859-1',$to_cp); |
347 |
|
|
348 |
$path .= "/" if ($path =~ /\/$/); |
$path .= "/" if ($path =~ /\/$/); |
349 |
carp "can't create index in '$path': $!" if (! -w $path); |
#carp "creating directory for index '$path'" if (! -w $path); |
350 |
|
|
351 |
return $self->root->to_jsfind($path,"0"); |
return $self->root->to_jsfind($path,"0"); |
352 |
} |
} |
357 |
$_[0] cmp $_[1]; |
$_[0] cmp $_[1]; |
358 |
} |
} |
359 |
|
|
360 |
|
=head2 _recode |
361 |
|
|
362 |
|
This is internal function to recode charset. |
363 |
|
|
364 |
|
It will also try to decode entities in data using L<HTML::Entities>. |
365 |
|
|
366 |
|
=cut |
367 |
|
|
368 |
|
sub _recode { |
369 |
|
my $self = shift; |
370 |
|
my $text = shift || return; |
371 |
|
|
372 |
|
sub _decode_html_entities { |
373 |
|
my $data = shift || return; |
374 |
|
$data = $iconv_l1->convert(decode_entities($data)) || croak "entity decode problem: $data"; |
375 |
|
} |
376 |
|
|
377 |
|
if ($iconv) { |
378 |
|
$text = $iconv->convert($text) || $text && carp "convert problem: $text"; |
379 |
|
$text =~ s/(\&\w+;)/_decode_html_entities($1)/ges; |
380 |
|
} |
381 |
|
|
382 |
|
return $text; |
383 |
|
} |
384 |
|
|
385 |
##################################################################### |
##################################################################### |
386 |
|
|
387 |
=head2 jsFind::Node methods |
=head1 jsFind::Node methods |
388 |
|
|
389 |
Each node has C<k> key-data pairs, with C<B> <= C<k> <= C<2B>, and |
Each node has C<k> key-data pairs, with C<B> <= C<k> <= C<2B>, and |
390 |
each has C<k+1> subnodes, which might be null. |
each has C<k+1> subnodes, which might be null. |
406 |
|
|
407 |
use Carp; |
use Carp; |
408 |
use File::Path; |
use File::Path; |
409 |
|
use Text::Iconv; |
410 |
|
|
411 |
|
use base 'jsFind'; |
412 |
|
|
413 |
my $KEYS = 0; |
my $KEYS = 0; |
414 |
my $DATA = 1; |
my $DATA = 1; |
415 |
my $SUBNODES = 2; |
my $SUBNODES = 2; |
416 |
|
|
417 |
=head3 new |
=head2 new |
418 |
|
|
419 |
Create New node |
Create New node |
420 |
|
|
434 |
bless [@_] => $package; |
bless [@_] => $package; |
435 |
} |
} |
436 |
|
|
437 |
=head3 locate_key |
=head2 locate_key |
438 |
|
|
439 |
Locate key in node using linear search. This should probably be replaced |
Locate key in node using linear search. This should probably be replaced |
440 |
by binary search for better performance. |
by binary search for better performance. |
471 |
} |
} |
472 |
|
|
473 |
|
|
474 |
=head3 emptynode |
=head2 emptynode |
475 |
|
|
476 |
Creates new empty node |
Creates new empty node |
477 |
|
|
484 |
new($_[0]); # Pass package name, but not anything else. |
new($_[0]); # Pass package name, but not anything else. |
485 |
} |
} |
486 |
|
|
487 |
=head3 is_empty |
=head2 is_empty |
488 |
|
|
489 |
Test if node is empty |
Test if node is empty |
490 |
|
|
498 |
!defined($self) || $#$self < 0; |
!defined($self) || $#$self < 0; |
499 |
} |
} |
500 |
|
|
501 |
=head3 key |
=head2 key |
502 |
|
|
503 |
Return C<$i>th key from node |
Return C<$i>th key from node |
504 |
|
|
514 |
$_[0]->[$KEYS][$_[1]]; |
$_[0]->[$KEYS][$_[1]]; |
515 |
} |
} |
516 |
|
|
517 |
=head3 data |
=head2 data |
518 |
|
|
519 |
Return C<$i>th data from node |
Return C<$i>th data from node |
520 |
|
|
527 |
$self->[$DATA][$n]; |
$self->[$DATA][$n]; |
528 |
} |
} |
529 |
|
|
530 |
=head3 kdp_replace |
=head2 kdp_replace |
531 |
|
|
532 |
Set key data pair for C<$i>th element in node |
Set key data pair for C<$i>th element in node |
533 |
|
|
548 |
$self->[$DATA][$n]]; |
$self->[$DATA][$n]]; |
549 |
} |
} |
550 |
|
|
551 |
=head3 kdp_insert |
=head2 kdp_insert |
552 |
|
|
553 |
|
Insert key/data pair in tree |
554 |
|
|
555 |
# No return value. |
$node->kdp_insert("key value" => "data value"); |
556 |
|
|
557 |
|
No return value. |
558 |
|
|
559 |
=cut |
=cut |
560 |
|
|
573 |
splice(@{$self->[$SUBNODES]}, $where, 0, undef); |
splice(@{$self->[$SUBNODES]}, $where, 0, undef); |
574 |
} |
} |
575 |
|
|
576 |
=head3 kdp_append |
=head2 kdp_append |
577 |
|
|
578 |
Adds new data keys and values to C<$i>th element in node |
Adds new data keys and values to C<$i>th element in node |
579 |
|
|
594 |
$self->[$DATA][$n]]; |
$self->[$DATA][$n]]; |
595 |
} |
} |
596 |
|
|
597 |
=head3 subnode |
=head2 subnode |
598 |
|
|
599 |
Set new or return existing subnode |
Set new or return existing subnode |
600 |
|
|
612 |
$self->[$SUBNODES][$n]; |
$self->[$SUBNODES][$n]; |
613 |
} |
} |
614 |
|
|
615 |
=head3 is_leaf |
=head2 is_leaf |
616 |
|
|
617 |
Test if node is leaf |
Test if node is leaf |
618 |
|
|
625 |
! defined $self->[$SUBNODES][0]; # undefined subnode means leaf node. |
! defined $self->[$SUBNODES][0]; # undefined subnode means leaf node. |
626 |
} |
} |
627 |
|
|
628 |
=head3 size |
=head2 size |
629 |
|
|
630 |
Return number of keys in the node |
Return number of keys in the node |
631 |
|
|
638 |
return scalar(@{$self->[$KEYS]}); |
return scalar(@{$self->[$KEYS]}); |
639 |
} |
} |
640 |
|
|
641 |
=head3 halves |
=head2 halves |
642 |
|
|
643 |
|
Split node into two halves so that keys C<0 .. $n-1> are in one node |
644 |
|
and keys C<$n+1 ... $size> are in the other. |
645 |
|
|
646 |
# Accept an index $n |
my ($left_node, $right_node, $kdp) = $node->halves($n); |
|
# Divide into two nodes so that keys 0 .. $n-1 are in one node |
|
|
# and keys $n+1 ... $size are in the other. |
|
647 |
|
|
648 |
=cut |
=cut |
649 |
|
|
667 |
($self->new(@left), $self->new(@right), \@middle); |
($self->new(@left), $self->new(@right), \@middle); |
668 |
} |
} |
669 |
|
|
670 |
=head3 to_string |
=head2 to_string |
671 |
|
|
672 |
Dumps tree as string |
Dumps tree as string |
673 |
|
|
718 |
|
|
719 |
=end comment |
=end comment |
720 |
|
|
721 |
=head3 to_dot |
=head2 to_dot |
722 |
|
|
723 |
Recursivly walk nodes of tree |
Recursivly walk nodes of tree |
724 |
|
|
757 |
$dot; |
$dot; |
758 |
} |
} |
759 |
|
|
760 |
=head3 to_jsfind |
=head2 to_xml |
761 |
|
|
762 |
|
Escape <, >, & and ", and to produce valid XML |
763 |
|
|
764 |
|
=cut |
765 |
|
|
766 |
|
my %escape = ('<'=>'<', '>'=>'>', '&'=>'&', '"'=>'"'); |
767 |
|
my $escape_re = join '|' => keys %escape; |
768 |
|
|
769 |
|
sub to_xml { |
770 |
|
my $self = shift || confess "you should call to_xml as object!"; |
771 |
|
|
772 |
|
my $d = shift || return; |
773 |
|
$d = $self->SUPER::_recode($d); |
774 |
|
confess "escape_re undefined!" unless ($escape_re); |
775 |
|
$d =~ s/($escape_re)/$escape{$1}/g; |
776 |
|
return $d; |
777 |
|
} |
778 |
|
|
779 |
|
=head2 to_jsfind |
780 |
|
|
781 |
Create jsFind xml files |
Create jsFind xml files |
782 |
|
|
783 |
my $nr=$tree->to_dot('/path/to/index','0'); |
my $nr=$tree->to_jsfind('/path/to/index','0'); |
784 |
|
|
785 |
Returns number of elements created |
Returns number of elements created |
786 |
|
|
787 |
=cut |
=cut |
788 |
|
|
789 |
|
|
790 |
sub to_jsfind { |
sub to_jsfind { |
791 |
my $self = shift; |
my $self = shift; |
792 |
my ($path,$file) = @_; |
my ($path,$file) = @_; |
793 |
|
|
794 |
return 0 if $self->is_empty; |
return 0 if $self->is_empty; |
795 |
|
|
796 |
|
confess("path is undefined.") unless ($path); |
797 |
|
confess("file is undefined. Did you call \$t->root->to_jsfind(..) instead of \$t->to_jsfind(..) ?") unless (defined($file)); |
798 |
|
|
799 |
my $nr_keys = 0; |
my $nr_keys = 0; |
800 |
|
|
801 |
my ($k, $d, $s) = @$self; |
my ($k, $d, $s) = @$self; |
807 |
my $key = lc($k->[$i]); |
my $key = lc($k->[$i]); |
808 |
|
|
809 |
if ($key) { |
if ($key) { |
810 |
$key_xml .= qq{<k>$key</k>}; |
$key_xml .= '<k>'.$self->to_xml($key).'</k>'; |
811 |
$data_xml .= qq{<e>}; |
$data_xml .= '<e>'; |
812 |
#use Data::Dumper; |
#use Data::Dumper; |
813 |
#print Dumper($d->[$i]); |
#print Dumper($d->[$i]); |
814 |
foreach my $path (keys %{$d->[$i]}) { |
foreach my $path (keys %{$d->[$i]}) { |
815 |
$data_xml .= '<l f="'.($d->[$i]->{$path}->{'f'} || 1).'" t="'.($d->[$i]->{$path}->{'t'} || 'no title').'">'.$path.'</l>'; |
$data_xml .= '<l f="'.($d->[$i]->{$path}->{'f'} || 1).'" t="'.$self->to_xml($d->[$i]->{$path}->{'t'} || 'no title').'">'.$self->to_xml($path).'</l>'; |
816 |
$nr_keys++; |
$nr_keys++; |
817 |
} |
} |
818 |
$data_xml .= qq{</e>}; |
$data_xml .= '</e>'; |
819 |
} |
} |
820 |
|
|
821 |
$nr_keys += $s->[$i]->to_jsfind("$path/$file","$i") if ($s->[$i]); |
$nr_keys += $s->[$i]->to_jsfind("$path/$file","$i") if ($s->[$i]); |
822 |
} |
} |
823 |
|
|
824 |
$key_xml .= "</n>"; |
$key_xml .= '</n>'; |
825 |
$data_xml .= "</d>"; |
$data_xml .= '</d>'; |
826 |
|
|
827 |
if (! -e $path) { |
if (! -e $path) { |
828 |
mkpath($path) || croak "can't create dir '$path': $!"; |
mkpath($path) || croak "can't create dir '$path': $!"; |
849 |
|
|
850 |
B-Trees in perl web site L<http://perl.plover.com/BTree/> |
B-Trees in perl web site L<http://perl.plover.com/BTree/> |
851 |
|
|
852 |
|
This module web site L<http://www.rot13.org/~dpavlin/jsFind.html> |
853 |
|
|
854 |
=head1 AUTHORS |
=head1 AUTHORS |
855 |
|
|
856 |
Mark-Jonson Dominus E<lt>mjd@pobox.comE<gt> wrote C<BTree.pm> which was |
Mark-Jonson Dominus E<lt>mjd@pobox.comE<gt> wrote C<BTree.pm> which was |