head     1.8;
branch   ;
access   ;
symbols  Version_2_1:1.8 C_Demo_1:1.5;
locks    cimarron:1.8; strict;
comment  @ * @;


1.8
date     90.09.25.16.13.06;  author kemnitz;  state Exp;
branches ;
next     1.7;

1.7
date     90.06.09.18.54.26;  author kemnitz;  state Exp;
branches ;
next     1.6;

1.6
date     89.09.28.17.45.43;  author hirohama;  state Exp;
branches ;
next     1.5;

1.5
date     89.09.05.16.56.07;  author mao;  state C_Demo_1;
branches ;
next     1.4;

1.4
date     89.08.09.18.13.05;  author cimarron;  state Exp;
branches ;
next     1.3;

1.3
date     89.05.07.17.44.06;  author hirohama;  state Exp;
branches ;
next     1.2;

1.2
date     89.02.02.16.30.41;  author dillon;  state Stab;
branches ;
next     1.1;

1.1
date     89.01.17.05.51.56;  author cimarron;  state Exp;
branches ;
next     ;


desc
@@


1.8
log
@Updating from revision 1.7 to revision 1.9
@
text
@/*
 * istrat.c --
 *	POSTGRES index strategy code.
 */

/* #define ISTRATDEBUG	1 */

#include "tmp/postgres.h"

RcsId("$Header: RCS/istrat.c,v 1.9 90/08/13 15:58:08 cimarron Exp $");

#include "access/att.h"
#include "access/heapam.h"
#include "access/istrat.h"
#include "access/itup.h"	/* for MaxIndexAttributeNumber */
#include "access/skey.h"
#include "access/tqual.h"	/* for NowTimeQual */

#include "utils/log.h"

#include "catalog/catname.h"
#include "catalog/pg_amop.h"
#include "catalog/pg_index.h"
#include "catalog/pg_proc.h"

#include "internal.h"

bool
StrategyNumberIsValid(strategyNumber)
	StrategyNumber	strategyNumber;
{
	return ((bool)(strategyNumber != InvalidStrategy));
}

bool
StrategyNumberIsInBounds(strategyNumber, maxStrategyNumber)
	StrategyNumber	strategyNumber;
	StrategyNumber	maxStrategyNumber;
{
	return ((bool)(InvalidStrategy < strategyNumber &&
		strategyNumber <= maxStrategyNumber));
}

bool
StrategyMapIsValid(map)
	StrategyMap	map;
{
	return ((bool)PointerIsValid(map));
}

bool
IndexStrategyIsValid(indexStrategy)
	IndexStrategy	indexStrategy;
{
	return ((bool)PointerIsValid(indexStrategy));
}

ScanKeyEntry
StrategyMapGetScanKeyEntry(map, strategyNumber)
	StrategyMap	map;
	StrategyNumber	strategyNumber;
{
	Assert(StrategyMapIsValid(map));
	Assert(StrategyNumberIsValid(strategyNumber));

	return (&map->entry[strategyNumber - 1]);
}

StrategyMap
IndexStrategyGetStrategyMap(indexStrategy, maxStrategyNumber, attributeNumber)
	IndexStrategy	indexStrategy;
	StrategyNumber	maxStrategyNumber;
	AttributeNumber	attributeNumber;
{
	Assert(IndexStrategyIsValid(indexStrategy));
	Assert(StrategyNumberIsValid(maxStrategyNumber));
	Assert(AttributeNumberIsValid(attributeNumber));

	maxStrategyNumber = AMStrategies(maxStrategyNumber);	/* XXX */

	return (&indexStrategy->strategyMapData[maxStrategyNumber *
		(attributeNumber - 1)]);
}

Size
AttributeNumberGetIndexStrategySize(maxAttributeNumber, maxStrategyNumber)
	AttributeNumber	maxAttributeNumber;
	StrategyNumber	maxStrategyNumber;
{
	maxStrategyNumber = AMStrategies(maxStrategyNumber);	/* XXX */

	return (maxAttributeNumber * maxStrategyNumber * sizeof (ScanKeyData));
}

