/* * PROGRAM: JRD access method * MODULE: Attachment.cpp * DESCRIPTION: JRD Attachment class * * 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): ______________________________________. * */ #include "firebird.h" #include "../jrd/Attachment.h" #include "../jrd/Database.h" #include "../jrd/Function.h" #include "../jrd/nbak.h" #include "../jrd/trace/TraceManager.h" #include "../jrd/PreparedStatement.h" #include "../jrd/tra.h" #include "../jrd/intl.h" #include "../jrd/blb_proto.h" #include "../jrd/exe_proto.h" #include "../jrd/intl_proto.h" #include "../jrd/met_proto.h" #include "../jrd/extds/ExtDS.h" #include "../common/classes/fb_string.h" #include "../common/classes/MetaName.h" #include "../common/StatusArg.h" using namespace Jrd; using namespace Firebird; // static method Jrd::Attachment* Jrd::Attachment::create(Database* dbb) { MemoryPool* const pool = dbb->createPool(); try { Attachment* const attachment = FB_NEW(*pool) Attachment(pool, dbb); pool->setStatsGroup(attachment->att_memory_stats); return attachment; } catch (const Firebird::Exception&) { dbb->deletePool(pool); throw; } } // static method void Jrd::Attachment::destroy(Attachment* const attachment) { if (!attachment) return; thread_db* tdbb = JRD_get_thread_data(); jrd_tra* sysTransaction = attachment->getSysTransaction(); if (sysTransaction) { // unwind any active system requests while (sysTransaction->tra_requests) EXE_unwind(tdbb, sysTransaction->tra_requests); jrd_tra::destroy(NULL, sysTransaction); } Database* const dbb = attachment->att_database; MemoryPool* const pool = attachment->att_pool; Firebird::MemoryStats temp_stats; pool->setStatsGroup(temp_stats); delete attachment; dbb->deletePool(pool); } MemoryPool* Jrd::Attachment::createPool() { MemoryPool* const pool = MemoryPool::createPool(att_pool, att_memory_stats); att_pools.add(pool); return pool; } void Jrd::Attachment::deletePool(MemoryPool* pool) { if (pool) { size_t pos; if (att_pools.find(pool, pos)) att_pools.remove(pos); MemoryPool::deletePool(pool); } } bool Jrd::Attachment::backupStateWriteLock(thread_db* tdbb, SSHORT wait) { if (att_backup_state_counter++) return true; if (att_database->dbb_backup_manager->lockStateWrite(tdbb, wait)) return true; att_backup_state_counter--; return false; } void Jrd::Attachment::backupStateWriteUnLock(thread_db* tdbb) { if (--att_backup_state_counter == 0) att_database->dbb_backup_manager->unlockStateWrite(tdbb); } bool Jrd::Attachment::backupStateReadLock(thread_db* tdbb, SSHORT wait) { if (att_backup_state_counter++) return true; if (att_database->dbb_backup_manager->lockStateRead(tdbb, wait)) return true; att_backup_state_counter--; return false; } void Jrd::Attachment::backupStateReadUnLock(thread_db* tdbb) { if (--att_backup_state_counter == 0) att_database->dbb_backup_manager->unlockStateRead(tdbb); } Jrd::Attachment::Attachment(MemoryPool* pool, Database* dbb) : att_pool(pool), att_memory_stats(&dbb->dbb_memory_stats), att_database(dbb), att_requests(*pool), att_lock_owner_id(Database::getLockOwnerId()), att_backup_state_counter(0), att_stats(*pool), att_working_directory(*pool), att_filename(*pool), att_timestamp(Firebird::TimeStamp::getCurrentTimeStamp()), att_context_vars(*pool), ddlTriggersContext(*pool), att_network_protocol(*pool), att_remote_address(*pool), att_remote_process(*pool), att_dsql_cache(*pool), att_udf_pointers(*pool), att_ext_connection(NULL), att_ext_call_depth(0), att_trace_manager(FB_NEW(*att_pool) TraceManager(this)), att_interface(NULL), att_public_interface(NULL), att_functions(*pool), att_internal(*pool), att_dyn_req(*pool), att_charsets(*pool), att_charset_ids(*pool), att_pools(*pool) { att_internal.grow(irq_MAX); att_dyn_req.grow(drq_MAX); } Jrd::Attachment::~Attachment() { destroyIntlObjects(); delete att_trace_manager; while (att_pools.hasData()) deletePool(att_pools.pop()); // For normal attachments that happens in release_attachment(), // but for special ones like GC should be done also in dtor - // they do not (and should not) call release_attachment(). // It's no danger calling detachLocksFromAttachment() // once more here because it nulls att_long_locks. // AP 2007 detachLocksFromAttachment(); } Jrd::PreparedStatement* Jrd::Attachment::prepareStatement(thread_db* tdbb, jrd_tra* transaction, const string& text, Firebird::MemoryPool* pool) { pool = pool ? pool : tdbb->getDefaultPool(); return FB_NEW(*pool) PreparedStatement(tdbb, *pool, this, transaction, text, true); } Jrd::PreparedStatement* Jrd::Attachment::prepareStatement(thread_db* tdbb, jrd_tra* transaction, const PreparedStatement::Builder& builder, Firebird::MemoryPool* pool) { pool = pool ? pool : tdbb->getDefaultPool(); return FB_NEW(*pool) PreparedStatement(tdbb, *pool, this, transaction, builder, true); } PreparedStatement* Jrd::Attachment::prepareUserStatement(thread_db* tdbb, jrd_tra* transaction, const string& text, Firebird::MemoryPool* pool) { pool = pool ? pool : tdbb->getDefaultPool(); return FB_NEW(*pool) PreparedStatement(tdbb, *pool, this, transaction, text, false); } MetaName Jrd::Attachment::nameToMetaCharSet(thread_db* tdbb, const MetaName& name) { if (att_charset == CS_METADATA || att_charset == CS_NONE) return name; UCHAR buffer[MAX_SQL_IDENTIFIER_SIZE]; ULONG len = INTL_convert_bytes(tdbb, CS_METADATA, buffer, MAX_SQL_IDENTIFIER_LEN, att_charset, (const BYTE*) name.c_str(), name.length(), ERR_post); buffer[len] = '\0'; return MetaName((const char*) buffer); } MetaName Jrd::Attachment::nameToUserCharSet(thread_db* tdbb, const MetaName& name) { if (att_charset == CS_METADATA || att_charset == CS_NONE) return name; UCHAR buffer[MAX_SQL_IDENTIFIER_SIZE]; ULONG len = INTL_convert_bytes(tdbb, att_charset, buffer, MAX_SQL_IDENTIFIER_LEN, CS_METADATA, (const BYTE*) name.c_str(), name.length(), ERR_post); buffer[len] = '\0'; return MetaName((const char*) buffer); } string Jrd::Attachment::stringToMetaCharSet(thread_db* tdbb, const string& str, const char* charSet) { USHORT charSetId = att_charset; if (charSet) { if (!MET_get_char_coll_subtype(tdbb, &charSetId, (const UCHAR*) charSet, strlen(charSet))) (Arg::Gds(isc_charset_not_found) << Arg::Str(charSet)).raise(); } if (charSetId == CS_METADATA || charSetId == CS_NONE) return str; HalfStaticArray buffer(str.length() * sizeof(ULONG)); ULONG len = INTL_convert_bytes(tdbb, CS_METADATA, buffer.begin(), buffer.getCapacity(), charSetId, (const BYTE*) str.c_str(), str.length(), ERR_post); return string((char*) buffer.begin(), len); } string Jrd::Attachment::stringToUserCharSet(thread_db* tdbb, const string& str) { if (att_charset == CS_METADATA || att_charset == CS_NONE) return str; HalfStaticArray buffer(str.length() * sizeof(ULONG)); ULONG len = INTL_convert_bytes(tdbb, att_charset, buffer.begin(), buffer.getCapacity(), CS_METADATA, (const BYTE*) str.c_str(), str.length(), ERR_post); return string((char*) buffer.begin(), len); } // We store in CS_METADATA. void Jrd::Attachment::storeMetaDataBlob(thread_db* tdbb, jrd_tra* transaction, bid* blobId, const string& text, USHORT fromCharSet) { UCharBuffer bpb; BLB_gen_bpb(isc_blob_text, isc_blob_text, fromCharSet, CS_METADATA, bpb); blb* blob = BLB_create2(tdbb, transaction, blobId, bpb.getCount(), bpb.begin()); try { BLB_put_data(tdbb, blob, (const UCHAR*) text.c_str(), text.length()); } catch (const Exception&) { BLB_close(tdbb, blob); throw; } BLB_close(tdbb, blob); } // We store raw stuff; don't attempt to translate. void Jrd::Attachment::storeBinaryBlob(thread_db* tdbb, jrd_tra* transaction, bid* blobId, const ByteChunk& chunk) { blb* blob = BLB_create2(tdbb, transaction, blobId, 0, NULL); try { BLB_put_data(tdbb, blob, chunk.data, chunk.length); } catch (const Exception&) { BLB_close(tdbb, blob); throw; } BLB_close(tdbb, blob); } void Jrd::Attachment::cancelExternalConnection(thread_db* tdbb) { if (att_ext_connection) { att_ext_connection->cancelExecution(tdbb); } } void Jrd::Attachment::detachLocksFromAttachment() { /************************************** * * d e t a c h L o c k s F r o m A t t a c h m e n t * ************************************** * * Functional description * Bug #7781, need to null out the attachment pointer of all locks which * were hung off this attachment block, to ensure that the attachment * block doesn't get dereferenced after it is released * **************************************/ if (!att_long_locks) return; Sync lckSync(&att_database->dbb_lck_sync, "Attachment::detachLocksFromAttachment"); lckSync.lock(SYNC_EXCLUSIVE); Lock* long_lock = att_long_locks; while (long_lock) { Lock* next = long_lock->lck_next; long_lock->lck_attachment = NULL; long_lock->lck_next = NULL; long_lock->lck_prior = NULL; long_lock = next; } att_long_locks = NULL; } // Find an inactive incarnation of a system request. If necessary, clone it. jrd_req* Jrd::Attachment::findSystemRequest(thread_db* tdbb, USHORT id, USHORT which) { static const int MAX_RECURSION = 100; // If the request hasn't been compiled or isn't active, there're nothing to do. //Database::CheckoutLockGuard guard(this, dbb_cmp_clone_mutex); fb_assert(which == IRQ_REQUESTS || which == DYN_REQUESTS); JrdStatement* statement = (which == IRQ_REQUESTS ? att_internal[id] : att_dyn_req[id]); if (!statement) return NULL; // Look for requests until we find one that is available. for (int n = 0;; ++n) { if (n > MAX_RECURSION) { ERR_post(Arg::Gds(isc_no_meta_update) << Arg::Gds(isc_req_depth_exceeded) << Arg::Num(MAX_RECURSION)); // Msg363 "request depth exceeded. (Recursive definition?)" } jrd_req* clone = statement->getRequest(tdbb, n); if (!(clone->req_flags & (req_active | req_reserved))) { clone->req_flags |= req_reserved; return clone; } } } void Jrd::Attachment::releaseLocks(thread_db* tdbb) { // Go through relations and indices and release // all existence locks that might have been taken. vec* rvector = att_relations; if (rvector) { vec::iterator ptr, end; for (ptr = rvector->begin(), end = rvector->end(); ptr < end; ++ptr) { jrd_rel* relation = *ptr; if (relation) { if (relation->rel_existence_lock) { LCK_release(tdbb, relation->rel_existence_lock); relation->rel_flags |= REL_check_existence; relation->rel_use_count = 0; } if (relation->rel_partners_lock) { LCK_release(tdbb, relation->rel_partners_lock); relation->rel_flags |= REL_check_partners; } if (relation->rel_rescan_lock) { LCK_release(tdbb, relation->rel_rescan_lock); relation->rel_flags &= ~REL_scanned; } for (IndexLock* index = relation->rel_index_locks; index; index = index->idl_next) { if (index->idl_lock) { index->idl_count = 0; LCK_release(tdbb, index->idl_lock); } } } } } // Release all procedure existence locks that might have been taken. vec* pvector = att_procedures; if (pvector) { vec::iterator pptr, pend; for (pptr = pvector->begin(), pend = pvector->end(); pptr < pend; ++pptr) { jrd_prc* procedure = *pptr; if (procedure) { if (procedure->prc_existence_lock) { LCK_release(tdbb, procedure->prc_existence_lock); procedure->prc_flags |= PRC_check_existence; procedure->prc_use_count = 0; } } } } // Release all function existence locks that might have been taken. for (Function** iter = att_functions.begin(); iter < att_functions.end(); ++iter) { Function* const function = *iter; if (function) function->releaseLocks(tdbb); } // Release collation existence locks. releaseIntlObjects(); // And release the system requests. for (JrdStatement** itr = att_internal.begin(); itr != att_internal.end(); ++itr) { if (*itr) (*itr)->release(tdbb); } for (JrdStatement** itr = att_dyn_req.begin(); itr != att_dyn_req.end(); ++itr) { if (*itr) (*itr)->release(tdbb); } }