2
<!--Copyright 1997-2002 by Sleepycat Software, Inc.-->
3
<!--All rights reserved.-->
4
<!--See the file LICENSE for redistribution information.-->
7
<title>Berkeley DB Reference Guide: Storing C/C++ structures/objects</title>
8
<meta name="description" content="Berkeley DB: An embedded database programmatic toolkit.">
9
<meta name="keywords" content="embedded,database,programmatic,toolkit,b+tree,btree,hash,hashing,transaction,transactions,locking,logging,access method,access methods,java,C,C++">
12
<table width="100%"><tr valign=top>
13
<td><h3><dl><dt>Berkeley DB Reference Guide:<dd>Access Methods</dl></h3></td>
14
<td align=right><a href="../../ref/am_misc/partial.html"><img src="../../images/prev.gif" alt="Prev"></a><a href="../../reftoc.html"><img src="../../images/ref.gif" alt="Ref"></a><a href="../../ref/am_misc/perm.html"><img src="../../images/next.gif" alt="Next"></a>
17
<h1 align=center>Storing C/C++ structures/objects</h1>
18
<p>Berkeley DB can store any kind of data, that is, it is entirely 8-bit clean.
19
How you use this depends, to some extent, on the application language
20
you are using. In the C/C++ languages, there are a couple of different
21
ways to store structures and objects.
22
<p>First, you can do some form of run-length encoding and copy your
23
structure into another piece of memory before storing it:
24
<p><blockquote><pre>struct {
30
u_int8_t *p, data_buffer[1024];
33
len = strlen(info.data1);
34
memcpy(p, &len, sizeof(len));
36
memcpy(p, info.data1, len);
38
memcpy(p, &info.data2, sizeof(info.data2));
39
p += sizeof(info.data2);
40
...</pre></blockquote>
41
<p>and so on, until all the fields of the structure have been loaded into
42
the byte array. If you want more examples, see the Berkeley DB logging
43
routines (for example, btree/btree_auto.c:__bam_split_log()). This
44
technique is generally known as "marshalling". If you use this
45
technique, you must then un-marshall the data when you read it back:
46
<p><blockquote><pre>struct {
55
memcpy(&len, p, sizeof(len));
57
info.data1 = malloc(len);
58
memcpy(info.data1, p, len);
60
memcpy(&info.data2, p, sizeof(info.data2));
61
p += sizeof(info.data2);
62
...</pre></blockquote>
64
<p>The second way to solve this problem only works if you have just one
65
variable length field in the structure. In that case, you can declare
66
the structure as follows:
67
<p><blockquote><pre>struct {
70
} info;</pre></blockquote>
71
<p>Then, let's say you have a string you want to store in this structure.
72
When you allocate the structure, you allocate it as:
73
<p><blockquote><pre>malloc(sizeof(struct info) + strlen(string));</pre></blockquote>
74
<p>Since the allocated memory is contiguous, you can the initialize the
76
<p><blockquote><pre>info.a = 1;
79
memcpy(&info.buf[0], string, strlen(string));</pre></blockquote>
80
<p>and give it to Berkeley DB to store, with a length of:
81
<p><blockquote><pre>sizeof(struct info) + strlen(string);</pre></blockquote>
82
<p>In this case, the structure can be copied out of the database and used
83
without any additional work.
84
<table width="100%"><tr><td><br></td><td align=right><a href="../../ref/am_misc/partial.html"><img src="../../images/prev.gif" alt="Prev"></a><a href="../../reftoc.html"><img src="../../images/ref.gif" alt="Ref"></a><a href="../../ref/am_misc/perm.html"><img src="../../images/next.gif" alt="Next"></a>
86
<p><font size=1><a href="http://www.sleepycat.com">Copyright Sleepycat Software</a></font>