File: stl_complex_rw.html

package info (click to toggle)
db5.3 5.3.28%2Bdfsg1-0.5
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 158,360 kB
  • sloc: ansic: 448,411; java: 111,824; tcl: 80,544; sh: 44,326; cs: 33,697; cpp: 21,604; perl: 14,557; xml: 10,799; makefile: 4,077; yacc: 1,003; awk: 965; sql: 801; erlang: 342; python: 216; php: 24; asm: 14
file content (408 lines) | stat: -rw-r--r-- 19,772 bytes parent folder | download | duplicates (8)
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
<?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>Store and Retrieve data or objects of complex types</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_primitive_rw.html" title="Working with primitive types" />
    <link rel="next" href="stl_persistence.html" title="Dbstl persistence" />
  </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">Store and Retrieve data or objects of complex types </th>
        </tr>
        <tr>
          <td width="20%" align="left"><a accesskey="p" href="stl_primitive_rw.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_persistence.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_complex_rw"></a>Store and Retrieve data or objects of complex types </h2>
          </div>
        </div>
      </div>
      <div class="toc">
        <dl>
          <dt>
            <span class="sect2">
              <a href="stl_complex_rw.html#idp51458752">Storing varying length objects</a>
            </span>
          </dt>
          <dt>
            <span class="sect2">
              <a href="stl_complex_rw.html#idp51477944">Storing arbitrary sequences</a>
            </span>
          </dt>
          <dt>
            <span class="sect2">
              <a href="stl_complex_rw.html#idp51524696">Notes</a>
            </span>
          </dt>
        </dl>
      </div>
      <div class="sect2" lang="en" xml:lang="en">
        <div class="titlepage">
          <div>
            <div>
              <h3 class="title"><a id="idp51458752"></a>Storing varying length objects</h3>
            </div>
          </div>
        </div>
        <p>
    A structure like this: 
</p>
        <pre class="programlisting">class SMSMsg
{
public:
	size_t mysize;
	time_t when;
	size_t szmsg;
	int to;
	char msg[1];
};  </pre>
        <p>
    with a varying length string in <code class="literal">msg</code> cannot simply be
    stored in a <code class="literal">db_vector&lt;SMSMsg&gt;</code> without some
    configuration on your part. This is because, by default, dbstl uses the
    <span class="bold"><strong>sizeof()</strong></span> operator to get the size of
    an object and then <code class="function">memcpy()</code> to copy the object. This
    process is not suitable for this use-case as it will fail to capture the
    variable length string contained in <code class="literal">msg</code>.
</p>
        <p>
    There are currently two ways to store these kind of objects:
