Ejemplo n.º 1
0
int mdb_tool_entry_close(
	BackendDB *be )
{
	if ( mdb_tool_info ) {
		slapd_shutdown = 1;
		ldap_pvt_thread_mutex_lock( &mdb_tool_index_mutex );

		/* There might still be some threads starting */
		while ( mdb_tool_index_tcount > 0 ) {
			ldap_pvt_thread_cond_wait( &mdb_tool_index_cond_main,
					&mdb_tool_index_mutex );
		}

		mdb_tool_index_tcount = mdb_tool_threads - 1;
		ldap_pvt_thread_cond_broadcast( &mdb_tool_index_cond_work );

		/* Make sure all threads are stopped */
		while ( mdb_tool_index_tcount > 0 ) {
			ldap_pvt_thread_cond_wait( &mdb_tool_index_cond_main,
				&mdb_tool_index_mutex );
		}
		ldap_pvt_thread_mutex_unlock( &mdb_tool_index_mutex );

		mdb_tool_info = NULL;
		slapd_shutdown = 0;
		ch_free( mdb_tool_index_rec );
		mdb_tool_index_tcount = mdb_tool_threads - 1;
	}

	if( idcursor ) {
		mdb_cursor_close( idcursor );
		idcursor = NULL;
	}
	if( cursor ) {
		mdb_cursor_close( cursor );
		cursor = NULL;
	}
	if( txn ) {
		MDB_TOOL_IDL_FLUSH( be, txn );
		if ( mdb_txn_commit( txn ))
			return -1;
		txn = NULL;
	}

	if( nholes ) {
		unsigned i;
		fprintf( stderr, "Error, entries missing!\n");
		for (i=0; i<nholes; i++) {
			fprintf(stderr, "  entry %ld: %s\n",
				holes[i].id, holes[i].dn.bv_val);
		}
		nholes = 0;
		return -1;
	}

	return 0;
}
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;
}
Ejemplo n.º 3
0
static void *perform(void *arg)
{
	db_thread *thr = (db_thread*)arg;
	int i,rc;
	mdbinf* mdb = &thr->mdb;

	srand((u32)pthread_self());
	open_txn(mdb, MDB_RDONLY);
	thr->resFrames = alloca((SQLITE_DEFAULT_PAGE_SIZE/thr->maxvalsize + 1)*sizeof(MDB_val));
	
	for (i = 0; i < 1000*100; i++)
	{
		int j = rand() % NCONS;

		if (i % 1000 == 0)
			printf("r %lld %d\n",(i64)pthread_self(),i);

		if (pthread_mutex_trylock(&g_cons[j].wal.mtx) != 0)
			continue;

		g_tsd_conn = &g_cons[j];

		rc = sqlite3_exec(g_cons[j].db,"SELECT max(id) FROM tab;",NULL,NULL,NULL);
		if (rc != SQLITE_OK)
		{
			printf("Error select");
			break;
		}

		pthread_mutex_unlock(&g_cons[j].wal.mtx);

		mdb_txn_reset(thr->mdb.txn);
		rc = mdb_txn_renew(thr->mdb.txn);
		if (rc != MDB_SUCCESS)
			break;
		rc = mdb_cursor_renew(mdb->txn, mdb->cursorLog);
		if (rc != MDB_SUCCESS)
			break;
		rc = mdb_cursor_renew(mdb->txn, mdb->cursorPages);
		if (rc != MDB_SUCCESS)
			break;
		rc = mdb_cursor_renew(mdb->txn, mdb->cursorInfo);
		if (rc != MDB_SUCCESS)
			break;
	}
	mdb_cursor_close(mdb->cursorLog);
	mdb_cursor_close(mdb->cursorPages);
	mdb_cursor_close(mdb->cursorInfo);
	mdb_txn_abort(mdb->txn);
	return NULL;
}
Ejemplo n.º 4
0
int
mdb_dn2entry(
	Operation *op,
	MDB_txn *tid,
	MDB_cursor *m2,
	struct berval *dn,
	Entry **e,
	ID *nsubs,
	int matched )
{
	struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
	int rc, rc2;
	ID id = NOID;
	struct berval mbv, nmbv;
	MDB_cursor *mc;

	Debug(LDAP_DEBUG_TRACE, "mdb_dn2entry(\"%s\")\n",
		dn->bv_val ? dn->bv_val : "", 0, 0 );

	*e = NULL;

	rc = mdb_dn2id( op, tid, m2, dn, &id, nsubs, &mbv, &nmbv );
	if ( rc ) {
		if ( matched ) {
			rc2 = mdb_cursor_open( tid, mdb->mi_id2entry, &mc );
			if ( rc2 == MDB_SUCCESS ) {
				rc2 = mdb_id2entry( op, mc, id, e );
				mdb_cursor_close( mc );
			}
		}

	} else {
		rc = mdb_cursor_open( tid, mdb->mi_id2entry, &mc );
		if ( rc == MDB_SUCCESS ) {
			rc = mdb_id2entry( op, mc, id, e );
			mdb_cursor_close(mc);
		}
	}
	if ( *e ) {
		(*e)->e_name = mbv;
		if ( rc == MDB_SUCCESS )
			ber_dupbv_x( &(*e)->e_nname, dn, op->o_tmpmemctx );
		else
			ber_dupbv_x( &(*e)->e_nname, &nmbv, op->o_tmpmemctx );
	} else {
		op->o_tmpfree( mbv.bv_val, op->o_tmpmemctx );
	}

	return rc;
}
Ejemplo n.º 5
0
bool LmdbDataset<K, V, KCoder, VCoder>::last_key(K* key) {
  DLOG(INFO) << "LMDB: Last key";

  int retval;

  MDB_txn* iter_txn;

  retval = mdb_txn_begin(env_, NULL, MDB_RDONLY, &iter_txn);
  CHECK_EQ(MDB_SUCCESS, retval) << "mdb_txn_begin failed "
      << mdb_strerror(retval);

  MDB_cursor* cursor;
  retval = mdb_cursor_open(iter_txn, dbi_, &cursor);
  CHECK_EQ(retval, MDB_SUCCESS) << mdb_strerror(retval);
  MDB_val mdbkey;
  MDB_val mdbval;
  retval = mdb_cursor_get(cursor, &mdbkey, &mdbval, MDB_LAST);
  CHECK_EQ(retval, MDB_SUCCESS) << mdb_strerror(retval);

  mdb_cursor_close(cursor);
  mdb_txn_abort(iter_txn);

  if (!KCoder::deserialize(reinterpret_cast<char*>(mdbkey.mv_data),
      mdbkey.mv_size, key)) {
    LOG(ERROR) << "failed to deserialize key";
    return false;
  }

  return true;
}
Ejemplo n.º 6
0
common::Error LmdbRaw::keys(const std::string &key_start, const std::string &key_end, uint64_t limit,
                   std::vector<std::string> *ret) {
  MDB_cursor *cursor;
  MDB_txn *txn = NULL;
  int rc = mdb_txn_begin(lmdb_->env, NULL, MDB_RDONLY, &txn);
  if (rc == LMDB_OK) {
    rc = mdb_cursor_open(txn, lmdb_->dbir, &cursor);
  }

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

  MDB_val key;
  MDB_val data;
  while ((mdb_cursor_get(cursor, &key, &data, MDB_NEXT) == LMDB_OK) && limit > ret->size()) {
    std::string skey((const char*)key.mv_data, key.mv_size);
    if (key_start < skey && key_end > skey) {
      ret->push_back(skey);
    }
  }
  mdb_cursor_close(cursor);
  mdb_txn_abort(txn);

  return common::Error();
}
Ejemplo n.º 7
0
common::Error LmdbRaw::dbsize(size_t* size) {
  if (!size) {
    return common::make_error_value("Invalid input argument", common::ErrorValue::E_ERROR);
  }

  MDB_cursor *cursor;
  MDB_txn *txn = NULL;
  int rc = mdb_txn_begin(lmdb_->env, NULL, MDB_RDONLY, &txn);
  if (rc == LMDB_OK) {
    rc = mdb_cursor_open(txn, lmdb_->dbir, &cursor);
  }

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

  MDB_val key;
  MDB_val data;
  size_t sz = 0;
  while (mdb_cursor_get(cursor, &key, &data, MDB_NEXT) == LMDB_OK) {
    sz++;
  }
  mdb_cursor_close(cursor);
  mdb_txn_abort(txn);

  *size = sz;
  return common::Error();
}
Ejemplo n.º 8
0
int
mdb_dn2id_children(
	Operation *op,
	MDB_txn *txn,
	Entry *e )
{
	struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
	MDB_dbi dbi = mdb->mi_dn2id;
	MDB_val		key, data;
	MDB_cursor	*cursor;
	int		rc;
	ID		id;

	key.mv_size = sizeof(ID);
	key.mv_data = &id;
	id = e->e_id;

	rc = mdb_cursor_open( txn, dbi, &cursor );
	if ( rc ) return rc;

	rc = mdb_cursor_get( cursor, &key, &data, MDB_SET );
	if ( rc == 0 ) {
		size_t dkids;
		rc = mdb_cursor_count( cursor, &dkids );
		if ( rc == 0 ) {
			if ( dkids < 2 ) rc = MDB_NOTFOUND;
		}
	}
	mdb_cursor_close( cursor );
	return rc;
}
Ejemplo n.º 9
0
void counter_dump(counter_t *lc, buffer_t *output) {
    MDB_val key, data, rps_key, rps_data;
    MDB_txn *txn = NULL;
    MDB_cursor *cursor = NULL;
    mdb_txn_begin(lc->env, NULL, MDB_RDONLY, &txn);
    mdb_cursor_open(txn, *lc->dbi, &cursor);
    int rc = 0;
    char *tmp_str = NULL;
    int size = 0;
    uint64_t rps = 0;
    while((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) {
        if((*(char *)key.mv_data) == '_') {
            continue;
        }
        size = asprintf(&tmp_str, "_%s_rps", (char *)key.mv_data);
        rps_key.mv_size = size;
        rps_key.mv_data = tmp_str;
        if(mdb_get(txn, *lc->dbi, &rps_key, &rps_data) == MDB_SUCCESS) {
            rps = *(uint64_t *)rps_data.mv_data;
        }
        free(tmp_str);
        size = asprintf(&tmp_str, "%s: %lu - %lurps\n", (char *)key.mv_data, *(uint64_t *)data.mv_data, rps);
        buffer_append(output, tmp_str, size);
        free(tmp_str);
    }
    mdb_cursor_close(cursor);
    mdb_txn_abort(txn);
}
Ejemplo n.º 10
0
int addOrUpdateIntIndexedProperty(GLMDB_env *glmdb_env, MDB_txn *mdbTxn, MDB_cursor * vertexCursor, long long elementId, int propertyKeyId,
		int propertyValue, unsigned char vertex) {

	int rc;
	MDB_cursor *intIndexCursor;
	if (vertex) {
		rc = mdb_cursor_open(mdbTxn, glmdb_env->vertexIntIndexDb, &intIndexCursor);
	} else {
		rc = mdb_cursor_open(mdbTxn, glmdb_env->edgeIntIndexDb, &intIndexCursor);
	}
	if (rc != 0) {
		goto failIndexedVertex;
	}
	rc = deleteEntryFromIntIndex(glmdb_env, mdbTxn, vertexCursor, intIndexCursor, (long long) elementId, (int) propertyKeyId);
	if (rc != 0 && rc != MDB_NOTFOUND) {
		goto failIndexedVertex;
	}
	rc = setVertexPropertyInt(vertexCursor, (long) elementId, (int) propertyKeyId, &propertyValue);
	if (rc != 0) {
		goto failIndexedVertex;
	}

	rc = setIntIndex(intIndexCursor, elementId, propertyKeyId, propertyValue);

	failIndexedVertex: mdb_cursor_close(intIndexCursor);
	return rc;
}
Ejemplo n.º 11
0
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);
}
Ejemplo n.º 12
0
VOID
VmDirMDBIndexIteratorFree(
    PVDIR_BACKEND_INDEX_ITERATOR    pIterator
    )
{
    PVDIR_MDB_INDEX_ITERATOR    pMdbIterator = NULL;

    if (pIterator)
    {
        pMdbIterator = (PVDIR_MDB_INDEX_ITERATOR)pIterator->pIterator;
        if (pMdbIterator)
        {
            if (pMdbIterator->pCursor)
            {
                mdb_cursor_close(pMdbIterator->pCursor);
            }
            if (pMdbIterator->pTxn)
            {
                if (pMdbIterator->bAbort)
                {
                    mdb_txn_abort(pMdbIterator->pTxn);
                }
                else
                {
                    mdb_txn_commit(pMdbIterator->pTxn);
                }
            }
            VMDIR_SAFE_FREE_MEMORY(pMdbIterator->pszVal);
            VMDIR_SAFE_FREE_MEMORY(pMdbIterator);
        }
        VMDIR_SAFE_FREE_MEMORY(pIterator);
    }
}
Ejemplo n.º 13
0
bool B_ACCURATE_LMDB::send_base_file_list(JCR *jcr)
{
   int result;
   int32_t LinkFIc;
   FF_PKT *ff_pkt;
   MDB_cursor *cursor;
   MDB_val key, data;
   bool retval = false;
   accurate_payload *payload;
   int stream = STREAM_UNIX_ATTRIBUTES;

   if (!jcr->accurate || jcr->getJobLevel() != L_FULL) {
      return true;
   }

   /*
    * Commit any pending write transactions.
    */
   if (m_db_rw_txn) {
      result = mdb_txn_commit(m_db_rw_txn);
      if (result != 0) {
         Jmsg1(jcr, M_FATAL, 0, _("Unable close write transaction: %s\n"), mdb_strerror(result));
         goto bail_out;
      }
      m_db_rw_txn = NULL;
   }

   ff_pkt = init_find_files();
   ff_pkt->type = FT_BASE;

   result = mdb_cursor_open(m_db_ro_txn, m_db_dbi, &cursor);
   if (result == 0) {
      while ((result = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) {
         payload = (accurate_payload *)data.mv_data;
         if (bit_is_set(payload->filenr, m_seen_bitmap)) {
            Dmsg1(dbglvl, "base file fname=%s\n", key.mv_data);
            decode_stat(payload->lstat, &ff_pkt->statp, sizeof(struct stat), &LinkFIc); /* decode catalog stat */
            ff_pkt->fname = (char *)key.mv_data;
            encode_and_send_attributes(jcr, ff_pkt, stream);
         }
      }
      mdb_cursor_close(cursor);
   } else {
      Jmsg1(jcr, M_FATAL, 0, _("Unable create cursor: %s\n"), mdb_strerror(result));
   }

   mdb_txn_reset(m_db_ro_txn);
   result = mdb_txn_renew(m_db_ro_txn);
   if (result != 0) {
      Jmsg1(jcr, M_FATAL, 0, _("Unable to renew read transaction: %s\n"), mdb_strerror(result));
      goto bail_out;
   }

   retval = true;

bail_out:
   term_find_files(ff_pkt);
   return retval;
}
Ejemplo n.º 14
0
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);
     }
}
Ejemplo n.º 15
0
int mdb_ad_read( struct mdb_info *mdb, MDB_txn *txn )
{
	int i, rc;
	MDB_cursor *mc;
	MDB_val key, data;
	struct berval bdata;
	const char *text;
	AttributeDescription *ad;

	rc = mdb_cursor_open( txn, mdb->mi_ad2id, &mc );
	if ( rc ) {
		Debug( LDAP_DEBUG_ANY,
			"mdb_ad_read: cursor_open failed %s(%d)\n",
			mdb_strerror(rc), rc, 0);
		return rc;
	}

	/* our array is 1-based, an index of 0 means no data */
	i = mdb->mi_numads+1;
	key.mv_size = sizeof(int);
	key.mv_data = &i;

	rc = mdb_cursor_get( mc, &key, &data, MDB_SET );

	while ( rc == MDB_SUCCESS ) {
		bdata.bv_len = data.mv_size;
		bdata.bv_val = data.mv_data;
		ad = NULL;
		rc = slap_bv2ad( &bdata, &ad, &text );
		if ( rc ) {
			rc = slap_bv2undef_ad( &bdata, &mdb->mi_ads[i], &text, 0 );
		} else {
			if ( ad->ad_index >= MDB_MAXADS ) {
				Debug( LDAP_DEBUG_ANY,
					"mdb_adb_read: too many AttributeDescriptions in use\n",
					0, 0, 0 );
				return LDAP_OTHER;
			}
			mdb->mi_adxs[ad->ad_index] = i;
			mdb->mi_ads[i] = ad;
		}
		i++;
		rc = mdb_cursor_get( mc, &key, &data, MDB_NEXT );
	}
	mdb->mi_numads = i-1;

done:
	if ( rc == MDB_NOTFOUND )
		rc = 0;

	mdb_cursor_close( mc );

	return rc;
}
Ejemplo n.º 16
0
int64 LMDBFileIndex::get_prefer_client( const SIndexKey& key )
{
	begin_txn(MDB_RDONLY);

	MDB_cursor* cursor;

	mdb_cursor_open(txn, dbi, &cursor);

	SIndexKey orig_key = key;

	MDB_val mdb_tkey;
	mdb_tkey.mv_data=const_cast<void*>(static_cast<const void*>(&key));
	mdb_tkey.mv_size=sizeof(SIndexKey);

	MDB_val mdb_tvalue;

	int rc=mdb_cursor_get(cursor,&mdb_tkey, &mdb_tvalue, MDB_SET_RANGE);

	int64 ret = 0;
	int retry_prev=2;
	while(rc==0 && retry_prev>0 && !_has_error)
	{
		SIndexKey* curr_key = reinterpret_cast<SIndexKey*>(mdb_tkey.mv_data);

		if(rc==MDB_NOTFOUND)
		{
			retry_prev=0;
		}
		else if(rc)
		{
			Server->Log("LMDB: Failed to read ("+(std::string)mdb_strerror(rc)+")", LL_ERROR);
			_has_error=true;
		}
		else if( curr_key->isEqualWithoutClientid(orig_key))
		{
			CRData data((const char*)mdb_tvalue.mv_data, mdb_tvalue.mv_size);
			data.getVarInt(&ret);
			retry_prev=0;
		}
		else
		{
			rc=mdb_cursor_get(cursor, &mdb_tkey, &mdb_tvalue, MDB_PREV);
			--retry_prev;
		}
	}

	mdb_cursor_close(cursor);

	abort_transaction();

	return ret;
}
Ejemplo n.º 17
0
int mail_cache_db_get_keys(struct mail_cache_db * cache_db,
    chash * keys)
{
  int r;
  MDB_env *env;
  MDB_txn *txn;
  MDB_dbi dbi;
  MDB_cursor *cursor;
  MDB_val mdb_key;
  MDB_val mdb_val;

  env = cache_db->internal_database;

  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_cursor_open(txn, dbi, &cursor);
  if (r != 0)
    goto error;

  r = mdb_cursor_get(cursor, &mdb_key, &mdb_val, MDB_FIRST);
  if (r != 0)
    goto cursor_error;

  while (r == 0) {
    chashdatum hash_key;
    chashdatum hash_data;

    hash_key.data = mdb_key.mv_data;
    hash_key.len = (unsigned int) mdb_key.mv_size;
    hash_data.data = NULL;
    hash_data.len = 0;

    r = chash_set(keys, &hash_key, &hash_data, NULL);
    if (r != 0)
      goto cursor_error;
    r = mdb_cursor_get(cursor, &mdb_key, &mdb_val, MDB_NEXT);
  }

  mdb_txn_commit(txn);
  return 0;

  cursor_error:
    mdb_cursor_close(cursor);
  error:
    mdb_txn_abort(txn);
  return -1;
}
Ejemplo n.º 18
0
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);
}
Ejemplo n.º 19
0
static inline void _cursor_cancel(cursor_t cursor){
	cursor_t next = cursor->next;
	if(next)
		next->prev = cursor->prev;
	// we always have a prev
	cursor->prev->next = next;

	mdb_cursor_close(cursor->cursor);
	cursor->cursor = NULL;

	// mark as cancelled
	cursor->prev = NULL;
}
Ejemplo n.º 20
0
int traverseIntIndexDb(GLMDB_env * glmdb_env, MDB_txn *txn, MDB_dbi dbi) {
	int rc = 0;
	MDB_cursor *cursor;
	MDB_val key, data;
	rc = mdb_cursor_open(txn, dbi, &cursor);
	if (rc != 0) {
		goto fail;
	}
	while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) {
		printIntIndexDbRecord(key, data);
	}
	fail: mdb_cursor_close(cursor);
	return rc;
}
Ejemplo n.º 21
0
std::map<int, int64> LMDBFileIndex::get_all_clients( const SIndexKey& key )
{
	begin_txn(MDB_RDONLY);

	MDB_cursor* cursor;

	mdb_cursor_open(txn, dbi, &cursor);

	SIndexKey orig_key = key;

	MDB_val mdb_tkey;
	mdb_tkey.mv_data=const_cast<void*>(static_cast<const void*>(&key));
	mdb_tkey.mv_size=sizeof(SIndexKey);

	MDB_val mdb_tvalue;

	int rc=mdb_cursor_get(cursor,&mdb_tkey, &mdb_tvalue, MDB_SET_RANGE);

	std::map<int, int64> ret;

	SIndexKey* curr_key = reinterpret_cast<SIndexKey*>(mdb_tkey.mv_data);

	while(rc==0 &&
		orig_key.isEqualWithoutClientid(*curr_key))
	{
		CRData data((const char*)mdb_tvalue.mv_data, mdb_tvalue.mv_size);
		int64 entryid;
		data.getVarInt(&entryid);

		ret[curr_key->getClientid()] = entryid;

		rc=mdb_cursor_get(cursor, &mdb_tkey, &mdb_tvalue, MDB_NEXT);
		curr_key = reinterpret_cast<SIndexKey*>(mdb_tkey.mv_data);
	}


	if(rc && rc!=MDB_NOTFOUND)
	{
		Server->Log("LMDB: Failed to read ("+(std::string)mdb_strerror(rc)+")", LL_ERROR);
		_has_error=true;
	}

	mdb_cursor_close(cursor);

	abort_transaction();

	return ret;
}
Ejemplo n.º 22
0
/** Put a document into the database at this key
 * If the "Prefers: ETag" header is set, we perform a CAS operation */
