# Dbt

`

###

    #include <db_cxx.h>

    class Dbt {
    public:
    	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(void *data, size_t size);
    	Dbt();
    	Dbt(const Dbt &);
    	Dbt &operator = (const Dbt &);
    	~Dbt();
    };

# Description

This manual page describes the specific details of the Dbt class, used to encode keys and data items in a database.

### Key/Data Pairs

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 reference strings of zero length up to strings of essentially unlimited length. See Database limits for more information.

The Dbt class provides simple access to an underlying data structure, whose elements can be examined or changed using the set_ or get_ methods. The remainder of the manual page sometimes refers to these accesses using the underlying name, e.g., simply ulen instead of Dbt::get_ulen and Dbt::set_ulen. Dbt can be subclassed, providing a way to associate with it additional data, or references to other structures.

The constructors set all elements of the underlying structure to zero. The constructor with two arguments has the effect of setting all elements to zero except for the specified data and size elements.

In the case where the flags structure element is 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 referenced by the pointer will be allocated and managed by Berkeley DB.

The elements of the structure underlying the Dbt class are defined as follows:

void *data;
  A pointer to a byte string. This element is accessed using Dbt::get_data and Dbt::set_data, and may be initialized using one of the constructors.

int offset;
  The number of bytes offset into the data array to determine the portion of the array actually used. This element is accessed using Dbt::get_offset and Dbt::set_offset.

u_int32_t size;
  The length of data, in bytes. This element is accessed using Dbt::get_size and Dbt::set_size, and may be initialized using the constructor with two arguments.

u_int32_t ulen;
  The size of the user's buffer (referenced by data), in bytes. This location is not written by the Db methods.

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

  This element is accessed using Dbt::get_ulen and Dbt::set_ulen.

u_int32_t dlen;
  The length of the partial record being read or written by the application, in bytes. See the DB_DBT_PARTIAL flag for more information. This element is accessed using Dbt::get_dlen, and Dbt::set_dlen.

u_int32_t doff;
  The offset of the partial record being read or written by the application, in bytes. See the DB_DBT_PARTIAL flag for more information. This element is accessed using Dbt::get_doff and Dbt::set_doff.

u_int32_t flags;
  This element is accessed using Dbt::get_flags and Dbt::set_flags.

  The flags value 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 method) and return a pointer to it in the data field of the key or data Dbt object. As any allocated memory becomes the responsibility of the calling application, the caller must be able to determine if memory was allocated.

    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 method) and return a pointer to it in the data field of the key or data Dbt object. As any allocated memory becomes the responsibility of the calling application, the caller must be able to determine if memory was allocated.

    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 object must reference 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 referenced by the data field. Otherwise, the size field is set to the length needed for the requested item, and the error ENOMEM 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 ENOMEM error, or throws an exception encapsulating an ENOMEM.

  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 the existing bytes or nul 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 reference 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 objects. If dlen is smaller than size, the record will grow, and 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 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.

### Retrieved key/data permanence

When using the non-cursor Berkeley DB calls to retrieve key/data items (e.g., Db::get), the memory referenced by the pointer stored into the Dbt is only valid until the next call to Berkeley DB using the Db handle returned by Db::open. (This includes any use of the returned Db handle, including by another thread of control within the process. For this reason, when multiple threads are using the returned DB handle concurrently, one of the DB_DBT_MALLOC, DB_DBT_REALLOC or DB_DBT_USERMEM flags must be specified for any non-cursor Dbt used for key or data retrieval.)

When using the cursor Berkeley DB calls to retrieve key/data items (e.g., Dbc::get), the memory referenced by the pointer into the Dbt is only valid until the next call to Berkeley DB using the Dbc handle returned by Db::cursor.

### Data alignment

The Berkeley DB access methods provide no guarantees about key/data byte string alignment, and applications are responsible for arranging any necessary alignment. The DB_DBT_MALLOC, DB_DBT_REALLOC and DB_DBT_USERMEM flags may be used to store returned items in memory of arbitrary alignment.

### Logical Record Numbers

In all cases for the Queue and Recno access methods, and when calling the Db::get and Dbc::get functions with the DB_SET_RECNO flag specified, the data field of the key must be a pointer to a memory location of type db_recno_t, as typedef'd in the #include <db_cxx.h> include file. This type is a 32-bit unsigned type, (which limits the number of logical records in a Queue or Recno database, and the maximum logical record which may be directly retrieved from a Btree database, to 4,294,967,296). The size field of the key should be the size of that type, i.e., in the C programming language, sizeof(db_recno_t).

Logical record numbers are 1-based, not 0-based, i.e., the first record in the database is record number 1.

`

Copyright Sleepycat Software