Beispiel #1
0
CTEST(node_serial_test, leaf_empty)
{
	int ret = 0;
	int fd = ness_os_open(BRT_FILE, O_RDWR | O_CREAT, 0777);
	struct block *b = block_new();
	struct hdr *hdr = (struct hdr*)xcalloc(1, sizeof(*hdr));
	struct options *opts = options_new();

	/*
	 * dummy brt leaf
	 */
	uint64_t nid = 3;

	struct node *dummy_leaf = leaf_alloc_empty(nid);
	leaf_alloc_bsm(dummy_leaf);
	ret = serialize_node_to_disk(fd, b, dummy_leaf, hdr);
	ASSERT_TRUE(ret > 0);
	//free
	node_free(dummy_leaf);

	struct node *dummy_leaf1;
	ret = deserialize_node_from_disk(fd, b, nid, &dummy_leaf1, 0);
	ASSERT_TRUE(ret > 0);
	ASSERT_EQUAL(0, basement_count(dummy_leaf1->u.l.le->bsm));
	//free
	node_free(dummy_leaf1);

	ness_os_close(fd);
	block_free(b);
	xfree(hdr);
	options_free(opts);
	xcheck_all_free();
}
Beispiel #2
0
struct logr *logr_open(struct options *opts, uint64_t logsn) {
	int flag;
	mode_t mode;
	struct logr *lgr;
	char name[FILE_NAME_MAXLEN];

	mode = S_IRWXU | S_IRWXG | S_IRWXO;
	flag = O_RDONLY | O_BINARY;

	memset(name, 0, FILE_NAME_MAXLEN);
	snprintf(name, FILE_NAME_MAXLEN, "%s/ness.redo.%" PRIu64,
	         opts->redo_path, logsn);
	lgr = xcalloc(1, sizeof(*lgr));
	lgr->fd = ness_os_open(name, flag, mode);
	lgr->fsize = _file_size(name);
	lgr->base = xmalloc(LOG_BASE_SIZE);
	lgr->base_size = LOG_BASE_SIZE;

	return lgr;
}
Beispiel #3
0
struct logw *logw_open(struct options *opts, uint64_t logsn) {
	int flag;
	mode_t mode;
	struct logw *lgw;
	char name[FILE_NAME_MAXLEN];

	mode = S_IRWXU | S_IRWXG | S_IRWXO;
	flag = O_CREAT | O_WRONLY | O_BINARY;

	ness_check_dir(opts->redo_path);
	memset(name, 0, FILE_NAME_MAXLEN);
	snprintf(name, FILE_NAME_MAXLEN, "%s/ness.redo.%" PRIu64,
	         opts->redo_path, logsn);
	lgw = xcalloc(1, sizeof(*lgw));
	lgw->base_size = (1 << 20);
	lgw->base = xcalloc(lgw->base_size, sizeof(char*));
	lgw->fd = ness_os_open(name, flag, mode);
	nassert(lgw->fd > -1);

