int main(void)
{
	btree_tree *tmp;
	uint32_t data_idx;
	size_t *size;
	time_t *ts;
	char *data;
	int i, tmp_key;
	char testdata[11];
	int error = 0;

	/* testing too large data */
	tmp = btree_create("test.mmap", 3, 6, 10, &error);
	if (!tmp) {
		printf("Couldn't create tree from disk image, errno %d.\n", error);
		exit(1);
	}

	for (i = 0; i < 205; i++) {
		tmp_key = i * 3;
		if (0 == btree_insert(tmp, tmp_key)) {
			sprintf(testdata, "H: %07d", i);
			btree_set_data(tmp, tmp_key, testdata, 10, time(NULL));
			btree_get_data(tmp, tmp_key, &data_idx, (void **)&data, &size, &ts);
			printf("%s %zd\n", data, *size);
			btree_data_unlock(tmp, data_idx);
		}
	}

	btree_dump(tmp);
	btree_close(tmp);

	return 0;
}
int main(void)
{
	btree_tree *tmp;
	int error = 0;

	tmp = btree_create("test.mmap", 3, 400, 1024, &error);
	if (!tmp) {
		printf("Couldn't create tree from disk image error %d.\n", error);
		exit(1);
	}

	setup(tmp);
	btree_insert(tmp, 'd');
	btree_insert(tmp, 'f');
	btree_insert(tmp, 'v');

	btree_delete(tmp, 'i');
	btree_delete(tmp, 'f');
	btree_delete(tmp, 'e');
	btree_delete(tmp, 'y');
	btree_delete(tmp, 't');
	btree_dump_dot(tmp);

	btree_close(tmp);

	return 0;
}
Beispiel #3
0
static void btree_thread_ui_free(struct bthread_info *ui)
{
    int i;

    for (i = 0; i < MAX_THREAD; i++) {
        store_close(ui->node[i].store);
        btree_close(ui->node[i].tree);
    }

    free(ui);
}
Beispiel #4
0
int
compact_db(const char *path)
{
	struct btree	*bt;
	int		 rc;

	log_info("compacting database %s", path);
	bt = btree_open(path, BT_NOSYNC | BT_REVERSEKEY, 0644);
	if (bt == NULL)
		return -1;

	do {
		if ((rc = btree_compact(bt)) == -1 && errno == EBUSY)
			usleep(100000);
	} while (rc == -1 && errno == EBUSY);

	btree_close(bt);
	return rc;
}
int main(int argc, char *argv[])
{
	btree_tree *tmp;
	FILE *f;
	char urlBuffer[2048], *data;
	uint64_t id;
	uint64_t i = 0, found = 0;
	int error;

	tmp = btree_open("tree2.mmap", &error);
	if (!tmp) {
		printf("Couldn't open tree, errno %d.\n", error);
		exit(1);
	}

	if (argc < 2) {
		printf("Please pass a text file via argv to search for in the btree\n");
		exit(1);	
	}

	f = fopen(argv[1], "r");
	if (!f) {
		printf("Couldn't open file %s, errno %d.\n", argv[1], errno);
		exit(1);
	}
	while (!feof(f)) {
		fgets(urlBuffer, 2048, f);
		data = strchr(urlBuffer, ' ');
		if (data) {
			data++;
			data[-1] = '\0';
			id = atoll(urlBuffer);
			found += btree_search(tmp, tmp->root, id);
			found += btree_search(tmp, tmp->root, id + 1);
			i++;
		}
	}
	printf("%lu %lu\n", found, i);

	btree_close(tmp);

	return 0;
}
Beispiel #6
0
int main(int argc, char **argv)
{

	long i,count,op;
	if(argc!=2)
	{
		fprintf(stderr,"Usage: nessdb_benchmark <op>\n");
        	exit(1);
	}

	if(strcmp(argv[1],"add")==0)
		op=OP_ADD;
	else if(strcmp(argv[1],"get")==0)
		op=OP_GET;
	else if(strcmp(argv[1],"walk")==0)
		op=OP_WALK;
	else
	{
		printf("not supported op %s\n", argv[1]);
        	exit(1);
    	}


	srand(time(NULL));
	print_header();
	print_environment();

	db_init_test();
	if(op==OP_ADD)
		db_tests();
	else if(op==OP_WALK)
		db_read_seq_test();
	else if(op==OP_GET)
		db_read_random_test();
	
	btree_close(&btree);

	return 0;
}
Beispiel #7
0
int main(int argc, char **argv) {
    struct btree *bt;
    uint64_t ptr;
    int j, count, op, arg;

    if (argc != 4) {
        fprintf(stderr,"Usage: btree_example <op> <size/ptr> <count>\n");
        exit(1);
    }
    count = atoi(argv[3]);
    arg = atoi(argv[2]);
    if (!strcasecmp(argv[1],"alloc")) {
        op = OP_ALLOC;
    } else if (!strcasecmp(argv[1],"free")) {
        op = OP_FREE;
    } else if (!strcasecmp(argv[1],"allocfree")) {
        op = OP_ALLOCFREE;
    } else if (!strcasecmp(argv[1],"add")) {
        op = OP_ADD;
    } else if (!strcasecmp(argv[1],"walk")) {
        op = OP_WALK;
    } else if (!strcasecmp(argv[1],"fill")) {
        op = OP_FILL;
    } else if (!strcasecmp(argv[1],"find")) {
        op = OP_FIND;
    } else {
        printf("not supported op %s\n", argv[1]);
        exit(1);
    }

    bt = btree_open(NULL, "./btree.db", BTREE_CREAT);
    btree_clear_flags(bt,BTREE_FLAG_USE_WRITE_BARRIER);
    if (bt == NULL) {
        perror("btree_open");
        exit(1);
    }
   
    for (j = 0; j < count; j++) {
        if (op == OP_ALLOC) {
            ptr = btree_alloc(bt,arg);
            printf("PTR: %llu\n", ptr);
        } else if (op == OP_FREE) {
            btree_free(bt,arg);
        } else if (op == OP_ALLOCFREE) {
            ptr = btree_alloc(bt,arg);
            printf("PTR: %llu\n", ptr);
            btree_free(bt,ptr);
        }
    }

    if (op == OP_ADD) {
        int retval;
        char key[16];
        memset(key,0,16);
        strcpy(key,argv[2]);

        retval = btree_add(bt,(unsigned char*)key,
            (unsigned char*)argv[3],strlen(argv[3]),1);
        printf("retval %d\n", retval);
        if (retval == -1) {
            printf("Error: %s\n", strerror(errno));
        }
    } else if (op == OP_WALK) {
        btree_walk(bt,bt->rootptr);
    } else if (op == OP_FILL) {
        for (j = 0; j < count; j++) {
            int r = random()%arg;
            int retval;
            char key[64];
            char val[64];

            memset(key,0,64);
            snprintf(key,64,"k%d",r);
            snprintf(val,64,"val:%d",r);
            retval = btree_add(bt,(unsigned char*)key,
                            (unsigned char*)val, strlen(val), 1);
            if (retval == -1) {
                printf("Error: %s\n", strerror(errno));
                goto err;
            }
        }
    } else if (op == OP_FIND) {
        int retval;
        char key[16], *data;
        memset(key,0,16);
        strcpy(key,argv[2]);
        uint64_t voff;
        uint32_t datalen;

        retval = btree_find(bt,(unsigned char*)key,&voff);
        if (retval == -1) {
            if (errno == ENOENT) {
                printf("Key not found\n");
                exit(0);
            } else {
                perror("Error searching for key");
                exit(1);
            }
        }
        printf("Key found at %llu\n", voff);

        btree_alloc_size(bt,&datalen,voff);
        data = malloc(datalen+1);
        btree_pread(bt,(unsigned char*)data,datalen,voff);
        data[datalen] = '\0';
        printf("Value: %s\n", data);
        free(data);
    }
    btree_close(bt);
    return 0;

err:
    btree_close(bt);
    return 1;
}
int main(void)
{
    btree_tree *tmp;
    uint32_t data_idx;
    char data[1024];
    int error = 0;

    memset(data, 4, 1023);
    memset(data+1023, 0, 1);

    tmp = btree_create("test.mmap", 3, 400, 1024, &error);
    if (!tmp) {
        printf("Couldn't create tree from disk image, errno %d.\n", error);
        exit(1);
    }

    insert_item(tmp, 'A');
    data_idx = insert_item(tmp, 'L');
    memcpy(tmp->data + data_idx * 1024, data, 1024);
    data_idx = insert_item(tmp, 'D');
    memcpy(tmp->data + data_idx * 1024, data, 1024);
    insert_item(tmp, 'F');

    insert_item(tmp, '4');
    insert_item(tmp, '2');
    insert_item(tmp, '3');
    insert_item(tmp, '5');
    insert_item(tmp, '1');

    insert_item(tmp, 'N');
    insert_item(tmp, 'P');
    insert_item(tmp, 'd');
    insert_item(tmp, 'f');
    insert_item(tmp, 'n');
    insert_item(tmp, 'p');
    insert_item(tmp, 'H');
    insert_item(tmp, 'C');
    insert_item(tmp, 'B');
    insert_item(tmp, 'E');
    insert_item(tmp, 'G');

    insert_item(tmp, 'I');
    insert_item(tmp, 'K');
    insert_item(tmp, 'J');
    insert_item(tmp, 'M');
    insert_item(tmp, 'o');

    insert_item(tmp, 'q');
    insert_item(tmp, 'r');
    insert_item(tmp, 'i');

    insert_item(tmp, 'j');
    insert_item(tmp, 'k');
    insert_item(tmp, 'd');
    insert_item(tmp, 't');
    insert_item(tmp, 'm');

    insert_item(tmp, 'O');
    insert_item(tmp, 'Q');
    insert_item(tmp, 'R');
    insert_item(tmp, 'S');
    insert_item(tmp, 'T');
    insert_item(tmp, 'U');

    insert_item(tmp, 'x');
    insert_item(tmp, 'w');
    insert_item(tmp, 'y');
    insert_item(tmp, 'u');
    insert_item(tmp, 'v');

    btree_dump_dot(tmp);
    btree_close(tmp);

    return 0;
}
Beispiel #9
0
int main(int argc, char **argv)
{
	const char *fname = "test.dat";

	if (argc < 2)
		return 0;

	srand(time(NULL));

	struct btree btree;
	uint8_t sha1[SHA1_LENGTH];
	char val[100];
	size_t i;

	if (file_exists(fname)) {
		if (btree_open(&btree, fname)) {
			warning("Unable to open database\n");
			return 1;
		}
	} else {
		if (btree_creat(&btree, fname)) {
			warning("Unable to create database\n");
			return 1;
		}
	}

	if (strcmp(argv[1], "insert") == 0) {
		memset(sha1, 0, sizeof sha1);

		start_timer();
		for (i = 0; i < COUNT; ++i) {
			sprintf((char *) sha1, "foobar %zd", i);
			sprintf(val, "value %zd", i*i);
			btree_insert(&btree, sha1, val, strlen(val));
		}
		printf("insert: %.6f\n", get_timer());
	}

	if (strcmp(argv[1], "get") == 0) {
		memset(sha1, 0, sizeof sha1);
		strcpy((char *) sha1, "foobar ");
		strcpy(val, "value ");

		start_timer();
		for (i = 0; i < COUNT; ++i) {
			/* optimize a bit */
			sprintf((char *) sha1 + 7, "%zd", i);
			sprintf(val + 6, "%zd", i*i);

			size_t len;
			void *data = btree_get(&btree, sha1, &len);
			if (data == NULL) {
				warning("not found: %zd\n", i);
				continue;
			}
			if (len != strlen(val) || memcmp(val, data, len)) {
				warning("data mismatch: %zd\n", i);
			}
			free(data);
		}
		printf("get: %.6f\n", get_timer());

	} else if (strcmp(argv[1], "refill") == 0) {
		/* delete half of the data, then add it back */

		memset(sha1, 0, sizeof sha1);
		for (i = 0; i < COUNT/2; i++) {
			sprintf((char *) sha1, "foobar %zd", i);
			if (btree_delete(&btree, sha1))
				warning("not found: %zd\n", i);
		}

		memset(sha1, 0, sizeof sha1);
		for (i = 0; i < COUNT/2; i++) {
			sprintf((char *) sha1, "foobar %zd", i);
			sprintf(val, "value %zd", i*i);
			btree_insert(&btree, sha1, val, strlen(val));
		}

	} else if (strcmp(argv[1], "delete") == 0) {
		memset(sha1, 0, sizeof sha1);

		start_timer();
		for (i = 0; i < COUNT; i++) {
			sprintf((char *) sha1, "foobar %zd", i);
			if (btree_delete(&btree, sha1))
				warning("not found: %zd\n", i);
		}
		printf("delete: %.6f\n", get_timer());

	} else
		warning("unknown command\n");

	btree_close(&btree);

	return 0;
}
Beispiel #10
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;
}
Beispiel #11
0
static void btree_ui_free(struct btree_info *ui)
{
	store_close(ui->userinfo_store);
	btree_close(ui->tree);
	free(ui);
}