2
<!--Copyright 1997-2002 by Sleepycat Software, Inc.-->
3
<!--All rights reserved.-->
4
<!--See the file LICENSE for redistribution information.-->
7
<title>Berkeley DB: hsearch</title>
8
<meta name="description" content="Berkeley DB: An embedded database programmatic toolkit.">
9
<meta name="keywords" content="embedded,database,programmatic,toolkit,b+tree,btree,hash,hashing,transaction,transactions,locking,logging,access method,access methods,java,C,C++">
12
<a name="2"><!--meow--></a>
13
<table width="100%"><tr valign=top>
18
<a href="../api_c/c_index.html"><img src="../images/api.gif" alt="API"></a><a href="../reftoc.html"><img src="../images/ref.gif" alt="Ref"></a>
23
#define DB_DBM_HSEARCH 1
30
typedef struct entry {
36
hsearch(ENTRY item, ACTION action);
39
hcreate(size_t nelem);
45
<p>The hsearch interface to the Berkeley DB library is intended to
46
provide a high-performance implementation and source code compatibility
47
for applications written to the historic hsearch interface.
48
It is not recommended for any other purpose.
49
<p>To compile hsearch applications, replace the application's
50
<b>#include</b> of the hsearch include
51
file (for example, <b>#include <search.h></b>)
52
with the following two lines:
53
<p><blockquote><pre>#define DB_DBM_HSEARCH 1
54
#include <db.h></pre></blockquote>
56
<p>The hcreate function creates an in-memory database. The
57
<b>nelem</b> argument is an estimation of the maximum number of key/data
58
pairs that will be stored in the database.
59
<p>The <b>hdestroy</b> function discards the database.
60
<p>Database elements are structures of type <b>ENTRY</b>, which contain at
61
least two fields: <b>key</b> and <b>data</b>. The field <b>key</b> is
62
declared to be of type <b>char *</b>, and is the key used for storage
63
and retrieval. The field <b>data</b> is declared to be of type
64
<b>void *</b>, and is its associated data.
65
<p>The hsearch function retrieves key/data pairs from, and stores
66
key/data pairs into the database.
67
<p>The <b>action</b> argument must be set to one of two values:
69
<p><dt>ENTER<dd>If the key does not already appear in the database, insert the key/data
70
pair into the database. If the key already appears in the database,
71
return a reference to an <b>ENTRY</b> structure which refers to the
72
existing key and its associated data element.
73
<p><dt>FIND<dd>Retrieve the specified key/data pair from the database.
75
<h3>Compatibility Notes</h3>
76
<p>Historically, hsearch required applications to maintain the keys
77
and data in the application's memory for as long as the <b>hsearch</b>
78
database existed. Because Berkeley DB handles key and data management
79
internally, there is no requirement that applications maintain local
80
copies of key and data items, although the only effect of doing so
81
should be the allocation of additional memory.
82
<h3>Hsearch Diagnostics</h3>
83
<p>The <b>hcreate</b> function returns 0 on failure, setting
84
<b>errno</b>, and non-zero on success.
85
<p>The <b>hsearch</b> function returns a pointer to an ENTRY structure on
86
success, and NULL, setting <b>errno</b>, if the <b>action</b>
87
specified was FIND and the item did not appear in the database.
89
<p>The hcreate method may fail and return a non-zero error for errors specified for other Berkeley DB and C library or system functions.
90
If a catastrophic error has occurred, the hcreate method may fail and
91
return <a href="../ref/program/errorret.html#DB_RUNRECOVERY">DB_RUNRECOVERY</a>,
92
in which case all subsequent Berkeley DB calls will fail in the same way.
93
<p>The hsearch method may fail and return a non-zero error for errors specified for other Berkeley DB and C library or system functions.
94
If a catastrophic error has occurred, the hsearch method may fail and
95
return <a href="../ref/program/errorret.html#DB_RUNRECOVERY">DB_RUNRECOVERY</a>,
96
in which case all subsequent Berkeley DB calls will fail in the same way.
97
<p>In addition, the <b>hsearch</b> function will fail, setting <b>errno</b>
98
to 0, if the <b>action</b> specified was FIND and the item did not appear in
101
<table width="100%"><tr><td><br></td><td align=right>
102
<a href="../api_c/c_index.html"><img src="../images/api.gif" alt="API"></a><a href="../reftoc.html"><img src="../images/ref.gif" alt="Ref"></a>
104
<p><font size=1><a href="http://www.sleepycat.com">Copyright Sleepycat Software</a></font>