6 require Config; import Config;
7 if ($Config{'extensions'} !~ /\bDB_File\b/) {
8 print "1..0 # Skip: DB_File was not built\n";
25 print "not " unless $result ;
31 my(@a) = unpack ("C*", $a) ;
32 my(@b) = unpack ("C*", $b) ;
34 my $len = (@a > @b ? @b : @a) ;
37 foreach $i ( 0 .. $len -1) {
38 return $a[$i] - $b[$i] if $a[$i] != $b[$i] ;
51 my $filename = shift ;
53 open ($fh, ">$filename") || die "Cannot open $filename: $!" ;
54 my $real_stdout = select($fh) ;
55 return bless [$fh, $real_stdout ] ;
69 #local $/ = undef unless wantarray ;
70 open(CAT,$file) || die "Cannot open $file: $!";
73 wantarray ? @result : join("", @result) ;
79 #local $/ = undef unless wantarray ;
80 open(CAT,$file) || die "Cannot open $file: $!";
84 wantarray ? @result : join("", @result) ;
88 my $db185mode = ($DB_File::db_version == 1 && ! $DB_File::db_185_compat) ;
89 my $null_keys_allowed = ($DB_File::db_ver < 2.004010
90 || $DB_File::db_ver >= 3.1 );
92 my $Dfile = "dbbtree.tmp";
97 # Check the interface to BTREEINFO
99 my $dbh = new DB_File::BTREEINFO ;
100 ok(1, ! defined $dbh->{flags}) ;
101 ok(2, ! defined $dbh->{cachesize}) ;
102 ok(3, ! defined $dbh->{psize}) ;
103 ok(4, ! defined $dbh->{lorder}) ;
104 ok(5, ! defined $dbh->{minkeypage}) ;
105 ok(6, ! defined $dbh->{maxkeypage}) ;
106 ok(7, ! defined $dbh->{compare}) ;
107 ok(8, ! defined $dbh->{prefix}) ;
109 $dbh->{flags} = 3000 ;
110 ok(9, $dbh->{flags} == 3000) ;
112 $dbh->{cachesize} = 9000 ;
113 ok(10, $dbh->{cachesize} == 9000);
115 $dbh->{psize} = 400 ;
116 ok(11, $dbh->{psize} == 400) ;
118 $dbh->{lorder} = 65 ;
119 ok(12, $dbh->{lorder} == 65) ;
121 $dbh->{minkeypage} = 123 ;
122 ok(13, $dbh->{minkeypage} == 123) ;
124 $dbh->{maxkeypage} = 1234 ;
125 ok(14, $dbh->{maxkeypage} == 1234 );
127 $dbh->{compare} = 1234 ;
128 ok(15, $dbh->{compare} == 1234) ;
130 $dbh->{prefix} = 1234 ;
131 ok(16, $dbh->{prefix} == 1234 );
133 # Check that an invalid entry is caught both for store & fetch
134 eval '$dbh->{fred} = 1234' ;
135 ok(17, $@ =~ /^DB_File::BTREEINFO::STORE - Unknown element 'fred' at/ ) ;
136 eval 'my $q = $dbh->{fred}' ;
137 ok(18, $@ =~ /^DB_File::BTREEINFO::FETCH - Unknown element 'fred' at/ ) ;
139 # Now check the interface to BTREE
142 ok(19, $X = tie(%h, 'DB_File',$Dfile, O_RDWR|O_CREAT, 0640, $DB_BTREE )) ;
144 my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
145 $blksize,$blocks) = stat($Dfile);
146 ok(20, ($mode & 0777) == (($^O eq 'os2' || $^O eq 'MacOS') ? 0666 : 0640)
147 || $^O eq 'amigaos' || $^O eq 'MSWin32' || $^O eq 'cygwin' || $^O eq 'NetWare');
149 my ($key, $value, $i);
150 while (($key,$value) = each(%h)) {
155 $h{'goner1'} = 'snork';
158 ok(22, $h{'abc'} eq 'ABC' );
159 ok(23, ! defined $h{'jimmy'} ) ;
160 ok(24, ! exists $h{'jimmy'} ) ;
161 ok(25, defined $h{'abc'} ) ;
164 $h{'jkl','mno'} = "JKL\034MNO";
165 $h{'a',2,3,4,5} = join("\034",'A',2,3,4,5);
169 $X->STORE('b', 'B') ;
182 $h{'goner2'} = 'snork';
186 # IMPORTANT - $X must be undefined before the untie otherwise the
187 # underlying DB close routine will not get called.
191 # tie to the same file again
192 ok(26, $X = tie(%h,'DB_File',$Dfile, O_RDWR, 0640, $DB_BTREE)) ;
194 # Modify an entry from the previous tie
215 $h{'goner3'} = 'snork';
218 $X->DELETE('goner3');
221 my @values = values(%h);
223 ok(27, $#keys == 29 && $#values == 29) ;
226 while (($key,$value) = each(%h)) {
227 if ($key eq $keys[$i] && $value eq $values[$i] && $key eq lc($value)) {
229 $i++ if $key eq $value;
235 @keys = ('blurfl', keys(%h), 'dyick');
236 ok(29, $#keys == 31) ;
238 #Check that the keys can be retrieved in order
240 my @c = sort lexical @b ;
241 ok(30, ArrayCompare(\@b, \@c)) ;
244 ok(31, $h{'foo'} eq '' ) ;
246 # Berkeley DB from version 2.4.10 to 3.0 does not allow null keys.
247 # This feature was reenabled in version 3.1 of Berkeley DB.
249 if ($null_keys_allowed) {
251 $result = ( $h{''} eq 'bar' );
257 # check cache overflow and numeric keys and contents
259 for ($i = 1; $i < 200; $i++) { $h{$i + 0} = $i + 0; }
260 for ($i = 1; $i < 200; $i++) { $ok = 0 unless $h{$i} == $i; }
263 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
264 $blksize,$blocks) = stat($Dfile);
267 @h{0..200} = 200..400;
268 my @foo = @h{0..200};
269 ok(35, join(':',200..400) eq join(':',@foo) );
271 # Now check all the non-tie specific stuff
274 # Check R_NOOVERWRITE flag will make put fail when attempting to overwrite
275 # an existing record.
277 my $status = $X->put( 'x', 'newvalue', R_NOOVERWRITE) ;
278 ok(36, $status == 1 );
280 # check that the value of the key 'x' has not been changed by the
282 ok(37, $h{'x'} eq 'X' );
285 $status = $X->put('key', 'value') ;
286 ok(38, $status == 0 );
288 #check that previous put can be retrieved
290 $status = $X->get('key', $value) ;
291 ok(39, $status == 0 );
292 ok(40, $value eq 'value' );
294 # Attempting to delete an existing key should work
296 $status = $X->del('q') ;
297 ok(41, $status == 0 );
298 if ($null_keys_allowed) {
299 $status = $X->del('') ;
303 ok(42, $status == 0 );
305 # Make sure that the key deleted, cannot be retrieved
306 ok(43, ! defined $h{'q'}) ;
307 ok(44, ! defined $h{''}) ;
312 ok(45, $X = tie(%h, 'DB_File',$Dfile, O_RDWR, 0640, $DB_BTREE ));
314 # Attempting to delete a non-existant key should fail
316 $status = $X->del('joe') ;
317 ok(46, $status == 1 );
319 # Check the get interface
321 # First a non-existing key
322 $status = $X->get('aaaa', $value) ;
323 ok(47, $status == 1 );
325 # Next an existing key
326 $status = $X->get('a', $value) ;
327 ok(48, $status == 0 );
328 ok(49, $value eq 'A' );
333 # use seq to find an approximate match
336 $status = $X->seq($key, $value, R_CURSOR) ;
337 ok(50, $status == 0 );
338 ok(51, $key eq 'key' );
339 ok(52, $value eq 'value' );
341 # seq when the key does not match
344 $status = $X->seq($key, $value, R_CURSOR) ;
345 ok(53, $status == 1 );
348 # use seq to set the cursor, then delete the record @ the cursor.
352 $status = $X->seq($key, $value, R_CURSOR) ;
353 ok(54, $status == 0 );
354 ok(55, $key eq 'x' );
355 ok(56, $value eq 'X' );
356 $status = $X->del(0, R_CURSOR) ;
357 ok(57, $status == 0 );
358 $status = $X->get('x', $value) ;
359 ok(58, $status == 1 );
361 # ditto, but use put to replace the key/value pair.
364 $status = $X->seq($key, $value, R_CURSOR) ;
365 ok(59, $status == 0 );
366 ok(60, $key eq 'y' );
367 ok(61, $value eq 'Y' );
369 $key = "replace key" ;
370 $value = "replace value" ;
371 $status = $X->put($key, $value, R_CURSOR) ;
372 ok(62, $status == 0 );
373 ok(63, $key eq 'replace key' );
374 ok(64, $value eq 'replace value' );
375 $status = $X->get('y', $value) ;
376 ok(65, 1) ; # hard-wire to always pass. the previous test ($status == 1)
377 # only worked because of a bug in 1.85/6
379 # use seq to walk forwards through a file
381 $status = $X->seq($key, $value, R_FIRST) ;
382 ok(66, $status == 0 );
383 my $previous = $key ;
386 while (($status = $X->seq($key, $value, R_NEXT)) == 0)
388 ($ok = 0), last if ($previous cmp $key) == 1 ;
391 ok(67, $status == 1 );
394 # use seq to walk backwards through a file
395 $status = $X->seq($key, $value, R_LAST) ;
396 ok(69, $status == 0 );
400 while (($status = $X->seq($key, $value, R_PREV)) == 0)
402 ($ok = 0), last if ($previous cmp $key) == -1 ;
403 #print "key = [$key] value = [$value]\n" ;
406 ok(70, $status == 1 );
410 # check seq FIRST/LAST
416 ok(72, $status == 0 );
423 ok(73, $status != 0 );
431 # Now try an in memory file
433 ok(74, $Y = tie(%h, 'DB_File',undef, O_RDWR|O_CREAT, 0640, $DB_BTREE ));
435 # fd with an in memory file should return failure
437 ok(75, $status == -1 );
444 my $bt = new DB_File::BTREEINFO ;
445 $bt->{flags} = R_DUP ;
447 ok(76, $YY = tie(%hh, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $bt )) ;
449 $hh{'Wall'} = 'Larry' ;
450 $hh{'Wall'} = 'Stone' ; # Note the duplicate key
451 $hh{'Wall'} = 'Brick' ; # Note the duplicate key
452 $hh{'Wall'} = 'Brick' ; # Note the duplicate key and value
453 $hh{'Smith'} = 'John' ;
454 $hh{'mouse'} = 'mickey' ;
456 # first work in scalar context
457 ok(77, scalar $YY->get_dup('Unknown') == 0 );
458 ok(78, scalar $YY->get_dup('Smith') == 1 );
459 ok(79, scalar $YY->get_dup('Wall') == 4 );
461 # now in list context
462 my @unknown = $YY->get_dup('Unknown') ;
463 ok(80, "@unknown" eq "" );
465 my @smith = $YY->get_dup('Smith') ;
466 ok(81, "@smith" eq "John" );
469 my @wall = $YY->get_dup('Wall') ;
471 @wall{@wall} = @wall ;
472 ok(82, (@wall == 4 && $wall{'Larry'} && $wall{'Stone'} && $wall{'Brick'}) );
476 my %unknown = $YY->get_dup('Unknown', 1) ;
477 ok(83, keys %unknown == 0 );
479 my %smith = $YY->get_dup('Smith', 1) ;
480 ok(84, keys %smith == 1 && $smith{'John'}) ;
482 my %wall = $YY->get_dup('Wall', 1) ;
483 ok(85, keys %wall == 3 && $wall{'Larry'} == 1 && $wall{'Stone'} == 1
484 && $wall{'Brick'} == 2);
491 # test multiple callbacks
492 my $Dfile1 = "btree1" ;
493 my $Dfile2 = "btree2" ;
494 my $Dfile3 = "btree3" ;
496 my $dbh1 = new DB_File::BTREEINFO ;
497 $dbh1->{compare} = sub {
498 no warnings 'numeric' ;
501 my $dbh2 = new DB_File::BTREEINFO ;
502 $dbh2->{compare} = sub { $_[0] cmp $_[1] } ;
504 my $dbh3 = new DB_File::BTREEINFO ;
505 $dbh3->{compare} = sub { length $_[0] <=> length $_[1] } ;
509 tie(%h, 'DB_File',$Dfile1, O_RDWR|O_CREAT, 0640, $dbh1 ) ;
510 tie(%g, 'DB_File',$Dfile2, O_RDWR|O_CREAT, 0640, $dbh2 ) ;
511 tie(%k, 'DB_File',$Dfile3, O_RDWR|O_CREAT, 0640, $dbh3 ) ;
513 my @Keys = qw( 0123 12 -1234 9 987654321 def ) ;
514 my (@srt_1, @srt_2, @srt_3);
516 no warnings 'numeric' ;
517 @srt_1 = sort { $a <=> $b } @Keys ;
519 @srt_2 = sort { $a cmp $b } @Keys ;
520 @srt_3 = sort { length $a <=> length $b } @Keys ;
532 return 0 if @$a != @$b ;
534 foreach (1 .. length @$a)
536 return 0 unless $$a[$_] eq $$b[$_] ;
542 ok(86, ArrayCompare (\@srt_1, [keys %h]) );
543 ok(87, ArrayCompare (\@srt_2, [keys %g]) );
544 ok(88, ArrayCompare (\@srt_3, [keys %k]) );
549 unlink $Dfile1, $Dfile2, $Dfile3 ;
554 ok(89, tie(%h, 'DB_File', $Dfile1, O_RDWR|O_CREAT, 0640, $DB_BTREE ) );
556 { $h{$_} = $_ * 100 }
558 # check that there are 10 elements in the hash
560 while (($key,$value) = each(%h)) {
570 while (($key,$value) = each(%h)) {
579 # check that attempting to tie an array to a DB_BTREE will fail
581 my $filename = "xyz" ;
583 eval { tie @x, 'DB_File', $filename, O_RDWR|O_CREAT, 0640, $DB_BTREE ; } ;
584 ok(92, $@ =~ /^DB_File can only tie an associative array to a DB_BTREE database/) ;
596 open(FILE, ">SubDB.pm") or die "Cannot open SubDB.pm: $!\n" ;
603 use vars qw( @ISA @EXPORT) ;
608 @EXPORT = @DB_File::EXPORT ;
614 $self->SUPER::STORE($key, $value * 2) ;
620 $self->SUPER::FETCH($key) - 1 ;
627 $self->SUPER::put($key, $value * 3) ;
632 $self->SUPER::get($_[0], $_[1]) ;
640 my $value = $self->FETCH($key) ;
641 return "[[$value]]" ;
649 BEGIN { push @INC, '.'; }
651 main::ok(93, $@ eq "") ;
655 $X = tie(%h, "SubDB","dbbtree.tmp", O_RDWR|O_CREAT, 0640, $DB_BTREE );
658 main::ok(94, $@ eq "") ;
660 my $ret = eval '$h{"fred"} = 3 ; return $h{"fred"} ' ;
661 main::ok(95, $@ eq "") ;
662 main::ok(96, $ret == 5) ;
665 $ret = eval '$X->put("joe", 4) ; $X->get("joe", $value) ; return $value' ;
666 main::ok(97, $@ eq "") ;
667 main::ok(98, $ret == 10) ;
669 $ret = eval ' R_NEXT eq main::R_NEXT ' ;
670 main::ok(99, $@ eq "" ) ;
671 main::ok(100, $ret == 1) ;
673 $ret = eval '$X->A_new_method("joe") ' ;
674 main::ok(101, $@ eq "") ;
675 main::ok(102, $ret eq "[[11]]") ;
679 unlink "SubDB.pm", "dbbtree.tmp" ;
688 my ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
693 my($fk, $sk, $fv, $sv) = @_ ;
695 $fetch_key eq $fk && $store_key eq $sk &&
696 $fetch_value eq $fv && $store_value eq $sv &&
700 ok(103, $db = tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_BTREE ) );
702 $db->filter_fetch_key (sub { $fetch_key = $_ }) ;
703 $db->filter_store_key (sub { $store_key = $_ }) ;
704 $db->filter_fetch_value (sub { $fetch_value = $_}) ;
705 $db->filter_store_value (sub { $store_value = $_ }) ;
711 ok(104, checkOutput( "", "fred", "", "joe")) ;
713 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
714 ok(105, $h{"fred"} eq "joe");
716 ok(106, checkOutput( "", "fred", "joe", "")) ;
718 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
719 ok(107, $db->FIRSTKEY() eq "fred") ;
721 ok(108, checkOutput( "fred", "", "", "")) ;
723 # replace the filters, but remember the previous set
724 my ($old_fk) = $db->filter_fetch_key
725 (sub { $_ = uc $_ ; $fetch_key = $_ }) ;
726 my ($old_sk) = $db->filter_store_key
727 (sub { $_ = lc $_ ; $store_key = $_ }) ;
728 my ($old_fv) = $db->filter_fetch_value
729 (sub { $_ = "[$_]"; $fetch_value = $_ }) ;
730 my ($old_sv) = $db->filter_store_value
731 (sub { s/o/x/g; $store_value = $_ }) ;
733 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
736 ok(109, checkOutput( "", "fred", "", "Jxe")) ;
738 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
739 ok(110, $h{"Fred"} eq "[Jxe]");
741 ok(111, checkOutput( "", "fred", "[Jxe]", "")) ;
743 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
744 ok(112, $db->FIRSTKEY() eq "FRED") ;
746 ok(113, checkOutput( "FRED", "", "", "")) ;
748 # put the original filters back
749 $db->filter_fetch_key ($old_fk);
750 $db->filter_store_key ($old_sk);
751 $db->filter_fetch_value ($old_fv);
752 $db->filter_store_value ($old_sv);
754 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
756 ok(114, checkOutput( "", "fred", "", "joe")) ;
758 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
759 ok(115, $h{"fred"} eq "joe");
760 ok(116, checkOutput( "", "fred", "joe", "")) ;
762 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
763 ok(117, $db->FIRSTKEY() eq "fred") ;
764 ok(118, checkOutput( "fred", "", "", "")) ;
767 $db->filter_fetch_key (undef);
768 $db->filter_store_key (undef);
769 $db->filter_fetch_value (undef);
770 $db->filter_store_value (undef);
772 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
774 ok(119, checkOutput( "", "", "", "")) ;
776 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
777 ok(120, $h{"fred"} eq "joe");
778 ok(121, checkOutput( "", "", "", "")) ;
780 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
781 ok(122, $db->FIRSTKEY() eq "fred") ;
782 ok(123, checkOutput( "", "", "", "")) ;
790 # DBM Filter with a closure
797 ok(124, $db = tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_BTREE ) );
807 return sub { ++$count ;
809 $result{$name} = "$name - $count: [@kept]" ;
813 $db->filter_store_key(Closure("store key")) ;
814 $db->filter_store_value(Closure("store value")) ;
815 $db->filter_fetch_key(Closure("fetch key")) ;
816 $db->filter_fetch_value(Closure("fetch value")) ;
821 ok(125, $result{"store key"} eq "store key - 1: [fred]");
822 ok(126, $result{"store value"} eq "store value - 1: [joe]");
823 ok(127, ! defined $result{"fetch key"} );
824 ok(128, ! defined $result{"fetch value"} );
825 ok(129, $_ eq "original") ;
827 ok(130, $db->FIRSTKEY() eq "fred") ;
828 ok(131, $result{"store key"} eq "store key - 1: [fred]");
829 ok(132, $result{"store value"} eq "store value - 1: [joe]");
830 ok(133, $result{"fetch key"} eq "fetch key - 1: [fred]");
831 ok(134, ! defined $result{"fetch value"} );
832 ok(135, $_ eq "original") ;
835 ok(136, $result{"store key"} eq "store key - 2: [fred jim]");
836 ok(137, $result{"store value"} eq "store value - 2: [joe john]");
837 ok(138, $result{"fetch key"} eq "fetch key - 1: [fred]");
838 ok(139, ! defined $result{"fetch value"} );
839 ok(140, $_ eq "original") ;
841 ok(141, $h{"fred"} eq "joe");
842 ok(142, $result{"store key"} eq "store key - 3: [fred jim fred]");
843 ok(143, $result{"store value"} eq "store value - 2: [joe john]");
844 ok(144, $result{"fetch key"} eq "fetch key - 1: [fred]");
845 ok(145, $result{"fetch value"} eq "fetch value - 1: [joe]");
846 ok(146, $_ eq "original") ;
854 # DBM Filter recursion detection
860 ok(147, $db = tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_BTREE ) );
862 $db->filter_store_key (sub { $_ = $h{$_} }) ;
864 eval '$h{1} = 1234' ;
865 ok(148, $@ =~ /^recursion detected in filter_store_key at/ );
874 # Examples from the POD
879 my $redirect = new Redirect $file ;
884 use warnings FATAL => qw(all) ;
892 my ($key1, $key2) = @_ ;
893 "\L$key1" cmp "\L$key2" ;
896 # specify the Perl sub that will do the comparison
897 $DB_BTREE->{'compare'} = \&Compare ;
900 tie %h, "DB_File", "tree", O_RDWR|O_CREAT, 0640, $DB_BTREE
901 or die "Cannot open file 'tree': $!\n" ;
903 # Add a key/value pair to the file
904 $h{'Wall'} = 'Larry' ;
905 $h{'Smith'} = 'John' ;
906 $h{'mouse'} = 'mickey' ;
907 $h{'duck'} = 'donald' ;
912 # Cycle through the keys printing them in order.
913 # Note it is not necessary to sort the keys as
914 # the btree will have kept them in order automatically.
923 delete $DB_BTREE->{'compare'} ;
925 ok(149, docat_del($file) eq <<'EOM') ;
932 my $redirect = new Redirect $file ;
937 use warnings FATAL => qw(all) ;
941 use vars qw($filename %h ) ;
946 # Enable duplicate records
947 $DB_BTREE->{'flags'} = R_DUP ;
949 tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0640, $DB_BTREE
950 or die "Cannot open $filename: $!\n";
952 # Add some key/value pairs to the file
953 $h{'Wall'} = 'Larry' ;
954 $h{'Wall'} = 'Brick' ; # Note the duplicate key
955 $h{'Wall'} = 'Brick' ; # Note the duplicate key and value
956 $h{'Smith'} = 'John' ;
957 $h{'mouse'} = 'mickey' ;
959 # iterate through the associative array
960 # and print each key/value pair.
962 { print "$_ -> $h{$_}\n" }
969 ok(150, docat_del($file) eq ($db185mode ? <<'EOM' : <<'EOM') ) ;
984 my $redirect = new Redirect $file ;
989 use warnings FATAL => qw(all) ;
993 use vars qw($filename $x %h $status $key $value) ;
998 # Enable duplicate records
999 $DB_BTREE->{'flags'} = R_DUP ;
1001 $x = tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0640, $DB_BTREE
1002 or die "Cannot open $filename: $!\n";
1004 # Add some key/value pairs to the file
1005 $h{'Wall'} = 'Larry' ;
1006 $h{'Wall'} = 'Brick' ; # Note the duplicate key
1007 $h{'Wall'} = 'Brick' ; # Note the duplicate key and value
1008 $h{'Smith'} = 'John' ;
1009 $h{'mouse'} = 'mickey' ;
1011 # iterate through the btree using seq
1012 # and print each key/value pair.
1014 for ($status = $x->seq($key, $value, R_FIRST) ;
1016 $status = $x->seq($key, $value, R_NEXT) )
1017 { print "$key -> $value\n" }
1024 ok(151, docat_del($file) eq ($db185mode == 1 ? <<'EOM' : <<'EOM') ) ;
1040 my $redirect = new Redirect $file ;
1045 use warnings FATAL => qw(all) ;
1049 use vars qw($filename $x %h ) ;
1051 $filename = "tree" ;
1053 # Enable duplicate records
1054 $DB_BTREE->{'flags'} = R_DUP ;
1056 $x = tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0640, $DB_BTREE
1057 or die "Cannot open $filename: $!\n";
1059 my $cnt = $x->get_dup("Wall") ;
1060 print "Wall occurred $cnt times\n" ;
1062 my %hash = $x->get_dup("Wall", 1) ;
1063 print "Larry is there\n" if $hash{'Larry'} ;
1064 print "There are $hash{'Brick'} Brick Walls\n" ;
1066 my @list = sort $x->get_dup("Wall") ;
1067 print "Wall => [@list]\n" ;
1069 @list = $x->get_dup("Smith") ;
1070 print "Smith => [@list]\n" ;
1072 @list = $x->get_dup("Dog") ;
1073 print "Dog => [@list]\n" ;
1079 ok(152, docat_del($file) eq <<'EOM') ;
1080 Wall occurred 3 times
1082 There are 2 Brick Walls
1083 Wall => [Brick Brick Larry]
1089 my $redirect = new Redirect $file ;
1094 use warnings FATAL => qw(all) ;
1098 use vars qw($filename $x %h $found) ;
1100 my $filename = "tree" ;
1102 # Enable duplicate records
1103 $DB_BTREE->{'flags'} = R_DUP ;
1105 $x = tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0640, $DB_BTREE
1106 or die "Cannot open $filename: $!\n";
1108 $found = ( $x->find_dup("Wall", "Larry") == 0 ? "" : "not") ;
1109 print "Larry Wall is $found there\n" ;
1111 $found = ( $x->find_dup("Wall", "Harry") == 0 ? "" : "not") ;
1112 print "Harry Wall is $found there\n" ;
1118 ok(153, docat_del($file) eq <<'EOM') ;
1120 Harry Wall is not there
1124 my $redirect = new Redirect $file ;
1129 use warnings FATAL => qw(all) ;
1133 use vars qw($filename $x %h $found) ;
1135 my $filename = "tree" ;
1137 # Enable duplicate records
1138 $DB_BTREE->{'flags'} = R_DUP ;
1140 $x = tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0640, $DB_BTREE
1141 or die "Cannot open $filename: $!\n";
1143 $x->del_dup("Wall", "Larry") ;
1145 $found = ( $x->find_dup("Wall", "Larry") == 0 ? "" : "not") ;
1146 print "Larry Wall is $found there\n" ;
1154 ok(154, docat_del($file) eq <<'EOM') ;
1155 Larry Wall is not there
1159 my $redirect = new Redirect $file ;
1164 use warnings FATAL => qw(all) ;
1169 use vars qw($filename $x %h $st $key $value) ;
1175 my $orig_key = $key ;
1176 $x->seq($key, $value, R_CURSOR) ;
1177 print "$orig_key\t-> $key\t-> $value\n" ;
1180 $filename = "tree" ;
1183 $x = tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0640, $DB_BTREE
1184 or die "Cannot open $filename: $!\n";
1186 # Add some key/value pairs to the file
1187 $h{'mouse'} = 'mickey' ;
1188 $h{'Wall'} = 'Larry' ;
1189 $h{'Walls'} = 'Brick' ;
1190 $h{'Smith'} = 'John' ;
1194 print "IN ORDER\n" ;
1195 for ($st = $x->seq($key, $value, R_FIRST) ;
1197 $st = $x->seq($key, $value, R_NEXT) )
1199 { print "$key -> $value\n" }
1201 print "\nPARTIAL MATCH\n" ;
1214 ok(155, docat_del($file) eq <<'EOM') ;
1224 a -> mouse -> mickey
1235 # ok(156, $db = tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_BTREE ) );
1238 # my $k = "newest" ;
1240 # my $status = $db->put($k, $v, R_SETCURSOR) ;
1241 # print "status = [$status]\n" ;
1242 # ok(157, $status == 0) ;
1243 # $status = $db->del($k, R_CURSOR) ;
1244 # print "status = [$status]\n" ;
1245 # ok(158, $status == 0) ;
1247 # ok(159, $db->get($k, $v, R_CURSOR)) ;
1249 # ok(160, keys %h == 1) ;
1257 # Bug ID 20001013.009
1259 # test that $hash{KEY} = undef doesn't produce the warning
1260 # Use of uninitialized value in null operation
1268 local $SIG{__WARN__} = sub {$a = $_[0]} ;
1270 tie %h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0664, $DB_BTREE
1271 or die "Can't open file: $!\n" ;
1279 # test that %hash = () doesn't produce the warning
1280 # Argument "" isn't numeric in entersub
1288 local $SIG{__WARN__} = sub {$a = $_[0]} ;
1290 tie %h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0664, $DB_BTREE
1291 or die "Can't open file: $!\n" ;
1299 # When iterating over a tied hash using "each", the key passed to FETCH
1300 # will be recycled and passed to NEXTKEY. If a Source Filter modifies the
1301 # key in FETCH via a filter_fetch_key method we need to check that the
1302 # modified key doesn't get passed to NEXTKEY.
1303 # Also Test "keys" & "values" while we are at it.
1313 ok(158, $db = tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_BTREE ) );
1314 $db->filter_fetch_key (sub { $_ =~ s/^Beta_/Alpha_/ if defined $_}) ;
1315 $db->filter_store_key (sub { $bad_key = 1 if /^Beta_/ ; $_ =~ s/^Alpha_/Beta_/}) ;
1317 $h{'Alpha_ABC'} = 2 ;
1318 $h{'Alpha_DEF'} = 5 ;
1320 ok(159, $h{'Alpha_ABC'} == 2);
1321 ok(160, $h{'Alpha_DEF'} == 5);
1323 my ($k, $v) = ("","");
1324 while (($k, $v) = each %h) {}
1325 ok(161, $bad_key == 0);
1328 foreach $k (keys %h) {}
1329 ok(162, $bad_key == 0);
1332 foreach $v (values %h) {}
1333 ok(163, $bad_key == 0);