Exemple #1
0
static mrb_value
hdb_assoc(mrb_state *mrb, mrb_value self)
{
  hdb_context *context = DATA_PTR(self);
  void *kbuf;
  int ksize;
  mrb_int kint;
  mrb_value ary;
  int sp;
  void *value;
  mrb_value key, result;

  mrb_get_args(mrb, "o", &key);

  if (!get_content(key, &kint, &kbuf, &ksize)) {
    return mrb_nil_value();
  }

  value = tchdbget(context->hdb, kbuf, ksize, &sp);
  if (value == NULL || sp <= 0) {
    if (value) {
      free(value);
    }
    return mrb_nil_value();
  }

  result = mrb_str_new(mrb, value, sp);
  free(value);

  ary = mrb_ary_new_capa(mrb, 2); 
  mrb_ary_push(mrb, ary, key);
  mrb_ary_push(mrb, ary, result);

  return ary; 
}
Exemple #2
0
/* perform get command */
static int procget(const char *path, const char *kbuf, int ksiz, int omode, bool px, bool pz){
  TCHDB *hdb = tchdbnew();
  if(g_dbgfd >= 0) tchdbsetdbgfd(hdb, g_dbgfd);
  if(!tchdbsetcodecfunc(hdb, _tc_recencode, NULL, _tc_recdecode, NULL)) printerr(hdb);
  if(!tchdbopen(hdb, path, HDBOREADER | omode)){
    printerr(hdb);
    tchdbdel(hdb);
    return 1;
  }
  bool err = false;
  int vsiz;
  char *vbuf = tchdbget(hdb, kbuf, ksiz, &vsiz);
  if(vbuf){
    printdata(vbuf, vsiz, px);
    if(!pz) putchar('\n');
    tcfree(vbuf);
  } else {
    printerr(hdb);
    err = true;
  }
  if(!tchdbclose(hdb)){
    if(!err) printerr(hdb);
    err = true;
  }
  tchdbdel(hdb);
  return err ? 1 : 0;
}
Exemple #3
0
void purge_dbb_beyond(INOBNO *inobno)
{
    char *asc_hash = NULL;
    char *key, *value;
    int size;
    int vsize;
    INOBNO inobnocur;

    printf("purge_dbb_beyond : purge fileblock metadata after blocknr %llu\n",inobno->blocknr);
    /* traverse records */
    tchdbiterinit(dbb);
    while ((key = tchdbiternext(dbb, &size)) != NULL) {
        value = tchdbget(dbb, key, size, &vsize);
        memcpy(&inobnocur.inode, key, sizeof(unsigned long long));
        memcpy(&inobnocur.blocknr, key + sizeof(unsigned long long),
               sizeof(unsigned long long));
        if ( inobno->inode == inobnocur.inode ) {
           if ( inobnocur.blocknr > inobno->blocknr ) {
              printf("delete reference %llu-%llu\n",inobnocur.inode,inobnocur.blocknr);
              tchdbout(dbb,&inobnocur,sizeof(INOBNO));
// Verify dbu in this case.
           }
        }
        free(asc_hash);
        free(value);
        free(key);
    }
}
Exemple #4
0
int unpack_files(char *root_dir, bool verbose, bool test) {

    char *key;
    void *value;
    int fd, count, key_size, val_size;


    /* traverse records */
    count = 0;
    tchdbiterinit(hdb);
    while ((key = tchdbiternext2(hdb)) != NULL) {
        key_size = (int) strlen(key);

        if (verbose || test) fprintf(stdout, "Extracting file: %s\n", key);

        value = tchdbget(hdb, key, key_size, &val_size);
        if (value && !test) {
            if (-1 == (fd = open(key, O_WRONLY | O_CREAT | O_TRUNC, 0644))) {
                fprintf(stdout, "***Failed open file %s\n", key);
                return 1;
            }

            write(fd, value, val_size);
            close(fd);
            free(value);
        }
        free(key);
        count++;
    }

    return count;
}
Exemple #5
0
static mrb_value
hdb_get(mrb_state *mrb, mrb_value self)
{
  hdb_context *context = DATA_PTR(self);
  mrb_value key, result;
  void *kbuf;
  int ksize;
  mrb_int kint;
  int sp = 0;
  void *value;


  mrb_get_args(mrb, "o", &key);

  if (!get_content(key, &kint, &kbuf, &ksize)) {
    mrb_raise(mrb, E_ARGUMENT_ERROR, "invalid type of key");
  }

  value = tchdbget(context->hdb, kbuf, ksize, &sp);
  if (value == NULL || sp <= 0) {
    if (value) {
      free(value);
    }
    return mrb_nil_value();
  }

  result = mrb_str_new(mrb, value, sp);
  free(value);
 
  return result; 
}
Exemple #6
0
/* get */
JNIEXPORT jbyteArray JNICALL Java_tokyocabinet_HDB_get
(JNIEnv *env, jobject self, jbyteArray key){
  if(!key){
    throwillarg(env);
    return NULL;
  }
  TCHDB *hdb = (TCHDB *)(intptr_t)(*env)->GetLongField(env, self, hdb_fid_ptr);
  jboolean ick;
  jbyte *kbuf = (*env)->GetByteArrayElements(env, key, &ick);
  if(!kbuf){
    throwoutmem(env);
    return NULL;
  }
  int ksiz = (*env)->GetArrayLength(env, key);
  int vsiz;
  char *vbuf = tchdbget(hdb, kbuf, ksiz, &vsiz);
  jbyteArray val;
  if(vbuf){
    val = (*env)->NewByteArray(env, vsiz);
    if(!val){
      throwoutmem(env);
      return NULL;
    }
    (*env)->SetByteArrayRegion(env, val, 0, vsiz, (jbyte *)vbuf);
    tcfree(vbuf);
  } else {
    val = NULL;
  }
  if(ick) (*env)->ReleaseByteArrayElements(env, key, kbuf, JNI_ABORT);
  return val;
}
Exemple #7
0
void check_inodes()
{
    char *key, *value;
    int size;
    int ksize;
    DDSTAT *ddstat;
    DBT *data;
    unsigned long long inode;
    char *nfi = "NFI";
    CRYPTO *crypto;
    struct stat stbuf;
    char *blockdatadir;

    if (NULL == config->blockdatabs) {
        blockdatadir = s_dirname(config->blockdata);
        stat(blockdatadir, &stbuf);
        free(blockdatadir);
    } else {
        stat(config->blockdata, &stbuf);
    }
    /* traverse records */
    tchdbiterinit(dbp);
    while ((key = tchdbiternext(dbp, &ksize)) != NULL) {
        show_progress();
        if (0 == memcmp(key, nfi, 3)) {
            value = tchdbget(dbp, key, strlen(key), &size);
            memcpy(&inode, value, sizeof(unsigned long long));
            free(value);
        } else {
            memcpy(&inode, key, sizeof(unsigned long long));
            data = search_dbdata(dbp, &inode, sizeof(unsigned long long));
            if (inode == 0) {
                crypto = (CRYPTO *) data->data;
            } else {
                ddstat = value_to_ddstat(data);
                if ( 0 == check_inode_orphaned(ddstat)) {
                   if (S_ISREG(ddstat->stbuf.st_mode)) {
                      if ( NULL != config->blockdatabs ) { 
                        check_inode_structure(ddstat);   
                      } else {
                        file_check_inode_structure(ddstat);   
                      }    
                   }
                } else {
                   printf("Deleting corrupted inode %llu\n",(unsigned long long)ddstat->stbuf.st_ino);
                   search_and_delete_dbdirent(ddstat);
                   delete_key(dbp, key, ksize);
                }
                ddstatfree(ddstat);
            }
            DBTfree(data);
        }
        free(key);
    }

}
Exemple #8
0
/* perform list command */
static int proclist(const char *path, int omode, int max, bool pv, bool px, const char *fmstr){
  TCHDB *hdb = tchdbnew();
  if(g_dbgfd >= 0) tchdbsetdbgfd(hdb, g_dbgfd);
  if(!tchdbsetcodecfunc(hdb, _tc_recencode, NULL, _tc_recdecode, NULL)) printerr(hdb);
  if(!tchdbopen(hdb, path, HDBOREADER | omode)){
    printerr(hdb);
    tchdbdel(hdb);
    return 1;
  }
  bool err = false;
  if(fmstr){
    TCLIST *keys = tchdbfwmkeys2(hdb, fmstr, max);
    for(int i = 0; i < tclistnum(keys); i++){
      int ksiz;
      const char *kbuf = tclistval(keys, i, &ksiz);
      printdata(kbuf, ksiz, px);
      if(pv){
        int vsiz;
        char *vbuf = tchdbget(hdb, kbuf, ksiz, &vsiz);
        if(vbuf){
          putchar('\t');
          printdata(vbuf, vsiz, px);
          tcfree(vbuf);
        }
      }
      putchar('\n');
    }
    tclistdel(keys);
  } else {
    if(!tchdbiterinit(hdb)){
      printerr(hdb);
      err = true;
    }
    TCXSTR *key = tcxstrnew();
    TCXSTR *val = tcxstrnew();
    int cnt = 0;
    while(tchdbiternext3(hdb, key, val)){
      printdata(tcxstrptr(key), tcxstrsize(key), px);
      if(pv){
        putchar('\t');
        printdata(tcxstrptr(val), tcxstrsize(val), px);
      }
      putchar('\n');
      if(max >= 0 && ++cnt >= max) break;
    }
    tcxstrdel(val);
    tcxstrdel(key);
  }
  if(!tchdbclose(hdb)){
    if(!err) printerr(hdb);
    err = true;
  }
  tchdbdel(hdb);
  return err ? 1 : 0;
}
Exemple #9
0
char *tc_get_value(TCHDB *hdb, const char *path,  const char *key, size_t key_len, int *value_len)
{
	if (key == NULL || value_len == NULL || hdb == NULL)
		return NULL;

	char *value;

	TC_RETRY_LOOP(hdb, path, ( value = tchdbget(hdb, key, key_len, value_len) ) != NULL, break);

	return value;
}
Exemple #10
0
void Retsu::Column::lookup(const RecordID key, string& result) {
	int vsize;
	void* value = tchdbget(database, &key, sizeof(RecordID), &vsize);
	
	if(value == NULL) {
		result = string("");
	} else {
		result = string((char*)value, (size_t)vsize);
		free(value);
	}
}
Exemple #11
0
void Retsu::Column::lookup(const RecordID key, double& result) {
	int vsize;
	void* value = tchdbget(database, &key, sizeof(RecordID), &vsize);
	
	if(value == NULL) {
		result = NAN;
	} else if(vsize != sizeof(double)) {
    result = NAN;
    free(value);
  } else {
		result = *((double*)value);
		free(value);
	}
}
Exemple #12
0
void Retsu::Column::lookup(const RIDList& keys, vector<string>& results) {
  results.reserve(keys.size());
  
  int vsize;
  RIDList::const_iterator key;
  for(key = keys.begin(); key != keys.end(); key++) {
    void* buffer = tchdbget(database, &(*key), sizeof(RecordID), &vsize);
    
    if(buffer != NULL) {
      results.push_back("");
    } else {
      results.push_back(string((char*)buffer, (size_t)vsize));
    }
    
    free(buffer);
  }  
}
Exemple #13
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;
}
Exemple #14
0
static int
_tc_fetch (mu_dbm_file_t db, struct mu_dbm_datum const *key,
	   struct mu_dbm_datum *ret)
{
  TCHDB *hdb = db->db_descr;
  void *ptr;
  int retsize;

  ptr = tchdbget (hdb, key->mu_dptr, key->mu_dsize, &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;
}
Exemple #15
0
void* Retsu::Column::lookup(const RecordID key, int& vsize) {
	return tchdbget(database, &key, sizeof(RecordID), &vsize);
}
Exemple #16
0
 void* db_obj_local::dbget(const void *kbuf, int ksiz, int *sp)
 {
   return tchdbget(_hdb,kbuf,ksiz,sp);
 }