]> git.sur5r.net Git - openldap/blobdiff - libraries/liblmdb/mdb.c
ITS#8582 MDB_LOCK_VERSION = 2 due to format change
[openldap] / libraries / liblmdb / mdb.c
index 5b799e112bcbbda4292ae519340eee563627fc96..21f50f2a880778f7587db20fe36c2db248e309d3 100644 (file)
@@ -5,7 +5,7 @@
  *     BerkeleyDB API, but much simplified.
  */
 /*
- * Copyright 2011-2016 Howard Chu, Symas Corp.
+ * Copyright 2011-2017 Howard Chu, Symas Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -149,7 +149,7 @@ typedef SSIZE_T     ssize_t;
 #include <resolv.h>    /* defines BYTE_ORDER on HPUX and Solaris */
 #endif
 
-#if defined(__APPLE__) || defined (BSD)
+#if defined(__APPLE__) || defined (BSD) || defined(__FreeBSD_kernel__)
 # if !(defined(MDB_USE_POSIX_MUTEX) || defined(MDB_USE_POSIX_SEM))
 # define MDB_USE_SYSV_SEM      1
 # endif
@@ -620,7 +620,7 @@ static txnid_t mdb_debug_start;
        /**     The version number for a database's datafile format. */
 #define MDB_DATA_VERSION        ((MDB_DEVEL) ? 999 : 1)
        /**     The version number for a database's lockfile format. */
-#define MDB_LOCK_VERSION        ((MDB_DEVEL) ? 999 : 1)
+#define MDB_LOCK_VERSION        ((MDB_DEVEL) ? 999 : 2)
 
        /**     @brief The max size of a key we can write, or 0 for computed max.
         *
@@ -809,6 +809,16 @@ typedef struct MDB_txbody {
        uint32_t        mtb_magic;
                /** Format of this lock file. Must be set to #MDB_LOCK_FORMAT. */
        uint32_t        mtb_format;
+               /**     The ID of the last transaction committed to the database.
+                *      This is recorded here only for convenience; the value can always
+                *      be determined by reading the main database meta pages.
+                */
+       volatile txnid_t                mtb_txnid;
+               /** The number of slots that have been used in the reader table.
+                *      This always records the maximum count, it is not decremented
+                *      when readers release their slots.
+                */
+       volatile unsigned       mtb_numreaders;
 #if defined(_WIN32) || defined(MDB_USE_POSIX_SEM)
        char    mtb_rmname[MNAME_LEN];
 #elif defined(MDB_USE_SYSV_SEM)
@@ -820,16 +830,6 @@ typedef struct MDB_txbody {
                 */
        mdb_mutex_t     mtb_rmutex;
 #endif
-               /**     The ID of the last transaction committed to the database.
-                *      This is recorded here only for convenience; the value can always
-                *      be determined by reading the main database meta pages.
-                */
-       volatile txnid_t                mtb_txnid;
-               /** The number of slots that have been used in the reader table.
-                *      This always records the maximum count, it is not decremented
-                *      when readers release their slots.
-                */
-       volatile unsigned       mtb_numreaders;
 } MDB_txbody;
 
        /** The actual reader table definition. */
