Debian Codegraph @

docs/programmer_reference/transapp_data_open.html

raw symbols SCIP
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>Opening the databases</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="transapp.html" title="Chapter 11.  Berkeley DB Transactional Data Store Applications" />
11 <link rel="prev" href="transapp_env_open.html" title="Opening the environment" />
12 <link rel="next" href="transapp_put.html" title="Recoverability and deadlock handling" />
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">Opening the databases</th>
22 </tr>
23 <tr>
24 <td width="20%" align="left"><a accesskey="p" href="transapp_env_open.html">Prev</a> </td>
25 <th width="60%" align="center">Chapter 11. 
26 Berkeley DB Transactional Data Store Applications
27 </th>
28 <td width="20%" align="right"> <a accesskey="n" href="transapp_put.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="transapp_data_open"></a>Opening the databases</h2>
38 </div>
39 </div>
40 </div>
41 <p>Next, we open three databases ("color" and "fruit" and "cats"), in the
42 database environment. Again, our <a href="../api_reference/C/db.html" class="olink">DB</a> database handles are
43 declared to be free-threaded using the <a href="../api_reference/C/dbopen.html#open_DB_THREAD" class="olink">DB_THREAD</a> flag, and so
44 may be used by any number of threads we subsequently create.</p>
45 <a id="prog_transapp2"></a>
46 <pre class="programlisting">int
47 main(int argc, char *argv[])
48 {
49 extern int optind;
50 DB *db_cats, *db_color, *db_fruit;
51 DB_ENV *dbenv;
52 int ch;
53
54 while ((ch = getopt(argc, argv, "")) != EOF)
55 switch (ch) {
56 case '?':
57 default:
58 usage();
59 }
60 argc -= optind;
61 argv += optind;
62
63 env_dir_create();
64 env_open(&amp;dbenv);
65 ...
66
67 /* Open database: Key is fruit class; Data is specific type. */
68 if (db_open(dbenv, &amp;db_fruit, "fruit", 0))
69 return (1);
70
71 /* Open database: Key is a color; Data is an integer. */
72 if (db_open(dbenv, &amp;db_color, "color", 0))
73 return (1);
74
75 /*
76 * Open database:
77 * Key is a name; Data is: company name, cat breeds.
78 */
79 if (db_open(dbenv, &amp;db_cats, "cats", 1))
80 return (1);
81
82 ...
83
84 return (0);
85 }
86
87 int
88 db_open(DB_ENV *dbenv, DB **dbp, char *name, int dups)
89 {
90 DB *db;
91 int ret;
92
93 /* Create the database handle. */
94 if ((ret = db_create(&amp;db, dbenv, 0)) != 0) {
95 dbenv-&gt;err(dbenv, ret, "db_create");
96 return (1);
97 }
98
99 /* Optionally, turn on duplicate data items. */
100 if (dups &amp;&amp; (ret = db-&gt;set_flags(db, DB_DUP)) != 0) {
101 (void)db-&gt;close(db, 0);
102 dbenv-&gt;err(dbenv, ret, "db-&gt;set_flags: DB_DUP");
103 return (1);
104 }
105
106 /*
107 * Open a database in the environment:
108 * create if it doesn't exist
109 * free-threaded handle
110 * read/write owner only
111 */
112 if ((ret = db-&gt;open(db, NULL, name, NULL, DB_BTREE,
113 DB_AUTO_COMMIT | DB_CREATE | DB_THREAD, S_IRUSR | S_IWUSR)) != 0) {
114 (void)db-&gt;close(db, 0);
115 dbenv-&gt;err(dbenv, ret, "db-&gt;open: %s", name);
116 return (1);
117 }
118
119 *dbp = db;
120 return (0);
121 }</pre>
122 <p>After opening the database, we can use the <a href="../api_reference/C/db_stat.html" class="olink">db_stat</a> utility to
123 display information about a database we have created:</p>
124 <pre class="programlisting">prompt&gt; db_stat -h TXNAPP -d color
125 53162 Btree magic number.
126 8 Btree version number.
127 Flags:
128 2 Minimum keys per-page.
129 8192 Underlying database page size.
130 1 Number of levels in the tree.
131 0 Number of unique keys in the tree.
132 0 Number of data items in the tree.
133 0 Number of tree internal pages.
134 0 Number of bytes free in tree internal pages (0% ff).
135 1 Number of tree leaf pages.
136 8166 Number of bytes free in tree leaf pages (0.% ff).
137 0 Number of tree duplicate pages.
138 0 Number of bytes free in tree duplicate pages (0% ff).
139 0 Number of tree overflow pages.
140 0 Number of bytes free in tree overflow pages (0% ff).
141 0 Number of pages on the free list.</pre>
142 <p>The database open must be enclosed within a transaction in order to be
143 recoverable. The transaction will ensure that created files are
144 re-created in recovered environments (or do not appear at all).
145 Additional database operations or operations on other databases can be
146 included in the same transaction, of course. In the simple case, where
147 the open is the only operation in the transaction, an application can
148 set the <a href="../api_reference/C/envset_flags.html#envset_flags_DB_AUTO_COMMIT" class="olink">DB_AUTO_COMMIT</a> flag instead of creating and managing
149 its own transaction handle. The <a href="../api_reference/C/envset_flags.html#envset_flags_DB_AUTO_COMMIT" class="olink">DB_AUTO_COMMIT</a> flag will
150 internally wrap the operation in a transaction, simplifying application
151 code.</p>
152 <p>The previous example is the simplest case of transaction protection for
153 database open. Obviously, additional database operations can be done
154 in the scope of the same transaction. For example, an application
155 maintaining a list of the databases in a database environment in a
156 well-known file might include an update of the list in the same
157 transaction in which the database is created. Or, an application might
158 create both a primary and secondary database in a single transaction.</p>
159 <p><a href="../api_reference/C/db.html" class="olink">DB</a> handles that will later be used for transactionally protected
160 database operations must be opened within a transaction. Specifying a
161 transaction handle to database operations using <a href="../api_reference/C/db.html" class="olink">DB</a> handles not
162 opened within a transaction will return an error. Similarly, not
163 specifying a transaction handle to database operations that will modify
164 the database, using handles that were opened within a transaction, will
165 also return an error.</p>
166 </div>
167 <div class="navfooter">
168 <hr />
169 <table width="100%" summary="Navigation footer">
170 <tr>
171 <td width="40%" align="left"><a accesskey="p" href="transapp_env_open.html">Prev</a> </td>
172 <td width="20%" align="center">
173 <a accesskey="u" href="transapp.html">Up</a>
174 </td>
175 <td width="40%" align="right"> <a accesskey="n" href="transapp_put.html">Next</a></td>
176 </tr>
177 <tr>
178 <td width="40%" align="left" valign="top">Opening the environment </td>
179 <td width="20%" align="center">
180 <a accesskey="h" href="index.html">Home</a>
181 </td>
182 <td width="40%" align="right" valign="top"> Recoverability and deadlock handling</td>
183 </tr>
184 </table>
185 </div>
186 </body>
187 </html>