	return lgw;
}
Beispiel #4
0
CTEST(node_serial_test, leaf_2_record)
{
	int ret = 0;
	int fd = ness_os_open(BRT_FILE, O_RDWR | O_CREAT, 0777);
	struct block *b = block_new();
	struct hdr *hdr = (struct hdr*)xcalloc(1, sizeof(*hdr));
	struct options *opts = options_new();

	/*
	 * dummy brt leaf
	 */
	uint64_t nid = 3;

	struct node *dummy_leaf = leaf_alloc_empty(nid);
	leaf_alloc_bsm(dummy_leaf);

	MSN msn = 0U;
	struct xids *xids = NULL;
	struct msg k, v;
	k.size = 6;
	k.data = "hello";
	v.size = 6;
	v.data = "world";
	basement_put(dummy_leaf->u.l.le->bsm, &k, &v, MSG_INSERT, msn, xids);

	struct msg k1, v1;
	k1.size = 6;
	k1.data = "hellx";
	v1.size = 6;
	v1.data = "worlx";
	basement_put(dummy_leaf->u.l.le->bsm, &k1, &v1, MSG_INSERT, msn, xids);

	ret = serialize_node_to_disk(fd, b, dummy_leaf, hdr);
	ASSERT_TRUE(ret > 0);

	//free
	node_free(dummy_leaf);

	struct node *dummy_leaf1;
	ret = deserialize_node_from_disk(fd, b, nid, &dummy_leaf1, 0);
	ASSERT_TRUE(ret > 0);

	ASSERT_EQUAL(2, basement_count(dummy_leaf1->u.l.le->bsm));

	struct basement_iter iter;
	basement_iter_init(&iter, dummy_leaf1->u.l.le->bsm);
	basement_iter_seek(&iter, &k);
	ASSERT_EQUAL(1, iter.valid);
	ASSERT_STR("world", iter.val.data);

	basement_iter_seek(&iter, &k1);
	ASSERT_EQUAL(1, iter.valid);
	ASSERT_STR("worlx", iter.val.data);

	//free
	node_free(dummy_leaf1);

	ness_os_close(fd);
	block_free(b);
	xfree(hdr);
	options_free(opts);
	xcheck_all_free();
}
Beispiel #5
0
CTEST(node_serial_test, node_2th_part_empty)
{
	int ret = 0;
	NID nid;
	uint32_t n_children = 3;
	int fd = ness_os_open(BRT_FILE, O_RDWR | O_CREAT, 0777);
	struct block *b = block_new();
	struct hdr *hdr = (struct hdr*)xcalloc(1, sizeof(*hdr));
	struct options *opts = options_new();

	/*
	 * serialize
	 */
	struct node *dummy_node;

	hdr->last_nid++;
	nid = hdr->last_nid;
	dummy_node = nonleaf_alloc_empty(nid, 1, n_children);
	nonleaf_alloc_buffer(dummy_node);

	struct msg p0;
	p0.size = 6;
	p0.data = "pivot0";
	msgcpy(&dummy_node->u.n.pivots[0], &p0);

	struct msg p1;
	p1.size = 6;
	p1.data = "pivot1";
	msgcpy(&dummy_node->u.n.pivots[1], &p1);

	MSN msn = 0U;
	struct xids *xids = NULL;
	struct msg k, v;
	k.size = 5;
	k.data = "hello";
	v.size = 5;
	v.data = "world";
	basement_put(dummy_node->u.n.parts[0].buffer, &k, &v, MSG_INSERT, msn,
	             xids);


	hdr->method = NESS_QUICKLZ_METHOD;
	ret = serialize_node_to_disk(fd, b, dummy_node, hdr);
	ASSERT_TRUE(ret > 0);
	node_free(dummy_node);

	//deserialize
	int light = 0;
	struct node *dummy_node1;
	ret = deserialize_node_from_disk(fd, b, nid, &dummy_node1, light);
	ASSERT_TRUE(ret > 0);

	ASSERT_EQUAL(1, dummy_node1->height);
	ASSERT_EQUAL(3, dummy_node1->u.n.n_children);
	ASSERT_DATA((const unsigned char*)"pivot0",
	            6,
	            (const unsigned char*)dummy_node1->u.n.pivots[0].data,
	            dummy_node1->u.n.pivots[0].size);

	ASSERT_DATA((const unsigned char*)"pivot1",
	            6,
	            (const unsigned char*)dummy_node1->u.n.pivots[1].data,
	            dummy_node1->u.n.pivots[1].size);
	ASSERT_EQUAL(3, dummy_node1->u.n.n_children);

	if (!light) {
		int cmp;
		struct basement_iter iter;
		struct basement *bsm;

		bsm = dummy_node1->u.n.parts[0].buffer;
		basement_iter_init(&iter, bsm);

		int mb_c = basement_count(dummy_node1->u.n.parts[0].buffer);
		ASSERT_EQUAL(1, mb_c);
		basement_iter_seek(&iter, &k);
		ret = basement_iter_valid(&iter);
		ASSERT_EQUAL(1, ret);
		cmp = msg_key_compare(&k, &iter.key);
		ASSERT_EQUAL(0, cmp);
		cmp = msg_key_compare(&v, &iter.val);
		ASSERT_EQUAL(0, cmp);

		mb_c = basement_count(dummy_node1->u.n.parts[1].buffer);
		ASSERT_EQUAL(0, mb_c);
	}
	node_free(dummy_node1);

	ness_os_close(fd);
	block_free(b);
	xfree(hdr);
	options_free(opts);
	xcheck_all_free();
}
Beispiel #6
0
struct tree *tree_open(const char *dbname,
                       struct env *e,
                       struct tree_callback *tcb) {
	int fd;
	int flag;
	mode_t mode;
	int is_create = 0;
	struct tree *t;
	struct node *root;
	struct cache_file *cf;

	t = xcalloc(1, sizeof(*t));
	t->e = e;

	mode = S_IRWXU | S_IRWXG | S_IRWXO;
	flag = O_RDWR | O_BINARY;
	if (e->use_directio)
		fd = ness_os_open_direct(dbname, flag, mode);
	else
		fd = ness_os_open(dbname, flag, mode);

	if (fd == -1) {
		if (e->use_directio)
			fd = ness_os_open(dbname, flag | O_CREAT, mode);
		else
			fd = ness_os_open_direct(dbname, flag | O_CREAT, mode);
		if (fd == -1)
			goto ERR;
		is_create = 1;
	}

	t->fd = fd;
	t->hdr = hdr_new(e);

	/* tree header */
	if (!is_create) {
		tcb->fetch_hdr_cb(fd, t->hdr);
	}

	/* create cache file */
	cf = cache_file_create(e->cache, t->fd, t->hdr, tcb);
	t->cf = cf;

	/* tree root node */
	if (is_create) {
		NID nid = hdr_next_nid(t->hdr);
		node_create(nid, 0, 1, t->hdr->version, t->e, &root);
		cache_put_and_pin(cf, nid, root);
		root->isroot = 1;
		node_set_dirty(root);

		cache_unpin(cf, root->cpair, make_cpair_attr(root));
		t->hdr->root_nid = root->nid;
		__DEBUG("create new root, NID %"PRIu64, root->nid);
	} else {
		/* get the root node */
		if (cache_get_and_pin(cf, t->hdr->root_nid, (void**)&root, L_READ) != NESS_OK)
			__PANIC("get root from cache error [%" PRIu64 "]", t->hdr->root_nid);

		root->isroot = 1;
		cache_unpin(cf, root->cpair, make_cpair_attr(root));
		__DEBUG("fetch root, NID %"PRIu64, root->nid);
	}

	return t;

ERR:
	xfree(t);
	return NESS_ERR;
}