1#!./perl -w 2 3use strict ; 4 5use lib 't' ; 6use BerkeleyDB; 7use util ; 8use Test::More; 9 10plan tests => 15; 11 12my $Dfile = "dbhash.tmp"; 13my $home = "./fred" ; 14 15umask(0); 16 17{ 18 # let object destruction kill everything 19 20 my $lex = new LexFile $Dfile ; 21 my %hash ; 22 my $value ; 23 24 ok my $lexD = new LexDir($home) ; 25 ok my $env = new BerkeleyDB::Env -Home => $home, @StdErrFile, 26 -Flags => DB_CREATE|DB_INIT_TXN| 27 DB_INIT_MPOOL|DB_INIT_LOCK ; 28 ok my $txn = $env->txn_begin() ; 29 ok my $db1 = tie %hash, 'BerkeleyDB::Hash', -Filename => $Dfile, 30 -Flags => DB_CREATE , 31 -Env => $env, 32 -Txn => $txn ; 33 34 ok $txn->txn_commit() == 0 ; 35 ok $txn = $env->txn_begin() ; 36 $db1->Txn($txn); 37 38 # create some data 39 my %data = ( 40 "red" => "boat", 41 "green" => "house", 42 "blue" => "sea", 43 ) ; 44 45 my $ret = 0 ; 46 while (my ($k, $v) = each %data) { 47 $ret += $db1->db_put($k, $v) ; 48 } 49 ok $ret == 0 ; 50 51 # should be able to see all the records 52 53 ok my $cursor = $db1->db_cursor() ; 54 my ($k, $v) = ("", "") ; 55 my $count = 0 ; 56 # sequence forwards 57 while ($cursor->c_get($k, $v, DB_NEXT) == 0) { 58 ++ $count ; 59 } 60 is $count, 3 ; 61 undef $cursor ; 62 63 # now abort the transaction 64 ok $txn->txn_abort() == 0 ; 65 66 # there shouldn't be any records in the database 67 $count = 0 ; 68 # sequence forwards 69 ok $cursor = $db1->db_cursor() ; 70 while ($cursor->c_get($k, $v, DB_NEXT) == 0) { 71 ++ $count ; 72 } 73 is $count, 0 ; 74 75 #undef $txn ; 76 #undef $cursor ; 77 #undef $db1 ; 78 #undef $env ; 79 #untie %hash ; 80 81} 82 83{ 84 my $lex = new LexFile $Dfile ; 85 my %hash ; 86 my $cursor ; 87 my ($k, $v) = ("", "") ; 88 ok my $db1 = tie %hash, 'BerkeleyDB::Hash', 89 -Filename => $Dfile, 90 -Flags => DB_CREATE ; 91 my $count = 0 ; 92 # sequence forwards 93 ok $cursor = $db1->db_cursor() ; 94 while ($cursor->c_get($k, $v, DB_NEXT) == 0) { 95 ++ $count ; 96 } 97 is $count, 0 ; 98} 99 100 101