dba_lmdb.c 7.4 KB

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