@@ -978,6 +978,11 @@ typedef struct MDB_page {
         * pgno on branch nodes.  On 64 bit platforms, #mn_flags is also used
         * for pgno.  (Branch nodes have no flags).  Lo and hi are in host byte
         * order in case some accesses can be optimized to 32-bit word access.
+        *
+        * Leaf node flags describe node contents.  #F_BIGDATA says the node's
+        * data part is the page number of an overflow page with actual data.
+        * #F_DUPDATA and #F_SUBDATA can be combined giving duplicate data in
+        * a sub-page/sub-database, and named databases (just #F_SUBDATA).
         */
 typedef struct MDB_node {
        /** part of data size or pgno
@@ -2001,13 +2006,15 @@ static void
 mdb_cursor_unref(MDB_cursor *mc)
 {
        int i;
-       if (!mc->mc_snum || !mc->mc_pg[0] || IS_SUBP(mc->mc_pg[0]))
-               return;
-       for (i=0; i<mc->mc_snum; i++)
-               mdb_page_unref(mc->mc_txn, mc->mc_pg[i]);
-       if (mc->mc_ovpg) {
-               mdb_page_unref(mc->mc_txn, mc->mc_ovpg);
-               mc->mc_ovpg = 0;
+       if (mc->mc_txn->mt_rpages[0].mid) {
+               if (!mc->mc_snum || !mc->mc_pg[0] || IS_SUBP(mc->mc_pg[0]))
+                       return;
+               for (i=0; i<mc->mc_snum; i++)
+                       mdb_page_unref(mc->mc_txn, mc->mc_pg[i]);
+               if (mc->mc_ovpg) {
+                       mdb_page_unref(mc->mc_txn, mc->mc_ovpg);
+                       mc->mc_ovpg = 0;
+               }
        }
        mc->mc_snum = mc->mc_top = 0;
        mc->mc_pg[0] = NULL;
@@ -6199,8 +6206,17 @@ mdb_page_search_root(MDB_cursor *mc, MDB_val *key, int flags)
 
                if (flags & (MDB_PS_FIRST|MDB_PS_LAST)) {
                        i = 0;
-                       if (flags & MDB_PS_LAST)
+                       if (flags & MDB_PS_LAST) {
                                i = NUMKEYS(mp) - 1;
+                               /* if already init'd, see if we're already in right place */
+                               if (mc->mc_flags & C_INITIALIZED) {
+                                       if (mc->mc_ki[mc->mc_top] == i) {
+                                               mc->mc_top = mc->mc_snum++;
+                                               mp = mc->mc_pg[mc->mc_top];
+                                               goto ready;
+                                       }
+                               }
+                       }
                } else {
                        int      exact;
                        node = mdb_node_search(mc, key, &exact);
@@ -6226,6 +6242,7 @@ mdb_page_search_root(MDB_cursor *mc, MDB_val *key, int flags)
                if ((rc = mdb_cursor_push(mc, mp)))
                        return rc;
 
+ready:
                if (flags & MDB_PS_MODIFY) {
                        if ((rc = mdb_page_touch(mc)) != 0)
                                return rc;
@@ -6581,15 +6598,20 @@ mdb_cursor_next(MDB_cursor *mc, MDB_val *key, MDB_val *data, MDB_cursor_op op)
        MDB_node        *leaf;
        int rc;
 
-       if ((mc->mc_flags & C_EOF) ||
-               ((mc->mc_flags & C_DEL) && op == MDB_NEXT_DUP)) {
+       if ((mc->mc_flags & C_DEL && op == MDB_NEXT_DUP))
                return MDB_NOTFOUND;
-       }
+
        if (!(mc->mc_flags & C_INITIALIZED))
                return mdb_cursor_first(mc, key, data);
 
        mp = mc->mc_pg[mc->mc_top];
 
+       if (mc->mc_flags & C_EOF) {
+               if (mc->mc_ki[mc->mc_top] >= NUMKEYS(mp)-1)
+                       return MDB_NOTFOUND;
+               mc->mc_flags ^= C_EOF;
+       }
+
        if (mc->mc_db->md_flags & MDB_DUPSORT) {
                leaf = NODEPTR(mp, mc->mc_ki[mc->mc_top]);
                if (F_ISSET(leaf->mn_flags, F_DUPDATA)) {
@@ -7004,16 +7026,13 @@ mdb_cursor_last(MDB_cursor *mc, MDB_val *key, MDB_val *data)
                mc->mc_xcursor->mx_cursor.mc_flags &= ~(C_INITIALIZED|C_EOF);
        }
 
-       if (!(mc->mc_flags & C_EOF)) {
-
-               if (!(mc->mc_flags & C_INITIALIZED) || mc->mc_top) {
-                       rc = mdb_page_search(mc, NULL, MDB_PS_LAST);
-                       if (rc != MDB_SUCCESS)
-                               return rc;
-               }
-               mdb_cassert(mc, IS_LEAF(mc->mc_pg[mc->mc_top]));
-
+       if (!(mc->mc_flags & C_INITIALIZED) || mc->mc_top) {
+               rc = mdb_page_search(mc, NULL, MDB_PS_LAST);
+               if (rc != MDB_SUCCESS)
+                       return rc;
        }
+       mdb_cassert(mc, IS_LEAF(mc->mc_pg[mc->mc_top]));
+
        mc->mc_ki[mc->mc_top] = NUMKEYS(mc->mc_pg[mc->mc_top]) - 1;
        mc->mc_flags |= C_INITIALIZED|C_EOF;
        leaf = NODEPTR(mc->mc_pg[mc->mc_top], mc->mc_ki[mc->mc_top]);
@@ -8422,9 +8441,15 @@ mdb_cursor_count(MDB_cursor *mc, mdb_size_t *countp)
        if (!(mc->mc_flags & C_INITIALIZED))
                return EINVAL;
 
-       if (!mc->mc_snum || (mc->mc_flags & C_EOF))
+       if (!mc->mc_snum)
                return MDB_NOTFOUND;
 
+       if (mc->mc_flags & C_EOF) {
+               if (mc->mc_ki[mc->mc_top] >= NUMKEYS(mc->mc_pg[mc->mc_top]))
+                       return MDB_NOTFOUND;
+               mc->mc_flags ^= C_EOF;
+       }
+
        leaf = NODEPTR(mc->mc_pg[mc->mc_top], mc->mc_ki[mc->mc_top]);
        if (!F_ISSET(leaf->mn_flags, F_DUPDATA)) {
                *countp = 1;