static int __put(h2o_req_t *req, kstr_t* key)
{
    char* sv_etag = __remove_stored_etag(key);
    h2o_iovec_t cli_etag = __get_if_match_header_value(req, key);
    if (0 < cli_etag.len &&
        !__should_etag_conditional_put_succeed(req, key, sv_etag, &cli_etag))
    {
        if (sv_etag)
            free(sv_etag);
        return h2oh_respond_with_error(req, 412, "BAD ETAG");
    }

    if (sv_etag)
        free(sv_etag);

    batch_item_t item = {
        .flags       = 0,
        .key.mv_data = key->s,
        .key.mv_size = key->len,
        .val.mv_data = req->entity.base,
        .val.mv_size = req->entity.len,
    };

    int e = bmon_offer(&sv->batch, &item);
    if (0 != e)
        return h2oh_respond_with_error(req, 400, batcher_error);
    return h2oh_respond_with_success(req, 200);
}

typedef struct
{
    h2o_generator_t super;
    h2o_req_t *req;
    MDB_txn *txn;
    MDB_cursor* curs;
    kstr_t* key;
} get_keys_generator_t;

static void __get_keys_close(h2o_generator_t *_self, h2o_req_t *req)
{
    get_keys_generator_t *gen = (void*)_self;

    mdb_cursor_close(gen->curs);

    int e = mdb_txn_commit(gen->txn);
    if (0 != e)
        mdb_fatal(e);
}
Ejemplo n.º 23
0
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;
}
Ejemplo n.º 24
0
DataLayer<Dtype>::~DataLayer<Dtype>() {
  this->JoinPrefetchThread();
  // clean up the database resources
  switch (this->layer_param_.data_param().backend()) {
  case DataParameter_DB_LEVELDB:
    break;  // do nothing
  case DataParameter_DB_LMDB:
    mdb_cursor_close(mdb_cursor_);
    mdb_close(mdb_env_, mdb_dbi_);
    mdb_txn_abort(mdb_txn_);
    mdb_env_close(mdb_env_);
    break;
  default:
    LOG(FATAL) << "Unknown database backend";
  }
}
Ejemplo n.º 25
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[MAX_DATA_ALLOCATE_SIZE], kval[MAX_KEY_ALLOCATE_SIZE];

    /* Note: Most error checking omitted for simplicity */

    rc = mdb_env_create(&env);
    mdb_env_set_mapsize(env, MAX_DB_SIZE);
    mdb_env_set_maxdbs(env, (MDB_dbi)10);
    rc = mdb_env_open(env, "./demoDB", 0, 0664);
    rc = mdb_txn_begin(env, NULL, 0, &txn);
    rc = mdb_dbi_open(txn, "what", 0, &dbi);
    
    // 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;
    }

    fprintf(stderr, "print out old data:\n");
    rc = mdb_txn_begin(env, NULL, MDB_RDONLY, &txn);
    rc = mdb_cursor_open(txn, dbi, &cursor);

    key.mv_size = sizeof(kval);
    key.mv_data = kval;
    data.mv_size = sizeof(sval);
    data.mv_data = sval;
    memset(kval, argv[1][0], sizeof(kval));

    rc = mdb_cursor_get(cursor, &key, &data, MDB_SET);
    fprintf(stderr, "key: %s, data: %s\n",(char *) key.mv_data,(char *) data.mv_data);

    mdb_cursor_close(cursor);
    mdb_txn_abort(txn);
