forked from dfuhry/lmdb-php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lmdb-php.i
374 lines (280 loc) · 9.55 KB
/
lmdb-php.i
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
%module lmdb_php
%include exception.i
%{
#include "/usr/include/lmdb.h"
%}
%ignore mdb_env_create;
%ignore mdb_env_open;
%ignore mdb_txn_begin;
%ignore mdb_dbi_open;
%ignore mdb_cursor_open;
%ignore mdb_env_get_maxreaders;
%ignore mdb_env_get_path;
%ignore mdb_env_get_fd;
%ignore mdb_env_get_flags;
%ignore mdb_env_set_userctx;
%ignore mdb_dbi_flags;
%ignore mdb_cursor_count;
%ignore mdb_reader_check;
%ignore mdb_stat;
%ignore mdb_set_relctx;
%ignore mdb_env_stat;
%ignore mdb_env_info;
%ignore mdb_set_compare;
%ignore mdb_set_dupsort;
%ignore mdb_set_relfunc;
%ignore mdb_env_set_assert;
#if MDB_VERSION_PATCH > 10
%ignore mdb_env_set_userctx;
#endif
%include "/usr/include/lmdb.h"
%newobject mdb_env_create_swig;
%newobject mdb_env_open_swig;
%newobject mdb_txn_begin_swig;
%newobject mdb_dbi_open_swig;
%newobject mdb_cursor_open_swig;
%newobject mdb_val_create;
%newobject mdb_env_info_swig;
%newobject mdb_env_stat_swig;
%newobject mdb_set_relctx_swig;
%newobject mdb_stat_swig;
%newobject mdb_env_get_fd_swig;
#if MDB_VERSION_PATCH > 10
%newobject mdb_env_set_userctx_swig;
#endif
%rename (mdb_env_create) mdb_env_create_swig();
%rename (mdb_env_open) mdb_env_open_swig (MDB_env *env, const char *path, unsigned int flags, int mode);
%rename (mdb_txn_begin) mdb_txn_begin_swig (MDB_env *env, MDB_txn *parent, unsigned int flags);
%rename (mdb_dbi_open) mdb_dbi_open_swig (MDB_txn *txn, const char *name, unsigned int flags);
%rename (mdb_cursor_open) mdb_cursor_open_swig(MDB_txn *txn, MDB_dbi dbi);
%rename (mdb_env_get_maxreaders) mdb_env_get_maxreaders_swig(MDB_env *env);
%rename (mdb_env_get_fd) mdb_env_get_fd_swig(MDB_env *env);
%rename (mdb_env_get_path) mdb_env_get_path_swig(MDB_env *env);
%rename (mdb_env_get_flags) mdb_env_get_flags_swig(MDB_env *env);
%rename (mdb_dbi_flags) mdb_dbi_flags_swig(MDB_txn *txn, MDB_dbi dbi);
%rename (mdb_cursor_count) mdb_cursor_count_swig(MDB_cursor *cursor);
%rename (mdb_reader_check) mdb_reader_check_swig(MDB_env *env);
%rename (mdb_stat) mdb_stat_swig(MDB_txn *txn, MDB_dbi dbi);
%rename (mdb_set_relctx) mdb_set_relctx_swig(MDB_txn *txn, MDB_dbi dbi, char *value);
%rename (mdb_env_stat) mdb_env_stat_swig(MDB_env *env);
%rename (mdb_env_info) mdb_env_info_swig(MDB_env *env);
#if MDB_VERSION_PATCH > 10
%rename (mdb_env_set_userctx) mdb_env_set_userctx_swig(MDB_env *env, char *value);
#endif
/*
* Have mdb_val_data output MDB_val into a returned PHP string byte-safely,
* i.e. preserving any "\0" bytes. Simply returning char* wouldn't do this.
*/
%typemap(out) MDB_val *mdb_val_data {
if ($1->mv_size) {
ZVAL_STRINGL($result, $1->mv_data, $1->mv_size);
} else {
ZVAL_EMPTY_STRING($result);
}
}
%apply (char *STRING, size_t LENGTH) { (char *input_str, size_t input_len) };
%inline %{
MDB_envinfo *mdb_env_info_swig(MDB_env *env){
MDB_envinfo *info;
info = (MDB_envinfo*)malloc( sizeof( MDB_envinfo ) );
int rc = mdb_env_info(env, info);
if(rc != 0){
php_error_docref(NULL, E_NOTICE, "mdb_env_info: %d\n", rc);
}
return info;
}
MDB_stat *mdb_env_stat_swig(MDB_env *env){
MDB_stat *stat;
stat = (MDB_stat*)malloc( sizeof( MDB_stat ) );
int rc = mdb_env_stat(env, stat);
if(rc != 0){
php_error_docref(NULL, E_NOTICE, "mdb_env_stat: %d\n", rc);
}
return stat;
}
int mdb_set_relctx_swig(MDB_txn *txn, MDB_dbi dbi, char *value){
int length = strlen(value) + 1;
char *res = malloc(length);
strncpy(res, value, length);
return mdb_set_relctx(txn, dbi, (void*)res);
}
MDB_stat *mdb_stat_swig(MDB_txn *txn, MDB_dbi dbi){
MDB_stat *stat;
stat = (MDB_stat*)malloc( sizeof( MDB_stat ) );
int rc = mdb_stat(txn, dbi, stat);
if(rc != 0){
php_error_docref(NULL, E_NOTICE, "mdb_stat: %d\n", rc);
}
return stat;
}
int mdb_reader_check_swig(MDB_env *env){
int dead;
int rc = mdb_reader_check(env, &dead);
if(rc != 0){
php_error_docref(NULL, E_NOTICE, "mdb_reader_check: %d\n", rc);
}
return dead;
}
int mdb_cursor_count_swig(MDB_cursor *cursor){
size_t duplicates;
int rc = mdb_cursor_count(cursor, &duplicates);
if(rc != 0){
php_error_docref(NULL, E_NOTICE, "mdb_cursor_count: %d\n", rc);
}
return duplicates;
}
int mdb_dbi_flags_swig(MDB_txn *txn, MDB_dbi dbi){
unsigned int flags;
int rc = mdb_dbi_flags(txn, dbi, &flags);
if(rc != 0){
php_error_docref(NULL, E_NOTICE, "mdb_dbi_flags: %d\n", rc);
}
return flags;
}
int mdb_env_get_flags_swig(MDB_env *env){
unsigned int flags;
int rc = mdb_env_get_maxreaders(env, &flags);
if(rc != 0){
php_error_docref(NULL, E_NOTICE, "mdb_env_get_flags: %d\n", rc);
}
return flags;
}
const char *mdb_env_get_path_swig(MDB_env *env){
const char *path;
int rc = mdb_env_get_path(env, &path);
if(rc != 0){
php_error_docref(NULL, E_NOTICE, "mdb_env_get_path: %d\n", rc);
}
return path;
}
mdb_filehandle_t *mdb_env_get_fd_swig (MDB_env *env){
mdb_filehandle_t fd;
int rc = mdb_env_get_fd(env, &fd);
if(rc != 0){
php_error_docref(NULL, E_NOTICE, "mdb_env_get_fd: %d\n", rc);
}
mdb_filehandle_t * fdp;
fdp = &fd;
return fdp;
}
int mdb_env_get_maxreaders_swig(MDB_env *env){
unsigned int readers;
int rc = mdb_env_get_maxreaders(env, &readers);
if(rc != 0){
php_error_docref(NULL, E_NOTICE, "mdb_env_get_maxreaders: %d\n", rc);
}
return readers;
}
MDB_env *mdb_env_create_swig() {
MDB_env *env;
int rc = mdb_env_create(&env);
if(rc != 0){
php_error_docref(NULL, E_NOTICE, "mdb_txn_begin: %d\n", rc);
}
return env;
}
int mdb_env_open_swig(MDB_env *env, const char *path, unsigned int flags, int mode) {
return mdb_env_open(env, path, flags, mode);
}
struct MDB_txn *mdb_txn_begin_swig(MDB_env *env, MDB_txn *parent, unsigned int flags){
MDB_txn *txn;
int rc = mdb_txn_begin(env, parent, flags, &txn);
if(rc != 0){
php_error_docref(NULL, E_NOTICE,"mdb_txn_begin: %d\n", rc);
}
return txn;
}
MDB_dbi mdb_dbi_open_swig (MDB_txn *txn, const char *name, unsigned int flags){
MDB_dbi dbi;
int rc = mdb_dbi_open(txn, name, flags, &dbi);
if(rc != 0){
php_error_docref(NULL, E_NOTICE, "mdb_dbi_open: %d\n", rc);
}
return dbi;
}
struct MDB_cursor *mdb_cursor_open_swig (MDB_txn *txn, MDB_dbi dbi){
MDB_cursor *cursor;
int rc = mdb_cursor_open(txn, dbi, &cursor);
if(rc != 0){
php_error_docref(NULL, E_NOTICE, "mdb_cursor_open: %d\n", rc);
}
return cursor;
}
MDB_val *mdb_val_create(char *input_str, size_t input_len){
MDB_val *key;
key = (MDB_val*)malloc( sizeof( MDB_val ) );
key->mv_size = input_len;
key->mv_data = "";
if (input_len) {
// TODO: Fix this malloc() memory leak. Not as simple as just doing
// free(key->mv_data) in a destructor since mdb operations can alter
// mv_size and mv_data pointer during get operations, after which
// mv_data should not be free()d.
// Maybe store pointer in a data structure indexed on MDB_val memory
// location, to be looked-up & freed when "owning" MDB_val is freed.
key->mv_data = malloc(input_len);
memcpy(key->mv_data, input_str, input_len);
}
return key;
}
size_t mdb_val_size( MDB_val *key){
return key->mv_size;
}
/*
* Placeholder that returns self; a previously-defined typemap will do the
* grander work of outputting key->mv_size and key->mv_data into a PHP string.
*/
MDB_val *mdb_val_data(MDB_val *key) {
return key;
}
int mdb_stat_psize(MDB_stat *stat){
return stat->ms_psize;
}
int mdb_stat_depth(MDB_stat *stat){
return stat->ms_depth;
}
int mdb_stat_branch_pages(MDB_stat *stat){
return stat->ms_branch_pages;
}
int mdb_stat_leaf_pages(MDB_stat *stat){
return stat->ms_leaf_pages;
}
int mdb_stat_overflow_pages(MDB_stat *stat){
return stat->ms_overflow_pages;
}
int mdb_stat_entries(MDB_stat *stat){
return stat->ms_entries;
}
char *mdb_info_mapaddr(MDB_envinfo *info){
return (char*)info->me_mapaddr;
}
int mdb_info_mapsize(MDB_envinfo *info){
return info->me_mapsize;
}
int mdb_info_last_pgno(MDB_envinfo *info){
return info->me_last_pgno;
}
int mdb_info_last_txnid(MDB_envinfo *info){
return info->me_last_txnid;
}
int mdb_info_maxreaders(MDB_envinfo *info){
return info->me_maxreaders;
}
int mdb_info_numreaders(MDB_envinfo *info){
return info->me_numreaders;
}
%}
#if MDB_VERSION_PATCH > 10
%inline %{
int mdb_env_set_userctx_swig(MDB_env *env, char *value){
int length = strlen(value) + 1;
char *res = malloc(length);
strncpy(res, value, length);
return mdb_env_set_userctx(env, (void*)res);
}
char *mdb_env_get_userctx_swig(MDB_env *env){
void *pointer = mdb_env_get_userctx(env);
return (char *)pointer;
}
%}
#endif