1 Reply Latest reply on Jun 13, 2014 5:42 AM by Winter Zhang

    strange problem while building a secondary index.




      I have a strange problem in creating a secondary index which is a part of primary data.

      I tested my program and a working sample program


      My data scheme looks like:

         Key = unique string

         Data = structure {


                          time_t timestamp;



          Secondary Key = timestamp in data (NOT unique)


      My BDB environment flags is "DB_CREATE | DB_INIT_CDB | DB_INIT_MPOOL | DB_THREAD"

      The primary DB is created as BTREE with a custom key compare function provided by calling DB->set_bt_compare.

      int my_key_compare(DB *db, const DBT *key1, const DBT *key2)


           const char *k1_v = (const char *)key1->data;

           const char *k2_v = (const char *)key2->data;


           return strcmp(k1_v, k2_v);



      The secondary Index DB is created as BTREE while permitting duplication. (DB_DUPSORT)

      It has two custom callback functions; one for data compare, the other for extracting a data from the primary data.


      int my_extract_timestamp(DB *db, const DBT *primary_key, const DBT *primary_data, DBT *secdondary_key)


           secondary_key->data = ( (MY_DATA *)(primary_data->data))->timestamp;

           secondary_key->size = sizeof(time_t);

           return 0;


      int my_secondary_dup_compare(DB *db, const DBT *key1, const DBT *key2)


           time_t      k1_v = *(time_t *)key1->data;

           time_t      k2_v = *(time_t *)key2->data;


           return k1_v - k2_v;


      The function 'my_extract_timestamp' is set by calling DB->associate().


      My problem is 'my_secondary_dup_compare' function called with a strange DBT values.

      I think the values should point to the value provided from my_extract_timestamp(), but they pointed to

      the key which provided when calling DB->put on the primary DB.


      Could somebody help me ?


      Any help highly appreciated.

        • 1. Re: strange problem while building a secondary index.
          Winter Zhang



          In the secondary database, the key is what you extract and the data is the key of the primary database. As your primary key is a unique string, your data in secondary database is also a unique string. The DB->set_dup_compare sets the comparison function for the duplicate data, so you are comparing time stamps on unique strings, not on what you extract.


          As you are comparing the time stamps which are the keys of secondary database, I guess here you want to set the bt_compare function instead of the dup_compare for the secondary database.


          Also, about this sentence:

          secondary_key->data = ( (MY_DATA *)(primary_data->data))->timestamp;


          The DBT.data should be an address, but this is a value here instead of an address.



          Winter, Oracle Berkeley DB