~ubuntu-branches/ubuntu/maverick/evolution-data-server/maverick-proposed

« back to all changes in this revision

Viewing changes to libdb/docs/ref/transapp/data_open.html

  • Committer: Bazaar Package Importer
  • Author(s): Didier Roche
  • Date: 2010-05-17 17:02:06 UTC
  • mfrom: (1.1.79 upstream) (1.6.12 experimental)
  • Revision ID: james.westby@ubuntu.com-20100517170206-4ufr52vwrhh26yh0
Tags: 2.30.1-1ubuntu1
* Merge from debian experimental. Remaining change:
  (LP: #42199, #229669, #173703, #360344, #508494)
  + debian/control:
    - add Vcs-Bzr tag
    - don't use libgnome
    - Use Breaks instead of Conflicts against evolution 2.25 and earlier.
  + debian/evolution-data-server.install,
    debian/patches/45_libcamel_providers_version.patch:
    - use the upstream versioning, not a Debian-specific one 
  + debian/libedata-book1.2-dev.install, debian/libebackend-1.2-dev.install,
    debian/libcamel1.2-dev.install, debian/libedataserverui1.2-dev.install:
    - install html documentation
  + debian/rules:
    - don't build documentation it's shipped with the tarball

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<!--$Id$-->
2
 
<!--Copyright 1997-2002 by Sleepycat Software, Inc.-->
3
 
<!--All rights reserved.-->
4
 
<!--See the file LICENSE for redistribution information.-->
5
 
<html>
6
 
<head>
7
 
<title>Berkeley DB Reference Guide: Opening the databases</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++">
10
 
</head>
11
 
<body bgcolor=white>
12
 
<table width="100%"><tr valign=top>
13
 
<td><h3><dl><dt>Berkeley DB Reference Guide:<dd>Berkeley DB Transactional Data Store Applications</dl></h3></td>
14
 
<td align=right><a href="../../ref/transapp/env_open.html"><img src="../../images/prev.gif" alt="Prev"></a><a href="../../reftoc.html"><img src="../../images/ref.gif" alt="Ref"></a><a href="../../ref/transapp/put.html"><img src="../../images/next.gif" alt="Next"></a>
15
 
</td></tr></table>
16
 
<p>
17
 
<h1 align=center>Opening the databases</h1>
18
 
<p>Next, we open three databases ("color" and "fruit" and "cats"), in the
19
 
database environment.  Again, our <a href="../../api_c/db_class.html">DB</a> database handles are
20
 
declared to be free-threaded using the <a href="../../api_c/env_open.html#DB_THREAD">DB_THREAD</a> flag, and so
21
 
may be used by any number of threads we subsequently create.
22
 
<p><blockquote><pre>int
23
 
main(int argc, char *argv)
24
 
{
25
 
        extern char *optarg;
26
 
        extern int optind;
27
 
        DB *db_cats, *db_color, *db_fruit;
28
 
        DB_ENV *dbenv;
29
 
        int ch;
30
 
<p>
31
 
        while ((ch = getopt(argc, argv, "")) != EOF)
32
 
                switch (ch) {
33
 
                case '?':
34
 
                default:
35
 
                        usage();
36
 
                }
37
 
        argc -= optind;
38
 
        argv += optind;
39
 
<p>
40
 
        env_dir_create();
41
 
        env_open(&dbenv);
42
 
<p>
43
 
<b>     /* Open database: Key is fruit class; Data is specific type. */
44
 
        if (db_open(dbenv, &db_fruit, "fruit", 0))
45
 
                return (1);
46
 
<p>
47
 
        /* Open database: Key is a color; Data is an integer. */
48
 
        if (db_open(dbenv, &db_color, "color", 0))
49
 
                return (1);
50
 
<p>
51
 
        /*
52
 
         * Open database:
53
 
         *      Key is a name; Data is: company name, cat breeds.
54
 
         */
55
 
        if (db_open(dbenv, &db_cats, "cats", 1))
56
 
                return (1);</b>
57
 
<p>
58
 
        return (0);
59
 
}
60
 
<p>
61
 
<b>int
62
 
db_open(DB_ENV *dbenv, DB **dbp, char *name, int dups)
63
 
{
64
 
        DB *db;
65
 
        int ret;
66
 
<p>
67
 
        /* Create the database handle. */
68
 
        if ((ret = db_create(&db, dbenv, 0)) != 0) {
69
 
                dbenv-&gt;err(dbenv, ret, "db_create");
70
 
                return (1);
71
 
        }
72
 
<p>
73
 
        /* Optionally, turn on duplicate data items. */
74
 
        if (dups && (ret = db-&gt;set_flags(db, DB_DUP)) != 0) {
75
 
                (void)db-&gt;close(db, 0);
76
 
                dbenv-&gt;err(dbenv, ret, "db-&gt;set_flags: DB_DUP");
77
 
                return (1);
78
 
        }
79
 
<p>
80
 
        /*
81
 
         * Open a database in the environment:
82
 
         *      create if it doesn't exist
83
 
         *      free-threaded handle
84
 
         *      read/write owner only
85
 
         */
86
 
        if ((ret = db-&gt;open(db, NULL, name, NULL, DB_BTREE,
87
 
            DB_CREATE | DB_THREAD | DB_AUTO_COMMIT, S_IRUSR | S_IWUSR)) != 0) {
88
 
                (void)db-&gt;close(db, 0);
89
 
                dbenv-&gt;err(dbenv, ret, "db-&gt;open: %s", name);
90
 
                return (1);
91
 
        }
92
 
<p>
93
 
        *dbp = db;
94
 
        return (0);
95
 
}</b></pre></blockquote>
96
 
<p>After opening the database, we can use the <a href="../../utility/db_stat.html">db_stat</a> utility to
97
 
display information about a database we have created:
98
 
<p><blockquote><pre>prompt&gt; db_stat -h TXNAPP -d color
99
 
53162   Btree magic number.
100
 
8       Btree version number.
101
 
Flags:
102
 
2       Minimum keys per-page.
103
 
8192    Underlying database page size.
104
 
1       Number of levels in the tree.
105
 
0       Number of unique keys in the tree.
106
 
0       Number of data items in the tree.
107
 
0       Number of tree internal pages.
108
 
0       Number of bytes free in tree internal pages (0% ff).
109
 
1       Number of tree leaf pages.
110
 
8166    Number of bytes free in tree leaf pages (0.% ff).
111
 
0       Number of tree duplicate pages.
112
 
0       Number of bytes free in tree duplicate pages (0% ff).
113
 
0       Number of tree overflow pages.
114
 
0       Number of bytes free in tree overflow pages (0% ff).
115
 
0       Number of pages on the free list.</pre></blockquote>
116
 
<p>The database open must be enclosed within a transaction in order to be
117
 
recoverable.  The transaction will ensure that created files are
118
 
re-created in recovered environments (or do not appear at all).
119
 
Additional database operations or operations on other databases can be
120
 
included in the same transaction, of course.  In the simple case, where
121
 
the open is the only operation in the transaction, an application can
122
 
set the <a href="../../api_c/env_set_flags.html#DB_AUTO_COMMIT">DB_AUTO_COMMIT</a> flag instead of creating and managing
123
 
its own transaction handle.  The <a href="../../api_c/env_set_flags.html#DB_AUTO_COMMIT">DB_AUTO_COMMIT</a> flag will
124
 
internally wrap the operation in a transaction, simplifying application
125
 
code.
126
 
<p>The previous example is the simplest case of transaction protection for
127
 
database open.  Obviously, additional database operations can be done
128
 
in the scope of the same transaction.  For example, an application
129
 
maintaining a list of the databases in a database environment in a
130
 
well-known file might include an update of the list in the same
131
 
transaction in which the database is created.  Or, an application might
132
 
create both a primary and secondary database in a single transaction.
133
 
<p><a href="../../api_c/db_class.html">DB</a> handles that will later be used for transactionally protected
134
 
operations must be opened within a transaction.  Specifying a
135
 
transaction handle to operations using handles not opened within a
136
 
transaction will return an error.  Similarly, not specifying a
137
 
transaction handle to operations using handles that were opened within
138
 
a transaction will also return an error.
139
 
<table width="100%"><tr><td><br></td><td align=right><a href="../../ref/transapp/env_open.html"><img src="../../images/prev.gif" alt="Prev"></a><a href="../../reftoc.html"><img src="../../images/ref.gif" alt="Ref"></a><a href="../../ref/transapp/put.html"><img src="../../images/next.gif" alt="Next"></a>
140
 
</td></tr></table>
141
 
<p><font size=1><a href="http://www.sleepycat.com">Copyright Sleepycat Software</a></font>
142
 
</body>
143
 
</html>