Exemplo n.º 1
0
Arquivo: main.c Projeto: wdalmut/tree
static char *test_list_existing_leafs() {
    leaf *root = (leaf *)malloc(sizeof(leaf));
    root->value = 100;

    btree_put(root, 110);
    btree_put(root, 87);
    btree_put(root, 90);
    btree_put(root, 153);
    btree_put(root, 120);
    btree_put(root, 124);

    result *node = btree_inorder(root, NULL);

    mu_assert("The result list should not be empty", node != NULL);

    mu_assert("First element should be 87",    node->value == 87);
    mu_assert("Second element should be 90",   node->next->value == 90);
    mu_assert("Third element should be 100",   node->next->next->value == 100);
    mu_assert("Fourth element should be 110",  node->next->next->next->value == 110);
    mu_assert("Fifth element should be 120",   node->next->next->next->next->value == 120);
    mu_assert("Sixth element should be 124",   node->next->next->next->next->next->value == 124);
    mu_assert("Seventh element should be 153", node->next->next->next->next->next->next->value == 153);

    return 0;
}
Exemplo n.º 2
0
Arquivo: db.cpp Projeto: nyaxt/ptnk
void
DB::Tx::put(BufferCRef key, BufferCRef value, put_mode_t mode)
{
	OverviewPage pgOvv(m_pio->readPage(m_pio->pgidStartPage()));

	page_id_t pgidOldRoot = pgOvv.getDefaultTableRoot();
	page_id_t pgidNewRoot = btree_put(pgidOldRoot, key, value, mode, m_pio.get());
	// m_pio->notifyPageWOldLink(pgOvv.pageOrigId()); // this can be safely omitted
	
	// handle root node update
	if(pgidNewRoot != pgidOldRoot)
	{
		pgOvv.setDefaultTableRoot(pgidNewRoot, NULL, m_pio.get());
	}
}
Exemplo n.º 3
0
Arquivo: db.cpp Projeto: nyaxt/ptnk
void
DB::Tx::put(TableOffCache* table, BufferCRef key, BufferCRef value, put_mode_t mode)
{
	OverviewPage pgOvv(m_pio->readPage(m_pio->pgidStartPage()));

	page_id_t pgidOldRoot = pgOvv.getTableRoot(table);
	if(pgidOldRoot == PGID_INVALID) PTNK_THROW_RUNTIME_ERR("table not found");
	page_id_t pgidNewRoot = btree_put(pgidOldRoot, key, value, mode, m_pio.get());
	// m_pio->notifyPageWOldLink(pgOvv.pageOrigId()); // this can be safely omitted
	
	// handle root node update
	if(pgidNewRoot != pgidOldRoot)
	{
		pgOvv.setTableRoot(table, pgidNewRoot, NULL, m_pio.get());
	}
}
Exemplo n.º 4
0
Arquivo: main.c Projeto: wdalmut/tree
static char *test_insert_new_nodes() {
    leaf *root = (leaf *)malloc(sizeof(leaf));
    root->value = 100;

    btree_put(root, 110);
    btree_put(root, 87);
    btree_put(root, 90);
    btree_put(root, 153);
    btree_put(root, 120);
    btree_put(root, 124);

    return 0;
}
Exemplo n.º 5
0
void post(SoupServer *server,
         SoupMessage *msg,
         const char *path)
{
	JsonParser *parser = json_parser_new ();
	GError *err = NULL;
	g_debug("POST\n path= %s\nbody = '%s'", path, msg->request_body->data);
	if (json_parser_load_from_data (parser, msg->request_body->data, -1, &err)) {
		g_debug("parsed sucessfully");
		GVariant *gv = json_gvariant_deserialize(json_parser_get_root(parser), NULL, NULL);
		if (gv) {
			char *debug = g_variant_print (gv, TRUE);
			g_debug ("converted to %s", debug);
			g_free (debug);
			//We need to store the signature, so package in a v
			GVariant *packaged  = g_variant_new_variant(gv);

			struct btval key;
			key.data = (void*)path+1;
			key.size = strlen(path)-1;
			key.free_data = FALSE;
			key.mp = NULL;
			struct btval val;
			val.data =(void*) g_variant_get_data(packaged);
			val.size = g_variant_get_size(packaged);
			val.free_data = FALSE;
			val.mp = NULL;
			g_debug ("inserting with key %s", (char*)key.data);
			g_debug ("data checksum is %s", g_compute_checksum_for_data(G_CHECKSUM_MD5, val.data, val.size)); 
			int success = btree_put(btree, &key, &val,0);
			if (0!=success) {
				g_debug("put failed: %s)", strerror(errno));
			}
			g_variant_unref (packaged);
			g_variant_unref (gv);	
		}
	} else {
		g_debug("failed to parse json: %s", err->message);
		g_error_free(err);
	}
	soup_message_set_status (msg, SOUP_STATUS_OK);
}
Exemplo n.º 6
0
Arquivo: main.c Projeto: wdalmut/tree
static char *test_get_an_existing_node() {
    leaf *root = (leaf *)malloc(sizeof(leaf));
    root->value = 100;

    btree_put(root, 110);
    btree_put(root, 87);
    btree_put(root, 90);
    btree_put(root, 153);
    btree_put(root, 120);
    btree_put(root, 124);

    leaf *node = btree_get(root, 153);

    mu_assert("The value must be present in the tree", node != NULL);
    mu_assert("The value recorded is not '153'", node->value == 153);

    return 0;
}
Exemplo n.º 7
0
int
main(int argc, char **argv)
{
	int		 c, rc = BT_FAIL;
	unsigned int	 flags = 0;
	struct btree	*bt;
	struct cursor	*cursor;
	const char	*filename = "test.db";
	struct btval	 key, data, maxkey;

	while ((c = getopt(argc, argv, "rf:")) != -1) {
		switch (c) {
		case 'r':
			flags |= BT_REVERSEKEY;
			break;
		case 'f':
			filename = optarg;
			break;
		}
	}

	argc -= optind;
	argv += optind;

	if (argc == 0)
		errx(1, "missing command");

	bt = btree_open(filename, flags | BT_NOSYNC, 0644);
	if (bt == NULL)
		err(1, filename);

	bzero(&key, sizeof(key));
	bzero(&data, sizeof(data));
	bzero(&maxkey, sizeof(maxkey));

	if (strcmp(argv[0], "put") == 0) {
		if (argc < 3)
			errx(1, "missing arguments");
		key.data = argv[1];
		key.size = strlen(key.data);
		data.data = argv[2];
		data.size = strlen(data.data);
		rc = btree_put(bt, &key, &data, 0);
		if (rc == BT_SUCCESS)
			printf("OK\n");
		else
			printf("FAIL\n");
	} else if (strcmp(argv[0], "del") == 0) {
		if (argc < 1)
			errx(1, "missing argument");
		key.data = argv[1];
		key.size = strlen(key.data);
		rc = btree_del(bt, &key, NULL);
		if (rc == BT_SUCCESS)
			printf("OK\n");
		else
			printf("FAIL\n");
	} else if (strcmp(argv[0], "get") == 0) {
		if (argc < 2)
			errx(1, "missing arguments");
		key.data = argv[1];
		key.size = strlen(key.data);
		rc = btree_get(bt, &key, &data);
		if (rc == BT_SUCCESS) {
			printf("OK %.*s\n", (int)data.size, (char *)data.data);
		} else {
			printf("FAIL\n");
		}
	} else if (strcmp(argv[0], "scan") == 0) {
		if (argc > 1) {
			key.data = argv[1];
			key.size = strlen(key.data);
			flags = BT_CURSOR;
		}
		else
			flags = BT_FIRST;
		if (argc > 2) {
			maxkey.data = argv[2];
			maxkey.size = strlen(key.data);
		}

		cursor = btree_cursor_open(bt);
		while ((rc = btree_cursor_get(cursor, &key, &data,
		    flags)) == BT_SUCCESS) {
			if (argc > 2 && btree_cmp(bt, &key, &maxkey) > 0)
				break;
			printf("OK %zi %.*s\n",
			    key.size, (int)key.size, (char *)key.data);
			flags = BT_NEXT;
		}
		btree_cursor_close(cursor);
	} else if (strcmp(argv[0], "compact") == 0) {
		if ((rc = btree_compact(bt)) != BT_SUCCESS)
			warn("compact");
	} else if (strcmp(argv[0], "revert") == 0) {
		if ((rc = btree_revert(bt)) != BT_SUCCESS)
			warn("revert");
	} else
		errx(1, "%s: invalid command", argv[0]);

	btree_close(bt);

	return rc;
}