⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 db-btree.t

📁 berkeleyDB,强大的嵌入式数据,多个数据库的内核
💻 T
📖 第 1 页 / 共 3 页
字号:
#!./perl -wBEGIN {    unless(grep /blib/, @INC) {        chdir 't' if -d 't';        @INC = '../lib' if -d '../lib';    }} use warnings;use strict;use Config; BEGIN {    if(-d "lib" && -f "TEST") {        if ($Config{'extensions'} !~ /\bDB_File\b/ ) {            print "1..0 # Skip: DB_File was not built\n";            exit 0;        }    }    if ($^O eq 'darwin'	&& $Config{db_version_major} == 1	&& $Config{db_version_minor} == 0	&& $Config{db_version_patch} == 0) {	warn <<EOM;## This test is known to crash in Mac OS X versions 10.2 (or earlier)# because of the buggy Berkeley DB version included with the OS.#EOM    }}use DB_File; use Fcntl;print "1..197\n";unlink glob "__db.*";sub ok{    my $no = shift ;    my $result = shift ;     print "not " unless $result ;    print "ok $no\n" ;}sub lexical{    my(@a) = unpack ("C*", $a) ;    my(@b) = unpack ("C*", $b) ;    my $len = (@a > @b ? @b : @a) ;    my $i = 0 ;    foreach $i ( 0 .. $len -1) {        return $a[$i] - $b[$i] if $a[$i] != $b[$i] ;    }    return @a - @b ;}{    package Redirect ;    use Symbol ;    sub new    {        my $class = shift ;        my $filename = shift ;	my $fh = gensym ;	open ($fh, ">$filename") || die "Cannot open $filename: $!" ;	my $real_stdout = select($fh) ;	return bless [$fh, $real_stdout ] ;    }    sub DESTROY    {        my $self = shift ;	close $self->[0] ;	select($self->[1]) ;    }}sub docat{     my $file = shift;    local $/ = undef ;    open(CAT,$file) || die "Cannot open $file: $!";    my $result = <CAT>;    close(CAT);    $result = normalise($result) ;    return $result ;}   sub docat_del{     my $file = shift;    my $result = docat($file);    unlink $file ;    return $result ;}   sub normalise{    my $data = shift ;    $data =~ s#\r\n#\n#g         if $^O eq 'cygwin' ;    return $data ;}sub safeUntie{    my $hashref = shift ;    my $no_inner = 1;    local $SIG{__WARN__} = sub {-- $no_inner } ;    untie %$hashref;    return $no_inner;}my $db185mode =  ($DB_File::db_version == 1 && ! $DB_File::db_185_compat) ;my $null_keys_allowed = ($DB_File::db_ver < 2.004010 				|| $DB_File::db_ver >= 3.1 );my $Dfile = "dbbtree.tmp";unlink $Dfile;umask(0);# Check the interface to BTREEINFOmy $dbh = new DB_File::BTREEINFO ;ok(1, ! defined $dbh->{flags}) ;ok(2, ! defined $dbh->{cachesize}) ;ok(3, ! defined $dbh->{psize}) ;ok(4, ! defined $dbh->{lorder}) ;ok(5, ! defined $dbh->{minkeypage}) ;ok(6, ! defined $dbh->{maxkeypage}) ;ok(7, ! defined $dbh->{compare}) ;ok(8, ! defined $dbh->{prefix}) ;$dbh->{flags} = 3000 ;ok(9, $dbh->{flags} == 3000) ;$dbh->{cachesize} = 9000 ;ok(10, $dbh->{cachesize} == 9000);$dbh->{psize} = 400 ;ok(11, $dbh->{psize} == 400) ;$dbh->{lorder} = 65 ;ok(12, $dbh->{lorder} == 65) ;$dbh->{minkeypage} = 123 ;ok(13, $dbh->{minkeypage} == 123) ;$dbh->{maxkeypage} = 1234 ;ok(14, $dbh->{maxkeypage} == 1234 );# Check that an invalid entry is caught both for store & fetcheval '$dbh->{fred} = 1234' ;ok(15, $@ =~ /^DB_File::BTREEINFO::STORE - Unknown element 'fred' at/ ) ;eval 'my $q = $dbh->{fred}' ;ok(16, $@ =~ /^DB_File::BTREEINFO::FETCH - Unknown element 'fred' at/ ) ;# Now check the interface to BTREEmy ($X, %h) ;ok(17, $X = tie(%h, 'DB_File',$Dfile, O_RDWR|O_CREAT, 0640, $DB_BTREE )) ;die "Could not tie: $!" unless $X;my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,   $blksize,$blocks) = stat($Dfile);my %noMode = map { $_, 1} qw( amigaos MSWin32 NetWare cygwin ) ;ok(18, ($mode & 0777) == (($^O eq 'os2' || $^O eq 'MacOS') ? 0666 : 0640)   || $noMode{$^O} );my ($key, $value, $i);while (($key,$value) = each(%h)) {    $i++;}ok(19, !$i ) ;$h{'goner1'} = 'snork';$h{'abc'} = 'ABC';ok(20, $h{'abc'} eq 'ABC' );ok(21, ! defined $h{'jimmy'} ) ;ok(22, ! exists $h{'jimmy'} ) ;ok(23,  defined $h{'abc'} ) ;$h{'def'} = 'DEF';$h{'jkl','mno'} = "JKL\034MNO";$h{'a',2,3,4,5} = join("\034",'A',2,3,4,5);$h{'a'} = 'A';#$h{'b'} = 'B';$X->STORE('b', 'B') ;$h{'c'} = 'C';#$h{'d'} = 'D';$X->put('d', 'D') ;$h{'e'} = 'E';$h{'f'} = 'F';$h{'g'} = 'X';$h{'h'} = 'H';$h{'i'} = 'I';$h{'goner2'} = 'snork';delete $h{'goner2'};# IMPORTANT - $X must be undefined before the untie otherwise the#             underlying DB close routine will not get called.undef $X ;untie(%h);# tie to the same file againok(24, $X = tie(%h,'DB_File',$Dfile, O_RDWR, 0640, $DB_BTREE)) ;# Modify an entry from the previous tie$h{'g'} = 'G';$h{'j'} = 'J';$h{'k'} = 'K';$h{'l'} = 'L';$h{'m'} = 'M';$h{'n'} = 'N';$h{'o'} = 'O';$h{'p'} = 'P';$h{'q'} = 'Q';$h{'r'} = 'R';$h{'s'} = 'S';$h{'t'} = 'T';$h{'u'} = 'U';$h{'v'} = 'V';$h{'w'} = 'W';$h{'x'} = 'X';$h{'y'} = 'Y';$h{'z'} = 'Z';$h{'goner3'} = 'snork';delete $h{'goner1'};$X->DELETE('goner3');my @keys = keys(%h);my @values = values(%h);ok(25, $#keys == 29 && $#values == 29) ;$i = 0 ;while (($key,$value) = each(%h)) {    if ($key eq $keys[$i] && $value eq $values[$i] && $key eq lc($value)) {	$key =~ y/a-z/A-Z/;	$i++ if $key eq $value;    }}ok(26, $i == 30) ;@keys = ('blurfl', keys(%h), 'dyick');ok(27, $#keys == 31) ;#Check that the keys can be retrieved in ordermy @b = keys %h ;my @c = sort lexical @b ;ok(28, ArrayCompare(\@b, \@c)) ;$h{'foo'} = '';ok(29, $h{'foo'} eq '' ) ;# Berkeley DB from version 2.4.10 to 3.0 does not allow null keys.# This feature was reenabled in version 3.1 of Berkeley DB.my $result = 0 ;if ($null_keys_allowed) {    $h{''} = 'bar';    $result = ( $h{''} eq 'bar' );}else  { $result = 1 }ok(30, $result) ;# check cache overflow and numeric keys and contentsmy $ok = 1;for ($i = 1; $i < 200; $i++) { $h{$i + 0} = $i + 0; }for ($i = 1; $i < 200; $i++) { $ok = 0 unless $h{$i} == $i; }ok(31, $ok);($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,   $blksize,$blocks) = stat($Dfile);ok(32, $size > 0 );@h{0..200} = 200..400;my @foo = @h{0..200};ok(33, join(':',200..400) eq join(':',@foo) );# Now check all the non-tie specific stuff# Check R_NOOVERWRITE flag will make put fail when attempting to overwrite# an existing record. my $status = $X->put( 'x', 'newvalue', R_NOOVERWRITE) ;ok(34, $status == 1 ); # check that the value of the key 'x' has not been changed by the # previous testok(35, $h{'x'} eq 'X' );# standard put$status = $X->put('key', 'value') ;ok(36, $status == 0 );#check that previous put can be retrieved$value = 0 ;$status = $X->get('key', $value) ;ok(37, $status == 0 );ok(38, $value eq 'value' );# Attempting to delete an existing key should work$status = $X->del('q') ;ok(39, $status == 0 );if ($null_keys_allowed) {    $status = $X->del('') ;} else {    $status = 0 ;}ok(40, $status == 0 );# Make sure that the key deleted, cannot be retrievedok(41, ! defined $h{'q'}) ;ok(42, ! defined $h{''}) ;undef $X ;untie %h ;ok(43, $X = tie(%h, 'DB_File',$Dfile, O_RDWR, 0640, $DB_BTREE ));# Attempting to delete a non-existant key should fail$status = $X->del('joe') ;ok(44, $status == 1 );# Check the get interface# First a non-existing key$status = $X->get('aaaa', $value) ;ok(45, $status == 1 );# Next an existing key$status = $X->get('a', $value) ;ok(46, $status == 0 );ok(47, $value eq 'A' );# seq# #### use seq to find an approximate match$key = 'ke' ;$value = '' ;$status = $X->seq($key, $value, R_CURSOR) ;ok(48, $status == 0 );ok(49, $key eq 'key' );ok(50, $value eq 'value' );# seq when the key does not match$key = 'zzz' ;$value = '' ;$status = $X->seq($key, $value, R_CURSOR) ;ok(51, $status == 1 );# use seq to set the cursor, then delete the record @ the cursor.$key = 'x' ;$value = '' ;$status = $X->seq($key, $value, R_CURSOR) ;ok(52, $status == 0 );ok(53, $key eq 'x' );ok(54, $value eq 'X' );$status = $X->del(0, R_CURSOR) ;ok(55, $status == 0 );$status = $X->get('x', $value) ;ok(56, $status == 1 );# ditto, but use put to replace the key/value pair.$key = 'y' ;$value = '' ;$status = $X->seq($key, $value, R_CURSOR) ;ok(57, $status == 0 );ok(58, $key eq 'y' );ok(59, $value eq 'Y' );$key = "replace key" ;$value = "replace value" ;$status = $X->put($key, $value, R_CURSOR) ;ok(60, $status == 0 );ok(61, $key eq 'replace key' );ok(62, $value eq 'replace value' );$status = $X->get('y', $value) ;ok(63, 1) ; # hard-wire to always pass. the previous test ($status == 1)	    # only worked because of a bug in 1.85/6# use seq to walk forwards through a file $status = $X->seq($key, $value, R_FIRST) ;ok(64, $status == 0 );my $previous = $key ;$ok = 1 ;while (($status = $X->seq($key, $value, R_NEXT)) == 0){    ($ok = 0), last if ($previous cmp $key) == 1 ;}ok(65, $status == 1 );ok(66, $ok == 1 );# use seq to walk backwards through a file $status = $X->seq($key, $value, R_LAST) ;ok(67, $status == 0 );$previous = $key ;$ok = 1 ;while (($status = $X->seq($key, $value, R_PREV)) == 0){    ($ok = 0), last if ($previous cmp $key) == -1 ;    #print "key = [$key] value = [$value]\n" ;}ok(68, $status == 1 );ok(69, $ok == 1 );# check seq FIRST/LAST# sync# ####$status = $X->sync ;ok(70, $status == 0 );# fd# ##$status = $X->fd ;ok(71, $status != 0 );undef $X ;untie %h ;unlink $Dfile;# Now try an in memory filemy $Y;ok(72, $Y = tie(%h, 'DB_File',undef, O_RDWR|O_CREAT, 0640, $DB_BTREE ));# fd with an in memory file should return failure$status = $Y->fd ;ok(73, $status == -1 );undef $Y ;untie %h ;# Duplicate keysmy $bt = new DB_File::BTREEINFO ;$bt->{flags} = R_DUP ;my ($YY, %hh);ok(74, $YY = tie(%hh, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $bt )) ;$hh{'Wall'} = 'Larry' ;$hh{'Wall'} = 'Stone' ; # Note the duplicate key$hh{'Wall'} = 'Brick' ; # Note the duplicate key$hh{'Wall'} = 'Brick' ; # Note the duplicate key and value$hh{'Smith'} = 'John' ;$hh{'mouse'} = 'mickey' ;# first work in scalar contextok(75, scalar $YY->get_dup('Unknown') == 0 );ok(76, scalar $YY->get_dup('Smith') == 1 );ok(77, scalar $YY->get_dup('Wall') == 4 );# now in list contextmy @unknown = $YY->get_dup('Unknown') ;ok(78, "@unknown" eq "" );my @smith = $YY->get_dup('Smith') ;ok(79, "@smith" eq "John" );{my @wall = $YY->get_dup('Wall') ;my %wall ;@wall{@wall} = @wall ;ok(80, (@wall == 4 && $wall{'Larry'} && $wall{'Stone'} && $wall{'Brick'}) );}# hashmy %unknown = $YY->get_dup('Unknown', 1) ;ok(81, keys %unknown == 0 );my %smith = $YY->get_dup('Smith', 1) ;ok(82, keys %smith == 1 && $smith{'John'}) ;my %wall = $YY->get_dup('Wall', 1) ;ok(83, keys %wall == 3 && $wall{'Larry'} == 1 && $wall{'Stone'} == 1 		&& $wall{'Brick'} == 2);undef $YY ;untie %hh ;unlink $Dfile;# test multiple callbacksmy $Dfile1 = "btree1" ;my $Dfile2 = "btree2" ;my $Dfile3 = "btree3" ; my $dbh1 = new DB_File::BTREEINFO ;$dbh1->{compare} = sub { 	no warnings 'numeric' ;	$_[0] <=> $_[1] } ;  my $dbh2 = new DB_File::BTREEINFO ;$dbh2->{compare} = sub { $_[0] cmp $_[1] } ; my $dbh3 = new DB_File::BTREEINFO ;$dbh3->{compare} = sub { length $_[0] <=> length $_[1] } ;  my (%g, %k);tie(%h, 'DB_File',$Dfile1, O_RDWR|O_CREAT, 0640, $dbh1 ) or die $!;tie(%g, 'DB_File',$Dfile2, O_RDWR|O_CREAT, 0640, $dbh2 ) or die $!;tie(%k, 'DB_File',$Dfile3, O_RDWR|O_CREAT, 0640, $dbh3 ) or die $!; my @Keys = qw( 0123 12 -1234 9 987654321 def  ) ;my (@srt_1, @srt_2, @srt_3);{   no warnings 'numeric' ;  @srt_1 = sort { $a <=> $b } @Keys ; 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -