예제 #1
0
파일: db.c 프로젝트: tomerdmnt/levelfs
const char *
db_iter_next(db_iter_t *it, size_t *klen) {
	const char *next_key = NULL;
	*klen = 0;

	if (it->first) {
		if (!leveldb_iter_valid(it->it))
			return NULL;
		it->first = 0;
	} else {
		leveldb_iter_next(it->it);
		if (!leveldb_iter_valid(it->it))
			return NULL;
	}

	next_key = leveldb_iter_key(it->it, klen);
	if (it->base_key_len > *klen) {
		klen = 0;
		return NULL;
	}
	if (strncmp(it->base_key, next_key, it->base_key_len) != 0) {
		klen = 0;
		return NULL;
	}

	return next_key;
}
예제 #2
0
파일: be-leveldb.c 프로젝트: dolfly/nmdb
int xleveldb_firstkey(struct db_conn *db, unsigned char *key, size_t *ksize)
{
	int rv = 0;
	const char *db_key;
	size_t db_ksize;

	leveldb_readoptions_t *options = leveldb_readoptions_create();
	leveldb_iterator_t *it = leveldb_create_iterator(db->conn, options);

	leveldb_iter_seek_to_first(it);
	if (! leveldb_iter_valid(it)) {
		rv = 0;
		goto exit;
	}

	db_key = leveldb_iter_key(it, &db_ksize);

	if (db_key == NULL || db_ksize > *ksize) {
		rv = 0;
		goto exit;
	}

	*ksize = db_ksize;
	memcpy(key, db_key, db_ksize);
	rv = 1;

exit:
	leveldb_readoptions_destroy(options);
	leveldb_iter_destroy(it);
	return rv;
}
int ldb_cursor_next(LDB_cursor *const cursor, DB_val *const key, DB_val *const val, int const dir) {
	if(!cursor) return DB_EINVAL;
	if(0 == dir) return DB_EINVAL;
	if(dir > 0) leveldb_iter_next(cursor->iter);
	if(dir < 0) leveldb_iter_prev(cursor->iter);
	cursor->valid = !!leveldb_iter_valid(cursor->iter);
	return ldb_cursor_current(cursor, key, val);
}
예제 #4
0
파일: be-leveldb.c 프로젝트: dolfly/nmdb
int xleveldb_nextkey(struct db_conn *db,
		const unsigned char *key, size_t ksize,
		unsigned char *nextkey, size_t *nksize)
{
	int rv;
	const char *db_nextkey;
	size_t db_nksize = 0;

	leveldb_readoptions_t *options = leveldb_readoptions_create();
	leveldb_iterator_t *it = leveldb_create_iterator(db->conn, options);

	leveldb_iter_seek(it, (const char *) key, ksize);
	if (! leveldb_iter_valid(it)) {
		rv = 0;
		goto exit;
	}

	leveldb_iter_next(it);
	if (! leveldb_iter_valid(it)) {
		rv = 0;
		goto exit;
	}

	db_nextkey = leveldb_iter_key(it, &db_nksize);

	if (db_nextkey == NULL || db_nksize > *nksize) {
		rv = 0;
		goto exit;
	}

	*nksize = db_nksize;
	memcpy(nextkey, db_nextkey, db_nksize);
	rv = 1;

exit:
	leveldb_iter_destroy(it);
	leveldb_readoptions_destroy(options);
	return rv;
}
int ldb_cursor_seek(LDB_cursor *const cursor, DB_val *const key, DB_val *const val, int const dir) {
	if(!cursor) return DB_EINVAL;
	if(!key) return DB_EINVAL;
	DB_val const orig = *key;
	leveldb_iter_seek(cursor->iter, key->data, key->size);
	cursor->valid = !!leveldb_iter_valid(cursor->iter);
	int rc = ldb_cursor_current(cursor, key, val);
	if(dir > 0) return rc;
	if(dir < 0) {
		if(DB_SUCCESS != rc) {
			leveldb_iter_seek_to_last(cursor->iter);
		} else if(0 != cursor->cmp(key, &orig)) {
			leveldb_iter_prev(cursor->iter);
		} else return rc;
		cursor->valid = !!leveldb_iter_valid(cursor->iter);
		return ldb_cursor_current(cursor, key, val);
	}
	if(DB_SUCCESS != rc) return rc;
	if(0 == cursor->cmp(key, &orig)) return rc;
	cursor->valid = 0;
	return DB_NOTFOUND;
}
예제 #6
0
파일: ndb_cursor.c 프로젝트: idning/ndb
sds
cursor_next_key(cursor_t *cursor)
{
    const char *str;
    size_t len;
    sds key;

    if (!leveldb_iter_valid(cursor->iter)) {
        return NULL;
    }

    str = leveldb_iter_key(cursor->iter, &len);
    key = sdscpylen(sdsempty(), str, len);  /* TODO: check mem */

    leveldb_iter_next(cursor->iter);
    return key;
}
예제 #7
0
파일: ndb_cursor.c 프로젝트: idning/ndb
rstatus_t
cursor_next(cursor_t *cursor, sds *key, sds *val, uint64_t *expire)
{
    size_t len;
    const char *str;

    if (!leveldb_iter_valid(cursor->iter)) {
        return NC_ERROR;
    }

    str = leveldb_iter_key(cursor->iter, &len);
    *key = sdsnewlen(str, len);
    ASSERT(*key != NULL);            /* TODO: check mem */

    str = leveldb_iter_value(cursor->iter, &len);
    store_decode_val(str, len, val, expire);

    leveldb_iter_next(cursor->iter);
    return NC_OK;
}
예제 #8
0
static
void listmdb(struct MetaDB* mdb) {
    leveldb_iterator_t* iter =
      leveldb_create_iterator(mdb->db, mdb->scan_options);

    leveldb_iter_seek_to_first(iter);
    while (leveldb_iter_valid(iter)) {
        size_t klen;
        const char* kstr = leveldb_iter_key(iter, &klen);
        const metadb_key_t* key = (const metadb_key_t *) kstr;
          printf("%ld %ld %s\n",
                 key->parent_id, key->partition_id, key->name_hash);
        size_t vlen;
        const char* vstr = leveldb_iter_value(iter, &vlen);
        (void) vstr;
        leveldb_iter_next(iter);
    }

    leveldb_iter_destroy(iter);
}
예제 #9
0
static struct stat_cache_entry *stat_cache_iter_current(struct stat_cache_iterator *iter) {
    struct stat_cache_entry *entry;
    const struct stat_cache_value *value;
    const char *key;
    size_t klen, vlen;

    assert(iter);

    //log_print(LOG_DEBUG, "checking iterator validity");

    // If we've gone beyond the end of the dataset, quit.
    if (!leveldb_iter_valid(iter->ldb_iter)) {
        return NULL;
    }

    //log_print(LOG_DEBUG, "fetching the key");

    key = leveldb_iter_key(iter->ldb_iter, &klen);
    // log_print(LOG_DEBUG, "fetched key: %s", key);

    //log_print(LOG_DEBUG, "fetched the key");

    // If we've gone beyond the end of the prefix range, quit.
    // Use (iter->key_prefix_len - 1) to exclude the NULL at the prefix end.
    if (strncmp(key, iter->key_prefix, iter->key_prefix_len - 1) != 0) {
        log_print(LOG_DEBUG, "Key %s does not match prefix %s for %lu characters. Ending iteration.", key, iter->key_prefix, iter->key_prefix_len);
        return NULL;
    }

    //log_print(LOG_DEBUG, "fetching the value");

    value = (const struct stat_cache_value *) leveldb_iter_value(iter->ldb_iter, &vlen);

    entry = malloc(sizeof(struct stat_cache_entry));
    entry->key = key;
    entry->value = value;
    // log_print(LOG_DEBUG, "iter_current: key = %s; value = %s", key, value);
    return entry;
}
예제 #10
0
int main(int argc, char** argv) {
  leveldb_t* db;
  leveldb_comparator_t* cmp;
  leveldb_cache_t* cache;
  leveldb_env_t* env;
  leveldb_options_t* options;
  leveldb_readoptions_t* roptions;
  leveldb_writeoptions_t* woptions;
  char* err = NULL;

#if defined(LEVELDB_PLATFORM_WINDOWS)
  snprintf(dbname, sizeof(dbname), "tmp\\leveldb_c_test");
#else
  snprintf(dbname, sizeof(dbname), "/tmp/leveldb_c_test-%d",
           ((int) geteuid()));
#endif

  StartPhase("create_objects");
  cmp = leveldb_comparator_create(NULL, CmpDestroy, CmpCompare, CmpName);
  env = leveldb_create_default_env();
  cache = leveldb_cache_create_lru(100000);

  options = leveldb_options_create();
  leveldb_options_set_comparator(options, cmp);
  leveldb_options_set_error_if_exists(options, 1);
  leveldb_options_set_cache(options, cache);
  leveldb_options_set_env(options, env);
  leveldb_options_set_info_log(options, NULL);
  leveldb_options_set_write_buffer_size(options, 100000);
  leveldb_options_set_paranoid_checks(options, 1);
  leveldb_options_set_max_open_files(options, 10);
  leveldb_options_set_block_size(options, 1024);
  leveldb_options_set_block_restart_interval(options, 8);
  leveldb_options_set_compression(options, leveldb_no_compression);

  roptions = leveldb_readoptions_create();
  leveldb_readoptions_set_verify_checksums(roptions, 1);
  leveldb_readoptions_set_fill_cache(roptions, 0);

  woptions = leveldb_writeoptions_create();
  leveldb_writeoptions_set_sync(woptions, 1);

  StartPhase("destroy");
  leveldb_destroy_db(options, dbname, &err);
  leveldb_free(&err);

  StartPhase("open_error");
  db = leveldb_open(options, dbname, &err);
  CheckCondition(err != NULL);
  leveldb_free(&err);

  StartPhase("open");
  leveldb_options_set_create_if_missing(options, 1);
  db = leveldb_open(options, dbname, &err);
  CheckNoError(err);
  CheckGet(db, roptions, "foo", NULL);

  StartPhase("put");
  leveldb_put(db, woptions, "foo", 3, "hello", 5, &err);
  CheckNoError(err);
  CheckGet(db, roptions, "foo", "hello");

  StartPhase("writebatch");
  {
    int pos = 0;
    leveldb_writebatch_t* wb = leveldb_writebatch_create();
    leveldb_writebatch_put(wb, "foo", 3, "a", 1);
    leveldb_writebatch_clear(wb);
    leveldb_writebatch_put(wb, "bar", 3, "b", 1);
    leveldb_writebatch_put(wb, "box", 3, "c", 1);
    leveldb_writebatch_delete(wb, "bar", 3);
    leveldb_write(db, woptions, wb, &err);
    CheckNoError(err);
    CheckGet(db, roptions, "foo", "hello");
    CheckGet(db, roptions, "bar", NULL);
    CheckGet(db, roptions, "box", "c");
    leveldb_writebatch_iterate(wb, &pos, CheckPut, CheckDel);
    CheckCondition(pos == 3);
    leveldb_writebatch_destroy(wb);
  }

  StartPhase("iter");
  {
    leveldb_iterator_t* iter = leveldb_create_iterator(db, roptions);
    CheckCondition(!leveldb_iter_valid(iter));
    leveldb_iter_seek_to_first(iter);
    CheckCondition(leveldb_iter_valid(iter));
    CheckIter(iter, "box", "c");
    leveldb_iter_next(iter);
    CheckIter(iter, "foo", "hello");
    leveldb_iter_prev(iter);
    CheckIter(iter, "box", "c");
    leveldb_iter_prev(iter);
    CheckCondition(!leveldb_iter_valid(iter));
    leveldb_iter_seek_to_last(iter);
    CheckIter(iter, "foo", "hello");
    leveldb_iter_seek(iter, "b", 1);
    CheckIter(iter, "box", "c");
    leveldb_iter_get_error(iter, &err);
    CheckNoError(err);
    leveldb_iter_destroy(iter);
  }

  StartPhase("approximate_sizes");
  {
    int i;
    int n = 20000;
    char keybuf[100];
    char valbuf[100];
    uint64_t sizes[2];
    const char* start[2] = { "a", "k00000000000000010000" };
    size_t start_len[2] = { 1, 21 };
    const char* limit[2] = { "k00000000000000010000", "z" };
    size_t limit_len[2] = { 21, 1 };
    leveldb_writeoptions_set_sync(woptions, 0);
    for (i = 0; i < n; i++) {
      snprintf(keybuf, sizeof(keybuf), "k%020d", i);
      snprintf(valbuf, sizeof(valbuf), "v%020d", i);
      leveldb_put(db, woptions, keybuf, strlen(keybuf), valbuf, strlen(valbuf),
                  &err);
      CheckNoError(err);
    }
    leveldb_approximate_sizes(db, 2, start, start_len, limit, limit_len, sizes);
    CheckCondition(sizes[0] > 0);
    CheckCondition(sizes[1] > 0);
  }

  StartPhase("property");
  {
    char* prop = leveldb_property_value(db, "nosuchprop");
    CheckCondition(prop == NULL);
    prop = leveldb_property_value(db, "leveldb.stats");
    CheckCondition(prop != NULL);
    leveldb_free(&prop);
  }

  StartPhase("snapshot");
  {
    const leveldb_snapshot_t* snap;
    snap = leveldb_create_snapshot(db);
    leveldb_delete(db, woptions, "foo", 3, &err);
    CheckNoError(err);
    leveldb_readoptions_set_snapshot(roptions, snap);
    CheckGet(db, roptions, "foo", "hello");
    leveldb_readoptions_set_snapshot(roptions, NULL);
    CheckGet(db, roptions, "foo", NULL);
    leveldb_release_snapshot(db, snap);
  }

  StartPhase("repair");
  {
    leveldb_close(db);
    leveldb_options_set_create_if_missing(options, 0);
    leveldb_options_set_error_if_exists(options, 0);
    leveldb_repair_db(options, dbname, &err);
    CheckNoError(err);
    db = leveldb_open(options, dbname, &err);
    CheckNoError(err);
    CheckGet(db, roptions, "foo", NULL);
    CheckGet(db, roptions, "bar", NULL);
    CheckGet(db, roptions, "box", "c");
  }

  StartPhase("cleanup");
  leveldb_close(db);
  leveldb_options_destroy(options);
  leveldb_readoptions_destroy(roptions);
  leveldb_writeoptions_destroy(woptions);
  leveldb_cache_destroy(cache);
  leveldb_comparator_destroy(cmp);
  leveldb_env_destroy(env);

  fprintf(stderr, "PASS\n");
  return 0;
}
예제 #11
0
파일: txdb.c 프로젝트: haraldh/bitc
int
txdb_open(struct config *config,
          char         **errStr,
          struct txdb  **out)
{
    leveldb_iterator_t* iter;
    struct txdb *txdb;
    int res;

