External File support

External file support is designed for efficient storage of large objects. An object is considered to be large if it is more than a third of the size of a page. Without external file support, large objects must be broken up into smaller pieces, and then reassembled and/or disassembled every time the record is read or updated. Berkeley DB external file support avoids this assembly/disassembly process by storing the large object in a special directory set aside for the purpose. The data itself is not kept in the database, nor is it placed into the in-memory cache.

external files can only be stored using the data portion of a key/data pair. They are supported only for Btree, Hash, and Heap databases, and only so long as the database is not configured for duplicate records, or duplicate sorted records. In addition, the DBT that you use to access the external file data cannot be configured as a partial DBT if you want to access the data using the external file’s streaming interface (introduced below).

Note that if the environment is transactionally-protected, then all access to the external file is also transactionally protected.

The external file threshold

The external file threshold is a positive integer, in bytes, which indicates how large an object must be before it is considered an external file. By default, the external file threshold for any given database is 0, which means that no object will ever be considered an external file. This means that the external file feature is not used by default for Berkeley DB databases.

In order to use the external file feature, you must set the external file threshold to a non-zero, positive integer value. You do this for a given database using the DB->set_blob_threshold() method. Note that this value must be set before you create the database. At any point after database creation time, this method is ignored.

In addition, if you are using an environment, you can change the default threshold for databases created in that environment to something other than 0 by using the DB_ENV->set_blob_threshold() method.

You can retrieve the external file threshold set for a database using the DB->get_blob_threshold(). You can retrieve the default external file threshold set for your environment using the DB_ENV->get_blob_threshold().

Creating external files

There are two ways to create an external file. Before you can use either mechanism, you must set the external file threshold to a non-zero positive integer value (see the previous section for details). Once the external file threshold has been set, you create an external file using one of the two following mechanisms:

  • Configure the DBT used to access the external file data (that is, the DBT used for the data portion of the record) with the DB_DBT_BLOB flag. This causes the data to be stored as an external file regardless of its size, so long as the database otherwise supports external files.

  • Alternatively, creating a data item with a size greater than the external file threshold will cause that data item to be automatically stored as an external file.

External file access

external files may be accessed in the same way as other DBT data, so long as the data itself will fit into memory. More likely, you will find it necessary to use the external file streaming API to read and write external file data. You open an external file stream using the DBC->db_stream() method, close it with the DB_STREAM->close() method, write to it using the the DB_STREAM->write() method, and read it using the DB_STREAM->read() method.

The following example code fragment can be found in your DB distribution at .../db/examples/c/ex_external_file.c.

  1. ...
  2. /* Some necessary variable declarations */
  3. DBC *dbc; /* Cursor handle */
  4. DB_ENV *dbenv; /* Environment handle */
  5. DB *dbp; /* Database handle */
  6. DB_STREAM *dbs; /* Stream handle */
  7. DB_TXN *txn; /* Transaction handle */
  8. DBT data, key; /* DBT handles */
  9. int ret;
  10. db_off_t size;
  11. ...
  12. /* Environment creation skipped for brevity's sake */
  13. ...
  14. /* Enable external files and set the size threshold. */
  15. if ((ret = dbenv->set_ext_file_threshold(dbenv, 1000, 0)) != 0) {
  16. dbenv->err(dbenv, ret, "set_ext_file_threshold");
  17. goto err;
  18. }
  19. ...
  20. /* Database and DBT creation skipped for brevity's sake */
  21. ...
  22. /*
  23. Access the external file using the DB_STREAM API.
  24. */
  25. if ((ret = dbenv->txn_begin(dbenv, NULL, &txn, 0)) != 0){
  26. dbenv->err(dbenv, ret, "txn");
  27. goto err;
  28. }
  29. if ((ret = dbp->cursor(dbp, txn, &dbc, 0)) != 0) {
  30. dbenv->err(dbenv, ret, "cursor");
  31. goto err;
  32. }
  33. /*
  34. * Set the cursor to an external file. Use DB_DBT_PARTIAL with
  35. * dlen == 0 to avoid getting any external file data.
  36. */
  37. data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL;
  38. data.dlen = 0;
  39. if ((ret = dbc->get(dbc, &key, &data, DB_FIRST)) != 0) {
  40. dbenv->err(dbenv, ret, "Not an external file");
  41. goto err;
  42. }
  43. data.flags = DB_DBT_USERMEM;
  44. /* Create a stream on the external file the cursor points to. */
  45. if ((ret = dbc->db_stream(dbc, &dbs, DB_STREAM_WRITE)) != 0) {
  46. dbenv->err(dbenv, 0, "Creating stream.");
  47. goto err;
  48. }
  49. /* Get the size of the external file. */
  50. if ((ret = dbs->size(dbs, &size, 0)) != 0) {
  51. dbenv->err(dbenv, 0, "Stream size.");
  52. goto err;
  53. }
  54. /* Read from the external file. */
  55. if ((ret = dbs->read(dbs, &data, 0, (u_int32_t)size, 0)) != 0) {
  56. dbenv->err(dbenv, 0, "Stream read.");
  57. goto err;
  58. }
  59. /* Write data to the external file, increasing its size. */
  60. if ((ret = dbs->write(dbs, &data, size/2, 0)) != 0) {
  61. dbenv->err(dbenv, 0, "Stream write.");
  62. goto err;
  63. }
  64. /* Close the stream. */
  65. if ((ret = dbs->close(dbs, 0)) != 0) {
  66. dbenv->err(dbenv, 0, "Stream close.");
  67. goto err;
  68. }
  69. dbs = NULL;
  70. dbc->close(dbc);
  71. dbc = NULL;
  72. txn->commit(txn, 0);
  73. txn = NULL;
  74. free(data.data);
  75. data.data = NULL;
  76. ...
  77. /* Handle clean up skipped. */

External file storage

external files are not stored in the normal database files on disk in the same way as is other data managed by DB. Instead, they are stored as binary files in a special directory set aside for the purpose.

If you are not using environments, this special external file directory is created relative to the current working directory from which your application is running. You can modify this default location using the DB->set_blob_dir() method, and retrieve the current external file directory using DB->get_blob_dir().

If you are using an environment, then by default the external file directory is created within the environment’s home directory. You can change this default location using DB_ENV->set_blob_dir() and retrieve the current default location using DB_ENV->get_blob_dir(). (Note that DB_ENV->get_blob_dir() can successfully retrieve the external file directory only if DB_ENV->set_blob_dir() was previously called.)

Note that because external files are stored outside of the Berkeley DB database files, they are not confined by the four gigabyte limit used for Berkeley DB key and data items. The external file size limit is system dependent. It can be the maximum value in bytes of a signed 32 bit integer (if the Berkeley DB-defined type db_off_t is four bytes in size), or a signed 64 bit integer (if db_off_t is eight bytes in size).

External Files and Replication

Replication supports external files without any special requirements. However, enabling external files in a replicated environment can result in long synchronization times between the client and master sites. To avoid this, execute a transaction checkpoint after updating or deleting one or more external file records.