Commit | Line | Data |
a0d0e21e |
1 | # DB_File.pm -- Perl 5 interface to Berkeley DB |
2 | # |
3 | # written by Paul Marquess (pmarquess@bfsec.bt.co.uk) |
610ab055 |
4 | # last modified 10th Nov 1996 |
5 | # version 1.05 |
8e07c86e |
6 | |
7 | package DB_File::HASHINFO ; |
785da04d |
8 | |
610ab055 |
9 | require 5.003 ; |
10 | |
785da04d |
11 | use strict; |
8e07c86e |
12 | use Carp; |
88108326 |
13 | require Tie::Hash; |
14 | @DB_File::HASHINFO::ISA = qw(Tie::Hash); |
8e07c86e |
15 | |
88108326 |
16 | sub new |
8e07c86e |
17 | { |
88108326 |
18 | my $pkg = shift ; |
19 | my %x ; |
20 | tie %x, $pkg ; |
21 | bless \%x, $pkg ; |
8e07c86e |
22 | } |
23 | |
610ab055 |
24 | |
88108326 |
25 | sub TIEHASH |
26 | { |
27 | my $pkg = shift ; |
28 | |
610ab055 |
29 | bless { 'bsize' => 0, |
30 | 'ffactor' => 0, |
31 | 'nelem' => 0, |
32 | 'cachesize' => 0, |
88108326 |
33 | 'hash' => undef, |
610ab055 |
34 | 'lorder' => 0, |
88108326 |
35 | }, $pkg ; |
36 | } |
8e07c86e |
37 | |
610ab055 |
38 | |
8e07c86e |
39 | sub FETCH |
40 | { |
88108326 |
41 | my $self = shift ; |
42 | my $key = shift ; |
8e07c86e |
43 | |
88108326 |
44 | return $self->{$key} if exists $self->{$key} ; |
45 | |
46 | my $pkg = ref $self ; |
47 | croak "${pkg}::FETCH - Unknown element '$key'" ; |
8e07c86e |
48 | } |
49 | |
50 | |
51 | sub STORE |
52 | { |
88108326 |
53 | my $self = shift ; |
54 | my $key = shift ; |
55 | my $value = shift ; |
56 | |
57 | if ( exists $self->{$key} ) |
8e07c86e |
58 | { |
88108326 |
59 | $self->{$key} = $value ; |
8e07c86e |
60 | return ; |
61 | } |
62 | |
88108326 |
63 | my $pkg = ref $self ; |
64 | croak "${pkg}::STORE - Unknown element '$key'" ; |
8e07c86e |
65 | } |
66 | |
67 | sub DELETE |
68 | { |
88108326 |
69 | my $self = shift ; |
70 | my $key = shift ; |
71 | |
72 | if ( exists $self->{$key} ) |
8e07c86e |
73 | { |
88108326 |
74 | delete $self->{$key} ; |
8e07c86e |
75 | return ; |
76 | } |
77 | |
88108326 |
78 | my $pkg = ref $self ; |
79 | croak "DB_File::HASHINFO::DELETE - Unknown element '$key'" ; |
8e07c86e |
80 | } |
81 | |
88108326 |
82 | sub EXISTS |
8e07c86e |
83 | { |
88108326 |
84 | my $self = shift ; |
85 | my $key = shift ; |
8e07c86e |
86 | |
88108326 |
87 | exists $self->{$key} ; |
8e07c86e |
88 | } |
89 | |
88108326 |
90 | sub NotHere |
8e07c86e |
91 | { |
88108326 |
92 | my $pkg = shift ; |
93 | my $method = shift ; |
8e07c86e |
94 | |
88108326 |
95 | croak "${pkg} does not define the method ${method}" ; |
8e07c86e |
96 | } |
97 | |
88108326 |
98 | sub DESTROY { undef %{$_[0]} } |
99 | sub FIRSTKEY { my $self = shift ; $self->NotHere(ref $self, "FIRSTKEY") } |
100 | sub NEXTKEY { my $self = shift ; $self->NotHere(ref $self, "NEXTKEY") } |
101 | sub CLEAR { my $self = shift ; $self->NotHere(ref $self, "CLEAR") } |
8e07c86e |
102 | |
103 | package DB_File::RECNOINFO ; |
785da04d |
104 | |
88108326 |
105 | use strict ; |
106 | |
107 | @DB_File::RECNOINFO::ISA = qw(DB_File::HASHINFO) ; |
8e07c86e |
108 | |
109 | sub TIEHASH |
110 | { |
88108326 |
111 | my $pkg = shift ; |
112 | |
610ab055 |
113 | bless { 'bval' => 0, |
114 | 'cachesize' => 0, |
115 | 'psize' => 0, |
116 | 'flags' => 0, |
117 | 'lorder' => 0, |
118 | 'reclen' => 0, |
88108326 |
119 | 'bfname' => "", |
120 | }, $pkg ; |
8e07c86e |
121 | } |
122 | |
88108326 |
123 | package DB_File::BTREEINFO ; |
8e07c86e |
124 | |
88108326 |
125 | use strict ; |
8e07c86e |
126 | |
88108326 |
127 | @DB_File::BTREEINFO::ISA = qw(DB_File::HASHINFO) ; |
8e07c86e |
128 | |
88108326 |
129 | sub TIEHASH |
8e07c86e |
130 | { |
88108326 |
131 | my $pkg = shift ; |
132 | |
610ab055 |
133 | bless { 'flags' => 0, |
134 | 'cachesize' => 0, |
135 | 'maxkeypage' => 0, |
136 | 'minkeypage' => 0, |
137 | 'psize' => 0, |
88108326 |
138 | 'compare' => undef, |
139 | 'prefix' => undef, |
610ab055 |
140 | 'lorder' => 0, |
88108326 |
141 | }, $pkg ; |
8e07c86e |
142 | } |
143 | |
144 | |
8e07c86e |
145 | package DB_File ; |
785da04d |
146 | |
147 | use strict; |
148 | use vars qw($VERSION @ISA @EXPORT $AUTOLOAD $DB_BTREE $DB_HASH $DB_RECNO) ; |
8e07c86e |
149 | use Carp; |
150 | |
785da04d |
151 | |
610ab055 |
152 | $VERSION = "1.05" ; |
8e07c86e |
153 | |
154 | #typedef enum { DB_BTREE, DB_HASH, DB_RECNO } DBTYPE; |
88108326 |
155 | $DB_BTREE = new DB_File::BTREEINFO ; |
156 | $DB_HASH = new DB_File::HASHINFO ; |
157 | $DB_RECNO = new DB_File::RECNOINFO ; |
8e07c86e |
158 | |
785da04d |
159 | require Tie::Hash; |
8e07c86e |
160 | require Exporter; |
161 | use AutoLoader; |
162 | require DynaLoader; |
785da04d |
163 | @ISA = qw(Tie::Hash Exporter DynaLoader); |
8e07c86e |
164 | @EXPORT = qw( |
165 | $DB_BTREE $DB_HASH $DB_RECNO |
88108326 |
166 | |
8e07c86e |
167 | BTREEMAGIC |
168 | BTREEVERSION |
169 | DB_LOCK |
170 | DB_SHMEM |
171 | DB_TXN |
172 | HASHMAGIC |
173 | HASHVERSION |
174 | MAX_PAGE_NUMBER |
175 | MAX_PAGE_OFFSET |
176 | MAX_REC_NUMBER |
177 | RET_ERROR |
178 | RET_SPECIAL |
179 | RET_SUCCESS |
180 | R_CURSOR |
181 | R_DUP |
182 | R_FIRST |
183 | R_FIXEDLEN |
184 | R_IAFTER |
185 | R_IBEFORE |
186 | R_LAST |
187 | R_NEXT |
188 | R_NOKEY |
189 | R_NOOVERWRITE |
190 | R_PREV |
191 | R_RECNOSYNC |
192 | R_SETCURSOR |
193 | R_SNAPSHOT |
194 | __R_UNUSED |
88108326 |
195 | |
8e07c86e |
196 | ); |
197 | |
198 | sub AUTOLOAD { |
785da04d |
199 | my($constname); |
8e07c86e |
200 | ($constname = $AUTOLOAD) =~ s/.*:://; |
785da04d |
201 | my $val = constant($constname, @_ ? $_[0] : 0); |
8e07c86e |
202 | if ($! != 0) { |
203 | if ($! =~ /Invalid/) { |
204 | $AutoLoader::AUTOLOAD = $AUTOLOAD; |
205 | goto &AutoLoader::AUTOLOAD; |
206 | } |
207 | else { |
785da04d |
208 | my($pack,$file,$line) = caller; |
8e07c86e |
209 | croak "Your vendor has not defined DB macro $constname, used at $file line $line. |
210 | "; |
211 | } |
212 | } |
213 | eval "sub $AUTOLOAD { $val }"; |
214 | goto &$AUTOLOAD; |
215 | } |
216 | |
f6b705ef |
217 | |
218 | # import borrowed from IO::File |
219 | # exports Fcntl constants if available. |
220 | sub import { |
221 | my $pkg = shift; |
222 | my $callpkg = caller; |
223 | Exporter::export $pkg, $callpkg; |
224 | eval { |
225 | require Fcntl; |
226 | Exporter::export 'Fcntl', $callpkg; |
227 | }; |
228 | } |
229 | |
785da04d |
230 | bootstrap DB_File $VERSION; |
8e07c86e |
231 | |
232 | # Preloaded methods go here. Autoload methods go after __END__, and are |
233 | # processed by the autosplit program. |
234 | |
610ab055 |
235 | sub TIEHASH |
236 | { |
237 | my (@arg) = @_ ; |
238 | |
239 | $arg[4] = tied %{ $arg[4] } |
240 | if @arg >= 5 && ref $arg[4] && $arg[4] =~ /=HASH/ && tied %{ $arg[4] } ; |
241 | |
242 | DoTie_(@arg) ; |
243 | } |
244 | |
245 | *TIEARRAY = \&TIEHASH ; |
88108326 |
246 | |
247 | sub get_dup |
248 | { |
249 | croak "Usage: \$db->get_dup(key [,flag])\n" |
250 | unless @_ == 2 or @_ == 3 ; |
251 | |
252 | my $db = shift ; |
253 | my $key = shift ; |
254 | my $flag = shift ; |
f6b705ef |
255 | my $value = 0 ; |
88108326 |
256 | my $origkey = $key ; |
257 | my $wantarray = wantarray ; |
f6b705ef |
258 | my %values = () ; |
88108326 |
259 | my @values = () ; |
260 | my $counter = 0 ; |
f6b705ef |
261 | my $status = 0 ; |
88108326 |
262 | |
f6b705ef |
263 | # iterate through the database until either EOF ($status == 0) |
264 | # or a different key is encountered ($key ne $origkey). |
265 | for ($status = $db->seq($key, $value, R_CURSOR()) ; |
266 | $status == 0 and $key eq $origkey ; |
267 | $status = $db->seq($key, $value, R_NEXT()) ) { |
88108326 |
268 | |
f6b705ef |
269 | # save the value or count number of matches |
270 | if ($wantarray) { |
271 | if ($flag) |
272 | { ++ $values{$value} } |
273 | else |
274 | { push (@values, $value) } |
275 | } |
276 | else |
277 | { ++ $counter } |
88108326 |
278 | |
88108326 |
279 | } |
280 | |
f6b705ef |
281 | return ($wantarray ? ($flag ? %values : @values) : $counter) ; |
88108326 |
282 | } |
283 | |
284 | |
8e07c86e |
285 | 1; |
286 | __END__ |
287 | |
288 | =cut |
3b35bae3 |
289 | |
290 | =head1 NAME |
291 | |
292 | DB_File - Perl5 access to Berkeley DB |
293 | |
294 | =head1 SYNOPSIS |
295 | |
296 | use DB_File ; |
88108326 |
297 | |
298 | [$X =] tie %hash, 'DB_File', [$filename, $flags, $mode, $DB_HASH] ; |
299 | [$X =] tie %hash, 'DB_File', $filename, $flags, $mode, $DB_BTREE ; |
300 | [$X =] tie @array, 'DB_File', $filename, $flags, $mode, $DB_RECNO ; |
760ac839 |
301 | |
3b35bae3 |
302 | $status = $X->del($key [, $flags]) ; |
303 | $status = $X->put($key, $value [, $flags]) ; |
304 | $status = $X->get($key, $value [, $flags]) ; |
760ac839 |
305 | $status = $X->seq($key, $value, $flags) ; |
3b35bae3 |
306 | $status = $X->sync([$flags]) ; |
307 | $status = $X->fd ; |
760ac839 |
308 | |
f6b705ef |
309 | # BTREE only |
88108326 |
310 | $count = $X->get_dup($key) ; |
311 | @list = $X->get_dup($key) ; |
312 | %list = $X->get_dup($key, 1) ; |
313 | |
f6b705ef |
314 | # RECNO only |
315 | $a = $X->length; |
316 | $a = $X->pop ; |
317 | $X->push(list); |
318 | $a = $X->shift; |
319 | $X->unshift(list); |
320 | |
3b35bae3 |
321 | untie %hash ; |
322 | untie @array ; |
323 | |
324 | =head1 DESCRIPTION |
325 | |
8e07c86e |
326 | B<DB_File> is a module which allows Perl programs to make use of the |
327 | facilities provided by Berkeley DB. If you intend to use this |
f6b705ef |
328 | module you should really have a copy of the Berkeley DB manual pages at |
8e07c86e |
329 | hand. The interface defined here mirrors the Berkeley DB interface |
330 | closely. |
3b35bae3 |
331 | |
8e07c86e |
332 | Berkeley DB is a C library which provides a consistent interface to a |
333 | number of database formats. B<DB_File> provides an interface to all |
334 | three of the database types currently supported by Berkeley DB. |
3b35bae3 |
335 | |
336 | The file types are: |
337 | |
338 | =over 5 |
339 | |
88108326 |
340 | =item B<DB_HASH> |
3b35bae3 |
341 | |
88108326 |
342 | This database type allows arbitrary key/value pairs to be stored in data |
8e07c86e |
343 | files. This is equivalent to the functionality provided by other |
344 | hashing packages like DBM, NDBM, ODBM, GDBM, and SDBM. Remember though, |
345 | the files created using DB_HASH are not compatible with any of the |
346 | other packages mentioned. |
3b35bae3 |
347 | |
8e07c86e |
348 | A default hashing algorithm, which will be adequate for most |
349 | applications, is built into Berkeley DB. If you do need to use your own |
350 | hashing algorithm it is possible to write your own in Perl and have |
351 | B<DB_File> use it instead. |
3b35bae3 |
352 | |
88108326 |
353 | =item B<DB_BTREE> |
354 | |
355 | The btree format allows arbitrary key/value pairs to be stored in a |
8e07c86e |
356 | sorted, balanced binary tree. |
3b35bae3 |
357 | |
8e07c86e |
358 | As with the DB_HASH format, it is possible to provide a user defined |
359 | Perl routine to perform the comparison of keys. By default, though, the |
360 | keys are stored in lexical order. |
3b35bae3 |
361 | |
88108326 |
362 | =item B<DB_RECNO> |
3b35bae3 |
363 | |
8e07c86e |
364 | DB_RECNO allows both fixed-length and variable-length flat text files |
365 | to be manipulated using the same key/value pair interface as in DB_HASH |
366 | and DB_BTREE. In this case the key will consist of a record (line) |
367 | number. |
3b35bae3 |
368 | |
369 | =back |
370 | |
371 | =head2 How does DB_File interface to Berkeley DB? |
372 | |
373 | B<DB_File> allows access to Berkeley DB files using the tie() mechanism |
8e07c86e |
374 | in Perl 5 (for full details, see L<perlfunc/tie()>). This facility |
375 | allows B<DB_File> to access Berkeley DB files using either an |
376 | associative array (for DB_HASH & DB_BTREE file types) or an ordinary |
377 | array (for the DB_RECNO file type). |
3b35bae3 |
378 | |
88108326 |
379 | In addition to the tie() interface, it is also possible to access most |
380 | of the functions provided in the Berkeley DB API directly. |
f6b705ef |
381 | See L<THE API INTERFACE>. |
3b35bae3 |
382 | |
88108326 |
383 | =head2 Opening a Berkeley DB Database File |
3b35bae3 |
384 | |
8e07c86e |
385 | Berkeley DB uses the function dbopen() to open or create a database. |
f6b705ef |
386 | Here is the C prototype for dbopen(): |
3b35bae3 |
387 | |
388 | DB* |
389 | dbopen (const char * file, int flags, int mode, |
390 | DBTYPE type, const void * openinfo) |
391 | |
392 | The parameter C<type> is an enumeration which specifies which of the 3 |
393 | interface methods (DB_HASH, DB_BTREE or DB_RECNO) is to be used. |
394 | Depending on which of these is actually chosen, the final parameter, |
395 | I<openinfo> points to a data structure which allows tailoring of the |
396 | specific interface method. |
397 | |
8e07c86e |
398 | This interface is handled slightly differently in B<DB_File>. Here is |
88108326 |
399 | an equivalent call using B<DB_File>: |
3b35bae3 |
400 | |
88108326 |
401 | tie %array, 'DB_File', $filename, $flags, $mode, $DB_HASH ; |
3b35bae3 |
402 | |
8e07c86e |
403 | The C<filename>, C<flags> and C<mode> parameters are the direct |
404 | equivalent of their dbopen() counterparts. The final parameter $DB_HASH |
405 | performs the function of both the C<type> and C<openinfo> parameters in |
406 | dbopen(). |
3b35bae3 |
407 | |
88108326 |
408 | In the example above $DB_HASH is actually a pre-defined reference to a |
409 | hash object. B<DB_File> has three of these pre-defined references. |
410 | Apart from $DB_HASH, there is also $DB_BTREE and $DB_RECNO. |
3b35bae3 |
411 | |
8e07c86e |
412 | The keys allowed in each of these pre-defined references is limited to |
413 | the names used in the equivalent C structure. So, for example, the |
414 | $DB_HASH reference will only allow keys called C<bsize>, C<cachesize>, |
88108326 |
415 | C<ffactor>, C<hash>, C<lorder> and C<nelem>. |
416 | |
417 | To change one of these elements, just assign to it like this: |
418 | |
419 | $DB_HASH->{'cachesize'} = 10000 ; |
420 | |
421 | The three predefined variables $DB_HASH, $DB_BTREE and $DB_RECNO are |
422 | usually adequate for most applications. If you do need to create extra |
423 | instances of these objects, constructors are available for each file |
424 | type. |
425 | |
426 | Here are examples of the constructors and the valid options available |
427 | for DB_HASH, DB_BTREE and DB_RECNO respectively. |
428 | |
429 | $a = new DB_File::HASHINFO ; |
430 | $a->{'bsize'} ; |
431 | $a->{'cachesize'} ; |
432 | $a->{'ffactor'}; |
433 | $a->{'hash'} ; |
434 | $a->{'lorder'} ; |
435 | $a->{'nelem'} ; |
436 | |
437 | $b = new DB_File::BTREEINFO ; |
438 | $b->{'flags'} ; |
439 | $b->{'cachesize'} ; |
440 | $b->{'maxkeypage'} ; |
441 | $b->{'minkeypage'} ; |
442 | $b->{'psize'} ; |
443 | $b->{'compare'} ; |
444 | $b->{'prefix'} ; |
445 | $b->{'lorder'} ; |
446 | |
447 | $c = new DB_File::RECNOINFO ; |
448 | $c->{'bval'} ; |
449 | $c->{'cachesize'} ; |
450 | $c->{'psize'} ; |
451 | $c->{'flags'} ; |
452 | $c->{'lorder'} ; |
453 | $c->{'reclen'} ; |
454 | $c->{'bfname'} ; |
455 | |
456 | The values stored in the hashes above are mostly the direct equivalent |
457 | of their C counterpart. Like their C counterparts, all are set to a |
f6b705ef |
458 | default values - that means you don't have to set I<all> of the |
88108326 |
459 | values when you only want to change one. Here is an example: |
460 | |
461 | $a = new DB_File::HASHINFO ; |
462 | $a->{'cachesize'} = 12345 ; |
463 | tie %y, 'DB_File', "filename", $flags, 0777, $a ; |
464 | |
465 | A few of the values need extra discussion here. When used, the C |
466 | equivalent of the keys C<hash>, C<compare> and C<prefix> store pointers |
467 | to C functions. In B<DB_File> these keys are used to store references |
468 | to Perl subs. Below are templates for each of the subs: |
469 | |
470 | sub hash |
471 | { |
472 | my ($data) = @_ ; |
473 | ... |
474 | # return the hash value for $data |
475 | return $hash ; |
476 | } |
3b35bae3 |
477 | |
88108326 |
478 | sub compare |
479 | { |
480 | my ($key, $key2) = @_ ; |
481 | ... |
482 | # return 0 if $key1 eq $key2 |
483 | # -1 if $key1 lt $key2 |
484 | # 1 if $key1 gt $key2 |
485 | return (-1 , 0 or 1) ; |
486 | } |
3b35bae3 |
487 | |
88108326 |
488 | sub prefix |
489 | { |
490 | my ($key, $key2) = @_ ; |
491 | ... |
492 | # return number of bytes of $key2 which are |
493 | # necessary to determine that it is greater than $key1 |
494 | return $bytes ; |
495 | } |
3b35bae3 |
496 | |
f6b705ef |
497 | See L<Changing the BTREE sort order> for an example of using the |
498 | C<compare> template. |
88108326 |
499 | |
500 | =head2 Default Parameters |
501 | |
502 | It is possible to omit some or all of the final 4 parameters in the |
503 | call to C<tie> and let them take default values. As DB_HASH is the most |
504 | common file format used, the call: |
505 | |
506 | tie %A, "DB_File", "filename" ; |
507 | |
508 | is equivalent to: |
509 | |
510 | tie %A, "DB_File", "filename", O_CREAT|O_RDWR, 0640, $DB_HASH ; |
511 | |
512 | It is also possible to omit the filename parameter as well, so the |
513 | call: |
514 | |
515 | tie %A, "DB_File" ; |
516 | |
517 | is equivalent to: |
518 | |
519 | tie %A, "DB_File", undef, O_CREAT|O_RDWR, 0640, $DB_HASH ; |
520 | |
f6b705ef |
521 | See L<In Memory Databases> for a discussion on the use of C<undef> |
88108326 |
522 | in place of a filename. |
523 | |
f6b705ef |
524 | =head2 In Memory Databases |
525 | |
526 | Berkeley DB allows the creation of in-memory databases by using NULL |
527 | (that is, a C<(char *)0> in C) in place of the filename. B<DB_File> |
528 | uses C<undef> instead of NULL to provide this functionality. |
529 | |
530 | =head1 DB_HASH |
531 | |
532 | The DB_HASH file format is probably the most commonly used of the three |
533 | file formats that B<DB_File> supports. It is also very straightforward |
534 | to use. |
535 | |
536 | =head2 A Simple Example. |
537 | |
538 | This example shows how to create a database, add key/value pairs to the |
539 | database, delete keys/value pairs and finally how to enumerate the |
540 | contents of the database. |
541 | |
610ab055 |
542 | use strict ; |
f6b705ef |
543 | use DB_File ; |
610ab055 |
544 | use vars qw( %h $k $v ) ; |
f6b705ef |
545 | |
546 | tie %h, "DB_File", "fruit", O_RDWR|O_CREAT, 0640, $DB_HASH |
547 | or die "Cannot open file 'fruit': $!\n"; |
548 | |
549 | # Add a few key/value pairs to the file |
550 | $h{"apple"} = "red" ; |
551 | $h{"orange"} = "orange" ; |
552 | $h{"banana"} = "yellow" ; |
553 | $h{"tomato"} = "red" ; |
554 | |
555 | # Check for existence of a key |
556 | print "Banana Exists\n\n" if $h{"banana"} ; |
557 | |
558 | # Delete a key/value pair. |
559 | delete $h{"apple"} ; |
560 | |
561 | # print the contents of the file |
562 | while (($k, $v) = each %h) |
563 | { print "$k -> $v\n" } |
564 | |
565 | untie %h ; |
566 | |
567 | here is the output: |
568 | |
569 | Banana Exists |
570 | |
571 | orange -> orange |
572 | tomato -> red |
573 | banana -> yellow |
574 | |
575 | Note that the like ordinary associative arrays, the order of the keys |
576 | retrieved is in an apparently random order. |
577 | |
578 | =head1 DB_BTREE |
579 | |
580 | The DB_BTREE format is useful when you want to store data in a given |
581 | order. By default the keys will be stored in lexical order, but as you |
582 | will see from the example shown in the next section, it is very easy to |
583 | define your own sorting function. |
584 | |
585 | =head2 Changing the BTREE sort order |
586 | |
587 | This script shows how to override the default sorting algorithm that |
588 | BTREE uses. Instead of using the normal lexical ordering, a case |
589 | insensitive compare function will be used. |
88108326 |
590 | |
610ab055 |
591 | use strict ; |
f6b705ef |
592 | use DB_File ; |
610ab055 |
593 | |
594 | my %h ; |
f6b705ef |
595 | |
596 | sub Compare |
597 | { |
598 | my ($key1, $key2) = @_ ; |
599 | "\L$key1" cmp "\L$key2" ; |
600 | } |
601 | |
602 | # specify the Perl sub that will do the comparison |
603 | $DB_BTREE->{'compare'} = \&Compare ; |
604 | |
605 | tie %h, "DB_File", "tree", O_RDWR|O_CREAT, 0640, $DB_BTREE |
606 | or die "Cannot open file 'tree': $!\n" ; |
607 | |
608 | # Add a key/value pair to the file |
609 | $h{'Wall'} = 'Larry' ; |
610 | $h{'Smith'} = 'John' ; |
611 | $h{'mouse'} = 'mickey' ; |
612 | $h{'duck'} = 'donald' ; |
613 | |
614 | # Delete |
615 | delete $h{"duck"} ; |
616 | |
617 | # Cycle through the keys printing them in order. |
618 | # Note it is not necessary to sort the keys as |
619 | # the btree will have kept them in order automatically. |
620 | foreach (keys %h) |
621 | { print "$_\n" } |
622 | |
623 | untie %h ; |
624 | |
625 | Here is the output from the code above. |
626 | |
627 | mouse |
628 | Smith |
629 | Wall |
630 | |
631 | There are a few point to bear in mind if you want to change the |
632 | ordering in a BTREE database: |
633 | |
634 | =over 5 |
635 | |
636 | =item 1. |
637 | |
638 | The new compare function must be specified when you create the database. |
639 | |
640 | =item 2. |
641 | |
642 | You cannot change the ordering once the database has been created. Thus |
643 | you must use the same compare function every time you access the |
88108326 |
644 | database. |
645 | |
f6b705ef |
646 | =back |
647 | |
648 | =head2 Handling duplicate keys |
649 | |
650 | The BTREE file type optionally allows a single key to be associated |
651 | with an arbitrary number of values. This option is enabled by setting |
652 | the flags element of C<$DB_BTREE> to R_DUP when creating the database. |
653 | |
88108326 |
654 | There are some difficulties in using the tied hash interface if you |
655 | want to manipulate a BTREE database with duplicate keys. Consider this |
656 | code: |
657 | |
610ab055 |
658 | use strict ; |
88108326 |
659 | use DB_File ; |
610ab055 |
660 | |
661 | use vars qw($filename %h ) ; |
662 | |
88108326 |
663 | $filename = "tree" ; |
664 | unlink $filename ; |
665 | |
666 | # Enable duplicate records |
667 | $DB_BTREE->{'flags'} = R_DUP ; |
668 | |
669 | tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0640, $DB_BTREE |
670 | or die "Cannot open $filename: $!\n"; |
671 | |
672 | # Add some key/value pairs to the file |
673 | $h{'Wall'} = 'Larry' ; |
674 | $h{'Wall'} = 'Brick' ; # Note the duplicate key |
f6b705ef |
675 | $h{'Wall'} = 'Brick' ; # Note the duplicate key and value |
88108326 |
676 | $h{'Smith'} = 'John' ; |
677 | $h{'mouse'} = 'mickey' ; |
678 | |
679 | # iterate through the associative array |
680 | # and print each key/value pair. |
681 | foreach (keys %h) |
682 | { print "$_ -> $h{$_}\n" } |
683 | |
f6b705ef |
684 | untie %h ; |
685 | |
88108326 |
686 | Here is the output: |
687 | |
688 | Smith -> John |
689 | Wall -> Larry |
690 | Wall -> Larry |
f6b705ef |
691 | Wall -> Larry |
88108326 |
692 | mouse -> mickey |
693 | |
f6b705ef |
694 | As you can see 3 records have been successfully created with key C<Wall> |
88108326 |
695 | - the only thing is, when they are retrieved from the database they |
f6b705ef |
696 | I<seem> to have the same value, namely C<Larry>. The problem is caused |
697 | by the way that the associative array interface works. Basically, when |
698 | the associative array interface is used to fetch the value associated |
699 | with a given key, it will only ever retrieve the first value. |
88108326 |
700 | |
701 | Although it may not be immediately obvious from the code above, the |
702 | associative array interface can be used to write values with duplicate |
703 | keys, but it cannot be used to read them back from the database. |
704 | |
705 | The way to get around this problem is to use the Berkeley DB API method |
706 | called C<seq>. This method allows sequential access to key/value |
f6b705ef |
707 | pairs. See L<THE API INTERFACE> for details of both the C<seq> method |
708 | and the API in general. |
88108326 |
709 | |
710 | Here is the script above rewritten using the C<seq> API method. |
711 | |
610ab055 |
712 | use strict ; |
88108326 |
713 | use DB_File ; |
88108326 |
714 | |
610ab055 |
715 | use vars qw($filename $x %h $status $key $value) ; |
716 | |
88108326 |
717 | $filename = "tree" ; |
718 | unlink $filename ; |
719 | |
720 | # Enable duplicate records |
721 | $DB_BTREE->{'flags'} = R_DUP ; |
722 | |
723 | $x = tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0640, $DB_BTREE |
724 | or die "Cannot open $filename: $!\n"; |
725 | |
726 | # Add some key/value pairs to the file |
727 | $h{'Wall'} = 'Larry' ; |
728 | $h{'Wall'} = 'Brick' ; # Note the duplicate key |
f6b705ef |
729 | $h{'Wall'} = 'Brick' ; # Note the duplicate key and value |
88108326 |
730 | $h{'Smith'} = 'John' ; |
731 | $h{'mouse'} = 'mickey' ; |
732 | |
f6b705ef |
733 | # iterate through the btree using seq |
88108326 |
734 | # and print each key/value pair. |
610ab055 |
735 | $key = $value = 0 ; |
f6b705ef |
736 | for ($status = $x->seq($key, $value, R_FIRST) ; |
737 | $status == 0 ; |
738 | $status = $x->seq($key, $value, R_NEXT) ) |
88108326 |
739 | { print "$key -> $value\n" } |
740 | |
741 | undef $x ; |
742 | untie %h ; |
743 | |
744 | that prints: |
745 | |
746 | Smith -> John |
747 | Wall -> Brick |
f6b705ef |
748 | Wall -> Brick |
88108326 |
749 | Wall -> Larry |
750 | mouse -> mickey |
751 | |
f6b705ef |
752 | This time we have got all the key/value pairs, including the multiple |
88108326 |
753 | values associated with the key C<Wall>. |
754 | |
f6b705ef |
755 | =head2 The get_dup method. |
756 | |
757 | B<DB_File> comes with a utility method, called C<get_dup>, to assist in |
88108326 |
758 | reading duplicate values from BTREE databases. The method can take the |
759 | following forms: |
760 | |
761 | $count = $x->get_dup($key) ; |
762 | @list = $x->get_dup($key) ; |
763 | %list = $x->get_dup($key, 1) ; |
764 | |
765 | In a scalar context the method returns the number of values associated |
766 | with the key, C<$key>. |
767 | |
768 | In list context, it returns all the values which match C<$key>. Note |
f6b705ef |
769 | that the values will be returned in an apparently random order. |
88108326 |
770 | |
f6b705ef |
771 | In list context, if the second parameter is present and evaluates TRUE, |
772 | the method returns an associative array. The keys of the associative |
773 | array correspond to the the values that matched in the BTREE and the |
774 | values of the array are a count of the number of times that particular |
775 | value occurred in the BTREE. |
88108326 |
776 | |
f6b705ef |
777 | So assuming the database created above, we can use C<get_dup> like |
88108326 |
778 | this: |
779 | |
610ab055 |
780 | my $cnt = $x->get_dup("Wall") ; |
88108326 |
781 | print "Wall occurred $cnt times\n" ; |
782 | |
610ab055 |
783 | my %hash = $x->get_dup("Wall", 1) ; |
88108326 |
784 | print "Larry is there\n" if $hash{'Larry'} ; |
f6b705ef |
785 | print "There are $hash{'Brick'} Brick Walls\n" ; |
88108326 |
786 | |
610ab055 |
787 | my @list = $x->get_dup("Wall") ; |
88108326 |
788 | print "Wall => [@list]\n" ; |
789 | |
f6b705ef |
790 | @list = $x->get_dup("Smith") ; |
88108326 |
791 | print "Smith => [@list]\n" ; |
792 | |
f6b705ef |
793 | @list = $x->get_dup("Dog") ; |
88108326 |
794 | print "Dog => [@list]\n" ; |
795 | |
796 | |
797 | and it will print: |
798 | |
f6b705ef |
799 | Wall occurred 3 times |
88108326 |
800 | Larry is there |
f6b705ef |
801 | There are 2 Brick Walls |
802 | Wall => [Brick Brick Larry] |
88108326 |
803 | Smith => [John] |
804 | Dog => [] |
3b35bae3 |
805 | |
f6b705ef |
806 | =head2 Matching Partial Keys |
807 | |
808 | The BTREE interface has a feature which allows partial keys to be |
809 | matched. This functionality is I<only> available when the C<seq> method |
810 | is used along with the R_CURSOR flag. |
811 | |
812 | $x->seq($key, $value, R_CURSOR) ; |
813 | |
814 | Here is the relevant quote from the dbopen man page where it defines |
815 | the use of the R_CURSOR flag with seq: |
816 | |
f6b705ef |
817 | Note, for the DB_BTREE access method, the returned key is not |
818 | necessarily an exact match for the specified key. The returned key |
819 | is the smallest key greater than or equal to the specified key, |
820 | permitting partial key matches and range searches. |
821 | |
f6b705ef |
822 | In the example script below, the C<match> sub uses this feature to find |
823 | and print the first matching key/value pair given a partial key. |
824 | |
610ab055 |
825 | use strict ; |
f6b705ef |
826 | use DB_File ; |
827 | use Fcntl ; |
610ab055 |
828 | |
829 | use vars qw($filename $x %h $st $key $value) ; |
f6b705ef |
830 | |
831 | sub match |
832 | { |
833 | my $key = shift ; |
610ab055 |
834 | my $value = 0; |
f6b705ef |
835 | my $orig_key = $key ; |
836 | $x->seq($key, $value, R_CURSOR) ; |
837 | print "$orig_key\t-> $key\t-> $value\n" ; |
838 | } |
839 | |
840 | $filename = "tree" ; |
841 | unlink $filename ; |
842 | |
843 | $x = tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0640, $DB_BTREE |
844 | or die "Cannot open $filename: $!\n"; |
845 | |
846 | # Add some key/value pairs to the file |
847 | $h{'mouse'} = 'mickey' ; |
848 | $h{'Wall'} = 'Larry' ; |
849 | $h{'Walls'} = 'Brick' ; |
850 | $h{'Smith'} = 'John' ; |
851 | |
852 | |
610ab055 |
853 | $key = $value = 0 ; |
f6b705ef |
854 | print "IN ORDER\n" ; |
855 | for ($st = $x->seq($key, $value, R_FIRST) ; |
856 | $st == 0 ; |
857 | $st = $x->seq($key, $value, R_NEXT) ) |
858 | |
859 | { print "$key -> $value\n" } |
860 | |
861 | print "\nPARTIAL MATCH\n" ; |
862 | |
863 | match "Wa" ; |
864 | match "A" ; |
865 | match "a" ; |
866 | |
867 | undef $x ; |
868 | untie %h ; |
869 | |
870 | Here is the output: |
871 | |
872 | IN ORDER |
873 | Smith -> John |
874 | Wall -> Larry |
875 | Walls -> Brick |
876 | mouse -> mickey |
877 | |
878 | PARTIAL MATCH |
879 | Wa -> Wall -> Larry |
880 | A -> Smith -> John |
881 | a -> mouse -> mickey |
882 | |
883 | =head1 DB_RECNO |
884 | |
885 | DB_RECNO provides an interface to flat text files. Both variable and |
886 | fixed length records are supported. |
3b35bae3 |
887 | |
88108326 |
888 | In order to make RECNO more compatible with Perl the array offset for |
889 | all RECNO arrays begins at 0 rather than 1 as in Berkeley DB. |
3b35bae3 |
890 | |
88108326 |
891 | As with normal Perl arrays, a RECNO array can be accessed using |
892 | negative indexes. The index -1 refers to the last element of the array, |
893 | -2 the second last, and so on. Attempting to access an element before |
894 | the start of the array will raise a fatal run-time error. |
3b35bae3 |
895 | |
f6b705ef |
896 | =head2 A Simple Example |
3b35bae3 |
897 | |
f6b705ef |
898 | Here is a simple example that uses RECNO. |
899 | |
610ab055 |
900 | use strict ; |
f6b705ef |
901 | use DB_File ; |
f6b705ef |
902 | |
610ab055 |
903 | my @h ; |
f6b705ef |
904 | tie @h, "DB_File", "text", O_RDWR|O_CREAT, 0640, $DB_RECNO |
905 | or die "Cannot open file 'text': $!\n" ; |
906 | |
907 | # Add a few key/value pairs to the file |
908 | $h[0] = "orange" ; |
909 | $h[1] = "blue" ; |
910 | $h[2] = "yellow" ; |
911 | |
912 | # Check for existence of a key |
913 | print "Element 1 Exists with value $h[1]\n" if $h[1] ; |
914 | |
915 | # use a negative index |
916 | print "The last element is $h[-1]\n" ; |
917 | print "The 2nd last element is $h[-2]\n" ; |
918 | |
919 | untie @h ; |
3b35bae3 |
920 | |
f6b705ef |
921 | Here is the output from the script: |
922 | |
923 | |
924 | Element 1 Exists with value blue |
925 | The last element is yellow |
926 | The 2nd last element is blue |
927 | |
928 | =head2 Extra Methods |
929 | |
930 | As you can see from the example above, the tied array interface is |
931 | quite limited. To make the interface more useful, a number of methods |
932 | are supplied with B<DB_File> to simulate the standard array operations |
933 | that are not currently implemented in Perl's tied array interface. All |
934 | these methods are accessed via the object returned from the tie call. |
935 | |
936 | Here are the methods: |
937 | |
938 | =over 5 |
3b35bae3 |
939 | |
f6b705ef |
940 | =item B<$X-E<gt>push(list) ;> |
941 | |
942 | Pushes the elements of C<list> to the end of the array. |
943 | |
944 | =item B<$value = $X-E<gt>pop ;> |
945 | |
946 | Removes and returns the last element of the array. |
947 | |
948 | =item B<$X-E<gt>shift> |
949 | |
950 | Removes and returns the first element of the array. |
951 | |
952 | =item B<$X-E<gt>unshift(list) ;> |
953 | |
954 | Pushes the elements of C<list> to the start of the array. |
955 | |
956 | =item B<$X-E<gt>length> |
957 | |
958 | Returns the number of elements in the array. |
959 | |
960 | =back |
961 | |
962 | =head2 Another Example |
963 | |
964 | Here is a more complete example that makes use of some of the methods |
965 | described above. It also makes use of the API interface directly (see |
966 | L<THE API INTERFACE>). |
967 | |
968 | use strict ; |
969 | use vars qw(@h $H $file $i) ; |
970 | use DB_File ; |
971 | use Fcntl ; |
972 | |
973 | $file = "text" ; |
974 | |
975 | unlink $file ; |
976 | |
977 | $H = tie @h, "DB_File", $file, O_RDWR|O_CREAT, 0640, $DB_RECNO |
978 | or die "Cannot open file $file: $!\n" ; |
979 | |
980 | # first create a text file to play with |
981 | $h[0] = "zero" ; |
982 | $h[1] = "one" ; |
983 | $h[2] = "two" ; |
984 | $h[3] = "three" ; |
985 | $h[4] = "four" ; |
986 | |
987 | |
988 | # Print the records in order. |
989 | # |
990 | # The length method is needed here because evaluating a tied |
991 | # array in a scalar context does not return the number of |
992 | # elements in the array. |
993 | |
994 | print "\nORIGINAL\n" ; |
995 | foreach $i (0 .. $H->length - 1) { |
996 | print "$i: $h[$i]\n" ; |
997 | } |
998 | |
999 | # use the push & pop methods |
1000 | $a = $H->pop ; |
1001 | $H->push("last") ; |
1002 | print "\nThe last record was [$a]\n" ; |
1003 | |
1004 | # and the shift & unshift methods |
1005 | $a = $H->shift ; |
1006 | $H->unshift("first") ; |
1007 | print "The first record was [$a]\n" ; |
1008 | |
1009 | # Use the API to add a new record after record 2. |
1010 | $i = 2 ; |
1011 | $H->put($i, "Newbie", R_IAFTER) ; |
1012 | |
1013 | # and a new record before record 1. |
1014 | $i = 1 ; |
1015 | $H->put($i, "New One", R_IBEFORE) ; |
1016 | |
1017 | # delete record 3 |
1018 | $H->del(3) ; |
1019 | |
1020 | # now print the records in reverse order |
1021 | print "\nREVERSE\n" ; |
1022 | for ($i = $H->length - 1 ; $i >= 0 ; -- $i) |
1023 | { print "$i: $h[$i]\n" } |
1024 | |
1025 | # same again, but use the API functions instead |
1026 | print "\nREVERSE again\n" ; |
610ab055 |
1027 | my ($s, $k, $v) = (0, 0, 0) ; |
f6b705ef |
1028 | for ($s = $H->seq($k, $v, R_LAST) ; |
1029 | $s == 0 ; |
1030 | $s = $H->seq($k, $v, R_PREV)) |
1031 | { print "$k: $v\n" } |
1032 | |
1033 | undef $H ; |
1034 | untie @h ; |
1035 | |
1036 | and this is what it outputs: |
1037 | |
1038 | ORIGINAL |
1039 | 0: zero |
1040 | 1: one |
1041 | 2: two |
1042 | 3: three |
1043 | 4: four |
1044 | |
1045 | The last record was [four] |
1046 | The first record was [zero] |
1047 | |
1048 | REVERSE |
1049 | 5: last |
1050 | 4: three |
1051 | 3: Newbie |
1052 | 2: one |
1053 | 1: New One |
1054 | 0: first |
1055 | |
1056 | REVERSE again |
1057 | 5: last |
1058 | 4: three |
1059 | 3: Newbie |
1060 | 2: one |
1061 | 1: New One |
1062 | 0: first |
1063 | |
1064 | Notes: |
1065 | |
1066 | =over 5 |
1067 | |
1068 | =item 1. |
1069 | |
1070 | Rather than iterating through the array, C<@h> like this: |
1071 | |
1072 | foreach $i (@h) |
1073 | |
1074 | it is necessary to use either this: |
1075 | |
1076 | foreach $i (0 .. $H->length - 1) |
1077 | |
1078 | or this: |
1079 | |
1080 | for ($a = $H->get($k, $v, R_FIRST) ; |
1081 | $a == 0 ; |
1082 | $a = $H->get($k, $v, R_NEXT) ) |
1083 | |
1084 | =item 2. |
1085 | |
1086 | Notice that both times the C<put> method was used the record index was |
1087 | specified using a variable, C<$i>, rather than the literal value |
1088 | itself. This is because C<put> will return the record number of the |
1089 | inserted line via that parameter. |
1090 | |
1091 | =back |
1092 | |
1093 | =head1 THE API INTERFACE |
3b35bae3 |
1094 | |
1095 | As well as accessing Berkeley DB using a tied hash or array, it is also |
88108326 |
1096 | possible to make direct use of most of the API functions defined in the |
8e07c86e |
1097 | Berkeley DB documentation. |
3b35bae3 |
1098 | |
88108326 |
1099 | To do this you need to store a copy of the object returned from the tie. |
3b35bae3 |
1100 | |
88108326 |
1101 | $db = tie %hash, "DB_File", "filename" ; |
3b35bae3 |
1102 | |
8e07c86e |
1103 | Once you have done that, you can access the Berkeley DB API functions |
88108326 |
1104 | as B<DB_File> methods directly like this: |
3b35bae3 |
1105 | |
1106 | $db->put($key, $value, R_NOOVERWRITE) ; |
1107 | |
88108326 |
1108 | B<Important:> If you have saved a copy of the object returned from |
1109 | C<tie>, the underlying database file will I<not> be closed until both |
1110 | the tied variable is untied and all copies of the saved object are |
610ab055 |
1111 | destroyed. |
88108326 |
1112 | |
1113 | use DB_File ; |
1114 | $db = tie %hash, "DB_File", "filename" |
1115 | or die "Cannot tie filename: $!" ; |
1116 | ... |
1117 | undef $db ; |
1118 | untie %hash ; |
1119 | |
1120 | All the functions defined in L<dbopen> are available except for |
1121 | close() and dbopen() itself. The B<DB_File> method interface to the |
1122 | supported functions have been implemented to mirror the way Berkeley DB |
1123 | works whenever possible. In particular note that: |
1124 | |
1125 | =over 5 |
1126 | |
1127 | =item * |
1128 | |
1129 | The methods return a status value. All return 0 on success. |
1130 | All return -1 to signify an error and set C<$!> to the exact |
1131 | error code. The return code 1 generally (but not always) means that the |
1132 | key specified did not exist in the database. |
1133 | |
1134 | Other return codes are defined. See below and in the Berkeley DB |
1135 | documentation for details. The Berkeley DB documentation should be used |
1136 | as the definitive source. |
1137 | |
1138 | =item * |
3b35bae3 |
1139 | |
88108326 |
1140 | Whenever a Berkeley DB function returns data via one of its parameters, |
1141 | the equivalent B<DB_File> method does exactly the same. |
3b35bae3 |
1142 | |
88108326 |
1143 | =item * |
1144 | |
1145 | If you are careful, it is possible to mix API calls with the tied |
1146 | hash/array interface in the same piece of code. Although only a few of |
1147 | the methods used to implement the tied interface currently make use of |
1148 | the cursor, you should always assume that the cursor has been changed |
1149 | any time the tied hash/array interface is used. As an example, this |
1150 | code will probably not do what you expect: |
1151 | |
1152 | $X = tie %x, 'DB_File', $filename, O_RDWR|O_CREAT, 0777, $DB_BTREE |
1153 | or die "Cannot tie $filename: $!" ; |
1154 | |
1155 | # Get the first key/value pair and set the cursor |
1156 | $X->seq($key, $value, R_FIRST) ; |
1157 | |
1158 | # this line will modify the cursor |
1159 | $count = scalar keys %x ; |
1160 | |
1161 | # Get the second key/value pair. |
1162 | # oops, it didn't, it got the last key/value pair! |
1163 | $X->seq($key, $value, R_NEXT) ; |
1164 | |
1165 | The code above can be rearranged to get around the problem, like this: |
1166 | |
1167 | $X = tie %x, 'DB_File', $filename, O_RDWR|O_CREAT, 0777, $DB_BTREE |
1168 | or die "Cannot tie $filename: $!" ; |
1169 | |
1170 | # this line will modify the cursor |
1171 | $count = scalar keys %x ; |
1172 | |
1173 | # Get the first key/value pair and set the cursor |
1174 | $X->seq($key, $value, R_FIRST) ; |
1175 | |
1176 | # Get the second key/value pair. |
1177 | # worked this time. |
1178 | $X->seq($key, $value, R_NEXT) ; |
1179 | |
1180 | =back |
1181 | |
1182 | All the constants defined in L<dbopen> for use in the flags parameters |
1183 | in the methods defined below are also available. Refer to the Berkeley |
1184 | DB documentation for the precise meaning of the flags values. |
1185 | |
1186 | Below is a list of the methods available. |
3b35bae3 |
1187 | |
1188 | =over 5 |
1189 | |
f6b705ef |
1190 | =item B<$status = $X-E<gt>get($key, $value [, $flags]) ;> |
88108326 |
1191 | |
1192 | Given a key (C<$key>) this method reads the value associated with it |
1193 | from the database. The value read from the database is returned in the |
1194 | C<$value> parameter. |
3b35bae3 |
1195 | |
88108326 |
1196 | If the key does not exist the method returns 1. |
3b35bae3 |
1197 | |
88108326 |
1198 | No flags are currently defined for this method. |
3b35bae3 |
1199 | |
f6b705ef |
1200 | =item B<$status = $X-E<gt>put($key, $value [, $flags]) ;> |
3b35bae3 |
1201 | |
88108326 |
1202 | Stores the key/value pair in the database. |
1203 | |
1204 | If you use either the R_IAFTER or R_IBEFORE flags, the C<$key> parameter |
8e07c86e |
1205 | will have the record number of the inserted key/value pair set. |
3b35bae3 |
1206 | |
88108326 |
1207 | Valid flags are R_CURSOR, R_IAFTER, R_IBEFORE, R_NOOVERWRITE and |
1208 | R_SETCURSOR. |
1209 | |
f6b705ef |
1210 | =item B<$status = $X-E<gt>del($key [, $flags]) ;> |
3b35bae3 |
1211 | |
88108326 |
1212 | Removes all key/value pairs with key C<$key> from the database. |
3b35bae3 |
1213 | |
88108326 |
1214 | A return code of 1 means that the requested key was not in the |
1215 | database. |
3b35bae3 |
1216 | |
88108326 |
1217 | R_CURSOR is the only valid flag at present. |
3b35bae3 |
1218 | |
f6b705ef |
1219 | =item B<$status = $X-E<gt>fd ;> |
3b35bae3 |
1220 | |
88108326 |
1221 | Returns the file descriptor for the underlying database. |
3b35bae3 |
1222 | |
f6b705ef |
1223 | See L<Locking Databases> for an example of how to make use of the |
88108326 |
1224 | C<fd> method to lock your database. |
3b35bae3 |
1225 | |
f6b705ef |
1226 | =item B<$status = $X-E<gt>seq($key, $value, $flags) ;> |
3b35bae3 |
1227 | |
88108326 |
1228 | This interface allows sequential retrieval from the database. See |
1229 | L<dbopen> for full details. |
1230 | |
1231 | Both the C<$key> and C<$value> parameters will be set to the key/value |
1232 | pair read from the database. |
1233 | |
1234 | The flags parameter is mandatory. The valid flag values are R_CURSOR, |
1235 | R_FIRST, R_LAST, R_NEXT and R_PREV. |
1236 | |
f6b705ef |
1237 | =item B<$status = $X-E<gt>sync([$flags]) ;> |
88108326 |
1238 | |
1239 | Flushes any cached buffers to disk. |
1240 | |
1241 | R_RECNOSYNC is the only valid flag at present. |
3b35bae3 |
1242 | |
1243 | =back |
1244 | |
f6b705ef |
1245 | =head1 HINTS AND TIPS |
3b35bae3 |
1246 | |
3b35bae3 |
1247 | |
cb1a09d0 |
1248 | =head2 Locking Databases |
3b35bae3 |
1249 | |
cb1a09d0 |
1250 | Concurrent access of a read-write database by several parties requires |
1251 | them all to use some kind of locking. Here's an example of Tom's that |
1252 | uses the I<fd> method to get the file descriptor, and then a careful |
1253 | open() to give something Perl will flock() for you. Run this repeatedly |
1254 | in the background to watch the locks granted in proper order. |
3b35bae3 |
1255 | |
cb1a09d0 |
1256 | use DB_File; |
1257 | |
1258 | use strict; |
1259 | |
1260 | sub LOCK_SH { 1 } |
1261 | sub LOCK_EX { 2 } |
1262 | sub LOCK_NB { 4 } |
1263 | sub LOCK_UN { 8 } |
1264 | |
1265 | my($oldval, $fd, $db, %db, $value, $key); |
1266 | |
1267 | $key = shift || 'default'; |
1268 | $value = shift || 'magic'; |
1269 | |
1270 | $value .= " $$"; |
1271 | |
1272 | $db = tie(%db, 'DB_File', '/tmp/foo.db', O_CREAT|O_RDWR, 0644) |
1273 | || die "dbcreat /tmp/foo.db $!"; |
1274 | $fd = $db->fd; |
1275 | print "$$: db fd is $fd\n"; |
1276 | open(DB_FH, "+<&=$fd") || die "dup $!"; |
1277 | |
1278 | |
1279 | unless (flock (DB_FH, LOCK_SH | LOCK_NB)) { |
1280 | print "$$: CONTENTION; can't read during write update! |
1281 | Waiting for read lock ($!) ...."; |
1282 | unless (flock (DB_FH, LOCK_SH)) { die "flock: $!" } |
1283 | } |
1284 | print "$$: Read lock granted\n"; |
1285 | |
1286 | $oldval = $db{$key}; |
1287 | print "$$: Old value was $oldval\n"; |
1288 | flock(DB_FH, LOCK_UN); |
1289 | |
1290 | unless (flock (DB_FH, LOCK_EX | LOCK_NB)) { |
1291 | print "$$: CONTENTION; must have exclusive lock! |
1292 | Waiting for write lock ($!) ...."; |
1293 | unless (flock (DB_FH, LOCK_EX)) { die "flock: $!" } |
1294 | } |
1295 | |
1296 | print "$$: Write lock granted\n"; |
1297 | $db{$key} = $value; |
610ab055 |
1298 | $db->sync; # to flush |
cb1a09d0 |
1299 | sleep 10; |
1300 | |
1301 | flock(DB_FH, LOCK_UN); |
88108326 |
1302 | undef $db; |
cb1a09d0 |
1303 | untie %db; |
1304 | close(DB_FH); |
1305 | print "$$: Updated db to $key=$value\n"; |
1306 | |
f6b705ef |
1307 | =head2 Sharing databases with C applications |
1308 | |
1309 | There is no technical reason why a Berkeley DB database cannot be |
1310 | shared by both a Perl and a C application. |
1311 | |
1312 | The vast majority of problems that are reported in this area boil down |
1313 | to the fact that C strings are NULL terminated, whilst Perl strings are |
1314 | not. |
1315 | |
1316 | Here is a real example. Netscape 2.0 keeps a record of the locations you |
1317 | visit along with the time you last visited them in a DB_HASH database. |
1318 | This is usually stored in the file F<~/.netscape/history.db>. The key |
1319 | field in the database is the location string and the value field is the |
1320 | time the location was last visited stored as a 4 byte binary value. |
1321 | |
1322 | If you haven't already guessed, the location string is stored with a |
1323 | terminating NULL. This means you need to be careful when accessing the |
1324 | database. |
1325 | |
1326 | Here is a snippet of code that is loosely based on Tom Christiansen's |
1327 | I<ggh> script (available from your nearest CPAN archive in |
1328 | F<authors/id/TOMC/scripts/nshist.gz>). |
1329 | |
610ab055 |
1330 | use strict ; |
f6b705ef |
1331 | use DB_File ; |
1332 | use Fcntl ; |
f6b705ef |
1333 | |
610ab055 |
1334 | use vars qw( $dotdir $HISTORY %hist_db $href $binary_time $date ) ; |
f6b705ef |
1335 | $dotdir = $ENV{HOME} || $ENV{LOGNAME}; |
1336 | |
1337 | $HISTORY = "$dotdir/.netscape/history.db"; |
1338 | |
1339 | tie %hist_db, 'DB_File', $HISTORY |
1340 | or die "Cannot open $HISTORY: $!\n" ;; |
1341 | |
1342 | # Dump the complete database |
1343 | while ( ($href, $binary_time) = each %hist_db ) { |
1344 | |
1345 | # remove the terminating NULL |
1346 | $href =~ s/\x00$// ; |
1347 | |
1348 | # convert the binary time into a user friendly string |
1349 | $date = localtime unpack("V", $binary_time); |
1350 | print "$date $href\n" ; |
1351 | } |
1352 | |
1353 | # check for the existence of a specific key |
1354 | # remember to add the NULL |
1355 | if ( $binary_time = $hist_db{"http://mox.perl.com/\x00"} ) { |
1356 | $date = localtime unpack("V", $binary_time) ; |
1357 | print "Last visited mox.perl.com on $date\n" ; |
1358 | } |
1359 | else { |
1360 | print "Never visited mox.perl.com\n" |
1361 | } |
1362 | |
1363 | untie %hist_db ; |
1364 | |
1365 | |
1366 | =head1 COMMON QUESTIONS |
1367 | |
1368 | =head2 Why is there Perl source in my database? |
1369 | |
1370 | If you look at the contents of a database file created by DB_File, |
1371 | there can sometimes be part of a Perl script included in it. |
1372 | |
1373 | This happens because Berkeley DB uses dynamic memory to allocate |
1374 | buffers which will subsequently be written to the database file. Being |
1375 | dynamic, the memory could have been used for anything before DB |
1376 | malloced it. As Berkeley DB doesn't clear the memory once it has been |
1377 | allocated, the unused portions will contain random junk. In the case |
1378 | where a Perl script gets written to the database, the random junk will |
1379 | correspond to an area of dynamic memory that happened to be used during |
1380 | the compilation of the script. |
1381 | |
1382 | Unless you don't like the possibility of there being part of your Perl |
1383 | scripts embedded in a database file, this is nothing to worry about. |
1384 | |
1385 | =head2 How do I store complex data structures with DB_File? |
1386 | |
1387 | Although B<DB_File> cannot do this directly, there is a module which |
1388 | can layer transparently over B<DB_File> to accomplish this feat. |
1389 | |
1390 | Check out the MLDBM module, available on CPAN in the directory |
1391 | F<modules/by-module/MLDBM>. |
1392 | |
1393 | =head2 What does "Invalid Argument" mean? |
1394 | |
1395 | You will get this error message when one of the parameters in the |
1396 | C<tie> call is wrong. Unfortunately there are quite a few parameters to |
1397 | get wrong, so it can be difficult to figure out which one it is. |
1398 | |
1399 | Here are a couple of possibilities: |
1400 | |
1401 | =over 5 |
1402 | |
1403 | =item 1. |
1404 | |
610ab055 |
1405 | Attempting to reopen a database without closing it. |
f6b705ef |
1406 | |
1407 | =item 2. |
1408 | |
1409 | Using the O_WRONLY flag. |
1410 | |
1411 | =back |
1412 | |
1413 | =head2 What does "Bareword 'DB_File' not allowed" mean? |
1414 | |
1415 | You will encounter this particular error message when you have the |
1416 | C<strict 'subs'> pragma (or the full strict pragma) in your script. |
1417 | Consider this script: |
1418 | |
1419 | use strict ; |
1420 | use DB_File ; |
1421 | use vars qw(%x) ; |
1422 | tie %x, DB_File, "filename" ; |
1423 | |
1424 | Running it produces the error in question: |
1425 | |
1426 | Bareword "DB_File" not allowed while "strict subs" in use |
1427 | |
1428 | To get around the error, place the word C<DB_File> in either single or |
1429 | double quotes, like this: |
1430 | |
1431 | tie %x, "DB_File", "filename" ; |
1432 | |
1433 | Although it might seem like a real pain, it is really worth the effort |
1434 | of having a C<use strict> in all your scripts. |
1435 | |
cb1a09d0 |
1436 | =head1 HISTORY |
1437 | |
1438 | =over |
1439 | |
1440 | =item 0.1 |
3b35bae3 |
1441 | |
1442 | First Release. |
1443 | |
cb1a09d0 |
1444 | =item 0.2 |
3b35bae3 |
1445 | |
1446 | When B<DB_File> is opening a database file it no longer terminates the |
1447 | process if I<dbopen> returned an error. This allows file protection |
1448 | errors to be caught at run time. Thanks to Judith Grass |
cb1a09d0 |
1449 | E<lt>grass@cybercash.comE<gt> for spotting the bug. |
3b35bae3 |
1450 | |
cb1a09d0 |
1451 | =item 0.3 |
8e07c86e |
1452 | |
1453 | Added prototype support for multiple btree compare callbacks. |
1454 | |
cb1a09d0 |
1455 | =item 1.0 |
8e07c86e |
1456 | |
1457 | B<DB_File> has been in use for over a year. To reflect that, the |
1458 | version number has been incremented to 1.0. |
1459 | |
1460 | Added complete support for multiple concurrent callbacks. |
1461 | |
1462 | Using the I<push> method on an empty list didn't work properly. This |
1463 | has been fixed. |
1464 | |
cb1a09d0 |
1465 | =item 1.01 |
4633a7c4 |
1466 | |
1467 | Fixed a core dump problem with SunOS. |
1468 | |
1469 | The return value from TIEHASH wasn't set to NULL when dbopen returned |
1470 | an error. |
1471 | |
88108326 |
1472 | =item 1.02 |
1473 | |
f6b705ef |
1474 | Merged OS/2 specific code into DB_File.xs |
88108326 |
1475 | |
1476 | Removed some redundant code in DB_File.xs. |
1477 | |
1478 | Documentation update. |
1479 | |
1480 | Allow negative subscripts with RECNO interface. |
1481 | |
1482 | Changed the default flags from O_RDWR to O_CREAT|O_RDWR. |
1483 | |
1484 | The example code which showed how to lock a database needed a call to |
1485 | C<sync> added. Without it the resultant database file was empty. |
1486 | |
f6b705ef |
1487 | Added get_dup method. |
88108326 |
1488 | |
f6b705ef |
1489 | =item 1.03 |
1490 | |
1491 | Documentation update. |
3b35bae3 |
1492 | |
f6b705ef |
1493 | B<DB_File> now imports the constants (O_RDWR, O_CREAT etc.) from Fcntl |
1494 | automatically. |
3b35bae3 |
1495 | |
f6b705ef |
1496 | The standard hash function C<exists> is now supported. |
1497 | |
1498 | Modified the behavior of get_dup. When it returns an associative |
1499 | array, the value is the count of the number of matching BTREE values. |
3b35bae3 |
1500 | |
610ab055 |
1501 | =item 1.04 |
1502 | |
1503 | Minor documentation changes. |
1504 | |
1505 | Fixed a bug in hash_cb. Patches supplied by Dave Hammen, |
1506 | E<lt>hammen@gothamcity.jsc.nasa.govE<gt>. |
1507 | |
1508 | Fixed a bug with the constructors for DB_File::HASHINFO, |
1509 | DB_File::BTREEINFO and DB_File::RECNOINFO. Also tidied up the |
1510 | constructors to make them C<-w> clean. |
1511 | |
1512 | Reworked part of the test harness to be more locale friendly. |
1513 | |
1514 | =item 1.05 |
1515 | |
1516 | Made all scripts in the documentation C<strict> and C<-w> clean. |
1517 | |
1518 | Added logic to F<DB_File.xs> to allow the module to be built after Perl |
1519 | is installed. |
1520 | |
1521 | =back |
1522 | |
3b35bae3 |
1523 | =head1 BUGS |
1524 | |
8e07c86e |
1525 | Some older versions of Berkeley DB had problems with fixed length |
1526 | records using the RECNO file format. The newest version at the time of |
1527 | writing was 1.85 - this seems to have fixed the problems with RECNO. |
3b35bae3 |
1528 | |
8e07c86e |
1529 | I am sure there are bugs in the code. If you do find any, or can |
1530 | suggest any enhancements, I would welcome your comments. |
3b35bae3 |
1531 | |
1532 | =head1 AVAILABILITY |
1533 | |
f6b705ef |
1534 | B<DB_File> comes with the standard Perl source distribution. Look in |
1535 | the directory F<ext/DB_File>. |
1536 | |
cb1a09d0 |
1537 | Berkeley DB is available at your nearest CPAN archive (see |
1538 | L<perlmod/"CPAN"> for a list) in F<src/misc/db.1.85.tar.gz>, or via the |
610ab055 |
1539 | host F<ftp.cs.berkeley.edu> in F</ucb/4bsd/db.tar.gz>. Alternatively, |
1540 | check out the Berkeley DB home page at F<http://www.bostic.com/db>. It |
1541 | is I<not> under the GPL. |
3b35bae3 |
1542 | |
88108326 |
1543 | If you are running IRIX, then get Berkeley DB from |
1544 | F<http://reality.sgi.com/ariel>. It has the patches necessary to |
1545 | compile properly on IRIX 5.3. |
1546 | |
3b35bae3 |
1547 | =head1 SEE ALSO |
1548 | |
1549 | L<perl(1)>, L<dbopen(3)>, L<hash(3)>, L<recno(3)>, L<btree(3)> |
1550 | |
3b35bae3 |
1551 | =head1 AUTHOR |
1552 | |
8e07c86e |
1553 | The DB_File interface was written by Paul Marquess |
88108326 |
1554 | E<lt>pmarquess@bfsec.bt.co.ukE<gt>. |
8e07c86e |
1555 | Questions about the DB system itself may be addressed to Keith Bostic |
88108326 |
1556 | E<lt>bostic@cs.berkeley.eduE<gt>. |
3b35bae3 |
1557 | |
1558 | =cut |