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                        
74                        <tt class="methodname">Db::set_flags()</tt>
75                    method.
76                </span>
77                
78        </p>
79      <p>
80            For example:
81        </p>
82      <pre class="programlisting">#include "db_cxx.h"
83
84...
85                                                                                                                                  
86int main(void)
87{
88    u_int32_t env_flags = DB_CREATE     |  // If the environment does not
89                                           // exist, create it.
90                          DB_INIT_LOCK  |  // Initialize locking
91                          DB_INIT_LOG   |  // Initialize locking
92                          DB_INIT_MPOOL |  // Initialize the cache
93                          DB_THREAD     |  // Free-thread the env handle
94                          DB_INIT_TXN;     // Initialize transactions
95
96    u_int32_t db_flags = DB_CREATE | DB_AUTO_COMMIT;
97    Db *dbp = NULL;
98    const char *file_name = "mydb.db";
99                                                                                                                                  
100    std::string envHome("/export1/testEnv");
101    DbEnv myEnv(0);
102
103    try {
104
105        myEnv.open(envHome.c_str(), env_flags, 0);
106        dbp = new Db(&amp;myEnv, 0);
107
108        // Turn off BTree reverse split.
109        dbp=&gt;set_flags(DB_REVSPLITOFF);
110
111        dbp-&gt;open(dbp,        // Pointer to the database 
112                  NULL,       // Txn pointer 
113                  file_name,  // File name 
114                  NULL,       // Logical db name 
115                  DB_BTREE,   // Database type (using btree)
116                  db_flags,   // Open flags 
117                  0);         // File mode. Using defaults
118
119    } catch(DbException &amp;e) {
120        std::cerr &lt;&lt; "Error opening database and environment: "
121                  &lt;&lt; file_name &lt;&lt; ", " &lt;&lt; envHome &lt;&lt; std::endl;
122        std::cerr &lt;&lt; e.what() &lt;&lt; std::endl;
123    }
124
125    try {
126        dbp-&gt;close(dbp, 0);
127        myEnv.close(0);
128    } catch(DbException &amp;e) {
129        std::cerr &lt;&lt; "Error closing database and environment: "
130                  &lt;&lt; file_name &lt;&lt; ", " &lt;&lt; envHome &lt;&lt; std::endl;
131        std::cerr &lt;&lt; e.what() &lt;&lt; std::endl;
132        return (EXIT_FAILURE);
133    }
134
135    return (EXIT_SUCCESS);
136} </pre>
137    </div>
138    <div class="navfooter">
139      <hr />
140      <table width="100%" summary="Navigation footer">
141        <tr>
142          <td width="40%" align="left"><a accesskey="p" href="txnnowait.html">Prev</a> </td>
143          <td width="20%" align="center">
144            <a accesskey="u" href="txnconcurrency.html">Up</a>
145          </td>
146          <td width="40%" align="right"> <a accesskey="n" href="filemanagement.html">Next</a></td>
147        </tr>
148        <tr>
149          <td width="40%" align="left" valign="top">No Wait on Blocks </td>
150          <td width="20%" align="center">
151            <a accesskey="h" href="index.html">Home</a>
152          </td>
153          <td width="40%" align="right" valign="top"> Chapter 5. Managing DB Files</td>
154        </tr>
155      </table>
156    </div>
157  </body>
158</html>
159