1 /*
2 +----------------------------------------------------------------------+
3 | Copyright (c) The PHP Group |
4 +----------------------------------------------------------------------+
5 | This source file is subject to version 3.01 of the PHP license, |
6 | that is bundled with this package in the file LICENSE, and is |
7 | available through the world-wide-web at the following url: |
8 | http://www.php.net/license/3_01.txt |
9 | If you did not receive a copy of the PHP license and are unable to |
10 | obtain it through the world-wide-web, please send a note to |
11 | license@php.net so we can mail you a copy immediately. |
12 +----------------------------------------------------------------------+
13 | Author: Anatol Belski <ab@php.net> |
14 +----------------------------------------------------------------------+
15 */
16
17 #ifdef HAVE_CONFIG_H
18 #include "config.h"
19 #endif
20
21 #include "php.h"
22
23 #if DBA_LMDB
24 #include "php_lmdb.h"
25
26 #ifdef LMDB_INCLUDE_FILE
27 #include LMDB_INCLUDE_FILE
28 #endif
29
30 struct php_lmdb_info {
31 MDB_env *env;
32 MDB_txn *txn;
33 MDB_dbi dbi;
34 MDB_cursor *cur;
35 };
36
37 #define LMDB_IT(it) (((struct php_lmdb_info *)info->dbf)->it)
38
DBA_OPEN_FUNC(lmdb)39 DBA_OPEN_FUNC(lmdb)
40 {
41 MDB_env *env;
42 MDB_txn *txn;
43 int rc, mode = 0644, flags = MDB_NOSUBDIR;
44 zend_long mapsize = 0;
45
46 if(info->argc > 0) {
47 mode = zval_get_long(&info->argv[0]);
48
49 if (info->argc > 1) {
50 mapsize = zval_get_long(&info->argv[1]);
51 if (mapsize < 0) {
52 *error = "mapsize must be greater than or equal to zero";
53 return FAILURE;
54 }
55 }
56 /* TODO implement handling of the additional flags. */
57 }
58
59 rc = mdb_env_create(&env);
60 if (rc) {
61 *error = mdb_strerror(rc);
62 return FAILURE;
63 }
64
65 if (mapsize > 0) {
66 rc = mdb_env_set_mapsize(env, (size_t) mapsize);
67 if (rc) {
68 *error = mdb_strerror(rc);
69 return FAILURE;
70 }
71 }
72
73 rc = mdb_env_open(env, info->path, flags, mode);
74 if (rc) {
75 mdb_env_close(env);
76 *error = mdb_strerror(rc);
77 return FAILURE;
78 }
79
80 rc = mdb_txn_begin(env, NULL, 0, &txn);
81 if (rc) {
82 mdb_env_close(env);
83 *error = mdb_strerror(rc);
84 return FAILURE;
85 }
86
87 info->dbf = pemalloc(sizeof(struct php_lmdb_info), info->flags & DBA_PERSISTENT);
88 if (!info->dbf) {
89 *error = "Failed to allocate php_lmdb_info.";
90 return FAILURE;
91 }
92 memset(info->dbf, 0, sizeof(struct php_lmdb_info));
93
94 rc = mdb_dbi_open(txn, NULL, 0, &LMDB_IT(dbi));
95 if (rc) {
96 mdb_env_close(env);
97 pefree(info->dbf, info->flags & DBA_PERSISTENT);
98 *error = mdb_strerror(rc);
99 return FAILURE;
100 }
101
102 LMDB_IT(env) = env;
103 LMDB_IT(txn) = txn;
104
105 mdb_txn_abort(LMDB_IT(txn));
106
107 return SUCCESS;
108 }
109
DBA_CLOSE_FUNC(lmdb)110 DBA_CLOSE_FUNC(lmdb)
111 {
112 mdb_dbi_close(LMDB_IT(env), LMDB_IT(dbi));
113 mdb_env_close(LMDB_IT(env));
114
115 pefree(info->dbf, info->flags & DBA_PERSISTENT);
116 }
117
DBA_FETCH_FUNC(lmdb)118 DBA_FETCH_FUNC(lmdb)
119 {
120 int rc;
121 MDB_val k, v;
122 char *ret = NULL;
123
124 if (LMDB_IT(cur)) {
125 rc = mdb_txn_renew(LMDB_IT(txn));
126 } else {
127 rc = mdb_txn_begin(LMDB_IT(env), NULL, MDB_RDONLY, &LMDB_IT(txn));
128 }
129 if (rc) {
130 php_error_docref1(NULL, key, E_WARNING, "%s", mdb_strerror(rc));
131 return NULL;
132 }
133
134 k.mv_size = keylen;
135 k.mv_data = key;
136
137 rc = mdb_get(LMDB_IT(txn), LMDB_IT(dbi), &k, &v);
138 if (rc) {
139 if (MDB_NOTFOUND != rc) {
140 php_error_docref1(NULL, key, E_WARNING, "%s", mdb_strerror(rc));
141 }
142 mdb_txn_abort(LMDB_IT(txn));
143 return NULL;
144 }
145
146 if (v.mv_data) {
147 if(newlen) *newlen = v.mv_size;
148 ret = estrndup(v.mv_data, v.mv_size);
149 }
150
151 if (LMDB_IT(cur)) {
152 mdb_txn_reset(LMDB_IT(txn));
153 } else {
154 mdb_txn_abort(LMDB_IT(txn));
155 }
156
157 return ret;
158 }
159
DBA_UPDATE_FUNC(lmdb)160 DBA_UPDATE_FUNC(lmdb)
161 {
162 int rc;
163 MDB_val k, v;
164
165 rc = mdb_txn_begin(LMDB_IT(env), NULL, 0, &LMDB_IT(txn));
166 if (rc) {
167 php_error_docref2(NULL, key, val, E_WARNING, "%s", mdb_strerror(rc));
168 return FAILURE;
169 }
170
171 k.mv_size = keylen;
172 k.mv_data = key;
173 v.mv_size = vallen;
174 v.mv_data = val;
175
176 rc = mdb_put(LMDB_IT(txn), LMDB_IT(dbi), &k, &v, mode == 1 ? MDB_NOOVERWRITE : 0);
177 if (rc) {
178 if (MDB_KEYEXIST != rc) {
179 php_error_docref2(NULL, key, val, E_WARNING, "%s", mdb_strerror(rc));
180 }
181 mdb_txn_abort(LMDB_IT(txn));
182 return FAILURE;
183 }
184
185 rc = mdb_txn_commit(LMDB_IT(txn));
186 if (rc) {
187 php_error_docref2(NULL, key, val, E_WARNING, "%s", mdb_strerror(rc));
188 mdb_txn_abort(LMDB_IT(txn));
189 return FAILURE;
190 }
191
192 return SUCCESS;
193 }
194
DBA_EXISTS_FUNC(lmdb)195 DBA_EXISTS_FUNC(lmdb)
196 {
197 int rc;
198 MDB_val k, v;
199
200 if (LMDB_IT(cur)) {
201 rc = mdb_txn_renew(LMDB_IT(txn));
202 } else {
203 rc = mdb_txn_begin(LMDB_IT(env), NULL, MDB_RDONLY, &LMDB_IT(txn));
204 }
205 if (rc) {
206 php_error_docref1(NULL, key, E_WARNING, "%s", mdb_strerror(rc));
207 return FAILURE;
208 }
209
210 k.mv_size = keylen;
211 k.mv_data = key;
212
213 rc = mdb_get(LMDB_IT(txn), LMDB_IT(dbi), &k, &v);
214 if (rc) {
215 if (MDB_NOTFOUND != rc) {
216 php_error_docref1(NULL, key, E_WARNING, "%s", mdb_strerror(rc));
217 }
218 mdb_txn_abort(LMDB_IT(txn));
219 return FAILURE;
220 }
221
222 if (LMDB_IT(cur)) {
223 mdb_txn_reset(LMDB_IT(txn));
224 } else {
225 mdb_txn_abort(LMDB_IT(txn));
226 }
227
228 return SUCCESS;
229 }
230
DBA_DELETE_FUNC(lmdb)231 DBA_DELETE_FUNC(lmdb)
232 {
233 int rc;
234 MDB_val k;
235
236 rc = mdb_txn_begin(LMDB_IT(env), NULL, 0, &LMDB_IT(txn));
237 if (rc) {
238 php_error_docref1(NULL, key, E_WARNING, "%s", mdb_strerror(rc));
239 return FAILURE;
240 }
241
242 k.mv_size = keylen;
243 k.mv_data = key;
244
245 rc = mdb_del(LMDB_IT(txn), LMDB_IT(dbi), &k, NULL);
246 if (!rc) {
247 rc = mdb_txn_commit(LMDB_IT(txn));
248 if (rc) {
249 php_error_docref1(NULL, key, E_WARNING, "%s", mdb_strerror(rc));
250 mdb_txn_abort(LMDB_IT(txn));
251 return FAILURE;
252 }
253 return SUCCESS;
254 }
255
256 php_error_docref1(NULL, key, E_WARNING, "%s", mdb_strerror(rc));
257
258 mdb_txn_abort(LMDB_IT(txn));
259 return FAILURE;
260 }
261
DBA_FIRSTKEY_FUNC(lmdb)262 DBA_FIRSTKEY_FUNC(lmdb)
263 {
264 int rc;
265 MDB_val k, v;
266 char *ret = NULL;
267
268 rc = mdb_txn_begin(LMDB_IT(env), NULL, MDB_RDONLY, &LMDB_IT(txn));
269 if (rc) {
270 php_error_docref(NULL, E_WARNING, "%s", mdb_strerror(rc));
271 return NULL;
272 }
273
274 rc = mdb_cursor_open(LMDB_IT(txn), LMDB_IT(dbi), &LMDB_IT(cur));
275 if (rc) {
276 mdb_txn_abort(LMDB_IT(txn));
277 php_error_docref(NULL, E_WARNING, "%s", mdb_strerror(rc));
278 return NULL;
279 }
280
281 rc = mdb_cursor_get(LMDB_IT(cur), &k, &v, MDB_FIRST);
282 if (rc) {
283 mdb_txn_abort(LMDB_IT(txn));
284 mdb_cursor_close(LMDB_IT(cur));
285 LMDB_IT(cur) = NULL;
286 if (MDB_NOTFOUND != rc) {
287 php_error_docref(NULL, E_WARNING, "%s", mdb_strerror(rc));
288 }
289 return NULL;
290 }
291
292 if(k.mv_data) {
293 if(newlen) *newlen = k.mv_size;
294 ret = estrndup(k.mv_data, k.mv_size);
295 }
296
297 mdb_txn_reset(LMDB_IT(txn));
298
299 return ret;
300 }
301
DBA_NEXTKEY_FUNC(lmdb)302 DBA_NEXTKEY_FUNC(lmdb)
303 {
304 int rc;
305 MDB_val k, v;
306 char *ret = NULL;
307
308 rc = mdb_txn_renew(LMDB_IT(txn));
309 if (rc) {
310 php_error_docref(NULL, E_WARNING, "%s", mdb_strerror(rc));
311 return NULL;
312 }
313
314 rc = mdb_cursor_get(LMDB_IT(cur), &k, &v, MDB_NEXT);
315 if (rc) {
316 mdb_txn_abort(LMDB_IT(txn));
317 mdb_cursor_close(LMDB_IT(cur));
318 LMDB_IT(cur) = NULL;
319 if (MDB_NOTFOUND != rc) {
320 php_error_docref(NULL, E_WARNING, "%s", mdb_strerror(rc));
321 }
322 return NULL;
323 }
324
325 if(k.mv_data) {
326 if(newlen) *newlen = k.mv_size;
327 ret = estrndup(k.mv_data, k.mv_size);
328 }
329
330 mdb_txn_reset(LMDB_IT(txn));
331
332 return ret;
333 }
334
DBA_OPTIMIZE_FUNC(lmdb)335 DBA_OPTIMIZE_FUNC(lmdb)
336 {
337 return SUCCESS;
338 }
339
DBA_SYNC_FUNC(lmdb)340 DBA_SYNC_FUNC(lmdb)
341 {
342 int rc;
343
344 rc = mdb_env_sync(LMDB_IT(env), 1);
345 if (rc) {
346 php_error_docref(NULL, E_WARNING, "%s", mdb_strerror(rc));
347 return FAILURE;
348 }
349
350 return SUCCESS;
351 }
352
DBA_INFO_FUNC(lmdb)353 DBA_INFO_FUNC(lmdb)
354 {
355 return estrdup(MDB_VERSION_STRING);
356 }
357
358 #endif
359