blib/lib/Alvis/Buffer.pm untested

filter: Statementstmt 0.0% 0 / 98 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 150 CC 18 SCAR 41.5
CC
18
Cov
0%
fix
90.0
save
90.0
add
6.0
CRAP 63.2
1 # $Id: Buffer.pm,v 1.1 2006/12/01 09:40:24 buntine Exp $
2  
3 package Alvis::Buffer;
4  
5 use strict;
6 use warnings;
7 use Time::Simple;
8  
9 use encoding 'utf8';
10 use open ':utf8';
11 binmode STDIN, ":utf8";
12 binmode STDERR, ":utf8";
13  
14 our $VERSION = '0.10';
15  
16 =head1 NAME
17  
18 Alvis::Buffer - Perl extension for buffering utilities for the Alvis pipeline
19  
20 =head1 SYNOPSIS
21  
22 use Alvis::Buffer;
23 $Buffer::BUFFER = "/tmp/building.xml";
24 $Buffer::verbose++;
25 &Buffer::fix() or die "Cannot Buffer::fix";
26 $in = new Alvis::Pipeline::Read(host => "harvester.alvis.info",
27 port => 16716,
28 spooldir => "/home/alvis/spool");
29 while ($xml = $in->read(1)) {
30 &clean_wrapping(\$xml);
31 &Buffer::add($xml);
32 if ( $Buffer::docs>1000 ) {
33 $filename = &Buffer::save();
34 if ( !$filename ) {
35 &Buffer::close();
36 die "Cannot Buffer::save";
37 }
38 }
39 }
40 $filename = &Buffer::save();
41 &Buffer::close();
42  
43 =head1 DESCRIPTION
44  
45 This module provides a way of buffering Alvis XML into manageable chunks
46 as it is read in from a pipeline (Alvis::Pipeline).
47 Chunks can be controlled by file size or document count, but this is
48 done externally to the module, and the module simple provides a
49 function to save the current buffer contents.
50  
51 Files of collected Alvis XML documents, with appropriate XML header
52 and footer parts, are saved in the relative directory "xml-add/"
53 under numbers 1,2,3, ... At each time of storage, the current
54 directory is checked to see which number to use to store the latest
55 batch. If "xml-add/" is empty, then "xml/" is checked instead.
56 Presumably, files in "xml-add/" are being processed into "xml/".
57  
58 The implementation is independent of any pipeline,
59 and assumes a number of fixed directories.
60 Assumes files are in UTF-8, and that documents are present
61 in elements named <documentRecord>.
62  
63 =head1 FUNCTIONS
64  
65 =head2 fix()
66  
67 &Buffer::fix() or die "Cannot Buffer::fix";
68  
69 Basic initialisation and checking to ensure the output buffer
70 is OK, and have the current document count and size in memory.
71 Returns 1 if everything is OK, else 0.
72  
73 If runtime stops or aborts while the output buffer is
74 still being built, a restart will safely recover the
75 contents as long as no data was lost on the file.
76  
77 =head2 add()
78  
79 &Buffer::add($xml);
80  
81 Add an XML chunk to the current buffer and updates current
82 document count and size in memory.
83  
84 =head2 save()
85  
86 $filename = &Buffer::save();
87 if ( !$filename ) {
88 die "Cannot Buffer::save";
89 } else {
90 print STDERR "New XML file $filename saved\n";
91 }
92  
93 Save the current buffer into "xml-add/" as an appropriote integer
94 name, such as "xml-add/$N.xml", where $N will be determined at the point
95 of saving as the next biggest integer. Returns the filename used
96 if everything is OK, else returns undef. This needs to be called
97 explicitly so the variables $Buffer::docs and $Buffer:.size should be
98 checked to determine when this should be done.
99  
100 =head2 close()
101  
102 &Buffer::close();
103  
104 Close the output buffer.
105  
106 =head1 VARIABLES and PARAMETERS
107  
108 Global variables are of two kinds.
109 There are those intended to define
110 characteristics of general use. These should be set by the user
111 before the functions are used, but reasonable defaults are used.
112  
113 =head2 BUFFER
114  
115 name of the output buffer file that collects XML chunks. Don't
116 include the randomising string "$$" if you want this file to
117 be available during a restart.
118  
119 =head2 HEADER
120  
121 text to enter at the front of a sequence of <documentRecord> elements.
122  
123 =head2 FOOTER
124  
125 text to enter at the end of a sequence of <documentRecord> elements.
126  
127 =head2 verbose
128  
129 set to a non-zero value if more debugging shoulöd be reported to STDERR.
130  
131 Then the are variables that are read-only and define current buffer
132 statistics.
133  
134 =head2 size
135  
136 Count of characters in the current buffer.
137  
138 =head2 docs
139  
140 Count of documents, as determined by occurrences of <documentRecord> elements.
141  
142  
143 =head1 SEE ALSO
144  
145 Alvis::Pipeline
146  
147 =head1 AUTHOR
148  
149 Wray Buntine, E<lt>buntine@hiit.fiE<gt>
150  
151 =head1 COPYRIGHT AND LICENSE
152  
153 Copyright (C) 2006 by Wray Buntine.
154  
155 This library is free software; you can redistribute it and/or modify
156 it under the same terms as Perl itself, either Perl version 5.8.4 or,
157 at your option, any later version of Perl 5 you may have available.
158  
159 =cut
160  
161  
162  
163  
164 #############################################
165 # parameters - fixed per instance
166 our $BUFFER="building.xml-ish";
167 our $HEADER='<?xml version="1.0" encoding="UTF-8"?>
168 <documentCollection xmlns="http://alvis.info/enriched/" version="1.1">
169 ';
170 our $TRAILER='</documentCollection>
171 ';
172  
173 #############################################
174 # run time variables
175 our $docs = 0;
176 our $size = 0;
177 our $verbose = 0;
178  
179 ############################################
180 #
181 # add XML chunk
182 sub add() {
183 my $xml = shift();
184 my $xc = $xml;
185 my $count = $xc =~ s/<\/documentRecord>//g;
186 # now save
187 print ABUF $xml,"\n";
188 if ( $verbose ) {
189 my $tt = Time::Simple->new;
190 print STDERR "Docs of $count of size " . length($xml) . " added at time "
191 . $tt->format . "\n";
192 }
193 $docs += $count;
194 $size += length($xml);
195 }
196  
197 sub close() {
198 CORE::close(ABUF);
199 }
200  
201 ############################################
202 #
203 # make sure the output XML buffer file is in OK state
204 # and open it for append, as filehandle ABUF
205 # and set $docs, $size
206 # return 0 on fatal error, after printing error message
207 sub fix() {
208 $docs = 0;
209 $size = 0;
210 if ( ! -f $BUFFER ) {
211 # start new one
212 if ( ! open(ABUF,">>$BUFFER") ) {
213 print STDERR "Cannot open $BUFFER: $!\n";
214 return 0;
215 }
216 select((select(ABUF), $| = 1)[0]);
217 print ABUF $HEADER;
218 } else {
219 # check old one first
220 if ( ! open(ABUF,"<$BUFFER") ) {
221 print STDERR "Cannot open $BUFFER: $!\n";
222 return 0;
223 }
224 # its a UTF-8 file, so have to read it all
225 # since cannot start half way through
226 my $last = "";
227 my $last2 = "";
228 my $last3 = "";
229 while ( ($_=<ABUF>) ) {
230 if ( ! /^\s+$/ ) {
231 $last3 = $last2;
232 $last2 = $last;
233 $last = $_;
234 if ( /<\/documentRecord/ ) {
235 $docs++;
236 }
237 $size += length($_);
238 }
239 }
240 $last = $last3 . $last2 . $last;
241 if ( $last !~ /<documentCollection [^>]+>\s*$/
242 && $last !~ /<\/documentRecord>\s*$/ ) {
243 print STDERR "Output buffer $BUFFER in unstable state\n";
244 if ( $last =~ /<\/documentCollection>/ ) {
245 print STDERR " has been completed, so move manually\n";
246 }
247 return 0;
248 }
249 CORE::close(ABUF);
250 # now open for append
251 if ( ! open(ABUF,">>$BUFFER") ) {
252 print STDERR "Cannot open $BUFFER: $!\n";
253 return 0;
254 }
255 select((select(ABUF), $| = 1)[0]);
256 }
257 1;
258 }
259  
260 ############################################
261 #
262 # rename output XML buffer file to xml-add/N.xml for some N
263 # and create a new output XML buffer file, name is returned;
264 # return undef on fatal error, after printing error message
265 sub save() {
266 print ABUF $TRAILER;
267 CORE::close(ABUF);
268 # determine next available name
269 if ( ! opendir(XA,"xml-add") ) {
270 print STDERR "Cannot opendir xml-add/: $!\n";
271 return undef;
272 }
273 my $latest = 0;
274 while ( ($_=readdir(XA)) ) {
275 if ( /^([0-9]+).xml$/ ) {
276 if ( $latest < int($1) ) {
277 $latest = int($1);
278 }
279 }
280 }
281 closedir(XA);
282 if ( ! $latest ) {
283 if ( !opendir(XA,"xml") ) {
284 print STDERR "Cannot opendir xml/: $!\n";
285 return undef;
286 }
287 while ( ($_=readdir(XA)) ) {
288 if ( /^([0-9]+).xml$/ ) {
289 if ( $latest < int($1) ) {
290 $latest = int($1);
291 }
292 }
293 }
294 closedir(XA);
295 }
296 $latest++;
297 my $nf = "xml-add/$latest.xml";
298 # now save
299 rename($BUFFER,$nf);
300 if ( ! &fix() ) {
301 return undef;
302 }
303 return $nf;
304 }
305  
306 1;