///////////////////////////////////////////////////////////////////////////////
// FindRoomForData
///////////////////////////////////////////////////////////////////////////////
int cBlockRecordFile::FindRoomForData(int32 dataSize) //throw (eArchive)
{
    ASSERT((dataSize > 0) && (dataSize <= cBlockRecordArray::MAX_DATA_SIZE));
    ASSERT(mbOpen);
#ifdef _BLOCKFILE_DEBUG
    AssertValid();
#endif
    cDebug d("cBlockRecordFile::FindRoomForData");

    // first, try the last added to block...
    //
    d.TraceDetail("Looking for room for %d bytes; first trying mLastAddedTo (%d)\n", dataSize, mLastAddedTo);
    if (mLastAddedTo >= 0)
    {
        util_InitBlockArray(mvBlocks[mLastAddedTo]);
        if (mvBlocks[mLastAddedTo].GetAvailableSpace() >= dataSize)
        {
            d.TraceDetail("---Found room in block %d\n", mLastAddedTo);
            return mLastAddedTo;
        }
    }
    //
    // ok, I guess we will have to iterate through all the blocks...
    //
    BlockArray::iterator it;
    int                  cnt = 0;
    for (it = mvBlocks.begin(); it != mvBlocks.end(); ++it, ++cnt)
    {
        util_InitBlockArray(*it);
        if (it->GetAvailableSpace() >= dataSize)
        {
            d.TraceDetail("---Found room in block %d\n", cnt);
            return cnt;
        }
    }
    //
    // if we got here, then we need to add a new block
    //
    d.TraceDetail("---We need to add new block(%d)\n", cnt);
    mBlockFile.CreateBlock();
    ASSERT((mBlockFile.GetNumBlocks() == (mvBlocks.size() + 1)) && (mvBlocks.size() == cnt));
    mvBlocks.push_back(cBlockRecordArray(&mBlockFile, cnt));
    mvBlocks.back().InitNewBlock();

    ASSERT(mvBlocks.back().GetAvailableSpace() >= dataSize);

    return cnt;
}
///////////////////////////////////////////////////////////////////////////////
// IsValidAddr
///////////////////////////////////////////////////////////////////////////////
bool cBlockRecordFile::IsValidAddr( cBlockRecordFile::tAddr addr ) //throw (eArchive)
{
    ASSERT( mbOpen );

    if( (addr.mBlockNum < 0) || (addr.mBlockNum >= mBlockFile.GetNumBlocks()) )
        return false;
    
    util_InitBlockArray( mvBlocks[ addr.mBlockNum ] );

    return ( mvBlocks[ addr.mBlockNum ].IsItemValid( addr.mIndex ) );
}
示例#3
0
////////////////////////////////////////////////////////////////////////////////////////////////
// MapQuantumDatabase --
//
// Our objective is to map all the information that the database is currently storing.  It
// may be necessary to do some hacking about with the database object in order to expose
// all the information necessary to accomplish this.
//
// Addition: 2/15/99: We want to print out statistics on the current state of the block
//	file, so let's do it here ( since we're traversing it to map the addresses, anyway ).
//////////////////////////////////////////////////////////////////////////////////////////////
void cDbDebug::MapQuantumDatabase( cDebugHierDb& db )
{
	//mirroring the implementation in blockrecordfile.h:
	typedef std::vector<cBlockRecordArray>	BlockArray;

	//Using the two added methods to gain access to the low-level implementation of the
	//quantum database.
	BlockArray* pBlkArray =		db.myGetBlockArray();

	// Print some statistics on the blockfile if we're in debug mode:
#ifdef _BLOCKFILE_DEBUG
	db.myGetBlockFile()->TraceContents();
#endif
	// Prints as much information about the current state of the block file as can be
	// assessed.

	// time to iterate over the database:
	std::vector< cBlockRecordArray >::iterator i;
	int count = 0;

	for( i = (*pBlkArray).begin(); i != (*pBlkArray).end(); ++i, ++count )
	{
		util_InitBlockArray( *i );
		//This is necessary to make sure that each block is initialized as we iterate
		//over the block array.
		//
		// Print out statistics on this block, if we're in debug mode:
#ifdef _BLOCKFILE_DEBUG
		i->TraceContents();
#endif


		//Search through all the indexes and determine which of them references valid
		//information.  Store these <int, int> pairs ( the first int being the value of
		//count....
		for( int j = 0; j <= i->GetNumItems(); ++j )
		{
			if( i->IsItemValid( j ) )
			{
				//
				// We found a valid node in the database, so store it in the map.
				//
				mpData->mDbMap.insert( cDbDebug_i::hierDbMap::value_type( std::pair<int, int> (count, j) , 0 ));
			}
			// if not, just don't store it.  The index is no longer valid.
		}
	}	
}