    txdb = safe_calloc(1, sizeof *txdb);
    txdb->hash_txo = hashtable_create(); /* index all interesting txos */
    txdb->hash_tx  = hashtable_create(); /* all TX brought to our attention */
    txdb->path     = txdb_get_db_path(config);
    txdb->tx_seq   = 0;

    theTxdb = txdb;

    if (!file_exists(txdb->path)) {
        Log(LGPFX" txdb DB '%s' does not exist. Creating..\n", txdb->path);
    }

    res = txdb_open_db(txdb);
    if (res) {
        *errStr = "failed to open tx DB";
        goto error;
    }

    res = file_chmod(txdb->path, 0700);
    if (res) {
        Log(LGPFX" Failed to chmod txdb to 0700: %s\n", strerror(res));
        goto error;
    }

    *out = txdb;

    iter = leveldb_create_iterator(txdb->db, txdb->rd_opts);
    leveldb_iter_seek_to_first(iter);

    while (leveldb_iter_valid(iter) && btc->stop == 0) {
        const char *key;
        const char *val;
        size_t klen;
        size_t vlen;

        key = leveldb_iter_key(iter, &klen);
        val = leveldb_iter_value(iter, &vlen);

        Log(LGPFX" found entry \"%s\" klen=%zu vlen=%zu\n", key, klen, vlen);

        if (klen > 4 && strncmp(key, "/tx/", 4) == 0) {
            res = txdb_load_tx(txdb, key, klen, val, vlen);
            ASSERT(res == 0);
        }

        leveldb_iter_next(iter);
    }
    leveldb_iter_destroy(iter);

