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

📄 hash.t

📁 介绍:MySQL是比较出名的数据库软件
💻 T
📖 第 1 页 / 共 2 页
字号:
#!./perl -w# ID: %I%, %G%   use strict ;BEGIN {    unless(grep /blib/, @INC) {        chdir 't' if -d 't';        @INC = '../lib' if -d '../lib';    }}#use Config;##BEGIN {#    if(-d "lib" && -f "TEST") {#        if ($Config{'extensions'} !~ /\bBerkeleyDB\b/ ) {#            print "1..74\n";#            exit 0;#        }#    }#}use BerkeleyDB; use File::Path qw(rmtree);print "1..210\n";my %DB_errors = (    'DB_INCOMPLETE'	=> "DB_INCOMPLETE: Sync was unable to complete",    'DB_KEYEMPTY'	=> "DB_KEYEMPTY: Non-existent key/data pair",    'DB_KEYEXIST'	=> "DB_KEYEXIST: Key/data pair already exists",    'DB_LOCK_DEADLOCK'  => "DB_LOCK_DEADLOCK: Locker killed to resolve a deadlock",    'DB_LOCK_NOTGRANTED' => "DB_LOCK_NOTGRANTED: Lock not granted",    'DB_NOTFOUND'	=> "DB_NOTFOUND: No matching key/data pair found",    'DB_OLD_VERSION'	=> "DB_OLDVERSION: Database requires a version upgrade",    'DB_RUNRECOVERY'	=> "DB_RUNRECOVERY: Fatal error, run database recovery",) ;{    package LexFile ;    sub new    {	my $self = shift ;	unlink @_ ; 	bless [ @_ ], $self ;    }    sub DESTROY    {	my $self = shift ;	unlink @{ $self } ;    }}sub ok{    my $no = shift ;    my $result = shift ;     print "not " unless $result ;    print "ok $no\n" ;}my $Dfile = "dbhash.tmp";my $Dfile2 = "dbhash2.tmp";my $Dfile3 = "dbhash3.tmp";unlink $Dfile;umask(0) ;# Check for invalid parameters{    # Check for invalid parameters    my $db ;    eval ' $db = new BerkeleyDB::Hash  -Stupid => 3 ; ' ;    ok 1, $@ =~ /unknown key value\(s\) Stupid/  ;    eval ' $db = new BerkeleyDB::Hash -Bad => 2, -Mode => 0345, -Stupid => 3; ' ;    ok 2, $@ =~ /unknown key value\(s\) (Bad |Stupid ){2}/  ;    eval ' $db = new BerkeleyDB::Hash -Env => 2 ' ;    ok 3, $@ =~ /^Env not of type BerkeleyDB::Env/ ;    eval ' $db = new BerkeleyDB::Hash -Txn => "fred" ' ;    ok 4, $@ =~ /^Txn not of type BerkeleyDB::Txn/ ;    my $obj = bless [], "main" ;    eval ' $db = new BerkeleyDB::Hash -Env => $obj ' ;    ok 5, $@ =~ /^Env not of type BerkeleyDB::Env/ ;}# Now check the interface to HASH{    my $lex = new LexFile $Dfile ;    ok 6, my $db = new BerkeleyDB::Hash -Filename => $Dfile, 				    -Flags    => DB_CREATE ;    # Add a k/v pair    my $value ;    my $status ;    ok 7, $db->db_put("some key", "some value") == 0  ;    ok 8, $db->status() == 0 ;    ok 9, $db->db_get("some key", $value) == 0 ;    ok 10, $value eq "some value" ;    ok 11, $db->db_put("key", "value") == 0  ;    ok 12, $db->db_get("key", $value) == 0 ;    ok 13, $value eq "value" ;    ok 14, $db->db_del("some key") == 0 ;    ok 15, ($status = $db->db_get("some key", $value)) == DB_NOTFOUND ;    ok 16, $status eq $DB_errors{'DB_NOTFOUND'} ;    ok 17, $db->status() == DB_NOTFOUND ;    ok 18, $db->status() eq $DB_errors{'DB_NOTFOUND'};    ok 19, $db->db_sync() == 0 ;    # Check NOOVERWRITE will make put fail when attempting to overwrite    # an existing record.    ok 20, $db->db_put( 'key', 'x', DB_NOOVERWRITE) == DB_KEYEXIST ;    ok 21, $db->status() eq $DB_errors{'DB_KEYEXIST'};    ok 22, $db->status() == DB_KEYEXIST ;    # check that the value of the key  has not been changed by the    # previous test    ok 23, $db->db_get("key", $value) == 0 ;    ok 24, $value eq "value" ;    # test DB_GET_BOTH    my ($k, $v) = ("key", "value") ;    ok 25, $db->db_get($k, $v, DB_GET_BOTH) == 0 ;    ($k, $v) = ("key", "fred") ;    ok 26, $db->db_get($k, $v, DB_GET_BOTH) == DB_NOTFOUND ;    ($k, $v) = ("another", "value") ;    ok 27, $db->db_get($k, $v, DB_GET_BOTH) == DB_NOTFOUND ;}{    # Check simple env works with a hash.    my $lex = new LexFile $Dfile ;    my $home = "./fred" ;    ok 28, -d $home ? chmod 0777, $home : mkdir($home, 0777) ;    ok 29, my $env = new BerkeleyDB::Env -Flags => DB_CREATE| DB_INIT_MPOOL,    					 -Home  => $home ;    ok 30, my $db = new BerkeleyDB::Hash -Filename => $Dfile, 				    -Env      => $env,				    -Flags    => DB_CREATE ;    # Add a k/v pair    my $value ;    ok 31, $db->db_put("some key", "some value") == 0 ;    ok 32, $db->db_get("some key", $value) == 0 ;    ok 33, $value eq "some value" ;    undef $db ;    undef $env ;    rmtree $home ;}{    # override default hash    my $lex = new LexFile $Dfile ;    my $value ;    $::count = 0 ;    ok 34, my $db = new BerkeleyDB::Hash -Filename => $Dfile, 				     -Hash     => sub {  ++$::count ; length $_[0] },				     -Flags    => DB_CREATE ;    ok 35, $db->db_put("some key", "some value") == 0 ;    ok 36, $db->db_get("some key", $value) == 0 ;    ok 37, $value eq "some value" ;    ok 38, $::count > 0 ;} {    # cursors    my $lex = new LexFile $Dfile ;    my %hash ;    my ($k, $v) ;    ok 39, my $db = new BerkeleyDB::Hash -Filename => $Dfile, 				     -Flags    => DB_CREATE ;    # create some data    my %data =  (		"red"	=> 2,		"green"	=> "house",		"blue"	=> "sea",		) ;    my $ret = 0 ;    while (($k, $v) = each %data) {        $ret += $db->db_put($k, $v) ;    }    ok 40, $ret == 0 ;    # create the cursor    ok 41, my $cursor = $db->db_cursor() ;    $k = $v = "" ;    my %copy = %data ;    my $extras = 0 ;    # sequence forwards    while ($cursor->c_get($k, $v, DB_NEXT) == 0) {        if ( $copy{$k} eq $v )             { delete $copy{$k} }	else	    { ++ $extras }    }    ok 42, $cursor->status() == DB_NOTFOUND ;    ok 43, $cursor->status() eq $DB_errors{'DB_NOTFOUND'} ;    ok 44, keys %copy == 0 ;    ok 45, $extras == 0 ;    # sequence backwards    %copy = %data ;    $extras = 0 ;    my $status ;    for ( $status = $cursor->c_get($k, $v, DB_LAST) ;	  $status == 0 ;    	  $status = $cursor->c_get($k, $v, DB_PREV)) {        if ( $copy{$k} eq $v )             { delete $copy{$k} }	else	    { ++ $extras }    }    ok 46, $status == DB_NOTFOUND ;    ok 47, $status eq $DB_errors{'DB_NOTFOUND'} ;    ok 48, $cursor->status() == $status ;    ok 49, $cursor->status() eq $status ;    ok 50, keys %copy == 0 ;    ok 51, $extras == 0 ;    ($k, $v) = ("green", "house") ;    ok 52, $cursor->c_get($k, $v, DB_GET_BOTH) == 0 ;    ($k, $v) = ("green", "door") ;    ok 53, $cursor->c_get($k, $v, DB_GET_BOTH) == DB_NOTFOUND ;    ($k, $v) = ("black", "house") ;    ok 54, $cursor->c_get($k, $v, DB_GET_BOTH) == DB_NOTFOUND ;    } {    # Tied Hash interface    my $lex = new LexFile $Dfile ;    my %hash ;    ok 55, tie %hash, 'BerkeleyDB::Hash', -Filename => $Dfile,                                      -Flags    => DB_CREATE ;    # check "each" with an empty database    my $count = 0 ;    while (my ($k, $v) = each %hash) {	++ $count ;    }    ok 56, (tied %hash)->status() == DB_NOTFOUND ;    ok 57, $count == 0 ;    # Add a k/v pair    my $value ;    $hash{"some key"} = "some value";    ok 58, (tied %hash)->status() == 0 ;    ok 59, $hash{"some key"} eq "some value";    ok 60, defined $hash{"some key"} ;    ok 61, (tied %hash)->status() == 0 ;    ok 62, exists $hash{"some key"} ;    ok 63, !defined $hash{"jimmy"} ;    ok 64, (tied %hash)->status() == DB_NOTFOUND ;    ok 65, !exists $hash{"jimmy"} ;    ok 66, (tied %hash)->status() == DB_NOTFOUND ;    delete $hash{"some key"} ;    ok 67, (tied %hash)->status() == 0 ;    ok 68, ! defined $hash{"some key"} ;    ok 69, (tied %hash)->status() == DB_NOTFOUND ;    ok 70, ! exists $hash{"some key"} ;    ok 71, (tied %hash)->status() == DB_NOTFOUND ;    $hash{1} = 2 ;    $hash{10} = 20 ;    $hash{1000} = 2000 ;    my ($keys, $values) = (0,0);    $count = 0 ;    while (my ($k, $v) = each %hash) {        $keys += $k ;	$values += $v ;	++ $count ;    }    ok 72, $count == 3 ;    ok 73, $keys == 1011 ;    ok 74, $values == 2022 ;    # now clear the hash    %hash = () ;    ok 75, keys %hash == 0 ;    untie %hash ;}{    # in-memory file    my $lex = new LexFile $Dfile ;    my %hash ;    my $fd ;    my $value ;    ok 76, my $db = tie %hash, 'BerkeleyDB::Hash' ;    ok 77, $db->db_put("some key", "some value") == 0  ;    ok 78, $db->db_get("some key", $value) == 0 ;    ok 79, $value eq "some value" ;    undef $db ;    untie %hash ;} {    # partial    # check works via API    my $lex = new LexFile $Dfile ;    my %hash ;    my $value ;    ok 80, my $db = tie %hash, 'BerkeleyDB::Hash', -Filename => $Dfile,                                      	       -Flags    => DB_CREATE ;    # create some data    my %data =  (		"red"	=> "boat",		"green"	=> "house",		"blue"	=> "sea",		) ;    my $ret = 0 ;    while (my ($k, $v) = each %data) {        $ret += $db->db_put($k, $v) ;    }    ok 81, $ret == 0 ;    # do a partial get    my($pon, $off, $len) = $db->partial_set(0,2) ;    ok 82, $pon == 0 && $off == 0 && $len == 0 ;    ok 83, ( $db->db_get("red", $value) == 0) && $value eq "bo" ;    ok 84, ( $db->db_get("green", $value) == 0) && $value eq "ho" ;    ok 85, ( $db->db_get("blue", $value) == 0) && $value eq "se" ;    # do a partial get, off end of data    ($pon, $off, $len) = $db->partial_set(3,2) ;    ok 86, $pon ;    ok 87, $off == 0 ;    ok 88, $len == 2 ;    ok 89, $db->db_get("red", $value) == 0 && $value eq "t" ;    ok 90, $db->db_get("green", $value) == 0 && $value eq "se" ;    ok 91, $db->db_get("blue", $value) == 0 && $value eq "" ;    # switch of partial mode    ($pon, $off, $len) = $db->partial_clear() ;    ok 92, $pon ;    ok 93, $off == 3 ;    ok 94, $len == 2 ;    ok 95, $db->db_get("red", $value) == 0 && $value eq "boat" ;    ok 96, $db->db_get("green", $value) == 0 && $value eq "house" ;    ok 97, $db->db_get("blue", $value) == 0 && $value eq "sea" ;    # now partial put    ($pon, $off, $len) = $db->partial_set(0,2) ;    ok 98, ! $pon ;    ok 99, $off == 0 ;    ok 100, $len == 0 ;    ok 101, $db->db_put("red", "") == 0 ;    ok 102, $db->db_put("green", "AB") == 0 ;    ok 103, $db->db_put("blue", "XYZ") == 0 ;    ok 104, $db->db_put("new", "KLM") == 0 ;

⌨️ 快捷键说明

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