leave:
    mdb_dbi_close(env, dbi);
    mdb_env_close(env);
    return 0;
}
Ejemplo n.º 26
0
static int
lmdb_storage_trim(void* handle, iid_t iid)
{
	struct lmdb_storage* s = handle;
	int result;
	iid_t min = 0;
	MDB_cursor* cursor = NULL;
	MDB_val key, data;

	if (iid == 0)
		return 0;

	lmdb_storage_put_trim_instance(handle, iid);

	if ((result = mdb_cursor_open(s->txn, s->dbi, &cursor)) != 0) {
		paxos_log_error("Could not create cursor. %s", mdb_strerror(result));
		goto cleanup_exit;
	}

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

	do {
		if ((result = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) {
			assert(key.mv_size = sizeof(iid_t));
			min = *(iid_t*)key.mv_data;
		} else {
			goto cleanup_exit;
		}

		if (min != 0 && min <= iid) {
			if (mdb_cursor_del(cursor, 0) != 0) {
				paxos_log_error("mdb_cursor_del failed. %s",
				mdb_strerror(result));
				goto cleanup_exit;
			}
		}
	} while (min <= iid);

	cleanup_exit:
	if (cursor) {
		mdb_cursor_close(cursor);
	}
	return 0;
}
Ejemplo n.º 27
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;
}
Ejemplo n.º 28
0
static int
mdb_tool_idl_flush_db( MDB_txn *txn, AttrInfo *ai )
{
	MDB_cursor *mc;
	Avlnode *root;
	int rc;

	mdb_cursor_open( txn, ai->ai_dbi, &mc );
	root = tavl_end( ai->ai_root, TAVL_DIR_LEFT );
	do {
		rc = mdb_tool_idl_flush_one( mc, ai, root->avl_data );
		if ( rc != -1 )
			rc = 0;
	} while ((root = tavl_next(root, TAVL_DIR_RIGHT)));
	mdb_cursor_close( mc );

	return rc;
}
Ejemplo n.º 29
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;
}
Ejemplo n.º 30
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;
}