Chapter 4. The Dbt Handle
Prev |  | Next

## Chapter 4. The Dbt Handle

    #include <db_cxx.h>

    class Dbt {
    public:
            Dbt(void *data, size_t size);
            Dbt();
            Dbt(const Dbt &);
            Dbt &operator = (const Dbt &);
            ~Dbt();

            void *get_data() const;
            void set_data(void *);

            u_int32_t get_size() const;
            void set_size(u_int32_t);

            u_int32_t get_ulen() const;
            void set_ulen(u_int32_t);

            u_int32_t get_dlen() const;
            void set_dlen(u_int32_t);

            u_int32_t get_doff() const;
            void set_doff(u_int32_t);

            u_int32_t get_flags() const;
            void set_flags(u_int32_t);

            DBT *Dbt::get_DBT();
            const DBT *Dbt::get_const_DBT() const;
            static Dbt *Dbt::get_Dbt(DBT *dbt);
            static const Dbt *Dbt::get_const_Dbt(const DBT *dbt);
    };

The `Dbt` class is used to encode key and data items in a Berkeley DB database.

Storage and retrieval for the Db access methods are based on key/data pairs. Both key and data items are represented by `Dbt` objects. Key and data byte strings may refer to strings of zero length up to strings of essentially unlimited length. See Database limits for more information.

In the case when the flags structure element is set to 0, when the application is providing Berkeley DB a key or data item to store into the database, Berkeley DB expects the data object to point to a byte string of size bytes. When returning a key/data item to the application, Berkeley DB will store into the data object a pointer to a byte string of size bytes, and the memory to which the pointer refers will be allocated and managed by Berkeley DB. Note that using the default flags for returned `Dbt`s is only compatible with single threaded usage of Berkeley DB.

Access to `Dbt` objects is not re-entrant. In particular, if multiple threads simultaneously access the same Dbt object using Db API calls, the results are undefined, and may result in a crash. One easy way to avoid problems is to use `Dbt` objects that are constructed as stack variables.

Each `Dbt` object has an associated `DBT` struct, which is used by the underlying implementation of Berkeley DB and its C-language API. The `Dbt::get_DBT()` method returns a pointer to this struct. Given a const `Dbt` object, `Dbt::get_const_DBT()` returns a const pointer to the same struct.

Given a `DBT` struct, the `Dbt::get_Dbt()` method returns the corresponding `Dbt` object, if there is one. If the `DBT` object was not associated with a `Dbt` (that is, it was not returned from a call to `Dbt::get_DBT()`), then the result of `Dbt::get_Dbt()` is undefined. Given a const `DBT` struct, `Dbt::get_const_Dbt()` returns the associated const `Dbt` object, if there is one.

These methods may be useful for Berkeley DB applications including both C and C++ language software. It should not be necessary to use these calls in a purely C++ application.

- `Dbt::set_data(void *data)`

  Set the data array.

  The `data` parameter is an array of bytes to be used to set the content for the `Dbt`.

- `Dbt::get_data()`

  Return the data array.

- `Dbt::set_size(u_int32_t size)`

  Sets the byte size of the data array, in bytes.

- `Dbt::get_size()`

  Return the data array size.

- `Dbt::set_ulen(u_int32_t value)`

  Set the byte size of the user-specified buffer.

  Note that applications can determine the length of a record by setting the `ulen` field to 0 and checking the return value in the size field. See the `DB_DBT_USERMEM` flag for more information.

- `Dbt::get_ulen()`

  Return the length in bytes of the user-specified buffer.

  Note that applications can determine the length of a record by setting the `ulen` field to 0 and checking the return value in the size field. See the `DB_DBT_USERMEM` flag for more information.

- `Dbt::set_dlen(u_int32_t dlen)`

  Set the length of the partial record being read or written by the application, in bytes. See the `DB_DBT_PARTIAL` flag for more information.

- `Dbt::get_dlen()`

  Return the length of the partial record, in bytes.

- `Dbt::set_doff(u_int32_t value)`

  Sets the offset of the partial record being read or written by the application, in bytes. See the `DB_DBT_PARTIAL` flag for more information.

- `Dbt::get_doff()`

  Return the offset of the partial record, in bytes.

