8
0
mirror of https://github.com/FirebirdSQL/firebird.git synced 2025-01-30 19:23:03 +01:00
firebird-mirror/src/jrd/GarbageCollector.cpp

293 lines
6.4 KiB
C++
Raw Normal View History

/*
* The contents of this file are subject to the Initial
* Developer's Public License Version 1.0 (the "License");
* you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
* http://www.ibphoenix.com/main.nfs?a=ibphoenix&page=ibp_idpl.
*
* Software distributed under the License is distributed AS IS,
* WITHOUT WARRANTY OF ANY KIND, either express or implied.
* See the License for the specific language governing rights
* and limitations under the License.
*
* The Original Code was created by Vlad Khorsun
* for the Firebird Open Source RDBMS project.
*
* Copyright (c) 2011 Vlad Khorsun <hvlad@users.sourceforge.net>
* and all contributors signed below.
*
* All Rights Reserved.
* Contributor(s): ______________________________________.
*/
#include "../common/classes/alloc.h"
#include "../jrd/GarbageCollector.h"
#include "../jrd/tra.h"
using namespace Jrd;
using namespace Firebird;
namespace Jrd {
2011-05-27 18:18:39 +02:00
void GarbageCollector::RelationData::clear()
{
TranData::ConstAccessor accessor(&m_tranData);
if (accessor.getFirst())
2011-05-27 18:18:39 +02:00
{
2011-05-28 04:05:45 +02:00
do
{
delete accessor.current()->second;
2011-05-27 18:18:39 +02:00
} while (accessor.getNext());
}
m_tranData.clear();
}
TraNumber GarbageCollector::RelationData::addPage(const ULONG pageno, const TraNumber tranid)
{
TraNumber minTraID = MAX_TRA_NUMBER;
TranData::ConstAccessor accessor(&m_tranData);
if (accessor.getFirst())
minTraID = accessor.current()->first;
// look if given page number is already set at given tx bitmap
PageBitmap* bm = NULL;
const bool bmExists = m_tranData.get(tranid, bm);
if (bm && bm->test(pageno))
return minTraID;
// search for given page at other transactions bitmaps
// if found at older tx - we are done, just return
2011-06-24 08:34:16 +02:00
// if found at younger tx - clear it as page should be set at oldest tx (our)
if (minTraID != MAX_TRA_NUMBER)
2011-05-27 18:18:39 +02:00
{
do
{
const TranBitMap* item = accessor.current();
if (item->first <= tranid)
{
if (item->second->test(pageno))
return minTraID;
}
else
{
if (item->second->clear(pageno))
break;
2011-05-28 04:05:45 +02:00
}
} while(accessor.getNext());
2011-05-27 18:18:39 +02:00
}
2011-06-24 08:34:16 +02:00
// add page to our tx bitmap
PBM_SET(&m_pool, &bm, pageno);
if (!bmExists)
{
m_tranData.put(tranid, bm);
if (minTraID > tranid)
minTraID = tranid;
}
return minTraID;
}
void GarbageCollector::RelationData::getPageBitmap(const TraNumber oldest_snapshot, PageBitmap** sbm)
{
TranData::Accessor accessor(&m_tranData);
while (accessor.getFirst())
{
TranBitMap* item = accessor.current();
if (item->first >= oldest_snapshot)
break;
PageBitmap* bm_tran = item->second;
PageBitmap** bm_or = PageBitmap::bit_or(sbm, &bm_tran);
2011-05-27 18:18:39 +02:00
if (*bm_or == item->second)
{
bm_tran = *sbm;
*sbm = item->second;
item->second = bm_tran;
}
2011-05-27 18:18:39 +02:00
delete item->second;
m_tranData.remove(item->first);
}
}
void GarbageCollector::RelationData::swept(const TraNumber oldest_snapshot)
{
TranData::Accessor accessor(&m_tranData);
while (accessor.getFirst())
{
TranBitMap* item = accessor.current();
if (item->first >= oldest_snapshot)
break;
delete item->second;
m_tranData.remove(item->first);
}
}
TraNumber GarbageCollector::RelationData::minTranID() const
{
TranData::ConstAccessor accessor(&m_tranData);
if (accessor.getFirst())
return accessor.current()->first;
2011-06-26 20:48:00 +02:00
2011-06-24 08:34:16 +02:00
return MAX_TRA_NUMBER;
}
GarbageCollector::~GarbageCollector()
{
SyncLockGuard exGuard(&m_sync, SYNC_EXCLUSIVE, "GarbageCollector::~GarbageCollector");
2011-05-27 18:18:39 +02:00
2014-07-17 20:48:46 +02:00
for (FB_SIZE_T pos = 0; pos < m_relations.getCount(); pos++)
{
RelationData* relData = m_relations[pos];
Sync sync(&relData->m_sync, "GarbageCollector::~GarbageCollector");
sync.lock(SYNC_EXCLUSIVE);
m_relations[pos] = NULL;
sync.unlock();
delete relData;
}
m_relations.clear();
}
TraNumber GarbageCollector::addPage(const USHORT relID, const ULONG pageno, const TraNumber tranid)
{
Sync syncGC(&m_sync, "GarbageCollector::addPage");
RelationData* relData = getRelData(syncGC, relID, true);
SyncLockGuard syncData(&relData->m_sync, SYNC_EXCLUSIVE, "GarbageCollector::addPage");
syncGC.unlock();
return relData->addPage(pageno, tranid);
}
bool GarbageCollector::getPageBitmap(const TraNumber oldest_snapshot, USHORT &relID, PageBitmap **sbm)
{
*sbm = NULL;
SyncLockGuard shGuard(&m_sync, SYNC_EXCLUSIVE, "GarbageCollector::getPageBitmap");
if (m_relations.isEmpty())
{
m_nextRelID = 0;
return false;
}
2014-07-17 20:48:46 +02:00
FB_SIZE_T pos;
2011-05-27 18:18:39 +02:00
if (!m_relations.find(m_nextRelID, pos) && (pos == m_relations.getCount()))
pos = 0;
for (; pos < m_relations.getCount(); pos++)
{
RelationData* relData = m_relations[pos];
SyncLockGuard syncData(&relData->m_sync, SYNC_EXCLUSIVE, "GarbageCollector::getPageBitmap");
2011-05-27 18:18:39 +02:00
relData->getPageBitmap(oldest_snapshot, sbm);
2011-05-27 18:18:39 +02:00
if (*sbm)
{
relID = relData->getRelID();
m_nextRelID = relID + 1;
return true;
}
}
m_nextRelID = 0;
return false;
}
void GarbageCollector::removeRelation(const USHORT relID)
{
Sync syncGC(&m_sync, "GarbageCollector::removeRelation");
syncGC.lock(SYNC_EXCLUSIVE);
2011-05-28 04:05:45 +02:00
2014-07-17 20:48:46 +02:00
FB_SIZE_T pos;
if (!m_relations.find(relID, pos))
return;
2011-05-28 04:05:45 +02:00
RelationData* relData = m_relations[pos];
Sync syncData(&relData->m_sync, "GarbageCollector::removeRelation");
syncData.lock(SYNC_EXCLUSIVE);
2011-05-28 04:05:45 +02:00
m_relations.remove(pos);
syncGC.unlock();
syncData.unlock();
delete relData;
}
void GarbageCollector::sweptRelation(const TraNumber oldest_snapshot, const USHORT relID)
{
Sync syncGC(&m_sync, "GarbageCollector::sweptRelation");
2011-05-28 04:05:45 +02:00
RelationData* relData = getRelData(syncGC, relID, false);
if (relData)
{
SyncLockGuard syncData(&relData->m_sync, SYNC_EXCLUSIVE, "GarbageCollector::sweptRelation");
2011-05-28 04:05:45 +02:00
syncGC.unlock();
relData->swept(oldest_snapshot);
}
}
TraNumber GarbageCollector::minTranID(const USHORT relID)
{
Sync syncGC(&m_sync, "GarbageCollector::minTranID");
RelationData* relData = getRelData(syncGC, relID, false);
if (relData)
{
SyncLockGuard syncData(&relData->m_sync, SYNC_SHARED, "GarbageCollector::minTranID");
2011-05-28 04:05:45 +02:00
syncGC.unlock();
return relData->minTranID();
}
return MAX_TRA_NUMBER;
}
2011-05-27 18:18:39 +02:00
GarbageCollector::RelationData* GarbageCollector::getRelData(Sync &sync, const USHORT relID,
bool allowCreate)
{
2014-07-17 20:48:46 +02:00
FB_SIZE_T pos;
sync.lock(SYNC_SHARED);
if (!m_relations.find(relID, pos))
{
if (!allowCreate)
return NULL;
sync.unlock();
sync.lock(SYNC_EXCLUSIVE);
if (!m_relations.find(relID, pos))
{
m_relations.insert(pos, FB_NEW(m_pool) RelationData(m_pool, relID));
}
}
return m_relations[pos];
}
} // namespace Jrd