Debian Codegraph @

lang/perl/BerkeleyDB/t/db-3.3.t

raw
1 #!./perl -w
2
3
4 use strict ;
5
6
7 use lib 't' ;
8 use BerkeleyDB;
9 use util ;
10 use Test::More;
11
12 BEGIN {
13 plan(skip_all => "this needs BerkeleyDB 3.3.x or better" )
14 if $BerkeleyDB::db_version < 3.3;
15
16 plan tests => 130;
17 }
18
19 umask(0);
20
21 {
22 # db->truncate
23
24 my $Dfile;
25 my $lex = new LexFile $Dfile ;
26 my %hash ;
27 my ($k, $v) ;
28 ok my $db = new BerkeleyDB::Hash -Filename => $Dfile,
29 -Flags => DB_CREATE ;
30
31 # create some data
32 my %data = (
33 "red" => 2,
34 "green" => "house",
35 "blue" => "sea",
36 ) ;
37
38 my $ret = 0 ;
39 while (($k, $v) = each %data) {
40 $ret += $db->db_put($k, $v) ;
41 }
42 ok $ret == 0 ;
43
44 # check there are three records
45 is countRecords($db), 3 ;
46
47 # now truncate the database
48 my $count = 0;
49 ok $db->truncate($count) == 0 ;
50
51 is $count, 3 ;
52 ok countRecords($db) == 0 ;
53
54 }
55
56 {
57 # db->associate -- secondary keys
58
59 sub sec_key
60 {
61 #print "in sec_key\n";
62 my $pkey = shift ;
63 my $pdata = shift ;
64
65 $_[0] = $pdata ;
66 return 0;
67 }
68
69 my ($Dfile1, $Dfile2);
70 my $lex = new LexFile $Dfile1, $Dfile2 ;
71 my %hash ;
72 my $status;
73 my ($k, $v, $pk) = ('','','');
74
75 # create primary database
76 ok my $primary = new BerkeleyDB::Hash -Filename => $Dfile1,
77 -Flags => DB_CREATE ;
78
79 # create secondary database
80 ok my $secondary = new BerkeleyDB::Hash -Filename => $Dfile2,
81 -Flags => DB_CREATE ;
82
83 # associate primary with secondary
84 ok $primary->associate($secondary, \&sec_key) == 0;
85
86 # add data to the primary
87 my %data = (
88 "red" => "flag",
89 "green" => "house",
90 "blue" => "sea",
91 ) ;
92
93 my $ret = 0 ;
94 while (($k, $v) = each %data) {
95 my $r = $primary->db_put($k, $v) ;
96 #print "put $r $BerkeleyDB::Error\n";
97 $ret += $r;
98 }
99 ok $ret == 0 ;
100
101 # check the records in the secondary
102 is countRecords($secondary), 3 ;
103
104 ok $secondary->db_get("house", $v) == 0;
105 is $v, "house";
106
107 ok $secondary->db_get("sea", $v) == 0;
108 is $v, "sea";
109
110 ok $secondary->db_get("flag", $v) == 0;
111 is $v, "flag";
112
113 # pget to primary database is illegal
114 ok $primary->db_pget('red', $pk, $v) != 0 ;
115
116 # pget to secondary database is ok
117 ok $secondary->db_pget('house', $pk, $v) == 0 ;
118 is $pk, 'green';
119 is $v, 'house';
120
121 ok my $p_cursor = $primary->db_cursor();
122 ok my $s_cursor = $secondary->db_cursor();
123
124 # c_get from primary
125 $k = 'green';
126 ok $p_cursor->c_get($k, $v, DB_SET) == 0;
127 is $k, 'green';
128 is $v, 'house';
129
130 # c_get from secondary
131 $k = 'sea';
132 ok $s_cursor->c_get($k, $v, DB_SET) == 0;
133 is $k, 'sea';
134 is $v, 'sea';
135
136 # c_pget from primary database should fail
137 $k = 1;
138 ok $p_cursor->c_pget($k, $pk, $v, DB_FIRST) != 0;
139
140 # c_pget from secondary database
141 $k = 'flag';
142 ok $s_cursor->c_pget($k, $pk, $v, DB_SET) == 0
143 or diag "$BerkeleyDB::Error\n";
144 is $k, 'flag';
145 is $pk, 'red';
146 is $v, 'flag';
147
148 # check put to secondary is illegal
149 ok $secondary->db_put("tom", "dick") != 0;
150 is countRecords($secondary), 3 ;
151
152 # delete from primary
153 ok $primary->db_del("green") == 0 ;
154 is countRecords($primary), 2 ;
155
156 # check has been deleted in secondary
157 ok $secondary->db_get("house", $v) != 0;
158 is countRecords($secondary), 2 ;
159
160 # delete from secondary
161 ok $secondary->db_del('flag') == 0 ;
162 is countRecords($secondary), 1 ;
163
164
165 # check deleted from primary
166 ok $primary->db_get("red", $v) != 0;
167 is countRecords($primary), 1 ;
168
169 }
170
171
172 # db->associate -- multiple secondary keys
173
174
175 # db->associate -- same again but when DB_DUP is specified.
176
177
178 {
179 # db->associate -- secondary keys, each with a user defined sort
180
181 sub sec_key2
182 {
183 my $pkey = shift ;
184 my $pdata = shift ;
185 #print "in sec_key2 [$pkey][$pdata]\n";
186
187 $_[0] = length $pdata ;
188 return 0;
189 }
190
191 my ($Dfile1, $Dfile2);
192 my $lex = new LexFile $Dfile1, $Dfile2 ;
193 my %hash ;
194 my $status;
195 my ($k, $v, $pk) = ('','','');
196
197 # create primary database
198 ok my $primary = new BerkeleyDB::Btree -Filename => $Dfile1,
199 -Compare => sub { return $_[0] cmp $_[1]},
200 -Flags => DB_CREATE ;
201
202 # create secondary database
203 ok my $secondary = new BerkeleyDB::Btree -Filename => $Dfile2,
204 -Compare => sub { return $_[0] <=> $_[1]},
205 -Property => DB_DUP,
206 -Flags => DB_CREATE ;
207
208 # associate primary with secondary
209 ok $primary->associate($secondary, \&sec_key2) == 0;
210
211 # add data to the primary
212 my %data = (
213 "red" => "flag",
214 "orange"=> "custard",
215 "green" => "house",
216 "blue" => "sea",
217 ) ;
218
219 my $ret = 0 ;
220 while (($k, $v) = each %data) {
221 my $r = $primary->db_put($k, $v) ;
222 #print "put [$r] $BerkeleyDB::Error\n";
223 $ret += $r;
224 }
225 ok $ret == 0 ;
226 #print "ret $ret\n";
227
228 #print "Primary\n" ; dumpdb($primary) ;
229 #print "Secondary\n" ; dumpdb($secondary) ;
230
231 # check the records in the secondary
232 is countRecords($secondary), 4 ;
233
234 my $p_data = joinkeys($primary, " ");
235 #print "primary [$p_data]\n" ;
236 is $p_data, join " ", sort { $a cmp $b } keys %data ;
237 my $s_data = joinkeys($secondary, " ");
238 #print "secondary [$s_data]\n" ;
239 is $s_data, join " ", sort { $a <=> $b } map { length } values %data ;
240
241 }
242
243 {
244 # db->associate -- primary recno, secondary hash
245
246 sub sec_key3
247 {
248 #print "in sec_key\n";
249 my $pkey = shift ;
250 my $pdata = shift ;
251
252 $_[0] = $pdata ;
253 return 0;
254 }
255
256 my ($Dfile1, $Dfile2);
257 my $lex = new LexFile $Dfile1, $Dfile2 ;
258 my %hash ;
259 my $status;
260 my ($k, $v, $pk) = ('','','');
261
262 # create primary database
263 ok my $primary = new BerkeleyDB::Recno -Filename => $Dfile1,
264 -Flags => DB_CREATE ;
265
266 # create secondary database
267 ok my $secondary = new BerkeleyDB::Hash -Filename => $Dfile2,
268 -Flags => DB_CREATE ;
269
270 # associate primary with secondary
271 ok $primary->associate($secondary, \&sec_key3) == 0;
272
273 # add data to the primary
274 my %data = (
275 0 => "flag",
276 1 => "house",
277 2 => "sea",
278 ) ;
279
280 my $ret = 0 ;
281 while (($k, $v) = each %data) {
282 my $r = $primary->db_put($k, $v) ;
283 #print "put $r $BerkeleyDB::Error\n";
284 $ret += $r;
285 }
286 ok $ret == 0 ;
287
288 # check the records in the secondary
289 is countRecords($secondary), 3 ;
290
291 ok $secondary->db_get("flag", $v) == 0;
292 is $v, "flag";
293
294 ok $secondary->db_get("house", $v) == 0;
295 is $v, "house";
296
297 ok $secondary->db_get("sea", $v) == 0;
298 is $v, "sea" ;
299
300 # pget to primary database is illegal
301 ok $primary->db_pget(0, $pk, $v) != 0 ;
302
303 # pget to secondary database is ok
304 ok $secondary->db_pget('house', $pk, $v) == 0 ;
305 is $pk, 1 ;
306 is $v, 'house';
307
308 ok my $p_cursor = $primary->db_cursor();
309 ok my $s_cursor = $secondary->db_cursor();
310
311 # c_get from primary
312 $k = 1;
313 ok $p_cursor->c_get($k, $v, DB_SET) == 0;
314 is $k, 1;
315 is $v, 'house';
316
317 # c_get from secondary
318 $k = 'sea';
319 ok $s_cursor->c_get($k, $v, DB_SET) == 0;
320 is $k, 'sea'
321 or warn "# key [$k]\n";
322 is $v, 'sea';
323
324 # c_pget from primary database should fail
325 $k = 1;
326 ok $p_cursor->c_pget($k, $pk, $v, DB_FIRST) != 0;
327
328 # c_pget from secondary database
329 $k = 'sea';
330 ok $s_cursor->c_pget($k, $pk, $v, DB_SET) == 0;
331 is $k, 'sea' ;
332 is $pk, 2 ;
333 is $v, 'sea';
334
335 # check put to secondary is illegal
336 ok $secondary->db_put("tom", "dick") != 0;
337 is countRecords($secondary), 3 ;
338
339 # delete from primary
340 ok $primary->db_del(2) == 0 ;
341 is countRecords($primary), 2 ;
342
343 # check has been deleted in secondary
344 ok $secondary->db_get("sea", $v) != 0;
345 is countRecords($secondary), 2 ;
346
347 # delete from secondary
348 ok $secondary->db_del('flag') == 0 ;
349 is countRecords($secondary), 1 ;
350
351
352 # check deleted from primary
353 ok $primary->db_get(0, $v) != 0;
354 is countRecords($primary), 1 ;
355
356 }
357
358 {
359 # db->associate -- primary hash, secondary recno
360
361 sub sec_key4
362 {
363 #print "in sec_key4\n";
364 my $pkey = shift ;
365 my $pdata = shift ;
366
367 $_[0] = length $pdata ;
368 return 0;
369 }
370
371 my ($Dfile1, $Dfile2);
372 my $lex = new LexFile $Dfile1, $Dfile2 ;
373 my %hash ;
374 my $status;
375 my ($k, $v, $pk) = ('','','');
376
377 # create primary database
378 ok my $primary = new BerkeleyDB::Hash -Filename => $Dfile1,
379 -Flags => DB_CREATE ;
380
381 # create secondary database
382 ok my $secondary = new BerkeleyDB::Recno -Filename => $Dfile2,
383 #-Property => DB_DUP,
384 -Flags => DB_CREATE ;
385
386 # associate primary with secondary
387 ok $primary->associate($secondary, \&sec_key4) == 0;
388
389 # add data to the primary
390 my %data = (
391 "red" => "flag",
392 "green" => "house",
393 "blue" => "sea",
394 ) ;
395
396 my $ret = 0 ;
397 while (($k, $v) = each %data) {
398 my $r = $primary->db_put($k, $v) ;
399 #print "put $r $BerkeleyDB::Error\n";
400 $ret += $r;
401 }
402 ok $ret == 0 ;
403
404 # check the records in the secondary
405 is countRecords($secondary), 3 ;
406
407 ok $secondary->db_get(0, $v) != 0;
408 ok $secondary->db_get(1, $v) != 0;
409 ok $secondary->db_get(2, $v) != 0;
410 ok $secondary->db_get(3, $v) == 0;
411 ok $v eq "sea";
412
413 ok $secondary->db_get(4, $v) == 0;
414 is $v, "flag";
415
416 ok $secondary->db_get(5, $v) == 0;
417 is $v, "house";
418
419 # pget to primary database is illegal
420 ok $primary->db_pget(0, $pk, $v) != 0 ;
421
422 # pget to secondary database is ok
423 ok $secondary->db_pget(4, $pk, $v) == 0 ;
424 is $pk, 'red'
425 or warn "# $pk\n";;
426 is $v, 'flag';
427
428 ok my $p_cursor = $primary->db_cursor();
429 ok my $s_cursor = $secondary->db_cursor();
430
431 # c_get from primary
432 $k = 'green';
433 ok $p_cursor->c_get($k, $v, DB_SET) == 0;
434 is $k, 'green';
435 is $v, 'house';
436
437 # c_get from secondary
438 $k = 3;
439 ok $s_cursor->c_get($k, $v, DB_SET) == 0;
440 is $k, 3 ;
441 is $v, 'sea';
442
443 # c_pget from primary database should fail
444 $k = 1;
445 ok $p_cursor->c_pget($k, $pk, $v, DB_SET) != 0;
446
447 # c_pget from secondary database
448 $k = 5;
449 ok $s_cursor->c_pget($k, $pk, $v, DB_SET) == 0
450 or diag "$BerkeleyDB::Error\n";
451 is $k, 5 ;
452 is $pk, 'green';
453 is $v, 'house';
454
455 # check put to secondary is illegal
456 ok $secondary->db_put(77, "dick") != 0;
457 is countRecords($secondary), 3 ;
458
459 # delete from primary
460 ok $primary->db_del("green") == 0 ;
461 is countRecords($primary), 2 ;
462
463 # check has been deleted in secondary
464 ok $secondary->db_get(5, $v) != 0;
465 is countRecords($secondary), 2 ;
466
467 # delete from secondary
468 ok $secondary->db_del(4) == 0 ;
469 is countRecords($secondary), 1 ;
470
471
472 # check deleted from primary
473 ok $primary->db_get("red", $v) != 0;
474 is countRecords($primary), 1 ;
475
476 }