1
<!--$Id: bigpic.so,v 8.29 2001/05/23 21:03:31 bostic Exp $-->
2
<!--Copyright 1997-2001 by Sleepycat Software, Inc.-->
3
<!--All rights reserved.-->
6
<title>Berkeley DB Reference Guide: The big picture</title>
7
<meta name="description" content="Berkeley DB: An embedded database programmatic toolkit.">
8
<meta name="keywords" content="embedded,database,programmatic,toolkit,b+tree,btree,hash,hashing,transaction,transactions,locking,logging,access method,access methods,java,C,C++">
11
<table width="100%"><tr valign=top>
12
<td><h3><dl><dt>Berkeley DB Reference Guide:<dd>Architecture</dl></h3></td>
13
<td align=right><a href="../../ref/am_misc/faq.html"><img src="../../images/prev.gif" alt="Prev"></a><a href="../../reftoc.html"><img src="../../images/ref.gif" alt="Ref"></a><a href="../../ref/arch/progmodel.html"><img src="../../images/next.gif" alt="Next"></a>
16
<h1 align=center>The big picture</h1>
17
<p>The previous chapters in this Reference Guide have described
18
applications that use the Berkeley DB access methods for fast data storage
19
and retrieval. The applications described in the following chapters
20
are similar in nature to the access method applications, but they are
21
also threaded and/or recoverable in the face of application or system
23
<p>Application code that uses only the Berkeley DB access methods might appear
25
<p><blockquote><pre>switch (ret = dbp->put(dbp, NULL, &key, &data, 0)) {
27
printf("db: %s: key stored.\n", (char *)key.data);
30
dbp->err(dbp, ret, "dbp->put");
33
<p>The underlying Berkeley DB architecture that supports this is
34
<p align=center><img src="smallpic.gif" alt="small">
35
<p>As you can see from this diagram, the application makes calls into the
36
access methods, and the access methods use the underlying shared memory
37
buffer cache to hold recently used file pages in main memory.
38
<p>When applications require recoverability, their calls to the Access
39
Methods must be wrapped in calls to the transaction subsystem. The
40
application must inform Berkeley DB where to begin and end transactions, and
41
must be prepared for the possibility that an operation may fail at any
42
particular time, causing the transaction to abort.
43
<p>An example of transaction-protected code might appear as follows:
44
<p><blockquote><pre>for (fail = 0;;) {
45
/* Begin the transaction. */
46
if ((ret = txn_begin(dbenv, NULL, &tid, 0)) != 0) {
47
dbenv->err(dbenv, ret, "txn_begin");
52
switch (ret = dbp->put(dbp, tid, &key, &data, 0)) {
54
/* Success: commit the change. */
55
printf("db: %s: key stored.\n", (char *)key.data);
56
if ((ret = txn_commit(tid, 0)) != 0) {
57
dbenv->err(dbenv, ret, "txn_commit");
61
case DB_LOCK_DEADLOCK:
63
/* Failure: retry the operation. */
64
if ((t_ret = txn_abort(tid)) != 0) {
65
dbenv->err(dbenv, t_ret, "txn_begin");
68
if (++fail == MAXIMUM_RETRY)
73
<p>In this example, the same operation is being done as before; however,
74
it is wrapped in transaction calls. The transaction is started with
75
<a href="../../api_c/txn_begin.html">txn_begin</a> and finished with <a href="../../api_c/txn_commit.html">txn_commit</a>. If the
76
operation fails due to a deadlock, the transaction is aborted using
77
<a href="../../api_c/txn_abort.html">txn_abort</a>, after which the operation may be retried.
78
<p>There are actually five major subsystems in Berkeley DB, as follows:
80
<p><dt>Access Methods<dd>The access methods subsystem provides general-purpose support for
81
creating and accessing database files formatted as Btrees, Hashed files,
82
and Fixed- and Variable-length records. These modules are useful in
83
the absence of transactions for applications that need fast formatted
84
file support. See <a href="../../api_c/db_open.html">DB->open</a> and <a href="../../api_c/db_cursor.html">DB->cursor</a> for more
85
information. These functions were already discussed in detail in the
87
<p><dt>Memory Pool<dd>The Memory Pool subsystem is the general-purpose shared memory buffer pool
88
used by Berkeley DB. This is the shared memory cache that allows multiple
89
processes and threads within processes to share access to databases. This
90
module is useful outside of the Berkeley DB package for processes that require
91
portable, page-oriented, cached, shared file access.
92
<p><dt>Transaction<dd>The Transaction subsystem allows a group of database changes to be
93
treated as an atomic unit so that either all of the changes are done,
94
or none of the changes are done. The transaction subsystem implements
95
the Berkeley DB transaction model. This module is useful outside of the Berkeley DB
96
package for processes that want to transaction-protect their own data
98
<p><dt>Locking<dd>The Locking subsystem is the general-purpose lock manager used by Berkeley DB.
99
This module is useful outside of the Berkeley DB package for processes that
100
require a portable, fast, configurable lock manager.
101
<p><dt>Logging<dd>The Logging subsystem is the write-ahead logging used to support the
102
Berkeley DB transaction model. It is largely specific to the Berkeley DB package,
103
and unlikely to be useful elsewhere except as a supporting module for
104
the Berkeley DB transaction subsystem.
106
<p>Here is a more complete picture of the Berkeley DB library:
107
<p align=center><img src="bigpic.gif" alt="large">
108
<p>In this model, the application makes calls to the access methods and to
109
the Transaction subsystem. The access methods and Transaction subsystems
110
in turn make calls into the Memory Pool, Locking and Logging subsystems
111
on behalf of the application.
112
<p>The underlying subsystems can be used independently by applications.
113
For example, the Memory Pool subsystem can be used apart from the rest
114
of Berkeley DB by applications simply wanting a shared memory buffer pool, or
115
the Locking subsystem may be called directly by applications that are
116
doing their own locking outside of Berkeley DB. However, this usage is not
117
common, and most applications will either use only the access methods
118
subsystem, or the access methods subsystem wrapped in calls to the Berkeley DB
119
transaction interfaces.
120
<table width="100%"><tr><td><br></td><td align=right><a href="../../ref/am_misc/faq.html"><img src="../../images/prev.gif" alt="Prev"></a><a href="../../reftoc.html"><img src="../../images/ref.gif" alt="Ref"></a><a href="../../ref/arch/progmodel.html"><img src="../../images/next.gif" alt="Next"></a>
122
<p><font size=1><a href="http://www.sleepycat.com">Copyright Sleepycat Software</a></font>