mirror of
https://github.com/isar/libmdbx.git
synced 2025-12-16 05:02:21 +08:00
Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c5e72817ca | ||
|
|
e1ce55d0e6 | ||
|
|
d23a3f3bc4 | ||
|
|
b1620bbe47 | ||
|
|
5bb92a4277 | ||
|
|
c381573d1f | ||
|
|
31873e8e2c | ||
|
|
e94d6efec6 |
9
.circleci/config.yml
Normal file
9
.circleci/config.yml
Normal file
@@ -0,0 +1,9 @@
|
||||
version: 2
|
||||
jobs:
|
||||
build:
|
||||
docker:
|
||||
- image: circleci/buildpack-deps:artful
|
||||
steps:
|
||||
- checkout
|
||||
- run: make all lmdb
|
||||
- run: make check
|
||||
10
README.md
10
README.md
@@ -5,8 +5,16 @@ Extended LMDB, aka "Расширенная LMDB".
|
||||
*The Future will Positive. Всё будет хорошо.*
|
||||
[](https://travis-ci.org/leo-yuriev/libmdbx)
|
||||
|
||||
English version by Google [is here](https://translate.googleusercontent.com/translate_c?act=url&ie=UTF8&sl=ru&tl=en&u=https://github.com/leo-yuriev/libmdbx/tree/stable%2F0.0).
|
||||
## Project Status for now
|
||||
|
||||
- The stable versions (the _stable/*_ branches) of are frozen, i.e. no new features or API changes, but only bug fixes.
|
||||
- The next (the _devel_ branch) version **is under active development**, i.e. current API and set of features are extreme volatile.
|
||||
- The immediate goal of development is formation of the stable API and the stable internal database format, which allows realise all planned features.
|
||||
- Planned features: Integrity check by Merkle tree, Support for raw block devices, Separate place for large data items, Using "roaring bitmaps" for garbage collector, Non-linear page reclaiming, Asynchronous lazy data flush to disk(s), etc.
|
||||
|
||||
-----
|
||||
|
||||
English version by Google [is here](https://translate.googleusercontent.com/translate_c?act=url&ie=UTF8&sl=ru&tl=en&u=https://github.com/leo-yuriev/libmdbx/tree/stable%2F0.0).
|
||||
|
||||
## Кратко
|
||||
|
||||
|
||||
14
circle.yml
14
circle.yml
@@ -1,14 +0,0 @@
|
||||
machine:
|
||||
timezone:
|
||||
Europe/Moscow
|
||||
|
||||
database:
|
||||
override:
|
||||
|
||||
compile:
|
||||
override:
|
||||
- make all lmdb
|
||||
|
||||
test:
|
||||
override:
|
||||
- make check
|
||||
10
lmdb.h
10
lmdb.h
@@ -390,7 +390,7 @@ typedef enum MDB_cursor_op {
|
||||
MDB_GET_BOTH, /**< Position at key/data pair. Only for #MDB_DUPSORT */
|
||||
MDB_GET_BOTH_RANGE, /**< position at key, nearest data. Only for #MDB_DUPSORT */
|
||||
MDB_GET_CURRENT, /**< Return key/data at current cursor position */
|
||||
MDB_GET_MULTIPLE, /**< Return key and up to a page of duplicate data items
|
||||
MDB_GET_MULTIPLE, /**< Return up to a page of duplicate data items
|
||||
from current cursor position. Move cursor to prepare
|
||||
for #MDB_NEXT_MULTIPLE. Only for #MDB_DUPFIXED */
|
||||
MDB_LAST, /**< Position at last key/data item */
|
||||
@@ -399,7 +399,7 @@ typedef enum MDB_cursor_op {
|
||||
MDB_NEXT, /**< Position at next data item */
|
||||
MDB_NEXT_DUP, /**< Position at next data item of current key.
|
||||
Only for #MDB_DUPSORT */
|
||||
MDB_NEXT_MULTIPLE, /**< Return key and up to a page of duplicate data items
|
||||
MDB_NEXT_MULTIPLE, /**< Return up to a page of duplicate data items
|
||||
from next cursor position. Move cursor to prepare
|
||||
for #MDB_NEXT_MULTIPLE. Only for #MDB_DUPFIXED */
|
||||
MDB_NEXT_NODUP, /**< Position at first data item of next key */
|
||||
@@ -410,7 +410,7 @@ typedef enum MDB_cursor_op {
|
||||
MDB_SET, /**< Position at specified key */
|
||||
MDB_SET_KEY, /**< Position at specified key, return key + data */
|
||||
MDB_SET_RANGE, /**< Position at first key greater than or equal to specified key. */
|
||||
MDB_PREV_MULTIPLE /**< Position at previous page and return key and up to
|
||||
MDB_PREV_MULTIPLE /**< Position at previous page and return up to
|
||||
a page of duplicate data items. Only for #MDB_DUPFIXED */
|
||||
} MDB_cursor_op;
|
||||
|
||||
@@ -1589,6 +1589,10 @@ int mdb_cursor_put(MDB_cursor *cursor, MDB_val *key, MDB_val *data,
|
||||
/** @brief Delete current key/data pair
|
||||
*
|
||||
* This function deletes the key/data pair to which the cursor refers.
|
||||
* This does not invalidate the cursor, so operations such as MDB_NEXT
|
||||
* can still be used on it.
|
||||
* Both MDB_NEXT and MDB_GET_CURRENT will return the same record after
|
||||
* this operation.
|
||||
* @param[in] cursor A cursor handle returned by #mdb_cursor_open()
|
||||
* @param[in] flags Options for this operation. This parameter
|
||||
* must be set to 0 or one of the values described here.
|
||||
|
||||
184
mdb.c
184
mdb.c
@@ -602,6 +602,8 @@ typedef struct MDB_page {
|
||||
indx_t mp_ptrs[1]; /**< dynamic size */
|
||||
} MDB_page;
|
||||
|
||||
#define PAGETYPE(p) ((p)->mp_flags & (P_BRANCH | P_LEAF | P_LEAF2 | P_OVERFLOW))
|
||||
|
||||
/** Size of the page header, excluding dynamic data at the end */
|
||||
#define PAGEHDRSZ ((unsigned) offsetof(MDB_page, mp_ptrs))
|
||||
|
||||
@@ -1304,9 +1306,6 @@ static void mdb_debug_log(int type, const char *function, int line, const char *
|
||||
# define mdb_assert_enabled() \
|
||||
unlikely(mdb_runtime_flags & MDBX_DBG_ASSERT)
|
||||
|
||||
# define mdb_audit_enabled() \
|
||||
unlikely(mdb_runtime_flags & MDBX_DBG_AUDIT)
|
||||
|
||||
# define mdb_debug_enabled(type) \
|
||||
unlikely(mdb_runtime_flags & \
|
||||
(type & (MDBX_DBG_TRACE | MDBX_DBG_EXTRA)))
|
||||
@@ -1317,7 +1316,6 @@ static void mdb_debug_log(int type, const char *function, int line, const char *
|
||||
# else
|
||||
# define mdb_debug_enabled(type) (0)
|
||||
# endif
|
||||
# define mdb_audit_enabled() (0)
|
||||
# define mdb_assert_enabled() (0)
|
||||
# define mdb_assert_fail(env, msg, func, line) \
|
||||
__assert_fail(msg, __FILE__, line, func)
|
||||
@@ -1427,161 +1425,6 @@ mdb_dkey(MDB_val *key, char *buf)
|
||||
return buf;
|
||||
}
|
||||
|
||||
#if 0 /* LY: debug stuff */
|
||||
static const char *
|
||||
mdb_leafnode_type(MDB_node *n)
|
||||
{
|
||||
static char *const tp[2][2] = {{"", ": DB"}, {": sub-page", ": sub-DB"}};
|
||||
return F_ISSET(n->mn_flags, F_BIGDATA) ? ": overflow page" :
|
||||
tp[F_ISSET(n->mn_flags, F_DUPDATA)][F_ISSET(n->mn_flags, F_SUBDATA)];
|
||||
}
|
||||
|
||||
/** Display all the keys in the page. */
|
||||
static void
|
||||
mdb_page_list(MDB_page *mp)
|
||||
{
|
||||
pgno_t pgno = mdb_dbg_pgno(mp);
|
||||
const char *type, *state = (mp->mp_flags & P_DIRTY) ? ", dirty" : "";
|
||||
MDB_node *node;
|
||||
unsigned i, nkeys, nsize, total = 0;
|
||||
MDB_val key;
|
||||
DKBUF;
|
||||
|
||||
switch (mp->mp_flags & (P_BRANCH|P_LEAF|P_LEAF2|P_META|P_OVERFLOW|P_SUBP)) {
|
||||
case P_BRANCH: type = "Branch page"; break;
|
||||
case P_LEAF: type = "Leaf page"; break;
|
||||
case P_LEAF|P_SUBP: type = "Sub-page"; break;
|
||||
case P_LEAF|P_LEAF2: type = "LEAF2 page"; break;
|
||||
case P_LEAF|P_LEAF2|P_SUBP: type = "LEAF2 sub-page"; break;
|
||||
case P_OVERFLOW:
|
||||
mdb_print("Overflow page %zu pages %u%s\n",
|
||||
pgno, mp->mp_pages, state);
|
||||
return;
|
||||
case P_META:
|
||||
mdb_print("Meta-page %zu txnid %zu\n",
|
||||
pgno, ((MDB_meta *)PAGEDATA(mp))->mm_txnid);
|
||||
return;
|
||||
default:
|
||||
mdb_print("Bad page %zu flags 0x%X\n", pgno, mp->mp_flags);
|
||||
return;
|
||||
}
|
||||
|
||||
nkeys = NUMKEYS(mp);
|
||||
mdb_print("%s %zu numkeys %u%s\n", type, pgno, nkeys, state);
|
||||
|
||||
for (i=0; i<nkeys; i++) {
|
||||
if (IS_LEAF2(mp)) { /* LEAF2 pages have no mp_ptrs[] or node headers */
|
||||
key.mv_size = nsize = mp->mp_leaf2_ksize;
|
||||
key.mv_data = LEAF2KEY(mp, i, nsize);
|
||||
total += nsize;
|
||||
mdb_print("key %u: nsize %u, %s\n", i, nsize, DKEY(&key));
|
||||
continue;
|
||||
}
|
||||
node = NODEPTR(mp, i);
|
||||
key.mv_size = node->mn_ksize;
|
||||
key.mv_data = node->mn_data;
|
||||
nsize = NODESIZE + key.mv_size;
|
||||
if (IS_BRANCH(mp)) {
|
||||
mdb_print("key %u: page %zu, %s\n", i, NODEPGNO(node), DKEY(&key));
|
||||
total += nsize;
|
||||
} else {
|
||||
if (F_ISSET(node->mn_flags, F_BIGDATA))
|
||||
nsize += sizeof(pgno_t);
|
||||
else
|
||||
nsize += NODEDSZ(node);
|
||||
total += nsize;
|
||||
nsize += sizeof(indx_t);
|
||||
mdb_print("key %u: nsize %u, %s%s\n",
|
||||
i, nsize, DKEY(&key), mdb_leafnode_type(node));
|
||||
}
|
||||
total = EVEN(total);
|
||||
}
|
||||
mdb_print("Total: header %u + contents %u + unused %u\n",
|
||||
IS_LEAF2(mp) ? PAGEHDRSZ : PAGEBASE + mp->mp_lower, total, SIZELEFT(mp));
|
||||
}
|
||||
|
||||
static void
|
||||
mdb_cursor_chk(MDB_cursor *mc)
|
||||
{
|
||||
unsigned i;
|
||||
MDB_node *node;
|
||||
MDB_page *mp;
|
||||
|
||||
if (!mc->mc_snum || !(mc->mc_flags & C_INITIALIZED)) return;
|
||||
for (i=0; i<mc->mc_top; i++) {
|
||||
mp = mc->mc_pg[i];
|
||||
node = NODEPTR(mp, mc->mc_ki[i]);
|
||||
if (unlikely(NODEPGNO(node) != mc->mc_pg[i+1]->mp_pgno))
|
||||
mdb_print("oops!\n");
|
||||
}
|
||||
if (unlikely(mc->mc_ki[i] >= NUMKEYS(mc->mc_pg[i])))
|
||||
mdb_print("ack!\n");
|
||||
if (XCURSOR_INITED(mc)) {
|
||||
node = NODEPTR(mc->mc_pg[mc->mc_top], mc->mc_ki[mc->mc_top]);
|
||||
if (((node->mn_flags & (F_DUPDATA|F_SUBDATA)) == F_DUPDATA) &&
|
||||
mc->mc_xcursor->mx_cursor.mc_pg[0] != NODEDATA(node)) {
|
||||
mdb_print("blah!\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif /* 0 */
|
||||
|
||||
/** Count all the pages in each DB and in the freelist
|
||||
* and make sure it matches the actual number of pages
|
||||
* being used.
|
||||
* All named DBs must be open for a correct count.
|
||||
*/
|
||||
static void mdb_audit(MDB_txn *txn)
|
||||
{
|
||||
MDB_cursor mc;
|
||||
MDB_val key, data;
|
||||
MDB_ID freecount, count;
|
||||
MDB_dbi i;
|
||||
int rc;
|
||||
|
||||
freecount = 0;
|
||||
mdb_cursor_init(&mc, txn, FREE_DBI, NULL);
|
||||
while ((rc = mdb_cursor_get(&mc, &key, &data, MDB_NEXT)) == 0)
|
||||
freecount += *(MDB_ID *)data.mv_data;
|
||||
mdb_tassert(txn, rc == MDB_NOTFOUND);
|
||||
|
||||
count = 0;
|
||||
for (i = 0; i<txn->mt_numdbs; i++) {
|
||||
MDB_xcursor mx;
|
||||
if (!(txn->mt_dbflags[i] & DB_VALID))
|
||||
continue;
|
||||
mdb_cursor_init(&mc, txn, i, &mx);
|
||||
if (txn->mt_dbs[i].md_root == P_INVALID)
|
||||
continue;
|
||||
count += txn->mt_dbs[i].md_branch_pages +
|
||||
txn->mt_dbs[i].md_leaf_pages +
|
||||
txn->mt_dbs[i].md_overflow_pages;
|
||||
if (txn->mt_dbs[i].md_flags & MDB_DUPSORT) {
|
||||
rc = mdb_page_search(&mc, NULL, MDB_PS_FIRST);
|
||||
for (; rc == MDB_SUCCESS; rc = mdb_cursor_sibling(&mc, 1)) {
|
||||
unsigned j;
|
||||
MDB_page *mp;
|
||||
mp = mc.mc_pg[mc.mc_top];
|
||||
for (j=0; j<NUMKEYS(mp); j++) {
|
||||
MDB_node *leaf = NODEPTR(mp, j);
|
||||
if (leaf->mn_flags & F_SUBDATA) {
|
||||
MDB_db db;
|
||||
memcpy(&db, NODEDATA(leaf), sizeof(db));
|
||||
count += db.md_branch_pages + db.md_leaf_pages +
|
||||
db.md_overflow_pages;
|
||||
}
|
||||
}
|
||||
}
|
||||
mdb_tassert(txn, rc == MDB_NOTFOUND);
|
||||
}
|
||||
}
|
||||
if (freecount + count + NUM_METAS != txn->mt_next_pgno) {
|
||||
mdb_print("audit: %lu freecount: %lu count: %lu total: %lu next_pgno: %lu\n",
|
||||
txn->mt_txnid, freecount, count+NUM_METAS,
|
||||
freecount+count+NUM_METAS, txn->mt_next_pgno);
|
||||
}
|
||||
}
|
||||
|
||||
int
|
||||
mdb_cmp(MDB_txn *txn, MDB_dbi dbi, const MDB_val *a, const MDB_val *b)
|
||||
{
|
||||
@@ -3364,6 +3207,8 @@ mdb_freelist_save(MDB_txn *txn)
|
||||
const int lifo = (env->me_flags & MDBX_LIFORECLAIM) != 0;
|
||||
|
||||
mdb_cursor_init(&mc, txn, FREE_DBI, NULL);
|
||||
mc.mc_next = txn->mt_cursors[FREE_DBI];
|
||||
txn->mt_cursors[FREE_DBI] = &mc;
|
||||
|
||||
/* MDB_RESERVE cancels meminit in ovpage malloc (when no WRITEMAP) */
|
||||
clean_limit = (env->me_flags & (MDB_NOMEMINIT|MDB_WRITEMAP))
|
||||
@@ -3645,6 +3490,7 @@ bailout:
|
||||
txn->mt_lifo_reclaimed = NULL;
|
||||
}
|
||||
}
|
||||
txn->mt_cursors[FREE_DBI] = mc.mc_next;
|
||||
|
||||
return rc;
|
||||
}
|
||||
@@ -3990,9 +3836,6 @@ mdb_txn_commit(MDB_txn *txn)
|
||||
env->me_pghead = NULL;
|
||||
mdb_midl_shrink(&txn->mt_free_pgs);
|
||||
|
||||
if (mdb_audit_enabled())
|
||||
mdb_audit(txn);
|
||||
|
||||
rc = mdb_page_flush(txn, 0);
|
||||
if (likely(rc == MDB_SUCCESS)) {
|
||||
MDB_meta meta;
|
||||
@@ -7032,7 +6875,8 @@ more:
|
||||
offset *= 4; /* space for 4 more */
|
||||
break;
|
||||
}
|
||||
/* FALLTHRU: Big enough MDB_DUPFIXED sub-page */
|
||||
/* Big enough MDB_DUPFIXED sub-page */
|
||||
/* fallthrough */
|
||||
case MDB_CURRENT | MDB_NODUPDATA:
|
||||
case MDB_CURRENT:
|
||||
fp->mp_flags |= P_DIRTY;
|
||||
@@ -8165,6 +8009,12 @@ mdb_node_move(MDB_cursor *csrc, MDB_cursor *cdst, int fromleft)
|
||||
|
||||
DKBUF;
|
||||
|
||||
mdb_tassert(csrc->mc_txn, PAGETYPE(csrc->mc_pg[csrc->mc_top]) == PAGETYPE(cdst->mc_pg[cdst->mc_top]));
|
||||
if (unlikely(PAGETYPE(csrc->mc_pg[csrc->mc_top]) != PAGETYPE(cdst->mc_pg[cdst->mc_top]))) {
|
||||
cdst->mc_txn->mt_flags |= MDB_TXN_ERROR;
|
||||
return MDB_PROBLEM;
|
||||
}
|
||||
|
||||
/* Mark src and dst as dirty. */
|
||||
if (unlikely((rc = mdb_page_touch(csrc)) ||
|
||||
(rc = mdb_page_touch(cdst))))
|
||||
@@ -8395,6 +8245,12 @@ mdb_page_merge(MDB_cursor *csrc, MDB_cursor *cdst)
|
||||
mdb_cassert(csrc, csrc->mc_snum > 1); /* can't merge root page */
|
||||
mdb_cassert(csrc, cdst->mc_snum > 1);
|
||||
|
||||
mdb_tassert(csrc->mc_txn, PAGETYPE(psrc) == PAGETYPE(pdst));
|
||||
if (unlikely(PAGETYPE(psrc) != PAGETYPE(pdst))) {
|
||||
cdst->mc_txn->mt_flags |= MDB_TXN_ERROR;
|
||||
return MDB_PROBLEM;
|
||||
}
|
||||
|
||||
/* Mark dst as dirty. */
|
||||
if (unlikely(rc = mdb_page_touch(cdst)))
|
||||
return rc;
|
||||
@@ -8598,7 +8454,7 @@ mdb_rebalance(MDB_cursor *mc)
|
||||
m3 = &m2->mc_xcursor->mx_cursor;
|
||||
else
|
||||
m3 = m2;
|
||||
if (!(m3->mc_flags & C_INITIALIZED) || (m3->mc_snum < mc->mc_snum))
|
||||
if (m3 == mc || !(m3->mc_flags & C_INITIALIZED))
|
||||
continue;
|
||||
if (m3->mc_pg[0] == mp) {
|
||||
m3->mc_snum = 0;
|
||||
|
||||
@@ -186,7 +186,7 @@ int main(int argc, char *argv[])
|
||||
break;
|
||||
case 'l':
|
||||
list = 1;
|
||||
/*FALLTHROUGH*/;
|
||||
/* fallthrough */
|
||||
case 'a':
|
||||
if (subname)
|
||||
usage(prog);
|
||||
|
||||
2
mdbx.c
2
mdbx.c
@@ -530,7 +530,7 @@ int mdbx_replace(MDB_txn *txn, MDB_dbi dbi,
|
||||
rc = mdbx_cursor_get(&mc, &present_key, &present_data, MDB_SET_KEY);
|
||||
if (unlikely(rc != MDB_SUCCESS)) {
|
||||
old_data->iov_base = NULL;
|
||||
old_data->iov_len = rc;
|
||||
old_data->iov_len = 0;
|
||||
if (rc != MDB_NOTFOUND || (flags & MDB_CURRENT))
|
||||
goto bailout;
|
||||
} else if (flags & MDB_NOOVERWRITE) {
|
||||
|
||||
1
mtest0.c
1
mtest0.c
@@ -21,6 +21,7 @@
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/sysmacros.h>
|
||||
#include "mdbx.h"
|
||||
|
||||
#include <pthread.h>
|
||||
|
||||
1
mtest1.c
1
mtest1.c
@@ -20,6 +20,7 @@
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/sysmacros.h>
|
||||
#include "mdbx.h"
|
||||
|
||||
#define E(expr) CHECK((rc = (expr)) == MDB_SUCCESS, #expr)
|
||||
|
||||
1
mtest2.c
1
mtest2.c
@@ -23,6 +23,7 @@
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/sysmacros.h>
|
||||
#include "mdbx.h"
|
||||
|
||||
#define E(expr) CHECK((rc = (expr)) == MDB_SUCCESS, #expr)
|
||||
|
||||
1
mtest3.c
1
mtest3.c
@@ -23,6 +23,7 @@
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/sysmacros.h>
|
||||
#include "mdbx.h"
|
||||
|
||||
#define E(expr) CHECK((rc = (expr)) == MDB_SUCCESS, #expr)
|
||||
|
||||
1
mtest4.c
1
mtest4.c
@@ -23,6 +23,7 @@
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/sysmacros.h>
|
||||
#include "mdbx.h"
|
||||
|
||||
#define E(expr) CHECK((rc = (expr)) == MDB_SUCCESS, #expr)
|
||||
|
||||
1
mtest5.c
1
mtest5.c
@@ -23,6 +23,7 @@
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/sysmacros.h>
|
||||
#include "mdbx.h"
|
||||
|
||||
#define E(expr) CHECK((rc = (expr)) == MDB_SUCCESS, #expr)
|
||||
|
||||
1
mtest6.c
1
mtest6.c
@@ -23,6 +23,7 @@
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/sysmacros.h>
|
||||
#include "mdbx.h"
|
||||
|
||||
#define E(expr) CHECK((rc = (expr)) == MDB_SUCCESS, #expr)
|
||||
|
||||
1
mtest7.c
1
mtest7.c
@@ -18,6 +18,7 @@
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
#include <sys/time.h>
|
||||
#include <sys/sysmacros.h>
|
||||
#include "mdbx.h"
|
||||
|
||||
#define E(expr) CHECK((rc = (expr)) == MDB_SUCCESS, #expr)
|
||||
|
||||
Reference in New Issue
Block a user