int db_cursor_seekr(MDB_cursor *const cursor, DB_range const *const range, MDB_val *const key, MDB_val *const data, int const dir) { int rc = mdb_cursor_seek(cursor, key, data, dir); if(MDB_SUCCESS != rc) return rc; MDB_val const *const limit = dir < 0 ? range->min : range->max; int x = mdb_cmp(mdb_cursor_txn(cursor), key, limit); if(x * dir < 0) return MDB_SUCCESS; mdb_cursor_renew(mdb_cursor_txn(cursor), cursor); return MDB_NOTFOUND; }
void hashidx_stream_delete(HashIdxStream *st) { MDB_txn *txn = mdb_cursor_txn(st->cur); mdb_cursor_close(st->cur); txn_manager_abort(st->db->txn_manager, txn); free(st); }
int db_cursor_firstr(MDB_cursor *const cursor, DB_range const *const range, MDB_val *const key, MDB_val *const data, int const dir) { if(!cursor) return EINVAL; if(0 == dir) return EINVAL; MDB_val const *const first = dir < 0 ? range->min : range->max; *key = first; int rc = mdb_cursor_seek(cursor, key, data, dir); if(MDB_SUCCESS != rc) return rc; int x = mdb_cmp(mdb_cursor_txn(cursor), first, key); if(0 == x) rc = mdb_cursor_next(cursor, key, data, dir); if(MDB_SUCCESS != rc) return rc; MDB_val const *const last = dir < 0 ? range->max : range->min; x = mdb_cmp(mdb_cursor_txn(cursor), key, last); if(x * dir < 0) return MDB_SUCCESS; mdb_cursor_renew(mdb_cursor_txn(cursor), cursor); return MDB_NOTFOUND; }
int db_cursor_first_pfx(MDB_cursor *const cursor, MDB_val const *const pfx, MDB_val *const key, MDB_val *const data) { *key = *pfx; int rc = mdb_cursor_seek(cursor, key, data, +1); rc = db_pfx(rc, pfx, key); if(MDB_SUCCESS == rc) return MDB_SUCCESS; mdb_cursor_renew(mdb_cursor_txn(cursor), cursor); return MDB_NOTFOUND; }
void page_db_link_stream_delete(PageDBLinkStream *es) { if (es) { if (es->cur) { txn_manager_abort(es->db->txn_manager, mdb_cursor_txn(es->cur)); mdb_cursor_close(es->cur); } free(es->to); free(es); } }
int db_cursor_seek(MDB_cursor *const cursor, MDB_val *const key, MDB_val *const data, int const dir) { if(!key) return EINVAL; MDB_val const orig = *key; MDB_cursor_op const op = 0 == dir ? MDB_SET : MDB_SET_RANGE; int rc = mdb_cursor_get(cursor, key, data, op); if(dir >= 0) return rc; if(MDB_SUCCESS == rc) { MDB_txn *const txn = mdb_cursor_txn(cursor); MDB_dbi const dbi = mdb_cursor_dbi(cursor); if(0 == mdb_cmp(txn, dbi, &orig, key)) return rc; } else if(MDB_NOTFOUND != rc) return rc; return mdb_cursor_get(cursor, key, data, MDB_PREV); }
FreqSchedulerError freq_scheduler_cursor_commit(FreqScheduler *sch, MDB_cursor *cursor) { MDB_txn *txn = mdb_cursor_txn(cursor); if (txn_manager_commit(sch->txn_manager, txn) != 0) { if (txn != 0) txn_manager_abort(sch->txn_manager, txn); freq_scheduler_set_error(sch, freq_scheduler_error_internal, __func__); freq_scheduler_add_error(sch, "commiting schedule transaction"); freq_scheduler_add_error(sch, sch->txn_manager->error->message); } return sch->error->code; }
static void slmdb_cursor_close(SLMDB *slmdb) { MDB_txn *txn; /* * Close the cursor and its read transaction. We can restore it later * from the saved key information. */ txn = mdb_cursor_txn(slmdb->cursor); mdb_cursor_close(slmdb->cursor); slmdb->cursor = 0; mdb_txn_abort(txn); }
StreamState page_db_link_stream_reset(void *st) { PageDBLinkStream *es = st; // if no cursor, try to create one if (es->cur) { txn_manager_abort(es->db->txn_manager, mdb_cursor_txn(es->cur)); mdb_cursor_close(es->cur); es->cur = 0; } if (page_db_link_stream_open_cursor(es) != 0) { return stream_state_error; } return es->state; }
/* * Class: jmdb_DatabaseWrapper * Method: cursorTxn * Signature: (J)J */ JNIEXPORT jlong JNICALL Java_jmdb_DatabaseWrapper_cursorTxn(JNIEnv *vm, jclass clazz, jlong cursorL) { MDB_cursor *cursorC = (MDB_cursor*) cursorL; MDB_txn *txn = mdb_cursor_txn(cursorC); return (jlong) txn; }
void freq_scheduler_cursor_abort(FreqScheduler *sch, MDB_cursor *cursor) { if (cursor) { txn_manager_abort(sch->txn_manager, mdb_cursor_txn(cursor)); } }
static int mdb_dn2id_upgrade( BackendDB *be ) { struct mdb_info *mi = (struct mdb_info *) be->be_private; MDB_txn *mt; MDB_cursor *mc = NULL; MDB_val key, data; char *ptr; int rc, writes=0, depth=0; int enable_meter = 0; ID id = 0, *num, count = 0; rec *stack; lutil_meter_t meter; if (!(mi->mi_flags & MDB_NEED_UPGRADE)) { Debug( LDAP_DEBUG_ANY, "database %s: No upgrade needed.\n", be->be_suffix[0].bv_val, 0, 0 ); return 0; } { MDB_stat st; mdb_stat(mdb_cursor_txn(cursor), mi->mi_dbis[MDB_ID2ENTRY], &st); if (!st.ms_entries) { /* Empty DB, nothing to upgrade? */ return 0; } if (isatty(2)) enable_meter = !lutil_meter_open(&meter, &lutil_meter_text_display, &lutil_meter_linear_estimator, st.ms_entries); } num = ch_malloc(STACKSIZ * (sizeof(ID) + sizeof(rec))); stack = (rec *)(num + STACKSIZ); rc = mdb_txn_begin(mi->mi_dbenv, NULL, 0, &mt); if (rc) { Debug(LDAP_DEBUG_ANY, "mdb_dn2id_upgrade: mdb_txn_begin failed, %s (%d)\n", mdb_strerror(rc), rc, 0 ); goto leave; } rc = mdb_cursor_open(mt, mi->mi_dbis[MDB_DN2ID], &mc); if (rc) { Debug(LDAP_DEBUG_ANY, "mdb_dn2id_upgrade: mdb_cursor_open failed, %s (%d)\n", mdb_strerror(rc), rc, 0 ); goto leave; } key.mv_size = sizeof(ID); /* post-order depth-first update */ for(;;) { size_t dkids; unsigned char *ptr; /* visit */ key.mv_data = &id; stack[depth].id = id; rc = mdb_cursor_get(mc, &key, &data, MDB_SET); if (rc) { Debug(LDAP_DEBUG_ANY, "mdb_dn2id_upgrade: mdb_cursor_get failed, %s (%d)\n", mdb_strerror(rc), rc, 0 ); goto leave; } num[depth] = 1; rc = mdb_cursor_count(mc, &dkids); if (rc) { Debug(LDAP_DEBUG_ANY, "mdb_dn2id_upgrade: mdb_cursor_count failed, %s (%d)\n", mdb_strerror(rc), rc, 0 ); goto leave; } if (dkids > 1) { rc = mdb_cursor_get(mc, &key, &data, MDB_NEXT_DUP); down: ptr = (unsigned char *)data.mv_data + data.mv_size - sizeof(ID); memcpy(&id, ptr, sizeof(ID)); depth++; memcpy(stack[depth].rdn, data.mv_data, data.mv_size); stack[depth].len = data.mv_size; continue; } /* pop: write updated count, advance to next node */ pop: /* update superior counts */ if (depth) num[depth-1] += num[depth]; key.mv_data = &id; id = stack[depth-1].id; data.mv_data = stack[depth].rdn; data.mv_size = stack[depth].len; rc = mdb_cursor_get(mc, &key, &data, MDB_GET_BOTH); if (rc) { Debug(LDAP_DEBUG_ANY, "mdb_dn2id_upgrade: mdb_cursor_get(BOTH) failed, %s (%d)\n", mdb_strerror(rc), rc, 0 ); goto leave; } data.mv_data = stack[depth].rdn; ptr = (unsigned char *)data.mv_data + data.mv_size; memcpy(ptr, &num[depth], sizeof(ID)); data.mv_size += sizeof(ID); rc = mdb_cursor_del(mc, 0); if (rc) { Debug(LDAP_DEBUG_ANY, "mdb_dn2id_upgrade: mdb_cursor_del failed, %s (%d)\n", mdb_strerror(rc), rc, 0 ); goto leave; } rc = mdb_cursor_put(mc, &key, &data, 0); if (rc) { Debug(LDAP_DEBUG_ANY, "mdb_dn2id_upgrade: mdb_cursor_put failed, %s (%d)\n", mdb_strerror(rc), rc, 0 ); goto leave; } count++; #if 1 if (enable_meter) lutil_meter_update(&meter, count, 0); #else { int len; ptr = data.mv_data; len = (ptr[0] & 0x7f) << 8 | ptr[1]; printf("ID: %zu, %zu, %.*s\n", stack[depth].id, num[depth], len, ptr+2); } #endif writes++; if (writes == 1000) { mdb_cursor_close(mc); rc = mdb_txn_commit(mt); if (rc) { Debug(LDAP_DEBUG_ANY, "mdb_dn2id_upgrade: mdb_txn_commit failed, %s (%d)\n", mdb_strerror(rc), rc, 0 ); goto leave; } rc = mdb_txn_begin(mi->mi_dbenv, NULL, 0, &mt); if (rc) { Debug(LDAP_DEBUG_ANY, "mdb_dn2id_upgrade: mdb_txn_begin(2) failed, %s (%d)\n", mdb_strerror(rc), rc, 0 ); goto leave; } rc = mdb_cursor_open(mt, mi->mi_dbis[MDB_DN2ID], &mc); if (rc) { Debug(LDAP_DEBUG_ANY, "mdb_dn2id_upgrade: mdb_cursor_open(2) failed, %s (%d)\n", mdb_strerror(rc), rc, 0 ); goto leave; } rc = mdb_cursor_get(mc, &key, &data, MDB_GET_BOTH); if (rc) { Debug(LDAP_DEBUG_ANY, "mdb_dn2id_upgrade: mdb_cursor_get(2) failed, %s (%d)\n", mdb_strerror(rc), rc, 0 ); goto leave; } writes = 0; } depth--; rc = mdb_cursor_get(mc, &key, &data, MDB_NEXT_DUP); if (rc == 0) goto down; rc = 0; if (depth) goto pop; else break; } leave: mdb_cursor_close(mc); if (mt) { int r2; r2 = mdb_txn_commit(mt); if (r2) { Debug(LDAP_DEBUG_ANY, "mdb_dn2id_upgrade: mdb_txn_commit(2) failed, %s (%d)\n", mdb_strerror(r2), r2, 0 ); if (!rc) rc = r2; } } ch_free(num); if (enable_meter) { lutil_meter_update(&meter, count, 1); lutil_meter_close(&meter); } return rc; }