Commit | Line | Data |
f6b705ef |
1 | #!./perl -w |
a0d0e21e |
2 | |
77fd2717 |
3 | BEGIN { |
4 | unless(grep /blib/, @INC) { |
5 | chdir 't' if -d 't'; |
6 | @INC = '../lib' if -d '../lib'; |
7 | } |
8 | } |
9 | |
bb50757b |
10 | use warnings; |
11 | use strict; |
77fd2717 |
12 | use Config; |
13 | |
a0d0e21e |
14 | BEGIN { |
77fd2717 |
15 | if(-d "lib" && -f "TEST") { |
16 | if ($Config{'extensions'} !~ /\bDB_File\b/ ) { |
bb50757b |
17 | print "1..0 # Skip: DB_File was not built\n"; |
77fd2717 |
18 | exit 0; |
19 | } |
a0d0e21e |
20 | } |
8e092815 |
21 | } |
22 | |
23 | BEGIN |
24 | { |
3ebf0137 |
25 | if ($^O eq 'darwin' |
26 | && $Config{db_version_major} == 1 |
27 | && $Config{db_version_minor} == 0 |
28 | && $Config{db_version_patch} == 0) { |
29 | warn <<EOM; |
30 | # |
0b9ee88a |
31 | # This test is known to crash in Mac OS X versions 10.2 (or earlier) |
3ebf0137 |
32 | # because of the buggy Berkeley DB version included with the OS. |
33 | # |
34 | EOM |
35 | } |
a0d0e21e |
36 | } |
37 | |
38 | use DB_File; |
39 | use Fcntl; |
40 | |
9c095db2 |
41 | print "1..197\n"; |
f6b705ef |
42 | |
262eaca6 |
43 | unlink glob "__db.*"; |
44 | |
f6b705ef |
45 | sub ok |
46 | { |
47 | my $no = shift ; |
48 | my $result = shift ; |
49 | |
50 | print "not " unless $result ; |
51 | print "ok $no\n" ; |
52 | } |
a0d0e21e |
53 | |
55497cff |
54 | sub lexical |
55 | { |
56 | my(@a) = unpack ("C*", $a) ; |
57 | my(@b) = unpack ("C*", $b) ; |
58 | |
59 | my $len = (@a > @b ? @b : @a) ; |
60 | my $i = 0 ; |
61 | |
62 | foreach $i ( 0 .. $len -1) { |
63 | return $a[$i] - $b[$i] if $a[$i] != $b[$i] ; |
64 | } |
65 | |
66 | return @a - @b ; |
67 | } |
68 | |
2c2d71f5 |
69 | { |
70 | package Redirect ; |
71 | use Symbol ; |
72 | |
73 | sub new |
74 | { |
75 | my $class = shift ; |
76 | my $filename = shift ; |
77 | my $fh = gensym ; |
78 | open ($fh, ">$filename") || die "Cannot open $filename: $!" ; |
79 | my $real_stdout = select($fh) ; |
80 | return bless [$fh, $real_stdout ] ; |
81 | |
82 | } |
83 | sub DESTROY |
84 | { |
85 | my $self = shift ; |
86 | close $self->[0] ; |
87 | select($self->[1]) ; |
88 | } |
89 | } |
90 | |
91 | sub docat |
92 | { |
93 | my $file = shift; |
77fd2717 |
94 | local $/ = undef ; |
2c2d71f5 |
95 | open(CAT,$file) || die "Cannot open $file: $!"; |
77fd2717 |
96 | my $result = <CAT>; |
2c2d71f5 |
97 | close(CAT); |
77fd2717 |
98 | $result = normalise($result) ; |
99 | return $result ; |
2c2d71f5 |
100 | } |
101 | |
102 | sub docat_del |
103 | { |
104 | my $file = shift; |
77fd2717 |
105 | my $result = docat($file); |
2c2d71f5 |
106 | unlink $file ; |
77fd2717 |
107 | return $result ; |
2c2d71f5 |
108 | } |
109 | |
77fd2717 |
110 | sub normalise |
111 | { |
112 | my $data = shift ; |
113 | $data =~ s#\r\n#\n#g |
114 | if $^O eq 'cygwin' ; |
115 | |
116 | return $data ; |
117 | } |
118 | |
efc79c7d |
119 | sub safeUntie |
120 | { |
121 | my $hashref = shift ; |
122 | my $no_inner = 1; |
123 | local $SIG{__WARN__} = sub {-- $no_inner } ; |
124 | untie %$hashref; |
125 | return $no_inner; |
126 | } |
127 | |
77fd2717 |
128 | |
2c2d71f5 |
129 | |
3245f058 |
130 | my $db185mode = ($DB_File::db_version == 1 && ! $DB_File::db_185_compat) ; |
131 | my $null_keys_allowed = ($DB_File::db_ver < 2.004010 |
132 | || $DB_File::db_ver >= 3.1 ); |
039d031f |
133 | |
9fe6733a |
134 | my $Dfile = "dbbtree.tmp"; |
a0d0e21e |
135 | unlink $Dfile; |
136 | |
137 | umask(0); |
138 | |
139 | # Check the interface to BTREEINFO |
140 | |
f6b705ef |
141 | my $dbh = new DB_File::BTREEINFO ; |
3fe9a6f1 |
142 | ok(1, ! defined $dbh->{flags}) ; |
143 | ok(2, ! defined $dbh->{cachesize}) ; |
144 | ok(3, ! defined $dbh->{psize}) ; |
145 | ok(4, ! defined $dbh->{lorder}) ; |
146 | ok(5, ! defined $dbh->{minkeypage}) ; |
147 | ok(6, ! defined $dbh->{maxkeypage}) ; |
148 | ok(7, ! defined $dbh->{compare}) ; |
149 | ok(8, ! defined $dbh->{prefix}) ; |
a0d0e21e |
150 | |
151 | $dbh->{flags} = 3000 ; |
f6b705ef |
152 | ok(9, $dbh->{flags} == 3000) ; |
a0d0e21e |
153 | |
154 | $dbh->{cachesize} = 9000 ; |
f6b705ef |
155 | ok(10, $dbh->{cachesize} == 9000); |
156 | |
a0d0e21e |
157 | $dbh->{psize} = 400 ; |
f6b705ef |
158 | ok(11, $dbh->{psize} == 400) ; |
a0d0e21e |
159 | |
160 | $dbh->{lorder} = 65 ; |
f6b705ef |
161 | ok(12, $dbh->{lorder} == 65) ; |
a0d0e21e |
162 | |
163 | $dbh->{minkeypage} = 123 ; |
f6b705ef |
164 | ok(13, $dbh->{minkeypage} == 123) ; |
a0d0e21e |
165 | |
166 | $dbh->{maxkeypage} = 1234 ; |
f6b705ef |
167 | ok(14, $dbh->{maxkeypage} == 1234 ); |
a0d0e21e |
168 | |
a0d0e21e |
169 | # Check that an invalid entry is caught both for store & fetch |
170 | eval '$dbh->{fred} = 1234' ; |
efc79c7d |
171 | ok(15, $@ =~ /^DB_File::BTREEINFO::STORE - Unknown element 'fred' at/ ) ; |
3245f058 |
172 | eval 'my $q = $dbh->{fred}' ; |
efc79c7d |
173 | ok(16, $@ =~ /^DB_File::BTREEINFO::FETCH - Unknown element 'fred' at/ ) ; |
a0d0e21e |
174 | |
175 | # Now check the interface to BTREE |
176 | |
3245f058 |
177 | my ($X, %h) ; |
efc79c7d |
178 | ok(17, $X = tie(%h, 'DB_File',$Dfile, O_RDWR|O_CREAT, 0640, $DB_BTREE )) ; |
05a54443 |
179 | die "Could not tie: $!" unless $X; |
a0d0e21e |
180 | |
3245f058 |
181 | my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime, |
a0d0e21e |
182 | $blksize,$blocks) = stat($Dfile); |
77fd2717 |
183 | |
184 | my %noMode = map { $_, 1} qw( amigaos MSWin32 NetWare cygwin ) ; |
185 | |
efc79c7d |
186 | ok(18, ($mode & 0777) == (($^O eq 'os2' || $^O eq 'MacOS') ? 0666 : 0640) |
77fd2717 |
187 | || $noMode{$^O} ); |
a0d0e21e |
188 | |
3245f058 |
189 | my ($key, $value, $i); |
a0d0e21e |
190 | while (($key,$value) = each(%h)) { |
191 | $i++; |
192 | } |
efc79c7d |
193 | ok(19, !$i ) ; |
a0d0e21e |
194 | |
195 | $h{'goner1'} = 'snork'; |
196 | |
197 | $h{'abc'} = 'ABC'; |
efc79c7d |
198 | ok(20, $h{'abc'} eq 'ABC' ); |
199 | ok(21, ! defined $h{'jimmy'} ) ; |
200 | ok(22, ! exists $h{'jimmy'} ) ; |
201 | ok(23, defined $h{'abc'} ) ; |
a0d0e21e |
202 | |
203 | $h{'def'} = 'DEF'; |
204 | $h{'jkl','mno'} = "JKL\034MNO"; |
205 | $h{'a',2,3,4,5} = join("\034",'A',2,3,4,5); |
206 | $h{'a'} = 'A'; |
207 | |
208 | #$h{'b'} = 'B'; |
209 | $X->STORE('b', 'B') ; |
210 | |
211 | $h{'c'} = 'C'; |
212 | |
213 | #$h{'d'} = 'D'; |
214 | $X->put('d', 'D') ; |
215 | |
216 | $h{'e'} = 'E'; |
217 | $h{'f'} = 'F'; |
218 | $h{'g'} = 'X'; |
219 | $h{'h'} = 'H'; |
220 | $h{'i'} = 'I'; |
221 | |
222 | $h{'goner2'} = 'snork'; |
223 | delete $h{'goner2'}; |
224 | |
225 | |
226 | # IMPORTANT - $X must be undefined before the untie otherwise the |
227 | # underlying DB close routine will not get called. |
228 | undef $X ; |
229 | untie(%h); |
230 | |
a0d0e21e |
231 | # tie to the same file again |
efc79c7d |
232 | ok(24, $X = tie(%h,'DB_File',$Dfile, O_RDWR, 0640, $DB_BTREE)) ; |
a0d0e21e |
233 | |
234 | # Modify an entry from the previous tie |
235 | $h{'g'} = 'G'; |
236 | |
237 | $h{'j'} = 'J'; |
238 | $h{'k'} = 'K'; |
239 | $h{'l'} = 'L'; |
240 | $h{'m'} = 'M'; |
241 | $h{'n'} = 'N'; |
242 | $h{'o'} = 'O'; |
243 | $h{'p'} = 'P'; |
244 | $h{'q'} = 'Q'; |
245 | $h{'r'} = 'R'; |
246 | $h{'s'} = 'S'; |
247 | $h{'t'} = 'T'; |
248 | $h{'u'} = 'U'; |
249 | $h{'v'} = 'V'; |
250 | $h{'w'} = 'W'; |
251 | $h{'x'} = 'X'; |
252 | $h{'y'} = 'Y'; |
253 | $h{'z'} = 'Z'; |
254 | |
255 | $h{'goner3'} = 'snork'; |
256 | |
257 | delete $h{'goner1'}; |
258 | $X->DELETE('goner3'); |
259 | |
3245f058 |
260 | my @keys = keys(%h); |
261 | my @values = values(%h); |
a0d0e21e |
262 | |
efc79c7d |
263 | ok(25, $#keys == 29 && $#values == 29) ; |
a0d0e21e |
264 | |
f6b705ef |
265 | $i = 0 ; |
a0d0e21e |
266 | while (($key,$value) = each(%h)) { |
2f52a358 |
267 | if ($key eq $keys[$i] && $value eq $values[$i] && $key eq lc($value)) { |
a0d0e21e |
268 | $key =~ y/a-z/A-Z/; |
269 | $i++ if $key eq $value; |
270 | } |
271 | } |
272 | |
efc79c7d |
273 | ok(26, $i == 30) ; |
a0d0e21e |
274 | |
55d68b4a |
275 | @keys = ('blurfl', keys(%h), 'dyick'); |
efc79c7d |
276 | ok(27, $#keys == 31) ; |
a0d0e21e |
277 | |
278 | #Check that the keys can be retrieved in order |
55497cff |
279 | my @b = keys %h ; |
280 | my @c = sort lexical @b ; |
efc79c7d |
281 | ok(28, ArrayCompare(\@b, \@c)) ; |
a0d0e21e |
282 | |
283 | $h{'foo'} = ''; |
efc79c7d |
284 | ok(29, $h{'foo'} eq '' ) ; |
a0d0e21e |
285 | |
3245f058 |
286 | # Berkeley DB from version 2.4.10 to 3.0 does not allow null keys. |
287 | # This feature was reenabled in version 3.1 of Berkeley DB. |
288 | my $result = 0 ; |
289 | if ($null_keys_allowed) { |
290 | $h{''} = 'bar'; |
291 | $result = ( $h{''} eq 'bar' ); |
292 | } |
293 | else |
294 | { $result = 1 } |
efc79c7d |
295 | ok(30, $result) ; |
a0d0e21e |
296 | |
297 | # check cache overflow and numeric keys and contents |
3245f058 |
298 | my $ok = 1; |
a0d0e21e |
299 | for ($i = 1; $i < 200; $i++) { $h{$i + 0} = $i + 0; } |
300 | for ($i = 1; $i < 200; $i++) { $ok = 0 unless $h{$i} == $i; } |
efc79c7d |
301 | ok(31, $ok); |
a0d0e21e |
302 | |
303 | ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime, |
304 | $blksize,$blocks) = stat($Dfile); |
efc79c7d |
305 | ok(32, $size > 0 ); |
a0d0e21e |
306 | |
307 | @h{0..200} = 200..400; |
3245f058 |
308 | my @foo = @h{0..200}; |
efc79c7d |
309 | ok(33, join(':',200..400) eq join(':',@foo) ); |
a0d0e21e |
310 | |
311 | # Now check all the non-tie specific stuff |
312 | |
313 | |
314 | # Check R_NOOVERWRITE flag will make put fail when attempting to overwrite |
315 | # an existing record. |
316 | |
3245f058 |
317 | my $status = $X->put( 'x', 'newvalue', R_NOOVERWRITE) ; |
efc79c7d |
318 | ok(34, $status == 1 ); |
a0d0e21e |
319 | |
320 | # check that the value of the key 'x' has not been changed by the |
321 | # previous test |
efc79c7d |
322 | ok(35, $h{'x'} eq 'X' ); |
a0d0e21e |
323 | |
324 | # standard put |
325 | $status = $X->put('key', 'value') ; |
efc79c7d |
326 | ok(36, $status == 0 ); |
a0d0e21e |
327 | |
328 | #check that previous put can be retrieved |
f6b705ef |
329 | $value = 0 ; |
a0d0e21e |
330 | $status = $X->get('key', $value) ; |
efc79c7d |
331 | ok(37, $status == 0 ); |
332 | ok(38, $value eq 'value' ); |
a0d0e21e |
333 | |
334 | # Attempting to delete an existing key should work |
335 | |
336 | $status = $X->del('q') ; |
efc79c7d |
337 | ok(39, $status == 0 ); |
3245f058 |
338 | if ($null_keys_allowed) { |
339 | $status = $X->del('') ; |
340 | } else { |
341 | $status = 0 ; |
342 | } |
efc79c7d |
343 | ok(40, $status == 0 ); |
a0d0e21e |
344 | |
345 | # Make sure that the key deleted, cannot be retrieved |
efc79c7d |
346 | ok(41, ! defined $h{'q'}) ; |
347 | ok(42, ! defined $h{''}) ; |
a0d0e21e |
348 | |
349 | undef $X ; |
350 | untie %h ; |
351 | |
efc79c7d |
352 | ok(43, $X = tie(%h, 'DB_File',$Dfile, O_RDWR, 0640, $DB_BTREE )); |
a0d0e21e |
353 | |
354 | # Attempting to delete a non-existant key should fail |
355 | |
356 | $status = $X->del('joe') ; |
efc79c7d |
357 | ok(44, $status == 1 ); |
a0d0e21e |
358 | |
359 | # Check the get interface |
360 | |
361 | # First a non-existing key |
362 | $status = $X->get('aaaa', $value) ; |
efc79c7d |
363 | ok(45, $status == 1 ); |
a0d0e21e |
364 | |
365 | # Next an existing key |
366 | $status = $X->get('a', $value) ; |
efc79c7d |
367 | ok(46, $status == 0 ); |
368 | ok(47, $value eq 'A' ); |
a0d0e21e |
369 | |
370 | # seq |
371 | # ### |
372 | |
373 | # use seq to find an approximate match |
374 | $key = 'ke' ; |
375 | $value = '' ; |
376 | $status = $X->seq($key, $value, R_CURSOR) ; |
efc79c7d |
377 | ok(48, $status == 0 ); |
378 | ok(49, $key eq 'key' ); |
379 | ok(50, $value eq 'value' ); |
a0d0e21e |
380 | |
381 | # seq when the key does not match |
382 | $key = 'zzz' ; |
383 | $value = '' ; |
384 | $status = $X->seq($key, $value, R_CURSOR) ; |
efc79c7d |
385 | ok(51, $status == 1 ); |
a0d0e21e |
386 | |
387 | |
388 | # use seq to set the cursor, then delete the record @ the cursor. |
389 | |
390 | $key = 'x' ; |
391 | $value = '' ; |
392 | $status = $X->seq($key, $value, R_CURSOR) ; |
efc79c7d |
393 | ok(52, $status == 0 ); |
394 | ok(53, $key eq 'x' ); |
395 | ok(54, $value eq 'X' ); |
a0d0e21e |
396 | $status = $X->del(0, R_CURSOR) ; |
efc79c7d |
397 | ok(55, $status == 0 ); |
a0d0e21e |
398 | $status = $X->get('x', $value) ; |
efc79c7d |
399 | ok(56, $status == 1 ); |
a0d0e21e |
400 | |
401 | # ditto, but use put to replace the key/value pair. |
402 | $key = 'y' ; |
403 | $value = '' ; |
404 | $status = $X->seq($key, $value, R_CURSOR) ; |
efc79c7d |
405 | ok(57, $status == 0 ); |
406 | ok(58, $key eq 'y' ); |
407 | ok(59, $value eq 'Y' ); |
a0d0e21e |
408 | |
409 | $key = "replace key" ; |
410 | $value = "replace value" ; |
411 | $status = $X->put($key, $value, R_CURSOR) ; |
efc79c7d |
412 | ok(60, $status == 0 ); |
413 | ok(61, $key eq 'replace key' ); |
414 | ok(62, $value eq 'replace value' ); |
a0d0e21e |
415 | $status = $X->get('y', $value) ; |
efc79c7d |
416 | ok(63, 1) ; # hard-wire to always pass. the previous test ($status == 1) |
a9fd575d |
417 | # only worked because of a bug in 1.85/6 |
a0d0e21e |
418 | |
419 | # use seq to walk forwards through a file |
420 | |
421 | $status = $X->seq($key, $value, R_FIRST) ; |
efc79c7d |
422 | ok(64, $status == 0 ); |
3245f058 |
423 | my $previous = $key ; |
a0d0e21e |
424 | |
425 | $ok = 1 ; |
426 | while (($status = $X->seq($key, $value, R_NEXT)) == 0) |
427 | { |
428 | ($ok = 0), last if ($previous cmp $key) == 1 ; |
429 | } |
430 | |
efc79c7d |
431 | ok(65, $status == 1 ); |
432 | ok(66, $ok == 1 ); |
a0d0e21e |
433 | |
434 | # use seq to walk backwards through a file |
435 | $status = $X->seq($key, $value, R_LAST) ; |
efc79c7d |
436 | ok(67, $status == 0 ); |
a0d0e21e |
437 | $previous = $key ; |
438 | |
439 | $ok = 1 ; |
440 | while (($status = $X->seq($key, $value, R_PREV)) == 0) |
441 | { |
442 | ($ok = 0), last if ($previous cmp $key) == -1 ; |
443 | #print "key = [$key] value = [$value]\n" ; |
444 | } |
445 | |
efc79c7d |
446 | ok(68, $status == 1 ); |
447 | ok(69, $ok == 1 ); |
a0d0e21e |
448 | |
449 | |
450 | # check seq FIRST/LAST |
451 | |
452 | # sync |
453 | # #### |
454 | |
455 | $status = $X->sync ; |
efc79c7d |
456 | ok(70, $status == 0 ); |
a0d0e21e |
457 | |
458 | |
459 | # fd |
460 | # ## |
461 | |
462 | $status = $X->fd ; |
8e092815 |
463 | ok(71, 1 ); |
464 | #ok(71, $status != 0 ); |
a0d0e21e |
465 | |
466 | |
467 | undef $X ; |
468 | untie %h ; |
469 | |
470 | unlink $Dfile; |
471 | |
472 | # Now try an in memory file |
3245f058 |
473 | my $Y; |
efc79c7d |
474 | ok(72, $Y = tie(%h, 'DB_File',undef, O_RDWR|O_CREAT, 0640, $DB_BTREE )); |
a0d0e21e |
475 | |
476 | # fd with an in memory file should return failure |
477 | $status = $Y->fd ; |
efc79c7d |
478 | ok(73, $status == -1 ); |
a0d0e21e |
479 | |
55d68b4a |
480 | |
a0d0e21e |
481 | undef $Y ; |
482 | untie %h ; |
483 | |
55d68b4a |
484 | # Duplicate keys |
485 | my $bt = new DB_File::BTREEINFO ; |
486 | $bt->{flags} = R_DUP ; |
3245f058 |
487 | my ($YY, %hh); |
efc79c7d |
488 | ok(74, $YY = tie(%hh, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $bt )) ; |
55d68b4a |
489 | |
490 | $hh{'Wall'} = 'Larry' ; |
491 | $hh{'Wall'} = 'Stone' ; # Note the duplicate key |
492 | $hh{'Wall'} = 'Brick' ; # Note the duplicate key |
f6b705ef |
493 | $hh{'Wall'} = 'Brick' ; # Note the duplicate key and value |
55d68b4a |
494 | $hh{'Smith'} = 'John' ; |
495 | $hh{'mouse'} = 'mickey' ; |
496 | |
497 | # first work in scalar context |
efc79c7d |
498 | ok(75, scalar $YY->get_dup('Unknown') == 0 ); |
499 | ok(76, scalar $YY->get_dup('Smith') == 1 ); |
500 | ok(77, scalar $YY->get_dup('Wall') == 4 ); |
55d68b4a |
501 | |
502 | # now in list context |
503 | my @unknown = $YY->get_dup('Unknown') ; |
efc79c7d |
504 | ok(78, "@unknown" eq "" ); |
55d68b4a |
505 | |
506 | my @smith = $YY->get_dup('Smith') ; |
efc79c7d |
507 | ok(79, "@smith" eq "John" ); |
55d68b4a |
508 | |
760ac839 |
509 | { |
f6b705ef |
510 | my @wall = $YY->get_dup('Wall') ; |
511 | my %wall ; |
512 | @wall{@wall} = @wall ; |
efc79c7d |
513 | ok(80, (@wall == 4 && $wall{'Larry'} && $wall{'Stone'} && $wall{'Brick'}) ); |
760ac839 |
514 | } |
55d68b4a |
515 | |
516 | # hash |
517 | my %unknown = $YY->get_dup('Unknown', 1) ; |
efc79c7d |
518 | ok(81, keys %unknown == 0 ); |
55d68b4a |
519 | |
520 | my %smith = $YY->get_dup('Smith', 1) ; |
efc79c7d |
521 | ok(82, keys %smith == 1 && $smith{'John'}) ; |
55d68b4a |
522 | |
f6b705ef |
523 | my %wall = $YY->get_dup('Wall', 1) ; |
efc79c7d |
524 | ok(83, keys %wall == 3 && $wall{'Larry'} == 1 && $wall{'Stone'} == 1 |
f6b705ef |
525 | && $wall{'Brick'} == 2); |
55d68b4a |
526 | |
527 | undef $YY ; |
528 | untie %hh ; |
529 | unlink $Dfile; |
530 | |
531 | |
8e07c86e |
532 | # test multiple callbacks |
3245f058 |
533 | my $Dfile1 = "btree1" ; |
534 | my $Dfile2 = "btree2" ; |
535 | my $Dfile3 = "btree3" ; |
8e07c86e |
536 | |
3245f058 |
537 | my $dbh1 = new DB_File::BTREEINFO ; |
538 | $dbh1->{compare} = sub { |
539 | no warnings 'numeric' ; |
540 | $_[0] <=> $_[1] } ; |
8e07c86e |
541 | |
3245f058 |
542 | my $dbh2 = new DB_File::BTREEINFO ; |
8e07c86e |
543 | $dbh2->{compare} = sub { $_[0] cmp $_[1] } ; |
544 | |
3245f058 |
545 | my $dbh3 = new DB_File::BTREEINFO ; |
8e07c86e |
546 | $dbh3->{compare} = sub { length $_[0] <=> length $_[1] } ; |
547 | |
548 | |
3245f058 |
549 | my (%g, %k); |
05a54443 |
550 | tie(%h, 'DB_File',$Dfile1, O_RDWR|O_CREAT, 0640, $dbh1 ) or die $!; |
551 | tie(%g, 'DB_File',$Dfile2, O_RDWR|O_CREAT, 0640, $dbh2 ) or die $!; |
552 | tie(%k, 'DB_File',$Dfile3, O_RDWR|O_CREAT, 0640, $dbh3 ) or die $!; |
8e07c86e |
553 | |
3245f058 |
554 | my @Keys = qw( 0123 12 -1234 9 987654321 def ) ; |
555 | my (@srt_1, @srt_2, @srt_3); |
556 | { |
557 | no warnings 'numeric' ; |
558 | @srt_1 = sort { $a <=> $b } @Keys ; |
559 | } |
8e07c86e |
560 | @srt_2 = sort { $a cmp $b } @Keys ; |
561 | @srt_3 = sort { length $a <=> length $b } @Keys ; |
562 | |
563 | foreach (@Keys) { |
3245f058 |
564 | $h{$_} = 1 ; |
8e07c86e |
565 | $g{$_} = 1 ; |
566 | $k{$_} = 1 ; |
567 | } |
568 | |
569 | sub ArrayCompare |
570 | { |
571 | my($a, $b) = @_ ; |
572 | |
573 | return 0 if @$a != @$b ; |
574 | |
575 | foreach (1 .. length @$a) |
576 | { |
577 | return 0 unless $$a[$_] eq $$b[$_] ; |
578 | } |
579 | |
580 | 1 ; |
581 | } |
582 | |
efc79c7d |
583 | ok(84, ArrayCompare (\@srt_1, [keys %h]) ); |
584 | ok(85, ArrayCompare (\@srt_2, [keys %g]) ); |
585 | ok(86, ArrayCompare (\@srt_3, [keys %k]) ); |
8e07c86e |
586 | |
587 | untie %h ; |
588 | untie %g ; |
589 | untie %k ; |
590 | unlink $Dfile1, $Dfile2, $Dfile3 ; |
591 | |
f6b705ef |
592 | # clear |
593 | # ##### |
594 | |
efc79c7d |
595 | ok(87, tie(%h, 'DB_File', $Dfile1, O_RDWR|O_CREAT, 0640, $DB_BTREE ) ); |
f6b705ef |
596 | foreach (1 .. 10) |
597 | { $h{$_} = $_ * 100 } |
598 | |
599 | # check that there are 10 elements in the hash |
600 | $i = 0 ; |
601 | while (($key,$value) = each(%h)) { |
602 | $i++; |
603 | } |
efc79c7d |
604 | ok(88, $i == 10); |
f6b705ef |
605 | |
606 | # now clear the hash |
607 | %h = () ; |
608 | |
609 | # check it is empty |
610 | $i = 0 ; |
611 | while (($key,$value) = each(%h)) { |
612 | $i++; |
613 | } |
efc79c7d |
614 | ok(89, $i == 0); |
f6b705ef |
615 | |
616 | untie %h ; |
617 | unlink $Dfile1 ; |
618 | |
05475680 |
619 | { |
620 | # check that attempting to tie an array to a DB_BTREE will fail |
621 | |
622 | my $filename = "xyz" ; |
623 | my @x ; |
624 | eval { tie @x, 'DB_File', $filename, O_RDWR|O_CREAT, 0640, $DB_BTREE ; } ; |
efc79c7d |
625 | ok(90, $@ =~ /^DB_File can only tie an associative array to a DB_BTREE database/) ; |
05475680 |
626 | unlink $filename ; |
627 | } |
628 | |
a6ed719b |
629 | { |
630 | # sub-class test |
631 | |
632 | package Another ; |
633 | |
3245f058 |
634 | use warnings ; |
a6ed719b |
635 | use strict ; |
636 | |
637 | open(FILE, ">SubDB.pm") or die "Cannot open SubDB.pm: $!\n" ; |
638 | print FILE <<'EOM' ; |
639 | |
640 | package SubDB ; |
641 | |
3245f058 |
642 | use warnings ; |
a6ed719b |
643 | use strict ; |
07200f1b |
644 | our (@ISA, @EXPORT); |
a6ed719b |
645 | |
646 | require Exporter ; |
647 | use DB_File; |
648 | @ISA=qw(DB_File); |
649 | @EXPORT = @DB_File::EXPORT ; |
650 | |
651 | sub STORE { |
652 | my $self = shift ; |
653 | my $key = shift ; |
654 | my $value = shift ; |
655 | $self->SUPER::STORE($key, $value * 2) ; |
656 | } |
657 | |
658 | sub FETCH { |
659 | my $self = shift ; |
660 | my $key = shift ; |
661 | $self->SUPER::FETCH($key) - 1 ; |
662 | } |
663 | |
664 | sub put { |
665 | my $self = shift ; |
666 | my $key = shift ; |
667 | my $value = shift ; |
668 | $self->SUPER::put($key, $value * 3) ; |
669 | } |
670 | |
671 | sub get { |
672 | my $self = shift ; |
673 | $self->SUPER::get($_[0], $_[1]) ; |
674 | $_[1] -= 2 ; |
675 | } |
676 | |
677 | sub A_new_method |
678 | { |
679 | my $self = shift ; |
680 | my $key = shift ; |
681 | my $value = $self->FETCH($key) ; |
682 | return "[[$value]]" ; |
683 | } |
684 | |
685 | 1 ; |
686 | EOM |
687 | |
688 | close FILE ; |
689 | |
a9fd575d |
690 | BEGIN { push @INC, '.'; } |
a6ed719b |
691 | eval 'use SubDB ; '; |
efc79c7d |
692 | main::ok(91, $@ eq "") ; |
a6ed719b |
693 | my %h ; |
694 | my $X ; |
695 | eval ' |
696 | $X = tie(%h, "SubDB","dbbtree.tmp", O_RDWR|O_CREAT, 0640, $DB_BTREE ); |
697 | ' ; |
698 | |
efc79c7d |
699 | main::ok(92, $@ eq "") ; |
a6ed719b |
700 | |
701 | my $ret = eval '$h{"fred"} = 3 ; return $h{"fred"} ' ; |
efc79c7d |
702 | main::ok(93, $@ eq "") ; |
703 | main::ok(94, $ret == 5) ; |
a6ed719b |
704 | |
705 | my $value = 0; |
706 | $ret = eval '$X->put("joe", 4) ; $X->get("joe", $value) ; return $value' ; |
efc79c7d |
707 | main::ok(95, $@ eq "") ; |
708 | main::ok(96, $ret == 10) ; |
a6ed719b |
709 | |
710 | $ret = eval ' R_NEXT eq main::R_NEXT ' ; |
efc79c7d |
711 | main::ok(97, $@ eq "" ) ; |
712 | main::ok(98, $ret == 1) ; |
a6ed719b |
713 | |
714 | $ret = eval '$X->A_new_method("joe") ' ; |
efc79c7d |
715 | main::ok(99, $@ eq "") ; |
716 | main::ok(100, $ret eq "[[11]]") ; |
a6ed719b |
717 | |
fac76ed7 |
718 | undef $X; |
719 | untie(%h); |
a6ed719b |
720 | unlink "SubDB.pm", "dbbtree.tmp" ; |
721 | |
722 | } |
723 | |
9fe6733a |
724 | { |
725 | # DBM Filter tests |
3245f058 |
726 | use warnings ; |
9fe6733a |
727 | use strict ; |
728 | my (%h, $db) ; |
729 | my ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; |
730 | unlink $Dfile; |
731 | |
732 | sub checkOutput |
733 | { |
734 | my($fk, $sk, $fv, $sv) = @_ ; |
735 | return |
736 | $fetch_key eq $fk && $store_key eq $sk && |
737 | $fetch_value eq $fv && $store_value eq $sv && |
738 | $_ eq 'original' ; |
739 | } |
740 | |
efc79c7d |
741 | ok(101, $db = tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_BTREE ) ); |
9fe6733a |
742 | |
743 | $db->filter_fetch_key (sub { $fetch_key = $_ }) ; |
744 | $db->filter_store_key (sub { $store_key = $_ }) ; |
745 | $db->filter_fetch_value (sub { $fetch_value = $_}) ; |
746 | $db->filter_store_value (sub { $store_value = $_ }) ; |
747 | |
748 | $_ = "original" ; |
749 | |
750 | $h{"fred"} = "joe" ; |
751 | # fk sk fv sv |
efc79c7d |
752 | ok(102, checkOutput( "", "fred", "", "joe")) ; |
9fe6733a |
753 | |
754 | ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; |
efc79c7d |
755 | ok(103, $h{"fred"} eq "joe"); |
9fe6733a |
756 | # fk sk fv sv |
efc79c7d |
757 | ok(104, checkOutput( "", "fred", "joe", "")) ; |
9fe6733a |
758 | |
759 | ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; |
efc79c7d |
760 | ok(105, $db->FIRSTKEY() eq "fred") ; |
9fe6733a |
761 | # fk sk fv sv |
efc79c7d |
762 | ok(106, checkOutput( "fred", "", "", "")) ; |
9fe6733a |
763 | |
764 | # replace the filters, but remember the previous set |
765 | my ($old_fk) = $db->filter_fetch_key |
766 | (sub { $_ = uc $_ ; $fetch_key = $_ }) ; |
767 | my ($old_sk) = $db->filter_store_key |
768 | (sub { $_ = lc $_ ; $store_key = $_ }) ; |
769 | my ($old_fv) = $db->filter_fetch_value |
770 | (sub { $_ = "[$_]"; $fetch_value = $_ }) ; |
771 | my ($old_sv) = $db->filter_store_value |
772 | (sub { s/o/x/g; $store_value = $_ }) ; |
773 | |
774 | ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; |
775 | $h{"Fred"} = "Joe" ; |
776 | # fk sk fv sv |
efc79c7d |
777 | ok(107, checkOutput( "", "fred", "", "Jxe")) ; |
9fe6733a |
778 | |
779 | ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; |
efc79c7d |
780 | ok(108, $h{"Fred"} eq "[Jxe]"); |
9fe6733a |
781 | # fk sk fv sv |
efc79c7d |
782 | ok(109, checkOutput( "", "fred", "[Jxe]", "")) ; |
9fe6733a |
783 | |
784 | ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; |
efc79c7d |
785 | ok(110, $db->FIRSTKEY() eq "FRED") ; |
9fe6733a |
786 | # fk sk fv sv |
efc79c7d |
787 | ok(111, checkOutput( "FRED", "", "", "")) ; |
9fe6733a |
788 | |
789 | # put the original filters back |
790 | $db->filter_fetch_key ($old_fk); |
791 | $db->filter_store_key ($old_sk); |
792 | $db->filter_fetch_value ($old_fv); |
793 | $db->filter_store_value ($old_sv); |
794 | |
795 | ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; |
796 | $h{"fred"} = "joe" ; |
efc79c7d |
797 | ok(112, checkOutput( "", "fred", "", "joe")) ; |
9fe6733a |
798 | |
799 | ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; |
efc79c7d |
800 | ok(113, $h{"fred"} eq "joe"); |
801 | ok(114, checkOutput( "", "fred", "joe", "")) ; |
9fe6733a |
802 | |
803 | ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; |
efc79c7d |
804 | ok(115, $db->FIRSTKEY() eq "fred") ; |
805 | ok(116, checkOutput( "fred", "", "", "")) ; |
9fe6733a |
806 | |
807 | # delete the filters |
808 | $db->filter_fetch_key (undef); |
809 | $db->filter_store_key (undef); |
810 | $db->filter_fetch_value (undef); |
811 | $db->filter_store_value (undef); |
812 | |
813 | ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; |
814 | $h{"fred"} = "joe" ; |
efc79c7d |
815 | ok(117, checkOutput( "", "", "", "")) ; |
9fe6733a |
816 | |
817 | ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; |
efc79c7d |
818 | ok(118, $h{"fred"} eq "joe"); |
819 | ok(119, checkOutput( "", "", "", "")) ; |
9fe6733a |
820 | |
821 | ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; |
efc79c7d |
822 | ok(120, $db->FIRSTKEY() eq "fred") ; |
823 | ok(121, checkOutput( "", "", "", "")) ; |
9fe6733a |
824 | |
825 | undef $db ; |
826 | untie %h; |
827 | unlink $Dfile; |
828 | } |
829 | |
830 | { |
831 | # DBM Filter with a closure |
832 | |
3245f058 |
833 | use warnings ; |
9fe6733a |
834 | use strict ; |
835 | my (%h, $db) ; |
836 | |
837 | unlink $Dfile; |
efc79c7d |
838 | ok(122, $db = tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_BTREE ) ); |
9fe6733a |
839 | |
840 | my %result = () ; |
841 | |
842 | sub Closure |
843 | { |
844 | my ($name) = @_ ; |
845 | my $count = 0 ; |
846 | my @kept = () ; |
847 | |
848 | return sub { ++$count ; |
849 | push @kept, $_ ; |
850 | $result{$name} = "$name - $count: [@kept]" ; |
851 | } |
852 | } |
853 | |
854 | $db->filter_store_key(Closure("store key")) ; |
855 | $db->filter_store_value(Closure("store value")) ; |
856 | $db->filter_fetch_key(Closure("fetch key")) ; |
857 | $db->filter_fetch_value(Closure("fetch value")) ; |
858 | |
859 | $_ = "original" ; |
860 | |
861 | $h{"fred"} = "joe" ; |
efc79c7d |
862 | ok(123, $result{"store key"} eq "store key - 1: [fred]"); |
863 | ok(124, $result{"store value"} eq "store value - 1: [joe]"); |
864 | ok(125, ! defined $result{"fetch key"} ); |
865 | ok(126, ! defined $result{"fetch value"} ); |
866 | ok(127, $_ eq "original") ; |
867 | |
868 | ok(128, $db->FIRSTKEY() eq "fred") ; |
869 | ok(129, $result{"store key"} eq "store key - 1: [fred]"); |
870 | ok(130, $result{"store value"} eq "store value - 1: [joe]"); |
871 | ok(131, $result{"fetch key"} eq "fetch key - 1: [fred]"); |
872 | ok(132, ! defined $result{"fetch value"} ); |
873 | ok(133, $_ eq "original") ; |
9fe6733a |
874 | |
875 | $h{"jim"} = "john" ; |
efc79c7d |
876 | ok(134, $result{"store key"} eq "store key - 2: [fred jim]"); |
877 | ok(135, $result{"store value"} eq "store value - 2: [joe john]"); |
878 | ok(136, $result{"fetch key"} eq "fetch key - 1: [fred]"); |
879 | ok(137, ! defined $result{"fetch value"} ); |
880 | ok(138, $_ eq "original") ; |
881 | |
882 | ok(139, $h{"fred"} eq "joe"); |
883 | ok(140, $result{"store key"} eq "store key - 3: [fred jim fred]"); |
884 | ok(141, $result{"store value"} eq "store value - 2: [joe john]"); |
885 | ok(142, $result{"fetch key"} eq "fetch key - 1: [fred]"); |
886 | ok(143, $result{"fetch value"} eq "fetch value - 1: [joe]"); |
887 | ok(144, $_ eq "original") ; |
9fe6733a |
888 | |
889 | undef $db ; |
890 | untie %h; |
891 | unlink $Dfile; |
892 | } |
893 | |
894 | { |
895 | # DBM Filter recursion detection |
3245f058 |
896 | use warnings ; |
9fe6733a |
897 | use strict ; |
898 | my (%h, $db) ; |
899 | unlink $Dfile; |
900 | |
efc79c7d |
901 | ok(145, $db = tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_BTREE ) ); |
9fe6733a |
902 | |
903 | $db->filter_store_key (sub { $_ = $h{$_} }) ; |
904 | |
905 | eval '$h{1} = 1234' ; |
efc79c7d |
906 | ok(146, $@ =~ /^recursion detected in filter_store_key at/ ); |
9fe6733a |
907 | |
908 | undef $db ; |
909 | untie %h; |
910 | unlink $Dfile; |
911 | } |
912 | |
913 | |
2c2d71f5 |
914 | { |
915 | # Examples from the POD |
916 | |
917 | |
918 | my $file = "xyzt" ; |
919 | { |
920 | my $redirect = new Redirect $file ; |
921 | |
922 | # BTREE example 1 |
923 | ### |
924 | |
3245f058 |
925 | use warnings FATAL => qw(all) ; |
2c2d71f5 |
926 | use strict ; |
927 | use DB_File ; |
928 | |
929 | my %h ; |
930 | |
931 | sub Compare |
932 | { |
933 | my ($key1, $key2) = @_ ; |
934 | "\L$key1" cmp "\L$key2" ; |
935 | } |
936 | |
937 | # specify the Perl sub that will do the comparison |
938 | $DB_BTREE->{'compare'} = \&Compare ; |
939 | |
940 | unlink "tree" ; |
941 | tie %h, "DB_File", "tree", O_RDWR|O_CREAT, 0640, $DB_BTREE |
942 | or die "Cannot open file 'tree': $!\n" ; |
943 | |
944 | # Add a key/value pair to the file |
945 | $h{'Wall'} = 'Larry' ; |
946 | $h{'Smith'} = 'John' ; |
947 | $h{'mouse'} = 'mickey' ; |
948 | $h{'duck'} = 'donald' ; |
949 | |
950 | # Delete |
951 | delete $h{"duck"} ; |
952 | |
953 | # Cycle through the keys printing them in order. |
954 | # Note it is not necessary to sort the keys as |
955 | # the btree will have kept them in order automatically. |
956 | foreach (keys %h) |
957 | { print "$_\n" } |
958 | |
959 | untie %h ; |
960 | |
961 | unlink "tree" ; |
962 | } |
963 | |
964 | delete $DB_BTREE->{'compare'} ; |
965 | |
efc79c7d |
966 | ok(147, docat_del($file) eq <<'EOM') ; |
2c2d71f5 |
967 | mouse |
968 | Smith |
969 | Wall |
970 | EOM |
971 | |
972 | { |
973 | my $redirect = new Redirect $file ; |
974 | |
975 | # BTREE example 2 |
976 | ### |
977 | |
3245f058 |
978 | use warnings FATAL => qw(all) ; |
2c2d71f5 |
979 | use strict ; |
980 | use DB_File ; |
981 | |
962cee9f |
982 | my ($filename, %h); |
2c2d71f5 |
983 | |
984 | $filename = "tree" ; |
985 | unlink $filename ; |
986 | |
987 | # Enable duplicate records |
988 | $DB_BTREE->{'flags'} = R_DUP ; |
989 | |
990 | tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0640, $DB_BTREE |
991 | or die "Cannot open $filename: $!\n"; |
992 | |
993 | # Add some key/value pairs to the file |
994 | $h{'Wall'} = 'Larry' ; |
995 | $h{'Wall'} = 'Brick' ; # Note the duplicate key |
996 | $h{'Wall'} = 'Brick' ; # Note the duplicate key and value |
997 | $h{'Smith'} = 'John' ; |
998 | $h{'mouse'} = 'mickey' ; |
999 | |
1000 | # iterate through the associative array |
1001 | # and print each key/value pair. |
1002 | foreach (keys %h) |
1003 | { print "$_ -> $h{$_}\n" } |
1004 | |
1005 | untie %h ; |
1006 | |
1007 | unlink $filename ; |
1008 | } |
1009 | |
efc79c7d |
1010 | ok(148, docat_del($file) eq ($db185mode ? <<'EOM' : <<'EOM') ) ; |
2c2d71f5 |
1011 | Smith -> John |
1012 | Wall -> Brick |
1013 | Wall -> Brick |
1014 | Wall -> Brick |
1015 | mouse -> mickey |
1016 | EOM |
1017 | Smith -> John |
1018 | Wall -> Larry |
1019 | Wall -> Larry |
1020 | Wall -> Larry |
1021 | mouse -> mickey |
1022 | EOM |
1023 | |
1024 | { |
1025 | my $redirect = new Redirect $file ; |
1026 | |
1027 | # BTREE example 3 |
1028 | ### |
1029 | |
3245f058 |
1030 | use warnings FATAL => qw(all) ; |
2c2d71f5 |
1031 | use strict ; |
1032 | use DB_File ; |
1033 | |
962cee9f |
1034 | my ($filename, $x, %h, $status, $key, $value); |
2c2d71f5 |
1035 | |
1036 | $filename = "tree" ; |
1037 | unlink $filename ; |
1038 | |
1039 | # Enable duplicate records |
1040 | $DB_BTREE->{'flags'} = R_DUP ; |
1041 | |
1042 | $x = tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0640, $DB_BTREE |
1043 | or die "Cannot open $filename: $!\n"; |
1044 | |
1045 | # Add some key/value pairs to the file |
1046 | $h{'Wall'} = 'Larry' ; |
1047 | $h{'Wall'} = 'Brick' ; # Note the duplicate key |
1048 | $h{'Wall'} = 'Brick' ; # Note the duplicate key and value |
1049 | $h{'Smith'} = 'John' ; |
1050 | $h{'mouse'} = 'mickey' ; |
1051 | |
1052 | # iterate through the btree using seq |
1053 | # and print each key/value pair. |
1054 | $key = $value = 0 ; |
1055 | for ($status = $x->seq($key, $value, R_FIRST) ; |
1056 | $status == 0 ; |
1057 | $status = $x->seq($key, $value, R_NEXT) ) |
1058 | { print "$key -> $value\n" } |
1059 | |
1060 | |
1061 | undef $x ; |
1062 | untie %h ; |
1063 | } |
1064 | |
efc79c7d |
1065 | ok(149, docat_del($file) eq ($db185mode == 1 ? <<'EOM' : <<'EOM') ) ; |
2c2d71f5 |
1066 | Smith -> John |
1067 | Wall -> Brick |
1068 | Wall -> Brick |
1069 | Wall -> Larry |
1070 | mouse -> mickey |
1071 | EOM |
1072 | Smith -> John |
1073 | Wall -> Larry |
1074 | Wall -> Brick |
1075 | Wall -> Brick |
1076 | mouse -> mickey |
1077 | EOM |
1078 | |
1079 | |
1080 | { |
1081 | my $redirect = new Redirect $file ; |
1082 | |
1083 | # BTREE example 4 |
1084 | ### |
1085 | |
3245f058 |
1086 | use warnings FATAL => qw(all) ; |
2c2d71f5 |
1087 | use strict ; |
1088 | use DB_File ; |
1089 | |
962cee9f |
1090 | my ($filename, $x, %h); |
2c2d71f5 |
1091 | |
1092 | $filename = "tree" ; |
1093 | |
1094 | # Enable duplicate records |
1095 | $DB_BTREE->{'flags'} = R_DUP ; |
1096 | |
1097 | $x = tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0640, $DB_BTREE |
1098 | or die "Cannot open $filename: $!\n"; |
1099 | |
1100 | my $cnt = $x->get_dup("Wall") ; |
1101 | print "Wall occurred $cnt times\n" ; |
1102 | |
1103 | my %hash = $x->get_dup("Wall", 1) ; |
1104 | print "Larry is there\n" if $hash{'Larry'} ; |
1105 | print "There are $hash{'Brick'} Brick Walls\n" ; |
1106 | |
1107 | my @list = sort $x->get_dup("Wall") ; |
1108 | print "Wall => [@list]\n" ; |
1109 | |
1110 | @list = $x->get_dup("Smith") ; |
1111 | print "Smith => [@list]\n" ; |
1112 | |
1113 | @list = $x->get_dup("Dog") ; |
1114 | print "Dog => [@list]\n" ; |
1115 | |
1116 | undef $x ; |
1117 | untie %h ; |
1118 | } |
1119 | |
efc79c7d |
1120 | ok(150, docat_del($file) eq <<'EOM') ; |
2c2d71f5 |
1121 | Wall occurred 3 times |
1122 | Larry is there |
1123 | There are 2 Brick Walls |
1124 | Wall => [Brick Brick Larry] |
1125 | Smith => [John] |
1126 | Dog => [] |
1127 | EOM |
1128 | |
1129 | { |
1130 | my $redirect = new Redirect $file ; |
1131 | |
1132 | # BTREE example 5 |
1133 | ### |
1134 | |
3245f058 |
1135 | use warnings FATAL => qw(all) ; |
2c2d71f5 |
1136 | use strict ; |
1137 | use DB_File ; |
1138 | |
962cee9f |
1139 | my ($filename, $x, %h, $found); |
2c2d71f5 |
1140 | |
07200f1b |
1141 | $filename = "tree" ; |
2c2d71f5 |
1142 | |
1143 | # Enable duplicate records |
1144 | $DB_BTREE->{'flags'} = R_DUP ; |
1145 | |
1146 | $x = tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0640, $DB_BTREE |
1147 | or die "Cannot open $filename: $!\n"; |
1148 | |
1149 | $found = ( $x->find_dup("Wall", "Larry") == 0 ? "" : "not") ; |
1150 | print "Larry Wall is $found there\n" ; |
1151 | |
1152 | $found = ( $x->find_dup("Wall", "Harry") == 0 ? "" : "not") ; |
1153 | print "Harry Wall is $found there\n" ; |
1154 | |
1155 | undef $x ; |
1156 | untie %h ; |
1157 | } |
1158 | |
efc79c7d |
1159 | ok(151, docat_del($file) eq <<'EOM') ; |
2c2d71f5 |
1160 | Larry Wall is there |
1161 | Harry Wall is not there |
1162 | EOM |
1163 | |
1164 | { |
1165 | my $redirect = new Redirect $file ; |
1166 | |
1167 | # BTREE example 6 |
1168 | ### |
1169 | |
3245f058 |
1170 | use warnings FATAL => qw(all) ; |
2c2d71f5 |
1171 | use strict ; |
1172 | use DB_File ; |
1173 | |
962cee9f |
1174 | my ($filename, $x, %h, $found); |
2c2d71f5 |
1175 | |
07200f1b |
1176 | $filename = "tree" ; |
2c2d71f5 |
1177 | |
1178 | # Enable duplicate records |
1179 | $DB_BTREE->{'flags'} = R_DUP ; |
1180 | |
1181 | $x = tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0640, $DB_BTREE |
1182 | or die "Cannot open $filename: $!\n"; |
1183 | |
1184 | $x->del_dup("Wall", "Larry") ; |
1185 | |
1186 | $found = ( $x->find_dup("Wall", "Larry") == 0 ? "" : "not") ; |
1187 | print "Larry Wall is $found there\n" ; |
1188 | |
1189 | undef $x ; |
1190 | untie %h ; |
1191 | |
1192 | unlink $filename ; |
1193 | } |
1194 | |
efc79c7d |
1195 | ok(152, docat_del($file) eq <<'EOM') ; |
2c2d71f5 |
1196 | Larry Wall is not there |
1197 | EOM |
1198 | |
1199 | { |
1200 | my $redirect = new Redirect $file ; |
1201 | |
1202 | # BTREE example 7 |
1203 | ### |
1204 | |
3245f058 |
1205 | use warnings FATAL => qw(all) ; |
2c2d71f5 |
1206 | use strict ; |
1207 | use DB_File ; |
1208 | use Fcntl ; |
1209 | |
962cee9f |
1210 | my ($filename, $x, %h, $st, $key, $value); |
2c2d71f5 |
1211 | |
1212 | sub match |
1213 | { |
1214 | my $key = shift ; |
1215 | my $value = 0; |
1216 | my $orig_key = $key ; |
1217 | $x->seq($key, $value, R_CURSOR) ; |
1218 | print "$orig_key\t-> $key\t-> $value\n" ; |
1219 | } |
1220 | |
1221 | $filename = "tree" ; |
1222 | unlink $filename ; |
1223 | |
1224 | $x = tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0640, $DB_BTREE |
1225 | or die "Cannot open $filename: $!\n"; |
1226 | |
1227 | # Add some key/value pairs to the file |
1228 | $h{'mouse'} = 'mickey' ; |
1229 | $h{'Wall'} = 'Larry' ; |
1230 | $h{'Walls'} = 'Brick' ; |
1231 | $h{'Smith'} = 'John' ; |
1232 | |
1233 | |
1234 | $key = $value = 0 ; |
1235 | print "IN ORDER\n" ; |
1236 | for ($st = $x->seq($key, $value, R_FIRST) ; |
1237 | $st == 0 ; |
1238 | $st = $x->seq($key, $value, R_NEXT) ) |
1239 | |
1240 | { print "$key -> $value\n" } |
1241 | |
1242 | print "\nPARTIAL MATCH\n" ; |
1243 | |
1244 | match "Wa" ; |
1245 | match "A" ; |
1246 | match "a" ; |
1247 | |
1248 | undef $x ; |
1249 | untie %h ; |
1250 | |
1251 | unlink $filename ; |
1252 | |
1253 | } |
1254 | |
efc79c7d |
1255 | ok(153, docat_del($file) eq <<'EOM') ; |
2c2d71f5 |
1256 | IN ORDER |
1257 | Smith -> John |
1258 | Wall -> Larry |
1259 | Walls -> Brick |
1260 | mouse -> mickey |
1261 | |
1262 | PARTIAL MATCH |
1263 | Wa -> Wall -> Larry |
1264 | A -> Smith -> John |
1265 | a -> mouse -> mickey |
1266 | EOM |
1267 | |
1268 | } |
1269 | |
a62982a8 |
1270 | #{ |
1271 | # # R_SETCURSOR |
1272 | # use strict ; |
1273 | # my (%h, $db) ; |
1274 | # unlink $Dfile; |
1275 | # |
1276 | # ok(156, $db = tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_BTREE ) ); |
1277 | # |
1278 | # $h{abc} = 33 ; |
1279 | # my $k = "newest" ; |
1280 | # my $v = 44 ; |
1281 | # my $status = $db->put($k, $v, R_SETCURSOR) ; |
1282 | # print "status = [$status]\n" ; |
1283 | # ok(157, $status == 0) ; |
1284 | # $status = $db->del($k, R_CURSOR) ; |
1285 | # print "status = [$status]\n" ; |
1286 | # ok(158, $status == 0) ; |
1287 | # $k = "newest" ; |
1288 | # ok(159, $db->get($k, $v, R_CURSOR)) ; |
1289 | # |
1290 | # ok(160, keys %h == 1) ; |
1291 | # |
1292 | # undef $db ; |
1293 | # untie %h; |
1294 | # unlink $Dfile; |
1295 | #} |
1296 | |
cbc5248d |
1297 | { |
1298 | # Bug ID 20001013.009 |
1299 | # |
1300 | # test that $hash{KEY} = undef doesn't produce the warning |
1301 | # Use of uninitialized value in null operation |
1302 | use warnings ; |
1303 | use strict ; |
1304 | use DB_File ; |
1305 | |
1306 | unlink $Dfile; |
1307 | my %h ; |
1308 | my $a = ""; |
1309 | local $SIG{__WARN__} = sub {$a = $_[0]} ; |
1310 | |
3245f058 |
1311 | tie %h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0664, $DB_BTREE |
cbc5248d |
1312 | or die "Can't open file: $!\n" ; |
1313 | $h{ABC} = undef; |
efc79c7d |
1314 | ok(154, $a eq "") ; |
cbc5248d |
1315 | untie %h ; |
1316 | unlink $Dfile; |
1317 | } |
1318 | |
3245f058 |
1319 | { |
1320 | # test that %hash = () doesn't produce the warning |
1321 | # Argument "" isn't numeric in entersub |
1322 | use warnings ; |
1323 | use strict ; |
1324 | use DB_File ; |
1325 | |
1326 | unlink $Dfile; |
1327 | my %h ; |
1328 | my $a = ""; |
1329 | local $SIG{__WARN__} = sub {$a = $_[0]} ; |
1330 | |
1331 | tie %h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0664, $DB_BTREE |
1332 | or die "Can't open file: $!\n" ; |
1333 | %h = (); ; |
efc79c7d |
1334 | ok(155, $a eq "") ; |
3245f058 |
1335 | untie %h ; |
1336 | unlink $Dfile; |
1337 | } |
1338 | |
0bf2e707 |
1339 | { |
1340 | # When iterating over a tied hash using "each", the key passed to FETCH |
1341 | # will be recycled and passed to NEXTKEY. If a Source Filter modifies the |
1342 | # key in FETCH via a filter_fetch_key method we need to check that the |
1343 | # modified key doesn't get passed to NEXTKEY. |
1344 | # Also Test "keys" & "values" while we are at it. |
1345 | |
1346 | use warnings ; |
1347 | use strict ; |
1348 | use DB_File ; |
1349 | |
1350 | unlink $Dfile; |
1351 | my $bad_key = 0 ; |
1352 | my %h = () ; |
1353 | my $db ; |
efc79c7d |
1354 | ok(156, $db = tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_BTREE ) ); |
0bf2e707 |
1355 | $db->filter_fetch_key (sub { $_ =~ s/^Beta_/Alpha_/ if defined $_}) ; |
1356 | $db->filter_store_key (sub { $bad_key = 1 if /^Beta_/ ; $_ =~ s/^Alpha_/Beta_/}) ; |
1357 | |
1358 | $h{'Alpha_ABC'} = 2 ; |
1359 | $h{'Alpha_DEF'} = 5 ; |
1360 | |
efc79c7d |
1361 | ok(157, $h{'Alpha_ABC'} == 2); |
1362 | ok(158, $h{'Alpha_DEF'} == 5); |
0bf2e707 |
1363 | |
1364 | my ($k, $v) = ("",""); |
1365 | while (($k, $v) = each %h) {} |
efc79c7d |
1366 | ok(159, $bad_key == 0); |
0bf2e707 |
1367 | |
1368 | $bad_key = 0 ; |
1369 | foreach $k (keys %h) {} |
efc79c7d |
1370 | ok(160, $bad_key == 0); |
0bf2e707 |
1371 | |
1372 | $bad_key = 0 ; |
1373 | foreach $v (values %h) {} |
efc79c7d |
1374 | ok(161, $bad_key == 0); |
0bf2e707 |
1375 | |
1376 | undef $db ; |
1377 | untie %h ; |
1378 | unlink $Dfile; |
1379 | } |
1380 | |
efc79c7d |
1381 | { |
1382 | # now an error to pass 'compare' a non-code reference |
1383 | my $dbh = new DB_File::BTREEINFO ; |
1384 | |
1385 | eval { $dbh->{compare} = 2 }; |
1386 | ok(162, $@ =~ /^Key 'compare' not associated with a code reference at/); |
1387 | |
1388 | eval { $dbh->{prefix} = 2 }; |
1389 | ok(163, $@ =~ /^Key 'prefix' not associated with a code reference at/); |
1390 | |
1391 | } |
1392 | |
1393 | |
262eaca6 |
1394 | #{ |
1395 | # # recursion detection in btree |
1396 | # my %hash ; |
1397 | # unlink $Dfile; |
1398 | # my $dbh = new DB_File::BTREEINFO ; |
1399 | # $dbh->{compare} = sub { $hash{3} = 4 ; length $_[0] } ; |
1400 | # |
1401 | # |
1402 | # my (%h); |
1403 | # ok(164, tie(%hash, 'DB_File',$Dfile, O_RDWR|O_CREAT, 0640, $dbh ) ); |
1404 | # |
1405 | # eval { $hash{1} = 2; |
1406 | # $hash{4} = 5; |
1407 | # }; |
1408 | # |
1409 | # ok(165, $@ =~ /^DB_File btree_compare: recursion detected/); |
1410 | # { |
1411 | # no warnings; |
1412 | # untie %hash; |
1413 | # } |
1414 | # unlink $Dfile; |
1415 | #} |
1416 | ok(164,1); |
1417 | ok(165,1); |
efc79c7d |
1418 | |
1419 | { |
1420 | # Check that two callbacks don't interact |
1421 | my %hash1 ; |
1422 | my %hash2 ; |
1423 | my $h1_count = 0; |
1424 | my $h2_count = 0; |
1425 | unlink $Dfile, $Dfile2; |
1426 | my $dbh1 = new DB_File::BTREEINFO ; |
1427 | $dbh1->{compare} = sub { ++ $h1_count ; $_[0] cmp $_[1] } ; |
1428 | |
1429 | my $dbh2 = new DB_File::BTREEINFO ; |
1430 | $dbh2->{compare} = sub { ;++ $h2_count ; $_[0] cmp $_[1] } ; |
1431 | |
1432 | |
1433 | |
1434 | my (%h); |
1435 | ok(166, tie(%hash1, 'DB_File',$Dfile, O_RDWR|O_CREAT, 0640, $dbh1 ) ); |
1436 | ok(167, tie(%hash2, 'DB_File',$Dfile2, O_RDWR|O_CREAT, 0640, $dbh2 ) ); |
1437 | |
1438 | $hash1{DEFG} = 5; |
1439 | $hash1{XYZ} = 2; |
1440 | $hash1{ABCDE} = 5; |
1441 | |
1442 | $hash2{defg} = 5; |
1443 | $hash2{xyz} = 2; |
1444 | $hash2{abcde} = 5; |
1445 | |
1446 | ok(168, $h1_count > 0); |
1447 | ok(169, $h1_count == $h2_count); |
1448 | |
1449 | ok(170, safeUntie \%hash1); |
1450 | ok(171, safeUntie \%hash2); |
1451 | unlink $Dfile, $Dfile2; |
1452 | } |
1453 | |
1454 | { |
1455 | # Check that DBM Filter can cope with read-only $_ |
1456 | |
1457 | use warnings ; |
1458 | use strict ; |
1459 | my (%h, $db) ; |
1460 | unlink $Dfile; |
1461 | |
1462 | ok(172, $db = tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_BTREE ) ); |
1463 | |
1464 | $db->filter_fetch_key (sub { }) ; |
1465 | $db->filter_store_key (sub { }) ; |
1466 | $db->filter_fetch_value (sub { }) ; |
1467 | $db->filter_store_value (sub { }) ; |
1468 | |
1469 | $_ = "original" ; |
1470 | |
1471 | $h{"fred"} = "joe" ; |
1472 | ok(173, $h{"fred"} eq "joe"); |
1473 | |
1474 | eval { grep { $h{$_} } (1, 2, 3) }; |
1475 | ok (174, ! $@); |
1476 | |
1477 | |
1478 | # delete the filters |
1479 | $db->filter_fetch_key (undef); |
1480 | $db->filter_store_key (undef); |
1481 | $db->filter_fetch_value (undef); |
1482 | $db->filter_store_value (undef); |
1483 | |
1484 | $h{"fred"} = "joe" ; |
1485 | |
1486 | ok(175, $h{"fred"} eq "joe"); |
1487 | |
1488 | ok(176, $db->FIRSTKEY() eq "fred") ; |
1489 | |
1490 | eval { grep { $h{$_} } (1, 2, 3) }; |
1491 | ok (177, ! $@); |
1492 | |
1493 | undef $db ; |
1494 | untie %h; |
1495 | unlink $Dfile; |
1496 | } |
1497 | |
5bbd4290 |
1498 | { |
1499 | # Check low-level API works with filter |
1500 | |
1501 | use warnings ; |
1502 | use strict ; |
1503 | my (%h, $db) ; |
1504 | my $Dfile = "xxy.db"; |
1505 | unlink $Dfile; |
1506 | |
1507 | ok(178, $db = tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_BTREE ) ); |
1508 | |
1509 | |
1510 | $db->filter_fetch_key (sub { $_ = unpack("i", $_) } ); |
1511 | $db->filter_store_key (sub { $_ = pack("i", $_) } ); |
1512 | $db->filter_fetch_value (sub { $_ = unpack("i", $_) } ); |
1513 | $db->filter_store_value (sub { $_ = pack("i", $_) } ); |
1514 | |
1515 | $_ = 'fred'; |
1516 | |
1517 | my $key = 22 ; |
1518 | my $value = 34 ; |
1519 | |
1520 | $db->put($key, $value) ; |
1521 | ok 179, $key == 22; |
1522 | ok 180, $value == 34 ; |
1523 | ok 181, $_ eq 'fred'; |
1524 | #print "k [$key][$value]\n" ; |
1525 | |
1526 | my $val ; |
1527 | $db->get($key, $val) ; |
1528 | ok 182, $key == 22; |
1529 | ok 183, $val == 34 ; |
1530 | ok 184, $_ eq 'fred'; |
1531 | |
1532 | $key = 51 ; |
1533 | $value = 454; |
1534 | $h{$key} = $value ; |
1535 | ok 185, $key == 51; |
1536 | ok 186, $value == 454 ; |
1537 | ok 187, $_ eq 'fred'; |
1538 | |
1539 | undef $db ; |
1540 | untie %h; |
1541 | unlink $Dfile; |
1542 | } |
9c095db2 |
1543 | |
1544 | |
1545 | |
1546 | { |
1547 | # Regression Test for bug 30237 |
1548 | # Check that substr can be used in the key to db_put |
1549 | # and that db_put does not trigger the warning |
1550 | # |
1551 | # Use of uninitialized value in subroutine entry |
1552 | |
1553 | |
1554 | use warnings ; |
1555 | use strict ; |
1556 | my (%h, $db) ; |
1557 | my $Dfile = "xxy.db"; |
1558 | unlink $Dfile; |
1559 | |
1560 | ok(188, $db = tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_BTREE )); |
1561 | |
1562 | my $warned = ''; |
1563 | local $SIG{__WARN__} = sub {$warned = $_[0]} ; |
1564 | |
1565 | # db-put with substr of key |
1566 | my %remember = () ; |
1567 | for my $ix ( 10 .. 12 ) |
1568 | { |
1569 | my $key = $ix . "data" ; |
1570 | my $value = "value$ix" ; |
1571 | $remember{$key} = $value ; |
1572 | $db->put(substr($key,0), $value) ; |
1573 | } |
1574 | |
1575 | ok 189, $warned eq '' |
1576 | or print "# Caught warning [$warned]\n" ; |
1577 | |
1578 | # db-put with substr of value |
1579 | $warned = ''; |
1580 | for my $ix ( 20 .. 22 ) |
1581 | { |
1582 | my $key = $ix . "data" ; |
1583 | my $value = "value$ix" ; |
1584 | $remember{$key} = $value ; |
1585 | $db->put($key, substr($value,0)) ; |
1586 | } |
1587 | |
1588 | ok 190, $warned eq '' |
1589 | or print "# Caught warning [$warned]\n" ; |
1590 | |
1591 | # via the tied hash is not a problem, but check anyway |
1592 | # substr of key |
1593 | $warned = ''; |
1594 | for my $ix ( 30 .. 32 ) |
1595 | { |
1596 | my $key = $ix . "data" ; |
1597 | my $value = "value$ix" ; |
1598 | $remember{$key} = $value ; |
1599 | $h{substr($key,0)} = $value ; |
1600 | } |
1601 | |
1602 | ok 191, $warned eq '' |
1603 | or print "# Caught warning [$warned]\n" ; |
1604 | |
1605 | # via the tied hash is not a problem, but check anyway |
1606 | # substr of value |
1607 | $warned = ''; |
1608 | for my $ix ( 40 .. 42 ) |
1609 | { |
1610 | my $key = $ix . "data" ; |
1611 | my $value = "value$ix" ; |
1612 | $remember{$key} = $value ; |
1613 | $h{$key} = substr($value,0) ; |
1614 | } |
1615 | |
1616 | ok 192, $warned eq '' |
1617 | or print "# Caught warning [$warned]\n" ; |
1618 | |
1619 | my %bad = () ; |
1620 | $key = ''; |
1621 | for ($status = $db->seq($key, $value, R_FIRST ) ; |
1622 | $status == 0 ; |
1623 | $status = $db->seq($key, $value, R_NEXT ) ) { |
1624 | |
1625 | #print "# key [$key] value [$value]\n" ; |
1626 | if (defined $remember{$key} && defined $value && |
1627 | $remember{$key} eq $value) { |
1628 | delete $remember{$key} ; |
1629 | } |
1630 | else { |
1631 | $bad{$key} = $value ; |
1632 | } |
1633 | } |
1634 | |
1635 | ok 193, keys %bad == 0 ; |
1636 | ok 194, keys %remember == 0 ; |
1637 | |
1638 | print "# missing -- $key $value\n" while ($key, $value) = each %remember; |
1639 | print "# bad -- $key $value\n" while ($key, $value) = each %bad; |
1640 | |
1641 | # Make sure this fix does not break code to handle an undef key |
1642 | # Berkeley DB undef key is bron between versions 2.3.16 and |
1643 | my $value = 'fred'; |
1644 | $warned = ''; |
1645 | $db->put(undef, $value) ; |
1646 | ok 195, $warned eq '' |
1647 | or print "# Caught warning [$warned]\n" ; |
1648 | $warned = ''; |
1649 | |
1650 | my $no_NULL = ($DB_File::db_ver >= 2.003016 && $DB_File::db_ver < 3.001) ; |
1651 | print "# db_ver $DB_File::db_ver\n"; |
1652 | $value = '' ; |
1653 | $db->get(undef, $value) ; |
1654 | ok 196, $no_NULL || $value eq 'fred' or print "# got [$value]\n" ; |
1655 | ok 197, $warned eq '' |
1656 | or print "# Caught warning [$warned]\n" ; |
1657 | $warned = ''; |
1658 | |
1659 | undef $db ; |
1660 | untie %h; |
1661 | unlink $Dfile; |
1662 | } |
a0d0e21e |
1663 | exit ; |