它基本上是一棵二叉树,它首先搜索哈希以确定它是left
还是right
:
if(hash > rec.hash){
off = rec.left;
entoff = rec.off + (sizeof(uint8_t) + sizeof(uint8_t));
} else if(hash < rec.hash){
off = rec.right;
entoff = rec.off + (sizeof(uint8_t) + sizeof(uint8_t)) +
(hdb->ba64 ? sizeof(uint64_t) : sizeof(uint32_t));
} else {
if(!rec.kbuf && !tchdbreadrecbody(hdb, &rec)) return false;
int kcmp = tcreckeycmp(kbuf, ksiz, rec.kbuf, rec.ksiz);
if(kcmp > 0){
off = rec.left;
...
} else if(kcmp < 0){
off = rec.right;
...
以下是哈希的计算方式:
static uint64_t tchdbbidx(TCHDB *hdb, const char *kbuf, int ksiz, uint8_t *hp){
...
uint32_t hash = 751;
const char *rp = kbuf + ksiz;
while(ksiz--){
...
hash = (hash * 31) ^ *(uint8_t *)--rp;
}
*hp = hash;
...
}
但是看起来哈希计算的方式并不能保证键的顺序,
这是一个错误吗?