+static int
+mdb_cursor_touch(MDB_cursor *mc)
+{
+ MDB_pageparent mpp;
+ MDB_dhead *dh;
+ unsigned int i;
+ int rc;
+
+ mpp.mp_parent = NULL;
+ mpp.mp_pi = 0;
+ if (mc->mc_dbi > MAIN_DBI && !mc->mc_txn->mt_dbxs[mc->mc_dbi].md_dirty) {
+ rc = mdb_search_page(mc->mc_txn, MAIN_DBI, &mc->mc_txn->mt_dbxs[mc->mc_dbi].md_name,
+ NULL, 1, &mpp);
+ if (rc) return rc;
+ mc->mc_txn->mt_dbxs[mc->mc_dbi].md_dirty = 1;
+ }
+ for(i=0; i<mc->mc_snum; i++) {
+ mpp.mp_page = mc->mc_stack[i].mp_page;
+ if (!F_ISSET(mpp.mp_page->mp_flags, P_DIRTY)) {
+ rc = mdb_touch(mc->mc_txn, mc->mc_dbi, &mpp);
+ if (rc) return rc;
+ mc->mc_stack[i].mp_page = mpp.mp_page;
+ if (!i) {
+ mc->mc_txn->mt_dbs[mc->mc_dbi].md_root = mpp.mp_page->mp_pgno;
+ }
+ }
+ dh = ((MDB_dhead *)mpp.mp_page)-1;
+ dh->md_parent = mpp.mp_parent;
+ dh->md_pi = mpp.mp_pi;
+ mpp.mp_parent = mpp.mp_page;
+ mpp.mp_pi = mc->mc_stack[i].mp_ki;
+ }
+ return MDB_SUCCESS;
+}
+
+int
+mdb_cursor_put(MDB_cursor *mc, MDB_val *key, MDB_val *data,
+ unsigned int flags)
+{
+ MDB_ppage *top;
+ MDB_node *leaf;
+ MDB_val xdata, *rdata, dkey;
+ MDB_db dummy;
+ char dbuf[PAGESIZE];
+ int do_sub = 0;
+ size_t nsize;
+ DKBUF;
+ int rc, rc2;
+
+ if (F_ISSET(mc->mc_txn->mt_flags, MDB_TXN_RDONLY))
+ return EACCES;
+
+ DPRINTF("==> put db %u key [%s], size %zu, data size %zu",
+ mc->mc_dbi, DKEY(key), key->mv_size, data->mv_size);
+
+ dkey.mv_size = 0;
+
+ if (flags == MDB_CURRENT) {
+ if (!(mc->mc_flags & C_INITIALIZED))
+ return EINVAL;
+ rc = MDB_SUCCESS;
+ } else if (mc->mc_txn->mt_dbs[mc->mc_dbi].md_root == P_INVALID) {
+ MDB_dpage *dp;
+ /* new database, write a root leaf page */
+ DPUTS("allocating new root leaf page");
+ if ((dp = mdb_new_page(mc->mc_txn, mc->mc_dbi, P_LEAF, 1)) == NULL) {
+ return ENOMEM;
+ }
+ mc->mc_snum = 0;
+ cursor_push_page(mc, &dp->p);
+ mc->mc_txn->mt_dbs[mc->mc_dbi].md_root = dp->p.mp_pgno;
+ mc->mc_txn->mt_dbs[mc->mc_dbi].md_depth++;
+ mc->mc_txn->mt_dbxs[mc->mc_dbi].md_dirty = 1;
+ if ((mc->mc_txn->mt_dbs[mc->mc_dbi].md_flags & (MDB_DUPSORT|MDB_DUPFIXED))
+ == MDB_DUPFIXED)
+ dp->p.mp_flags |= P_LEAF2;
+ mc->mc_flags |= C_INITIALIZED;
+ rc = MDB_NOTFOUND;
+ goto top;
+ } else {
+ int exact = 0;
+ rc = mdb_cursor_set(mc, key, NULL, MDB_SET, &exact);
+ if (flags == MDB_NOOVERWRITE && rc == 0) {
+ DPRINTF("duplicate key [%s]", DKEY(key));
+ return MDB_KEYEXIST;
+ }
+ if (rc && rc != MDB_NOTFOUND)
+ return rc;
+ }
+
+ /* Cursor is positioned, now make sure all pages are writable */
+ rc2 = mdb_cursor_touch(mc);
+ if (rc2) return rc2;
+
+top:
+ top = CURSOR_TOP(mc);
+
+ /* The key already exists */
+ if (rc == MDB_SUCCESS) {
+ /* there's only a key anyway, so this is a no-op */
+ if (IS_LEAF2(top->mp_page)) {
+ int ksize = mc->mc_txn->mt_dbs[mc->mc_dbi].md_pad;
+ if (key->mv_size != ksize)
+ return EINVAL;
+ if (flags == MDB_CURRENT) {
+ char *ptr = LEAF2KEY(top->mp_page, top->mp_ki, ksize);
+ memcpy(ptr, key->mv_data, ksize);
+ }
+ return MDB_SUCCESS;
+ }
+
+ leaf = NODEPTR(top->mp_page, top->mp_ki);
+
+ /* DB has dups? */
+ if (F_ISSET(mc->mc_txn->mt_dbs[mc->mc_dbi].md_flags, MDB_DUPSORT)) {
+ /* Was a single item before, must convert now */
+ if (!F_ISSET(leaf->mn_flags, F_DUPDATA)) {
+ dkey.mv_size = NODEDSZ(leaf);
+ dkey.mv_data = dbuf;
+ memcpy(dbuf, NODEDATA(leaf), dkey.mv_size);
+ /* data matches, ignore it */
+ if (!mdb_dcmp(mc->mc_txn, mc->mc_dbi, data, &dkey))
+ return (flags == MDB_NODUPDATA) ? MDB_KEYEXIST : MDB_SUCCESS;
+ memset(&dummy, 0, sizeof(dummy));
+ if (mc->mc_txn->mt_dbs[mc->mc_dbi].md_flags & MDB_DUPFIXED) {
+ dummy.md_pad = data->mv_size;
+ dummy.md_flags = MDB_DUPFIXED;
+ if (mc->mc_txn->mt_dbs[mc->mc_dbi].md_flags & MDB_INTEGERDUP)
+ dummy.md_flags |= MDB_INTEGERKEY;
+ }
+ dummy.md_root = P_INVALID;
+ if (dkey.mv_size == sizeof(MDB_db)) {
+ memcpy(NODEDATA(leaf), &dummy, sizeof(dummy));
+ goto put_sub;
+ }
+ mdb_del_node(top->mp_page, top->mp_ki, 0);
+ do_sub = 1;
+ rdata = &xdata;
+ xdata.mv_size = sizeof(MDB_db);
+ xdata.mv_data = &dummy;
+ goto new_sub;
+ }
+ goto put_sub;
+ }
+ /* same size, just replace it */
+ if (!F_ISSET(leaf->mn_flags, F_BIGDATA) &&
+ NODEDSZ(leaf) == data->mv_size) {
+ memcpy(NODEDATA(leaf), data->mv_data, data->mv_size);
+ goto done;
+ }
+ mdb_del_node(top->mp_page, top->mp_ki, 0);
+ } else {
+ DPRINTF("inserting key at index %i", top->mp_ki);
+ }
+
+ rdata = data;
+
+new_sub:
+ nsize = IS_LEAF2(top->mp_page) ? key->mv_size : mdb_leaf_size(mc->mc_txn->mt_env, key, rdata);
+ if (SIZELEFT(top->mp_page) < nsize) {
+ rc = mdb_split(mc->mc_txn, mc->mc_dbi, &top->mp_page, &top->mp_ki, key, rdata, P_INVALID);
+ } else {
+ /* There is room already in this leaf page. */
+ rc = mdb_add_node(mc->mc_txn, mc->mc_dbi, top->mp_page, top->mp_ki, key, rdata, 0, 0);
+ }
+
+ if (rc != MDB_SUCCESS)
+ mc->mc_txn->mt_flags |= MDB_TXN_ERROR;
+ else {
+ /* Remember if we just added a subdatabase */
+ if (flags & F_SUBDATA) {
+ leaf = NODEPTR(top->mp_page, top->mp_ki);
+ leaf->mn_flags |= F_SUBDATA;
+ }
+
+ /* Now store the actual data in the child DB. Note that we're
+ * storing the user data in the keys field, so there are strict
+ * size limits on dupdata. The actual data fields of the child
+ * DB are all zero size.
+ */
+ if (do_sub) {
+ leaf = NODEPTR(top->mp_page, top->mp_ki);
+put_sub:
+ if (flags == MDB_CURRENT)
+ mdb_xcursor_init2(mc);
+ else
+ mdb_xcursor_init1(mc, top->mp_page, leaf);
+ xdata.mv_size = 0;
+ xdata.mv_data = "";
+ if (flags == MDB_NODUPDATA)
+ flags = MDB_NOOVERWRITE;
+ /* converted, write the original data first */
+ if (dkey.mv_size) {
+ rc = mdb_cursor_put(&mc->mc_xcursor->mx_cursor, &dkey, &xdata, flags);
+ if (rc) return rc;
+ leaf->mn_flags |= F_DUPDATA;
+ }
+ rc = mdb_cursor_put(&mc->mc_xcursor->mx_cursor, data, &xdata, flags);
+ mdb_xcursor_fini(mc);
+ memcpy(NODEDATA(leaf),
+ &mc->mc_xcursor->mx_txn.mt_dbs[mc->mc_xcursor->mx_cursor.mc_dbi],
+ sizeof(MDB_db));
+ }
+ mc->mc_txn->mt_dbs[mc->mc_dbi].md_entries++;
+ }
+done:
+ return rc;
+}
+
+int
+mdb_cursor_del(MDB_cursor *mc, unsigned int flags)
+{
+ MDB_pageparent mpp;
+ MDB_ppage *top;
+ MDB_node *leaf;
+ int rc;
+
+ if (F_ISSET(mc->mc_txn->mt_flags, MDB_TXN_RDONLY))
+ return EACCES;
+
+ if (!mc->mc_flags & C_INITIALIZED)
+ return EINVAL;
+
+ rc = mdb_cursor_touch(mc);
+ if (rc) return rc;
+
+ top = CURSOR_TOP(mc);
+ leaf = NODEPTR(top->mp_page, top->mp_ki);
+ mpp.mp_page = top->mp_page;
+ if (mc->mc_snum > 1) {
+ MDB_ppage *parent = CURSOR_PARENT(mc);
+ mpp.mp_parent = parent->mp_page;
+ mpp.mp_pi = parent->mp_ki;
+ } else {
+ mpp.mp_parent = NULL;
+ mpp.mp_pi = 0;
+ }
+
+ if (!IS_LEAF2(top->mp_page) && F_ISSET(leaf->mn_flags, F_DUPDATA)) {
+ MDB_pageparent mp2;
+
+ if (flags != MDB_NODUPDATA) {
+/* mdb_xcursor_init2(mc); */
+ rc = mdb_cursor_del(&mc->mc_xcursor->mx_cursor, 0);
+ mdb_xcursor_fini(mc);
+ /* If sub-DB still has entries, we're done */
+ if (mc->mc_xcursor->mx_txn.mt_dbs[mc->mc_xcursor->mx_cursor.mc_dbi].md_root
+ != P_INVALID) {
+ memcpy(NODEDATA(leaf),
+ &mc->mc_xcursor->mx_txn.mt_dbs[mc->mc_xcursor->mx_cursor.mc_dbi],
+ sizeof(MDB_db));
+ mc->mc_txn->mt_dbs[mc->mc_dbi].md_entries--;
+ return rc;
+ }
+ /* otherwise fall thru and delete the sub-DB */
+ }
+
+ /* add all the child DB's pages to the free list */
+ mc->mc_xcursor->mx_cursor.mc_snum = 0;
+ rc = mdb_search_page(&mc->mc_xcursor->mx_txn, mc->mc_xcursor->mx_cursor.mc_dbi,
+ NULL, &mc->mc_xcursor->mx_cursor, 0, &mp2);
+ if (rc == MDB_SUCCESS) {
+ MDB_ppage *top, *parent;
+ MDB_dpage *dp;
+ ID2 mid;
+ MDB_node *ni;
+ unsigned int i;
+ int dirty_root = 0;
+
+ mc->mc_txn->mt_dbs[mc->mc_dbi].md_entries -=
+ mc->mc_xcursor->mx_txn.mt_dbs[mc->mc_xcursor->mx_cursor.mc_dbi].md_entries;
+
+ cursor_pop_page(&mc->mc_xcursor->mx_cursor);
+ if (mc->mc_xcursor->mx_cursor.mc_snum) {
+#if 0
+ if (mc->mc_xcursor->mx_cursor.mc_stack[0].mp_page->mp_flags & P_DIRTY) {
+ dirty_root = 1;
+ }
+#endif
+ while (mc->mc_xcursor->mx_cursor.mc_snum > 1) {
+ top = CURSOR_TOP(&mc->mc_xcursor->mx_cursor);
+ parent = CURSOR_PARENT(&mc->mc_xcursor->mx_cursor);
+ for (i=0; i<NUMKEYS(top->mp_page); i++) {
+ MDB_page *mp;
+ pgno_t pg;
+ ni = NODEPTR(top->mp_page, i);
+ pg = NODEPGNO(ni);
+ if ((rc = mdb_get_page(mc->mc_txn, pg, &mp)))
+ return rc;
+#if 0
+ if (mp->mp_flags & P_DIRTY) {
+ /* drop it */
+ mid.mid = pg;
+ mdb_mid2l_delete(mc->mc_txn->mt_u.dirty_list, &mid);
+ dp = mid.mptr;
+ dp->h.md_parent = (MDB_page *)mc->mc_txn->mt_env->me_dpages;
+ mc->mc_txn->mt_env->me_dpages = dp;
+ } else
+#endif
+ {
+ /* free it */
+ mdb_midl_insert(mc->mc_txn->mt_free_pgs, pg);
+ }
+ }
+ rc = mdb_sibling(&mc->mc_xcursor->mx_cursor, 1);
+ if (rc) break;
+ }
+ }
+#if 0
+ if (dirty_root) {
+ /* drop it */
+ mid.mid = mc->mc_xcursor->mx_txn.mt_dbs[mc->mc_xcursor->mx_cursor.mc_dbi].md_root;
+ mdb_mid2l_delete(mc->mc_txn->mt_u.dirty_list, &mid);
+ dp = mid.mptr;
+ dp->h.md_parent = (MDB_page *)mc->mc_txn->mt_env->me_dpages;
+ mc->mc_txn->mt_env->me_dpages = dp;
+ } else
+#endif
+ {
+ /* free it */
+ mdb_midl_insert(mc->mc_txn->mt_free_pgs,
+ mc->mc_xcursor->mx_txn.mt_dbs[mc->mc_xcursor->mx_cursor.mc_dbi].md_root);
+ }
+ }
+ }
+
+ return mdb_del0(mc, top->mp_ki, &mpp, leaf);
+}
+