Example #1
0
/*
 * Initialize ENTRYID and USN sequence.
 */
static
DWORD
MDBInitSequence(
    PVDIR_MDB_DB pDB,
    VDIR_DB      mdbDbi
    )
{
    DWORD           dwError = 0;
    PVDIR_DB_TXN    pTxn = NULL;
    VDIR_DB_DBT     key = {0};
    VDIR_DB_DBT     value = {0};
    unsigned char   EidBytes[sizeof( ENTRYID )] = {0};
    ENTRYID         initEIDValue = ENTRY_ID_SEQ_INITIAL_VALUE;
    ENTRYID         initUNSValue = USN_SEQ_INITIAL_VALUE;

    assert(pDB);

    dwError = mdb_txn_begin( pDB->mdbEnv, NULL, BE_DB_FLAGS_ZERO, &pTxn );
    BAIL_ON_VMDIR_ERROR(dwError);

    key.mv_data = &EidBytes[0];
    MDBEntryIdToDBT(BE_MDB_ENTRYID_SEQ_KEY, &key);

    dwError =  mdb_get(pTxn, mdbDbi, &key, &value);
    if (dwError == MDB_NOTFOUND)
    {
        // first time, initialize two sequence records
        value.mv_data = &initEIDValue;
        value.mv_size = sizeof(initEIDValue);

        // set entryid sequence record
        dwError = mdb_put(pTxn, mdbDbi, &key, &value,  MDB_NOOVERWRITE);
        BAIL_ON_VMDIR_ERROR(dwError);

        MDBEntryIdToDBT(BE_MDB_USN_SEQ_KEY, &key);
        value.mv_data = &initUNSValue;
        value.mv_size = sizeof(initUNSValue);

        // set usn sequence record
        dwError = mdb_put(pTxn, mdbDbi, &key, &value, MDB_NOOVERWRITE);
        BAIL_ON_VMDIR_ERROR(dwError);
    }

    dwError = mdb_txn_commit(pTxn);
    pTxn = NULL;
    BAIL_ON_VMDIR_ERROR(dwError);

cleanup:

    return dwError;

error:

    if (pTxn)
    {
        mdb_txn_abort(pTxn);
    }

    goto cleanup;
}
Example #2
0
int CDbxMdb::Load(bool bSkipInit)
{
	if (!Remap())
		return EGROKPRF_CANTREAD;

	if (!bSkipInit) {
		txn_ptr trnlck(m_pMdbEnv);
		mdb_open(trnlck, "global", MDB_CREATE | MDB_INTEGERKEY, &m_dbGlobal);
		mdb_open(trnlck, "contacts", MDB_CREATE | MDB_INTEGERKEY, &m_dbContacts);
		mdb_open(trnlck, "modules", MDB_CREATE | MDB_INTEGERKEY, &m_dbModules);
		mdb_open(trnlck, "events", MDB_CREATE | MDB_INTEGERKEY, &m_dbEvents);
		mdb_open(trnlck, "eventsrt", MDB_CREATE | MDB_INTEGERKEY, &m_dbEventsSort);
		mdb_open(trnlck, "settings", MDB_CREATE, &m_dbSettings);

		DWORD keyVal = 1;
		MDB_val key = { sizeof(DWORD), &keyVal }, data;
		if (mdb_get(trnlck, m_dbGlobal, &key, &data) == MDB_SUCCESS) {
			DBHeader *hdr = (DBHeader*)data.mv_data;
			if (hdr->dwSignature != DBHEADER_SIGNATURE)
				DatabaseCorruption(NULL);

			memcpy(&m_header, data.mv_data, sizeof(m_header));
		}
		else {
			m_header.dwSignature = DBHEADER_SIGNATURE;
			m_header.dwVersion = 1;
			data.mv_data = &m_header; data.mv_size = sizeof(m_header);
			mdb_put(trnlck, m_dbGlobal, &key, &data, 0);

			keyVal = 0;
			DBContact dbc = { DBCONTACT_SIGNATURE, 0, 0, 0 };
			data.mv_data = &dbc; data.mv_size = sizeof(dbc);
			mdb_put(trnlck, m_dbContacts, &key, &data, 0);
		}
		trnlck.commit();

		if (InitModuleNames()) return EGROKPRF_CANTREAD;
		if (InitCrypt())       return EGROKPRF_CANTREAD;

		// everything is ok, go on
		if (!m_bReadOnly) {
			// we don't need events in the service mode
			if (ServiceExists(MS_DB_SETSAFETYMODE)) {
				hContactDeletedEvent = CreateHookableEvent(ME_DB_CONTACT_DELETED);
				hContactAddedEvent = CreateHookableEvent(ME_DB_CONTACT_ADDED);
				hSettingChangeEvent = CreateHookableEvent(ME_DB_CONTACT_SETTINGCHANGED);
				hEventMarkedRead = CreateHookableEvent(ME_DB_EVENT_MARKED_READ);

				hEventAddedEvent = CreateHookableEvent(ME_DB_EVENT_ADDED);
				hEventDeletedEvent = CreateHookableEvent(ME_DB_EVENT_DELETED);
				hEventFilterAddedEvent = CreateHookableEvent(ME_DB_EVENT_FILTER_ADD);
			}
		}

		FillContacts();
	}

	return ERROR_SUCCESS;
}
int db_dbi_open(MDB_txn *const txn, DB_schema *const schema, unsigned int opts, DB_column const *const cols, count_t const ncols, strarg_t const name, MDB_dbi *const dbi) {
	int rc;

	uint64_t const dbname_id = db_string_id(txn, schema, name);
	if(!dbname_id) return -1;

	DB_VAL(dbinfo_val, 2);
	db_bind(dbinfo_val, 0);
	db_bind(dbinfo_val, dbname_id);
	DB_VAL(info_val, 1);
	db_bind(info_val, 0xff & opts);
	mdb_put(txn, schema->schema, dbinfo_val, info_val, MDB_NOOVERWRITE);
	// TODO: Check opts

	MDB_cursor *cur = NULL;
	mdb_cursor_open(txn, schema->schema, &cur);

	DB_VAL(dbcols_val, 2);
	db_bind(dbcols_val, 1);
	db_bind(dbcols_val, dbname_id);
	MDB_val col_val;
	mdb_cursor_get(cur, &dbcols_val, &col_val, MDB_GET);
	for(; MDB_SUCCESS == rc; rc = mdb_cursor_get(cur, &dbcols_val, &col_val, MDB_NEXT_DUP)) {
		uint64_t const col = db_column(col_val, 0);
		uint64_t const type = db_column(col_val, 1);
		strarg_t const colname = db_column_text(txn, schema, col_val, 2);

		if(col >= ncols) break; // Extra columns are not an error.
		if(type != cols[i].type || 0 != strcmp(colname, cols[i].name)) {
			mdb_cursor_close(cur); cur = NULL;
			return -1;
		}
	}

	mdb_cursor_close(cur); cur = NULL;

	for(index_t i = 0; i < ncols; ++i) {
		uint64_t const colname_id = db_string_id(txn, schema, cols[i].name);
		if(!colname_id) return -1;

		DB_VAL(col_val, 3);
		db_bind(col_val, i);
		db_bind(col_val, cols[i].type);
		db_bind(col_val, colname_id);
		rc = mdb_put(txn, schema->schema, dbcols_val, col_val, MDB_NODUPDATA);
		if(MDB_SUCCESS != rc && MDB_KEYEXIST != rc) return -1;
	}

	mdb_dbi_open(txn, name, MDB_CREATE | opts, dbi);
	return 0;
}
Example #4
0
    void storeBatch (Batch const& batch)
    {
        MDB_txn* txn = nullptr;

        int error = 0;

        error = mdb_txn_begin (m_env, NULL, 0, &txn);

        if (error == 0)
        {
            EncodedBlob::Pool::ScopedItem item (m_blobPool);

            BOOST_FOREACH (NodeObject::Ptr const& object, batch)
            {
                EncodedBlob& encoded (item.getObject ());

                encoded.prepare (object);

                MDB_val key;
                key.mv_size = m_keyBytes;
                key.mv_data = mdb_cast (encoded.getKey ());

                MDB_val data;
                data.mv_size = encoded.getSize ();
                data.mv_data = mdb_cast (encoded.getData ());

                error = mdb_put (txn, m_dbi, &key, &data, 0);

                if (error != 0)
                {
                    WriteLog (lsWARNING, NodeObject) << "mdb_put failed, error=" << error;
                    break;
                }
            }
    void write(const std::string& keystr, const std::string& value) {
        if (use_leveldb) {  // leveldb
            batch->Put(keystr, value);
        } else {  // lmdb
            mdb_data.mv_size = value.size();
            mdb_data.mv_data = const_cast<char*>(&value[0]);
            mdb_key.mv_size = keystr.size();
            mdb_key.mv_data = const_cast<char*>(&keystr[0]);
            CHECK_EQ(mdb_put(mdb_txn, mdb_dbi, &mdb_key, &mdb_data, 0), MDB_SUCCESS)
            << "mdb_put failed";
        }

        if (++count % 1000 == 0) {
            // Commit txn
            if (use_leveldb) {  // leveldb
                db->Write(leveldb::WriteOptions(), batch);
                delete batch;
                batch = new leveldb::WriteBatch();
            } else {  // lmdb
                CHECK_EQ(mdb_txn_commit(mdb_txn), MDB_SUCCESS)
                << "mdb_txn_commit failed";
                CHECK_EQ(mdb_txn_begin(mdb_env, NULL, 0, &mdb_txn), MDB_SUCCESS)
                << "mdb_txn_begin failed";
            }
            LOG(ERROR) << "Processed " << count << " files.";
        }
    }
Example #6
0
 int LMDBEngine::Put(const Slice& key, const Slice& value)
 {
     LMDBContext& holder = m_ctx_local.GetValue();
     MDB_txn *txn = holder.readonly_txn;
     if (NULL == txn || holder.batch_write == 0)
     {
         MDB_val k, v;
         k.mv_data = const_cast<char*>(key.data());
         k.mv_size = key.size();
         v.mv_data = const_cast<char*>(value.data());
         v.mv_size = value.size();
         mdb_txn_begin(m_env, NULL, 0, &txn);
         mdb_put(txn, m_dbi, &k, &v, 0);
         mdb_txn_commit(txn);
         return 0;
     }
     PutOperation* op = new PutOperation;
     op->key.assign((const char*) key.data(), key.size());
     op->value.assign((const char*) value.data(), value.size());
     m_write_queue.Push(op);
     if (holder.batch_write == 0)
     {
         CheckPointOperation* ck = new CheckPointOperation(holder.cond);
         m_write_queue.Push(ck);
         NotifyBackgroundThread();
         ck->Wait();
         DELETE(ck);
         return 0;
     }
     return 0;
 }
Example #7
0
bool LmdbDataset<K, V, KCoder, VCoder>::put(const K& key, const V& value) {
  DLOG(INFO) << "LMDB: Put";

  vector<char> serialized_key;
  if (!KCoder::serialize(key, &serialized_key)) {
    LOG(ERROR) << "failed to serialize key";
    return false;
  }

  vector<char> serialized_value;
  if (!VCoder::serialize(value, &serialized_value)) {
    LOG(ERROR) << "failed to serialized value";
    return false;
  }

  MDB_val mdbkey, mdbdata;
  mdbdata.mv_size = serialized_value.size();
  mdbdata.mv_data = serialized_value.data();
  mdbkey.mv_size = serialized_key.size();
  mdbkey.mv_data = serialized_key.data();

  CHECK_NOTNULL(write_txn_);
  CHECK_NE(0, dbi_);

  int retval = mdb_put(write_txn_, dbi_, &mdbkey, &mdbdata, 0);
  if (MDB_SUCCESS != retval) {
    LOG(ERROR) << "mdb_put failed " << mdb_strerror(retval);
    return false;
  }

  return true;
}
Example #8
0
int mail_cache_db_put(struct mail_cache_db * cache_db,
    const void * key, size_t key_len, const void * value, size_t value_len)
{
  int r;
  MDB_env *env;
  MDB_txn *txn;
  MDB_dbi dbi;
  MDB_val mdb_key;
  MDB_val mdb_val;

  env = cache_db->internal_database;

  mdb_key.mv_size = key_len;
  mdb_key.mv_data = (void *) key;
  mdb_val.mv_size = value_len;
  mdb_val.mv_data = (void *) value;

  r = mdb_txn_begin(env, NULL, 0, &txn);
  if (r != 0)
    return -1;
  r = mdb_dbi_open(txn, NULL, 0, &dbi);
  if (r != 0)
    goto error;

  r = mdb_put(txn, dbi, &mdb_key, &mdb_val, 0);
  if (r != 0)
    goto error;

  mdb_txn_commit(txn);
  return 0;

  error:
    mdb_txn_abort(txn);
    return -1;
}
Example #9
0
common::Error LmdbRaw::put(const std::string& key, const std::string& value) {
  MDB_val mkey;
  mkey.mv_size = key.size();
  mkey.mv_data = (void*)key.c_str();
  MDB_val mval;
  mval.mv_size = value.size();
  mval.mv_data = (void*)value.c_str();

  MDB_txn *txn = NULL;
  int rc = mdb_txn_begin(lmdb_->env, NULL, 0, &txn);
  if (rc == LMDB_OK) {
    rc = mdb_put(txn, lmdb_->dbir, &mkey, &mval, 0);
    if (rc == LMDB_OK) {
        rc = mdb_txn_commit(txn);
    } else {
        mdb_txn_abort(txn);
    }
  }

  if (rc != LMDB_OK) {
    char buff[1024] = {0};
    common::SNPrintf(buff, sizeof(buff), "put function error: %s", mdb_strerror(rc));
    return common::make_error_value(buff, common::ErrorValue::E_ERROR);
  }

  return common::Error();
}
Example #10
0
/**
 * This function works in stages. First we clean up the given key to prevent
 * any trickery by users. Then we retrieve the existing value, increment it,
 * and store it back in the db.
 */
uint64_t counter_inc(counter_t *lc, const char *key) {
    char clean_key[KEYSZ] = { 0 };
    key_clean(clean_key, key);
    clean_key[15] = '\0';

    MDB_val mkey, data, update;
    MDB_txn *txn = NULL;

    mkey.mv_size = (KEYSZ) * sizeof(char);
    mkey.mv_data = (void *)clean_key;

    mdb_txn_begin(lc->env, NULL, 0, &txn);
    uint64_t stored_counter = 0;
    // We only care about success here, since if mdb_get fails it is because
    // the key doesn't exist and we need to make it.
    if(mdb_get(txn, *lc->dbi, &mkey, &data) == MDB_SUCCESS) {
        stored_counter = *(uint64_t *)data.mv_data;
    }
    stored_counter++;
    update.mv_size = sizeof(uint64_t);
    update.mv_data = (void *)&stored_counter;
    if(mdb_put(txn, *lc->dbi, &mkey, &update, 0) != MDB_SUCCESS) {
        perror("mdb_put");
        mdb_txn_abort(txn);
        return 0;
    }

    MDB_CHECK(mdb_txn_commit(txn), MDB_SUCCESS, 0);
    return stored_counter;
}
Example #11
0
int kvdb_lmdb_put(kvdb_t *kvdb, const char *key, uint32_t klen, void *value, uint32_t vlen)
{
    kvdb_lmdb_t *lmdb = (kvdb_lmdb_t*)kvdb;
    kvenv_lmdb_t *kvenv_lmdb = (kvenv_lmdb_t*)kvdb->kvenv;

    MDB_val m_val;
    MDB_val m_key;
    MDB_txn *txn;

    m_val.mv_size = vlen; 
    m_val.mv_data = value;
    m_key.mv_size = klen; 
    m_key.mv_data = (void*)key;

    int rc = mdb_txn_begin(kvenv_lmdb->env, NULL, 0, &txn);
    if( rc==0 ){
        rc = mdb_put(txn, lmdb->dbi, &m_key, &m_val, 0);
        if( rc==0 ){
            rc = mdb_txn_commit(txn);
        }else{
            error_log("kvdb_lmdb_put() failure. error: %s", mdb_strerror(rc));
            mdb_txn_abort(txn);
        }
    }

    return rc;
}
Example #12
0
int mdb_ad_get( struct mdb_info *mdb, MDB_txn *txn, AttributeDescription *ad )
{
	int i, rc;
	MDB_val key, val;

	rc = mdb_ad_read( mdb, txn );
	if (rc)
		return rc;

	if ( mdb->mi_adxs[ad->ad_index] )
		return 0;

	i = mdb->mi_numads+1;
	key.mv_size = sizeof(int);
	key.mv_data = &i;
	val.mv_size = ad->ad_cname.bv_len;
	val.mv_data = ad->ad_cname.bv_val;

	rc = mdb_put( txn, mdb->mi_ad2id, &key, &val, 0 );
	if ( rc == MDB_SUCCESS ) {
		mdb->mi_adxs[ad->ad_index] = i;
		mdb->mi_ads[i] = ad;
		mdb->mi_numads++;
	} else {
		Debug( LDAP_DEBUG_ANY,
			"mdb_ad_get: mdb_put failed %s(%d)\n",
			mdb_strerror(rc), rc, 0);
	}

	return rc;
}
Example #13
0
	int LMDBEngine::FlushWriteBatch(BatchHolder& holder)
	{
		if (NULL != holder.txn)
		{
			StringSet::iterator it = holder.dels.begin();
			while (it != holder.dels.end())
			{
				MDB_val k;
				k.mv_data = (void*) (it->c_str());
				k.mv_size = it->size();
				mdb_del(holder.txn, m_dbi, &k, NULL);
				it++;
			}
			std::map<std::string, std::string>::iterator sit =
			        holder.inserts.begin();
			while (sit != holder.inserts.end())
			{
				MDB_val k, v;
				k.mv_data = (void*) (sit->first.c_str());
				k.mv_size = sit->first.size();
				v.mv_data = (void*) (sit->second.c_str());
				v.mv_size = sit->second.size();
				mdb_put(holder.txn, m_dbi, &k, &v, 0);
				sit++;
			}
			mdb_txn_commit(holder.txn);
			holder.txn = NULL;
			holder.Clear();
		}
		return 0;
	}
Example #14
0
int     slmdb_put(SLMDB *slmdb, MDB_val *mdb_key,
		          MDB_val *mdb_value, int flags)
{
    MDB_txn *txn;
    int     status;

    /*
     * Start a write transaction if there's no bulk-mode txn.
     */
    if (slmdb->txn)
	txn = slmdb->txn;
    else if ((status = slmdb_txn_begin(slmdb, 0, &txn)) != 0)
	SLMDB_API_RETURN(slmdb, status);

    /*
     * Do the update.
     */
    if ((status = mdb_put(txn, slmdb->dbi, mdb_key, mdb_value, flags)) != 0) {
	mdb_txn_abort(txn);
	if (status != MDB_KEYEXIST) {
	    if ((status = slmdb_recover(slmdb, status)) == 0)
		status = slmdb_put(slmdb, mdb_key, mdb_value, flags);
	    SLMDB_API_RETURN(slmdb, status);
	}
    }

    /*
     * Commit the transaction if it's not the bulk-mode txn.
     */
    if (status == 0 && slmdb->txn == 0 && (status = mdb_txn_commit(txn)) != 0
	&& (status = slmdb_recover(slmdb, status)) == 0)
	status = slmdb_put(slmdb, mdb_key, mdb_value, flags);

    SLMDB_API_RETURN(slmdb, status);
}
Example #15
0
void LMDBTransaction::Put(const string& key, const string& value) {
  MDB_val mdb_key, mdb_value;
  mdb_key.mv_data = const_cast<char*>(key.data());
  mdb_key.mv_size = key.size();
  mdb_value.mv_data = const_cast<char*>(value.data());
  mdb_value.mv_size = value.size();
  MDB_CHECK(mdb_put(mdb_txn_, *mdb_dbi_, &mdb_key, &mdb_value, 0));
}
Example #16
0
void put(const char *k)
{
    keyv.mv_size = strlen(k);
    keyv.mv_data = k;
    valv.mv_size = 0;
    valv.mv_data = "";
    check(mdb_put(txn, dbi, &keyv, &valv, 0));
}
Example #17
0
int counter_gen_stats(void *tdata) {
    counter_t *lc = (counter_t *)tdata;
    MDB_val key, data;
    MDB_val stat_key, stat_data;
    MDB_txn *txn = NULL;
    MDB_cursor *cursor = NULL;
    mdb_txn_begin(lc->env, NULL, 0, &txn);
    mdb_cursor_open(txn, *lc->dbi, &cursor);
    int rc = 0;
    char *tmp_str = NULL;
    uint64_t last_counter = 0;
    uint64_t reqs_per_sec = 0;
    int size = 0;
    while((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) {
        if((*(char *)key.mv_data) == '_') {
            continue;
        }
        size = asprintf(&tmp_str, "_%s_last", (char *)key.mv_data);
        stat_key.mv_size = size;
        stat_key.mv_data = tmp_str;
        last_counter = 0;
        if(mdb_get(txn, *lc->dbi, &stat_key, &stat_data) == MDB_SUCCESS) {
            last_counter = *(uint64_t *)stat_data.mv_data;
        }
        // runs every STATS_SECS secs
        reqs_per_sec = (*(uint64_t *)data.mv_data - last_counter) / STATS_SECS;
        stat_data.mv_size = sizeof(uint64_t);
        stat_data.mv_data = data.mv_data;
        mdb_put(txn, *lc->dbi, &stat_key, &data, 0);
        free(tmp_str);
        size = asprintf(&tmp_str, "_%s_rps", (char *)key.mv_data);
        stat_key.mv_size = size;
        stat_key.mv_data = tmp_str;
        stat_data.mv_size = sizeof(uint64_t);
        stat_data.mv_data = &reqs_per_sec;
        mdb_put(txn, *lc->dbi, &stat_key, &stat_data, 0);
        free(tmp_str);

    }
    mdb_cursor_close(cursor);
    mdb_txn_commit(txn);
    return 0;
}
Example #18
0
// Add or update a key in database.
void database_put(MDB_env *db, MDB_val *key, MDB_val *value) {
	MDB_dbi dbi;
	MDB_txn *txn;

	// transaction init
	mdb_txn_begin(db, NULL, 0, &txn);
	// open database in read-write mode
	mdb_dbi_open(txn, NULL, 0, &dbi);
	// put data
	mdb_put(txn, dbi, key, value, 0);
	// close database
	mdb_dbi_close(db, dbi);
	// transaction commit
	mdb_txn_commit(txn);
}
Example #19
0
CAMLprim value caml_mdb_put(value txn,value dbi,value key,value data,value flags){
  CAMLparam5(txn,dbi,key,data,flags);
  MDB_val key_,data_;
  key_.mv_data=String_val(key);
  key_.mv_size=caml_string_length(key);
  data_.mv_data=String_val(data);
  data_.mv_size=caml_string_length(data);
  if(mdb_put((MDB_txn*)txn,
             (MDB_dbi) Int_val(dbi),
             &key_,
             &data_,
             Int_val(flags)
             )){
    caml_failwith("error in mdb_put");
  }
  CAMLreturn0;
}
Example #20
0
static int put(struct dbengine *db, const char *key, size_t keylen,
               const char *data, size_t datalen, struct txn **tidptr, int mflags)
{
    MDB_val mkey, mval;
    struct txn *tid;
    int r, mr;

    mkey.mv_data = (void*) key;
    mkey.mv_size = keylen;
    mval.mv_data = (void*) data;
    mval.mv_size = datalen;

    /* Invalidate cursor */
    if (db->mcur) {
        mdb_cursor_close(db->mcur);
        db->mcur = NULL;
    }

    /* Open or reuse transaction */
    r = getorset_txn(db, tidptr, &tid, 0);
    if (r) goto fail;

    mr = mdb_put(tid->mtxn, tid->dbi, &mkey, &mval, mflags);
    if (mr) {
        /* Return the appropriate error code for existing key overwrites */
        syslog(LOG_ERR, "cryusdb_lmdb(%s): %s", db->fname, mdb_strerror(mr));
        r = (mr == MDB_KEYEXIST && (mflags & MDB_NOOVERWRITE)) ? \
            CYRUSDB_EXISTS : CYRUSDB_INTERNAL;
        goto fail;
    }

    /* Commit or export the transaction */
    if (!tidptr) {
        r = commit_txn(db, tid);
        if (r) goto fail;
    } else {
        *tidptr = tid;
    }

    return CYRUSDB_OK;

fail:
    if (tid && (!tidptr || !*tidptr)) abort_txn(db, tid);
    return r;
}
Example #21
0
static int
lmdb_storage_put(void* handle, paxos_accepted* acc)
{
	struct lmdb_storage* s = handle;
	int result;
	MDB_val key, data;
	char* buffer = paxos_accepted_to_buffer(acc);

	key.mv_data = &acc->iid;
	key.mv_size = sizeof(iid_t);

	data.mv_data = buffer;
	data.mv_size = sizeof(paxos_accepted) + acc->value.paxos_value_len;

	result = mdb_put(s->txn, s->dbi, &key, &data, 0);
	assert(result == 0);
	free(buffer);
	return 0;
}
Example #22
0
bool Context::Write(size_t ksz, void* key,
		    size_t dsz, void* data){
  Error(0);
  if (envHandle==nullptr){
    return false;
  }
  else if (txnHandle==nullptr){
    return false;
  }
  else{
    MDB_val kkey { ksz, key };
    MDB_val ddata { dsz, data};
    putReturn = mdb_put(txnHandle,dbiHandle,&kkey,&ddata,0);
    if (putReturn !=0){
      Error(putReturn);
      return false;
    }
    else return true;
  }
}
Example #23
0
static int __batcher_commit(batch_monitor_t* m, batch_queue_t* bq)
{
    if (0 == heap_count(bq->queue))
        return 0;

    MDB_txn *txn;

    int e = mdb_txn_begin(sv->db_env, NULL, 0, &txn);
    if (0 != e)
        mdb_fatal(e);

    while (0 < heap_count(bq->queue))
    {
        batch_item_t* item = heap_poll(bq->queue);
        e = mdb_put(txn, sv->docs, &item->key, &item->val, item->flags);
        switch (e)
        {
        case 0:
            break;
        case MDB_MAP_FULL:
        {
            mdb_txn_abort(txn);
            while ((item = heap_poll(bq->queue)))
                ;
            snprintf(batcher_error, BATCHER_ERROR_LEN, "NOT ENOUGH SPACE");
            return -1;
        }
        case MDB_KEYEXIST:
            item->flags = WOULD_OVERWRITE;
            break;
        default:
            mdb_fatal(e);
        }
    }

    e = mdb_txn_commit(txn);
    if (0 != e)
        mdb_fatal(e);

    return 0;
}
Example #24
0
int main(int argc,char * argv[])
{
    int rc;
    MDB_env *env;
    MDB_dbi dbi;
    MDB_val key, data;
    MDB_txn *txn;
    MDB_cursor *cursor;
    char sval[32];

    rc = mdb_env_create(&env);
    rc = mdb_env_open(env, "./testdb", 0, 0664);
    rc = mdb_txn_begin(env, NULL, 0, &txn);
    rc = mdb_open(txn, NULL, 0, &dbi);

    key.mv_size = sizeof(int);
    key.mv_data = sval;
    data.mv_size = sizeof(sval);
    data.mv_data = sval;

    sprintf(sval, "%03x %d foo bar", 32, 3141592);
    rc = mdb_put(txn, dbi, &key, &data, 0);
    rc = mdb_txn_commit(txn);
    if (rc) {
        fprintf(stderr, "mdb_txn_commit: (%d) %s\n", rc, mdb_strerror(rc));
        goto leave;
    }
    rc = mdb_txn_begin(env, NULL, MDB_RDONLY, &txn);
    rc = mdb_cursor_open(txn, dbi, &cursor);
    while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) {
        printf("key: %p %.*s, data: %p %.*s\n",
               key.mv_data,  (int) key.mv_size,  (char *) key.mv_data,
               data.mv_data, (int) data.mv_size, (char *) data.mv_data);
    }
    mdb_cursor_close(cursor);
    mdb_txn_abort(txn);
leave:
    mdb_close(env, dbi);
    mdb_env_close(env);
    return 0;
}
Example #25
0
static int
lmdb_storage_put_trim_instance(void* handle, iid_t iid)
{
	struct lmdb_storage* s = handle;
	iid_t k = 0;
	int result;
	MDB_val key, data;

	key.mv_data = &k;
	key.mv_size = sizeof(iid_t);

	data.mv_data = &iid;
	data.mv_size = sizeof(iid_t);

	result = mdb_put(s->txn, s->dbi, &key, &data, 0);
	if (result != 0)
		paxos_log_error("%s\n", mdb_strerror(result));
	assert(result == 0);

	return 0;
}
Example #26
0
int _db_put(DB *db, DB_TXN *txnid, DBT *key, DBT *data, uint32_t flags)
{
    //FIXME
    printf("%s\n", __FUNCTION__);

    MDB_val _key;
    MDB_val _data;

    _key.mv_size = key->size;
    _key.mv_data = key->data;
    _data.mv_size = data->size;
    _data.mv_data = data->data;

    uint32_t _flags = 0;

    if (mdb_txn_begin(db->_internal->env, NULL, 0, &db->_internal->txn) != 0) return EINVAL;
    mdb_put(db->_internal->txn, db->_internal->dbi, &_key, &_data, _flags);
    mdb_txn_commit(db->_internal->txn);

    return 0;
}
Example #27
0
bool DBPrivWrite(DBPriv *db, const void *key, int key_size, const void *value, int value_size)
{
    MDB_val mkey, data;
    DBTxn *txn;
    int rc = GetWriteTransaction(db, &txn);
    if (rc == MDB_SUCCESS)
    {
        assert(!txn->cursor_open);
        mkey.mv_data = (void *)key;
        mkey.mv_size = key_size;
        data.mv_data = (void *)value;
        data.mv_size = value_size;
        rc = mdb_put(txn->txn, db->dbi, &mkey, &data, 0);
        if (rc != MDB_SUCCESS)
        {
            Log(LOG_LEVEL_ERR, "Could not write database entry: %s", mdb_strerror(rc));
            AbortTransaction(db);
        }
    }
    return rc == MDB_SUCCESS;
}
Example #28
0
	int LMDBEngine::Put(const Slice& key, const Slice& value)
	{
		MDB_val k, v;
		k.mv_data = const_cast<char*>(key.data());
		k.mv_size = key.size();
		v.mv_data = const_cast<char*>(value.data());
		v.mv_size = value.size();
		BatchHolder& holder = m_batch_local.GetValue();
		if (!holder.EmptyRef())
		{
			//mdb_put(holder.txn, m_dbi, &k, &v, 0);
			holder.Put(key, value);
		}
		else
		{
			MDB_txn *txn = NULL;
			mdb_txn_begin(m_env, NULL, 0, &txn);
			mdb_put(txn, m_dbi, &k, &v, 0);
			mdb_txn_commit(txn);
		}
		return 0;
	}
Example #29
0
/*
 * Class:     jmdb_DatabaseWrapper
 * Method:    put
 * Signature: (JI[BII[BIII)V
 */
JNIEXPORT void JNICALL Java_jmdb_DatabaseWrapper_put(JNIEnv *vm, jclass clazz,
		jlong txnL, jint dbi, jbyteArray keyA, jint kofs, jint klen,
		jbyteArray valueA, jint vofs, jint vlen, jint flags) {
	MDB_txn *txnC = (MDB_txn*) txnL;
	enum {
		NONE, OOM, MDB
	} result = NONE;
	jint ret;
	MDB_val key, value;

	jbyte *keyC = (*vm)->GetPrimitiveArrayCritical(vm, keyA, NULL);
	jbyte *valueC = (*vm)->GetPrimitiveArrayCritical(vm, valueA, NULL);
	if (keyC == NULL || valueC == NULL) {
		result = OOM;
		ret = -1;
	} else {
		key.mv_size = klen;
		key.mv_data = keyC + kofs;
		value.mv_size = vlen;
		value.mv_data = valueC + vofs;
		ret = mdb_put(txnC, (MDB_dbi) dbi, &key, &value, (unsigned int) flags);
		if (ret) {
			result = MDB;
		}
	}
	(*vm)->ReleasePrimitiveArrayCritical(vm, valueA, valueC, 0);
	(*vm)->ReleasePrimitiveArrayCritical(vm, keyA, keyC, JNI_ABORT);

	switch (result) {
	case NONE:
		return;
	case OOM:
		throwNew(vm, "java/lang/OutOfMemoryError", "");
		return;
	case MDB:
		throwDatabaseException(vm, ret);
		return;
	}
}
Example #30
0
int gcache_put(struct gcache *gc, char *keystr, char *payload)
{
	int rc;
	MDB_val key, data;
	MDB_txn *txn;

	if (gc == NULL)
		return (1);

	if (strcmp(payload, "DELETE") == 0)
		return gcache_del(gc, keystr);

	rc = mdb_txn_begin(gc->env, NULL, 0, &txn);
	if (rc != 0) {
		olog(LOG_ERR, "gcache_put: mdb_txn_begin: %s", mdb_strerror(rc));
		return (-1);
	}

	key.mv_data	= keystr;
	key.mv_size	= strlen(keystr);
	data.mv_data	= payload;
	data.mv_size	= strlen(payload) + 1;	/* including nul-byte so we can
						 * later decode string directly
						 * from this buffer */

	rc = mdb_put(txn, gc->dbi, &key, &data, 0);
	if (rc != 0) {
		olog(LOG_ERR, "gcache_put: mdb_put: %s", mdb_strerror(rc));
		/* fall through to commit */
	}

	rc = mdb_txn_commit(txn);
	if (rc) {
		olog(LOG_ERR, "gcache_put: mdb_txn_commit: (%d) %s", rc, mdb_strerror(rc));
		mdb_txn_abort(txn);
	}
	return (rc);
}