Ejemplo n.º 1
0
void
Freeze::handleDbException(const DbException& dx,
                          Key& key, Dbt& dbKey,
                          Value& value, Dbt& dbValue,
                          const char* file, int line)
{
    bool bufferSmallException =
#if (DB_VERSION_MAJOR == 4) && (DB_VERSION_MINOR == 2)
        (dx.get_errno() == ENOMEM);
#else
        (dx.get_errno() == DB_BUFFER_SMALL || dx.get_errno() == ENOMEM);
#endif

    bool resized = false;
    if(bufferSmallException)
    {
        if(dbKey.get_size() > dbKey.get_ulen())
        {
            size_t oldKeySize = key.size();
            key.resize(dbKey.get_size());
            initializeOutDbt(key, dbKey);
            dbKey.set_size(static_cast<u_int32_t>(oldKeySize));
            resized = true;
        }

        if(dbValue.get_size() > dbValue.get_ulen())
        {
            value.resize(dbValue.get_size());
            initializeOutDbt(value, dbValue);
            resized = true;
        }
    }

    if(!resized)
    {
        handleDbException(dx, file, line);
    }
}
Ejemplo n.º 2
0
AntiCacheBlock AntiCacheDB::readBlock(std::string tableName, int16_t blockId) {
    Dbt key;
    key.set_data(&blockId);
    key.set_size(sizeof(int16_t));

    Dbt value;
    value.set_flags(DB_DBT_MALLOC);
    
    VOLT_DEBUG("Reading evicted block with id %d", blockId);
    
    int ret_value = m_db->get(NULL, &key, &value, 0);
    if (ret_value != 0) {
        VOLT_ERROR("Invalid anti-cache blockId '%d' for table '%s'", blockId, tableName.c_str());
        throw UnknownBlockAccessException(tableName, blockId);
    }
    else {
//        m_db->del(NULL, &key, 0);  // if we have this the benchmark won't end
        assert(value.get_data() != NULL);
    }
    
    AntiCacheBlock block(blockId, value);
    return (block);
}
Ejemplo n.º 3
0
void MultiPartHeader::deleteAllParts(Db* pDB)
{
	int ret = 0;
    Dbc *cursorp = 0;

	Dbt key;
	Dbt data;

    //qDebug() << "Deleting all parts for key " << multiPartKey;

	key.set_data(&multiPartKey);
	key.set_size(sizeof(quint64));

	// Get a cursor
	pDB->cursor(NULL, &cursorp, DB_WRITECURSOR);

	while ((ret = cursorp->get(&key, &data, DB_SET)) == 0)
	    cursorp->del(0);

	pDB->sync(0);

	if (cursorp != NULL)
	    cursorp->close();
}
Ejemplo n.º 4
0
void BtRecExample::run()
{
	db_recno_t recno;
	int ret;
	char buf[1024];

	// Acquire a cursor for the database.
	dbp->cursor(NULL, &dbcp, 0);

	//
	// Prompt the user for a record number, then retrieve and display
	// that record.
	//
	for (;;) {
		// Get a record number.
		cout << "recno #> ";
		cout.flush();
		if (fgets(buf, sizeof(buf), stdin) == NULL)
			break;
		recno = atoi(buf);

		//
		// Start with a fresh key each time,
		// the dbp->get() routine returns
		// the key and data pair, not just the key!
		//
		Dbt key(&recno, sizeof(recno));
		Dbt data;

		if ((ret = dbcp->get(&key, &data, DB_SET_RECNO)) != 0) {
			dbp->err(ret, "DBcursor->get");
			throw DbException(ret);
		}

		// Display the key and data.
		show("k/d\t", &key, &data);

		// Move the cursor a record forward.
		if ((ret = dbcp->get(&key, &data, DB_NEXT)) != 0) {
			dbp->err(ret, "DBcursor->get");
			throw DbException(ret);
		}

		// Display the key and data.
		show("next\t", &key, &data);

		//
		// Retrieve the record number for the following record into
		// local memory.
		//
		data.set_data(&recno);
		data.set_size(sizeof(recno));
		data.set_ulen(sizeof(recno));
		data.set_flags(data.get_flags() | DB_DBT_USERMEM);

		if ((ret = dbcp->get(&key, &data, DB_GET_RECNO)) != 0) {
			if (ret != DB_NOTFOUND && ret != DB_KEYEMPTY) {
				dbp->err(ret, "DBcursor->get");
				throw DbException(ret);
			}
		}
		else {
			cout << "retrieved recno: " << (u_long)recno << "\n";
		}
	}

	dbcp->close();
	dbcp = NULL;
}
Ejemplo n.º 5
0
// Modify or create a part based on the following information
MultiPartHeader::Add_Code MultiPartHeader::modifyPart(Db* pdb, quint16 partNo, RawHeader *rh, quint16 hostId)
{
	//qDebug() << "Entering modifyPart";
    Add_Code retCode = New_Part;
	int ret = 0;
	bool createPart = false;
	Header* h = 0;
    char *phead = 0;
    Dbc *cursorp = 0;

	Dbt key;
	Dbt data;

	//qDebug() << "Creating part for key " << multiPartKey;

	key.set_data(&multiPartKey);
	key.set_size(sizeof(quint64));

	data.set_data(&partNo);
	data.set_size(sizeof(quint16));

	// Get a cursor
	pdb->cursor(NULL, &cursorp, DB_WRITECURSOR);
	//qDebug("1");

	// Position the cursor to the first record in the database whose
	// key matches the search key and whose data begins with the search
	// data.
	ret = cursorp->get(&key, &data, DB_GET_BOTH);
	if  (ret == 0)
	{
		//qDebug("2");
	    // update the data
		h = new Header((char*)data.get_data(), (char*)key.get_data());
		//qDebug("3");

		if (h->isHeldByServer(hostId))
		{
			retCode = Duplicate_Part;
		}
		else
			retCode = Updated_Part;

        // qDebug() << "Found an update for part " << partNo << ", existing part " << h->getPartNo() << ", existing msgid " << h->getMessageId() << ", new msgid " << rh->m_mid;

		// Always update the article number
		h->setServerPartNum(hostId, rh->m_num);

		//qDebug("4");
		phead=h->data();

		data.set_data(phead);
		data.set_size(h->getRecordSize());

		//qDebug("5");
		cursorp->put(&key, &data, DB_CURRENT);
		//qDebug("6");
        Q_DELETE_ARRAY(phead);
	}
	else if (ret == DB_NOTFOUND) // create the part
	{
		//qDebug("7");
		createPart = true;
	}
	else // encountered an error
	{
		qDebug() << "Unable to find part " << partNo << " for key " << multiPartKey << ", result = " << ret;
		retCode = Error_Part;
	}

	// Close the cursor
	if (cursorp != NULL)
	    cursorp->close();

	if (createPart == true)
	{
		//qDebug("8");
		quint32 partSize = rh->m_bytes.toULong();
		h = new Header(multiPartKey, partNo, partSize);

		//qDebug("9");

		h->setMessageId(rh->m_mid);
		h->setServerPartNum(hostId, rh->m_num);

		//save in the parts db
		phead=h->data();

		data.set_data(phead);
		data.set_size(h->getRecordSize());

		//qDebug("10");

		ret = pdb->put(0, &key, &data, 0);
		if (ret)
		{
		    qDebug() << "Unable to create part " << partNo << " for key " << multiPartKey << ", result = " << ret;
		    retCode = Error_Part;
		}
		else
			retCode = New_Part;
		//qDebug("11");
        Q_DELETE_ARRAY(phead);
	}

    Q_DELETE(h);

	//qDebug("12\n");

	//qDebug() << "Exiting modifyPart";
	return retCode;
}
Ejemplo n.º 6
0
int* ROSWOSSegment::lookupByIndex( char* key, int size, Dbc *iter,
				   u_int32_t flags, void **_ptr, void **buf )
{
   	size_t retklen, retdlen;
	Dbt skey;
	void *retkey, *retdata;	
	bool second_pass = false;
	int answer;

	memset(&skey, 0, sizeof(skey)); 
	memset(&_bulk_data, 0, sizeof(_bulk_data)); 
	//memset( (*buf), 0, sizeof( (*buf) ) );

	//_bulk_data.set_data( _bulk_buffer );
	_bulk_data.set_data( (*buf) );
	_bulk_data.set_ulen( BUFFER_LENGTH );
	_bulk_data.set_flags( DB_DBT_USERMEM );

	skey.set_data( key );
	skey.set_size( size );

	//if  ( _debug ) return NULL;

	do
	  {

	// if bulk buffer is done, get a new one.
	    //if ( _p_bulk == NULL )
	if ( ((*_ptr)) == NULL )
	  {
	    //cout << " Request from iter " << iter << "  key " << *(int *)key << " FLAGS? " << flags <<  endl;
	    memset( (*buf), 0, sizeof( (*buf) ) );

	    answer = iter->/*_sort_key_cursor->*/get( &skey, &_bulk_data, 
				   flags | DB_MULTIPLE_KEY );

	    if ( answer == DB_NOTFOUND )
	      return NULL;
	    //DB_MULTIPLE_INIT(_p_bulk, _bulk_data.get_DBT());
	    DB_MULTIPLE_INIT((*_ptr), _bulk_data.get_DBT());
	    //cout << " DB_NOTFOUND " << DB_NOTFOUND << " ans " <<  answer << " -- Getting answers, page size " << BUFFER_LENGTH << " ITER " << iter << " PTR " << (*_ptr) <<endl;
	  }
	//else
	//cout << " ROSWOSSegment, *_ptr is not NULL, go go go" << endl;
	/*if ( answer == DB_NOTFOUND )
	  {
	    //_debug = true;
	    // THAT really does NOT close the iterator. WHY, I have not
	    // a faintest idea!
	    //cout << " ITERATOR " << iter << " IS CLOSED " << endl;
	    //iter->close();
	    return NULL;
	    }*/

	//if ( !_p_bulk )
	if ( !((*_ptr)) )
	  return NULL;

	// I don't understand that.
	//if ( !_p_bulk && second_pass )
	//cout << " PTR " << (*_ptr) << " SECOND PASS " << second_pass << endl;
	if ( !((*_ptr)) && second_pass )
	  return NULL;
	
	DB_MULTIPLE_KEY_NEXT((*_ptr), _bulk_data.get_DBT(), retkey, retklen, retdata, retdlen);
	//DB_MULTIPLE_KEY_NEXT(_p_bulk, _bulk_data.get_DBT(), retkey, retklen, retdata, retdlen);
	// cout << " ANd now, ptr is " << *_ptr << " int ptr " << ((int*)retkey) << endl;

	second_pass = true;

	  }
	//while( !_p_bulk );  // if bulk is null, get the buffer.
	while( !(*_ptr) );  // if bulk is null, get the buffer.
	
	/*if ( (*((int*)retkey)) > 10500 )
	{
	cout << DB_NOTFOUND << " <=> " << answer << " Ret answer " << *((int *)retkey) << " retDATA " << *((int *)retdata) << endl;
	cout << "     Recieved buffer " << buf << " points to buf " << (*buf) << " and ptr " << *_ptr << endl;
	*/
	/*if ( *((int *)retdata) == 596992  &&
	     10511 == *((int *)retkey) )
	  *(int *)retdata = 597221;

	if ( *((int *)retdata) == 598272  &&
	     10524 == *((int *)retkey) )
	     *(int *)retdata = 598362;*/

	//cout << " Finally returning " << ( ((int *)retdata) ) << endl << endl;
	return( ((int *)retdata) );
  /*
  //  THIS IS AN ITERATION SEQUENCE. DO NOT ERASE.
	while ( _sort_key_cursor->get( &skey, &data, DB_NEXT | DB_MULTIPLE_KEY ) == 0 )
	  {
	    
	    for (DB_MULTIPLE_INIT(p, data.get_DBT());;) 
	      {
		
		DB_MULTIPLE_KEY_NEXT(p, data.get_DBT(), retkey, retklen, retdata, retdlen);
		if (p == NULL)
		  break;
		//cout << " Ret klen " << *((int *)retkey) << " retdlen " << *((int *)retdata) << endl;
		//printf("key: %.*s, data: %.*s\n",
		//(int)retklen, (char*)retkey, (int)retdlen, (char*)retdata);
		ctr2++;
	      }

	    cout << " Got SORT key " << *((int *)skey.get_data()) << endl;
	    cout << " Curr count " << ctr2 << endl;
	  }
	*/

  /*
	Dbt dkey, data; // Dbt is a key / data pair
	int ret; //,a, b;
	char *result = ( char* )malloc( sizeof(int) );

	
	memset(&dkey, 0, sizeof(dkey)); 
	memset(&data, 0, sizeof(data)); 

	data.set_data( &result );
	data.set_size( sizeof(int) );
	// BDB documentation neglects to mention that once you use your own
	// buffer you MUST set this parameter or things will break horribly.
	// thanks to a helpful post on a google newsgroup...
	data.set_ulen( sizeof(int) );

	dkey.set_data( key );
	dkey.set_size( size );

	try
	{
	  int before = StopWatch::ticks();
	  ret = iter->get( &dkey, &data, flags );
		int after = StopWatch::ticks();
		db_access+=(after-before)*StopWatch::secondsPerTick();
		//cout << _segNameDebug <<  " Got " << (after-before)*StopWatch::secondsPerTick() << " for " << db_access << endl;
	}
	catch(DbException& e) 
	{
		if (DEBUG) cout << "Exception " << e.what() << endl;
		// Throw exception here
		exit( 1 );
	}

	if ( ret < 0 )
	{
	  //cout << ret << "Nothing found for key ... " << key << endl;
	  return NULL;
	}
	

	//int a,b;
	//memcpy( &a, key, 4 );
	//memcpy( &b, data.get_data(), 4 );
	
	
	//cout << "Receiving  key " <<  b << " for given key " << a << endl;
	
	//if ( fake_key > 599874 )
	//return NULL;

	  //memcpy( result, &fake_key, sizeof(int) );
	//fake_key++;
	
	memcpy( result, data.get_data(), sizeof(int) );

	return ( int* )result;
  */
}
Ejemplo n.º 7
0
vector<Identity>::const_iterator
Freeze::EvictorIteratorI::nextBatch()
{
    DeactivateController::Guard 
        deactivateGuard(_store->evictor()->deactivateController());

    _batch.clear();

    if(!_more)
    {
        return _batch.end();
    }

    vector<EvictorElementPtr> evictorElements;
    evictorElements.reserve(_batchSize);
     
    Key firstKey = _key;

    CommunicatorPtr communicator = _store->communicator();
   
    try
    {
        for(;;)
        {
            _batch.clear();
            evictorElements.clear();
            
            Dbt dbKey;
            initializeOutDbt(_key, dbKey);

            Dbt dbValue;
            dbValue.set_flags(DB_DBT_USERMEM | DB_DBT_PARTIAL);
           
            Dbc* dbc = 0;
            try
            {
                //
                // Move to the first record
                // 
                u_int32_t flags = DB_NEXT;

                if(_initialized)
                {
                    //
                    // _key represents the next element not yet returned
                    // if it has been deleted, we want the one after
                    //
                    flags = DB_SET_RANGE;

                    //
                    // Will be used as input as well
                    //
                    dbKey.set_size(static_cast<u_int32_t>(firstKey.size()));
                }
                
                _store->db()->cursor(0, &dbc, 0);

                bool done = false;
                do
                {
                    for(;;)
                    {
                        try
                        {
                            //
                            // It is critical to set key size to key capacity before the
                            // get, as a resize that increases the size inserts 0
                            //
                            _key.resize(_key.capacity());

                            _more = (dbc->get(&dbKey, &dbValue, flags) == 0);
                            if(_more)
                            {
                                _key.resize(dbKey.get_size());
                                _initialized = true;

                                flags = DB_NEXT;
                    
                                Ice::Identity ident;
                                ObjectStore::unmarshal(ident, _key, communicator);
                                if(_batch.size() < _batchSize)
                                {
                                    _batch.push_back(ident);
                                }
                                else
                                {
                                    //
                                    // Keep the last element in _key
                                    //
                                    done = true;
                                }
                            }
                            break;
                        }
                        catch(const DbDeadlockException&)
                        {
                            throw;
                        }
                        catch(const DbException& dx)
                        {
                            handleDbException(dx, _key, dbKey, __FILE__, __LINE__);
                        }
                    }
                }
                while(!done && _more);

                Dbc* toClose = dbc;
                dbc = 0;
                toClose->close();
                break; // for (;;)
            }
            catch(const DbDeadlockException&)
            {
                if(dbc != 0)
                {
                    try
                    {
                        dbc->close();
                    }
                    catch(const DbDeadlockException&)
                    {
                        //
                        // Ignored
                        //
                    }
                }
                _key = firstKey;
                //
                // Retry
                //
            }
            catch(...)
            {
                if(dbc != 0)
                {
                    try
                    {
                        dbc->close();
                    }
                    catch(const DbDeadlockException&)
                    {
                        //
                        // Ignored
                        //
                    }
                }
                throw;
            }
        }
    }
    catch(const DbException& dx)
    {
        handleDbException(dx, __FILE__, __LINE__);
    }
    
    if(_batch.size() == 0)
    {
        return _batch.end();
    }
    else
    {
        return _batch.begin();
    }
}