-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathmake_lmdb.c
156 lines (135 loc) · 4.21 KB
/
make_lmdb.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <limits.h>
#include <ctype.h>
#include <unistd.h>
#include <stdint.h>
#include <sys/time.h>
#include "lmdb.h"
#define LINE_SIZE 10240
int main(int argc, char *argv[])
{
int size;
char string[LINE_SIZE+1];
char s_key[LINE_SIZE+1];
char s_value[LINE_SIZE+1];
char* token;
char* save;
int cnt_line;
int rc;
MDB_env* env;
MDB_txn* txn;
MDB_cursor* mc;
MDB_dbi dbi;
MDB_val key, data;
char* envname;
int envflags=0;
int putflags=0;
char* subname;
char* prog = argv[0];
size_t map_size = (SIZE_MAX / (1024*1024*1024) / 4)*6; // 4giga * 6
int batch;
struct timeval tv1, tv2;
if(argc != 3) {
fprintf(stderr,"%s <envname> <subname>\n",prog);
exit(1);
}
gettimeofday(&tv1, NULL);
envflags = MDB_NOSUBDIR | MDB_NOLOCK;
envname = argv[1];
rc = mdb_env_create(&env);
if(rc) {
fprintf(stderr, "mdb_env_create failed, error %d %s\n", rc, mdb_strerror(rc));
return EXIT_FAILURE;
}
mdb_env_set_maxdbs(env, 2);
mdb_env_set_mapsize(env, map_size);
rc = mdb_env_open(env, envname, envflags, 0664);
if(rc) {
fprintf(stderr, "mdb_env_open failed, error %d %s\n", rc, mdb_strerror(rc));
goto env_close;
}
rc = mdb_txn_begin(env, NULL, 0, &txn);
if(rc) {
fprintf(stderr, "mdb_txn_begin failed, error %d %s\n", rc, mdb_strerror(rc));
goto env_close;
}
subname = argv[2];
rc = mdb_open(txn, subname, MDB_CREATE, &dbi);
if (rc) {
fprintf(stderr, "mdb_open failed, error %d %s\n", rc, mdb_strerror(rc));
goto txn_abort;
}
rc = mdb_cursor_open(txn, dbi, &mc);
if (rc) {
fprintf(stderr, "mdb_cursor_open failed, error %d %s\n", rc, mdb_strerror(rc));
goto txn_abort;
}
batch = 0;
cnt_line = 0;
while(fgets(string, LINE_SIZE, stdin) != NULL) {
size = strlen(string);
if(string[size-1] == '\n'){
string[size-1] = '\0';
--size;
}
if(size > 1 && string[size-1] == '\r'){
string[size-1] = '\0';
--size;
}
if(string[0] == '\0')
continue;
if(cnt_line % 10000 == 0)
fprintf(stderr,"[linecount]\t%d\n",cnt_line);
token = strtok_r(string, "\t", &save);
if(token != NULL) {
strcpy(s_key, token);
token = strtok_r(NULL, "\t", &save);
if(token != NULL) {
strcpy(s_value, token);
} else continue;
} else continue;
key.mv_data = s_key;
key.mv_size = strlen(s_key) + 1;
data.mv_data = s_value;
data.mv_size = strlen(s_value) + 1;
rc = mdb_cursor_put(mc, &key, &data, putflags);
if(rc == MDB_KEYEXIST)
continue;
if(batch % 100000000 == 0) {
rc = mdb_txn_commit(txn);
if(rc) {
fprintf(stderr, "%s: line %d: txn_commit: %s\n", prog, cnt_line, mdb_strerror(rc));
goto env_close;
}
rc = mdb_txn_begin(env, NULL, 0, &txn);
if(rc) {
fprintf(stderr, "mdb_txn_begin failed, error %d %s\n", rc, mdb_strerror(rc));
goto env_close;
}
rc = mdb_cursor_open(txn, dbi, &mc);
if(rc) {
fprintf(stderr, "mdb_cursor_open failed, error %d %s\n", rc, mdb_strerror(rc));
goto txn_abort;
}
}
cnt_line++;
batch++;
}
rc = mdb_txn_commit(txn);
txn = NULL;
if(rc) {
fprintf(stderr, "%s: txn_commit fail: %s\n", prog, mdb_strerror(rc));
goto env_close;
}
mdb_dbi_close(env, dbi);
txn_abort:
mdb_txn_abort(txn);
env_close:
mdb_env_close(env);
gettimeofday(&tv2, NULL);
fprintf(stderr, "<-end > : t2.sec = %d t2.usec = %d\n",(int)tv2.tv_sec,(int)tv2.tv_usec);
fprintf(stderr, "<+time> : sec = %d usec = %d\n",(int)(tv2.tv_sec-tv1.tv_sec),(int)(tv2.tv_usec-tv1.tv_usec));
return rc ? EXIT_FAILURE : EXIT_SUCCESS;
}