]> git.sur5r.net Git - openldap/blobdiff - libraries/liblmdb/mdb.c
ITS#8489 reset cursor EOF flag in cursor_set
[openldap] / libraries / liblmdb / mdb.c
index 8e5b907015dbacec153d364a17b1260a2b1a863a..0110402c5b5bbbdcfb860efa783c759cb4480f52 100644 (file)
@@ -303,6 +303,10 @@ union semun {
 #  define MDB_USE_ROBUST       0
 # else
 #  define MDB_USE_ROBUST       1
+# endif
+#endif /* !MDB_USE_ROBUST */
+
+#if defined(MDB_USE_POSIX_MUTEX) && (MDB_USE_ROBUST)
 /* glibc < 2.12 only provided _np API */
 #  if (defined(__GLIBC__) && GLIBC_VER < 0x02000c) || \
        (defined(PTHREAD_MUTEX_ROBUST_NP) && !defined(PTHREAD_MUTEX_ROBUST))
@@ -310,10 +314,9 @@ union semun {
 #   define pthread_mutexattr_setrobust(attr, flag)     pthread_mutexattr_setrobust_np(attr, flag)
 #   define pthread_mutex_consistent(mutex)     pthread_mutex_consistent_np(mutex)
 #  endif
-# endif
-#endif /* MDB_USE_ROBUST */
+#endif /* MDB_USE_POSIX_MUTEX && MDB_USE_ROBUST */
 
-#if defined(MDB_OWNERDEAD) && MDB_USE_ROBUST
+#if defined(MDB_OWNERDEAD) && (MDB_USE_ROBUST)
 #define MDB_ROBUST_SUPPORTED   1
 #endif
 
@@ -868,9 +871,23 @@ typedef struct MDB_txninfo {
          + (((MDB_PIDLOCK) != 0) << 16)))
 /** @} */
 
-/** Common header for all page types.
- * Overflow records occupy a number of contiguous pages with no
- * headers on any page after the first.
+/** Common header for all page types. The page type depends on #mp_flags.
+ *
+ * #P_BRANCH and #P_LEAF pages have unsorted '#MDB_node's at the end, with
+ * sorted #mp_ptrs[] entries referring to them. Exception: #P_LEAF2 pages
+ * omit mp_ptrs and pack sorted #MDB_DUPFIXED values after the page header.
+ *
+ * #P_OVERFLOW records occupy one or more contiguous pages where only the
+ * first has a page header. They hold the real data of #F_BIGDATA nodes.
+ *
+ * #P_SUBP sub-pages are small leaf "pages" with duplicate data.
+ * A node with flag #F_DUPDATA but not #F_SUBDATA contains a sub-page.
+ * (Duplicate data can also go in sub-databases, which use normal pages.)
+ *
+ * #P_META pages contain #MDB_meta, the start point of an LMDB snapshot.
+ *
+ * Each non-metapage up to #MDB_meta.%mm_last_pg is reachable exactly once
+ * in the snapshot: Either used by a database or listed in a freeDB record.
  */
 typedef struct MDB_page {
 #define        mp_pgno mp_p.p_pgno
@@ -879,7 +896,7 @@ typedef struct MDB_page {
                pgno_t          p_pgno; /**< page number */
                struct MDB_page *p_next; /**< for in-memory list of freed pages */
        } mp_p;
-       uint16_t        mp_pad;
+       uint16_t        mp_pad;                 /**< key size if this is a LEAF2 page */
 /**    @defgroup mdb_page      Page Flags
  *     @ingroup internal
  *     Flags for the page headers.
@@ -946,7 +963,9 @@ typedef struct MDB_page {
        /** The number of overflow pages needed to store the given size. */
 #define OVPAGES(size, psize)   ((PAGEHDRSZ-1 + (size)) / (psize) + 1)
 
-       /** Link in #MDB_txn.%mt_loose_pgs list */
+       /** Link in #MDB_txn.%mt_loose_pgs list.
+        *  Kept outside the page header, which is needed when reusing the page.
+        */
 #define NEXT_LOOSE_PAGE(p)             (*(MDB_page **)((p) + 2))
 
        /** Header for a single key/data pair within a page.
@@ -1074,9 +1093,9 @@ typedef struct MDB_db {
        pgno_t          md_root;                /**< the root page of this tree */
 } MDB_db;
 
-       /** mdb_dbi_open flags */
 #define MDB_VALID      0x8000          /**< DB handle is valid, for me_dbflags */
 #define PERSISTENT_FLAGS       (0xffff & ~(MDB_VALID))
+       /** #mdb_dbi_open() flags */
 #define VALID_FLAGS    (MDB_REVERSEKEY|MDB_DUPSORT|MDB_INTEGERKEY|MDB_DUPFIXED|\
        MDB_INTEGERDUP|MDB_REVERSEDUP|MDB_CREATE)
 