</p>
        <div class="orderedlist">
          <ol type="1">
            <li>
              <p>
             Register callback functions with dbstl that are used to measure an object's size,
             and then marshal/unmarshal the object.
        </p>
            </li>
            <li>
              <p>
            Use a <code class="classname">DbstlDbt</code> wrapper object.
        </p>
            </li>
          </ol>
        </div>
        <div class="sect3" lang="en" xml:lang="en">
          <div class="titlepage">
            <div>
              <div>
                <h4 class="title"><a id="idp51457480"></a>Storing by marshaling objects</h4>
              </div>
            </div>
          </div>
          <p>
        One way to store an object that contains variable-sized fields is
        to marshall all of the object's data into a single contiguous area
        in memory, and then store the contents of that buffer. This means that upon retrieval, the 
        contents of the buffer must be unmarshalled. To do these things,
        you must register three callback functions:
    </p>
          <div class="itemizedlist">
            <ul type="disc">
              <li>
                <p>
        <code class="function">typedef void (*ElemRstoreFunct)(T&amp; dest, const void *srcdata);</code>
	</p>
                <p>
        This callback is used to unmarshal an object, updating <span class="bold"><strong>dest</strong></span>
        using data found in <span class="bold"><strong>srcdata</strong></span>.  The data in
        <span class="bold"><strong>srcdata </strong></span> contains the chunk of memory into which the
        object was originally marshalled.  The default unmarshalling function simply performs a cast
        (for example, <code class="literal">dest = *((T*)srcdata)</code>), which assumes the  
        <span class="bold"><strong>srcdata</strong></span> simply points to the memory layout of the object.
	</p>
              </li>
              <li>
                <p>
        <code class="function">typedef size_t (*ElemSizeFunct)(const T&amp; elem);</code>
    </p>
                <p>
        This callback returns the size in bytes needed to store the  
        <span class="bold"><strong>elem</strong></span> object. By default this function
        simply uses <span class="bold"><strong>sizeof(elem)</strong></span> to determine 
        the size of <span class="bold"><strong>elem</strong></span>.
    </p>
              </li>
              <li>
                <p>
        <code class="function">typedef void (*ElemCopyFunct)(void *dest, const T&amp;elem);</code>
    </p>
                <p>
        This callback is used to arrange all data contained by <span class="bold"><strong>elem</strong></span>
        into the chunk of memory to which <span class="bold"><strong>dest</strong></span> refers.  The size of
        <span class="bold"><strong>dest</strong></span> is set by the <code class="function">ElemSizeFunct</code>
        function, discussed above.  The default marshalling function simply uses
        <code class="function">memcpy()</code> to copy <span class="bold"><strong>elem</strong></span> to 
        <span class="bold"><strong>dest</strong></span>.
    </p>
              </li>
            </ul>
          </div>
          <p>
    The <code class="function">DbstlElemTraits&lt;SMSMsg&gt;::instance()-&gt;set_size_function()</code>,
    <code class="function">set_copy_function()</code> and <code class="function">set_restore_function()</code> methods
    are used to register these callback functions.  If a callback is not registered, its
    default function is used.
</p>
          <p>
    By providing non-default implementations of the callbacks described here, you can store objects
    of varying length and/or objects which do not reside in a continuous memory chunk — for
    example, objects containing a pointer which refers another object, or a string, and so forth.
    As a result, containers/iterators can manage variable length objects in the same as they would
    manage objects that reside in continuous chunks of memory and are of identical size.
</p>
        </div>
        <div class="sect3" lang="en" xml:lang="en">
          <div class="titlepage">
            <div>
              <div>
                <h4 class="title"><a id="idp51445440"></a>Using a <code class="classname">DbstlDbt</code> wrapper object</h4>
              </div>
            </div>
          </div>
          <p>
    To use a <code class="classname">DbstlDbt</code> wrapper object to store objects of variable length, a
    <code class="literal">db_vector&lt;DbstlDbt&gt;</code> container is used to store complex objects in a
    <code class="classname">db_vector</code>. <code class="classname">DbstlDbt</code> derives from DB C++ API's
    <code class="classname">Dbt</code>class, but can manage its referenced memory properly and release it
    upon destruction. The memory referenced by <code class="classname">DbstlDbt</code> objects is required
    to be allocated using the <code class="function">malloc()</code>/<code class="function">realloc()</code> functions
    from the standard C library.
</p>
          <p>
    Note that the use of <code class="classname">DbstlDbt</code> wrapper class is not ideal. It exists only
    to allow raw bytes of no specific type to be stored in a container. 
</p>
          <p>
    To store an <code class="classname">SMSMsg</code> object into a <code class="literal">db_vector&lt;DbstlDbt&gt;</code>
    container using a <code class="classname">DbstlDbt</code> object:
</p>
          <div class="orderedlist">
            <ol type="1">
              <li>	
    Wrap the <code class="classname">SMSMSg</code> object into a <code class="classname">DbstlDbt</code> object, 
    then marshal the SMSMsg object properly into the memory chunk referenced by 
    <code class="methodname">DbstlDbt::data</code>.
</li>
              <li>	
    Store the <code class="classname">DbstlDbt</code> object into a <code class="literal">db_vector&lt;DbstlDbt&gt;</code> 
    container.  The bytes in the memory chunk referenced by the <code class="classname">DbstlDbt</code> object's
    <span class="bold"><strong>data</strong></span> member are stored in the
    <code class="literal">db_vector&lt;DbstlDbt&gt;</code> container.
