forked from nigthwatch/luabdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
db_udata_ops.c
277 lines (252 loc) · 6.42 KB
/
db_udata_ops.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
#include "luabdb_private.h"
#include <db.h>
#include <lauxlib.h>
#include <lualib.h>
#include <pthread.h>
///retrieves _db member from the 'db' table. Does not modify the stack.
static DB* retrieve_db(lua_State *L, int index)
{
luaL_checktype(L, index, LUA_TTABLE);
lua_getfield(L, index, "_db");
dbgprint("retr_db 1\n");
DB** pdb = luabdb_todb(L, -1);
dbgprint("retr_db 2\n");
lua_pop(L, 1);
return *pdb;
}
// Nice, we have 'put', but what about associated index databases???
// It would be perfect if the BDB assoc index callback could be forwarded to a lua function
//
static int dbt_op_put(lua_State *L)
{
DB *db = retrieve_db(L, 1);
DB_TXN *txn = NULL;
DBT key;
DBT value;
size_t key_len;
size_t value_len;
u_int32_t flags = 0;
int status;
memset(&key, 0, sizeof(DBT));
memset(&value, 0, sizeof(DBT));
key.data = (void *) luaL_checklstring(L, 2, &key_len);
value.data = (void *) luaL_checklstring(L, 3, &value_len);
key.size = key_len;
value.size = value_len;
if (lua_gettop(L) >= 4) txn = luabdb_totxn(L, 4);
if (lua_gettop(L) >= 5) flags = luabdb_getflags(L, 5);
set_local_lua_state(L);
//dbgprint("txn: 0x%x, flags: 0x%x, key size: %d, value size: %d\n", txn, flags, key_len, value_len);
status = db->put(db, txn, &key, &value, flags);
//dbgprint("put done: 0x%x\n", status);
handle_dbexception(L, status);
return 0;
}
static int dbt_op_get(lua_State *L)
{
DB *db = retrieve_db(L, 1);
DB_TXN *txn = NULL;
DBT key;
DBT value;
size_t key_len;
size_t value_len;
u_int32_t flags = 0;
int status;
memset(&key, 0, sizeof(DBT));
memset(&value, 0, sizeof(DBT));
key.data = (void *) luaL_checklstring(L, 2, &key_len);
key.size = key_len;
value.flags = DB_DBT_MALLOC;
if (lua_gettop(L) >= 3) txn = luabdb_totxn(L, 3);
if (lua_gettop(L) >= 4) flags = luabdb_getflags(L, 4);
//dbgprint("get txn: 0x%x, flags: 0x%x, key size: %d, TOP: %d\n", txn, flags, key_len, lua_gettop(L));
status = db->get(db, txn, &key, &value, flags);
//dbgprint("get done: 0x%x\n", status);
if (status == 0)
{
lua_pushlstring(L, value.data, value.size);
free(value.data);
return 1;
}
else if (status == DB_NOTFOUND)
{
lua_pushnil(L);
return 1;
}
else handle_dbexception(L, status);
}
///barkeleydb
static int db_associate_callback(DB *secondary, const DBT *key, const DBT *value, DBT *secKey)
{
dbgprint("db_associate_callback\n");
lua_State *L = get_local_lua_state();
if (L == NULL) {
dbgprint("no local lua state\n");
return 1001;
}
get_lua_object_from_registry(L, secondary);
lua_getfield(L, -1, "_indexFn");
lua_pushlstring(L, key->data, key->size);
lua_pushlstring(L, value->data, value->size);
lua_call(L, 2, 1);
secKey->data = lua_tolstring(L, -1, &secKey->size);
dbgprint("index key: %s\n", secKey->data);
return 0;
}
//DB:associate(DB secondary, function(DB, id, value, luaValue), transaction, flags)
static int dbt_op_associate(lua_State *L)
{
DB *db = retrieve_db(L, 1);
DB *secDb = retrieve_db(L, 2);
DB_TXN *txn = NULL;
if (secDb == NULL) {
luaL_error(L, "secondary db missing");
}
u_int32_t flags = DB_CREATE;
set_local_lua_state(L);
lua_pushvalue(L, 3);
lua_setfield(L, 2, "_indexFn");
lua_pushvalue(L, 1);
lua_setfield(L, 2, "_masterDb");
int status = db->associate(db, txn, secDb, db_associate_callback, flags);
dbgprint("associated %d\n", status);
handle_dbexception(L, status);
//now insert the callback function into secDb table
return 0;
}
///get_dbname returns 2 values: db file name and database name
static int dbt_op_get_dbname(lua_State *L)
{
DB *db;
const char* pfile = NULL;
const char* pname = NULL;
db = luabdb_todb(L, 1);
int status;
status = db->get_dbname(db, &pfile, &pname);
handle_error(status);
if (pfile != NULL)
{
lua_pushstring(L, pfile);
free(pfile);
}
else
{
lua_pushnil(L);
}
if (pname != NULL)
{
lua_pushstring(L, pname);
free(pname);
}
else
{
lua_pushnil(L);
}
return 2;
}
static int dbt_op_testMapping(lua_State *L)
{
DB *db = retrieve_db(L, 1);
if (db == NULL) {
lua_pushnil(L);
return 1;
}
get_lua_object_from_registry(L, db);
return 1;
}
#define _(name) { #name, dbt_op_##name }
#define u_(name) { #name, luabdb_unimplemented }
static luaL_Reg dbt_funcs[] = {
_(put),
_(get),
_(testMapping),
u_(close),
_(associate),
u_(associate_foreign),
u_(compact),
u_(del),
u_(err),
u_(exists),
u_(fd),
u_(get_byteswapped),
u_(get_dbname),
u_(get_multiple),
u_(get_open_flags),
u_(get_type),
u_(join),
u_(key_range),
u_(remove),
u_(rename),
u_(set_priority),
u_(get_priority),
u_(stat),
u_(stat_print),
u_(sync),
u_(truncate),
u_(upgrade),
u_(verify),
u_(cursor),
u_(set_alloc),
u_(set_cachesize),
u_(get_cachesize),
u_(set_create_dir),
u_(get_create_dir),
u_(set_dup_compare),
u_(set_encrypt),
u_(get_encrypt_flags),
u_(set_errcall),
u_(set_errfile),
u_(get_errfile),
u_(set_errpfx),
u_(get_errpfx),
u_(set_feedback),
u_(set_flags),
u_(get_flags),
u_(set_lorder),
u_(get_lorder),
u_(set_msgcall),
u_(set_msgfile),
u_(get_msgfile),
u_(set_pagesize),
u_(get_pagesize),
u_(set_partition),
u_(set_partition_dirs),
u_(get_partition_dirs),
u_(set_append_recno),
u_(set_bt_compare),
u_(set_bt_compress),
u_(set_bt_minkey),
u_(get_bt_minkey),
u_(set_bt_prefix),
u_(set_re_delim),
u_(get_re_delim),
u_(set_re_len),
u_(get_re_len),
u_(set_re_pad),
u_(get_re_pad),
u_(set_re_source),
u_(get_re_source),
u_(set_h_compare),
u_(set_h_ffactor),
u_(get_h_ffactor),
u_(set_h_hash),
u_(set_h_nelem),
u_(get_h_nelem),
u_(set_q_extentsize),
u_(get_q_extentsize),
u_(get_env),
u_(get_mpf),
u_(get_transactional),
{ NULL, NULL }
};
#undef _
#undef u_
//initialize DBT meta
int init_dbt_ops(lua_State *L)
{
dbgprint("init_dbt_ops\n");
luaL_getmetatable(L, LUABDB_DBT);
lua_getfield(L, -1, "__index");
luaL_register(L, NULL, dbt_funcs);
return 0;
}