| 1 | <?xml version="1.0" encoding="UTF-8" standalone="no"?> |
| 2 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
| 3 | <html xmlns="http://www.w3.org/1999/xhtml"> |
| 4 | <head> |
| 5 | <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> |
| 6 | <title>hsearch</title> |
| 7 | <link rel="stylesheet" href="apiReference.css" type="text/css" /> |
| 8 | <meta name="generator" content="DocBook XSL Stylesheets V1.73.2" /> |
| 9 | <link rel="start" href="index.html" title="Berkeley DB C API Reference" /> |
| 10 | <link rel="up" href="historic.html" title="Appendix B. Historic Interfaces" /> |
| 11 | <link rel="prev" href="dbm.html" title="dbm/ndbm" /> |
| 12 | <link rel="next" href="setfunc.html" title="Appendix C. Berkeley DB Application Space Static Functions" /> |
| 13 | </head> |
| 14 | <body> |
| 15 | <div xmlns="" class="navheader"> |
| 16 | <div class="libver"> |
| 17 | <p>Library Version 11.2.5.3</p> |
| 18 | </div> |
| 19 | <table width="100%" summary="Navigation header"> |
| 20 | <tr> |
| 21 | <th colspan="3" align="center">hsearch</th> |
| 22 | </tr> |
| 23 | <tr> |
| 24 | <td width="20%" align="left"><a accesskey="p" href="dbm.html">Prev</a> </td> |
| 25 | <th width="60%" align="center">Appendix B. |
| 26 | Historic Interfaces |
| 27 | </th> |
| 28 | <td width="20%" align="right"> <a accesskey="n" href="setfunc.html">Next</a></td> |
| 29 | </tr> |
| 30 | </table> |
| 31 | <hr /> |
| 32 | </div> |
| 33 | <div class="sect1" lang="en" xml:lang="en"> |
| 34 | <div class="titlepage"> |
| 35 | <div> |
| 36 | <div> |
| 37 | <h2 class="title" style="clear: both"><a id="hsearch"></a>hsearch</h2> |
| 38 | </div> |
| 39 | </div> |
| 40 | </div> |
| 41 | <pre class="programlisting">#define DB_DBM_HSEARCH 1 |
| 42 | #include <db.h> |
| 43 | |
| 44 | typedef enum { |
| 45 | FIND, ENTER |
| 46 | } ACTION; |
| 47 | |
| 48 | typedef struct entry { |
| 49 | char *key; |
| 50 | void *data; |
| 51 | } ENTRY; |
| 52 | |
| 53 | ENTRY * |
| 54 | hsearch(ENTRY item, ACTION action); |
| 55 | |
| 56 | int |
| 57 | hcreate(size_t nelem); |
| 58 | |
| 59 | void |
| 60 | hdestroy(void); </pre> |
| 61 | <p> |
| 62 | The hsearch functions are intended to provide a high-performance |
| 63 | implementation and source code compatibility for applications written |
| 64 | to the historic hsearch interface. It is not recommended for any |
| 65 | other purpose. |
| 66 | </p> |
| 67 | <p> |
| 68 | To compile hsearch applications, replace the application's <span class="bold"><strong>#include</strong></span> of the hsearch include file (for |
| 69 | example, <span class="bold"><strong>#include <search.h></strong></span>) |
| 70 | with the following two lines: |
| 71 | </p> |
| 72 | <pre class="programlisting">#define DB_DBM_HSEARCH 1 |
| 73 | #include <db.h></pre> |
| 74 | <p> |
| 75 | and recompile. |
| 76 | </p> |
| 77 | <p> |
| 78 | The hcreate function creates an in-memory database. The <span class="bold"><strong>nelem</strong></span> parameter is an estimation of the maximum |
| 79 | number of key/data pairs that will be stored in the database. |
| 80 | </p> |
| 81 | <p> |
| 82 | The <span class="bold"><strong>hdestroy</strong></span> function discards the |
| 83 | database. |
| 84 | </p> |
| 85 | <p> |
| 86 | Database elements are structures of type <span class="bold"><strong>ENTRY</strong></span>, which contain at least two fields: |
| 87 | <span class="bold"><strong>key</strong></span> and <span class="bold"><strong>data</strong></span>. The field <span class="bold"><strong>key</strong></span> is declared to be of type <span class="bold"><strong>char *</strong></span>, and is the key used for storage and |
| 88 | retrieval. The field <span class="bold"><strong>data</strong></span> is |
| 89 | declared to be of type <span class="bold"><strong>void *</strong></span>, and is |
| 90 | its associated data. |
| 91 | </p> |
| 92 | <p> |
| 93 | The hsearch function retrieves key/data pairs from, and stores |
| 94 | key/data pairs into the database. |
| 95 | </p> |
| 96 | <p> |
| 97 | The <span class="bold"><strong>action</strong></span> parameter must be set to |
| 98 | one of two values: |
| 99 | </p> |
| 100 | <div class="itemizedlist"> |
| 101 | <ul type="disc"> |
| 102 | <li> |
| 103 | <p> |
| 104 | <span class="bold"><strong>ENTER</strong></span> |
| 105 | </p> |
| 106 | <p> |
| 107 | If the key does not already appear in the database, insert the |
| 108 | key/data pair into the database. If the key already appears in the |
| 109 | database, return a reference to an <span class="bold"><strong>ENTRY</strong></span> structure which refers to the existing |
| 110 | key and its associated data element. |
| 111 | </p> |
| 112 | </li> |
| 113 | <li> |
| 114 | <p> |
| 115 | <span class="bold"><strong>FIND</strong></span> |
| 116 | </p> |
| 117 | <p> |
| 118 | Retrieve the specified key/data pair from the database. |
| 119 | </p> |
| 120 | </li> |
| 121 | </ul> |
| 122 | </div> |
| 123 | <div class="sect2" lang="en" xml:lang="en"> |
| 124 | <div class="titlepage"> |
| 125 | <div> |
| 126 | <div> |
| 127 | <h3 class="title"><a id="hsearch_compatnotes"></a>Compatibility Notes</h3> |
| 128 | </div> |
| 129 | </div> |
| 130 | </div> |
| 131 | <p> |
| 132 | Historically, hsearch required applications to maintain the keys and |
| 133 | data in the application's memory for as long as the <span class="bold"><strong>hsearch</strong></span> database existed. Because Berkeley DB |
| 134 | handles key and data management internally, there is no requirement |
| 135 | that applications maintain local copies of key and data items, |
| 136 | although the only effect of doing so should be the allocation of |
| 137 | additional memory. |
| 138 | </p> |
| 139 | </div> |
| 140 | <div class="sect2" lang="en" xml:lang="en"> |
| 141 | <div class="titlepage"> |
| 142 | <div> |
| 143 | <div> |
| 144 | <h3 class="title"><a id="hsearch_diagnostics"></a>Hsearch Diagnostics</h3> |
| 145 | </div> |
| 146 | </div> |
| 147 | </div> |
| 148 | <p> |
| 149 | The <span class="bold"><strong>hcreate</strong></span> function returns 0 on |
| 150 | failure, setting <span class="bold"><strong>errno</strong></span>, and non-zero |
| 151 | on success. |
| 152 | </p> |
| 153 | <p> |
| 154 | The <span class="bold"><strong>hsearch</strong></span> function returns a |
| 155 | pointer to an ENTRY structure on success, and NULL, setting <span class="bold"><strong>errno</strong></span>, if the <span class="bold"><strong>action</strong></span> specified was FIND and the item did not |
| 156 | appear in the database. |
| 157 | </p> |
| 158 | </div> |
| 159 | <div class="sect2" lang="en" xml:lang="en"> |
| 160 | <div class="titlepage"> |
| 161 | <div> |
| 162 | <div> |
| 163 | <h3 class="title"><a id="hsearch_errors"></a>Hsearch Errors</h3> |
| 164 | </div> |
| 165 | </div> |
| 166 | </div> |
| 167 | <p> |
| 168 | The <span class="bold"><strong>hsearch</strong></span> function will fail, |
| 169 | setting <span class="bold"><strong>errno</strong></span> to 0, if the <span class="bold"><strong>action</strong></span> specified was FIND and the item did not |
| 170 | appear in the database. |
| 171 | </p> |
| 172 | <p> |
| 173 | In addition, the hcreate, hsearch and hdestroy functions may fail and |
| 174 | return an error for errors specified for other Berkeley DB and C |
| 175 | library or system functions. |
| 176 | </p> |
| 177 | </div> |
| 178 | </div> |
| 179 | <div class="navfooter"> |
| 180 | <hr /> |
| 181 | <table width="100%" summary="Navigation footer"> |
| 182 | <tr> |
| 183 | <td width="40%" align="left"><a accesskey="p" href="dbm.html">Prev</a> </td> |
| 184 | <td width="20%" align="center"> |
| 185 | <a accesskey="u" href="historic.html">Up</a> |
| 186 | </td> |
| 187 | <td width="40%" align="right"> <a accesskey="n" href="setfunc.html">Next</a></td> |
| 188 | </tr> |
| 189 | <tr> |
| 190 | <td width="40%" align="left" valign="top">dbm/ndbm </td> |
| 191 | <td width="20%" align="center"> |
| 192 | <a accesskey="h" href="index.html">Home</a> |
| 193 | </td> |
| 194 | <td width="40%" align="right" valign="top"> Appendix C. |
| 195 | Berkeley DB Application Space Static Functions |
| 196 | </td> |
| 197 | </tr> |
| 198 | </table> |
| 199 | </div> |
| 200 | </body> |
| 201 | </html> |