Skip to content

Instantly share code, notes, and snippets.

@w32zhong
Created January 10, 2015 05:32
Show Gist options
  • Save w32zhong/0a45b4c333b53923cf6f to your computer and use it in GitHub Desktop.
Save w32zhong/0a45b4c333b53923cf6f to your computer and use it in GitHub Desktop.
tokyo cabinet B+ tree multi-value example
// char *str; size_t size;
// FILE *f = open_memstream(&str, &size);
// tex_tr_print(parser_root, f);
// fclose(f);
// printf("str=\n%s||size=%ld\n", str, size);
// free(str);
#include <tcutil.h>
#include <tcbdb.h>
enum {
DB_OMOD_WR,
DB_OMOD_RD
};
void *db_init(const char *name, int mod)
{
TCBDB *bdb = tcbdbnew();
if (mod == DB_OMOD_WR) {
if (!tcbdbopen(bdb, name, BDBOCREAT | BDBOWRITER))
return NULL;
} else if (mod == DB_OMOD_RD) {
if (!tcbdbopen(bdb, name, BDBOREADER))
return NULL;
}
return bdb;
}
void db_release(void *db)
{
tcbdbclose((TCBDB*)db);
tcbdbdel((TCBDB*)db);
}
uint64_t db_records(void *db)
{
return tcbdbrnum((TCBDB*)db);
}
void db_sync(void *db)
{
tcbdbsync((TCBDB*)db);
}
const char *db_last_err(void *db)
{
return tcbdberrmsg(tcbdbecode(db));
}
#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>
int
main()
{
int vnum;
int i;
int integer = 123;
TCLIST *li;
// void *db = db_init("test.db", DB_OMOD_WR);
void *db = db_init("test.db", DB_OMOD_RD);
printf("%" PRIu64 " records.\n", db_records(db));
// tcbdbputdup2((TCBDB*)db, "good", "boy");
// tcbdbputdup((TCBDB*)db, "good", 4, &integer, sizeof(int));
// tcbdbputdup2((TCBDB*)db, "good", "dog");
// tcbdbputdup2((TCBDB*)db, "bad", "ass");
vnum = tcbdbvnum2((TCBDB*)db, "good");
printf("number of values: %d\n", vnum);
vnum = tcbdbvnum2((TCBDB*)db, "bad");
printf("number of values: %d\n", vnum);
li = tcbdbget4((TCBDB*)db, "good", 4);
for (i = 0; i < tclistnum(li); i++) {
if (i == 1) {
int sz;
int *val = (int *)tclistval(li, i, &sz);
printf("val %d: %d (size=%d)\n", i, *val, sz);
} else {
printf("val %d: %s\n", i, tclistval2(li, i));
}
}
tclistdel(li);
li = tcbdbget4((TCBDB*)db, "bad", 3);
for (i = 0; i < tclistnum(li); i++) {
printf("val %d: %s\n", i, tclistval2(li, i));
}
tclistdel(li);
db_release(db);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment