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