Sorted Duplicate Records

| No Comments | No TrackBacks
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.

No TrackBacks

TrackBack URL: http://lua.me/cgi-bin/mt/mt-tb.cgi/29

Leave a comment

About this Entry

This page contains a single entry by Jiang published on April 27, 2008 8:40 AM.

A Mistake of Using xmlreader from Libxml2 was the previous entry in this blog.

Some Feelings of Using Ubuntu 8.04 is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.