void
IndexStrategyInitialize(indexStrategy, indexObjectId, accessMethodObjectId,
		maxStrategyNumber)
	IndexStrategy	indexStrategy;
	ObjectId	indexObjectId;
	ObjectId	accessMethodObjectId;
	StrategyNumber	maxStrategyNumber;
{
	Relation		relation;
	Relation		operatorRelation;
	HeapScanDesc		scan;
	HeapTuple		tuple;
	ScanKeyEntryData	entry[2];
	StrategyMap		map;
	AttributeNumber		attributeNumber;
	AttributeOffset		attributeIndex;
	AttributeNumber		maxAttributeNumber;
	ObjectId		operatorClassObjectId[MaxIndexAttributeNumber];

	maxStrategyNumber = AMStrategies(maxStrategyNumber);

	entry[0].flags = 0;
	entry[0].attributeNumber = IndexRelationIdAttributeNumber;
	entry[0].procedure = ObjectIdEqualRegProcedure;
	entry[0].argument = ObjectIdGetDatum(indexObjectId);

	relation = RelationNameOpenHeapRelation(IndexRelationName);
	scan = RelationBeginHeapScan(relation, false, NowTimeQual, 1,
		(ScanKey)entry);
	tuple = HeapScanGetNextTuple(scan, false, (Buffer *)NULL);
	if (!HeapTupleIsValid(tuple)) {
		elog(WARN, "IndexStrategyInitialize: corrupted catalogs");
	}

/*
 * XXX note that the following assumes the INDEX tuple is well formed and
 * that the key[] and class[] are 0 terminated.
 */
	attributeIndex = 0;
	for (;;) {
		ObjectId	objectId;

		objectId = LintCast(IndexTupleForm, HeapTupleGetForm(tuple))
			->indkey[attributeIndex];
		if (!ObjectIdIsValid(objectId)) {
			if (attributeIndex == 0) {
				elog(WARN, "InitializeIndexStrategy: invalid IDEX");
			}
			break;
		}

		objectId = LintCast(IndexTupleForm, HeapTupleGetForm(tuple))
			->indclass[attributeIndex];
		if (!ObjectIdIsValid(objectId)) {
			elog(WARN, "InitializeIndexStrategy: corrupted INDEX tuple");
		}
		operatorClassObjectId[attributeIndex] = objectId;

		attributeIndex += 1;
	}
	maxAttributeNumber = attributeIndex;

	HeapScanEnd(scan);
	RelationCloseHeapRelation(relation);

	entry[0].flags = 0;
	entry[0].attributeNumber =
		AccessMethodOperatorAccessMethodIdAttributeNumber;
	entry[0].procedure = ObjectIdEqualRegProcedure;
	entry[0].argument = ObjectIdGetDatum(accessMethodObjectId);

	entry[1].flags = 0;
	entry[1].attributeNumber =
		AccessMethodOperatorOperatorClassIdAttributeNumber;
	entry[1].procedure = ObjectIdEqualRegProcedure;

	relation =
		RelationNameOpenHeapRelation(AccessMethodOperatorRelationName);

	operatorRelation = RelationNameOpenHeapRelation(OperatorRelationName);

	for (attributeNumber = maxAttributeNumber; attributeNumber > 0;
			attributeNumber -= 1) {

		StrategyNumber	strategy;

		entry[1].argument = 
			ObjectIdGetDatum(operatorClassObjectId[attributeNumber - 1]);

		map = IndexStrategyGetStrategyMap(indexStrategy,
			maxStrategyNumber, attributeNumber);

		for (strategy = 1; strategy <= maxStrategyNumber;
				strategy += 1) {
			ScanKeyEntrySetIllegal(
				StrategyMapGetScanKeyEntry(map, strategy));
		}

		scan = RelationBeginHeapScan(relation, false, NowTimeQual,
			2, (ScanKey)entry);

		while (HeapTupleIsValid(tuple = HeapScanGetNextTuple(scan,
				false, (Buffer *)NULL))) {

			AccessMethodOperatorTupleForm	form;

			form = LintCast(AccessMethodOperatorTupleForm,
				HeapTupleGetForm(tuple));

			OperatorRelationFillScanKeyEntry(operatorRelation,
				form->amopoprid, StrategyMapGetScanKeyEntry(
					map, form->amopstrategy));
		}

		HeapScanEnd(scan);
	}

	RelationCloseHeapRelation(operatorRelation);
	RelationCloseHeapRelation(relation);
}

static
void
OperatorRelationFillScanKeyEntry(operatorRelation, operatorObjectId, entry)
	Relation	operatorRelation;
	ObjectId	operatorObjectId;
	ScanKeyEntry	entry;
{
	HeapScanDesc	scan;
	ScanKeyData	scanKeyData;
	HeapTuple	tuple;

	scanKeyData.data[0].flags = 0;
	scanKeyData.data[0].attributeNumber = ObjectIdAttributeNumber;
	scanKeyData.data[0].procedure = ObjectIdEqualRegProcedure;
	scanKeyData.data[0].argument = ObjectIdGetDatum(operatorObjectId);

	scan = RelationBeginHeapScan(operatorRelation, false, NowTimeQual,
		1, &scanKeyData);

	tuple = HeapScanGetNextTuple(scan, false, (Buffer *)NULL);
	if (!HeapTupleIsValid(tuple)) {
		elog(WARN, "OperatorObjectIdFillScanKeyEntry: unknown operator %lu",
			(uint32)operatorObjectId);
	}

	entry->flags = 0;
	entry->procedure =
		LintCast(OperatorTupleForm, HeapTupleGetForm(tuple))->oprcode;

	if (!RegProcedureIsValid(entry->procedure)) {
		elog(WARN, "OperatorObjectIdFillScanKeyEntry: no procedure for operator %lu",
			(uint32)operatorObjectId);
	}

	HeapScanEnd(scan);
}

