base_lmdb_store.pyx 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758
  1. import logging
  2. import os
  3. import threading
  4. import multiprocessing
  5. from contextlib import contextmanager
  6. from os import makedirs, path
  7. from shutil import rmtree
  8. from lakesuperior import env, wsgi
  9. from lakesuperior.cy_include cimport cylmdb as lmdb
  10. from libc cimport errno
  11. from cpython.mem cimport PyMem_Malloc, PyMem_Realloc, PyMem_Free
  12. from cython.parallel import parallel, prange
  13. logger = logging.getLogger(__name__)
  14. cdef void _check(int rc, str message='') except *:
  15. """
  16. Check return code.
  17. """
  18. if rc == lmdb.MDB_NOTFOUND:
  19. raise KeyNotFoundError()
  20. if rc == lmdb.MDB_KEYEXIST:
  21. raise KeyExistsError()
  22. if rc == errno.EINVAL:
  23. raise InvalidParamError(
  24. 'Invalid LMDB parameter error.\n'
  25. 'Please verify that a transaction is open and valid for the '
  26. 'current operation.'
  27. )
  28. if rc != lmdb.MDB_SUCCESS:
  29. out_msg = (
  30. message + '\nInternal error ({}): '.format(rc)
  31. if len(message) else 'LMDB Error ({}): '.format(rc))
  32. out_msg += lmdb.mdb_strerror(rc).decode()
  33. raise LmdbError(out_msg)
  34. class LmdbError(Exception):
  35. pass
  36. class KeyNotFoundError(LmdbError):
  37. pass
  38. class KeyExistsError(LmdbError):
  39. pass
  40. class InvalidParamError(LmdbError):
  41. pass
  42. cdef class BaseLmdbStore:
  43. """
  44. Generic LMDB store abstract class.
  45. This class contains convenience method to create an LMDB store for any
  46. purpose and provides some convenience methods to wrap cursors and
  47. transactions into contexts.
  48. Example usage::
  49. >>> class MyStore(BaseLmdbStore):
  50. ... path = '/base/store/path'
  51. ... dbi_flags = ('db1', 'db2')
  52. ...
  53. >>> ms = MyStore()
  54. >>> # "with" wraps the operation in a transaction.
  55. >>> with ms.cur(index='db1', write=True):
  56. ... cur.put(b'key1', b'val1')
  57. True
  58. """
  59. dbi_labels = []
  60. dbi_flags = {}
  61. """
  62. Configuration of databases in the environment.
  63. This is an OderedDict whose keys are the database labels and whose values
  64. are LMDB flags for creating and opening the databases as per
  65. `http://www.lmdb.tech/doc/group__mdb.html#gac08cad5b096925642ca359a6d6f0562a`_
  66. .
  67. If the environment has only one database, do not override this value (i.e.
  68. leave it to ``None``).
  69. :rtype: dict or None
  70. """
  71. env_flags = 0
  72. """
  73. LMDB environment flags.
  74. See `mdb_env_open
  75. <http://www.lmdb.tech/doc/group__mdb.html#ga32a193c6bf4d7d5c5d579e71f22e9340>`_
  76. """
  77. env_perms = 0o640
  78. """
  79. The UNIX permissions to set on created environment files and semaphores.
  80. See `mdb_env_open
  81. <http://www.lmdb.tech/doc/group__mdb.html#ga32a193c6bf4d7d5c5d579e71f22e9340>`_
  82. """
  83. options = {}
  84. """
  85. LMDB environment option overrides. Setting this is not required.
  86. See `LMDB documentation
  87. <http://lmdb.readthedocs.io/en/release/#environment-class`>_ for details
  88. on available options.
  89. Default values are available for the following options:
  90. - ``map_size``: 1 Gib
  91. - ``max_dbs``: dependent on the number of DBs defined in
  92. :py:meth:``dbi_flags``. Only override if necessary.
  93. - ``max_spare_txns``: dependent on the number of threads, if accessed via
  94. WSGI, or ``1`` otherwise. Only override if necessary.
  95. :rtype: dict
  96. """
  97. readers_mult = 4
  98. """
  99. Number to multiply WSGI workers by to set the numer of LMDB reader slots.
  100. """
  101. ### INIT & TEARDOWN ###
  102. def __init__(self, env_path, open_env=True, create=True):
  103. """
  104. Initialize DB environment and databases.
  105. :param str env_path: The file path of the store.
  106. :param bool open: Whether to open the store immediately. If ``False``
  107. the store can be manually opened with :py:meth:`opn_env`.
  108. :param bool create: Whether the file and directory structure should
  109. be created if the store is opened immediately.
  110. """
  111. self._open = False
  112. self.is_txn_open = False
  113. self.env_path = env_path
  114. if open_env:
  115. self.open_env(create)
  116. #logger.info('Init DB with path: {}'.format(self.env_path))
  117. def __dealloc__(self):
  118. self.close_env()
  119. @property
  120. def is_open(self):
  121. return self._open
  122. @property
  123. def readers(self):
  124. return self._readers
  125. def open_env(self, create):
  126. """
  127. Open, and optionally create, store environment.
  128. """
  129. if self.is_open:
  130. logger.warning('Environment already open.')
  131. return
  132. logger.debug('Opening environment at {}.'.format(self.env_path))
  133. if create:
  134. #logger.info('Creating db env at {}'.format(self.env_path))
  135. parent_path = (
  136. path.dirname(self.env_path)
  137. if lmdb.MDB_NOSUBDIR & self.flags
  138. else self.env_path)
  139. if not path.exists(parent_path):
  140. #logger.info(
  141. # 'Creating store directory at {}'.format(parent_path))
  142. try:
  143. makedirs(parent_path, mode=0o750, exist_ok=True)
  144. except Exception as e:
  145. raise LmdbError(
  146. 'Could not create store at {}. Error: {}'.format(
  147. self.env_path, e))
  148. # Create environment handle.
  149. _check(
  150. lmdb.mdb_env_create(&self.dbenv),
  151. 'Error creating DB environment handle: {}')
  152. logger.debug('Created DBenv @ {:x}'.format(<unsigned long>self.dbenv))
  153. # Set map size.
  154. _check(
  155. lmdb.mdb_env_set_mapsize(self.dbenv, self.options.get(
  156. 'map_size', 1024 ** 3)),
  157. 'Error setting map size: {}')
  158. # Set max databases.
  159. max_dbs = self.options.get('max_dbs', len(self.dbi_labels))
  160. _check(
  161. lmdb.mdb_env_set_maxdbs(self.dbenv, max_dbs),
  162. 'Error setting max. databases: {}')
  163. # Set max readers.
  164. self._readers = self.options.get(
  165. 'max_spare_txns', wsgi.workers * self.readers_mult)
  166. _check(
  167. lmdb.mdb_env_set_maxreaders(self.dbenv, self._readers),
  168. 'Error setting max. readers: {}')
  169. logger.debug('Max. readers: {}'.format(self._readers))
  170. # Clear stale readers.
  171. self._clear_stale_readers()
  172. # Open DB environment.
  173. logger.debug('DBenv address: {:x}'.format(<unsigned long>self.dbenv))
  174. _check(
  175. lmdb.mdb_env_open(
  176. self.dbenv, self.env_path.encode(),
  177. self.env_flags, self.env_perms),
  178. f'Error opening the database environment: {self.env_path}.')
  179. self._init_dbis(create)
  180. self._open = True
  181. cdef void _clear_stale_readers(self) except *:
  182. """
  183. Clear stale readers.
  184. """
  185. cdef int stale_readers
  186. _check(lmdb.mdb_reader_check(self.dbenv, &stale_readers))
  187. if stale_readers > 0:
  188. logger.debug('Cleared {} stale readers.'.format(stale_readers))
  189. cdef void _init_dbis(self, create=True) except *:
  190. """
  191. Initialize databases and cursors.
  192. """
  193. cdef:
  194. size_t i
  195. lmdb.MDB_txn *txn
  196. lmdb.MDB_dbi dbi
  197. # At least one slot (for environments without a database)
  198. self.dbis = <lmdb.MDB_dbi *>PyMem_Malloc(
  199. max(len(self.dbi_labels), 1) * sizeof(lmdb.MDB_dbi))
  200. if not self.dbis:
  201. raise MemoryError()
  202. # DBIs seem to start from 2. We want to map cursor pointers in the
  203. # array to DBIs, so we need an extra slot.
  204. self.curs = <lmdb.MDB_cursor **>PyMem_Malloc(
  205. (len(self.dbi_labels) + 2) * sizeof(lmdb.MDB_cursor*))
  206. if not self.curs:
  207. raise MemoryError()
  208. create_flag = lmdb.MDB_CREATE if create is True else 0
  209. txn_flags = 0 if create else lmdb.MDB_RDONLY
  210. rc = lmdb.mdb_txn_begin(self.dbenv, NULL, txn_flags, &txn)
  211. logger.info(f'Creating DBs.')
  212. try:
  213. if len(self.dbi_labels):
  214. for i, dblabel in enumerate(self.dbi_labels):
  215. flags = self.dbi_flags.get(dblabel, 0) | create_flag
  216. _check(lmdb.mdb_dbi_open(
  217. txn, dblabel.encode(), flags, self.dbis + i))
  218. dbi = self.dbis[i]
  219. logger.debug(f'Created DB {dblabel}: {dbi}')
  220. # Open and close cursor to initialize the memory slot.
  221. _check(lmdb.mdb_cursor_open(
  222. txn, dbi, self.curs + dbi))
  223. #lmdb.mdb_cursor_close(self.curs[dbi])
  224. else:
  225. _check(lmdb.mdb_dbi_open(txn, NULL, 0, self.dbis))
  226. _check(lmdb.mdb_cursor_open(txn, self.dbis[0], self.curs))
  227. #lmdb.mdb_cursor_close(self.curs[self.dbis[0]])
  228. _check(lmdb.mdb_txn_commit(txn))
  229. except:
  230. lmdb.mdb_txn_abort(txn)
  231. raise
  232. cpdef void close_env(self, bint commit_pending_transaction=False) except *:
  233. logger.debug('Cleaning up store env.')
  234. if self.is_open:
  235. logger.debug('Closing store env.')
  236. if self.is_txn_open is True:
  237. if commit_pending_transaction:
  238. self._txn_commit()
  239. else:
  240. self._txn_abort()
  241. self._clear_stale_readers()
  242. PyMem_Free(self.dbis)
  243. PyMem_Free(self.curs)
  244. lmdb.mdb_env_close(self.dbenv)
  245. self._open = False
  246. cpdef void destroy(self, _path='') except *:
  247. """
  248. Destroy the store.
  249. https://www.youtube.com/watch?v=lIVq7FJnPwg
  250. :param str _path: unused. Left for RDFLib API compatibility. (actually
  251. quite dangerous if it were used: it could turn into a
  252. general-purpose recursive file and folder delete method!)
  253. """
  254. if path.exists(self.env_path):
  255. if lmdb.MDB_NOSUBDIR & self.flags:
  256. try:
  257. os.unlink(self.env_path)
  258. except FileNotFoundError:
  259. pass
  260. try:
  261. os.unlink(self.env_path + '-lock')
  262. except FileNotFoundError:
  263. pass
  264. else:
  265. rmtree(self.env_path)
  266. ### PYTHON-ACCESSIBLE METHODS ###
  267. @contextmanager
  268. def txn_ctx(self, write=False):
  269. """
  270. Transaction context manager.
  271. Open and close a transaction for the duration of the functions in the
  272. context. If a transaction has already been opened in the store, a new
  273. one is opened only if the current transaction is read-only and the new
  274. requested transaction is read-write.
  275. If a new write transaction is opened, the old one is kept on hold until
  276. the new transaction is closed, then restored. All cursors are
  277. invalidated and must be restored as well if one needs to reuse them.
  278. :param bool write: Whether a write transaction is to be opened.
  279. :rtype: lmdb.Transaction
  280. """
  281. cdef lmdb.MDB_txn* hold_txn
  282. will_open = False
  283. if not self.is_open:
  284. raise LmdbError('Store is not open.')
  285. # If another transaction is open, only open the new transaction if
  286. # the current one is RO and the new one RW.
  287. if self.is_txn_open:
  288. if write:
  289. will_open = not self.is_txn_rw
  290. else:
  291. will_open = True
  292. # If a new transaction needs to be opened and replace the old one,
  293. # the old one must be put on hold and swapped out when the new txn
  294. # is closed.
  295. if will_open:
  296. will_reset = self.is_txn_open
  297. if will_open:
  298. #logger.debug('Beginning {} transaction.'.format(
  299. # 'RW' if write else 'RO'))
  300. if will_reset:
  301. hold_txn = self.txn
  302. try:
  303. self._txn_begin(write=write)
  304. self.is_txn_rw = write
  305. #logger.debug('In txn_ctx, before yield')
  306. yield
  307. #logger.debug('In txn_ctx, after yield')
  308. self._txn_commit()
  309. #logger.debug('after _txn_commit')
  310. if will_reset:
  311. lmdb.mdb_txn_reset(hold_txn)
  312. self.txn = hold_txn
  313. _check(lmdb.mdb_txn_renew(self.txn))
  314. self.is_txn_rw = False
  315. except:
  316. self._txn_abort()
  317. raise
  318. else:
  319. logger.info(
  320. 'Transaction is already active. Not opening another one.'
  321. )
  322. #logger.debug('before yield')
  323. yield
  324. #logger.debug('after yield')
  325. def begin(self, write=False):
  326. """
  327. Begin a transaction manually if not already in a txn context.
  328. The :py:meth:`txn_ctx` context manager should be used whenever
  329. possible rather than this method.
  330. """
  331. if not self.is_open:
  332. raise RuntimeError('Store must be opened first.')
  333. #logger.debug('Beginning a {} transaction.'.format(
  334. # 'read/write' if write else 'read-only'))
  335. self._txn_begin(write=write)
  336. def commit(self):
  337. """Commit main transaction."""
  338. #logger.debug('Committing transaction.')
  339. self._txn_commit()
  340. def abort(self):
  341. """Abort main transaction."""
  342. #logger.debug('Rolling back transaction.')
  343. self._txn_abort()
  344. def rollback(self):
  345. """Alias for :py:meth:`abort`"""
  346. self.abort()
  347. def key_exists(self, key, dblabel='', new_txn=True):
  348. """
  349. Return whether a key exists in a database (Python-facing method).
  350. Wrap in a new transaction. Only use this if a transaction has not been
  351. opened.
  352. """
  353. if new_txn is True:
  354. with self.txn_ctx():
  355. return self._key_exists(
  356. key, len(key), dblabel=dblabel.encode())
  357. else:
  358. return self._key_exists(key, len(key), dblabel=dblabel.encode())
  359. cdef inline bint _key_exists(
  360. self, unsigned char *key, unsigned char klen,
  361. unsigned char *dblabel=b'') except -1:
  362. """
  363. Return whether a key exists in a database.
  364. To be used within an existing transaction.
  365. """
  366. cdef lmdb.MDB_val key_v, data_v
  367. key_v.mv_data = key
  368. key_v.mv_size = klen
  369. #logger.debug(
  370. # 'Checking if key {} with size {} exists...'.format(key, klen))
  371. try:
  372. _check(lmdb.mdb_get(
  373. self.txn, self.get_dbi(dblabel), &key_v, &data_v))
  374. except KeyNotFoundError:
  375. #logger.debug('...no.')
  376. return False
  377. #logger.debug('...yes.')
  378. return True
  379. def put(self, key, data, dblabel='', flags=0):
  380. """
  381. Put one key/value pair (Python-facing method).
  382. """
  383. self._put(
  384. key, len(key), data, len(data), dblabel=dblabel.encode(),
  385. txn=self.txn, flags=flags)
  386. cdef void _put(
  387. self, unsigned char *key, size_t key_size, unsigned char *data,
  388. size_t data_size, unsigned char *dblabel='',
  389. lmdb.MDB_txn *txn=NULL, unsigned int flags=0) except *:
  390. """
  391. Put one key/value pair.
  392. """
  393. if txn is NULL:
  394. txn = self.txn
  395. key_v.mv_data = key
  396. key_v.mv_size = key_size
  397. data_v.mv_data = data
  398. data_v.mv_size = data_size
  399. #logger.debug('Putting: {}, {} into DB {}'.format(key[: key_size],
  400. # data[: data_size], dblabel))
  401. rc = lmdb.mdb_put(txn, self.get_dbi(dblabel), &key_v, &data_v, flags)
  402. _check(rc, 'Error putting data: {}, {}'.format(
  403. key[: key_size], data[: data_size]))
  404. cpdef bytes get_data(self, key, dblabel=''):
  405. """
  406. Get a single value (non-dup) for a key (Python-facing method).
  407. """
  408. cdef lmdb.MDB_val rv
  409. try:
  410. self._get_data(key, len(key), &rv, dblabel=dblabel.encode())
  411. return (<unsigned char *>rv.mv_data)[: rv.mv_size]
  412. except KeyNotFoundError:
  413. return None
  414. cdef void _get_data(
  415. self, unsigned char *key, size_t klen, lmdb.MDB_val *rv,
  416. unsigned char *dblabel='') except *:
  417. """
  418. Get a single value (non-dup) for a key.
  419. """
  420. cdef:
  421. unsigned char *ret
  422. key_v.mv_data = key
  423. key_v.mv_size = len(key)
  424. _check(
  425. lmdb.mdb_get(self.txn, self.get_dbi(dblabel), &key_v, rv),
  426. 'Error getting data for key \'{}\'.'.format(key.decode()))
  427. def delete(self, key, dblabel=''):
  428. """
  429. Delete one single value by key. Python-facing method.
  430. """
  431. self._delete(key, len(key), dblabel.encode())
  432. cdef void _delete(
  433. self, unsigned char *key, size_t klen,
  434. unsigned char *dblabel=b'') except *:
  435. """
  436. Delete one single value by key from a non-dup database.
  437. TODO Allow deleting duplicate keys.
  438. """
  439. key_v.mv_data = key
  440. key_v.mv_size = klen
  441. try:
  442. _check(lmdb.mdb_del(self.txn, self.get_dbi(dblabel), &key_v, NULL))
  443. except KeyNotFoundError:
  444. pass
  445. cpdef dict stats(self):
  446. """Gather statistics about the database."""
  447. return self._stats()
  448. cdef dict _stats(self):
  449. """
  450. Gather statistics about the database.
  451. Cython-only, non-transaction-aware method.
  452. """
  453. cdef:
  454. lmdb.MDB_stat stat
  455. size_t entries
  456. lmdb.mdb_env_stat(self.dbenv, &stat)
  457. env_stats = <dict>stat
  458. db_stats = {}
  459. for i, dblabel in enumerate(self.dbi_labels):
  460. _check(
  461. lmdb.mdb_stat(self.txn, self.dbis[i], &stat),
  462. 'Error getting datbase stats: {}')
  463. entries = stat.ms_entries
  464. db_stats[dblabel.encode()] = <dict>stat
  465. return {
  466. 'env_stats': env_stats,
  467. 'env_size': os.stat(self.env_path).st_size,
  468. 'db_stats': {
  469. db_label: db_stats[db_label.encode()]
  470. for db_label in self.dbi_labels
  471. },
  472. }
  473. # UNFINISHED
  474. #cdef int _reader_list_callback(self, const unsigned char *msg, void *ctx):
  475. # """
  476. # Callback for reader info function.
  477. # Example from py-lmdb:
  478. # static int env_readers_callback(const char *msg, void *str_)
  479. # {
  480. # PyObject **str = str_;
  481. # PyObject *s = PyUnicode_FromString(msg);
  482. # PyObject *new;
  483. # if(! s) {
  484. # return -1;
  485. # }
  486. # new = PyUnicode_Concat(*str, s);
  487. # Py_CLEAR(*str);
  488. # *str = new;
  489. # if(! new) {
  490. # return -1;
  491. # }
  492. # return 0;
  493. # }
  494. # """
  495. # cdef:
  496. # unicode str = ctx[0].decode('utf-8')
  497. # unicode s = msg.decode('utf-8')
  498. # if not len(s):
  499. # return -1
  500. # str += s
  501. # logger.info('message: {}'.format(msg))
  502. # if not len(str):
  503. # return -1
  504. # ctx = &str
  505. #cpdef str reader_list(self):
  506. # """
  507. # Information about the reader lock table.
  508. # """
  509. # cdef unsigned char *ctx
  510. # lmdb.mdb_reader_list(self.dbenv, <lmdb.MDB_msg_func *>self._reader_list_callback, &ctx)
  511. # logger.info('Reader info: {}'.format(ctx))
  512. # return (ctx).decode('ascii')
  513. ### CYTHON METHODS ###
  514. cdef void _txn_begin(self, write=True, lmdb.MDB_txn *parent=NULL) except *:
  515. if not self.is_open:
  516. raise LmdbError('Store is not open.')
  517. cdef:
  518. unsigned int flags
  519. flags = 0 if write else lmdb.MDB_RDONLY
  520. logger.debug('Opening {} transaction in PID {}, thread {}'.format(
  521. 'RW' if write else 'RO',
  522. multiprocessing.current_process().pid,
  523. threading.currentThread().getName()))
  524. #logger.debug('Readers: {}'.format(self.reader_list()))
  525. rc = lmdb.mdb_txn_begin(self.dbenv, parent, flags, &self.txn)
  526. _check(rc, 'Error opening transaction.')
  527. logger.debug('Opened transaction @ {:x}'.format(<unsigned long>self.txn))
  528. self.is_txn_open = True
  529. self.is_txn_rw = write
  530. logger.debug('txn is open: {}'.format(self.is_txn_open))
  531. cdef void _txn_commit(self) except *:
  532. txid = '{:x}'.format(<unsigned long>self.txn)
  533. try:
  534. _check(lmdb.mdb_txn_commit(self.txn))
  535. logger.debug('Transaction @ {} committed.'.format(txid))
  536. self.is_txn_open = False
  537. self.is_txn_rw = False
  538. except:
  539. self._txn_abort()
  540. raise
  541. cdef void _txn_abort(self) except *:
  542. txid = '{:x}'.format(<unsigned long>self.txn)
  543. lmdb.mdb_txn_abort(self.txn)
  544. self.is_txn_open = False
  545. self.is_txn_rw = False
  546. logger.info('Transaction @ {} aborted.'.format(txid))
  547. cpdef int txn_id(self):
  548. return self._txn_id()
  549. cdef size_t _txn_id(self) except -1:
  550. return lmdb.mdb_txn_id(self.txn)
  551. cdef lmdb.MDB_dbi get_dbi(
  552. self, unsigned char *dblabel=NULL, lmdb.MDB_txn *txn=NULL):
  553. """
  554. Return a DB handle by database name.
  555. """
  556. cdef size_t dbidx
  557. if txn is NULL:
  558. txn = self.txn
  559. if dblabel is NULL:
  560. logger.debug('Getting DBI without label.')
  561. dbidx = (
  562. 0 if dblabel is NULL
  563. else self.dbi_labels.index(dblabel.decode()))
  564. #logger.debug(
  565. # f'Got DBI {self.dbis[dbidx]} with label {dblabel} '
  566. # f'and index #{dbidx}')
  567. return self.dbis[dbidx]
  568. cdef lmdb.MDB_cursor *_cur_open(
  569. self, unsigned char *dblabel=NULL, lmdb.MDB_txn *txn=NULL) except *:
  570. cdef:
  571. lmdb.MDB_dbi dbi
  572. if txn is NULL:
  573. txn = self.txn
  574. dbi = self.get_dbi(dblabel, txn=txn)
  575. logger.debug(f'Opening cursor for DB {dblabel} (DBI {dbi})...')
  576. #try:
  577. # # FIXME Either reuse the cursor, if it works, or remove this code.
  578. # _check(lmdb.mdb_cursor_renew(txn, self.curs[dbi]))
  579. # logger.debug(f'Repurposed existing cursor for DBI {dbi}.')
  580. #except LmdbError as e:
  581. # _check(
  582. # lmdb.mdb_cursor_open(txn, dbi, self.curs + dbi),
  583. # f'Error opening cursor: {dblabel}')
  584. # logger.debug(f'Created brand new cursor for DBI {dbi}.')
  585. _check(
  586. lmdb.mdb_cursor_open(txn, dbi, self.curs + dbi),
  587. f'Error opening cursor: {dblabel}')
  588. logger.debug('...opened @ {:x}.'.format(<unsigned long>self.curs[dbi]))
  589. return self.curs[dbi]
  590. cdef void _cur_close(self, lmdb.MDB_cursor *cur) except *:
  591. """Close a cursor."""
  592. #logger.info('Closing cursor @ {:x} for DBI {}...'.format(
  593. # <unsigned long>cur, lmdb.mdb_cursor_dbi(cur) ))
  594. lmdb.mdb_cursor_close(cur)
  595. #logger.info('...closed.')