Comparision Function
When the default comparision function (lexicographical comparison function) is not enough, we have to write our own comparision function. This is very important. If we don't write it correctly, it may cause big problem!
When using integer as key on little-endian system (explanation of little-endian), a custom comparision function becomes even more important. In this function we can not compare the data getting from the argument directly, instead, data should be copy to a block of continuous memory before being used for comparison. This is because db doesn't guarantee the alignment of data. A sample function is available here.
Key Extractor Function
When there is a secondary db, we use key extractor function to "extract" secondary key from primary db's data part. For obvious reasons, it's quite an important function in secondary db exist programes. When an error occurs in such kind of programe, always remember to check this function.
About Secondary DB
Secondary db is in fact a secondary index of data store in primary db. Secondary db does not have its own data, therefore when data is requested from secondary db, it always go to primary db to fetch the data.
Understanding the issue talked above is very important for understanding the points we are going to talk about now:
- Berkeley db allows duplicate key in a db. However, when secondary db exists, we should always keep the primary key unique. Because the secondary db will not be able to get a unique data part from the primary.
- Every time we read from or modify a db in a secondary db exist system, we should open both primary and secondary dbs, associate the secondary db with the primary db and set specific flag (if we did the first time we use this db). If "secondary inconsistent with primary" exception is thrown out, we should check this point together with the two functions mentioned above.
As a beginner, that's all of the points that I think should be remembered through out the development process. Although vey limited, I think it still guarantees that the dbs we created work. If more function are required, we certainly should dig deeper and get to know more about berkeley db - a relatively simple yet troublesome embedded database.