Debian Codegraph @

docs/programmer_reference/stl_persistence.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>Dbstl persistence</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_complex_rw.html" title="Store and Retrieve data or objects of complex types" />
12 <link rel="next" href="stl_container_specific.html" title="Dbstl container specific notes" />
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 persistence</th>
22 </tr>
23 <tr>
24 <td width="20%" align="left"><a accesskey="p" href="stl_complex_rw.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_container_specific.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_persistence"></a>Dbstl persistence</h2>
36 </div>
37 </div>
38 </div>
39 <div class="toc">
40 <dl>
41 <dt>
42 <span class="sect2">
43 <a href="stl_persistence.html#directdbget">Direct database get</a>
44 </span>
45 </dt>
46 <dt>
47 <span class="sect2">
48 <a href="stl_persistence.html#chg_persistence">Change persistence</a>
49 </span>
50 </dt>
51 <dt>
52 <span class="sect2">
53 <a href="stl_persistence.html#obj_life_persistence">Object life time and persistence </a>
54 </span>
55 </dt>
56 </dl>
57 </div>
58 <p>
59 The following sections provide information on how to achieve
60 persistence using dbstl.
61 </p>
62 <div class="sect2" lang="en" xml:lang="en">
63 <div class="titlepage">
64 <div>
65 <div>
66 <h3 class="title"><a id="directdbget"></a>Direct database get</h3>
67 </div>
68 </div>
69 </div>
70 <p>
71 Each container has a <span class="bold"><strong>begin()</strong></span> method
72 which produces an iterator. These
73 <span class="bold"><strong>begin</strong></span> methods take a boolean parameter,
74 <span class="bold"><strong>directdb_get</strong></span>, which controls the
75 caching behavior of the iterator. The default value of this parameter
76 is <code class="literal">true</code>.
77 </p>
78 <p>
79 If <span class="bold"><strong>directdb_get</strong></span> is
80 <code class="literal">true</code>, then the persistent object is fetched anew
81 from the database each time the iterator is dereferenced as a pointer
82 by use of the star-operator
83 (<span class="bold"><strong>*iterator</strong></span>) or by use of the arrow-operator
84 (<span class="bold"><strong>iterator-&gt;member</strong></span>). If
85 <span class="bold"><strong>directdb_get</strong></span> is <code class="literal">false</code>, then
86 the first dereferencing of the iterator fetches the object from the
87 database, but later dereferences can return cached data.
88 </p>
89 <p>
90 With <span class="bold"><strong>directdb_get</strong></span> set to <code class="literal">true</code>, if you call:
91 </p>
92 <pre class="programlisting">(*iterator).datamember1=new-value1;
93 (*iterator).datamember2=new-value2; </pre>
94 <p>
95 then the assignment to <code class="literal">datamember1</code> will be lost,
96 because the second dereferencing of the iterator would cause the cached
97 copy of the object to be overwritten by the object's persistent data
98 from the database.
99 </p>
100 <p>
101 You also can use the arrow operator like this:
102 </p>
103 <pre class="programlisting">iterator-&gt;datamember1=new-value1;
104 iterator-&gt;datamember2=new-value2; </pre>
105 <p>
106 This works exactly the same way as <span class="bold"><strong>iterator::operator*</strong></span>.
107 For this reason, the same caching rules apply to arrow operators as they do
108 for star operators.
109 </p>
110 <p>
111 One way to avoid this problem is to create a reference to the object,
112 and use it to access the object:
113 </p>
114 <pre class="programlisting">container::value_type &amp;ref = *iterator;
115 ref.datamember1=new-value1;
116 ref.datamember2=new-value2;
117 ...// more member function calls and datamember assignments
118 ref._DB_STL_StoreElement(); </pre>
119 <p>
120 The above code will not lose the newly assigned value of <code class="literal">ref.datamember1</code>
121 in the way that the previous example did.
122 </p>
123 <p>
124 In order to avoid these complications, you can assign to the object
125 referenced by an iterator with another object of the same type like this:
126 </p>
127 <pre class="programlisting">container::value_type obj2;
128 obj2.datamember1 = new-value1;
129 obj2.datamember2 = new-value2;
130 *itr = obj2; </pre>
131 <p>
132 This code snippet causes the new values in <code class="literal">obj2</code> to
133 be stored into the underlying database.
134 </p>
135 <p>
136 If you have two iterators going through the same container like this:
137 </p>
138 <pre class="programlisting">
139 for (iterator1 = v.begin(), iterator2 = v.begin();
140 iterator1 != v.end();
141 ++iterator1, ++iterator2) {
142 *iterator1 = new_value;
143 print(*iterator2);
144 } </pre>
145 <p>
146 then the printed value will depend on the value of
147 <span class="bold"><strong>directdb_get</strong></span> with which the
148 iterator had been created. If <span class="bold"><strong>directdb_get</strong></span>
149 is <code class="literal">false</code>, then the original, persistent value is
150 printed; otherwise the newly assigned value is returned from the
151 cache when <code class="literal">iterator2</code> is dereferenced. This
152 happens because each iterator has its own cached copy of the
153 persistent object, and the dereferencing of
154 <code class="literal">iterator2</code> refreshes
155 <code class="literal">iterator2</code>'s copy from the database, retrieving
156 the value stored by the assignment to
157 <code class="literal">*iterator1</code>.
158 </p>
159 <p>
160 Alternatively, you can set <span class="bold"><strong>directdb_get
161 </strong></span> to <code class="literal">false</code> and call
162 <code class="methodname">iterator2-&gt;refresh()</code> immediately before
163 the dereferencing of <code class="literal">iterator2</code>, so that
164 <code class="literal">iterator2</code>'s cached value is refreshed.
165 </p>
166 <p>
167 If <span class="bold"><strong>directdb_get</strong></span> is
168 <code class="literal">false</code>, a few of the tests in dbstl's test kit
169 will fail. This is because the above contrived case appears in
170 several of C++ STL tests. Consequently, the default value of the
171 <span class="bold"><strong>directdb_get</strong></span> parameter in the
172 <code class="methodname">container::begin()</code> methods is
173 <code class="literal">true</code>. If your use cases avoid such bizarre usage
174 of iterators, you can set it to <code class="literal">false</code>, which
175 makes the iterator read operation faster.
176 </p>
177 </div>
178 <div class="sect2" lang="en" xml:lang="en">
179 <div class="titlepage">
180 <div>
181 <div>
182 <h3 class="title"><a id="chg_persistence"></a>Change persistence</h3>
183 </div>
184 </div>
185 </div>
186 <p>
187 If you modify the object to which an iterator refers by using one
188 of the following:
189 </p>
190 <pre class="programlisting">(*iterator).member_function_call()</pre>
191 <p>
192 or
193 </p>
194 <pre class="programlisting">(*iterator).data_member = new_value</pre>
195 <p>
196 then you should call
197 <code class="methodname">iterator-&gt;_DB_STL_StoreElement()</code> to
198 store the change. Otherwise the change is lost after the
199 iterator moves on to other elements.
200 </p>
201 <p>
202 If you are storing a sequence, and you modified some part of it, you
203 should also call
204 <code class="methodname">iterator-&gt;_DB_STL_StoreElement()</code>
205 before moving the iterator.
206 </p>
207 <p>
208 And in both cases, if <span class="bold"><strong>directdb_get</strong></span>
209 is <code class="literal">true</code> (this is the default value), you should
210 call <code class="methodname">_DB_STL_StoreElement()</code> after the
211 change and before the next iterator movement OR the next
212 dereferencing of the iterator by the star or arrow operators
213 (<code class="literal">iterator::operator*</code> or
214 <code class="literal">iterator::operator-&gt;</code>). Otherwise, you will
215 lose the change.
216 </p>
217 <p>
218 If you update the element by assigning to a dereferenced iterator like
219 this:
220 </p>
221 <pre class="programlisting">*iterator = new_element;</pre>
222 <p>
223 then you never have to call
224 <code class="methodname">_DB_STL_StoreElement()</code> because the change
225 is stored in the database automatically.
226 </p>
227 </div>
228 <div class="sect2" lang="en" xml:lang="en">
229 <div class="titlepage">
230 <div>
231 <div>
232 <h3 class="title"><a id="obj_life_persistence"></a>Object life time and persistence </h3>
233 </div>
234 </div>
235 </div>
236 <p>
237 Dbstl is an interface to Berkeley DB, so it is used to store data
238 persistently. This is really a different purpose from that of
239 regular C++ STL. This difference in their goals has implications on
240 expected object lifetime: In standard STL, when you store an object
241 A of type ID into C++ stl vector V using V.push_back(A), if a
242 proper copy constructor is provided in A's class type, then the
243 copy of A (call it B) and everything in B, such as another object C
244 pointed to by B's data member B.c_ptr, will be stored in V and will
245 live as long as B is still in V and V is alive. B will be destroyed
246 when V is destroyed or B is erased from V.
247 </p>
248 <p>
249 This is not true for dbstl, which will copy A's data and store it
250 in the underlying database. The copy is by default a shallow copy,
251 but users can register their object marshalling and unmarshalling
252 functions using the <code class="classname">DbstlElemTraits</code> class
253 template. So if A is passed to a <code class="classname">db_vector</code>
254 container, <code class="literal">dv</code>, by using
255 <code class="literal">dv.push_back(A)</code>, then dbstl copies A's data
256 using the registered functions, and stores data into the underlying
257 database. Consequently, A will be valid, even if the container is
258 destroyed, because it is stored into the database.
259 </p>
260 <p>
261 If the copy is simply a shallow copy, and A is later destroyed, then
262 the pointer stored in the database will become invalid. The next time
263 we use the retrieved object, we will be using an invalid pointer, which
264 probably will result in errors. To avoid this, store the referred
265 object C rather than the pointer member A.c_ptr itself, by registering
266 the right marshalling/unmarshalling function with
267 <code class="classname">DbstlElemTraits</code>.
268 </p>
269 <p>
270 For example, consider the following example class declaration:
271 </p>
272 <pre class="programlisting">class ID
273 {
274 public:
275 string Name;
276 int Score;
277 }; </pre>
278 <p>
279 Here, the class ID has a data member <span class="bold"><strong>Name</strong></span>, which refers to a memory address of
280 the actual characters in the string. If we simply shallow copy an
281 object, <code class="literal">id</code>, of class ID to store it, then the
282 stored data, <code class="literal">idd</code>, is invalid when
283 <code class="literal">id</code> is destroyed. This is because
284 <code class="literal">idd</code> and <code class="literal">id</code> refer to a common
285 memory address which is the base address of the memory space storing
286 all characters in the string, and this memory space is released when
287 <code class="literal">id</code> is destroyed. So <code class="literal">idd</code> will be
288 referring to an invalid address. The next time we retrieve
289 <code class="literal">idd</code> and use it, there will probably be memory
290 corruption.
291 </p>
292 <p>
293 The way to store <code class="literal">id</code> is to write a marshal/unmarshal
294 function pair like this:
295 </p>
296 <pre class="programlisting">void copy_id(void *dest, const ID&amp;elem)
297 {
298 memcpy(dest, &amp;elem.Score, sizeof(elem.Score));
299 char *p = ((char *)dest) + sizeof(elem.Score);
300 strcpy(p, elem.Name.c_str());
301 }
302
303 void restore_id(ID&amp; dest, const void *srcdata)
304 {
305 memcpy(&amp;dest.Score, srcdata, sizeof(dest.Score));
306 const char *p = ((char *)srcdata) + sizeof(dest.Score);
307 dest.Name = p;
308 }
309
310 size_t size_id(const ID&amp; elem)
311 {
312 return sizeof(elem.Score) + elem.Name.size() +
313 1;// store the '\0' char.
314 } </pre>
315 <p>
316 Then register the above functions before storing any instance of
317 <code class="classname">ID</code>:
318 </p>
319 <pre class="programlisting">DbstlElemTraits&lt;ID&gt;::instance()-&gt;set_copy_function(copy_id);
320 DbstlElemTraits&lt;ID&gt;::instance()-&gt;set_size_function(size_id);
321 DbstlElemTraits&lt;ID&gt;::instance()-&gt;set_restore_function(restore_id); </pre>
322 <p>
323 This way, the actual data of instances of ID are stored, and so the
324 data will persist even if the container itself is destroyed.
325 </p>
326 </div>
327 </div>
328 <div class="navfooter">
329 <hr />
330 <table width="100%" summary="Navigation footer">
331 <tr>
332 <td width="40%" align="left"><a accesskey="p" href="stl_complex_rw.html">Prev</a> </td>
333 <td width="20%" align="center">
334 <a accesskey="u" href="stl.html">Up</a>
335 </td>
336 <td width="40%" align="right"> <a accesskey="n" href="stl_container_specific.html">Next</a></td>
337 </tr>
338 <tr>
339 <td width="40%" align="left" valign="top">Store and Retrieve data or objects of complex types  </td>
340 <td width="20%" align="center">
341 <a accesskey="h" href="index.html">Home</a>
342 </td>
343 <td width="40%" align="right" valign="top"> Dbstl container specific notes</td>
344 </tr>
345 </table>
346 </div>
347 </body>
348 </html>