Beispiel #1
0
DBPriv *DBPrivOpenDB(const char *dbpath, ARG_UNUSED dbid id)
{
    DBPriv *db = xcalloc(1, sizeof(DBPriv));

    pthread_mutex_init(&db->cursor_lock, NULL);

    if (!OpenTokyoDatabase(dbpath, &db->hdb))
    {
        Log(LOG_LEVEL_ERR, "Could not open Tokyo database at path '%s'. (OpenTokyoDatabase: %s)",
              dbpath, ErrorMessage(db->hdb));

        int errcode = tchdbecode(db->hdb);

        if(errcode != TCEMETA && errcode != TCEREAD)
        {
            goto err;
        }

        tchdbdel(db->hdb);

        return DB_PRIV_DATABASE_BROKEN;
    }

    return db;

err:
    pthread_mutex_destroy(&db->cursor_lock);
    tchdbdel(db->hdb);
    free(db);
    return NULL;
}
Beispiel #2
0
static mrb_value
hdb_ecode(mrb_state *mrb, mrb_value self)
{
  hdb_context *context = DATA_PTR(self);

  return mrb_fixnum_value(tchdbecode(context->hdb));
}
Beispiel #3
0
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;
}
Beispiel #4
0
static bool Delete(TCHDB *hdb, const void *key, int key_size)
{
    if (!tchdbout(hdb, key, key_size) && tchdbecode(hdb) != TCENOREC)
    {
        CfOut(cf_error, "", "!! tchdbout: Could not delete key: %s",
              ErrorMessage(hdb));
        return false;
    }

    return true;
}
Beispiel #5
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;
}
Beispiel #6
0
static bool Delete(TCHDB *hdb, const void *key, int key_size)
{
    if (!tchdbout(hdb, key, key_size) && tchdbecode(hdb) != TCENOREC)
    {
        Log(LOG_LEVEL_ERR, "Could not delete Tokyo key. (tchdbout: %s)",
            ErrorMessage(hdb));
        return false;
    }

    return true;
}
/**
 * Missing function from tcadb.c ??
 */
