1I:Berkeley DB Reference Guide: Access Methods[P4

Berkeley DB Reference Guide: Access Methods



<

Btree comparison function (bt_compare)



EThe Btree data structure is a sorted, balanced tree structure storingassociated key/data pairs.

<The sort order for the Btree can be specified as part of theedb_open call to open the database, specifically by setting theibt_compare element of the DB_INFO structure. If noHcomparison routine is specified, the sort order is lexical, with shorter"keys collating before longer keys.

ESort routines are passed pointers to keys as arguments. The keys areBrepresented as DBT structures. The routine must return an integerBless than, equal to, or greater than zero if the first argument isFconsidered to be respectively less than, equal to, or greater than theFsecond argument. The only fields that the routines may examine in the6DBT structures are data and size fields.

JAn example routine that might be used to sort integer keys in the databaseis as follows:



;Note that the data must first be copied into memory that isDappropriately aligned, as Berkeley DB does not guarantee any kind of:alignment of the underlying data, including for comparison routines.

CWhen doing integer comparison routines, it is important to rememberAthat databases created on machines of different architectures mayChave different integer byte orders, for which your code may need to compensate.

EAn example routine that might be used to sort keys based on the firstCfive bytes of the key (ignoring any subsequent keys) is as follows:



HAKÿÿ