Exemple #1
0
int main()
{
	g_type_init();

	SoupServer *server = soup_server_new (
		SOUP_SERVER_PORT, 3000,
		NULL);
	soup_server_add_handler (server, NULL, (SoupServerCallback) server_callback,
             NULL,NULL);

	btree = btree_open("/tmp/badger", 0, 0666);
	soup_server_run(server);
}
Exemple #2
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;
}
Exemple #4
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;
}
Exemple #5
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;
}
Exemple #6
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;
}