need types.h
[unix-history] / usr / src / lib / libc / db / man / dbopen.3
CommitLineData
bcd70bc9
KB
1.\" Copyright (c) 1990 The Regents of the University of California.
2.\" All rights reserved.
3.\"
4.\" %sccs.include.redist.man%
5.\"
6.\" @(#)dbopen.3 5.1 (Berkeley) %G%
7.\"
8.TH DB 3 ""
9.UC 7
10.SH NAME
11btree_open, flat_open, hash_open \- database manipulation routines
12.SH SYNOPSIS
13.nf
14.ft B
15#include <db.h>
16
17DB *
18btree_open(const char *file, int flags, int mode, const BTREEINFO * private);
19
20DB *
21flat_open(const char *file, int flags, int mode, const FLATINFO * private);
22
23DB *
24hash_open(const char *file, int flags, int mode, const HASHINFO * private);
25.ft R
26.fi
27.SH DESCRIPTION
28.IR Btree_open ,
29.IR flat_open ,
30and
31.I hash_open
32are interfaces, respectively, to database files in btree, flat,
33and hashed record formats.
34Access to all file types is based on key/data pairs, where both keys
35and data are of essentially unlimited size.
36.PP
37Each routine opens
38.I file
39for reading and/or writing.
40The
41.I flags
42and
43.I mode arguments
44are as specified to the
45.IR open (2)
46routine, however only the O_CREAT, O_EXCL, O_RDONLY, O_RDWR, O_TRUNC
47and O_WRONLY flags are meaningful.
48Databases which are temporary, i.e. not intended to be preserved
49on disk, may be created by setting the file parameter to NULL.
50The argument
51.I private
52is a pointer to a private, access-method specific structure described
53below.
54.PP
55The open routines return a pointer to a structure representing the
56database on success and NULL on error.
57This structure is as follows:
58.sp
59typedef struct {
60.RS
61void *internal;
62.br
63int (*close)(), (*delete)(), (*get)(), (*put)(), (*seq)(), (*sync)();
64.RE
65} DB;
66.sp
67.PP
68This structure is as follows:
69.TP
70internal
71A pointer to an internal structure private to the access method.
72.TP
73close
74A pointer to a routine to flush any cached information to disk, free any
75allocated resources, and close the database file, whose function prototype
76is:
77.sp
78.in +5
79close(const DB *db);
80.in -5
81.sp
82Since key/data pairs may be cached in memory, failing to close the
83file with the
84.I close
85routine may result in inconsistent or lost information.
86The
87.I close
88routine returns 0 on error and 1 on success.
89.TP
90delete
91A pointer to a routine to remove key/data pairs from the database,
92whose function prototype is:
93.sp
94.in +5
95delete(const DB *db, const VALUE *key);
96.in -5
97.sp
98The
99.I delete
100routine returns 0 on error, 1 on success, and -1 if the specified
101.I key
102was not in the file.
103.TP
104get
105A pointer to a routine which is the interface for keyed retrieval from
106the database, whose function prototype is:
107.sp
108.in +5
109get(const DB *db, const VALUE *key, VALUE *data);
110.in -5
111.sp
112The address and length of the data associated with the specified
113.I key
114are returned in the structure referenced by
115.IR data .
116The
117.I get
118routine returns 0 on error, 1 on success, and -1 if the
119.I key
120was not in the file.
121.TP
122put
123A pointer to a routine to store key/data pairs in the database,
124whose function prototype is:
125.sp
126.in +5
127put(const DB *db, const VALUE *key, const VALUE *data, u_long flag);
128.in -5
129.sp
130The parameter flag, if set, should be either R_APPEND or R_INSERT,
131optionally
132.IR or 'ed
133with R_NOOVERWRITE.
134.RS
135.TP
136R_APPEND
137Append the data immediately after the data referenced by
138.IR key ,
139creating a new record.
140(This implies that the access method is able to create new keys itself,
141i.e. the keys are ordered and independent, for example, record numbers.
142Currently applicable only to the flat file access method.)
143.TP
144R_INSERT
145Insert the data immediately before the data referenced by
146.IR key ,
147creating a new record.
148(This implies that the access method is able to create new keys itself,
149i.e. the keys are ordered and independent, for example, record numbers.
150Currently applicable only to the flat file access method.)
151.TP
152R_NOOVERWRITE
153Enter the new key/data pair only if the key does not previously exist.
154.RE
155.PP
156The
157.I put
158routine returns 0 on error, 1 on success, and -1 if the
159R_NOOVERWRITE
160.I flag
161is set and the key already exists in the file.
162.TP
163seq
164A pointer to a routine which is the interface for sequential
165retrieval from the database, whose function prototype is:
166.sp
167.in +5
168seq(const DB *db, VALUE *key, VALUE *data, int flag);
169.in -5
170.sp
171The address and length of the key are returned in the structure
172referenced by
173.IR key ,
174and the address and length of the data are returned in the
175structure referenced
176by
177.IR data .
178.PP
179The flag value, if set, should be one of the following values:
180.RS
181.TP
182R_FIRST
183The first key of the hash table is returned.
184.TP
185R_LAST
186The last key of the hash table is returned.
187.TP
188R_NEXT
189Retrieve the record immediately after the most recently requested
190record.
191.TP
192R_PREV
193Retrieve the record immediately before the most recently requested
194record.
195.RE
196.PP
197The first time the
198.I seq
199routine is called, the first record of the database is returned
200if
201.I flag
202is not set or is set to R_FIRST or R_NEXT.
203.PP
204The
205.I seq
206routine returns 0 on error, 1 on success, -1 if end-of-file is reached,
207and -2 if the input is a character device and no complete records are
208available.
209.TP
210sync
211A pointer to a routine to flush any cached information to disk,
212whose function prototype is:
213.sp
214.in +5
215sync(const DB *db);
216.in -5
217.sp
218If the database is in memory only, the
219.I sync
220routine is a no-op.
221The
222.I sync
223routine returns 0 on error and 1 on success.
224.PP
225Each of the routines take a pointer to a structure as returned by
226the open routine, one or more pointers to key/data structures, and,
227optionally, a flag value.
228.PP
229Keys (and data) are represented by the following data structure:
230.sp
231typedef struct {
232.RS
233u_char *data;
234.br
235size_t size;
236.RE
237} ENTRY;
238.PP
239The elements of this structure are as follows:
240.TP
241data
242A pointer to a byte string.
243.TP
244size
245The length of the byte string.
246.SH BTREE
247One of the access methods is a btree: a sorted, balanced
248tree structure with associated key and data pairs.
249.PP
250<Mike fill this in?>
251.PP
252The private data structure provided to
253.I btree_open
254is as follows:
255.sp
256typedef struct {
257.RS
258u_long flags;
259.br
260int cachesize;
261.br
262int pagesize;
263.RE
264} BTREEINFO;
265.PP
266The elements of this structure are as follows:
267.TP
268flags
269The flag value is specified by
270.IR or 'ing
271the following values:
272.RS
273.TP
274R_SMALLCACHE
275A flag informing the routines that they are not expected to be
276the primary data cache, and to minimize any caching they do.
277.RE
278.TP
279cachesize
280.TP
281pagesize
282.SH HASH
283One of the access methods is hashed access and storage.
284The private data structure provided to
285.I hash_open
286is as follows:
287.sp
288typedef struct {
289.RS
290u_long flags;
291.br
292int bsize;
293.br
294int ffactor;
295.br
296int nelem;
297.br
298u_long (*hash)(const void *, const size_t);
299.RE
300} HASHINFO;
301.PP
302The elements of this structure are as follows:
303.TP
304flags
305The flag value is specified by
306.IR or 'ing
307the following values:
308.RS
309.TP
310R_SMALLCACHE
311A flag informing the routines that they are not expected to be
312the primary cache, and to minimize any caching they do.
313.RE
314.TP
315bsize
316.I Bsize
317defines the hash table bucket size, and is, by default 1024, bytes.
318For tables with large data items, it may be preferable to increase the
319page size, and, conversely, applications doing exclusively in-memory hashing
320may want to use a very small bucket size, for example, 256, to minimize
321hash chain collisions.
322.TP
323ffactor
324.I Ffactor
325indicates a desired density within the hash table.
326It is an approximation of the number of keys allowed to accumulate in any
327one bucket, determining when the hash table grows or shrinks.
328The default value is 5.
329.TP
330hash
331.I Hash
332is a user defined hash function.
333Since no hash function performs equally well on all possible data, the
334user may find that the built-in hash function does poorly on a particular
335data set.
336Any user specified hash function should take two arguments, a pointer to
337a byte string and a length, and return an unsigned long to be used as
338the hash value.
339.TP
340nelem
341.I Nelem
342is an estimate of the final size of the hash table.
343If not set, the default value is 1.
344If not set or set too low, hash tables will expand gracefully as keys
345are entered, although a slight performance degradation may be noticed.
346.PP
347If the hash table already exists, and the O_TRUNC flag is not
348specified to
349.IR hash_open ,
350the parameters
351.IR bsize ,
352.IR ffactor ,
353and
354.I nelem
355are ignored.
356.PP
357If a hash function is specified,
358.I hash_open
359will attempt to determine if the hash function specified is the same as
360the one with which the database was created, and will fail if it is not.
361.PP
362Backward compatible interfaces to the routines described in
363.IR dbm (3),
364.IR hsearch (3),
365and
366.IR ndbm (3)
367are provided as part of the compatibility library, ``libcompat.a''.
368.SH "FLAT FILES"
369One of the access methods is either variable or fixed-length records,
370the former delimited by a specific byte value.
371The private data structure provided to
372.I flat_open
373is as follows:
374.sp
375typedef struct {
376.RS
377u_long flags;
378.br
379int cachesize;
380.br
381size_t reclen;
382.br
383u_char bval;
384.RE
385} VLENINFO;
386.PP
387The elements of this structure are as follows:
388.TP
389flags
390The flag value is specified by
391.IR or 'ing
392the following values:
393.RS
394.TP
395R_FIXEDLEN
396The records are fixed-length, not byte delimited.
397The structure element
398.I reclen
399specifies the length of the record, and the structure element
400.I bval
401is used as the pad character.
402.TP
403R_SMALLCACHE
404A flag informing the routines that they are not expected to be
405the primary cache, and to minimize any caching they do.
406.RE
407.TP
408cachesize
409The amount of memory to be used as a data cache.
410.TP
411reclen
412The length of a fixed-length record.
413.TP
414bval
415The delimiting byte to be used to mark the end of a record for
416variable-length records, and the pad character for fixed-length
417records.
418.PP
419Variable-length and fixed-length data files require
420.I key
421structures to reference a byte followed by three unsigned longs.
422The numbers are used as a record number, a byte offset and a record length,
423respectively, and the byte is a flag value which indicates the validity
424of the other fields.
425These access methods do no validity checking as to the correctness of any
426of these values, nor are they constrained to use the values provided.
427If any of the record number, byte offset or record length are not specified
428by the calling routine, and the record retrieval is successful, the correct
429values are copied into the caller's
430.I key
431structure.
432The flag value is specified by
433.IR or 'ing
434the following values:
435.TP
436R_LENGTH
437The record length is valid.
438.TP
439R_OFFSET
440The byte offset is valid.
441.TP
442R_RECNO
443The record number is valid.
444.SH ERRORS
445The
446.I open
447routines may fail and set errno for any of the errors specified for the
448library routines
449.IR open (2)
450and
451.IR malloc (3)
452or the following:
453.TP
454[EINVAL]
455A parameter has been specified (hash function, pad byte etc.) that is
456incompatible with the current file specification or there is a mismatch
457between the version number of file and the software.
458.PP
459The
460.I get
461routines may fail and set errno for any of the errors specified for the
462library routine
463.IR malloc (3).
464.PP
465The
466.I close
467routines may fail and set errno for any of the errors specified for the
468library routines
469.IR close (2),
470.IR free (3),
471or
472.IR fsync (2).
473.PP
474The
475.I sync
476routines may fail and set errno for any of the errors specified for the
477library routine
478.IR fsync (2).