    txdb_export_tx_info(txdb);
    txdb_print_coins(txdb, 1);

exit:
    return res;

error:
    txdb_close(txdb);
    theTxdb = NULL;
    *out = NULL;
    goto exit;
}
int
main ()
{
unsigned int N_KEYS;
unsigned int N_BATCH;

printf("\n number of keys:");
scanf("%u",&N_KEYS);

printf("\n batch size:");
scanf("%u",&N_BATCH);






  tinymt32_t tinymt32;
//initialize random generator
  tinymt32_init (&tinymt32, 0);


  int iter;
  int sec_iter;
  int stop;
  int counter;


//initialize database
  char *errptr = NULL;

  leveldb_options_t *options = leveldb_options_create ();

/* initializeOptions */
  leveldb_options_set_create_if_missing (options, 1);
  leveldb_options_set_write_buffer_size(options,120000000 );
  leveldb_options_set_max_open_files(options,800000);
/*open Database */

  leveldb_t *db_helper =
    leveldb_open (options, "/mnt/database/database_helper", &errptr);
  leveldb_t *db = leveldb_open (options, "/mnt/database/database", &errptr);



  leveldb_readoptions_t *readoptions = leveldb_readoptions_create ();

  leveldb_writeoptions_t *writeoptions = leveldb_writeoptions_create ();

leveldb_writebatch_t* batch=leveldb_writebatch_create();


  int64_t diff = zclock_time ();

//write into database_helper
  iter=0;
  while( iter < N_KEYS)
    {
     sec_iter=0;
     while((iter<N_KEYS) && (sec_iter<N_BATCH)){
      unsigned int key = tinymt32_generate_uint32 (&tinymt32);
      unsigned int val = tinymt32_generate_uint32 (&tinymt32);
      leveldb_writebatch_put (batch,
		   (const char *) &key, sizeof (int),
		   (const char *) &val, sizeof (int));
    sec_iter++;
    iter++;
    }
      leveldb_write(
    db_helper,
    writeoptions,
    batch,
    &errptr);

      if(errptr!=NULL){
      printf("\n%s",errptr);
      }
      assert (errptr == NULL);
      leveldb_writebatch_clear(batch);
    }
  
      leveldb_writebatch_destroy(batch);

  diff = zclock_time () - diff;

  printf ("\nrandom write:  %d", diff);


  diff = zclock_time ();
//write sequentially into db

  leveldb_iterator_t *liter = leveldb_create_iterator (db_helper,
						       readoptions);

  leveldb_iter_seek_to_first (liter);

  while (leveldb_iter_valid (liter))
    {
      size_t length;
      char key[4];
      memcpy (key, leveldb_iter_key (liter, &length), 4);
      char val[4];
      memcpy (val, leveldb_iter_value (liter, &length), 4);

      leveldb_iter_get_error (liter, &errptr);
      assert (errptr == NULL);
      leveldb_put (db,
		   writeoptions,
		   (const char *) &key, sizeof (int),
		   (const char *) &val, sizeof (int), &errptr);
      assert (errptr == NULL);
      leveldb_iter_next (liter);
    }


  diff = zclock_time () - diff;




  leveldb_close (db);
  leveldb_close (db_helper);





}
예제 #13
0
파일: db.c 프로젝트: WuGaohang/ktsdb
unsigned char db_iterator_valid(db_iterator_t *it) {
	return leveldb_iter_valid(it);
}
예제 #14
0
static PyObject* RangeIterator_next(RangeIterator  *iter)
{

    VERBOSE("%s", "Entering RangeIterator_next...");
	if (iter->_db == 0 || !leveldb_iter_valid(iter->_iterator)) {
		RangeIterator_clean(iter);
		return 0;
	}
	if (iter->_to != NULL) {
        const char *tkey; /* c style key */
        size_t key_len; /* key length */
        if (iter->_iterator != NULL) {
            tkey = leveldb_iter_key(iter->_iterator, &key_len);
        } else return NULL;
      
        int limit_len = (strlen(iter->_to) > key_len) ? key_len: strlen(iter->_to);
 
        int c = memcmp(iter->_to, tkey, limit_len);
		if (!(0 <= c)) {
            VERBOSE("%s", "Invalid iterator................................");
			RangeIterator_clean(iter);
			return 0;
		}
	}
 

	// get key and (optional) value
    const char *ckey; /* c style key */
    size_t key_len; /* key length */
	if (iter->_iterator != NULL) {
		ckey = leveldb_iter_key(iter->_iterator, &key_len);
	} else return NULL;

    VERBOSE("The key is %s, length is %d", ckey, key_len);
	PyObject* key = PyString_FromStringAndSize(ckey, key_len);
	PyObject* value = 0;
	PyObject* ret = key;

	if (key == 0) return 0;

	const char *cvalue; /* c style value */
	size_t value_len; /* value length */
	if (iter->_iterator != NULL) {
		cvalue = leveldb_iter_value(iter->_iterator, &value_len);
	} else return NULL;
	if (iter->_include_value) {
		value = PyString_FromStringAndSize(cvalue, value_len);

		if (value == 0) {
			Py_XDECREF(key);
			return 0;
		}
	}

	// key/value pairs are returned as 2-tuples
	if (value != NULL) {
		ret = PyTuple_New(2);

		if (ret == 0) {
			Py_DECREF(key);
			Py_XDECREF(value);
			return 0;
		}

		PyTuple_SET_ITEM(ret, 0, key);
		PyTuple_SET_ITEM(ret, 1, value);
	}

	// get next value
    leveldb_iter_next(iter->_iterator);

	// return k/v pair or single key
	return ret;
}
예제 #15
0
    char *ldb_values(struct _leveldb_stuff *ldbs, const char *ptn, size_t ptn_len, int *size)
    {
        char *err = NULL;
        char *result = NULL;
        char *p_dst = NULL;
        char *p_key = NULL;
        char *p_value = NULL;
        size_t klen = 0;
        size_t vlen = 0;
        int index = 0;
        int i, j, z = 0;
        struct kv_list list = { 0 };
        struct some_kv *p_new, *p_old, *p_tmp = NULL;

        leveldb_iterator_t* iter = leveldb_create_iterator(ldbs->db, ldbs->roptions);
        leveldb_iterator_t* iter_save = iter;
        if (!!leveldb_iter_valid(iter)) {/* first use it is invalid */
            fprintf(stderr, "%s:%d: this iter is valid already!\n", __FILE__, __LINE__);
            *size = -1;
            return NULL ;
        }

        leveldb_iter_seek_to_first(iter);
        p_old = p_new = &list.head;
        while (leveldb_iter_valid(iter)) {
            /* parse kv */
            p_key = (char *) leveldb_iter_key(iter, &klen);
            log_debug("%p iter key = %s, klen = %ld\n", p_key, p_key, klen);
            p_value = (char *) leveldb_iter_value(iter, &vlen);

            leveldb_iter_get_error(iter, &err);
            if (err) {
                goto FAIL_ITER_PARSE;
            }

            if (!string_match_len(ptn, ptn_len, p_value, vlen, 0)) {
                leveldb_iter_next(iter);
                continue;
            }

            /* save parse */
            list.count++;/* kv counts */
            list.klens += klen;
            list.knubs += get_number_len(klen);
            index = list.count % SOME_KV_NODES_COUNT;
            if ((list.count / SOME_KV_NODES_COUNT >= 1) && (index == 1)) {
                /* new store */
                p_new = malloc(sizeof(struct some_kv));
                if (p_new == NULL ) {
                    /* free store */
                    index = GET_NEED_COUNT( list.count, SOME_KV_NODES_COUNT );
                    p_tmp = &list.head;
                    for (i = 0, z = list.count - 1; (i < index) && (p_tmp != NULL ); i++ ) {
                        for (j = 0; (j < SOME_KV_NODES_COUNT) && (z > 0); j++, z--) {
                            free(p_tmp->nodes[j].key);
                        }
                        p_old = p_tmp;
                        p_tmp = p_tmp->next;
                        if (p_old != &list.head) {
                            free(p_old);
                        }
                    }
                    goto FAIL_MEMORY;
                }
                memset(p_new, 0, sizeof(struct some_kv));
                p_old->next = p_new;
                p_new->prev = p_old;
                p_old = p_new;
            }

            /*
             * fix bug: index is error if list.count = n * SOME_KV_NODES_COUNT(1024),
             *          SOME_KV_NODES_COUNT = 1024, n > 0.
             */
            if (index == 0) {
                index = SOME_KV_NODES_COUNT;
            }

            /* save key */
            p_new->nodes[index - 1].klen = klen;
            p_new->nodes[index - 1].key = malloc(GET_NEED_COUNT( klen, G_PAGE_SIZE ) * G_PAGE_SIZE);
            memcpy(p_new->nodes[index - 1].key, p_key, klen);

            /* find next */
            leveldb_iter_next(iter);
        }

        /* create result */
        if (list.count > 0) {
            /* has members */
            /* *2\r\n$5\r\nmykey\r\n$5\r\nmyval\r\n */
            *size = strlen("*\r\n") + get_number_len(list.count) + strlen("$\r\n\r\n") * (list.count)
                + list.knubs + list.klens;
            index = GET_NEED_COUNT( *size, G_PAGE_SIZE ) * G_PAGE_SIZE;
            result = (char *) malloc(index);
            if (result == NULL ) goto FAIL_MEMORY;
            memset(result, 0, index);
            log_debug("----->>>ALL SIZE IS %d, BUFF %p : LEN IS %d\n", *size, result, index);

            /* split piece */
            index = GET_NEED_COUNT( list.count, SOME_KV_NODES_COUNT );
            p_tmp = &list.head;
            sprintf(result, "*%d\r\n", list.count);
            p_dst = result + strlen(result);
            for (i = 0, z = list.count; (i < index) && (p_tmp != NULL ); i++ ) {
                for (j = 0; (j < SOME_KV_NODES_COUNT) && (z > 0); j++, z--) {
                    p_dst = set_bulk(p_dst, p_tmp->nodes[j].key, p_tmp->nodes[j].klen);
                    free(p_tmp->nodes[j].key);
                }
                p_old = p_tmp;
                p_tmp = p_tmp->next;
                if (p_old != &list.head) {
                    free(p_old);
                }
            }
        } else {
            /* no members */
            *size = 0;
        }

        leveldb_iter_destroy(iter_save);
        return result;
FAIL_ITER_PARSE: fprintf(stderr, "%s:%d: %s\n", __FILE__, __LINE__, (err));
                 leveldb_free(err);
                 err = NULL;
                 leveldb_iter_destroy(iter);
                 *size = -1;
                 return NULL ;
FAIL_MEMORY: fprintf(stderr, "%s:%d: FAILED MALLOC !\n", __FILE__, __LINE__);
             leveldb_iter_destroy(iter);
             *size = -1;
             return NULL ;
    }
