7 |
our $VERSION = '0.02'; |
our $VERSION = '0.02'; |
8 |
|
|
9 |
use Carp; |
use Carp; |
10 |
|
use File::Temp qw/ tempdir /; |
11 |
|
|
12 |
=head1 NAME |
=head1 NAME |
13 |
|
|
122 |
|
|
123 |
} |
} |
124 |
|
|
125 |
|
=head2 index_document |
126 |
|
|
127 |
|
Quick way to add simple data to index. |
128 |
|
|
129 |
|
$i->index_document($key, $data); |
130 |
|
$i->index_document( 42 => 'meaning of life' ); |
131 |
|
|
132 |
|
=cut |
133 |
|
|
134 |
|
sub index_document { |
135 |
|
my $self = shift; |
136 |
|
|
137 |
|
my %doc = @_; |
138 |
|
|
139 |
|
foreach my $id (keys %doc) { |
140 |
|
$self->_create_doc( |
141 |
|
path => $id, |
142 |
|
body => $doc{$id}, |
143 |
|
); |
144 |
|
} |
145 |
|
|
146 |
|
return 1; |
147 |
|
} |
148 |
|
|
149 |
|
=head1 PRIVATE METHODS |
150 |
|
|
151 |
|
Private methods implement internals for creating temporary file needed for |
152 |
|
swish++. You should have no need to call them directly, and they are here |
153 |
|
just to have documentation. |
154 |
|
|
155 |
|
=head2 _init_index |
156 |
|
|
157 |
|
Create temporary directory in which files for indexing will be created and |
158 |
|
start index process. |
159 |
|
|
160 |
|
my $i->_init_index || die "can't start indexer"; |
161 |
|
|
162 |
|
=cut |
163 |
|
|
164 |
|
sub _init_index { |
165 |
|
my $self = shift; |
166 |
|
|
167 |
|
$self->{'tmp_dir'} = tempdir( CLEANUP => 1 ) || confess "can't create temporary directory: $!"; |
168 |
|
|
169 |
|
my $opt = "-v 4"; |
170 |
|
|
171 |
|
my $open_cmd = '| index '.$opt.' -e "html:*" -i '.$self->{'index_dir'}.'/index -'; |
172 |
|
|
173 |
|
chdir $self->{'tmp_dir'} || confess "can't chdir to ".$self->{'tmp_dir'}.": $!"; |
174 |
|
|
175 |
|
CORE::open($self->{'index_fh'}, $open_cmd) || confess "can't start index with $open_cmd: $!"; |
176 |
|
|
177 |
|
return $self->{'index_fh'}; |
178 |
|
} |
179 |
|
|
180 |
|
=head2 _create_doc |
181 |
|
|
182 |
|
Create temporary file and pass it's name to swish++ |
183 |
|
|
184 |
|
$i->_create_doc( |
185 |
|
path => 'path/to/store/in/index', |
186 |
|
body => 'data to story in body tag', |
187 |
|
meta => { |
188 |
|
'meta name' => 'data for this meta', |
189 |
|
'another' => 'again more data', |
190 |
|
} |
191 |
|
); |
192 |
|
|
193 |
|
=cut |
194 |
|
|
195 |
|
sub _create_doc { |
196 |
|
my $self = shift; |
197 |
|
|
198 |
|
my $arg = {@_}; |
199 |
|
|
200 |
|
# open indexer if needed |
201 |
|
$self->{'index_fh'} ||= $self->_init_index; |
202 |
|
|
203 |
|
my $path = $self->{'tmp_dir'} || confess "no tmp_dir?"; |
204 |
|
|
205 |
|
CORE::open(TMP, '>', $arg->{'path'}) || die "can't create temp file ".$arg->{'path'}.": $!"; |
206 |
|
|
207 |
|
print TMP '<html>'; |
208 |
|
|
209 |
|
if ($arg->{'meta'}) { |
210 |
|
confess "not yet implemented"; |
211 |
|
} |
212 |
|
|
213 |
|
print TMP '<body>' . ($arg->{'body'} || '') . '</body></html>'; |
214 |
|
|
215 |
|
close(TMP) || confess "can't close tmp file ".$arg->{'path'}.": $!"; |
216 |
|
|
217 |
|
print { $self->{'index_fh'} } $arg->{'path'}."\n"; |
218 |
|
} |
219 |
|
|
220 |
1; |
1; |
221 |
__END__ |
__END__ |
222 |
|
|