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>Reverse BTree Splits</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="Getting Started with Berkeley DB Transaction Processing" />
10    <link rel="up" href="txnconcurrency.html" title="Chapter��4.��Concurrency" />
11    <link rel="prev" href="txnnowait.html" title="No Wait on Blocks" />
12    <link rel="next" href="filemanagement.html" title="Chapter��5.��Managing DB Files" />
13  </head>
14  <body>
15    <div class="navheader">
16      <table width="100%" summary="Navigation header">
17        <tr>
18          <th colspan="3" align="center">Reverse BTree Splits</th>
19        </tr>
20        <tr>
21          <td width="20%" align="left"><a accesskey="p" href="txnnowait.html">Prev</a>��</td>
22          <th width="60%" align="center">Chapter��4.��Concurrency</th>
23          <td width="20%" align="right">��<a accesskey="n" href="filemanagement.html">Next</a></td>
24        </tr>
25      </table>
26      <hr />
27    </div>
28    <div class="sect1" lang="en" xml:lang="en">
29      <div class="titlepage">
30        <div>
31          <div>
32            <h2 class="title" style="clear: both"><a id="reversesplit"></a>Reverse BTree Splits</h2>
33          </div>
34        </div>
35      </div>
36      <p>
37            If your application is using the Btree access method, and your
38            application is repeatedly deleting then adding records to your
39            database, then you might be able to reduce lock contention by
40            turning off reverse Btree splits.
41        </p>
42      <p>
43            As pages are emptied in a database, DB attempts to
44            delete empty pages in order to keep
45            the database as small as possible and minimize search time.
46            Moreover, when a page in the database fills
47            up, DB, of course, adds additional pages 
48            to make room for more data.
49        </p>
50      <p>
51            Adding and deleting pages in the database requires that the
52            writing thread lock the parent page. Consequently, as the
53            number of pages in your database diminishes, your application
54            will see increasingly more lock contention; the maximum level
55            of concurrency in a database of two pages is far smaller than
56            that in a database of 100 pages, because there are fewer pages
57            that can be locked.
58        </p>
59      <p>
60            Therefore, if you prevent the database from being reduced to a
61            minimum number of pages, you can improve your application's
62            concurrency throughput. Note, however, that you should do so
63            only if your application tends to delete and then add the same
64            data. If this is not the case, then preventing reverse Btree 
65            splits can harm your database search time.
66        </p>
67      <p>
68            To turn off reverse Btree splits, 
69                <span>
70                    provide the
71                    <code class="literal">DB_REVSPLITOFF</code> flag to the 
72                        <code class="methodname">DB-&gt;set_flags()</code>
73                        
74                    method.
75                </span>
76                
77        </p>
78      <p>
79            For example:
80        </p>
81      <pre class="programlisting">#include &lt;stdio.h&gt;
82#include &lt;stdlib.h&gt;
83
84#include "db.h"
85
86int
87main(void)
88{
89    int ret, ret_c;
90    u_int32_t db_flags, env_flags;
91    DB *dbp;
92    DB_ENV *envp;
93    const char *db_home_dir = "/tmp/myEnvironment";
94    const char *file_name = "mydb.db";
95    
96    dbp = NULL;
97    envp = NULL;
98
99    /* Open the environment */
100    ret = db_env_create(&amp;envp, 0);
101    if (ret != 0) {
102        fprintf(stderr, "Error creating environment handle: %s\n",
103            db_strerror(ret));
104        return (EXIT_FAILURE);
105    }
106                                                                                                                                  
107    env_flags = DB_CREATE      | 
108                DB_INIT_LOCK   | 
109                DB_INIT_LOG    | 
110                DB_INIT_TXN    |
111                DB_THREAD      | 
112                DB_INIT_MPOOL;
113
114    ret = envp-&gt;open(envp, db_home_dir, env_flags, 0);
115    if (ret != 0) {
116        fprintf(stderr, "Error opening environment: %s\n",
117            db_strerror(ret));
118        goto err;
119    }
120
121    /* Initialize the DB handle */
122    ret = db_create(&amp;dbp, envp, 0);
123    if (ret != 0) {
124        envp-&gt;err(envp, ret, "Database creation failed");
125        goto err;
126    }
127
128    /* Set btree reverse split to off */
129    ret = db-&gt;set_flags(&amp;db, DB_REVSPLITOFF);
130    if (ret != 0) {
131        envp-&gt;err(envp, ret, "Turning off Btree reverse split failed");
132        goto err;
133    }
134
135    db_flags = DB_CREATE | DB_AUTO_COMMIT;
136    ret = dbp-&gt;open(dbp,        /* Pointer to the database */
137                    NULL,       /* Txn pointer */
138                    file_name,  /* File name */
139                    NULL,       /* Logical db name */
140                    DB_BTREE,   /* Database type (using btree) */
141                    db_flags,   /* Open flags */
142                    0);         /* File mode. Using defaults */
143    if (ret != 0) {
144        envp-&gt;err(envp, ret, "Database '%s' open failed",
145            file_name);
146        goto err;
147    }
148
149
150err:
151    /* Close the database */
152    if (dbp != NULL) {
153        ret_c = dbp-&gt;close(dbp, 0);
154        if (ret_c != 0) {
155            envp-&gt;err(envp, ret_c, "Database close failed.");
156            ret = ret_c
157        }
158    }
159
160
161    /* Close the environment */
162    if (envp != NULL) {
163        ret_c = envp-&gt;close(envp, 0);
164        if (ret_c != 0) {
165            fprintf(stderr, "environment close failed: %s\n",
166                db_strerror(ret_c));
167            ret = ret_c;
168        }
169    }
170
171    return (ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
172} </pre>
173    </div>
174    <div class="navfooter">
175      <hr />
176      <table width="100%" summary="Navigation footer">
177        <tr>
178          <td width="40%" align="left"><a accesskey="p" href="txnnowait.html">Prev</a>��</td>
179          <td width="20%" align="center">
180            <a accesskey="u" href="txnconcurrency.html">Up</a>
181          </td>
182          <td width="40%" align="right">��<a accesskey="n" href="filemanagement.html">Next</a></td>
183        </tr>
184        <tr>
185          <td width="40%" align="left" valign="top">No Wait on Blocks��</td>
186          <td width="20%" align="center">
187            <a accesskey="h" href="index.html">Home</a>
188          </td>
189          <td width="40%" align="right" valign="top">��Chapter��5.��Managing DB Files</td>
190        </tr>
191      </table>
192    </div>
193  </body>
194</html>
195