Berkeley DB engine supports duplicate records that share a key. The
duplicate records can be sorted or unsorted, which depends on the flag
is DB_DUP or DB_DUPSORT. If DB_DUPSORT value is selected, we should
provide a sorting function. The engine does not accept two duplicate
records is judged to be equal to each other by the sorting function. In
other words, if two duplicate records are equal to each other, only one
copy will be stored. However, this can't be accepted in some scenarios.
One solution for this problem is to adjust the return value of sorting function. For example,
int CompareRecord(Db *dbp, const Dbt *a, const Dbt *b)
{
int ai, bi
int ret = 0;
memcpy(&ai, a->get_data(), sizeof(int));
memcpy(&bi, b->get_data(), sizeof(int));
ret = ai - bi;
if(ret == 0)
ret = -1;
return ret;
}
In the codes above, if ai is equal to bi, the sorting function will return -1 instead of 0. Therefore, both ai and bi can be stored in the database.
One solution for this problem is to adjust the return value of sorting function. For example,
int CompareRecord(Db *dbp, const Dbt *a, const Dbt *b)
{
int ai, bi
int ret = 0;
memcpy(&ai, a->get_data(), sizeof(int));
memcpy(&bi, b->get_data(), sizeof(int));
ret = ai - bi;
if(ret == 0)
ret = -1;
return ret;
}
In the codes above, if ai is equal to bi, the sorting function will return -1 instead of 0. Therefore, both ai and bi can be stored in the database.

Leave a comment