2008-02-14 13:24:27 +01:00
|
|
|
/*
|
|
|
|
* PROGRAM: JRD access method
|
|
|
|
* MODULE: Database.cpp
|
|
|
|
* DESCRIPTION: Common descriptions
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Interbase 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.Inprise.com/IPL.html
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an
|
|
|
|
* "AS IS" basis, 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 Inprise Corporation
|
|
|
|
* and its predecessors. Portions created by Inprise Corporation are
|
|
|
|
* Copyright (C) Inprise Corporation.
|
|
|
|
*
|
|
|
|
* All Rights Reserved.
|
|
|
|
* Contributor(s): ______________________________________.
|
|
|
|
*
|
2008-03-20 17:42:29 +01:00
|
|
|
* Sean Leyne
|
2008-02-14 13:24:27 +01:00
|
|
|
* Claudio Valderrama C.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "firebird.h"
|
2008-10-14 11:10:36 +02:00
|
|
|
// Definition of block types for data allocation in JRD
|
|
|
|
#include "../include/fb_blk.h"
|
|
|
|
|
2008-02-14 13:24:27 +01:00
|
|
|
#include "../jrd/ibase.h"
|
|
|
|
#include "../jrd/ods.h"
|
2008-10-14 11:10:36 +02:00
|
|
|
#include "../jrd/lck.h"
|
2008-02-14 13:24:27 +01:00
|
|
|
#include "../jrd/Database.h"
|
2010-01-23 17:28:40 +01:00
|
|
|
#include "../jrd/nbak.h"
|
2008-02-14 13:24:27 +01:00
|
|
|
#include "../jrd/tra.h"
|
2008-10-14 11:10:36 +02:00
|
|
|
#include "../jrd/lck_proto.h"
|
2012-05-31 18:53:42 +02:00
|
|
|
#include "../jrd/CryptoManager.h"
|
2008-02-15 09:46:08 +01:00
|
|
|
#include "../jrd/os/pio_proto.h"
|
2008-02-14 13:24:27 +01:00
|
|
|
|
2008-10-10 17:58:05 +02:00
|
|
|
// Thread data block
|
2010-10-12 10:02:57 +02:00
|
|
|
#include "../common/ThreadData.h"
|
2008-02-14 13:24:27 +01:00
|
|
|
|
|
|
|
// recursive mutexes
|
|
|
|
#include "../common/thd.h"
|
|
|
|
|
2010-04-19 00:19:11 +02:00
|
|
|
using namespace Firebird;
|
|
|
|
|
2008-02-14 13:24:27 +01:00
|
|
|
namespace Jrd
|
|
|
|
{
|
|
|
|
bool Database::onRawDevice() const
|
|
|
|
{
|
|
|
|
#ifdef SUPPORT_RAW_DEVICES
|
|
|
|
return PIO_on_raw_device(dbb_filename);
|
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2011-05-10 03:12:14 +02:00
|
|
|
string Database::getUniqueFileId() const
|
2009-01-28 13:27:18 +01:00
|
|
|
{
|
2009-01-28 14:02:59 +01:00
|
|
|
const PageSpace* const pageSpace = dbb_page_manager.findPageSpace(DB_PAGE_SPACE);
|
2009-01-28 13:27:18 +01:00
|
|
|
|
2011-05-10 03:12:14 +02:00
|
|
|
UCharBuffer buffer;
|
2009-01-28 14:02:59 +01:00
|
|
|
PIO_get_unique_file_id(pageSpace->file, buffer);
|
2009-01-28 13:27:18 +01:00
|
|
|
|
2011-05-10 03:12:14 +02:00
|
|
|
string file_id;
|
2009-02-08 12:23:46 +01:00
|
|
|
char* s = file_id.getBuffer(2 * buffer.getCount());
|
2009-01-28 14:02:59 +01:00
|
|
|
for (size_t i = 0; i < buffer.getCount(); i++)
|
|
|
|
{
|
2009-02-08 12:23:46 +01:00
|
|
|
sprintf(s, "%02x", (int) buffer[i]);
|
|
|
|
s += 2;
|
2009-01-28 13:27:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return file_id;
|
|
|
|
}
|
|
|
|
|
2008-02-14 13:24:27 +01:00
|
|
|
Database::~Database()
|
|
|
|
{
|
2013-08-06 11:37:44 +02:00
|
|
|
{ // scope
|
|
|
|
SyncLockGuard guard(&dbb_sortbuf_sync, SYNC_EXCLUSIVE, "Database::~Database");
|
|
|
|
|
|
|
|
while (dbb_sort_buffers.hasData())
|
|
|
|
delete[] dbb_sort_buffers.pop();
|
|
|
|
}
|
|
|
|
|
|
|
|
{ // scope
|
2011-05-10 03:12:14 +02:00
|
|
|
SyncLockGuard guard(&dbb_pools_sync, SYNC_EXCLUSIVE, "Database::~Database");
|
|
|
|
|
2011-05-09 12:15:19 +02:00
|
|
|
fb_assert(dbb_pools[0] == dbb_permanent);
|
2011-05-10 03:12:14 +02:00
|
|
|
|
2011-05-09 12:15:19 +02:00
|
|
|
for (size_t i = 1; i < dbb_pools.getCount(); ++i)
|
|
|
|
MemoryPool::deletePool(dbb_pools[i]);
|
2008-02-14 13:24:27 +01:00
|
|
|
}
|
2008-09-04 11:58:20 +02:00
|
|
|
|
2009-01-28 13:27:18 +01:00
|
|
|
delete dbb_monitoring_data;
|
2010-01-23 17:28:40 +01:00
|
|
|
delete dbb_backup_manager;
|
2012-05-31 18:53:42 +02:00
|
|
|
delete dbb_crypto_manager;
|
2009-01-28 13:27:18 +01:00
|
|
|
|
2012-12-18 14:54:18 +01:00
|
|
|
while (dbb_active_threads)
|
|
|
|
{
|
|
|
|
thread_db* tdbb = dbb_active_threads;
|
|
|
|
tdbb->deactivate();
|
|
|
|
tdbb->setDatabase(NULL);
|
|
|
|
}
|
|
|
|
|
2012-12-14 18:59:02 +01:00
|
|
|
fb_assert(!locked());
|
2009-01-28 13:27:18 +01:00
|
|
|
// This line decrements the usage counter and may cause the destructor to be called.
|
|
|
|
// It should happen with the dbb_sync unlocked.
|
2010-11-08 11:21:04 +01:00
|
|
|
LockManager::destroy(dbb_lock_mgr);
|
|
|
|
EventManager::destroy(dbb_event_mgr);
|
2008-02-14 13:24:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Database::deletePool(MemoryPool* pool)
|
|
|
|
{
|
2008-04-17 09:52:52 +02:00
|
|
|
if (pool)
|
2008-02-14 13:24:27 +01:00
|
|
|
{
|
|
|
|
{
|
2011-05-10 03:12:14 +02:00
|
|
|
SyncLockGuard guard(&dbb_pools_sync, SYNC_EXCLUSIVE, "Database::deletePool");
|
2011-05-09 12:15:19 +02:00
|
|
|
size_t pos;
|
2011-05-10 03:12:14 +02:00
|
|
|
|
2011-05-09 12:15:19 +02:00
|
|
|
if (dbb_pools.find(pool, pos))
|
|
|
|
dbb_pools.remove(pos);
|
2008-02-14 13:24:27 +01:00
|
|
|
}
|
2008-04-17 09:52:52 +02:00
|
|
|
|
|
|
|
MemoryPool::deletePool(pool);
|
2008-02-14 13:24:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-29 13:32:55 +02:00
|
|
|
int Database::blocking_ast_sweep(void* ast_object)
|
|
|
|
{
|
|
|
|
Database* dbb = static_cast<Database*>(ast_object);
|
|
|
|
AsyncContextHolder tdbb(dbb, FB_FUNCTION);
|
|
|
|
|
2013-07-07 01:11:13 +02:00
|
|
|
if ((dbb->dbb_flags & DBB_sweep_starting) && !(dbb->dbb_flags & DBB_sweep_in_progress))
|
2013-06-29 13:32:55 +02:00
|
|
|
{
|
|
|
|
dbb->dbb_flags &= ~DBB_sweep_starting;
|
|
|
|
LCK_release(tdbb, dbb->dbb_sweep_lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
Lock* Database::createSweepLock(thread_db* tdbb)
|
|
|
|
{
|
2013-07-07 01:11:13 +02:00
|
|
|
if (!dbb_sweep_lock)
|
2013-06-29 13:32:55 +02:00
|
|
|
{
|
|
|
|
dbb_sweep_lock = FB_NEW_RPT(*dbb_permanent, 0) Lock(tdbb, 0, LCK_sweep);
|
|
|
|
dbb_sweep_lock->lck_ast = blocking_ast_sweep;
|
|
|
|
dbb_sweep_lock->lck_object = this;
|
|
|
|
}
|
|
|
|
return dbb_sweep_lock;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Database::allowSweepThread(thread_db* tdbb)
|
|
|
|
{
|
|
|
|
if (readOnly())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
Jrd::Attachment* const attachment = tdbb->getAttachment();
|
|
|
|
if (attachment->att_flags & ATT_no_cleanup)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
AtomicCounter::counter_type old = dbb_flags;
|
|
|
|
if ((old & (DBB_sweep_in_progress | DBB_sweep_starting)) || (dbb_ast_flags & DBB_shutdown))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (dbb_flags.compareExchange(old, old | DBB_sweep_starting))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
createSweepLock(tdbb);
|
|
|
|
if (!LCK_lock(tdbb, dbb_sweep_lock, LCK_EX, LCK_NO_WAIT))
|
|
|
|
{
|
|
|
|
// clear lock error from status vector
|
|
|
|
fb_utils::init_status(tdbb->tdbb_status_vector);
|
|
|
|
|
|
|
|
dbb_flags &= ~DBB_sweep_starting;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Database::allowSweepRun(thread_db* tdbb)
|
|
|
|
{
|
|
|
|
if (readOnly())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
Jrd::Attachment* const attachment = tdbb->getAttachment();
|
|
|
|
if (attachment->att_flags & ATT_no_cleanup)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
AtomicCounter::counter_type old = dbb_flags;
|
2013-09-18 20:38:36 +02:00
|
|
|
if (old & DBB_sweep_in_progress)
|
2013-06-29 13:32:55 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
if (dbb_flags.compareExchange(old, old | DBB_sweep_in_progress))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(dbb_flags & DBB_sweep_starting))
|
|
|
|
{
|
|
|
|
createSweepLock(tdbb);
|
|
|
|
if (!LCK_lock(tdbb, dbb_sweep_lock, LCK_EX, -1))
|
|
|
|
{
|
|
|
|
// clear lock error from status vector
|
|
|
|
fb_utils::init_status(tdbb->tdbb_status_vector);
|
|
|
|
|
|
|
|
dbb_flags &= ~DBB_sweep_in_progress;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
dbb_flags &= ~DBB_sweep_starting;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Database::clearSweepFlags(thread_db* tdbb)
|
|
|
|
{
|
|
|
|
if (!(dbb_flags & (DBB_sweep_starting | DBB_sweep_in_progress)))
|
|
|
|
return;
|
|
|
|
|
2013-07-07 01:11:13 +02:00
|
|
|
if (dbb_sweep_lock)
|
2013-06-29 13:32:55 +02:00
|
|
|
LCK_release(tdbb, dbb_sweep_lock);
|
2013-07-07 01:11:13 +02:00
|
|
|
|
2013-06-29 13:32:55 +02:00
|
|
|
dbb_flags &= ~(DBB_sweep_in_progress | DBB_sweep_starting);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-02-15 08:16:57 +01:00
|
|
|
Database::SharedCounter::SharedCounter()
|
|
|
|
{
|
|
|
|
memset(m_counters, 0, sizeof(m_counters));
|
|
|
|
}
|
|
|
|
|
2011-02-25 08:32:36 +01:00
|
|
|
Database::SharedCounter::~SharedCounter()
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < TOTAL_ITEMS; i++)
|
|
|
|
{
|
|
|
|
delete m_counters[i].lock;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-15 08:16:57 +01:00
|
|
|
void Database::SharedCounter::shutdown(thread_db* tdbb)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < TOTAL_ITEMS; i++)
|
|
|
|
{
|
|
|
|
if (m_counters[i].lock)
|
|
|
|
LCK_release(tdbb, m_counters[i].lock);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SLONG Database::SharedCounter::generate(thread_db* tdbb, ULONG space, ULONG prefetch)
|
|
|
|
{
|
|
|
|
fb_assert(space < TOTAL_ITEMS);
|
|
|
|
ValueCache* const counter = &m_counters[space];
|
|
|
|
Database* const dbb = tdbb->getDatabase();
|
|
|
|
|
2011-05-09 12:15:19 +02:00
|
|
|
SyncLockGuard guard(&dbb->dbb_sh_counter_sync, SYNC_EXCLUSIVE, "Database::SharedCounter::generate");
|
|
|
|
|
2011-02-15 08:16:57 +01:00
|
|
|
if (!counter->lock)
|
|
|
|
{
|
2012-06-21 17:37:38 +02:00
|
|
|
Lock* const lock = FB_NEW_RPT(*dbb->dbb_permanent, 0)
|
|
|
|
Lock(tdbb, sizeof(SLONG), LCK_shared_counter, counter, blockingAst);
|
2011-02-15 08:16:57 +01:00
|
|
|
counter->lock = lock;
|
|
|
|
lock->lck_key.lck_long = space;
|
|
|
|
LCK_lock(tdbb, lock, LCK_PW, LCK_WAIT);
|
|
|
|
|
|
|
|
counter->curVal = 1;
|
|
|
|
counter->maxVal = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (counter->curVal > counter->maxVal)
|
|
|
|
{
|
2012-06-21 17:37:38 +02:00
|
|
|
LCK_convert(tdbb, counter->lock, LCK_PW, LCK_WAIT);
|
2011-02-15 08:16:57 +01:00
|
|
|
|
|
|
|
counter->curVal = LCK_read_data(tdbb, counter->lock);
|
|
|
|
|
|
|
|
if (!counter->curVal)
|
|
|
|
{
|
|
|
|
// zero IDs are somewhat special, so let's better skip them
|
|
|
|
counter->curVal = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
counter->maxVal = counter->curVal + prefetch - 1;
|
|
|
|
LCK_write_data(tdbb, counter->lock, counter->maxVal + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return counter->curVal++;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Database::SharedCounter::blockingAst(void* ast_object)
|
2008-10-14 11:10:36 +02:00
|
|
|
{
|
2011-02-15 08:16:57 +01:00
|
|
|
ValueCache* const counter = static_cast<ValueCache*>(ast_object);
|
|
|
|
fb_assert(counter && counter->lock);
|
|
|
|
Database* const dbb = counter->lock->lck_dbb;
|
2008-10-14 11:10:36 +02:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2012-12-14 18:59:02 +01:00
|
|
|
AsyncContextHolder tdbb(dbb, FB_FUNCTION);
|
2011-05-09 12:15:19 +02:00
|
|
|
|
|
|
|
SyncLockGuard guard(&dbb->dbb_sh_counter_sync, SYNC_EXCLUSIVE, "Database::blockingAstSharedCounter");
|
2008-10-14 11:10:36 +02:00
|
|
|
|
2011-02-15 08:16:57 +01:00
|
|
|
LCK_downgrade(tdbb, counter->lock);
|
2008-10-14 11:10:36 +02:00
|
|
|
}
|
2011-05-10 03:12:14 +02:00
|
|
|
catch (const Exception&)
|
2008-10-14 11:10:36 +02:00
|
|
|
{} // no-op
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2008-03-19 17:19:56 +01:00
|
|
|
} // namespace
|