1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
|
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Dbstl examples</title>
<link rel="stylesheet" href="gettingStarted.css" type="text/css" />
<meta name="generator" content="DocBook XSL Stylesheets V1.73.2" />
<link rel="start" href="index.html" title="Berkeley DB Programmer's Reference Guide" />
<link rel="up" href="stl.html" title="Chapter 7. Standard Template Library API" />
<link rel="prev" href="stl_usecase.html" title="Dbstl typical use cases" />
<link rel="next" href="stl_db_usage.html" title="Berkeley DB configuration" />
</head>
<body>
<div xmlns="" class="navheader">
<div class="libver">
<p>Library Version 11.2.5.3</p>
</div>
<table width="100%" summary="Navigation header">
<tr>
<th colspan="3" align="center">Dbstl examples</th>
</tr>
<tr>
<td width="20%" align="left"><a accesskey="p" href="stl_usecase.html">Prev</a> </td>
<th width="60%" align="center">Chapter 7. Standard Template Library API</th>
<td width="20%" align="right"> <a accesskey="n" href="stl_db_usage.html">Next</a></td>
</tr>
</table>
<hr />
</div>
<div class="sect1" lang="en" xml:lang="en">
<div class="titlepage">
<div>
<div>
<h2 class="title" style="clear: both"><a id="stl_examples"></a>Dbstl examples</h2>
</div>
</div>
</div>
<p>
Because dbstl is so much like C++ STL, its usage exactly mirrors that
of C++ STL, with the exception of a few optional Berkeley DB specific
configurations. In fact, the only difference between a program using
dbstl and one using C++ STL is the class names. That is,
<code class="classname">vector</code> becomes <code class="classname">db_vector</code>,
and <code class="classname">map</code> becomes <code class="classname">db_map</code>.
</p>
<p>
The typical procedure for using dbstl is:
</p>
<div class="orderedlist">
<ol type="1">
<li>
<p>
Optionally create and open your own Berkeley DB environment and
database handles using the DB C++ API. If you perform these opens
using the C++ API, make sure to perform necessary
environment and database configurations at that time.
</p>
</li>
<li>
<p>
Optionally pass environment and database handles to dbstl container
constructors when you create dbstl container objects. Note that you
can create a dbstl container without passing it an environment and
database object. When you do this, an internal anonymous database
is created for you. In this situation, dbstl provides no data
persistence guarantees.
</p>
</li>
<li>
<p>
Perform dbstl-specific configurations. For example, you can
configure cursor open flags, as well as database
access for autocommit. You can also configure callback functions.
</p>
</li>
<li>
<p>
Interact with the data contained in your Berkeley DB databases using
dbstl containers and iterators. This usage of dbstl is identical to
C++ STL container and iterator usage.
</p>
</li>
<li>
<p>
At this time, you can also use dbstl calls that are specific to Berkeley DB.
For example, you can use Berkeley DB specific calls that manage transaction
begin/commit/abort, handle registration, and so forth. While these
calls are part of dbstl, they have no equivalence in the C++ STL
APIs.
</p>
</li>
<li>
<p>
When your application is done using Berkeley DB, you do not need to
explicitly close any Berkeley DB handles (environments, database,
cursors, and so forth). Dbstl automatically closes all such handles
for you.
</p>
</li>
</ol>
</div>
<p>
For examples of dbstl usage, see the example programs in the
<code class="filename">$db/examples_stl</code> directory.
</p>
<p>
The following program listing provides two code fragments. You can
find more example code in the <code class="filename">dbstl/examples/</code>
and <code class="filename">dbstl/test</code> directories.
</p>
<pre class="programlisting">//////////////// Code Snippet 1 ////////////////
db_vector<int, ElementHolder<int> > vctr(100);
for (int i = 0; i < 100; i++)
vctr[i] = i;
for (int i = 0; i < 100; i++) {
cout<<"\nvctr["<<i<<"] : "<<vctr[i];
vctr[i] = vctr[i] * vctr[i];
cout<<"\nvctr["<<i<<"] squre : "<<vctr[i];
}
//////////////// Code Snippet 2 ////////////////
typedef db_map<char *, const char *, ElementHolder<const char *> >
strmap_t2;
strmap_t2 strmap;
char str[2], str2[2];
str[1] = str2[1] = '\0';
for (char c = 0; c < 26; c++) {
str[0] = c + 'a';
str2[0] = 'z' - c;
strmap[str] = str2;
}
for (strmap_t2::iterator itr = strmap.begin(); itr != strmap.end(); ++itr)
cout<<endl<<itr->first<<" : "<<itr->second;
using namespace dbstl;
dbstl::db_map<char, int> v;
v['i'] = 1;
cout<<v['i'];
dbstl::db_map<char *, char *> name_addr_map;
// The strings rather than the memory pointers are stored into DB.
name_addr_map["Alex"] = "Sydney Australia";
name_addr_map["David"] = "Shenzhen China";
cout<<"Alex's address:"<<name_addr_map["Alex"];
dbstl::db_vector<Person> vi;
// Some callback configurations follow here.
// The strings and objects rather than pointers are stored into DB.
Person obj("David Zhao", "Oracle", new Office("Boston", "USA"));
vi.push_back(obj); // More person storage.
for (int I = 0; I < vi.size(); I++)
cout<<vi[I]; </pre>
<p>
The first snippet initializes a db_vector container of 100 elements,
with an in-memory anonymous database internally created by dbstl. The
only difference between this and C++ STL is dbstl requires one more
<code class="literal">type</code> parameter:
<em class="parameter"><code>ElementHolder<int></code></em>. The
<code class="classname">ElementHolder</code> class template should be used for
every type of dbstl container that will store C++ primitive data types,
such as <code class="literal">int</code>, <code class="literal">float</code>,
<code class="literal">char *</code>, <code class="literal">wchar_t *</code>, and so forth. But these
class templates should not be used for class types for reasons that we
explain in the following chapters.
</p>
<p>
In the second code snippet, the assignment:
</p>
<pre class="programlisting">strmap[str] = str2;</pre>
<p>
is used to store a string pair (<code class="literal">(str, str2)</code>) instead
of pointers to the underlying database.
</p>
<p>
The rest of the code used in these snippets is identical to the code
you would use for C++ STL containers. However, by using dbstl, you are
storing data into a Berkeley DB database. If you create your own
database with backing files on disk, your data or objects can persist
and be restored when the program runs again.
</p>
</div>
<div class="navfooter">
<hr />
<table width="100%" summary="Navigation footer">
<tr>
<td width="40%" align="left"><a accesskey="p" href="stl_usecase.html">Prev</a> </td>
<td width="20%" align="center">
<a accesskey="u" href="stl.html">Up</a>
</td>
<td width="40%" align="right"> <a accesskey="n" href="stl_db_usage.html">Next</a></td>
</tr>
<tr>
<td width="40%" align="left" valign="top">Dbstl typical use cases </td>
<td width="20%" align="center">
<a accesskey="h" href="index.html">Home</a>
</td>
<td width="40%" align="right" valign="top"> Berkeley DB configuration</td>
</tr>
</table>
</div>
</body>
</html>
|