/usr/share/doc/libtokyocabinet-dev/examples/tctdbex.c is in libtokyocabinet-dev 1.4.48-10.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | #include <tcutil.h>
#include <tctdb.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdint.h>
int main(int argc, char **argv){
TCTDB *tdb;
int ecode, pksiz, i, rsiz;
char pkbuf[256];
const char *rbuf, *name;
TCMAP *cols;
TDBQRY *qry;
TCLIST *res;
/* create the object */
tdb = tctdbnew();
/* open the database */
if(!tctdbopen(tdb, "casket.tct", TDBOWRITER | TDBOCREAT)){
ecode = tctdbecode(tdb);
fprintf(stderr, "open error: %s\n", tctdberrmsg(ecode));
}
/* store a record */
pksiz = sprintf(pkbuf, "%ld", (long)tctdbgenuid(tdb));
cols = tcmapnew3("name", "mikio", "age", "30", "lang", "ja,en,c", NULL);
if(!tctdbput(tdb, pkbuf, pksiz, cols)){
ecode = tctdbecode(tdb);
fprintf(stderr, "put error: %s\n", tctdberrmsg(ecode));
}
tcmapdel(cols);
/* store a record in a naive way */
pksiz = sprintf(pkbuf, "12345");
cols = tcmapnew();
tcmapput2(cols, "name", "falcon");
tcmapput2(cols, "age", "31");
tcmapput2(cols, "lang", "ja");
if(!tctdbput(tdb, pkbuf, pksiz, cols)){
ecode = tctdbecode(tdb);
fprintf(stderr, "put error: %s\n", tctdberrmsg(ecode));
}
tcmapdel(cols);
/* store a record with a TSV string */
if(!tctdbput3(tdb, "abcde", "name\tjoker\tage\t19\tlang\ten,es")){
ecode = tctdbecode(tdb);
fprintf(stderr, "put error: %s\n", tctdberrmsg(ecode));
}
/* search for records */
qry = tctdbqrynew(tdb);
tctdbqryaddcond(qry, "age", TDBQCNUMGE, "20");
tctdbqryaddcond(qry, "lang", TDBQCSTROR, "ja,en");
tctdbqrysetorder(qry, "name", TDBQOSTRASC);
tctdbqrysetlimit(qry, 10, 0);
res = tctdbqrysearch(qry);
for(i = 0; i < tclistnum(res); i++){
rbuf = tclistval(res, i, &rsiz);
cols = tctdbget(tdb, rbuf, rsiz);
if(cols){
printf("%s", rbuf);
tcmapiterinit(cols);
while((name = tcmapiternext2(cols)) != NULL){
printf("\t%s\t%s", name, tcmapget2(cols, name));
}
printf("\n");
tcmapdel(cols);
}
}
tclistdel(res);
tctdbqrydel(qry);
/* close the database */
if(!tctdbclose(tdb)){
ecode = tctdbecode(tdb);
fprintf(stderr, "close error: %s\n", tctdberrmsg(ecode));
}
/* delete the object */
tctdbdel(tdb);
return 0;
}
|