A beginner's note on berkeley db

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:

  1. 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.
  2. 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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值