perl5.001 patch.1f
[p5sagit/p5-mst-13.2.git] / pod / modpods / DB_File.pod
CommitLineData
a0d0e21e 1=head1 NAME
2
3DB_File - Perl5 access to Berkeley DB
4
5=head1 SYNOPSIS
6
7 use DB_File ;
8
9 [$X =] tie %hash, DB_File, $filename [, $flags, $mode, $DB_HASH] ;
10 [$X =] tie %hash, DB_File, $filename, $flags, $mode, $DB_BTREE ;
11 [$X =] tie @array, DB_File, $filename, $flags, $mode, $DB_RECNO ;
12
13 $status = $X->del($key [, $flags]) ;
14 $status = $X->put($key, $value [, $flags]) ;
15 $status = $X->get($key, $value [, $flags]) ;
16 $status = $X->seq($key, $value [, $flags]) ;
17 $status = $X->sync([$flags]) ;
18 $status = $X->fd ;
19
20 untie %hash ;
21 untie @array ;
22
23=head1 DESCRIPTION
24
25B<DB_File> is a module which allows Perl programs to make use of
26the facilities provided by Berkeley DB. If you intend to use this
27module you should really have a copy of the Berkeley DB manual
28page at hand. The interface defined here
29mirrors the Berkeley DB interface closely.
30
31Berkeley DB is a C library which provides a consistent interface to a number of
32database formats.
33B<DB_File> provides an interface to all three of the database types currently
34supported by Berkeley DB.
35
36The file types are:
37
38=over 5
39
40=item DB_HASH
41
42This database type allows arbitrary key/data pairs to be stored in data files.
43This is equivalent to the functionality provided by
44other hashing packages like DBM, NDBM, ODBM, GDBM, and SDBM.
45Remember though, the files created using DB_HASH are
46not compatible with any of the other packages mentioned.
47
48A default hashing algorithm, which will be adequate for most applications,
49is built into Berkeley DB.
50If you do need to use your own hashing algorithm it is possible to write your
51own in Perl and have B<DB_File> use it instead.
52
53=item DB_BTREE
54
55The btree format allows arbitrary key/data pairs to be stored in a sorted,
56balanced binary tree.
57
58As with the DB_HASH format, it is possible to provide a user defined Perl routine
59to perform the comparison of keys. By default, though, the keys are stored
60in lexical order.
61
62=item DB_RECNO
63
64DB_RECNO allows both fixed-length and variable-length flat text files to be
65manipulated using
66the same key/value pair interface as in DB_HASH and DB_BTREE.
67In this case the key will consist of a record (line) number.
68
69=back
70
71=head2 How does DB_File interface to Berkeley DB?
72
73B<DB_File> allows access to Berkeley DB files using the tie() mechanism
74in Perl 5 (for full details, see L<perlfunc/tie()>).
75This facility allows B<DB_File> to access Berkeley DB files using
76either an associative array (for DB_HASH & DB_BTREE file types) or an
77ordinary array (for the DB_RECNO file type).
78
79In addition to the tie() interface, it is also possible to use most of the
80functions provided in the Berkeley DB API.
81
82=head2 Differences with Berkeley DB
83
84Berkeley DB uses the function dbopen() to open or create a
85database. Below is the C prototype for dbopen().
86
87 DB*
88 dbopen (const char * file, int flags, int mode,
89 DBTYPE type, const void * openinfo)
90
91The parameter C<type> is an enumeration which specifies which of the 3
92interface methods (DB_HASH, DB_BTREE or DB_RECNO) is to be used.
93Depending on which of these is actually chosen, the final parameter,
94I<openinfo> points to a data structure which allows tailoring of the
95specific interface method.
96
97This interface is handled
98slightly differently in B<DB_File>. Here is an equivalent call using
99B<DB_File>.
100
101 tie %array, DB_File, $filename, $flags, $mode, $DB_HASH ;
102
103The C<filename>, C<flags> and C<mode> parameters are the direct equivalent
104of their dbopen() counterparts. The final parameter $DB_HASH
105performs the function of both the C<type> and C<openinfo>
106parameters in dbopen().
107
108In the example above $DB_HASH is actually a reference to a hash object.
109B<DB_File> has three of these pre-defined references.
110Apart from $DB_HASH, there is also $DB_BTREE and $DB_RECNO.
111
112The keys allowed in each of these pre-defined references is limited to the names
113used in the equivalent C structure.
114So, for example, the $DB_HASH reference will only allow keys called C<bsize>,
115C<cachesize>, C<ffactor>, C<hash>, C<lorder> and C<nelem>.
116
117To change one of these elements, just assign to it like this
118
119 $DB_HASH{cachesize} = 10000 ;
120
121
122=head2 RECNO
123
124
125In order to make RECNO more compatible with Perl the array offset for all
126RECNO arrays begins at 0 rather than 1 as in Berkeley DB.
127
128
129=head2 In Memory Databases
130
131Berkeley DB allows the creation of in-memory databases by using NULL (that is, a
132C<(char *)0 in C) in
133place of the filename.
134B<DB_File> uses C<undef> instead of NULL to provide this functionality.
135
136
137=head2 Using the Berkeley DB Interface Directly
138
139As well as accessing Berkeley DB using a tied hash or array, it is also
140possible to make direct use of most of the functions defined in the Berkeley DB
141documentation.
142
143
144To do this you need to remember the return value from the tie.
145
146 $db = tie %hash, DB_File, "filename"
147
148Once you have done that, you can access the Berkeley DB API functions directly.
149
150 $db->put($key, $value, R_NOOVERWRITE) ;
151
152All the functions defined in L<dbx(3X)> are available except
153for close() and dbopen() itself.
154The B<DB_File> interface to these functions have been implemented to mirror
155the the way Berkeley DB works. In particular note that all the functions return
156only a status value. Whenever a Berkeley DB function returns data via one of
157its parameters, the B<DB_File> equivalent does exactly the same.
158
159All the constants defined in L<dbopen> are also available.
160
161Below is a list of the functions available.
162
163=over 5
164
165=item get
166
167Same as in C<recno> except that the flags parameter is optional.
168Remember the value
169associated with the key you request is returned in the $value parameter.
170
171=item put
172
173As usual the flags parameter is optional.
174
175If you use either the R_IAFTER or
176R_IBEFORE flags, the key parameter will have the record number of the inserted
177key/value pair set.
178
179=item del
180
181The flags parameter is optional.
182
183=item fd
184
185As in I<recno>.
186
187=item seq
188
189The flags parameter is optional.
190
191Both the key and value parameters will be set.
192
193=item sync
194
195The flags parameter is optional.
196
197=back
198
199=head1 EXAMPLES
200
201It is always a lot easier to understand something when you see a real example.
202So here are a few.
203
204=head2 Using HASH
205
206 use DB_File ;
207 use Fcntl ;
208
209 tie %h, DB_File, "hashed", O_RDWR|O_CREAT, 0640, $DB_HASH ;
210
211 # Add a key/value pair to the file
212 $h{"apple"} = "orange" ;
213
214 # Check for existence of a key
215 print "Exists\n" if $h{"banana"} ;
216
217 # Delete
218 delete $h{"apple"} ;
219
220 untie %h ;
221
222=head2 Using BTREE
223
224Here is sample of code which used BTREE. Just to make life more interesting
225the default comparision function will not be used. Instead a Perl sub, C<Compare()>,
226will be used to do a case insensitive comparison.
227
228 use DB_File ;
229 use Fcntl ;
230
231 sub Compare
232 {
233 my ($key1, $key2) = @_ ;
234
235 "\L$key1" cmp "\L$key2" ;
236 }
237
238 $DB_BTREE->{compare} = 'Compare' ;
239
240 tie %h, DB_File, "tree", O_RDWR|O_CREAT, 0640, $DB_BTREE ;
241
242 # Add a key/value pair to the file
243 $h{'Wall'} = 'Larry' ;
244 $h{'Smith'} = 'John' ;
245 $h{'mouse'} = 'mickey' ;
246 $h{'duck'} = 'donald' ;
247
248 # Delete
249 delete $h{"duck"} ;
250
251 # Cycle through the keys printing them in order.
252 # Note it is not necessary to sort the keys as
253 # the btree will have kept them in order automatically.
254 foreach (keys %h)
255 { print "$_\n" }
256
257 untie %h ;
258
259Here is the output from the code above.
260
261 mouse
262 Smith
263 Wall
264
265
266=head2 Using RECNO
267
268 use DB_File ;
269 use Fcntl ;
270
271 $DB_RECNO->{psize} = 3000 ;
272
273 tie @h, DB_File, "text", O_RDWR|O_CREAT, 0640, $DB_RECNO ;
274
275 # Add a key/value pair to the file
276 $h[0] = "orange" ;
277
278 # Check for existence of a key
279 print "Exists\n" if $h[1] ;
280
281 untie @h ;
282
283
284
285=head1 WARNINGS
286
287If you happen find any other functions defined in the source for this module
288that have not been mentioned in this document -- beware.
289I may drop them at a moments notice.
290
291If you cannot find any, then either you didn't look very hard or the moment has
292passed and I have dropped them.
293
294=head1 BUGS
295
296Some older versions of Berkeley DB had problems with fixed length records
297using the RECNO file format. The newest version at the time of writing
298was 1.85 - this seems to have fixed the problems with RECNO.
299
300I am sure there are bugs in the code. If you do find any, or can suggest any
301enhancements, I would welcome your comments.
302
303=head1 AVAILABILITY
304
305Berkeley DB is available via the hold C<ftp.cs.berkeley.edu> in the
306directory C</ucb/4bsd/db.tar.gz>. It is I<not> under the GPL.
307
308=head1 SEE ALSO
309
310L<perl(1)>, L<dbopen(3)>, L<hash(3)>, L<recno(3)>, L<btree(3)>
311
312Berkeley DB is available from F<ftp.cs.berkeley.edu> in the directory F</ucb/4bsd>.
313
314=head1 AUTHOR
315
316The DB_File interface was written by
317Paul Marquess <pmarquess@bfsec.bt.co.uk>.
318Questions about the DB system itself may be addressed to
319Keith Bostic <bostic@cs.berkeley.edu>.