예제 #16
0
파일: txdb.c 프로젝트: haraldh/bitc
void
txdb_confirm_one_tx(struct txdb   *txdb,
                    const uint256 *blkHash,
                    const uint256 *txHash)
{
    leveldb_iterator_t *iter;
    struct tx_entry *txe;
    char bkHashStr[80];
    char txHashStr[80];

    ASSERT(!uint256_iszero(blkHash));
    ASSERT(!uint256_iszero(txHash));

    txe = txdb_get_tx_entry(txdb, txHash);
    if (txe == NULL) {
        return;
    }

    if (txe->relevant == 0) {
        txdb_remove_from_hashtable(txdb, txHash);
        NOT_TESTED();
        return;
    }

    if (!uint256_iszero(&txe->blkHash)) {
        /*
         * It's possible for the ASSERT below to fire if a tx is confirmed in
         * a block that is later orphaned. The tx should then be relayed again
         * until it finds its way in a new block.
         */
        ASSERT(uint256_issame(&txe->blkHash, blkHash));
        return;
    }

    peergroup_stop_broadcast_tx(btc->peerGroup, txHash);
    memcpy(&txe->blkHash, blkHash, sizeof *blkHash);

    uint256_snprintf_reverse(bkHashStr, sizeof bkHashStr, blkHash);
    uint256_snprintf_reverse(txHashStr, sizeof txHashStr, txHash);
    Warning(LGPFX" %s confirmed in %s\n", txHashStr, bkHashStr);

    NOT_TESTED();

    iter = leveldb_create_iterator(txdb->db, txdb->rd_opts);
    leveldb_iter_seek_to_first(iter);

    while (leveldb_iter_valid(iter)) {
        struct tx_ser_key *txkey;
        struct tx_ser_data *txdata;
        struct buff *buf;
        const char *key;
        const char *val;
        size_t klen;
        size_t vlen;
        char *err = NULL;

        key = leveldb_iter_key(iter, &klen);
        txkey = txdb_deserialize_tx_key(key, klen);

        if (txkey == NULL || uint256_issame(txHash, &txkey->txHash) == 0) {
            free(txkey);
            leveldb_iter_next(iter);
            continue;
        }

        NOT_TESTED();

        val = leveldb_iter_value(iter, &vlen);
        txdata = txdb_deserialize_tx_data(val, vlen);
        ASSERT(uint256_iszero(&txdata->blkHash));
        ASSERT(txdata->timestamp != 0);
        memcpy(&txdata->blkHash, blkHash, sizeof *blkHash);

        buf = txdb_serialize_tx_data(txdata);

        leveldb_put(txdb->db, txdb->wr_opts, key, klen,
                    buff_base(buf), buff_curlen(buf), &err);
        buff_free(buf);
        if (err) {
            Warning(LGPFX" failed to write tx entry: %s\n", err);
        }

        txdb_export_tx_info(txdb);

        free(txkey);
        free(txdata->buf);
        free(txdata);
        break;
    }
    leveldb_iter_destroy(iter);
}
예제 #17
0
파일: c_test.c 프로젝트: Aleda/tera
int main(int argc, char** argv) {
  leveldb_t* db;
  leveldb_comparator_t* cmp;
  leveldb_cache_t* cache;
  leveldb_env_t* env;
  leveldb_options_t* options;
  leveldb_readoptions_t* roptions;
  leveldb_writeoptions_t* woptions;
  char* err = NULL;
  int run = -1;

  CheckCondition(leveldb_major_version() >= 1);
  CheckCondition(leveldb_minor_version() >= 1);

  snprintf(dbname, sizeof(dbname),
           "%s/leveldb_c_test-%di/meta/0",
           GetTempDir(),
           ((int) geteuid()));

  StartPhase("create_objects");
  cmp = leveldb_comparator_create(NULL, CmpDestroy, CmpCompare, CmpName);
  env = leveldb_create_default_env();
  cache = leveldb_cache_create_lru(100000);

  options = leveldb_options_create();
  leveldb_options_set_comparator(options, cmp);
  leveldb_options_set_error_if_exists(options, 1);
  leveldb_options_set_cache(options, cache);
  leveldb_options_set_env(options, env);
  leveldb_options_set_info_log(options, NULL);
  leveldb_options_set_write_buffer_size(options, 100000);
  leveldb_options_set_paranoid_checks(options, 1);
  leveldb_options_set_max_open_files(options, 10);
  leveldb_options_set_block_size(options, 1024);
  leveldb_options_set_block_restart_interval(options, 8);
  leveldb_options_set_compression(options, leveldb_no_compression);

  roptions = leveldb_readoptions_create();
  leveldb_readoptions_set_verify_checksums(roptions, 1);
  leveldb_readoptions_set_fill_cache(roptions, 0);

  woptions = leveldb_writeoptions_create();
  leveldb_writeoptions_set_sync(woptions, 1);

  StartPhase("destroy");
  leveldb_destroy_db(options, dbname, &err);
  Free(&err);

  StartPhase("open");
  db = leveldb_open(options, dbname, &err);
  CheckNoError(err);
  CheckGet(db, roptions, "foo", NULL);

  StartPhase("put");
  leveldb_put(db, woptions, "foo", 3, "hello", 5, &err);
  CheckNoError(err);
  CheckGet(db, roptions, "foo", "hello");

  StartPhase("compactall");
  leveldb_compact_range(db, NULL, 0, NULL, 0);
  CheckGet(db, roptions, "foo", "hello");

  StartPhase("compactrange");
  leveldb_compact_range(db, "a", 1, "z", 1);
  CheckGet(db, roptions, "foo", "hello");

  StartPhase("writebatch");
  {
    leveldb_writebatch_t* wb = leveldb_writebatch_create();
    leveldb_writebatch_put(wb, "foo", 3, "a", 1);
    leveldb_writebatch_clear(wb);
    leveldb_writebatch_put(wb, "bar", 3, "b", 1);
    leveldb_writebatch_put(wb, "box", 3, "c", 1);
    leveldb_writebatch_delete(wb, "bar", 3);
    leveldb_write(db, woptions, wb, &err);
    CheckNoError(err);
    CheckGet(db, roptions, "foo", "hello");
    CheckGet(db, roptions, "bar", NULL);
    CheckGet(db, roptions, "box", "c");
    int pos = 0;
    leveldb_writebatch_iterate(wb, &pos, CheckPut, CheckDel);
    CheckCondition(pos == 3);
    leveldb_writebatch_destroy(wb);
  }

  StartPhase("iter");
  {
    leveldb_iterator_t* iter = leveldb_create_iterator(db, roptions);
    CheckCondition(!leveldb_iter_valid(iter));
    leveldb_iter_seek_to_first(iter);
    CheckCondition(leveldb_iter_valid(iter));
    CheckIter(iter, "box", "c");
    leveldb_iter_next(iter);
    CheckIter(iter, "foo", "hello");
    leveldb_iter_prev(iter);
    CheckIter(iter, "box", "c");
    leveldb_iter_prev(iter);
    CheckCondition(!leveldb_iter_valid(iter));
    leveldb_iter_seek_to_last(iter);
    CheckIter(iter, "foo", "hello");
    leveldb_iter_seek(iter, "b", 1);
    CheckIter(iter, "box", "c");
    leveldb_iter_get_error(iter, &err);
    CheckNoError(err);
    leveldb_iter_destroy(iter);
  }

  StartPhase("approximate_sizes");
  {
    int i;
    int n = 20000;
    char keybuf[100];
    char valbuf[100];
    uint64_t sizes[2];
    const char* start[2] = { "a", "k00000000000000010000" };
    size_t start_len[2] = { 1, 21 };
    const char* limit[2] = { "k00000000000000010000", "z" };
    size_t limit_len[2] = { 21, 1 };
    leveldb_writeoptions_set_sync(woptions, 0);
    for (i = 0; i < n; i++) {
      snprintf(keybuf, sizeof(keybuf), "k%020d", i);
      snprintf(valbuf, sizeof(valbuf), "v%020d", i);
      leveldb_put(db, woptions, keybuf, strlen(keybuf), valbuf, strlen(valbuf),
                  &err);
      CheckNoError(err);
    }
    leveldb_approximate_sizes(db, 2, start, start_len, limit, limit_len, sizes);
    CheckCondition(sizes[0] > 0);
    CheckCondition(sizes[1] > 0);
  }

  StartPhase("property");
  {
    char* prop = leveldb_property_value(db, "nosuchprop");
/*    CheckCondition(prop == NULL);*/
    prop = leveldb_property_value(db, "leveldb.stats");
    CheckCondition(prop != NULL);
    Free(&prop);
  }

  StartPhase("snapshot");
  {
    const leveldb_snapshot_t* snap;
    snap = leveldb_create_snapshot(db);
    leveldb_delete(db, woptions, "foo", 3, &err);
    CheckNoError(err);
    leveldb_readoptions_set_snapshot(roptions, snap);
    CheckGet(db, roptions, "foo", "hello");
    leveldb_readoptions_set_snapshot(roptions, NULL);
    CheckGet(db, roptions, "foo", NULL);
    leveldb_release_snapshot(db, snap);
  }

  StartPhase("repair");
  {
    leveldb_close(db);
    leveldb_options_set_error_if_exists(options, 0);
    leveldb_repair_db(options, dbname, &err);
    CheckNoError(err);
    db = leveldb_open(options, dbname, &err);
    CheckNoError(err);
    CheckGet(db, roptions, "foo", NULL);
    CheckGet(db, roptions, "bar", NULL);
    CheckGet(db, roptions, "box", "c");
    leveldb_options_set_error_if_exists(options, 1);
  }

  StartPhase("filter");
  for (run = 0; run < 2; run++) {
    // First run uses custom filter, second run uses bloom filter
    CheckNoError(err);
    leveldb_filterpolicy_t* policy;
    if (run == 0) {
      policy = leveldb_filterpolicy_create(
          NULL, FilterDestroy, FilterCreate, FilterKeyMatch, FilterName);
    } else {
      policy = leveldb_filterpolicy_create_bloom(10);
    }

    // Create new database
    leveldb_close(db);
    leveldb_destroy_db(options, dbname, &err);
    leveldb_options_set_filter_policy(options, policy);
    db = leveldb_open(options, dbname, &err);
    CheckNoError(err);
    leveldb_put(db, woptions, "foo", 3, "foovalue", 8, &err);
    CheckNoError(err);
    leveldb_put(db, woptions, "bar", 3, "barvalue", 8, &err);
    CheckNoError(err);
    leveldb_compact_range(db, NULL, 0, NULL, 0);

    fake_filter_result = 1;
    CheckGet(db, roptions, "foo", "foovalue");
    CheckGet(db, roptions, "bar", "barvalue");
    if (phase == 0) {
      // Must not find value when custom filter returns false
      fake_filter_result = 0;
      CheckGet(db, roptions, "foo", NULL);
      CheckGet(db, roptions, "bar", NULL);
      fake_filter_result = 1;

      CheckGet(db, roptions, "foo", "foovalue");
      CheckGet(db, roptions, "bar", "barvalue");
    }
    leveldb_options_set_filter_policy(options, NULL);
    leveldb_filterpolicy_destroy(policy);
  }

  StartPhase("cleanup");
  leveldb_close(db);
  leveldb_options_destroy(options);
  leveldb_readoptions_destroy(roptions);
  leveldb_writeoptions_destroy(woptions);
  leveldb_cache_destroy(cache);
  leveldb_comparator_destroy(cmp);
  leveldb_env_destroy(env);

  fprintf(stderr, "PASS\n");
  return 0;
}
예제 #18
0
int mdhim_leveldb_get_prev(void *dbh, void **key, int *key_len, 
			   void **data, int32_t *data_len, 
			   struct mdhim_store_opts_t *mstore_opts) {
	leveldb_readoptions_t *options;
	leveldb_t *db = (leveldb_t *) dbh;
	int ret = MDHIM_SUCCESS;
	leveldb_iterator_t *iter;
	const char *res;
	int len = 0;
	void *old_key;
	int old_key_len;
	struct timeval start, end;

	//Init the data to return
	*data = NULL;
	*data_len = 0;

	gettimeofday(&start, NULL);
	//Create the options and iterator
	options = (leveldb_readoptions_t *) mstore_opts->db_ptr3;
	old_key = *key;
	old_key_len = *key_len;
	*key = NULL;
	*key_len = 0;

	iter = leveldb_create_iterator(db, options);

	//If the user didn't supply a key, then seek to the last
	if (!old_key || old_key_len == 0) {
		leveldb_iter_seek_to_last(iter);
	} else {
		leveldb_iter_seek(iter, old_key, old_key_len);
		if (!leveldb_iter_valid(iter)) { 
			mlog(MDHIM_SERVER_DBG2, "Could not get a valid iterator in leveldb");
			goto error;
		}

		leveldb_iter_prev(iter);
	}

	if (!leveldb_iter_valid(iter)) {
		goto error;
	}

	res = leveldb_iter_value(iter, (size_t *) &len);
	if (res) {
		*data = malloc(len);
		memcpy(*data, res, len);
		*data_len = len;
	} else {
		*data = NULL;
		*data_len = 0;
	}

	res = leveldb_iter_key(iter, (size_t *) key_len);
	if (res) {
		*key = malloc(*key_len);
		memcpy(*key, res, *key_len);
	} else {
		*key = NULL;
		*key_len = 0;
	}

	if (!*data) {
		goto error;
	}

        //Destroy iterator
	leveldb_iter_destroy(iter);      
	gettimeofday(&end, NULL);
	mlog(MDHIM_SERVER_DBG, "Took: %d seconds to get the prev record", 
	     (int) (end.tv_sec - start.tv_sec));
	return ret;

error:	
	 //Destroy iterator
	leveldb_iter_destroy(iter);      
	*key = NULL;
	*key_len = 0;
	*data = NULL;
	*data_len = 0;
	return MDHIM_DB_ERROR;
}