</li>
              <li>	
    Reading from the container returns a <code class="classname">DbstlDbt</code> object whose 
    <span class="bold"><strong>data</strong></span> field points to the <code class="classname">SMSMsg</code> object 
    located in a continuous chunk of memory. The application needs to perform its own unmarshalling.
</li>
              <li>	
    The memory referenced by <code class="literal">DbstlDbt::data</code> is freed automatically, 
    and so the application should not attempt to free the memory.
</li>
            </ol>
          </div>
          <p>
    <code class="classname">ElementHolder</code> should not be used to store objects of a class because it
    doesn't support access to object members using <span class="bold"><strong>(*iter).member</strong></span>
    or  <span class="bold"><strong>iter-&gt;member</strong></span> expressions. In this case,  the default
    <code class="literal">ElementRef&lt;ddt&gt;</code> is used automatically.
</p>
          <p>
    <code class="classname">ElementRef</code> inherits from <code class="classname">ddt</code>, which allows
    <span class="bold"><strong>*iter</strong></span> to return the object stored in the container.
    (Technically it is an <code class="classname">ElementRef&lt;ddt&gt; object</code>, whose "base class"
    part is the object you stored). There are a few data members and member functions in
    <code class="classname">ElementRef</code>, which all start with <code class="literal">_DB_STL_</code>.  To avoid
    potential name clashes, applications should not use names prefixing <code class="literal">_DB_STL_</code>
    in classes whose instances may be stored into dbstl containers.
</p>
          <p>
    Example code demonstrating this feature can be found in the
    <code class="methodname">StlAdvancedFeaturesExample::arbitrary_object_storage</code> method.
</p>
        </div>
      </div>
      <div class="sect2" lang="en" xml:lang="en">
        <div class="titlepage">
          <div>
            <div>
              <h3 class="title"><a id="idp51477944"></a>Storing arbitrary sequences</h3>
            </div>
          </div>
        </div>
        <p>
    A sequence is a group of related objects, such as an array, a string, and so forth. 
    You can store sequences of any structure using dbstl, so long as you implement and register the
    proper callback functions. By using these callbacks, each object in the sequence can be a
    complex object with data members that are all not stored in a continuous memory chunk.
</p>
        <p>
    Note that when using these callbacks, when you retrieve a stored sequence from the database, the
    entire sequence will reside in a single continuous block of memory with the same layout as that
    constructed by your sequence copy function.
</p>
        <p>
    For example, given a type RGB:
</p>
        <pre class="programlisting">
struct RGB{char r, g, b, bright;};  </pre>
        <p>
    and an array of RGB objects, the following steps describe how to store an array into one
    key/data pair of a <code class="classname">db_map</code> container.
</p>
        <div class="orderedlist">
          <ol type="1">
            <li>
    Use a <code class="classname">db_map&lt;int, RGB *, ElementHolder&lt;RGB *&gt; &gt;</code> container.
</li>
            <li>
              <p>
        Define two functions. The first returns the number of objects in a sequence, the second that
        copies objects from a sequence to a defined destination in memory:
    </p>
              <pre class="programlisting">typedef size_t (*SequenceLenFunct)(const RGB*);  </pre>
              <p>
    and
</p>
              <pre class="programlisting"> typedef void (*SequenceCopyFunct)(RGB*dest, const RGB*src);  </pre>
            </li>
            <li>
Call DbstlElemTraits&lt;RGB&gt;::set_sequence_len_function()/set_sequence_copy_function()
to register them as callbacks. 
</li>
          </ol>
        </div>
        <div class="sect3" lang="en" xml:lang="en">
          <div class="titlepage">
            <div>
              <div>
                <h4 class="title"><a id="idp51487704"></a>The <code class="function">SequenceLenFunct</code> function</h4>
              </div>
            </div>
          </div>
          <pre class="programlisting">typedef size_t (*SequenceLenFunct)(const RGB*);  </pre>
          <p>
    A <code class="function">SequenceLenFunct</code> function returns the number of objects in a sequence. It
    is called when inserting into or reading from the database, so there must be enough information
    in the sequence itself to enable the <code class="function">SequenceLenFunct</code> function to tell how many
    objects the sequence contains.  The <code class="literal">char*</code> and <code class="literal">wchar_t*</code>
    strings use a <code class="literal">'\0'</code> special character to do this. For example, RGB(0, 0, 0, 0)
    could be used to denote the end of the sequence.  Note that for your implementation of this 
    callback, you are not required to use a
    trailing object with a special value like <code class="literal">'\0'</code> or 
    <code class="literal">RGB(0, 0, 0, 0)</code> to denote the end of the sequence. You are free to use
    what mechanism you want in your 
    <code class="function">SequenceLenFunct</code> function implementation to figure out the length of the sequence.
