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