1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
|
#!./perl -w
# ID: %I%, %G%
use strict ;
BEGIN {
unless(grep /blib/, @INC) {
chdir 't' if -d 't';
@INC = '../lib' if -d '../lib';
}
}
use BerkeleyDB;
use t::util ;
if ($BerkeleyDB::db_ver < 2.005002)
{
print "1..0 # Skip: join needs Berkeley DB 2.5.2 or later\n" ;
exit 0 ;
}
print "1..37\n";
my $Dfile1 = "dbhash1.tmp";
my $Dfile2 = "dbhash2.tmp";
my $Dfile3 = "dbhash3.tmp";
unlink $Dfile1, $Dfile2, $Dfile3 ;
umask(0) ;
{
# error cases
my $lex = new LexFile $Dfile1, $Dfile2, $Dfile3 ;
my %hash1 ;
my $value ;
my $status ;
my $cursor ;
ok 1, my $db1 = tie %hash1, 'BerkeleyDB::Hash',
-Filename => $Dfile1,
-Flags => DB_CREATE,
-DupCompare => sub { $_[0] lt $_[1] },
-Property => DB_DUP|DB_DUPSORT ;
# no cursors supplied
eval '$cursor = $db1->db_join() ;' ;
ok 2, $@ =~ /Usage: \$db->BerkeleyDB::Common::db_join\Q([cursors], flags=0)/;
# empty list
eval '$cursor = $db1->db_join([]) ;' ;
ok 3, $@ =~ /db_join: No cursors in parameter list/;
# cursor list, isn't a []
eval '$cursor = $db1->db_join({}) ;' ;
ok 4, $@ =~ /cursors is not an array reference at/ ;
eval '$cursor = $db1->db_join(\1) ;' ;
ok 5, $@ =~ /cursors is not an array reference at/ ;
}
{
# test a 2-way & 3-way join
my $lex = new LexFile $Dfile1, $Dfile2, $Dfile3 ;
my %hash1 ;
my %hash2 ;
my %hash3 ;
my $value ;
my $status ;
my $home = "./fred" ;
ok 6, my $lexD = new LexDir($home);
ok 7, my $env = new BerkeleyDB::Env -Home => $home,
-Flags => DB_CREATE|DB_INIT_TXN
|DB_INIT_MPOOL;
#|DB_INIT_MPOOL| DB_INIT_LOCK;
ok 8, my $txn = $env->txn_begin() ;
ok 9, my $db1 = tie %hash1, 'BerkeleyDB::Hash',
-Filename => $Dfile1,
-Flags => DB_CREATE,
-DupCompare => sub { $_[0] cmp $_[1] },
-Property => DB_DUP|DB_DUPSORT,
-Env => $env,
-Txn => $txn ;
;
ok 10, my $db2 = tie %hash2, 'BerkeleyDB::Hash',
-Filename => $Dfile2,
-Flags => DB_CREATE,
-DupCompare => sub { $_[0] cmp $_[1] },
-Property => DB_DUP|DB_DUPSORT,
-Env => $env,
-Txn => $txn ;
ok 11, my $db3 = tie %hash3, 'BerkeleyDB::Btree',
-Filename => $Dfile3,
-Flags => DB_CREATE,
-DupCompare => sub { $_[0] cmp $_[1] },
-Property => DB_DUP|DB_DUPSORT,
-Env => $env,
-Txn => $txn ;
ok 12, addData($db1, qw( apple Convenience
peach Shopway
pear Farmer
raspberry Shopway
strawberry Shopway
gooseberry Farmer
blueberry Farmer
));
ok 13, addData($db2, qw( red apple
red raspberry
red strawberry
yellow peach
yellow pear
green gooseberry
blue blueberry)) ;
ok 14, addData($db3, qw( expensive apple
reasonable raspberry
expensive strawberry
reasonable peach
reasonable pear
expensive gooseberry
reasonable blueberry)) ;
ok 15, my $cursor2 = $db2->db_cursor() ;
my $k = "red" ;
my $v = "" ;
ok 16, $cursor2->c_get($k, $v, DB_SET) == 0 ;
# Two way Join
ok 17, my $cursor1 = $db1->db_join([$cursor2]) ;
my %expected = qw( apple Convenience
raspberry Shopway
strawberry Shopway
) ;
# sequence forwards
while ($cursor1->c_get($k, $v) == 0) {
delete $expected{$k}
if defined $expected{$k} && $expected{$k} eq $v ;
#print "[$k] [$v]\n" ;
}
ok 18, keys %expected == 0 ;
ok 19, $cursor1->status() == DB_NOTFOUND ;
# Three way Join
ok 20, $cursor2 = $db2->db_cursor() ;
$k = "red" ;
$v = "" ;
ok 21, $cursor2->c_get($k, $v, DB_SET) == 0 ;
ok 22, my $cursor3 = $db3->db_cursor() ;
$k = "expensive" ;
$v = "" ;
ok 23, $cursor3->c_get($k, $v, DB_SET) == 0 ;
ok 24, $cursor1 = $db1->db_join([$cursor2, $cursor3]) ;
%expected = qw( apple Convenience
strawberry Shopway
) ;
# sequence forwards
while ($cursor1->c_get($k, $v) == 0) {
delete $expected{$k}
if defined $expected{$k} && $expected{$k} eq $v ;
#print "[$k] [$v]\n" ;
}
ok 25, keys %expected == 0 ;
ok 26, $cursor1->status() == DB_NOTFOUND ;
# test DB_JOIN_ITEM
# #################
ok 27, $cursor2 = $db2->db_cursor() ;
$k = "red" ;
$v = "" ;
ok 28, $cursor2->c_get($k, $v, DB_SET) == 0 ;
ok 29, $cursor3 = $db3->db_cursor() ;
$k = "expensive" ;
$v = "" ;
ok 30, $cursor3->c_get($k, $v, DB_SET) == 0 ;
ok 31, $cursor1 = $db1->db_join([$cursor2, $cursor3]) ;
%expected = qw( apple 1
strawberry 1
) ;
# sequence forwards
$k = "" ;
$v = "" ;
while ($cursor1->c_get($k, $v, DB_JOIN_ITEM) == 0) {
delete $expected{$k}
if defined $expected{$k} ;
#print "[$k]\n" ;
}
ok 32, keys %expected == 0 ;
ok 33, $cursor1->status() == DB_NOTFOUND ;
ok 34, $cursor1->c_close() == 0 ;
ok 35, $cursor2->c_close() == 0 ;
ok 36, $cursor3->c_close() == 0 ;
ok 37, ($status = $txn->txn_commit) == 0;
undef $txn ;
#undef $cursor1;
#undef $cursor2;
#undef $cursor3;
undef $db1 ;
undef $db2 ;
undef $db3 ;
undef $env ;
untie %hash1 ;
untie %hash2 ;
untie %hash3 ;
}
print "# at the end\n";
|