Пример #1
0
CTEST(basement, empty)
{
	int ret;
	struct basement_iter iter;
	struct basement *bsm = basement_new();

	basement_iter_init(&iter, bsm);
	basement_iter_seektofirst(&iter);
	ret = basement_iter_valid(&iter);
	ASSERT_EQUAL(0, ret);

	struct msg m1 = {
		.size = 6,
		.data = "key-01"
	};

	basement_iter_seek(&iter, &m1);
	ret = basement_iter_valid(&iter);
	ASSERT_EQUAL(0, ret);

	basement_free(bsm);
	xcheck_all_free();
}

void _random_key(char *key,int length) {
	int i;
	char salt[36]= "abcdefghijklmnopqrstuvwxyz123456789";

	for (i = 0; i < length; i++)
		key[i] = salt[rand() % 36];
}
Пример #2
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();
}
Пример #3
0
CTEST(basement, insert_and_lookup)
{
	int i;
	int ret;
	int R = 1000;
	char kbuf[KEY_SIZE];
	char vbuf[VAL_SIZE];
	struct basement_iter iter;
	struct basement *bsm = basement_new();
	struct msg **msgs = xcalloc(R, sizeof(*msgs));
	struct txnid_pair xidpair =  {
		.child_xid = TXNID_NONE,
		.parent_xid = TXNID_NONE
	};

	MSN msn = 0U;

	_random_key(vbuf, VAL_SIZE);
	for (i = 0; i < R; i++) {
		memset(kbuf, 0, KEY_SIZE);
		snprintf(kbuf, KEY_SIZE, "key-%d", i);

		struct msg k = {.data = kbuf, .size = KEY_SIZE};
		struct msg v = {.data = vbuf, .size = VAL_SIZE};

		msgs[i] = msgdup(&k);
		basement_put(bsm,
		             msn++,
		             MSG_INSERT,
		             &k,
		             &v,
		             &xidpair
		            );
	}

	basement_iter_init(&iter, bsm);
	basement_iter_seektolast(&iter);
	ret = basement_iter_valid(&iter);
	ASSERT_EQUAL(1, ret);

	for (i = 0; i < R; i++) {
		basement_iter_seek(&iter, msgs[i]);
		ret = basement_iter_valid(&iter);
		ASSERT_EQUAL(1, ret);
		ret = msg_key_compare(msgs[i], &iter.key);
		ASSERT_EQUAL(0, ret);
	}

	/* do msg free */
	for (i = 0; i < R; i++) {
		xfree(msgs[i]->data);
		xfree(msgs[i]);
	}
	xfree(msgs);

	basement_free(bsm);
	xcheck_all_free();
}
Пример #4
0
CTEST(cron_test, start)
{
	uint32_t c = 0;
	struct cron *cron = cron_new(run_100ms, 100);

	cron_start(cron, &c);
	usleep(550000); // 500ms
	cron_stop(cron);

	ASSERT_EQUAL(3, c);

	cron_free(cron);
	xcheck_all_free();
}
Пример #5
0
CTEST(cron_test, change_period)
{
	uint32_t c = 0;
	struct cron *cron = cron_new(run_0sec, 1000000);

	cron_start(cron, &c);

	cron_change_period(cron, 100);
	usleep(350000); // 300ms
	cron_stop(cron);

	ASSERT_EQUAL(3, c);

	cron_free(cron);
	xcheck_all_free();
}
Пример #6
0
CTEST(cron_test, signal)
{
	uint32_t c = 0;
	struct cron *cron = cron_new(run_0sec, 100000000);

	cron_start(cron, &c);

	usleep(1000); // 10ms

	cron_signal(cron);
	usleep(1000); // 10ms

	ASSERT_EQUAL(1, c);

	cron_stop(cron);
	cron_free(cron);
	xcheck_all_free();
}
Пример #7
0
CTEST(cursor, empty)
{
	int ret;
	struct options *opts;
	struct status *status;
	struct cache *cache;
	struct tree *tree;

	/* create */
	opts = options_new();
	status = status_new();
	cache = dbcache_new(opts);
	tree = tree_new(DBPATH0, opts, status, cache, 1);


	/* cursor */
	struct cursor *cur;

	cur = cursor_new(tree);

	/* first */
	tree_cursor_first(cur);
	ret = tree_cursor_valid(cur);
	ASSERT_EQUAL(0, ret);

	/* last */
	tree_cursor_last(cur);
	ret = tree_cursor_valid(cur);
	ASSERT_EQUAL(0, ret);

	/* next */
	tree_cursor_next(cur);
	ret = tree_cursor_valid(cur);
	ASSERT_EQUAL(0, ret);

	cursor_free(cur);

	/* free */
	dbcache_free(cache);
	tree_free(tree);
	options_free(opts);
	status_free(status);
	xcheck_all_free();
}
Пример #8
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();
}
Пример #9
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();
}
Пример #10
0
CTEST(basement, multiversion)
{
	int i;
	int ret;
	int R = 123;
	char kbuf[KEY_SIZE];
	char vbuf[VAL_SIZE];
	struct basement_iter iter;
	struct basement *bsm = basement_new();
	struct txnid_pair xidpair =  {
		.child_xid = TXNID_NONE,
		.parent_xid = TXNID_NONE
	};

	_random_key(vbuf, VAL_SIZE);

	MSN msn = 0U;
	for (i = 0; i < R; i++) {
		memset(kbuf, 0, KEY_SIZE);
		snprintf(kbuf, KEY_SIZE, "key-%d", i);

		struct msg k = {.data = kbuf, .size = KEY_SIZE};
		struct msg v = {.data = vbuf, .size = VAL_SIZE};

		basement_put(bsm,
		             msn++,
		             MSG_INSERT,
		             &k,
		             &v,
		             &xidpair
		            );
	}

	/* 3 versions of key-66 */
	R = 3;
	for (i = 0; i < R; i++) {
		memset(kbuf, 0, KEY_SIZE);
		snprintf(kbuf, KEY_SIZE, "key-66");

		struct msg k = {.data = kbuf, .size = KEY_SIZE};
		struct msg v = {.data = vbuf, .size = VAL_SIZE};

		basement_put(bsm,
		             msn++,
		             MSG_INSERT,
		             &k,
		             &v,
		             &xidpair
		            );
	}

	/* 5 versions of key-88 */
	R = 5;
	for (i = 0; i < R; i++) {
		memset(kbuf, 0, KEY_SIZE);
		snprintf(kbuf, KEY_SIZE, "key-88");

		struct msg k = {.data = kbuf, .size = KEY_SIZE};
		struct msg v = {.data = vbuf, .size = VAL_SIZE};

		basement_put(bsm,
		             msn++,
		             MSG_INSERT,
		             &k,
		             &v,
		             &xidpair
		            );
	}

	/* key-66 */
	memset(kbuf, 0, KEY_SIZE);
	snprintf(kbuf, KEY_SIZE, "key-66");

	struct msg k1 = {.data = kbuf, .size = KEY_SIZE};
	basement_iter_init(&iter, bsm);
	basement_iter_seek(&iter, &k1);
	ret = basement_iter_valid(&iter);
	ASSERT_EQUAL(1, ret);
	ASSERT_EQUAL(66, iter.msn);

	basement_iter_next(&iter);
	ret = basement_iter_valid(&iter);
	ASSERT_EQUAL(1, ret);
	ASSERT_EQUAL(123, iter.msn);

	/* less than valid */
	ret = basement_iter_valid_lessorequal(&iter, &k1);
	ASSERT_EQUAL(1, ret);

	memset(kbuf, 0, KEY_SIZE);
	snprintf(kbuf, KEY_SIZE, "key-67");
	k1.size = KEY_SIZE;
	k1.data = kbuf;
	ret = basement_iter_valid_lessorequal(&iter, &k1);
	ASSERT_EQUAL(1, ret);

	memset(kbuf, 0, KEY_SIZE);
	snprintf(kbuf, KEY_SIZE, "key-65");
	k1.size = KEY_SIZE;
	k1.data = kbuf;
	ret = basement_iter_valid_lessorequal(&iter, &k1);
	ASSERT_EQUAL(0, ret);

	/* key-88 */
	memset(kbuf, 0, KEY_SIZE);
	snprintf(kbuf, KEY_SIZE, "key-88");

	struct msg k2 = {.data = kbuf, .size = KEY_SIZE};
	basement_iter_seek(&iter, &k2);
	ret = basement_iter_valid(&iter);
	ASSERT_EQUAL(1, ret);
	ASSERT_EQUAL(88, iter.msn);

	basement_iter_next(&iter);
	basement_iter_next(&iter);
	ret = basement_iter_valid(&iter);
	ASSERT_EQUAL(1, ret);
	ASSERT_EQUAL(127, iter.msn);

	/* next&prev diff key */
	/* key-87 */
	memset(kbuf, 0, KEY_SIZE);
	snprintf(kbuf, KEY_SIZE, "key-87");

	struct msg k3 = {.data = kbuf, .size = KEY_SIZE};
	basement_iter_prev_diff_key(&iter);
	ret = msg_key_compare(&k3, &iter.key);
	ASSERT_EQUAL(0, ret);

	/* key-88 */
	basement_iter_next(&iter);
	memset(kbuf, 0, KEY_SIZE);
	snprintf(kbuf, KEY_SIZE, "key-88");
	struct msg k22 = {.data = kbuf, .size = KEY_SIZE};
	ret = msg_key_compare(&k22, &iter.key);
	ASSERT_EQUAL(0, ret);

	/* key-89 */
	memset(kbuf, 0, KEY_SIZE);
	snprintf(kbuf, KEY_SIZE, "key-89");

	struct msg k4 = {.data = kbuf, .size = KEY_SIZE};
	basement_iter_next_diff_key(&iter);
	ret = msg_key_compare(&k4, &iter.key);
	ASSERT_EQUAL(0, ret);

	basement_free(bsm);
	xcheck_all_free();
}