-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmain.c
138 lines (114 loc) · 3.04 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
* Simple test program that demostrates how to use the btree.
*
* In a real program, the database key should be derived by hashing with
* SHA-1 from a binary string. In this example program, just 20-character
* fake keys are used.
*/
#include "btree.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/sha.h>
#include <sys/stat.h>
#include <time.h>
#define warning(fmt...) fprintf(stderr, "WARNING: " fmt)
static int file_exists(const char *path)
{
struct stat st;
return stat(path, &st) == 0;
}
static struct timespec start;
static void start_timer(void)
{
clock_gettime(CLOCK_MONOTONIC, &start);
}
static double get_timer(void)
{
struct timespec end;
clock_gettime(CLOCK_MONOTONIC, &end);
long seconds = end.tv_sec - start.tv_sec;
long nseconds = end.tv_nsec - start.tv_nsec;
return seconds + (double) nseconds / 1.0e9;
}
#define COUNT 1000000
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());
} else 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;
}