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