/[webpac]/trunk2/lib/WebPAC/jsFind.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

Contents of /trunk2/lib/WebPAC/jsFind.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 445 - (show annotations)
Tue Sep 14 21:02:28 2004 UTC (16 years ago) by dpavlin
File size: 3949 byte(s)
create GraphViz files for debug

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 Turning debugging for this function by inserting
149
150 log4perl.logger.WebPAC.jsFind.close=DEBUG
151
152 into C<log.conf> will also result in creation of GraphViz C<.dot> files
153 for each index in current directory.
154
155 =cut
156
157 sub close {
158 my $self = shift;
159
160 my $log = $self->_get_logger();
161
162 foreach my $index_name (keys %{$self->{'tree'}}) {
163 my $path = $self->{'index_path'}."/".$index_name;
164
165 $log->debug("saving index '$index_name' xml files to '$path'");
166
167 $self->tree($index_name)->to_jsfind($path,'ISO-8859-2','UTF-8');
168
169 if ($log->is_debug()) {
170 my $dot_file = $index_name.".dot";
171
172 $log->debug("saving graphviz file for '$index_name' to '$dot_file'");
173
174 open(DOT, ">", $dot_file) || $log->logdie("can't open '$dot_file': $!");
175 print DOT $self->tree($index_name)->to_dot;
176 close(DOT);
177 }
178 }
179
180 }
181
182 #
183
184 =head1 INTERNAL METHODS
185
186 You shouldn't call this methods directly.
187
188 =head2 _get_logger
189
190 Get C<Log::Log4perl> object with a twist: domains are defined for each
191 method
192
193 my $log = $webpac->_get_logger();
194
195 =cut
196
197 sub _get_logger {
198 my $self = shift;
199
200 my $name = (caller(1))[3] || caller;
201 return get_logger($name);
202 }
203 1;

  ViewVC Help
Powered by ViewVC 1.1.26