@@ -1115,7 +1134,10 @@ typedef struct MDB_meta {
 #define        mm_psize        mm_dbs[FREE_DBI].md_pad
        /** Any persistent environment flags. @ref mdb_env */
 #define        mm_flags        mm_dbs[FREE_DBI].md_flags
-       pgno_t          mm_last_pg;                     /**< last used page in file */
+       /** Last used page in the datafile.
+        *      Actually the file may be shorter if the freeDB lists the final pages.
+        */
+       pgno_t          mm_last_pg;
        volatile txnid_t        mm_txnid;       /**< txnid that committed this page */
 } MDB_meta;
 
@@ -1168,7 +1190,7 @@ struct MDB_txn {
         *      in this transaction, linked through #NEXT_LOOSE_PAGE(page).
         */
        MDB_page        *mt_loose_pgs;
-       /* #Number of loose pages (#mt_loose_pgs) */
+       /*Number of loose pages (#mt_loose_pgs) */
        int                     mt_loose_count;
        /** The sorted list of dirty pages we temporarily wrote to disk
         *      because the dirty list was full. page numbers in here are
@@ -1720,7 +1742,7 @@ mdb_page_list(MDB_page *mp)
                        pgno, ((MDB_meta *)METADATA(mp))->mm_txnid);
                return;
        default:
-               fprintf(stderr, "Bad page %"Yu" flags 0x%u\n", pgno, mp->mp_flags);
+               fprintf(stderr, "Bad page %"Yu" flags 0x%X\n", pgno, mp->mp_flags);
                return;
        }
 
@@ -6601,8 +6623,10 @@ mdb_cursor_set(MDB_cursor *mc, MDB_val *key, MDB_val *data,
        if (key->mv_size == 0)
                return MDB_BAD_VALSIZE;
 
-       if (mc->mc_xcursor)
+       if (mc->mc_xcursor) {
+               MDB_CURSOR_UNREF(&mc->mc_xcursor->mx_cursor, 0);
                mc->mc_xcursor->mx_cursor.mc_flags &= ~(C_INITIALIZED|C_EOF);
+       }
 
        /* See if we're already on the right page */
        if (mc->mc_flags & C_INITIALIZED) {
@@ -6668,6 +6692,7 @@ mdb_cursor_set(MDB_cursor *mc, MDB_val *key, MDB_val *data,
                                                }
                                        }
                                        rc = 0;
+                                       mc->mc_flags &= ~C_EOF;
                                        goto set2;
                                }
                        }
@@ -6734,8 +6759,6 @@ set1:
                return MDB_SUCCESS;
        }
 
-       if (mc->mc_xcursor)
-               MDB_CURSOR_UNREF(&mc->mc_xcursor->mx_cursor, 0);
        if (F_ISSET(leaf->mn_flags, F_DUPDATA)) {
                mdb_xcursor_init1(mc, leaf);
        }
@@ -8284,6 +8307,9 @@ mdb_cursor_count(MDB_cursor *mc, mdb_size_t *countp)
 void
 mdb_cursor_close(MDB_cursor *mc)
 {
+       if (mc) {
+               MDB_CURSOR_UNREF(mc, 0);
+       }
        if (mc && !mc->mc_backup) {
                /* remove from txn, if tracked */
                if ((mc->mc_flags & C_UNTRACK) && mc->mc_txn->mt_cursors) {