1 |
package WebPAC::jsFind; |
2 |
|
3 |
use warnings; |
4 |
use strict; |
5 |
|
6 |
use Carp; |
7 |
use jsFind 0.04; |
8 |
use Log::Log4perl qw(get_logger :levels); |
9 |
|
10 |
=head1 NAME |
11 |
|
12 |
WebPAC::jsFind - create jsFind index instead of swish-e |
13 |
|
14 |
=head1 DESCRIPTION |
15 |
|
16 |
This module will create jsFind index, which is static B-Tree index |
17 |
searchable by JavaScript. It's very useful if you want to build |
18 |
CD-ROM with static content and search engine. |
19 |
|
20 |
=head1 METHODS |
21 |
|
22 |
=head2 new |
23 |
|
24 |
Create new index object |
25 |
|
26 |
my $index = new WebPAC::jsFind( |
27 |
index_path => '/path/to/jsFind/index', |
28 |
keys => 10, |
29 |
log => 'log4perl.conf', |
30 |
); |
31 |
|
32 |
C<index> is path to location where jsFind index should be created. |
33 |
|
34 |
C<keys> is optional parametar which specify number of keys in each node |
35 |
(which has to be even number). Default is 10. |
36 |
|
37 |
C<log> is optional parametar which specify filename of L<Log::Log4Perl> |
38 |
config file. Default is C<log.conf>. |
39 |
|
40 |
=cut |
41 |
|
42 |
sub new { |
43 |
my $class = shift; |
44 |
my $self = {@_}; |
45 |
bless($self, $class); |
46 |
|
47 |
confess "need index_path argument!" unless ($self->{'index_path'}); |
48 |
|
49 |
my $log_file = $self->{'log'} || "log.conf"; |
50 |
Log::Log4perl->init($log_file); |
51 |
|
52 |
return $self; |
53 |
} |
54 |
|
55 |
=head2 tree |
56 |
|
57 |
Create or retreive jsFind tree object |
58 |
|
59 |
$index->tree('index_name'); |
60 |
|
61 |
=cut |
62 |
|
63 |
sub tree { |
64 |
my $self = shift; |
65 |
|
66 |
my $index_name = shift || confess "need index name!"; |
67 |
|
68 |
if (! $self->{'tree'}->{$index_name}) { |
69 |
$self->{'tree'}->{$index_name} = new jsFind(B => $self->{keys} || 10); |
70 |
my $log = $self->_get_logger(); |
71 |
$log->debug("tree object $index_name created"); |
72 |
|
73 |
} |
74 |
|
75 |
return $self->{'tree'}->{$index_name}; |
76 |
|
77 |
} |
78 |
|
79 |
=head2 insert |
80 |
|
81 |
Insert data into index |
82 |
|
83 |
$index->insert( |
84 |
index_name => 'index_name', |
85 |
path => 'path', |
86 |
headline => 'headline text', |
87 |
words => 'words to insert into index' |
88 |
); |
89 |
|
90 |
=cut |
91 |
|
92 |
sub insert { |
93 |
my $self = shift; |
94 |
|
95 |
my $args = {@_}; |
96 |
|
97 |
my $log = $self->_get_logger(); |
98 |
|
99 |
confess "need index name" unless ($args->{'index_name'}); |
100 |
confess "need path" unless ($args->{'path'}); |
101 |
if (! $args->{'headline'}) { |
102 |
carp "no headline for ",$args->{'path'}," ?"; |
103 |
$args->{'headline'} = "no headline: ".$args->{'path'}; |
104 |
} |
105 |
return unless (defined($args->{'words'})); |
106 |
|
107 |
my $words = $args->{'words'}; |
108 |
|
109 |
# chop leading and trailing spaces |
110 |
$words =~ s/^\s+//; |
111 |
$words =~ s/\s+$//; |
112 |
|
113 |
my %usage; |
114 |
foreach (split(/\s+/,$words)) { |
115 |
$usage{$_}++; |
116 |
} |
117 |
|
118 |
$log->debug("inserting '$words'", |
119 |
" into index ",$args->{'index_name'}, |
120 |
" headline: ",$args->{'headline'}, |
121 |
" path: ",$args->{'path'} |
122 |
); |
123 |
|
124 |
foreach my $word (keys %usage) { |
125 |
|
126 |
$self->tree($args->{'index_name'})->B_search( |
127 |
Key => $word, |
128 |
Data => { $args->{'path'} => { |
129 |
t => $args->{'headline'}, |
130 |
f => $usage{$word}, |
131 |
}, |
132 |
}, |
133 |
Insert => 1, |
134 |
Append => 1, |
135 |
); |
136 |
} |
137 |
} |
138 |
|
139 |
=head2 close |
140 |
|
141 |
This method will dump indexes to disk. |
142 |
|
143 |
$index->close; |
144 |
|
145 |
This method will create directories if needed and store tree xml files |
146 |
for all indexes. |
147 |
|
148 |
|
149 |
=cut |
150 |
|
151 |
sub close { |
152 |
my $self = shift; |
153 |
|
154 |
my $log = $self->_get_logger(); |
155 |
|
156 |
foreach my $index_name (keys %{$self->{'tree'}}) { |
157 |
my $path = $self->{'index_path'}."/".$index_name; |
158 |
|
159 |
$log->debug("saving index '$index_name' xml files to '$path'"); |
160 |
|
161 |
$self->tree($index_name)->to_jsfind($path,'ISO-8859-2','UTF-8'); |
162 |
} |
163 |
|
164 |
} |
165 |
|
166 |
# |
167 |
|
168 |
=head1 INTERNAL METHODS |
169 |
|
170 |
You shouldn't call this methods directly. |
171 |
|
172 |
=head2 _get_logger |
173 |
|
174 |
Get C<Log::Log4perl> object with a twist: domains are defined for each |
175 |
method |
176 |
|
177 |
my $log = $webpac->_get_logger(); |
178 |
|
179 |
=cut |
180 |
|
181 |
sub _get_logger { |
182 |
my $self = shift; |
183 |
|
184 |
my $name = (caller(1))[3] || caller; |
185 |
return get_logger($name); |
186 |
} |
187 |
1; |