Question

Im using Berkeley DB C++ API 6.0 on OSX. My application creates a database with the following tables:

Primary table: (int, myStruct) -> myStruct is a buffer.

Secondary index: (float, myStruct) -> The float key is an information that I retrieve in myStruct buffer with the following callback.

int meanExtractor(Db *sdbp,
              const Dbt *pkey,
              const Dbt *pdata,
              Dbt *skey)
{
   Dbt data = *pdata;
   feature<float> f;
   restoreDescriptor(f, data);

   void* mean = malloc( sizeof(float) );
   memcpy( mean, &f.mean, sizeof(float) );

   skey->set_data(mean);
   skey->set_size(sizeof(float));
   skey->set_flags( DB_DBT_APPMALLOC );

   return 0;
}

When I iterate over the secondary index and print the key/data pairs, the float keys are well stored. My problem is that I can't query this table. I would like to execute this SQL Query for example:

SELECT * FROM secondary index WHERE keys > 1.5 && keys < 3.4

My table is filled by 50000 keys between 0.001 and 49.999. The thing is when I use this method for example:

I assume the Db and the table are already opened
float i = 0.05;
Dbt key = Dbt(&i, sizeof(float));

Dbc* dbc;
db->cursor( txn, &dbc, 0 );
int ret;
ret = dbc->get( key, &vald, DB_SET_RANGE));

Its retrieved this key: 0.275. It should retrieve 0.05 (because it exists) or at least 0.051. And for any other floating value in the Dbt key, it gives me some stupid values. If I put the DB_SET flag, it just doesn't find any keys. My idea was to set the cursor to the smallest key greater than or equal to my key, and then to iterate with the flag DB_NEXT until I reach the end of my range. This must come from the searching algorithm of BerkeleyDB but I saw some (usefull but not enough) examples that do exactly what I need but with the Java API, so it proves that is possible to do...

I'm pretty stucked with this one, so if anybody already had this problem before, thx for helping me. I can put other parts of my code if necessary.

Était-ce utile?

La solution

I used a custom bt_compare function on my secondary index and it works perfectly now.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top