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

« back to all changes in this revision

Viewing changes to libdb/docs/ref/transapp/cursor.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: Transactional cursors</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/read.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/nested.html"><img src="../../images/next.gif" alt="Next"></a>
15
 
</td></tr></table>
16
 
<p>
17
 
<h1 align=center>Transactional cursors</h1>
18
 
<p>Berkeley DB cursors may be used inside a transaction, exactly as any other
19
 
<a href="../../api_c/db_class.html">DB</a> method.  The enclosing transaction ID must be specified when
20
 
the cursor is created, but it does not then need to be further specified
21
 
on operations performed using the cursor.  One important point to
22
 
remember is that a cursor <b>must be closed</b> before the enclosing
23
 
transaction is committed or aborted.
24
 
<p>The following code fragment uses a cursor to store a new key in the cats
25
 
database with four associated data items.  The key is a name.  The data
26
 
items are a company name, an address, and a list of the breeds of cat
27
 
owned.  Each of the data entries is stored as a duplicate data item.
28
 
In this example, transactions are necessary to ensure that either all or none
29
 
of the data items appear in case of system or application failure.
30
 
<p><blockquote><pre>int
31
 
main(int argc, char *argv)
32
 
{
33
 
        extern char *optarg;
34
 
        extern int optind;
35
 
        DB *db_cats, *db_color, *db_fruit;
36
 
        DB_ENV *dbenv;
37
 
        pthread_t ptid;
38
 
        int ch;
39
 
<p>
40
 
        while ((ch = getopt(argc, argv, "")) != EOF)
41
 
                switch (ch) {
42
 
                case '?':
43
 
                default:
44
 
                        usage();
45
 
                }
46
 
        argc -= optind;
47
 
        argv += optind;
48
 
<p>
49
 
        env_dir_create();
50
 
        env_open(&dbenv);
51
 
<p>
52
 
        /* Open database: Key is fruit class; Data is specific type. */
53
 
        db_open(dbenv, &db_fruit, "fruit", 0);
54
 
<p>
55
 
        /* Open database: Key is a color; Data is an integer. */
56
 
        db_open(dbenv, &db_color, "color", 0);
57
 
<p>
58
 
        /*
59
 
         * Open database:
60
 
         *      Key is a name; Data is: company name, cat breeds.
61
 
         */
62
 
        db_open(dbenv, &db_cats, "cats", 1);
63
 
<p>
64
 
        add_fruit(dbenv, db_fruit, "apple", "yellow delicious");
65
 
<p>
66
 
        add_color(dbenv, db_color, "blue", 0);
67
 
        add_color(dbenv, db_color, "blue", 3);
68
 
<p>
69
 
<b>     add_cat(dbenv, db_cats,
70
 
                "Amy Adams",
71
 
                "Sleepycat Software",
72
 
                "abyssinian",
73
 
                "bengal",
74
 
                "chartreaux",
75
 
                NULL);</b>
76
 
<p>
77
 
        return (0);
78
 
}
79
 
<p>
80
 
<b>int
81
 
add_cat(DB_ENV *dbenv, DB *db, char *name, ...)
82
 
{
83
 
        va_list ap;
84
 
        DBC *dbc;
85
 
        DBT key, data;
86
 
        DB_TXN *tid;
87
 
        int fail, ret, t_ret;
88
 
        char *s;
89
 
<p>
90
 
        /* Initialization. */
91
 
        fail = 0;
92
 
<p>
93
 
        memset(&key, 0, sizeof(key));
94
 
        memset(&data, 0, sizeof(data));
95
 
        key.data = name;
96
 
        key.size = strlen(name);
97
 
<p>
98
 
retry:  /* Begin the transaction. */
99
 
        if ((ret = dbenv-&gt;txn_begin(dbenv, NULL, &tid, 0)) != 0) {
100
 
                dbenv-&gt;err(dbenv, ret, "DB_ENV-&gt;txn_begin");
101
 
                exit (1);
102
 
        }
103
 
<p>
104
 
        /* Delete any previously existing item. */
105
 
        switch (ret = db-&gt;del(db, tid, &key, 0)) {
106
 
        case 0:
107
 
        case DB_NOTFOUND:
108
 
                break;
109
 
        case DB_LOCK_DEADLOCK:
110
 
        default:
111
 
                /* Retry the operation. */
112
 
                if ((t_ret = tid-&gt;abort(tid)) != 0) {
113
 
                        dbenv-&gt;err(dbenv, t_ret, "DB_TXN-&gt;abort");
114
 
                        exit (1);
115
 
                }
116
 
                if (++fail == MAXIMUM_RETRY)
117
 
                        return (ret);
118
 
                goto retry;
119
 
        }
120
 
<p>
121
 
        /* Create a cursor. */
122
 
        if ((ret = db-&gt;cursor(db, tid, &dbc, 0)) != 0) {
123
 
                dbenv-&gt;err(dbenv, ret, "db-&gt;cursor");
124
 
                exit (1);
125
 
        }
126
 
<p>
127
 
        /* Append the items, in order. */
128
 
        va_start(ap, name);
129
 
        while ((s = va_arg(ap, char *)) != NULL) {
130
 
                data.data = s;
131
 
                data.size = strlen(s);
132
 
                switch (ret = dbc-&gt;c_put(dbc, &key, &data, DB_KEYLAST)) {
133
 
                case 0:
134
 
                        break;
135
 
                case DB_LOCK_DEADLOCK:
136
 
                default:
137
 
                        va_end(ap);
138
 
<p>
139
 
                        /* Retry the operation. */
140
 
                        if ((t_ret = dbc-&gt;c_close(dbc)) != 0) {
141
 
                                dbenv-&gt;err(
142
 
                                    dbenv, t_ret, "dbc-&gt;c_close");
143
 
                                exit (1);
144
 
                        }
145
 
                        if ((t_ret = tid-&gt;abort(tid)) != 0) {
146
 
                                dbenv-&gt;err(dbenv, t_ret, "DB_TXN-&gt;abort");
147
 
                                exit (1);
148
 
                        }
149
 
                        if (++fail == MAXIMUM_RETRY)
150
 
                                return (ret);
151
 
                        goto retry;
152
 
                }
153
 
        }
154
 
        va_end(ap);
155
 
<p>
156
 
        /* Success: commit the change. */
157
 
        if ((ret = dbc-&gt;c_close(dbc)) != 0) {
158
 
                dbenv-&gt;err(dbenv, ret, "dbc-&gt;c_close");
159
 
                exit (1);
160
 
        }
161
 
        if ((ret = tid-&gt;commit(tid, 0)) != 0) {
162
 
                dbenv-&gt;err(dbenv, ret, "DB_TXN-&gt;commit");
163
 
                exit (1);
164
 
        }
165
 
        return (0);
166
 
}</b></pre></blockquote>
167
 
<table width="100%"><tr><td><br></td><td align=right><a href="../../ref/transapp/read.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/nested.html"><img src="../../images/next.gif" alt="Next"></a>
168
 
</td></tr></table>
169
 
<p><font size=1><a href="http://www.sleepycat.com">Copyright Sleepycat Software</a></font>
170
 
</body>
171
 
</html>