我正在使用东京内阁实施btree,但我想知道是否可以保持值的排序。我知道我可以使用tcbdbsetcmpfunc
为键设置自定义比较功能,但不确定值?
我问这个是因为大多数时候我只需要前1000条记录,假设我的值已经排序。否则我将需要循环数百万条记录对它们进行排序并得到前1000条记录,这可能很慢。
例如:
#include <tcutil.h>
#include <tcbdb.h>
#include <stdbool.h>
#include <stdint.h>
struct foo {
int one;
double two;
char *three;
};
// sort by three field
static int val_cmp(const char *aptr, int asiz, const char *bptr, int bsiz, void *op) {
return 1;
}
int main() {
int ecode;
TCBDB *db;
db = tcbdbnew();
struct foo *f;
tcbdbsetcmpfunc(db, val_cmp, f); // sort by struct->three?
// open the database
if(!tcbdbopen(db, "struct.tcb", BDBOWRITER | BDBOCREAT)){
ecode = tcbdbecode(db);
fprintf(stderr, "open error: %s\n", tcbdberrmsg(ecode));
}
f = malloc(sizeof(struct foo));
f->one = 100;
f->two = 1.1111;
f->three = "Hello World";
printf("put: %d\n", tcbdbput(db, "foo", 3, f, sizeof(struct foo)));
f = malloc(sizeof(struct foo));
f->one = 100;
f->two = 1.1111;
f->three = "Hello Planet";
printf("put: %d\n", tcbdbput(db, "bar", 3, f, sizeof(struct foo)));
char *key;
BDBCUR *cursor;
cursor = tcbdbcurnew(db);
tcbdbcurfirst(cursor);
while ((key = tcbdbcurkey2(cursor)) != NULL) {
struct foo *val;
int size;
val = tcbdbcurval(cursor, &size);
printf("%s: one=%d\n", key, val->one);
printf("%s: two=%f\n", key, val->two);
tcbdbcurnext(cursor);
}
tcbdbdel(db);
return 0;
}
答案 0 :(得分:0)
我认为您无法定义值的订单。
我建议有第二个tokyocabinet数据库,映射 从值到键。我希望这个方向是间接的 你的表现仍然很好。
答案 1 :(得分:0)
东京内阁没有嵌入式排序机制。您可以使用列表和自定制订购