static const char * _libtokyocabinet_tcaerrmsg(TCADB *db)
{
  switch (tcadbomode(db))
  {
  case ADBOHDB:
    return tcerrmsg(tchdbecode((TCHDB *)tcadbreveal(db)));
  case ADBOBDB:
    return tcerrmsg(tcbdbecode((TCBDB *)tcadbreveal(db)));
  default:
    return tcerrmsg(TCEMISC);
  }
}
Beispiel #8
0
/** 
 * 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;  
}
Beispiel #9
0
/** 
 *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;
}
Beispiel #10
0
static int
_tc_delete (mu_dbm_file_t db, struct mu_dbm_datum const *key)
{
  TCHDB *hdb = db->db_descr;

  if (tchdbout (hdb, key->mu_dptr, key->mu_dsize))
    return 0;
  db->db_errno.n = tchdbecode (hdb);
  if (db->db_errno.n == TCENOREC)
    return MU_ERR_NOENT;
  return MU_ERR_FAILURE;
}
Beispiel #11
0
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;
}
Beispiel #12
0
      void open (char const dsname [], bool newdb = false)
      {
         if(bOpen_) { throw std::runtime_error("The store is already open"); }

         int const OMODE = (newdb ? HDBOTRUNC : 0) | (HDBOWRITER | HDBOREADER | HDBOCREAT);

         bOpen_ = tchdbopen(pdb_, dsname, OMODE);

         if(!bOpen_)
         {
            throw_tcexception(tchdbecode(pdb_), "Failed to open TCH file");
         }
      }
Beispiel #13
0
bool DBPrivRead(DBPriv *db, const void *key, int key_size, void *dest, int dest_size)
{
    if (tchdbget3(db->hdb, key, key_size, dest, dest_size) == -1)
    {
        if (tchdbecode(db->hdb) != TCENOREC)
        {
            Log(LOG_LEVEL_ERR, "Could not read key '%s': (tchdbget3: %s)", (const char *)key, ErrorMessage(db->hdb));
        }
        return false;
    }

    return true;
}
Beispiel #14
0
bool DBPrivRead(DBPriv *db, const void *key, int key_size, void *dest, int dest_size)
{
    if (tchdbget3(db->hdb, key, key_size, dest, dest_size) == -1)
    {
        if (tchdbecode(db->hdb) != TCENOREC)
        {
            CfOut(cf_error, "", "ReadComplexKeyDB(%s): Could not read: %s\n", (const char *)key, ErrorMessage(db->hdb));
        }
        return false;
    }

    return true;
}
Beispiel #15
0
bool open_tyrant_db() {

    int ecode;


    /* open the database */
    if (!tcrdbopen(tdb, tcxstrptr(tdb_host), tdb_port)) {
        ecode = tchdbecode(tdb);
        fprintf(stdout, "Failed to open database, error: %s\n", tcrdberrmsg(ecode));
        return false;
    }

    return true;

}
Beispiel #16
0
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;
}
Beispiel #17
0
/**
 * 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;
}
Beispiel #18
0
/** 
 * 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;  
}
Beispiel #19
0
static int
_tc_nextkey (mu_dbm_file_t db, struct mu_dbm_datum *ret)
{
  TCHDB *hdb = db->db_descr;
  void *ptr;
  int retsize;

  ptr = tchdbiternext (hdb, &retsize);
  if (ptr)
    {
      mu_dbm_datum_free (ret);
      ret->mu_dptr = ptr;
      ret->mu_dsize = retsize;
      return 0;
    }
  else if ((db->db_errno.n = tchdbecode (hdb)) == TCENOREC)
    return MU_ERR_NOENT;
  return MU_ERR_FAILURE;
}
Beispiel #20
0
DBPriv *DBPrivOpenDB(const char *dbpath)
{
    DBPriv *db = xcalloc(1, sizeof(DBPriv));

    pthread_mutex_init(&db->cursor_lock, NULL);

    if (!OpenTokyoDatabase(dbpath, &db->hdb))
    {
        CfOut(cf_error, "", "!! Could not open database %s: %s",
              dbpath, ErrorMessage(db->hdb));

        int errcode = tchdbecode(db->hdb);

        if(errcode != TCEMETA && errcode != TCEREAD)
        {
            goto err;
        }

        tchdbdel(db->hdb);

        CfOut(cf_error, "", "!! Database \"%s\" is broken, recreating...", dbpath);

        DBPathMoveBroken(dbpath);

        if (!OpenTokyoDatabase(dbpath, &db->hdb))
        {
            CfOut(cf_error, "", "!! Could not open database %s after recreate: %s",
                  dbpath, ErrorMessage(db->hdb));
            goto err;
        }
    }

    return db;

err:
    pthread_mutex_destroy(&db->cursor_lock);
    tchdbdel(db->hdb);
    free(db);
    return NULL;
}
Beispiel #21
0
static int
_tc_store (mu_dbm_file_t db,
	   struct mu_dbm_datum const *key,
	   struct mu_dbm_datum const *contents,
	   int replace)
{
  TCHDB *hdb = db->db_descr;
  bool result;

  if (replace)
    result = tchdbput (hdb, key->mu_dptr, key->mu_dsize,
		       contents->mu_dptr, contents->mu_dsize);
  else
    result = tchdbputkeep (hdb, key->mu_dptr, key->mu_dsize,
			   contents->mu_dptr, contents->mu_dsize);
  if (result)
    return 0;
  db->db_errno.n = tchdbecode (hdb);
  if (db->db_errno.n == TCEKEEP)
    return MU_ERR_EXISTS;
  return MU_ERR_FAILURE;
}
Beispiel #22
0
static int
_tc_open (mu_dbm_file_t db, int flags, int mode)
{
  int f;
  bool result;
  mode_t save_um;
  TCHDB *hdb;

  switch (flags)
    {
    case MU_STREAM_CREAT:
      f = HDBOWRITER | HDBOCREAT;
      break;

    case MU_STREAM_READ:
      f = HDBOREADER;
      break;

    case MU_STREAM_RDWR:
      f = HDBOREADER | HDBOWRITER;
      break;

    default:
      return EINVAL;
    }

  hdb = tchdbnew ();

  save_um = umask (0666 & ~mode);
  result = tchdbopen (hdb, db->db_name, f);
  umask (save_um);
  if (!result)
    {
      db->db_errno.n = tchdbecode (hdb);
      return MU_ERR_FAILURE;
    }
  db->db_descr = hdb;
  return 0;
}
Beispiel #23
0
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;
}
Beispiel #24
0
static const char *ErrorMessage(TCHDB *hdb)
{
    return tchdberrmsg(tchdbecode(hdb));
}
Beispiel #25
0
/* 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));
}
Beispiel #26
0
/* ecode */
JNIEXPORT jint JNICALL Java_tokyocabinet_HDB_ecode
(JNIEnv *env, jobject self){
  TCHDB *hdb = (TCHDB *)(intptr_t)(*env)->GetLongField(env, self, hdb_fid_ptr);
  return tchdbecode(hdb);
}
Beispiel #27
0
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);
}
Beispiel #28
0
std::string Retsu::Column::error() {
	return string(tchdberrmsg(tchdbecode(this->database)));
}
Beispiel #29
0
 int db_obj_local::dbecode() const
 {
   return tchdbecode(_hdb);
 }