unsigned int i;
if (key->mv_size > MAXKEYSIZE)
return "MAXKEYSIZE";
-#if 0
+#if 1
for (i=0; i<key->mv_size; i++)
ptr += sprintf(ptr, "%02x", *c++);
#else
return ENOMEM;
DPRINTF("touched db %u page %lu -> %lu", dbi, mp->mp_pgno, dp->p.mp_pgno);
assert(mp->mp_pgno != dp->p.mp_pgno);
- mdb_midl_insert(txn->mt_free_pgs, mp->mp_pgno);
+ mdb_midl_append(txn->mt_free_pgs, mp->mp_pgno);
pgno = dp->p.mp_pgno;
memcpy(&dp->p, mp, txn->mt_env->me_psize);
mp = &dp->p;
mc.mc_snum = 0;
mdb_search_page(txn, FREE_DBI, &key, &mc, 1, &mpp);
+ mdb_midl_sort(txn->mt_free_pgs);
#if DEBUG > 1
{
unsigned int i;
#endif
{
/* free it */
- mdb_midl_insert(mc->mc_txn->mt_free_pgs, pg);
+ mdb_midl_append(mc->mc_txn->mt_free_pgs, pg);
}
}
rc = mdb_sibling(&mc->mc_xcursor->mx_cursor, 1);
#endif
{
/* free it */
- mdb_midl_insert(mc->mc_txn->mt_free_pgs,
+ mdb_midl_append(mc->mc_txn->mt_free_pgs,
mc->mc_xcursor->mx_txn.mt_dbs[mc->mc_xcursor->mx_cursor.mc_dbi].md_root);
}
}
txn->mt_dbs[dbi].md_root = P_INVALID;
txn->mt_dbs[dbi].md_depth = 0;
txn->mt_dbs[dbi].md_leaf_pages = 0;
- mdb_midl_insert(txn->mt_free_pgs, mpp->mp_page->mp_pgno);
+ mdb_midl_append(txn->mt_free_pgs, mpp->mp_page->mp_pgno);
} else if (IS_BRANCH(mpp->mp_page) && NUMKEYS(mpp->mp_page) == 1) {
DPUTS("collapsing root page!");
- mdb_midl_insert(txn->mt_free_pgs, mpp->mp_page->mp_pgno);
+ mdb_midl_append(txn->mt_free_pgs, mpp->mp_page->mp_pgno);
txn->mt_dbs[dbi].md_root = NODEPGNO(NODEPTR(mpp->mp_page, 0));
if ((rc = mdb_get_page(txn, txn->mt_dbs[dbi].md_root, &root)))
return rc;
ovpages = OVPAGES(NODEDSZ(leaf), mc->mc_txn->mt_env->me_psize);
for (i=0; i<ovpages; i++) {
DPRINTF("freed ov page %lu", pg);
- mdb_midl_insert(mc->mc_txn->mt_free_pgs, pg);
+ mdb_midl_append(mc->mc_txn->mt_free_pgs, pg);
pg++;
}
}
*/
#define CMP(x,y) ( (x) < (y) ? -1 : (x) > (y) )
+#if 0
static unsigned mdb_midl_search( IDL ids, ID id )
{
/*
if ( x <= ids[0] && ids[x] == id ) {
/* duplicate */
+ assert(0);
return -1;
}
return 0;
}
+#endif
+
+int mdb_midl_append( IDL ids, ID id )
+{
+ /* Too big? */
+ if (ids[0] >= MDB_IDL_UM_SIZE)
+ return -1;
+ ids[0]++;
+ ids[ids[0]] = id;
+ return 0;
+}
+
+/* Quicksort + Insertion sort for small arrays */
+
+#define SMALL 8
+#define SWAP(a,b) itmp=(a);(a)=(b);(b)=itmp
+
+void
+mdb_midl_sort( ID *ids )
+{
+ int istack[16*sizeof(int)];
+ int i,j,k,l,ir,jstack;
+ ID a, itmp;
+
+ ir = ids[0];
+ l = 1;
+ jstack = 0;
+ for(;;) {
+ if (ir - l < SMALL) { /* Insertion sort */
+ for (j=l+1;j<=ir;j++) {
+ a = ids[j];
+ for (i=j-1;i>=1;i--) {
+ if (ids[i] >= a) break;
+ ids[i+1] = ids[i];
+ }
+ ids[i+1] = a;
+ }
+ if (jstack == 0) break;
+ ir = istack[jstack--];
+ l = istack[jstack--];
+ } else {
+ k = (l + ir) >> 1; /* Choose median of left, center, right */
+ SWAP(ids[k], ids[l+1]);
+ if (ids[l] < ids[ir]) {
+ SWAP(ids[l], ids[ir]);
+ }
+ if (ids[l+1] < ids[ir]) {
+ SWAP(ids[l+1], ids[ir]);
+ }
+ if (ids[l] < ids[l+1]) {
+ SWAP(ids[l], ids[l+1]);
+ }
+ i = l+1;
+ j = ir;
+ a = ids[l+1];
+ for(;;) {
+ do i++; while(ids[i] > a);
+ do j--; while(ids[j] < a);
+ if (j < i) break;
+ SWAP(ids[i],ids[j]);
+ }
+ ids[l+1] = ids[j];
+ ids[j] = a;
+ jstack += 2;
+ if (ir-i+1 >= j-1) {
+ istack[jstack] = ir;
+ istack[jstack-1] = i;
+ ir = j-1;
+ } else {
+ istack[jstack] = j-1;
+ istack[jstack-1] = l;
+ l = i;
+ }
+ }
+ }
+}
unsigned mdb_mid2l_search( ID2L ids, ID id )
{
*/
int mdb_midl_insert( IDL ids, ID id );
+ /** Append an ID onto an IDL.
+ * @param[in,out] ids The IDL to append to.
+ * @param[in] id The ID to append.
+ * @return 0 on success, -2 if the IDL is too large.
+ */
+int mdb_midl_append( IDL ids, ID id );
+
+ /** Sort an IDL.
+ * @param[in,out] ids The IDL to sort.
+ */
+void mdb_midl_sort( IDL ids );
+
/** An ID2 is an ID/pointer pair.
*/
typedef struct ID2 {