NOTEDB/binary.pm untested

filter: Statementstmt 0.0% 0 / 255 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 37 MC/DCmcdc 0.0% 0 / 20 Subroutinesub 0.0% 0 / 26 Podpod 0.0% 0 / 18 totaltotal 0.0% 0 / 384 CC 15 SCAR 21.0
CC
15
Cov
0%
get_search
30.0
new
12.0
_retrieve
12.0
CRAP 8.2
1 #!/usr/bin/perl
2 # $Id: binary.pm,v 1.3 2000/08/11 00:05:58 zarahg Exp $
3 # Perl module for note
4 # binary database backend. see docu: perldoc NOTEDB::binary
5 #
6 package NOTEDB::binary;
7  
8 $NOTEDB::binary::VERSION = "1.13";
9  
10 use strict;
11 use IO::Seekable;
12 use File::Spec;
13 use FileHandle;
14 use Fcntl qw(LOCK_EX LOCK_UN);
15  
16 use NOTEDB;
17 use Exporter ();
18 use vars qw(@ISA @EXPORT);
19 @ISA = qw(NOTEDB Exporter);
20  
21  
22  
23  
24 sub new {
25 my($this, %param) = @_;
26  
27 my $class = ref($this) || $this;
28 my $self = {};
29 bless($self,$class);
30  
31 $self->{NOTEDB} = $self->{dbname} = $param{dbname} || File::Spec->catfile($ENV{HOME}, ".notedb");
32 my $MAX_NOTE = $param{MaxNoteByte} || 4096;
33 my $MAX_TIME = $param{MaxTimeByte} || 64;
34  
35 if(! -e $self->{NOTEDB}) {
36 open(TT,">$self->{NOTEDB}") or die "Could not create $self->{NOTEDB}: $!\n";
37 close (TT);
38 }
39 elsif(! -w $self->{NOTEDB}) {
40 print "$self->{NOTEDB} is not writable!\n";
41 exit(1);
42 }
43  
44  
45 my $TYPEDEF = "i a$MAX_NOTE a$MAX_TIME";
46 my $SIZEOF = length pack($TYPEDEF, () );
47  
48 $self->{sizeof} = $SIZEOF;
49 $self->{typedef} = $TYPEDEF;
50 $self->{maxnote} = $MAX_NOTE;
51 $self->{LOCKFILE} = $self->{NOTEDB} . "~LOCK";
52  
53 return $self;
54 }
55  
56  
57 sub DESTROY
58 {
59 # clean the desk!
60 }
61  
62 sub version {
63 my $this = shift;
64 return $NOTEDB::binary::VERSION;
65 }
66  
67  
68  
69 sub set_del_all
70 {
71 my $this = shift;
72 unlink $this->{NOTEDB};
73 open(TT,">$this->{NOTEDB}") or die "Could not create $this->{NOTEDB}: $!\n";
74 close (TT);
75 }
76  
77  
78 sub get_single {
79 my($this, $num) = @_;
80 my($address, $note, $date, $n, $t, $buffer, );
81  
82 open NOTE, "+<$this->{NOTEDB}" or die "could not open $this->{NOTEDB}\n";
83 flock NOTE, LOCK_EX;
84  
85 $address = ($num-1) * $this->{sizeof};
86 seek(NOTE, $address, IO::Seekable::SEEK_SET);
87 read(NOTE, $buffer, $this->{sizeof});
88 ($num, $n, $t) = unpack($this->{typedef}, $buffer);
89  
90 $note = $this->ude($n);
91 $date = $this->ude($t);
92  
93 flock NOTE, LOCK_UN;
94 close NOTE;
95  
96 return $note, $date;
97 }
98  
99  
100 sub get_all
101 {
102 my $this = shift;
103 my($num, $note, $date, %res);
104  
105 if ($this->unchanged) {
106 return %{$this->{cache}};
107 }
108 open NOTE, "+<$this->{NOTEDB}" or die "could not open $this->{NOTEDB}\n";
109 flock NOTE, LOCK_EX;
110 my($buffer, $t, $n);
111 seek(NOTE, 0, 0); # START FROM BEGINNING
112 while(read(NOTE, $buffer, $this->{sizeof})) {
113 ($num, $note, $date) = unpack($this->{typedef}, $buffer);
114 $t = $this->ude($date);
115 $n = $this->ude($note);
116 $res{$num}->{'note'} = $n;
117 $res{$num}->{'date'} = $t;
118 }
119 flock NOTE, LOCK_UN;
120 close NOTE;
121  
122 $this->cache(%res);
123 return %res;
124 }
125  
126 sub import_data {
127 my ($this, $data) = @_;
128 foreach my $num (sort keys %{$data}) {
129 my $pos = $this->get_nextnum();
130 $this->set_edit($pos, $data->{$num}->{note}, $data->{$num}->{date});
131 }
132 }
133  
134 sub get_nextnum
135 {
136 my $this = shift;
137 my($num, $te, $me, $buffer);
138  
139 if ($this->unchanged) {
140 $num = 1;
141 foreach (keys %{$this->{cache}}) {
142 $num++;
143 }
144 return $num;
145 }
146 open NOTE, "+<$this->{NOTEDB}" or die "could not open $this->{NOTEDB}\n";
147 flock NOTE, LOCK_EX;
148  
149 seek(NOTE, 0, 0); # START FROM BEGINNING
150 while(read(NOTE, $buffer, $this->{sizeof})) {
151 ($num, $te, $me) = unpack($this->{typedef}, $buffer);
152 }
153 $num += 1;
154 flock NOTE, LOCK_UN;
155 close NOTE;
156  
157 return $num;
158 }
159  
160 sub get_search
161 {
162 my($this, $searchstring) = @_;
163 my($buffer, $num, $note, $date, %res, $t, $n, $match);
164  
165 my $regex = $this->generate_search($searchstring);
166 eval $regex;
167 if ($@) {
168 print "invalid expression: \"$searchstring\"!\n";
169 return;
170 }
171 $match = 0;
172  
173 if ($this->unchanged) {
174 foreach my $num (keys %{$this->{cache}}) {
175 $_ = $this->{cache}{$num}->{note};
176 eval $regex;
177 if ($match) {
178 $res{$num}->{note} = $this->{cache}{$num}->{note};
179 $res{$num}->{date} = $this->{cache}{$num}->{date}
180 }
181 $match = 0;
182 }
183 return %res;
184 }
185  
186 open NOTE, "+<$this->{NOTEDB}" or die "could not open $this->{NOTEDB}\n";
187 flock NOTE, LOCK_EX;
188  
189 seek(NOTE, 0, 0); # START FROM BEGINNING
190 while(read(NOTE, $buffer, $this->{sizeof})) {
191 ($num, $note, $date) = unpack($this->{typedef}, $buffer);
192 $n = $this->ude($note);
193 $t = $this->ude($date);
194 $_ = $n;
195 eval $regex;
196 if($match)
197 {
198 $res{$num}->{'note'} = $n;
199 $res{$num}->{'date'} = $t;
200 }
201 $match = 0;
202 }
203 flock NOTE, LOCK_UN;
204 close NOTE;
205  
206 return %res;
207 }
208  
209  
210  
211  
212 sub set_edit {
213 my($this, $num, $note, $date) = @_;
214  
215 $this->warn_if_too_big($note, $num);
216  
217 my $address = ($num -1 ) * $this->{sizeof};
218  
219 open NOTE, "+<$this->{NOTEDB}" or die "could not open $this->{NOTEDB}\n";
220 flock NOTE, LOCK_EX;
221  
222 seek(NOTE, $address, IO::Seekable::SEEK_SET);
223 my $n = $this->uen($note);
224 my $t = $this->uen($date);
225  
226 my $buffer = pack($this->{typedef}, $num, $n, $t);
227 print NOTE $buffer;
228  
229 flock NOTE, LOCK_UN;
230 close NOTE;
231  
232 $this->changed;
233 }
234  
235  
236 sub set_new {
237 my($this, $num, $note, $date) = @_;
238  
239 $this->warn_if_too_big($note, $num);
240  
241 open NOTE, "+<$this->{NOTEDB}" or die "could not open $this->{NOTEDB}\n";
242 flock NOTE, LOCK_EX;
243  
244 seek(NOTE, 0, IO::Seekable::SEEK_END); # APPEND
245 my $n = $this->uen($note);
246 my $t = $this->uen($date);
247 my $buffer = pack($this->{typedef}, $num, $n, $t);
248 print NOTE $buffer;
249  
250 flock NOTE, LOCK_UN;
251 close NOTE;
252  
253 $this->changed;
254 }
255  
256  
257 sub set_del
258 {
259 my($this, $num) = @_;
260 my(%orig, $note, $date, $T, $setnum, $buffer, $n, $N, $t);
261  
262 $setnum = 1;
263  
264 %orig = $this->get_all();
265 return "ERROR" if (! exists $orig{$num});
266  
267 delete $orig{$num};
268  
269 # overwrite, but keep number!
270 open NOTE, ">$this->{NOTEDB}" or die "could not open $this->{NOTEDB}\n";
271 flock NOTE, LOCK_EX;
272 seek(NOTE, 0, 0); # START FROM BEGINNING
273 foreach $N (keys %orig) {
274 $n = $this->uen($orig{$N}->{'note'});
275 $t = $this->uen($orig{$N}->{'date'});
276 $buffer = pack( $this->{typedef}, $N, $n, $t);
277 # keep orig number, note have to call recount!
278 print NOTE $buffer;
279 seek(NOTE, 0, IO::Seekable::SEEK_END);
280 $setnum++;
281 }
282 flock NOTE, LOCK_UN;
283 close NOTE;
284  
285 $this->changed;
286  
287 return;
288 }
289  
290 sub set_recountnums
291 {
292 my($this) = @_;
293 my(%orig, $note, $date, $T, $setnum, $buffer, $n, $N, $t);
294  
295 $setnum = 1;
296 %orig = $this->get_all();
297  
298 open NOTE, ">$this->{NOTEDB}" or die "could not open $this->{NOTEDB}\n";
299 flock NOTE, LOCK_EX;
300 seek(NOTE, 0, 0); # START FROM BEGINNING
301  
302 foreach $N (sort {$a <=> $b} keys %orig) {
303 $n = $this->uen($orig{$N}->{'note'});
304 $t = $this->uen($orig{$N}->{'date'});
305 $buffer = pack( $this->{typedef}, $setnum, $n, $t);
306 print NOTE $buffer;
307 seek(NOTE, 0, IO::Seekable::SEEK_END);
308 $setnum++;
309 }
310 flock NOTE, LOCK_UN;
311 close NOTE;
312  
313 $this->changed;
314  
315 return;
316 }
317  
318 sub uen
319 {
320 my $this = shift;
321 my($T);
322 if($NOTEDB::crypt_supported == 1) {
323 eval {
324 $T = pack("u", $this->{cipher}->encrypt($_[0]));
325 };
326 }
327 else {
328 $T = pack("u", $_[0]);
329 }
330 chomp $T;
331  
332 return $T;
333 }
334  
335 sub ude
336 {
337 my $this = shift;
338 my($T);
339 if($NOTEDB::crypt_supported == 1) {
340 eval {
341 $T = $this->{cipher}->decrypt(unpack("u",$_[0]));
342 };
343 }
344 else {
345 $T = unpack("u", $_[0]);
346 }
347 return $T;
348 }
349  
350  
351 sub warn_if_too_big {
352 my ($this, $note, $num) = @_;
353  
354 my $len = length($this->uen($note));
355  
356 if ($len > $this->{maxnote}) {
357 # calculate the 30% uuencoding overhead
358 my $overhead = int(($this->{maxnote} / 100) * 28);
359  
360 # fetch what's left by driver
361 my $left = substr $note, $this->{maxnote} - $overhead;
362  
363 $left = "\n$left\n";
364 $left =~ s/\n/\n> /gs;
365  
366 print STDERR "*** WARNING $this->{version} WARNING ***\n"
367 ."The driver encountered a string length problem with your\n"
368 ."note entry number $num. The entry is too long. Either shorten\n"
369 ."the entry or resize the database field for entries.\n\n"
370 ."The following data has been cut off the entry:\n"
371 ."\n$left\n\n";
372  
373 my $copy = File::Spec->catfile($ENV{'HOME'}, "entry-$num.txt");
374 open N, ">$copy" or die "Could not open $copy: $!\n";
375 print N $note;
376 close N;
377  
378 print "*** Wrote the complete note entry number $num to file: $copy ***\n";
379 }
380 }
381  
382 sub _retrieve {
383 my ($this) = @_;
384 my $file = $this->{dbname};
385 if (-s $file) {
386 if ($this->changed() || $this->{unread}) {
387 open NOTE, "+<$this->{NOTEDB}" or die "could not open $this->{NOTEDB}\n";
388 flock NOTE, LOCK_EX;
389 my($buffer, $t, $n, %res);
390 seek(NOTE, 0, 0); # START FROM BEGINNING
391 while(read(NOTE, $buffer, $this->{sizeof})) {
392 my ($num, $note, $date) = unpack($this->{typedef}, $buffer);
393 $t = $this->ude($date);
394 $n = $this->ude($note);
395 $res{$num}->{'note'} = $n;
396 $res{$num}->{'date'} = $t;
397 }
398 flock NOTE, LOCK_UN;
399 close NOTE;
400  
401 $this->cache(%res);
402 return %res;
403 }
404 else {
405 return %{$this->{data}};
406 }
407 }
408 else {
409 return ();
410 }
411 }
412  
413 sub _store {
414 # compatibility dummy
415 return 1;
416 }
417  
418 1; # keep this!
419  
420 __END__