| 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>Chapter 8. Berkeley DB Architecture</title> |
| 7 | <link rel="stylesheet" href="gettingStarted.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 Programmer's Reference Guide" /> |
| 10 | <link rel="up" href="index.html" title="Berkeley DB Programmer's Reference Guide" /> |
| 11 | <link rel="prev" href="stl_known_issues.html" title="Dbstl known issues" /> |
| 12 | <link rel="next" href="arch_progmodel.html" title="Programming model" /> |
| 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">Chapter 8. |
| 22 | Berkeley DB Architecture |
| 23 | </th> |
| 24 | </tr> |
| 25 | <tr> |
| 26 | <td width="20%" align="left"><a accesskey="p" href="stl_known_issues.html">Prev</a> </td> |
| 27 | <th width="60%" align="center"> </th> |
| 28 | <td width="20%" align="right"> <a accesskey="n" href="arch_progmodel.html">Next</a></td> |
| 29 | </tr> |
| 30 | </table> |
| 31 | <hr /> |
| 32 | </div> |
| 33 | <div class="chapter" lang="en" xml:lang="en"> |
| 34 | <div class="titlepage"> |
| 35 | <div> |
| 36 | <div> |
| 37 | <h2 class="title"><a id="arch"></a>Chapter 8. |
| 38 | Berkeley DB Architecture |
| 39 | </h2> |
| 40 | </div> |
| 41 | </div> |
| 42 | </div> |
| 43 | <div class="toc"> |
| 44 | <p> |
| 45 | <b>Table of Contents</b> |
| 46 | </p> |
| 47 | <dl> |
| 48 | <dt> |
| 49 | <span class="sect1"> |
| 50 | <a href="arch.html#arch_bigpic">The big picture</a> |
| 51 | </span> |
| 52 | </dt> |
| 53 | <dt> |
| 54 | <span class="sect1"> |
| 55 | <a href="arch_progmodel.html">Programming model</a> |
| 56 | </span> |
| 57 | </dt> |
| 58 | <dt> |
| 59 | <span class="sect1"> |
| 60 | <a href="arch_apis.html">Programmatic APIs</a> |
| 61 | </span> |
| 62 | </dt> |
| 63 | <dd> |
| 64 | <dl> |
| 65 | <dt> |
| 66 | <span class="sect2"> |
| 67 | <a href="arch_apis.html#idp51640232">C</a> |
| 68 | </span> |
| 69 | </dt> |
| 70 | <dt> |
| 71 | <span class="sect2"> |
| 72 | <a href="arch_apis.html#idp51656168">C++</a> |
| 73 | </span> |
| 74 | </dt> |
| 75 | <dt> |
| 76 | <span class="sect2"> |
| 77 | <a href="arch_apis.html#idp51646944">STL</a> |
| 78 | </span> |
| 79 | </dt> |
| 80 | <dt> |
| 81 | <span class="sect2"> |
| 82 | <a href="arch_apis.html#idp51647760">Java</a> |
| 83 | </span> |
| 84 | </dt> |
| 85 | <dt> |
| 86 | <span class="sect2"> |
| 87 | <a href="arch_apis.html#idp51664896">Dbm/Ndbm, Hsearch</a> |
| 88 | </span> |
| 89 | </dt> |
| 90 | </dl> |
| 91 | </dd> |
| 92 | <dt> |
| 93 | <span class="sect1"> |
| 94 | <a href="arch_script.html">Scripting languages</a> |
| 95 | </span> |
| 96 | </dt> |
| 97 | <dd> |
| 98 | <dl> |
| 99 | <dt> |
| 100 | <span class="sect2"> |
| 101 | <a href="arch_script.html#idp51640920">Perl</a> |
| 102 | </span> |
| 103 | </dt> |
| 104 | <dt> |
| 105 | <span class="sect2"> |
| 106 | <a href="arch_script.html#idp51639128">PHP</a> |
| 107 | </span> |
| 108 | </dt> |
| 109 | <dt> |
| 110 | <span class="sect2"> |
| 111 | <a href="arch_script.html#idp51657264">Tcl</a> |
| 112 | </span> |
| 113 | </dt> |
| 114 | </dl> |
| 115 | </dd> |
| 116 | <dt> |
| 117 | <span class="sect1"> |
| 118 | <a href="arch_utilities.html">Supporting utilities</a> |
| 119 | </span> |
| 120 | </dt> |
| 121 | </dl> |
| 122 | </div> |
| 123 | <div class="sect1" lang="en" xml:lang="en"> |
| 124 | <div class="titlepage"> |
| 125 | <div> |
| 126 | <div> |
| 127 | <h2 class="title" style="clear: both"><a id="arch_bigpic"></a>The big picture</h2> |
| 128 | </div> |
| 129 | </div> |
| 130 | </div> |
| 131 | <p>The previous chapters in this Reference Guide have described |
| 132 | applications that use the Berkeley DB access methods for fast data storage |
| 133 | and retrieval. The applications described in the following chapters |
| 134 | are similar in nature to the access method applications, but they are |
| 135 | also threaded and/or recoverable in the face of application or system |
| 136 | failure.</p> |
| 137 | <p>Application code that uses only the Berkeley DB access methods might appear |
| 138 | as follows:</p> |
| 139 | <pre class="programlisting">switch (ret = dbp->/put(dbp, NULL, &key, &data, 0)) { |
| 140 | case 0: |
| 141 | printf("db: %s: key stored.\n", (char *)key.data); |
| 142 | break; |
| 143 | default: |
| 144 | dbp->/err(dbp, ret, "dbp->/put"); |
| 145 | exit (1); |
| 146 | }</pre> |
| 147 | <p>The underlying Berkeley DB architecture that supports this is</p> |
| 148 | <div class="mediaobject"> |
| 149 | <img src="arch_smallpic.gif" /> |
| 150 | </div> |
| 151 | <p>As you can see from this diagram, the application makes calls into the |
| 152 | access methods, and the access methods use the underlying shared memory |
| 153 | buffer cache to hold recently used file pages in main memory.</p> |
| 154 | <p>When applications require recoverability, their calls to the Access |
| 155 | Methods must be wrapped in calls to the transaction subsystem. The |
| 156 | application must inform Berkeley DB where to begin and end transactions, and |
| 157 | must be prepared for the possibility that an operation may fail at any |
| 158 | particular time, causing the transaction to abort.</p> |
| 159 | <p>An example of transaction-protected code might appear as follows:</p> |
| 160 | <pre class="programlisting">for (fail = 0;;) { |
| 161 | /* Begin the transaction. */ |
| 162 | if ((ret = dbenv->/txn_begin(dbenv, NULL, &tid, 0)) != 0) { |
| 163 | dbenv->/err(dbenv, ret, "dbenv->/txn_begin"); |
| 164 | exit (1); |
| 165 | } |
| 166 | |
| 167 | /* Store the key. */ |
| 168 | switch (ret = dbp->/put(dbp, tid, &key, &data, 0)) { |
| 169 | case 0: |
| 170 | /* Success: commit the change. */ |
| 171 | printf("db: %s: key stored.\n", (char *)key.data); |
| 172 | if ((ret = tid->/commit(tid, 0)) != 0) { |
| 173 | dbenv->/err(dbenv, ret, "DB_TXN->/commit"); |
| 174 | exit (1); |
| 175 | } |
| 176 | return (0); |
| 177 | case DB_LOCK_DEADLOCK: |
| 178 | default: |
| 179 | /* Failure: retry the operation. */ |
| 180 | if ((t_ret = tid->/abort(tid)) != 0) { |
| 181 | dbenv->/err(dbenv, t_ret, "DB_TXN->/abort"); |
| 182 | exit (1); |
| 183 | } |
| 184 | if (fail++ == MAXIMUM_RETRY) |
| 185 | return (ret); |
| 186 | continue; |
| 187 | } |
| 188 | }</pre> |
| 189 | <p>In this example, the same operation is being done as before; however, |
| 190 | it is wrapped in transaction calls. The transaction is started with |
| 191 | <a href="../api_reference/C/txnbegin.html" class="olink">DB_ENV->txn_begin()</a> and finished with <a href="../api_reference/C/txncommit.html" class="olink">DB_TXN->commit()</a>. If the |
| 192 | operation fails due to a deadlock, the transaction is aborted using |
| 193 | <a href="../api_reference/C/txnabort.html" class="olink">DB_TXN->abort()</a>, after which the operation may be retried.</p> |
| 194 | <p>There are actually five major subsystems in Berkeley DB, as follows:</p> |
| 195 | <div class="variablelist"> |
| 196 | <dl> |
| 197 | <dt> |
| 198 | <span class="term">Access Methods</span> |
| 199 | </dt> |
| 200 | <dd>The access methods subsystem provides general-purpose support for |
| 201 | creating and accessing database files formatted as Btrees, Hashed files, |
| 202 | and Fixed- and Variable-length records. These modules are useful in |
| 203 | the absence of transactions for applications that need fast formatted |
| 204 | file support. See <a href="../api_reference/C/dbopen.html" class="olink">DB->open()</a> and <a href="../api_reference/C/dbcursor.html" class="olink">DB->cursor()</a> for more |
| 205 | information. These functions were already discussed in detail in the |
| 206 | previous chapters.</dd> |
| 207 | <dt> |
| 208 | <span class="term">Memory Pool</span> |
| 209 | </dt> |
| 210 | <dd>The Memory Pool subsystem is the general-purpose shared memory buffer pool |
| 211 | used by Berkeley DB. This is the shared memory cache that allows multiple |
| 212 | processes and threads within processes to share access to databases. This |
| 213 | module is useful outside of the Berkeley DB package for processes that require |
| 214 | portable, page-oriented, cached, shared file access.</dd> |
| 215 | <dt> |
| 216 | <span class="term">Transaction</span> |
| 217 | </dt> |
| 218 | <dd>The Transaction subsystem allows a group of database changes to be |
| 219 | treated as an atomic unit so that either all of the changes are done, |
| 220 | or none of the changes are done. The transaction subsystem implements |
| 221 | the Berkeley DB transaction model. This module is useful outside of the Berkeley DB |
| 222 | package for processes that want to transaction-protect their own data |
| 223 | modifications.</dd> |
| 224 | <dt> |
| 225 | <span class="term">Locking</span> |
| 226 | </dt> |
| 227 | <dd>The Locking subsystem is the general-purpose lock manager used by Berkeley DB. |
| 228 | This module is useful outside of the Berkeley DB package for processes that |
| 229 | require a portable, fast, configurable lock manager.</dd> |
| 230 | <dt> |
| 231 | <span class="term">Logging</span> |
| 232 | </dt> |
| 233 | <dd>The Logging subsystem is the write-ahead logging used to support the |
| 234 | Berkeley DB transaction model. It is largely specific to the Berkeley DB package, |
| 235 | and unlikely to be useful elsewhere except as a supporting module for |
| 236 | the Berkeley DB transaction subsystem.</dd> |
| 237 | </dl> |
| 238 | </div> |
| 239 | <p>Here is a more complete picture of the Berkeley DB library:</p> |
| 240 | <div class="mediaobject"> |
| 241 | <img src="arch_bigpic.gif" /> |
| 242 | </div> |
| 243 | <p>In this model, the application makes calls to the access methods and to |
| 244 | the Transaction subsystem. The access methods and Transaction subsystems |
| 245 | in turn make calls into the Memory Pool, Locking and Logging subsystems |
| 246 | on behalf of the application.</p> |
| 247 | <p>The underlying subsystems can be used independently by applications. |
| 248 | For example, the Memory Pool subsystem can be used apart from the rest |
| 249 | of Berkeley DB by applications simply wanting a shared memory buffer pool, or |
| 250 | the Locking subsystem may be called directly by applications that are |
| 251 | doing their own locking outside of Berkeley DB. However, this usage is not |
| 252 | common, and most applications will either use only the access methods |
| 253 | subsystem, or the access methods subsystem wrapped in calls to the Berkeley DB |
| 254 | transaction interfaces.</p> |
| 255 | </div> |
| 256 | </div> |
| 257 | <div class="navfooter"> |
| 258 | <hr /> |
| 259 | <table width="100%" summary="Navigation footer"> |
| 260 | <tr> |
| 261 | <td width="40%" align="left"><a accesskey="p" href="stl_known_issues.html">Prev</a> </td> |
| 262 | <td width="20%" align="center"> </td> |
| 263 | <td width="40%" align="right"> <a accesskey="n" href="arch_progmodel.html">Next</a></td> |
| 264 | </tr> |
| 265 | <tr> |
| 266 | <td width="40%" align="left" valign="top">Dbstl known issues </td> |
| 267 | <td width="20%" align="center"> |
| 268 | <a accesskey="h" href="index.html">Home</a> |
| 269 | </td> |
| 270 | <td width="40%" align="right" valign="top"> Programming model</td> |
| 271 | </tr> |
| 272 | </table> |
| 273 | </div> |
| 274 | </body> |
| 275 | </html> |