- `Dbt::set_flags(u_int32_t flags)`

  Set the object flag value.

  The flags parameter must be set to 0 or by bitwise inclusively OR'ing together one or more of the following values:

  - `DB_DBT_MALLOC`

    When this flag is set, Berkeley DB will allocate memory for the returned key or data item (using malloc(3), or the user-specified malloc function), and return a pointer to it in the data field of the key or data `DBT` structure. Because any allocated memory becomes the responsibility of the calling application, the caller must determine whether memory was allocated using the returned value of the data field.

    It is an error to specify more than one of `DB_DBT_MALLOC`, `DB_DBT_REALLOC`, and `DB_DBT_USERMEM`.

  - `DB_DBT_REALLOC`

    When this flag is set Berkeley DB will allocate memory for the returned key or data item (using realloc(3), or the user-specified realloc function), and return a pointer to it in the data field of the key or data DBT structure. Because any allocated memory becomes the responsibility of the calling application, the caller must determine whether memory was allocated using the returned value of the data field.

    It is an error to specify more than one of `DB_DBT_MALLOC`, `DB_DBT_REALLOC`, and `DB_DBT_USERMEM`.

  - `DB_DBT_USERMEM`

    The data field of the key or data structure must refer to memory that is at least ulen bytes in length. If the length of the requested item is less than or equal to that number of bytes, the item is copied into the memory to which the data field refers. Otherwise, the size field is set to the length needed for the requested item, and the error `DB_BUFFER_SMALL` is returned.

    It is an error to specify more than one of `DB_DBT_MALLOC`, `DB_DBT_REALLOC`, and `DB_DBT_USERMEM`.

  If `DB_DBT_MALLOC` or `DB_DBT_REALLOC` is specified, Berkeley DB allocates a properly sized byte array to contain the data. This can be convenient if you know little about the nature of the data, specifically the size of data in the database. However, if your application makes repeated calls to retrieve keys or data, you may notice increased garbage collection due to this allocation. If you know the maximum size of data you are retrieving, you might decrease the memory burden and speed your application by allocating your own byte array and using `DB_DBT_USERMEM`. Even if you don't know the maximum size, you can use this option and reallocate your array whenever your retrieval API call returns an `DB_BUFFER_SMALL` error or throws an exception encapsulating an `DB_BUFFER_SMALL`.

  - `DB_DBT_PARTIAL`

    Do partial retrieval or storage of an item. If the calling application is doing a get, the dlen bytes starting doff bytes from the beginning of the retrieved data record are returned as if they comprised the entire record. If any or all of the specified bytes do not exist in the record, the get is successful, and any existing bytes are returned.

    For example, if the data portion of a retrieved record was 100 bytes, and a partial retrieval was done using a DBT having a dlen field of 20 and a doff field of 85, the get call would succeed, the data field would refer to the last 15 bytes of the record, and the size field would be set to 15.

    If the calling application is doing a put, the dlen bytes starting doff bytes from the beginning of the specified key's data record are replaced by the data specified by the data and size structure elements. If dlen is smaller than size the record will grow; if dlen is larger than size the record will shrink. If the specified bytes do not exist, the record will be extended using nul bytes as necessary, and the put call will succeed.

    It is an error to attempt a partial put using the Db::put() method in a database that supports duplicate records. Partial puts in databases supporting duplicate records must be done using a Dbc::put() method.

    It is an error to attempt a partial put with differing dlen and size values in Queue or Recno databases with fixed-length records.

    For example, if the data portion of a retrieved record was 100 bytes, and a partial put was done using a DBT having a dlen field of 20, a doff field of 85, and a size field of 30, the resulting record would be 115 bytes in length, where the last 30 bytes would be those specified by the put call.

  - `DB_DBT_APPMALLOC`

    After an application-supplied callback routine passed to either Db::associate() or Db::set_append_recno() is executed, the data field of a DBT may refer to memory allocated with malloc(3) or realloc(3). In that case, the callback sets the `DB_DBT_APPMALLOC` flag in the DBT so that Berkeley DB will call free(3) to deallocate the memory when it is no longer required.

  - `DB_DBT_MULTIPLE`

    Set in a secondary key creation callback routine passed to Db::associate() to indicate that multiple secondary keys should be associated with the given primary key/data pair. If set, the size field indicates the number of secondary keys and the data field refers to an array of that number of DBT structures.

    The `DB_DBT_APPMALLOC` flag may be set on any of the DBT structures to indicate that their data field needs to be freed.

## DBT and Bulk Operations

DBT and Bulk Operations | Description
--- | ---
DbMultipleIterator | Base class for bulk get retrieval
DbMultipleDataIterator | Bulk retrieval iterator for data items
DbMultipleKeyDataIterator | Bulk retrieval iterator for key/data pairs
DbMultipleRecnoDataIterator | Bulk retrieval iterator for record number / data item pairs
DbMultipleBuilder | Base class for bulk buffer building
DbMultipleDataBuilder | Bulk buffer builder for data items
DbMultipleKeyDataBuilder | Bulk buffer builder for key/data pairs
DbMultipleRecnoDataBuilder | Bulk buffer builder for record number / data pairs

Prev |  | Next
Dbc::set_priority() | Home | DbMultipleIterator