| 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>Partitioning databases</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="Berkeley DB Programmer's Reference Guide" /> |
| 10 | <link rel="up" href="am.html" title="Chapter 3. Access Method Operations" /> |
| 11 | <link rel="prev" href="am_opensub.html" title="Opening multiple databases in a single file" /> |
| 12 | <link rel="next" href="am_get.html" title="Retrieving records" /> |
| 13 | </head> |
| 14 | <body> |
| 15 | <div xmlns="" class="navheader"> |
| 16 | <div class="libver"> |
| 17 | <p>Library Version 11.2.5.3</p> |
| 18 | </div> |
| 19 | <table width="100%" summary="Navigation header"> |
| 20 | <tr> |
| 21 | <th colspan="3" align="center">Partitioning databases</th> |
| 22 | </tr> |
| 23 | <tr> |
| 24 | <td width="20%" align="left"><a accesskey="p" href="am_opensub.html">Prev</a> </td> |
| 25 | <th width="60%" align="center">Chapter 3. |
| 26 | Access Method Operations |
| 27 | </th> |
| 28 | <td width="20%" align="right"> <a accesskey="n" href="am_get.html">Next</a></td> |
| 29 | </tr> |
| 30 | </table> |
| 31 | <hr /> |
| 32 | </div> |
| 33 | <div class="sect1" lang="en" xml:lang="en"> |
| 34 | <div class="titlepage"> |
| 35 | <div> |
| 36 | <div> |
| 37 | <h2 class="title" style="clear: both"><a id="am_partition"></a>Partitioning databases</h2> |
| 38 | </div> |
| 39 | </div> |
| 40 | </div> |
| 41 | <div class="toc"> |
| 42 | <dl> |
| 43 | <dt> |
| 44 | <span class="sect2"> |
| 45 | <a href="am_partition.html#am_partition_keys">Specifying partition keys</a> |
| 46 | </span> |
| 47 | </dt> |
| 48 | <dt> |
| 49 | <span class="sect2"> |
| 50 | <a href="am_partition.html#am_partition_function">Partitioning callback</a> |
| 51 | </span> |
| 52 | </dt> |
| 53 | <dt> |
| 54 | <span class="sect2"> |
| 55 | <a href="am_partition.html#partition_file_placement">Placing partition files</a> |
| 56 | </span> |
| 57 | </dt> |
| 58 | </dl> |
| 59 | </div> |
| 60 | <p> |
| 61 | You can improve concurrency on your database reads and writes by |
| 62 | splitting access to a single database into multiple databases. This |
| 63 | helps to avoid contention for internal database pages, as well as |
| 64 | allowing you to spread your databases across multiple disks, |
| 65 | which can help to improve disk I/O. |
| 66 | </p> |
| 67 | <div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"> |
| 68 | <h3 class="title">Note</h3> |
| 69 | <p> |
| 70 | Database partitions are not supported by the C# and Java APIs at |
| 71 | this time. |
| 72 | </p> |
| 73 | </div> |
| 74 | <p> |
| 75 | While you can manually do this by creating and using more than one |
| 76 | database for your data, DB is capable of partitioning your |
| 77 | database for you. When you use DB's built-in database partitioning |
| 78 | feature, your access to your data is performed in exactly the same way |
| 79 | as if you were only using one database; all the work of knowing which |
| 80 | database to use to access a particular record is handled for you under |
| 81 | the hood. |
| 82 | </p> |
| 83 | <p> |
| 84 | Only the BTree and Hash access methods are supported for partitioned |
| 85 | databases. |
| 86 | </p> |
| 87 | <p> |
| 88 | You indicate that you want your database to be partitioned by calling |
| 89 | <a href="../api_reference/C/dbset_partition.html" class="olink">DB->set_partition()</a> before opening your database the first time. You can |
| 90 | indicate the directory in which each partition is contained using the |
| 91 | <a href="../api_reference/C/dbset_partition_dirs.html" class="olink">DB->set_partition_dirs()</a> method. |
| 92 | </p> |
| 93 | <p> |
| 94 | Once you have partitioned a database, you cannot change your |
| 95 | partitioning scheme. |
| 96 | </p> |
| 97 | <p> |
| 98 | There are two ways to indicate what key/data pairs should go on which |
| 99 | partition. The first is by specifying an array of <a href="../api_reference/C/dbt.html" class="olink">DBT</a>s that indicate |
| 100 | the minimum key value for a given partition. The second is by providing |
| 101 | a callback that returns the number of the partition on which a specified |
| 102 | key is placed. |
| 103 | </p> |
| 104 | <div class="sect2" lang="en" xml:lang="en"> |
| 105 | <div class="titlepage"> |
| 106 | <div> |
| 107 | <div> |
| 108 | <h3 class="title"><a id="am_partition_keys"></a>Specifying partition keys</h3> |
| 109 | </div> |
| 110 | </div> |
| 111 | </div> |
| 112 | <p> |
| 113 | For simple cases, you can partition your database by providing |
| 114 | an array of <a href="../api_reference/C/dbt.html" class="olink">DBT</a>s, each element of which provides the minimum |
| 115 | key value to be placed on a partition. There must be one fewer |
| 116 | elements in this array than you have partitions. The first |
| 117 | element of the array indicates the minimum key value for the |
| 118 | second partition in your database. Key values that are less |
| 119 | than the first key value provided in this array are placed on |
| 120 | the first partition (partition 0). |
| 121 | </p> |
| 122 | <div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"> |
| 123 | <h3 class="title">Note</h3> |
| 124 | <p> |
| 125 | You can use partition keys only if you are using the Btree |
| 126 | access method. |
| 127 | </p> |
| 128 | </div> |
| 129 | <p> |
| 130 | For example, suppose you had a database of fruit, and you want |
| 131 | three partitions for your database. Then you need a <a href="../api_reference/C/dbt.html" class="olink">DBT</a> array |
| 132 | of size two. The first element in this array indicates the |
| 133 | minimum keys that should be placed on partition 1. The second |
| 134 | element in this array indicates the minimum key value placed on |
| 135 | partition 2. Keys that compare less than the first <a href="../api_reference/C/dbt.html" class="olink">DBT</a> in the |
| 136 | array are placed on partition 0. |
| 137 | </p> |
| 138 | <p> |
| 139 | All comparisons are performed according to the lexicographic |
| 140 | comparison used by your platform. |
| 141 | </p> |
| 142 | <p> |
| 143 | For example, suppose you want all fruits whose names begin |
| 144 | with: |
| 145 | </p> |
| 146 | <div class="itemizedlist"> |
| 147 | <ul type="disc"> |
| 148 | <li> |
| 149 | <p> |
| 150 | 'a' - 'f' to go on partition 0 |
| 151 | </p> |
| 152 | </li> |
| 153 | <li> |
| 154 | <p> |
| 155 | 'g' - 'p' to go on partition 1 |
| 156 | </p> |
| 157 | </li> |
| 158 | <li> |
| 159 | <p> |
| 160 | 'q' - 'z' to go on partition 2. |
| 161 | </p> |
| 162 | </li> |
| 163 | </ul> |
| 164 | </div> |
| 165 | <p> |
| 166 | Then you would accomplish this with the following code |
| 167 | fragment: |
| 168 | </p> |
| 169 | <div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"> |
| 170 | <h3 class="title">Note</h3> |
| 171 | <p> |
| 172 | The <a href="../api_reference/C/dbset_partition.html" class="olink">DB->set_partition()</a> partition callback parameter must |
| 173 | be <code class="literal">NULL</code> if you are using an array of |
| 174 | <a href="../api_reference/C/dbt.html" class="olink">DBT</a>s to partition your database. |
| 175 | </p> |
| 176 | </div> |
| 177 | <a id="prog_am10"></a> |
| 178 | <pre class="programlisting">DB *dbp = NULL; |
| 179 | DB_ENV *envp = NULL; |
| 180 | DBT partKeys[2]; |
| 181 | u_int32_t db_flags; |
| 182 | const char *file_name = "mydb.db"; |
| 183 | int ret; |
| 184 | |
| 185 | ... |
| 186 | |
| 187 | /* Skipping environment open to shorten this example */ |
| 188 | |
| 189 | |
| 190 | /* Initialize the DB handle */ |
| 191 | ret = db_create(&dbp, envp, 0); |
| 192 | if (ret != 0) { |
| 193 | fprintf(stderr, "%s\n", db_strerror(ret)); |
| 194 | return (EXIT_FAILURE); |
| 195 | } |
| 196 | |
| 197 | /* Setup the partition keys */ |
| 198 | memset(&partKeys[0], 0, sizeof(DBT)); |
| 199 | partKeys[0].data = "g"; |
| 200 | partKeys[0].size = sizeof("g") - 1; |
| 201 | |
| 202 | memset(&partKeys[1], 0, sizeof(DBT)); |
| 203 | partKeys[1].data = "q"; |
| 204 | partKeys[1].size = sizeof("q") - 1; |
| 205 | |
| 206 | dbp->set_partition(dbp, 3, partKeys, NULL); |
| 207 | |
| 208 | /* Now open the database */ |
| 209 | db_flags = DB_CREATE; /* Allow database creation */ |
| 210 | |
| 211 | ret = dbp->open(dbp, /* Pointer to the database */ |
| 212 | NULL, /* Txn pointer */ |
| 213 | file_name, /* File name */ |
| 214 | NULL, /* Logical db name */ |
| 215 | DB_BTREE, /* Database type (using btree) */ |
| 216 | db_flags, /* Open flags */ |
| 217 | 0); /* File mode. Using defaults */ |
| 218 | if (ret != 0) { |
| 219 | dbp->err(dbp, ret, "Database '%s' open failed", |
| 220 | file_name); |
| 221 | return (EXIT_FAILURE); |
| 222 | } </pre> |
| 223 | </div> |
| 224 | <div class="sect2" lang="en" xml:lang="en"> |
| 225 | <div class="titlepage"> |
| 226 | <div> |
| 227 | <div> |
| 228 | <h3 class="title"><a id="am_partition_function"></a>Partitioning callback</h3> |
| 229 | </div> |
| 230 | </div> |
| 231 | </div> |
| 232 | <p> |
| 233 | In some cases, a simple lexicographical comparison of key data |
| 234 | will not sufficiently support a partitioning scheme. For |
| 235 | those situations, you should write a partitioning function. |
| 236 | This function accepts a pointer to the <a href="../api_reference/C/db.html" class="olink">DB</a> and the <a href="../api_reference/C/dbt.html" class="olink">DBT</a>, and |
| 237 | it returns the number of the partition on which the key |
| 238 | belongs. |
| 239 | </p> |
| 240 | <p> |
| 241 | Note that <a href="../api_reference/C/db.html" class="olink">DB</a> actually places the key on the partition |
| 242 | calculated by: |
| 243 | </p> |
| 244 | <pre class="programlisting">returned_partition modulo number_of_partitions</pre> |
| 245 | <p> |
| 246 | Also, remember that if you use a partitioning function when you |
| 247 | create your database, then you must use the same partitioning |
| 248 | function every time you open that database in the future. |
| 249 | </p> |
| 250 | <p> |
| 251 | The following code fragment illustrates a partition callback: |
| 252 | </p> |
| 253 | <a id="prog_am11"></a> |
| 254 | <pre class="programlisting">u_int32_t db_partition_fn(DB *db, DBT *key) { |
| 255 | char *key_data; |
| 256 | u_int32_t ret_number; |
| 257 | /* Obtain your key data, unpacking it as necessary |
| 258 | * Here, we do the very simple thing just for illustrative purposes. |
| 259 | */ |
| 260 | |
| 261 | key_data = (char *)key->data; |
| 262 | |
| 263 | /* Here you would perform whatever comparison you require to determine |
| 264 | * what partition the key belongs on. If you return either 0 or the |
| 265 | * number of partitions in the database, the key is placed in the first |
| 266 | * database partition. Else, it is placed on: |
| 267 | * |
| 268 | * returned_number mod number_of_partitions |
| 269 | */ |
| 270 | |
| 271 | ret_number = 0; |
| 272 | |
| 273 | return ret_number; |
| 274 | } </pre> |
| 275 | <p> |
| 276 | You then cause your partition callback to be used by providing it |
| 277 | to the <a href="../api_reference/C/dbset_partition.html" class="olink">DB->set_partition()</a> method, as illustrated by the following |
| 278 | code fragment. |
| 279 | </p> |
| 280 | <div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"> |
| 281 | <h3 class="title">Note</h3> |
| 282 | <p> |
| 283 | The <a href="../api_reference/C/dbset_partition.html" class="olink">DB->set_partition()</a> <a href="../api_reference/C/dbt.html" class="olink">DBT</a> array parameter must |
| 284 | be <code class="literal">NULL</code> if you are using a partition |
| 285 | call back to partition your database. |
| 286 | </p> |
| 287 | </div> |
| 288 | <a id="prog_am12"></a> |
| 289 | <pre class="programlisting">DB *dbp = NULL; |
| 290 | DB_ENV *envp = NULL; |
| 291 | u_int32_t db_flags; |
| 292 | const char *file_name = "mydb.db"; |
| 293 | int ret; |
| 294 | |
| 295 | ... |
| 296 | |
| 297 | /* Skipping environment open to shorten this example */ |
| 298 | |
| 299 | |
| 300 | /* Initialize the DB handle */ |
| 301 | ret = db_create(&dbp, envp, 0); |
| 302 | if (ret != 0) { |
| 303 | fprintf(stderr, "%s\n", db_strerror(ret)); |
| 304 | return (EXIT_FAILURE); |
| 305 | } |
| 306 | |
| 307 | dbp->set_partition(dbp, 3, NULL, db_partition_fn); |
| 308 | |
| 309 | /* Now open the database */ |
| 310 | db_flags = DB_CREATE; /* Allow database creation */ |
| 311 | |
| 312 | ret = dbp->open(dbp, /* Pointer to the database */ |
| 313 | NULL, /* Txn pointer */ |
| 314 | file_name, /* File name */ |
| 315 | NULL, /* Logical db name */ |
| 316 | DB_BTREE, /* Database type (using btree) */ |
| 317 | db_flags, /* Open flags */ |
| 318 | 0); /* File mode. Using defaults */ |
| 319 | if (ret != 0) { |
| 320 | dbp->err(dbp, ret, "Database '%s' open failed", |
| 321 | file_name); |
| 322 | return (EXIT_FAILURE); |
| 323 | } </pre> |
| 324 | </div> |
| 325 | <div class="sect2" lang="en" xml:lang="en"> |
| 326 | <div class="titlepage"> |
| 327 | <div> |
| 328 | <div> |
| 329 | <h3 class="title"><a id="partition_file_placement"></a>Placing partition files</h3> |
| 330 | </div> |
| 331 | </div> |
| 332 | </div> |
| 333 | <p> |
| 334 | When you partition a database, a database file is created on |
| 335 | disk in the same way as if you were not partitioning the |
| 336 | database. That is, this file uses the name you provide to the |
| 337 | <a href="../api_reference/C/dbopen.html" class="olink">DB->open()</a> <code class="literal">file</code> parameter. |
| 338 | </p> |
| 339 | <p> |
| 340 | However, DB then also creates a series of database files on |
| 341 | disk, one for each partition that you want to use. These |
| 342 | partition files share the same name as the database file name, |
| 343 | but are also number sequentially. So if you create a database |
| 344 | named <code class="filename">mydb.db</code>, and you create 3 partitions |
| 345 | for it, then you will see the following database files on disk: |
| 346 | </p> |
| 347 | <pre class="programlisting"> mydb.db |
| 348 | __dbp.mydb.db.000 |
| 349 | __dbp.mydb.db.001 |
| 350 | __dbp.mydb.db.002 </pre> |
| 351 | <p> |
| 352 | All of the database's contents go into the numbered database |
| 353 | files. You can cause these files to be placed in different |
| 354 | directories (and, hence, different disk partitions or even |
| 355 | disks) by using the <a href="../api_reference/C/dbset_partition_dirs.html" class="olink">DB->set_partition_dirs()</a> method. |
| 356 | </p> |
| 357 | <p> |
| 358 | <a href="../api_reference/C/dbset_partition_dirs.html" class="olink">DB->set_partition_dirs()</a> takes a NULL-terminated array of |
| 359 | strings, each one of which should represent an existing |
| 360 | filesystem directory. |
| 361 | </p> |
| 362 | <p> |
| 363 | If you are using an environment, the directories specified |
| 364 | using <a href="../api_reference/C/dbset_partition_dirs.html" class="olink">DB->set_partition_dirs()</a> must also be included in the |
| 365 | environment list specified by <a href="../api_reference/C/envadd_data_dir.html" class="olink">DB_ENV->add_data_dir()</a>. |
| 366 | </p> |
| 367 | <p> |
| 368 | If you are not using an environment, then the the directories |
| 369 | specified to <a href="../api_reference/C/dbset_partition_dirs.html" class="olink">DB->set_partition_dirs()</a> can be either complete |
| 370 | paths to currently existing directories, or paths relative to |
| 371 | the application's current working directory. |
| 372 | </p> |
| 373 | <p> |
| 374 | Ideally, you will provide <a href="../api_reference/C/dbset_partition_dirs.html" class="olink">DB->set_partition_dirs()</a> with an array |
| 375 | that is the same size as the number of partitions you are |
| 376 | creating for your database. Partition files are then placed |
| 377 | according to the order that directories are contained in the |
| 378 | array; partition 0 is placed in directory_array[0], partition 1 |
| 379 | in directory_array[1], and so forth. However, if you provide an |
| 380 | array of directories that is smaller than the number of |
| 381 | database partitions, then the directories are used on a |
| 382 | round-robin fashion. |
| 383 | </p> |
| 384 | <p> |
| 385 | You must call <a href="../api_reference/C/dbset_partition_dirs.html" class="olink">DB->set_partition_dirs()</a> before you create your |
| 386 | database, and before you open your database each time |
| 387 | thereafter. The array provided to <a href="../api_reference/C/dbset_partition_dirs.html" class="olink">DB->set_partition_dirs()</a> must not |
| 388 | change after the database has been created. |
| 389 | </p> |
| 390 | </div> |
| 391 | </div> |
| 392 | <div class="navfooter"> |
| 393 | <hr /> |
| 394 | <table width="100%" summary="Navigation footer"> |
| 395 | <tr> |
| 396 | <td width="40%" align="left"><a accesskey="p" href="am_opensub.html">Prev</a> </td> |
| 397 | <td width="20%" align="center"> |
| 398 | <a accesskey="u" href="am.html">Up</a> |
| 399 | </td> |
| 400 | <td width="40%" align="right"> <a accesskey="n" href="am_get.html">Next</a></td> |
| 401 | </tr> |
| 402 | <tr> |
| 403 | <td width="40%" align="left" valign="top">Opening multiple databases in a single file </td> |
| 404 | <td width="20%" align="center"> |
| 405 | <a accesskey="h" href="index.html">Home</a> |
| 406 | </td> |
| 407 | <td width="40%" align="right" valign="top"> Retrieving records</td> |
| 408 | </tr> |
| 409 | </table> |
| 410 | </div> |
| 411 | </body> |
| 412 | </html> |