8
0
mirror of https://github.com/FirebirdSQL/firebird.git synced 2025-01-26 08:03:03 +01:00
firebird-mirror/src/jrd/pcmet.epp
hvlad 7bc64eef14 Fixed CORE-879 :
Dependencies are not cleared when creation of expression index fails
2006-07-28 21:12:24 +00:00

338 lines
8.9 KiB
Plaintext

/*
* PROGRAM: JRD Access Method
* MODULE: pcmet.epp
* DESCRIPTION: Meta data for expression indices
*
* 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 <string.h>
#include "../jrd/common.h"
#include "../jrd/ibase.h"
#include "../jrd/jrd.h"
#include "../jrd/irq.h"
#include "../jrd/tra.h"
#include "../jrd/val.h"
#include "../jrd/ods.h"
#include "../jrd/btr.h"
#include "../jrd/all.h"
#include "../jrd/req.h"
#include "../jrd/exe.h"
#include "../jrd/met.h"
#include "../jrd/lck.h"
#include "../jrd/cmp_proto.h"
#include "../jrd/dfw_proto.h"
#include "../jrd/err_proto.h"
#include "../jrd/exe_proto.h"
#include "../jrd/gds_proto.h"
#include "../jrd/idx_proto.h"
#include "../jrd/tra_proto.h"
#include "../jrd/lck_proto.h"
#include "../jrd/met_proto.h"
#include "../jrd/mov_proto.h"
#include "../jrd/pcmet_proto.h"
#include "../jrd/thd.h"
using namespace Jrd;
DATABASE DB = FILENAME "ODS.RDB";
bool PCMET_expression_index(thread_db* tdbb, SSHORT phase, DeferredWork* work,
jrd_tra* transaction)
{
/**************************************
*
* P C M E T _ e x p r e s s i o n _ i n d e x
*
**************************************
*
* Functional description
* Create a new expression index.
*
**************************************/
jrd_rel* relation;
index_desc idx;
JrdMemoryPool* new_pool = NULL;
jrd_req* current_request, *request;
jrd_tra* current_transaction;
SET_TDBB(tdbb);
const Database* dbb = tdbb->tdbb_database;
switch (phase)
{
case 0:
MET_delete_dependencies(tdbb, work->dfw_name, obj_expression_index);
return false;
case 1:
case 2:
return true;
case 3:
relation = NULL;
MOVE_CLEAR(&idx, sizeof(index_desc));
request = CMP_find_request(tdbb, irq_c_exp_index, IRQ_REQUESTS);
/*
if (request)
{
EXE_unwind(tdbb, request);
}
*/
FOR(REQUEST_HANDLE request)
IDX IN RDB$INDICES CROSS
REL IN RDB$RELATIONS OVER RDB$RELATION_NAME WITH
IDX.RDB$EXPRESSION_BLR NOT MISSING AND
IDX.RDB$INDEX_NAME EQ work->dfw_name.c_str()
if (!REQUEST(irq_c_exp_index))
{
REQUEST(irq_c_exp_index) = request;
}
if (!relation)
{
relation = MET_relation(tdbb, REL.RDB$RELATION_ID);
if (relation->rel_name.length() == 0)
{
relation->rel_name = REL.RDB$RELATION_NAME;
}
if (IDX.RDB$INDEX_ID && IDX.RDB$STATISTICS < 0.0)
{
SelectivityList selectivity(*tdbb->getDefaultPool());
const USHORT id = IDX.RDB$INDEX_ID - 1;
IDX_statistics(tdbb, relation, id, selectivity);
DFW_update_index(work->dfw_name.c_str(), id, selectivity);
EXE_unwind(tdbb, request);
return false;
}
if (IDX.RDB$INDEX_ID)
{
IDX_delete_index(tdbb, relation, IDX.RDB$INDEX_ID - 1);
MET_delete_dependencies(tdbb, work->dfw_name,
obj_expression_index);
MODIFY IDX
IDX.RDB$INDEX_ID.NULL = TRUE;
END_MODIFY;
}
if (IDX.RDB$INDEX_INACTIVE)
{
EXE_unwind(tdbb, request);
return false;
}
if (IDX.RDB$SEGMENT_COUNT)
ERR_post(isc_no_meta_update,
isc_arg_gds, isc_no_segments_err,
isc_arg_string, ERR_cstring(work->dfw_name), 0);
/* Msg359: segments not allowed in expression index %s */
if (IDX.RDB$UNIQUE_FLAG)
idx.idx_flags |= idx_unique;
if (IDX.RDB$INDEX_TYPE == 1)
idx.idx_flags |= idx_descending;
CompilerScratch* csb = 0;
/* allocate a new pool to contain the expression tree
for the expression index */
new_pool = JrdMemoryPool::createPool();
{
Jrd::ContextPoolHolder context(tdbb, new_pool);
MET_scan_relation(tdbb, relation);
if (!IDX.RDB$EXPRESSION_BLR.NULL)
{
MetaTmp(IDX.RDB$INDEX_NAME)
idx.idx_expression = MET_get_dependencies(tdbb, relation,
NULL, NULL,
&IDX.RDB$EXPRESSION_BLR,
&idx.idx_expression_request,
&csb, tmp,
obj_expression_index, 0);
}
}
/* fake a description of the index */
idx.idx_count = 1;
idx.idx_flags |= idx_expressn;
CMP_get_desc(tdbb, csb, idx.idx_expression,
&idx.idx_expression_desc);
idx.idx_rpt[0].idx_itype =
DFW_assign_index_type(work,
idx.idx_expression_desc.dsc_dtype,
idx.idx_expression_desc.dsc_sub_type);
idx.idx_rpt[0].idx_selectivity = 0;
delete csb;
}
END_FOR;
if (!REQUEST(irq_c_exp_index))
{
REQUEST(irq_c_exp_index) = request;
}
if (!relation)
ERR_post(isc_no_meta_update,
isc_arg_gds, isc_idx_create_err,
isc_arg_string, ERR_cstring(work->dfw_name), 0);
// Msg308: can't create index %s
// Actually create the index
SelectivityList selectivity(*tdbb->getDefaultPool());
current_request = tdbb->tdbb_request;
current_transaction = tdbb->tdbb_transaction;
TRA_attach_request(transaction, idx.idx_expression_request);
tdbb->tdbb_request = idx.idx_expression_request;
IDX_create_index(tdbb, relation, &idx, work->dfw_name.c_str(), NULL,
transaction, selectivity);
TRA_detach_request(idx.idx_expression_request);
tdbb->tdbb_request = current_request;
tdbb->tdbb_transaction = current_transaction;
DFW_update_index(work->dfw_name.c_str(), idx.idx_id, selectivity);
// Get rid of the pool containing the expression tree
if (new_pool)
{
JrdMemoryPool::deletePool(new_pool);
}
break;
}
return false;
}
void PCMET_lookup_index(thread_db* tdbb, jrd_rel* relation, index_desc* idx)
{
/**************************************
*
* P C M E T _ l o o k u p _ i n d e x
*
**************************************
*
* Functional description
* Lookup information about an index, in
* the metadata cache if possible.
*
**************************************/
SET_TDBB(tdbb);
const Database* dbb = tdbb->tdbb_database;
// Check the index blocks for the relation to see if we
// have a cached block
IndexBlock* index_block;
for (index_block = relation->rel_index_blocks; index_block;
index_block = index_block->idb_next)
{
if (index_block->idb_id == idx->idx_id)
break;
}
if (index_block && index_block->idb_expression)
{
idx->idx_expression = index_block->idb_expression;
idx->idx_expression_request = index_block->idb_expression_request;
MOVE_FAST(&index_block->idb_expression_desc,
&idx->idx_expression_desc, sizeof(struct dsc));
return;
}
if (!(relation->rel_flags & REL_scanned) ||
(relation->rel_flags & REL_being_scanned))
{
MET_scan_relation(tdbb, relation);
}
CompilerScratch* csb = NULL;
jrd_req* request = CMP_find_request(tdbb, irq_l_exp_index, IRQ_REQUESTS);
FOR(REQUEST_HANDLE request)
IDX IN RDB$INDICES WITH
IDX.RDB$RELATION_NAME EQ relation->rel_name.c_str() AND
IDX.RDB$INDEX_ID EQ idx->idx_id + 1
if (!REQUEST(irq_l_exp_index))
{
REQUEST(irq_l_exp_index) = request;
}
if (idx->idx_expression_request)
{
CMP_release(tdbb, idx->idx_expression_request);
idx->idx_expression_request = NULL;
}
/* parse the blr, making sure to create the resulting expression
tree and request in its own pool so that it may be cached
with the index block in the "permanent" metadata cache */
{
Jrd::ContextPoolHolder context(tdbb, JrdMemoryPool::createPool());
idx->idx_expression =
MET_parse_blob(tdbb, relation, &IDX.RDB$EXPRESSION_BLR, &csb,
&idx->idx_expression_request, false);
}
END_FOR;
if (!REQUEST(irq_l_exp_index))
{
REQUEST(irq_l_exp_index) = request;
}
if (csb)
{
CMP_get_desc(tdbb, csb, idx->idx_expression,
&idx->idx_expression_desc);
}
delete csb;
/* if there is no existing index block for this index, create
one and link it in with the index blocks for this relation */
if (!index_block)
index_block = IDX_create_index_block(tdbb, relation, idx->idx_id);
/* if we can't get the lock, no big deal: just give up on caching the index info */
if (!LCK_lock_non_blocking(tdbb, index_block->idb_lock, LCK_SR, LCK_NO_WAIT))
return;
/* whether the index block already existed or was just created,
fill in the cached information about the index */
index_block->idb_expression = idx->idx_expression;
index_block->idb_expression_request = idx->idx_expression_request;
MOVE_FAST(&idx->idx_expression_desc, &index_block->idb_expression_desc,
sizeof(struct dsc));
}