void throw_tcexception(int ecode, char const * msg = "Unexpected error") { std::stringstream oss; oss << msg << ": " << tchdberrmsg(ecode); throw std::runtime_error(oss.str()); }
/* errmsg */ JNIEXPORT jstring JNICALL Java_tokyocabinet_HDB_errmsg (JNIEnv *env, jclass cls, jint ecode){ jstring jmsg = (*env)->NewStringUTF(env, tchdberrmsg(ecode)); if(!jmsg){ throwoutmem(env); return NULL; } return jmsg; }
int main (int argc, char *argv[]) { TCHDB * const restrict hdb = tchdbnew(); /* create the object */ char *value; const char *p; const char *end; struct stat st; int fd; const unsigned int klen = 8; /* input stream must have keys of constant len 8 */ if (argc < 3) return -1; /* open the database */ if(!tchdbopen(hdb, argv[1], HDBOREADER | HDBONOLCK)) { fprintf(stderr, "open error: %s\n", tchdberrmsg(tchdbecode(hdb))); return -1; } /* open input file */ if ((fd = open(argv[2], O_RDONLY, 0777)) == -1) {perror("open"); return -1;} if (fstat(fd, &st) != 0) {perror("fstat");return -1;} p = (const char *)mmap(0, st.st_size, PROT_READ, MAP_SHARED, fd, 0); if (p == MAP_FAILED) {perror("mmap"); return -1;} close(fd); /* read each key from input mmap and query mcdb * (no error checking since key might not exist) */ for (end = p+st.st_size; p < end; p += klen) { #if 0 tchdbvsiz(hdb, p, klen); /* retrieve record value size */ #else if ((value = tchdbget(hdb, p, klen, &fd))) /* retrieve records */ free(value); #endif } if(!tchdbclose(hdb)) /* close the database */ fprintf(stderr, "close error: %s\n", tchdberrmsg(tchdbecode(hdb))); tchdbdel(hdb); /* delete the object */ return 0; }
/** * delete a record by key */ int DeleteRecord(tcDatabase hdb, tcKey key) { int ecode; if(!tchdbout((TCHDB*)hdb, &key, sizeof(tcKey))) { ecode = tchdbecode(hdb); fprintf(stderr, "delete error: %s\n", tchdberrmsg(ecode)); return FAILURE; } return SUCCESS; }
/** * set a record to database */ int SetRecord(tcDatabase hdb, tcKey key, tcValue value) { int ecode; if (!tchdbput((TCHDB*)hdb, &key, sizeof(tcKey), value.str, value.len)) { ecode = tchdbecode((TCHDB*)hdb); fprintf(stderr, "put error: %s\n", tchdberrmsg(ecode)); return FAILURE; } //printf("set successfully!\n"); return SUCCESS; }
/** *close database */ int CloseDB(tcDatabase hdb) { int ecode; if (!tchdbclose((TCHDB*)hdb)) { ecode = tchdbecode((TCHDB*)hdb); fprintf(stderr, "close error: %s\n", tchdberrmsg(ecode)); exit(-1); } tchdbdel((TCHDB*)hdb); return SUCCESS; }
bool open_cabinet_db(int64_t bnum, bool optimize) { int ecode; if (optimize) fprintf(stdout, "Opening database optimized for %d items\n", bnum); /* tune up the db*/ if (optimize && !tchdbtune(hdb, bnum, -1, -1, HDBTLARGE)) { ecode = tchdbecode(hdb); fprintf(stdout, "Failed to tune up database, error: %s\n", tchdberrmsg(ecode)); return false; } /* open the database */ if (!tchdbopen(hdb, tcxstrptr(db_file), HDBOWRITER | HDBOCREAT)) { ecode = tchdbecode(hdb); fprintf(stdout, "Failed to open database, error: %s\n", tchdberrmsg(ecode)); return false; } return true; }
char *get_host_pubkey(char *host) { char *key; char value[BUFSIZE]; char *buffer = (char *) malloc(BUFSIZE *sizeof(char)); char *keyname = NULL; char needle[BUFSIZE]; TCHDB *hdb; hdb = tchdbnew(); int ecode; struct addrinfo *result; struct addrinfo *res; if(tchdbopen(hdb,LASTSEEN_DB,HDBOREADER) != 1){ printf("%s for database '%s'\n",tchdberrmsg(tchdbecode(hdb)),LASTSEEN_DB); return NULL; } int error = getaddrinfo(host,NULL,NULL,&result); for(res = result; res != NULL; res = res->ai_next) { inet_ntop(res->ai_family, get_in_addr((struct sockaddr *)res->ai_addr), needle, sizeof(needle)); tchdbiterinit(hdb); while((key = tchdbiternext2(hdb)) != NULL && keyname == NULL){ if(strspn(key,"k") == 1){ tchdbget3(hdb, key, strlen(key) + 1, value, BUFSIZE); if(strcmp(value,needle) == 0){ keyname = ++key; } } } } tchdbclose(hdb); if(keyname) { sprintf(buffer,"%s/root-%s.pub",PPKEYS,keyname); return buffer; }else{ for(res = result; res != NULL; res = res->ai_next) { inet_ntop(res->ai_family, get_in_addr((struct sockaddr *)res->ai_addr), needle, sizeof(needle)); sprintf(buffer,"%s/root-%s.pub",PPKEYS,needle); if(file_exist(buffer)){ return buffer; } } } return NULL; }
/** * delete a hash database */ tcDatabase CreateDB(const char *dbName) { int ecode; TCHDB *hdb = tchdbnew(); if (dbName == NULL) { dbName = "default.hdb"; } if (!tchdbopen(hdb, dbName, HDBOWRITER | HDBOCREAT)) { ecode = tchdbecode(hdb); fprintf(stderr, "open error: %s\n", tchdberrmsg(ecode)); exit(-1); } //printf("open successfully!\n"); return (tcDatabase)hdb; }
/** * get a record from database */ int GetRecord(tcDatabase hdb, tcKey key, tcValue *pvalue) { int ecode; int ret; ret = tchdbget3(hdb, &key, sizeof(tcKey), pvalue->str, pvalue->len); //printf("%d\n", ret); if(ret != -1) { pvalue->str[ret] = '\0'; pvalue->len = ret; return SUCCESS; } ecode = tchdbecode(hdb); fprintf(stderr, "get error: %s\n", tchdberrmsg(ecode)); return FAILURE; }
/* print error information */ static void printerr(TCHDB *hdb){ const char *path = tchdbpath(hdb); int ecode = tchdbecode(hdb); fprintf(stderr, "%s: %s: %d: %s\n", g_progname, path ? path : "-", ecode, tchdberrmsg(ecode)); }
static const char *ErrorMessage(TCHDB *hdb) { return tchdberrmsg(tchdbecode(hdb)); }
static char const * _tc_strerror (mu_dbm_file_t db) { return tchdberrmsg (db->db_errno.n); }
const char* db_obj_local::dberrmsg(int ecode) const { return tchdberrmsg(ecode); }
int pack_file(char *file, TCXSTR *root_key, bool resume) { struct stat st; int ecode; TCXSTR *key; key = tcxstrdup(root_key); size_t file_path_len; file_path_len = strlen(file); char *file_name; file_name = get_file_name(file, file_path_len); tcxstrcat2(key, file_name); if (resume && key_exists(tcxstrptr(key))) { fprintf(stdout, "already exists"); return; } if (-1 == stat(file, &st)) { return 1; } if (!S_ISREG(st.st_mode)) { fprintf(stdout, "Not regular file: %s", file); return 2; } int fd; if (-1 == (fd = open(file, O_RDONLY))) { fprintf(stdout, "***Failed open file %s", file); return 1; } void *fmap; if (MAP_FAILED == (fmap = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0))) { fprintf(stdout, "mmaping failed for %s", file); close(fd); return -1; } /* store records */ if (use_cabinet_lib) { if (!tchdbput(hdb, tcxstrptr(key), tcxstrsize(key), fmap, st.st_size)) { ecode = tchdbecode(hdb); fprintf(stdout, "put error: %s", tchdberrmsg(ecode)); } } else { if (!tcrdbput(tdb, tcxstrptr(key), tcxstrsize(key), fmap, st.st_size)) { ecode = tcrdbecode(tdb); fprintf(stdout, "put error: %s", tcrdberrmsg(ecode)); } } fprintf(stdout, "%d bytes", st.st_size); munmap(fmap, st.st_size); close(fd); tcxstrdel(key); }
std::string Retsu::Column::error() { return string(tchdberrmsg(tchdbecode(this->database))); }
int main(int argc, char *argv[]) { int rc; char *val; char str[30]; bool ret; uint64_t id; uint64_t re; unsigned int num; TCHDB *loong_info; char username[] = "lijinxing"; // id = 1210519264165944251LL; memset(&str, 0, sizeof(str)); id = ident_key(); snprintf(str, sizeof(str), "%llu", id); num = strhash(str); printf("num = %u\r\nmod = %u\r\nstr = %s\r\n", num, num % 10, str); unlink("test.db"); loong_info = tchdbnew(); if(!tchdbopen(loong_info, "test.db", HDBOWRITER | HDBOCREAT)) { rc = tchdbecode(loong_info); printf("loong_info.db open error: %s\r\n", tchdberrmsg(rc)); return 0; } if(!tchdbput(loong_info, username, strlen(username), str, strlen(str))) { rc = tchdbecode(loong_info); printf("loong_user error: %s\r\n", tchdberrmsg(rc)); } if(!tchdbput(loong_info, (char *)&(id), sizeof(uint64_t), str, strlen(str))) { rc = tchdbecode(loong_info); printf("loong_user error: %s\r\n", tchdberrmsg(rc)); } val = tchdbget2(loong_info, username); if(val == NULL) { printf("ûÕÒµ½\r\n"); } else { printf("val = %s\r\n", val); free(val); } if(!tchdbput(loong_info, username, strlen(username), "Àî½õÐÇ", strlen("Àî½õÐÇ"))) { rc = tchdbecode(loong_info); printf("loong_user error: %s\r\n", tchdberrmsg(rc)); } val = tchdbget2(loong_info, username); if(val == NULL) { printf("ûÕÒµ½\r\n"); } else { printf("val = %s\r\n", val); free(val); } /* ret = tchdbout(loong_info, (char *)&(id), sizeof(uint64_t)); if(ret) { printf("uint64_tɾ³ý³É¹¦\r\n"); } else { printf("uint64_tɾ³ýʧ°Ü\r\n"); } val = tchdbget(loong_info, (char *)&(id), sizeof(uint64_t), &rc); if(val == NULL) { printf("id ûÕÒµ½\r\n"); } else { printf("id val = %s\r\n", val); free(val); } ret = tchdbout(loong_info, username, strlen(username)); if(ret) { printf("stringɾ³ý³É¹¦\r\n"); } else { printf("stringɾ³ýʧ°Ü\r\n"); } val = tchdbget2(loong_info, username); if(val == NULL) { printf("ûÕÒµ½\r\n"); } else { printf("val = %s\r\n", val); free(val); } */ tchdbclose(loong_info); tchdbdel(loong_info); Test_out(); // fetch_user_info("1220582278313757000"); return 0; }