tokyo 캐비닛을 사용하여 btree를 구현하고 있지만 정렬 된 값을 유지할 수 있는지 알고 싶습니다. tcbdbsetcmpfunc
을 사용하여 키에 대한 사용자 지정 비교 함수를 설정할 수 있지만 값에 대해서는 확실하지 않습니까?C에서 tokyo 캐비닛을 사용하여 값별로 맞춤 정렬
대부분의 경우 내 값이 정렬되어 있다고 가정하고 처음 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;
}
사용 예 https://github.com/PLJNS/Rutgers-Systems-Programming-Fall-2013/blob/master/Sorted%20List/sorted-list. 기음 –