-
Notifications
You must be signed in to change notification settings - Fork 6
/
lcdb.c
299 lines (254 loc) · 7.37 KB
/
lcdb.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <lua.h>
#include <lauxlib.h>
#ifndef O_BINARY
#ifdef _O_BINARY
#define O_BINARY _O_BINARY
#else
#define O_BINARY 0 // system doesn't need O_BINARY
#endif
#endif
#include "cdb.h"
#define LCDB_DB "cdb.db"
#define LCDB_MAKE "cdb.make"
static struct cdb *new_cdb(lua_State *L) {
struct cdb *cdbp = (struct cdb*)lua_newuserdata(L, sizeof(struct cdb));
luaL_getmetatable(L, LCDB_DB);
lua_setmetatable(L, -2);
return cdbp;
}
static struct cdb *check_cdb(lua_State *L, int n) {
struct cdb *cdbp = (struct cdb*)luaL_checkudata(L, n, LCDB_DB);
luaL_argcheck(L, cdbp->cdb_fd >= 0, n, "attempted to use a closed cdb");
return cdbp;
}
static int push_errno(lua_State *L, int xerrno) {
lua_pushnil(L);
lua_pushstring(L, strerror(xerrno));
return 2;
}
/* cdb.open(filename) */
static int lcdb_open(lua_State *L) {
struct cdb *cdbp;
const char *filename = luaL_checkstring(L, 1);
int ret;
int fd = open(filename, O_RDONLY | O_BINARY);
if (fd < 0)
return push_errno(L, errno);
cdbp = new_cdb(L);
ret = cdb_init(cdbp, fd);
if (ret < 0) {
lua_pushnil(L);
lua_pushfstring(L, LCDB_DB": file %s is not a valid database (or mmap failed)", filename);
return 2;
}
return 1;
}
/* db:close() */
static int lcdbm_gc(lua_State *L) {
struct cdb *cdbp = (struct cdb*)luaL_checkudata(L, 1, LCDB_DB);
if (cdbp->cdb_fd >= 0) {
close(cdbp->cdb_fd);
cdb_free(cdbp);
cdbp->cdb_fd = -1;
}
return 0;
}
/* db:__tostring() */
static int lcdbm_tostring(lua_State *L) {
struct cdb *cdbp = (struct cdb*)luaL_checkudata(L, 1, LCDB_DB);
if (cdbp->cdb_fd >= 0)
lua_pushfstring(L, "<"LCDB_DB"> (%p)", cdbp);
else
lua_pushfstring(L, "<"LCDB_DB"> (closed)");
return 1;
}
/* db:get(key) */
static int lcdbm_get(lua_State *L) {
size_t klen;
int ret;
struct cdb *cdbp = check_cdb(L, 1);
const char *key = luaL_checklstring(L, 2, &klen);
ret = cdb_find(cdbp, key, klen);
if (ret > 0) {
lua_pushlstring(L, cdb_getdata(cdbp), cdb_datalen(cdbp));
return 1;
} else if (ret == 0) {
lua_pushnil(L);
return 1;
} else {
return luaL_error(L, LCDB_DB": error in find. Database corrupt?");
}
}
/* db:find_all(key) */
static int lcdbm_find_all(lua_State *L) {
size_t klen;
int ret;
int n = 1;
struct cdb *cdbp = check_cdb(L, 1);
const char *key = luaL_checklstring(L, 2, &klen);
struct cdb_find cdbf;
cdb_findinit(&cdbf, cdbp, key, klen);
lua_newtable(L);
while((ret = cdb_findnext(&cdbf))) {
if (ret < 0) { /* error */
return luaL_error(L, LCDB_DB": error in find_all. Database corrupt?");
}
lua_pushlstring(L, cdb_getdata(cdbp), cdb_datalen(cdbp));
lua_rawseti(L, -2, n);
n++;
}
return 1;
}
static int lcdbm_iternext(lua_State *L) {
struct cdb *cdbp = (struct cdb*)lua_touserdata(L, lua_upvalueindex(1));
unsigned pos = lua_tointeger(L, lua_upvalueindex(2));
int ret = cdb_seqnext(&pos, cdbp);
lua_pushinteger(L, pos);
lua_replace(L, lua_upvalueindex(2));
if (ret > 0) {
lua_pushlstring(L, cdb_getkey(cdbp), cdb_keylen(cdbp));
lua_pushlstring(L, cdb_getdata(cdbp), cdb_datalen(cdbp));
return 2;
} else if (ret == 0) { /* finished */
lua_pushnil(L);
return 1;
} else { /* error */
return luaL_error(L, LCDB_DB": error in iterator. Database corrupt?");
}
}
/* for k, v in db:pairs() do ... end */
static int lcdbm_pairs(lua_State *L) {
struct cdb *cdbp = check_cdb(L, 1);
unsigned pos;
cdb_seqinit(&pos, cdbp);
lua_pushinteger(L, pos);
lua_pushcclosure(L, lcdbm_iternext, 2);
return 1;
}
static struct cdb_make *new_cdb_make(lua_State *L) {
struct cdb_make *cdbmp = (struct cdb_make*)lua_newuserdata(L, sizeof(struct cdb_make));
luaL_getmetatable(L, LCDB_MAKE);
lua_setmetatable(L, -2);
lua_newtable(L);
lua_setfenv(L, 1);
return cdbmp;
}
static struct cdb_make *check_cdb_make(lua_State *L, int n) {
struct cdb_make *cdbmp = luaL_checkudata(L, n, LCDB_MAKE);
luaL_argcheck(L, cdbmp->cdb_fd >= 0, n, "attempted to use a closed cdb_make");
return cdbmp;
}
/* cdb.make(destination, temporary) */
static int lcdb_make(lua_State *L) {
int fd;
int ret;
struct cdb_make *cdbmp;
const char *dest = luaL_checkstring(L, 1);
const char *tmpname = luaL_checkstring(L, 2);
fd = open(tmpname, O_RDWR|O_CREAT|O_EXCL|O_BINARY, 0666);
if (fd < 0)
return push_errno(L, errno);
cdbmp = new_cdb_make(L);
ret = cdb_make_start(cdbmp, fd);
/* store destination and tmpname in userdata environment */
lua_getfenv(L, -1);
lua_pushstring(L, dest);
lua_setfield(L, -2, "dest");
lua_pushstring(L, tmpname);
lua_setfield(L, -2, "tmpname");
lua_pop(L, 1); /* pop the environment */
if (ret < 0)
return push_errno(L, errno);
return 1;
}
static int lcdbmakem_gc(lua_State *L) {
struct cdb_make *cdbmp = luaL_checkudata(L, 1, LCDB_MAKE);
if (cdbmp->cdb_fd >= 0) {
close(cdbmp->cdb_fd);
cdb_make_free(cdbmp);
cdbmp->cdb_fd = -1;
}
return 0;
}
/* maker:__tostring() */
static int lcdbmakem_tostring(lua_State *L) {
struct cdb_make *cdbmp = luaL_checkudata(L, 1, LCDB_MAKE);
if (cdbmp->cdb_fd >= 0)
lua_pushfstring(L, "<"LCDB_MAKE"> (%p)", cdbmp);
else
lua_pushfstring(L, "<"LCDB_MAKE"> (closed)");
return 1;
}
/* maker:add(key, value, [mode]) */
static int lcdbmakem_add(lua_State *L) {
static const char *const opts[] = { "add", "replace", "replace0", "insert", NULL };
size_t klen, vlen;
struct cdb_make *cdbmp = check_cdb_make(L, 1);
const char *key = luaL_checklstring(L, 2, &klen);
const char *value = luaL_checklstring(L, 3, &vlen);
/* by default, add unconditionally */
int mode = luaL_checkoption(L, 4, "add", opts);
int ret = cdb_make_put(cdbmp, key, klen, value, vlen, mode);
if (ret < 0)
return luaL_error(L, strerror(errno));
return 0;
}
/* maker:finish() */
static int lcdbmakem_finish(lua_State *L) {
struct cdb_make *cdbmp = check_cdb_make(L, 1);
/* retrieve destination, current filename */
lua_getfenv(L, -1);
lua_getfield(L, -1, "dest");
const char *dest = lua_tostring(L, -1);
lua_getfield(L, -2, "tmpname");
const char *tmpname = lua_tostring(L, -1);
lua_pop(L, 3);
if (cdb_make_finish(cdbmp) < 0 || fsync(cdb_fileno(cdbmp)) < 0 ||
close(cdb_fileno(cdbmp)) < 0 || rename(tmpname, dest) < 0) {
cdb_make_free(cdbmp); /* in case cdb_make_finish failed before freeing */
cdbmp->cdb_fd = -1;
return luaL_error(L, strerror(errno));
}
cdbmp->cdb_fd = -1;
lua_pushboolean(L, 1);
return 1;
}
static const struct luaL_Reg lcdb_f [] = {
{"open", lcdb_open},
{"make", lcdb_make},
{NULL, NULL}
};
static const struct luaL_Reg lcdb_m [] = {
{"__gc", lcdbm_gc},
{"close", lcdbm_gc},
{"__tostring", lcdbm_tostring},
{"find_all", lcdbm_find_all},
{"get", lcdbm_get},
{"pairs", lcdbm_pairs},
{"iter", lcdbm_pairs},
{NULL, NULL}
};
static const struct luaL_Reg lcdbmake_m [] = {
{"__gc", lcdbmakem_gc},
{"__tostring", lcdbmakem_tostring},
{"add", lcdbmakem_add},
{"finish", lcdbmakem_finish},
{NULL, NULL}
};
int luaopen_cdb(lua_State *L) {
luaL_newmetatable(L, LCDB_DB);
lua_pushvalue(L, -1);
lua_setfield(L, -2, "__index");
luaL_register(L, NULL, lcdb_m);
luaL_newmetatable(L, LCDB_MAKE);
lua_pushvalue(L, -1);
lua_setfield(L, -2, "__index");
luaL_register(L, NULL, lcdbmake_m);
lua_newtable(L);
luaL_register(L, NULL, lcdb_f);
return 1;
}