</p>
        </div>
        <div class="sect3" lang="en" xml:lang="en">
          <div class="titlepage">
            <div>
              <div>
                <h4 class="title"><a id="idp51519720"></a>The <code class="function">SequenceCopyFunct</code> function</h4>
              </div>
            </div>
          </div>
          <pre class="programlisting"> typedef void (*SequenceCopyFunct)(RGB*dest, const RGB*src); </pre>
          <p>
    <code class="function">SequenceCopyFunct</code> copies objects from the sequence 
    <span class="bold"><strong>src</strong></span> into memory chunk <span class="bold"><strong>dest</strong></span>. 
    If the objects in the sequence do not reside in a continuous memory chunk, this function must
    marshal each object in the sequence into the <span class="bold"><strong>dest</strong></span> memory chunk.
</p>
          <p>
    The sequence objects will reside in the continuous memory chunk referred to by <span class="bold"><strong>dest</strong></span>, which has been sized by <code class="classname">SequenceLenFunct</code>
    and <code class="classname">ElemSizeFunct</code> if available (which is when objects in the sequence are
    of varying lengths). <code class="classname">ElemSizeFunct</code> function is not needed in this example
    because <span class="bold"><strong>RGB</strong></span> is a simple fixed length type, the
    <code class="literal">sizeof()</code> operator is sufficient to return the size of the sequence.
</p>
        </div>
      </div>
      <div class="sect2" lang="en" xml:lang="en">
        <div class="titlepage">
          <div>
            <div>
              <h3 class="title"><a id="idp51524696"></a>Notes</h3>
            </div>
          </div>
        </div>
        <div class="itemizedlist">
          <ul type="disc">
            <li>
              <p>
            The get and set functions of this class are not protected by any mutexes.  When using
            multiple threads to access the function pointers, the callback functions must be
            registered to the singleton of this class before any retrieval of the callback function
            pointers. Isolation may also be required among multiple threads. The best way is to
            register all callback function pointers in a single thread before making use of the any
            containers.
        </p>
            </li>
            <li>
              <p>
            If objects in a sequence are not of identical sizes, or are not located in a consecutive
            chunk of memory, you also need to implement and register the
            <code class="function">DbstlElemTraits&lt;&gt;::ElemSizeFunct</code> callback function to measure
            the size of each object. When this function is registered, it is also used when
            allocating memory space.
        </p>
              <p>
            There is example code demonstrating the use this feature in the 
            <code class="methodname">StlAdvancedFeaturesExample::arbitray_sequence_storage()</code> method.
        </p>
            </li>
            <li>
              <p>
            A consequence of this dbstl feature is that you can not store a pointer value directly
            because dbstl will think it is a sequence head pointer. Instead, you need to convert the
            pointer into a <code class="literal">long</code> and then store it into a <code class="literal">long</code>
            container. And please note that pointer values are probably meaningless if the stored
            value is to be used across different application run times.
        </p>
            </li>
          </ul>
        </div>
      </div>
    </div>
    <div class="navfooter">
      <hr />
      <table width="100%" summary="Navigation footer">
        <tr>
          <td width="40%" align="left"><a accesskey="p" href="stl_primitive_rw.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_persistence.html">Next</a></td>
        </tr>
        <tr>
          <td width="40%" align="left" valign="top">Working with primitive types  </td>
          <td width="20%" align="center">
            <a accesskey="h" href="index.html">Home</a>
          </td>
          <td width="40%" align="right" valign="top"> Dbstl persistence</td>
        </tr>
      </table>
    </div>
  </body>
</html>