#ifdef	ISTRATDEBUG
int
IndexStrategyDisplay(indexStrategy, numberOfStrategies, numberOfAttributes)
	IndexStrategy	indexStrategy;
	StrategyNumber	numberOfStrategies;
	AttributeNumber	numberOfAttributes;
{
	StrategyMap	strategyMap;
	AttributeNumber	attributeNumber;
	StrategyNumber	strategyNumber;

	for (attributeNumber = 1; attributeNumber <= numberOfAttributes;
			attributeNumber += 1) {

		strategyMap = IndexStrategyGetStrategyMap(indexStrategy,
			numberOfStrategies, attributeNumber);

		for (strategyNumber = 1; strategyNumber <=
				AMStrategies(numberOfStrategies);
				strategyNumber += 1) {

			printf(":att %d\t:str %d\t:opr 0x%x(%d)\n",
				attributeNumber, strategyNumber,
				strategyMap->entry[strategyNumber - 1].procedure,
				strategyMap->entry[strategyNumber - 1].procedure);
		}
	}
}
#endif	/* defined(ISTRATDEBUG) */
@


1.7
log
@Got rid of datum indirection.
@
text
@d8 1
a8 1
#include "c.h"
d10 1
a10 11
#include "anum.h"
#include "att.h"
#include "cat.h"
#include "catname.h"
#include "heapam.h"
#include "itup.h"	/* for MaxIndexAttributeNumber */
#include "log.h"
#include "regproc.h"
#include "rproc.h"
#include "skey.h"
#include "tqual.h"	/* for NowTimeQual */
d12 14
a25 1
#include "istrat.h"
a26 2

RcsId("$Header: RCS/istrat.c,v 1.6 89/09/28 17:45:43 hirohama Exp Locker: kemnitz $");
@


1.6
log
@TimeRange -> TimeQual
@
text
@d25 1
a25 1
RcsId("$Header: RCS/istrat.c,v 1.5 89/09/05 16:56:07 mao C_Demo_1 Locker: hirohama $");
d118 1
a118 1
	entry[0].argument.objectId.value = indexObjectId;
d163 1
a163 1
	entry[0].argument.objectId.value = accessMethodObjectId;
d180 2
a181 2
		entry[1].argument.objectId.value =
			operatorClassObjectId[attributeNumber - 1];
d229 1
a229 1
	scanKeyData.data[0].argument.objectId.value = operatorObjectId;
@


1.5
log
@Working version of C-only demo
@
text
@d20 1
a20 1
#include "trange.h"
d25 1
a25 1
RcsId("$Header: /usr6/postgres/mao/postgres/src/access/genam/RCS/istrat.c,v 1.4 89/08/09 18:13:05 cimarron Exp $");
d121 1
a121 1
	scan = RelationBeginHeapScan(relation, false, DefaultTimeRange, 1,
d192 1
a192 1
		scan = RelationBeginHeapScan(relation, false, DefaultTimeRange,
d231 1
a231 1
	scan = RelationBeginHeapScan(operatorRelation, false, DefaultTimeRange,
@


1.4
log
@"retrieve(x=1)"
@
text
@d25 1
a25 1
RcsId("$Header: /usr6/postgres/cimarron/postgres3/src/access/genam/RCS/istrat.c,v 1.3 89/05/07 17:44:06 hirohama Exp $");
@


1.3
log
@removed #include "mmgr.h"
@
text
@d25 1
a25 1
RcsId("$Header: istrat.c,v 1.2 89/02/02 16:30:41 hirohama Locked $");
d104 1
a104 1
	HeapScan		scan;
d222 1
a222 1
	HeapScan	scan;
@


1.2
log
@Txfer from old tree
@
text
@a16 1
#include "mmgr.h"
d25 1
a25 1
RcsId("$Header: istrat.c,v 1.7 88/08/21 16:34:23 dillon Locked $");
@


1.1
log
@Initial revision
@
text
@a0 1

a1 26
 * 
 * POSTGRES Data Base Management System
 * 
 * Copyright (c) 1988 Regents of the University of California
 * 
 * Permission to use, copy, modify, and distribute this software and its
 * documentation for educational, research, and non-profit purposes and
 * without fee is hereby granted, provided that the above copyright
 * notice appear in all copies and that both that copyright notice and
 * this permission notice appear in supporting documentation, and that
 * the name of the University of California not be used in advertising
 * or publicity pertaining to distribution of the software without
 * specific, written prior permission.  Permission to incorporate this
 * software into commercial products can be obtained from the Campus
 * Software Office, 295 Evans Hall, University of California, Berkeley,
 * Ca., 94720 provided only that the the requestor give the University
 * of California a free licence to any derived software for educational
 * and research purposes.  The University of California makes no
 * representations about the suitability of this software for any
 * purpose.  It is provided "as is" without express or implied warranty.
 * 
 */



/*
d26 1
a26 1
RcsId("$Header: istrat.c,v 1.1 88/11/11 16:35:34 postgres Exp $");
@
