mirror of
https://github.com/postgres/postgres.git
synced 2025-11-10 17:42:29 +03:00
Massive commit to run PGINDENT on all *.c and *.h files.
This commit is contained in:
@@ -1,19 +1,19 @@
|
||||
/*-------------------------------------------------------------------------
|
||||
*
|
||||
* rtget.c--
|
||||
* fetch tuples from an rtree scan.
|
||||
* fetch tuples from an rtree scan.
|
||||
*
|
||||
* Copyright (c) 1994, Regents of the University of California
|
||||
*
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $Header: /cvsroot/pgsql/src/backend/access/rtree/Attic/rtget.c,v 1.7 1996/11/21 06:13:43 vadim Exp $
|
||||
* $Header: /cvsroot/pgsql/src/backend/access/rtree/Attic/rtget.c,v 1.8 1997/09/07 04:39:11 momjian Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
#include <postgres.h>
|
||||
|
||||
|
||||
#include <storage/bufmgr.h>
|
||||
#include <access/sdir.h>
|
||||
#include <access/relscan.h>
|
||||
@@ -21,14 +21,15 @@
|
||||
#include <access/rtree.h>
|
||||
#include <storage/bufpage.h>
|
||||
#ifndef HAVE_MEMMOVE
|
||||
# include <regex/utils.h>
|
||||
#include <regex/utils.h>
|
||||
#else
|
||||
# include <string.h>
|
||||
#include <string.h>
|
||||
#endif
|
||||
|
||||
|
||||
static OffsetNumber findnext(IndexScanDesc s, Page p, OffsetNumber n,
|
||||
ScanDirection dir);
|
||||
static OffsetNumber
|
||||
findnext(IndexScanDesc s, Page p, OffsetNumber n,
|
||||
ScanDirection dir);
|
||||
static RetrieveIndexResult rtscancache(IndexScanDesc s, ScanDirection dir);
|
||||
static RetrieveIndexResult rtfirst(IndexScanDesc s, ScanDirection dir);
|
||||
static RetrieveIndexResult rtnext(IndexScanDesc s, ScanDirection dir);
|
||||
@@ -38,278 +39,315 @@ static ItemPointer rtheapptr(Relation r, ItemPointer itemp);
|
||||
RetrieveIndexResult
|
||||
rtgettuple(IndexScanDesc s, ScanDirection dir)
|
||||
{
|
||||
RetrieveIndexResult res;
|
||||
|
||||
/* if we have it cached in the scan desc, just return the value */
|
||||
if ((res = rtscancache(s, dir)) != (RetrieveIndexResult) NULL)
|
||||
RetrieveIndexResult res;
|
||||
|
||||
/* if we have it cached in the scan desc, just return the value */
|
||||
if ((res = rtscancache(s, dir)) != (RetrieveIndexResult) NULL)
|
||||
return (res);
|
||||
|
||||
/* not cached, so we'll have to do some work */
|
||||
if (ItemPointerIsValid(&(s->currentItemData)))
|
||||
{
|
||||
res = rtnext(s, dir);
|
||||
}
|
||||
else
|
||||
{
|
||||
res = rtfirst(s, dir);
|
||||
}
|
||||
return (res);
|
||||
|
||||
/* not cached, so we'll have to do some work */
|
||||
if (ItemPointerIsValid(&(s->currentItemData))) {
|
||||
res = rtnext(s, dir);
|
||||
} else {
|
||||
res = rtfirst(s, dir);
|
||||
}
|
||||
return (res);
|
||||
}
|
||||
|
||||
static RetrieveIndexResult
|
||||
static RetrieveIndexResult
|
||||
rtfirst(IndexScanDesc s, ScanDirection dir)
|
||||
{
|
||||
Buffer b;
|
||||
Page p;
|
||||
OffsetNumber n;
|
||||
OffsetNumber maxoff;
|
||||
RetrieveIndexResult res;
|
||||
RTreePageOpaque po;
|
||||
RTreeScanOpaque so;
|
||||
RTSTACK *stk;
|
||||
BlockNumber blk;
|
||||
IndexTuple it;
|
||||
|
||||
b = ReadBuffer(s->relation, P_ROOT);
|
||||
p = BufferGetPage(b);
|
||||
po = (RTreePageOpaque) PageGetSpecialPointer(p);
|
||||
so = (RTreeScanOpaque) s->opaque;
|
||||
|
||||
for (;;) {
|
||||
maxoff = PageGetMaxOffsetNumber(p);
|
||||
if (ScanDirectionIsBackward(dir))
|
||||
n = findnext(s, p, maxoff, dir);
|
||||
else
|
||||
n = findnext(s, p, FirstOffsetNumber, dir);
|
||||
|
||||
while (n < FirstOffsetNumber || n > maxoff) {
|
||||
|
||||
ReleaseBuffer(b);
|
||||
if (so->s_stack == (RTSTACK *) NULL)
|
||||
return ((RetrieveIndexResult) NULL);
|
||||
|
||||
stk = so->s_stack;
|
||||
b = ReadBuffer(s->relation, stk->rts_blk);
|
||||
p = BufferGetPage(b);
|
||||
po = (RTreePageOpaque) PageGetSpecialPointer(p);
|
||||
maxoff = PageGetMaxOffsetNumber(p);
|
||||
|
||||
if (ScanDirectionIsBackward(dir)) {
|
||||
n = OffsetNumberPrev(stk->rts_child);
|
||||
} else {
|
||||
n = OffsetNumberNext(stk->rts_child);
|
||||
}
|
||||
so->s_stack = stk->rts_parent;
|
||||
pfree(stk);
|
||||
|
||||
n = findnext(s, p, n, dir);
|
||||
Buffer b;
|
||||
Page p;
|
||||
OffsetNumber n;
|
||||
OffsetNumber maxoff;
|
||||
RetrieveIndexResult res;
|
||||
RTreePageOpaque po;
|
||||
RTreeScanOpaque so;
|
||||
RTSTACK *stk;
|
||||
BlockNumber blk;
|
||||
IndexTuple it;
|
||||
|
||||
b = ReadBuffer(s->relation, P_ROOT);
|
||||
p = BufferGetPage(b);
|
||||
po = (RTreePageOpaque) PageGetSpecialPointer(p);
|
||||
so = (RTreeScanOpaque) s->opaque;
|
||||
|
||||
for (;;)
|
||||
{
|
||||
maxoff = PageGetMaxOffsetNumber(p);
|
||||
if (ScanDirectionIsBackward(dir))
|
||||
n = findnext(s, p, maxoff, dir);
|
||||
else
|
||||
n = findnext(s, p, FirstOffsetNumber, dir);
|
||||
|
||||
while (n < FirstOffsetNumber || n > maxoff)
|
||||
{
|
||||
|
||||
ReleaseBuffer(b);
|
||||
if (so->s_stack == (RTSTACK *) NULL)
|
||||
return ((RetrieveIndexResult) NULL);
|
||||
|
||||
stk = so->s_stack;
|
||||
b = ReadBuffer(s->relation, stk->rts_blk);
|
||||
p = BufferGetPage(b);
|
||||
po = (RTreePageOpaque) PageGetSpecialPointer(p);
|
||||
maxoff = PageGetMaxOffsetNumber(p);
|
||||
|
||||
if (ScanDirectionIsBackward(dir))
|
||||
{
|
||||
n = OffsetNumberPrev(stk->rts_child);
|
||||
}
|
||||
else
|
||||
{
|
||||
n = OffsetNumberNext(stk->rts_child);
|
||||
}
|
||||
so->s_stack = stk->rts_parent;
|
||||
pfree(stk);
|
||||
|
||||
n = findnext(s, p, n, dir);
|
||||
}
|
||||
if (po->flags & F_LEAF)
|
||||
{
|
||||
ItemPointerSet(&(s->currentItemData), BufferGetBlockNumber(b), n);
|
||||
|
||||
it = (IndexTuple) PageGetItem(p, PageGetItemId(p, n));
|
||||
|
||||
res = FormRetrieveIndexResult(&(s->currentItemData), &(it->t_tid));
|
||||
|
||||
ReleaseBuffer(b);
|
||||
return (res);
|
||||
}
|
||||
else
|
||||
{
|
||||
stk = (RTSTACK *) palloc(sizeof(RTSTACK));
|
||||
stk->rts_child = n;
|
||||
stk->rts_blk = BufferGetBlockNumber(b);
|
||||
stk->rts_parent = so->s_stack;
|
||||
so->s_stack = stk;
|
||||
|
||||
it = (IndexTuple) PageGetItem(p, PageGetItemId(p, n));
|
||||
blk = ItemPointerGetBlockNumber(&(it->t_tid));
|
||||
|
||||
ReleaseBuffer(b);
|
||||
b = ReadBuffer(s->relation, blk);
|
||||
p = BufferGetPage(b);
|
||||
po = (RTreePageOpaque) PageGetSpecialPointer(p);
|
||||
}
|
||||
}
|
||||
if (po->flags & F_LEAF) {
|
||||
ItemPointerSet(&(s->currentItemData), BufferGetBlockNumber(b), n);
|
||||
|
||||
it = (IndexTuple) PageGetItem(p, PageGetItemId(p, n));
|
||||
|
||||
res = FormRetrieveIndexResult(&(s->currentItemData), &(it->t_tid));
|
||||
|
||||
ReleaseBuffer(b);
|
||||
return (res);
|
||||
} else {
|
||||
stk = (RTSTACK *) palloc(sizeof(RTSTACK));
|
||||
stk->rts_child = n;
|
||||
stk->rts_blk = BufferGetBlockNumber(b);
|
||||
stk->rts_parent = so->s_stack;
|
||||
so->s_stack = stk;
|
||||
|
||||
it = (IndexTuple) PageGetItem(p, PageGetItemId(p, n));
|
||||
blk = ItemPointerGetBlockNumber(&(it->t_tid));
|
||||
|
||||
ReleaseBuffer(b);
|
||||
b = ReadBuffer(s->relation, blk);
|
||||
p = BufferGetPage(b);
|
||||
po = (RTreePageOpaque) PageGetSpecialPointer(p);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static RetrieveIndexResult
|
||||
static RetrieveIndexResult
|
||||
rtnext(IndexScanDesc s, ScanDirection dir)
|
||||
{
|
||||
Buffer b;
|
||||
Page p;
|
||||
OffsetNumber n;
|
||||
OffsetNumber maxoff;
|
||||
RetrieveIndexResult res;
|
||||
RTreePageOpaque po;
|
||||
RTreeScanOpaque so;
|
||||
RTSTACK *stk;
|
||||
BlockNumber blk;
|
||||
IndexTuple it;
|
||||
|
||||
blk = ItemPointerGetBlockNumber(&(s->currentItemData));
|
||||
n = ItemPointerGetOffsetNumber(&(s->currentItemData));
|
||||
|
||||
if (ScanDirectionIsForward(dir)) {
|
||||
n = OffsetNumberNext(n);
|
||||
} else {
|
||||
n = OffsetNumberPrev(n);
|
||||
}
|
||||
Buffer b;
|
||||
Page p;
|
||||
OffsetNumber n;
|
||||
OffsetNumber maxoff;
|
||||
RetrieveIndexResult res;
|
||||
RTreePageOpaque po;
|
||||
RTreeScanOpaque so;
|
||||
RTSTACK *stk;
|
||||
BlockNumber blk;
|
||||
IndexTuple it;
|
||||
|
||||
b = ReadBuffer(s->relation, blk);
|
||||
p = BufferGetPage(b);
|
||||
po = (RTreePageOpaque) PageGetSpecialPointer(p);
|
||||
so = (RTreeScanOpaque) s->opaque;
|
||||
|
||||
for (;;) {
|
||||
maxoff = PageGetMaxOffsetNumber(p);
|
||||
n = findnext(s, p, n, dir);
|
||||
|
||||
while (n < FirstOffsetNumber || n > maxoff) {
|
||||
|
||||
ReleaseBuffer(b);
|
||||
if (so->s_stack == (RTSTACK *) NULL)
|
||||
return ((RetrieveIndexResult) NULL);
|
||||
|
||||
stk = so->s_stack;
|
||||
b = ReadBuffer(s->relation, stk->rts_blk);
|
||||
p = BufferGetPage(b);
|
||||
maxoff = PageGetMaxOffsetNumber(p);
|
||||
po = (RTreePageOpaque) PageGetSpecialPointer(p);
|
||||
|
||||
if (ScanDirectionIsBackward(dir)) {
|
||||
n = OffsetNumberPrev(stk->rts_child);
|
||||
} else {
|
||||
n = OffsetNumberNext(stk->rts_child);
|
||||
}
|
||||
so->s_stack = stk->rts_parent;
|
||||
pfree(stk);
|
||||
|
||||
n = findnext(s, p, n, dir);
|
||||
blk = ItemPointerGetBlockNumber(&(s->currentItemData));
|
||||
n = ItemPointerGetOffsetNumber(&(s->currentItemData));
|
||||
|
||||
if (ScanDirectionIsForward(dir))
|
||||
{
|
||||
n = OffsetNumberNext(n);
|
||||
}
|
||||
if (po->flags & F_LEAF) {
|
||||
ItemPointerSet(&(s->currentItemData), BufferGetBlockNumber(b), n);
|
||||
|
||||
it = (IndexTuple) PageGetItem(p, PageGetItemId(p, n));
|
||||
|
||||
res = FormRetrieveIndexResult(&(s->currentItemData), &(it->t_tid));
|
||||
|
||||
ReleaseBuffer(b);
|
||||
return (res);
|
||||
} else {
|
||||
stk = (RTSTACK *) palloc(sizeof(RTSTACK));
|
||||
stk->rts_child = n;
|
||||
stk->rts_blk = BufferGetBlockNumber(b);
|
||||
stk->rts_parent = so->s_stack;
|
||||
so->s_stack = stk;
|
||||
|
||||
it = (IndexTuple) PageGetItem(p, PageGetItemId(p, n));
|
||||
blk = ItemPointerGetBlockNumber(&(it->t_tid));
|
||||
|
||||
ReleaseBuffer(b);
|
||||
b = ReadBuffer(s->relation, blk);
|
||||
p = BufferGetPage(b);
|
||||
po = (RTreePageOpaque) PageGetSpecialPointer(p);
|
||||
|
||||
if (ScanDirectionIsBackward(dir)) {
|
||||
n = PageGetMaxOffsetNumber(p);
|
||||
} else {
|
||||
n = FirstOffsetNumber;
|
||||
}
|
||||
else
|
||||
{
|
||||
n = OffsetNumberPrev(n);
|
||||
}
|
||||
|
||||
b = ReadBuffer(s->relation, blk);
|
||||
p = BufferGetPage(b);
|
||||
po = (RTreePageOpaque) PageGetSpecialPointer(p);
|
||||
so = (RTreeScanOpaque) s->opaque;
|
||||
|
||||
for (;;)
|
||||
{
|
||||
maxoff = PageGetMaxOffsetNumber(p);
|
||||
n = findnext(s, p, n, dir);
|
||||
|
||||
while (n < FirstOffsetNumber || n > maxoff)
|
||||
{
|
||||
|
||||
ReleaseBuffer(b);
|
||||
if (so->s_stack == (RTSTACK *) NULL)
|
||||
return ((RetrieveIndexResult) NULL);
|
||||
|
||||
stk = so->s_stack;
|
||||
b = ReadBuffer(s->relation, stk->rts_blk);
|
||||
p = BufferGetPage(b);
|
||||
maxoff = PageGetMaxOffsetNumber(p);
|
||||
po = (RTreePageOpaque) PageGetSpecialPointer(p);
|
||||
|
||||
if (ScanDirectionIsBackward(dir))
|
||||
{
|
||||
n = OffsetNumberPrev(stk->rts_child);
|
||||
}
|
||||
else
|
||||
{
|
||||
n = OffsetNumberNext(stk->rts_child);
|
||||
}
|
||||
so->s_stack = stk->rts_parent;
|
||||
pfree(stk);
|
||||
|
||||
n = findnext(s, p, n, dir);
|
||||
}
|
||||
if (po->flags & F_LEAF)
|
||||
{
|
||||
ItemPointerSet(&(s->currentItemData), BufferGetBlockNumber(b), n);
|
||||
|
||||
it = (IndexTuple) PageGetItem(p, PageGetItemId(p, n));
|
||||
|
||||
res = FormRetrieveIndexResult(&(s->currentItemData), &(it->t_tid));
|
||||
|
||||
ReleaseBuffer(b);
|
||||
return (res);
|
||||
}
|
||||
else
|
||||
{
|
||||
stk = (RTSTACK *) palloc(sizeof(RTSTACK));
|
||||
stk->rts_child = n;
|
||||
stk->rts_blk = BufferGetBlockNumber(b);
|
||||
stk->rts_parent = so->s_stack;
|
||||
so->s_stack = stk;
|
||||
|
||||
it = (IndexTuple) PageGetItem(p, PageGetItemId(p, n));
|
||||
blk = ItemPointerGetBlockNumber(&(it->t_tid));
|
||||
|
||||
ReleaseBuffer(b);
|
||||
b = ReadBuffer(s->relation, blk);
|
||||
p = BufferGetPage(b);
|
||||
po = (RTreePageOpaque) PageGetSpecialPointer(p);
|
||||
|
||||
if (ScanDirectionIsBackward(dir))
|
||||
{
|
||||
n = PageGetMaxOffsetNumber(p);
|
||||
}
|
||||
else
|
||||
{
|
||||
n = FirstOffsetNumber;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static OffsetNumber
|
||||
static OffsetNumber
|
||||
findnext(IndexScanDesc s, Page p, OffsetNumber n, ScanDirection dir)
|
||||
{
|
||||
OffsetNumber maxoff;
|
||||
IndexTuple it;
|
||||
RTreePageOpaque po;
|
||||
RTreeScanOpaque so;
|
||||
|
||||
maxoff = PageGetMaxOffsetNumber(p);
|
||||
po = (RTreePageOpaque) PageGetSpecialPointer(p);
|
||||
so = (RTreeScanOpaque) s->opaque;
|
||||
|
||||
/*
|
||||
* If we modified the index during the scan, we may have a pointer to
|
||||
* a ghost tuple, before the scan. If this is the case, back up one.
|
||||
*/
|
||||
|
||||
if (so->s_flags & RTS_CURBEFORE) {
|
||||
so->s_flags &= ~RTS_CURBEFORE;
|
||||
n = OffsetNumberPrev(n);
|
||||
}
|
||||
|
||||
while (n >= FirstOffsetNumber && n <= maxoff) {
|
||||
it = (IndexTuple) PageGetItem(p, PageGetItemId(p, n));
|
||||
if (po->flags & F_LEAF) {
|
||||
if (index_keytest(it,
|
||||
RelationGetTupleDescriptor(s->relation),
|
||||
s->numberOfKeys, s->keyData))
|
||||
break;
|
||||
} else {
|
||||
if (index_keytest(it,
|
||||
RelationGetTupleDescriptor(s->relation),
|
||||
so->s_internalNKey, so->s_internalKey))
|
||||
break;
|
||||
OffsetNumber maxoff;
|
||||
IndexTuple it;
|
||||
RTreePageOpaque po;
|
||||
RTreeScanOpaque so;
|
||||
|
||||
maxoff = PageGetMaxOffsetNumber(p);
|
||||
po = (RTreePageOpaque) PageGetSpecialPointer(p);
|
||||
so = (RTreeScanOpaque) s->opaque;
|
||||
|
||||
/*
|
||||
* If we modified the index during the scan, we may have a pointer to
|
||||
* a ghost tuple, before the scan. If this is the case, back up one.
|
||||
*/
|
||||
|
||||
if (so->s_flags & RTS_CURBEFORE)
|
||||
{
|
||||
so->s_flags &= ~RTS_CURBEFORE;
|
||||
n = OffsetNumberPrev(n);
|
||||
}
|
||||
|
||||
if (ScanDirectionIsBackward(dir)) {
|
||||
n = OffsetNumberPrev(n);
|
||||
} else {
|
||||
n = OffsetNumberNext(n);
|
||||
|
||||
while (n >= FirstOffsetNumber && n <= maxoff)
|
||||
{
|
||||
it = (IndexTuple) PageGetItem(p, PageGetItemId(p, n));
|
||||
if (po->flags & F_LEAF)
|
||||
{
|
||||
if (index_keytest(it,
|
||||
RelationGetTupleDescriptor(s->relation),
|
||||
s->numberOfKeys, s->keyData))
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (index_keytest(it,
|
||||
RelationGetTupleDescriptor(s->relation),
|
||||
so->s_internalNKey, so->s_internalKey))
|
||||
break;
|
||||
}
|
||||
|
||||
if (ScanDirectionIsBackward(dir))
|
||||
{
|
||||
n = OffsetNumberPrev(n);
|
||||
}
|
||||
else
|
||||
{
|
||||
n = OffsetNumberNext(n);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return (n);
|
||||
|
||||
return (n);
|
||||
}
|
||||
|
||||
static RetrieveIndexResult
|
||||
static RetrieveIndexResult
|
||||
rtscancache(IndexScanDesc s, ScanDirection dir)
|
||||
{
|
||||
RetrieveIndexResult res;
|
||||
ItemPointer ip;
|
||||
|
||||
if (!(ScanDirectionIsNoMovement(dir)
|
||||
&& ItemPointerIsValid(&(s->currentItemData)))) {
|
||||
|
||||
return ((RetrieveIndexResult) NULL);
|
||||
}
|
||||
|
||||
ip = rtheapptr(s->relation, &(s->currentItemData));
|
||||
|
||||
if (ItemPointerIsValid(ip))
|
||||
res = FormRetrieveIndexResult(&(s->currentItemData), ip);
|
||||
else
|
||||
res = (RetrieveIndexResult) NULL;
|
||||
|
||||
pfree (ip);
|
||||
RetrieveIndexResult res;
|
||||
ItemPointer ip;
|
||||
|
||||
return (res);
|
||||
if (!(ScanDirectionIsNoMovement(dir)
|
||||
&& ItemPointerIsValid(&(s->currentItemData))))
|
||||
{
|
||||
|
||||
return ((RetrieveIndexResult) NULL);
|
||||
}
|
||||
|
||||
ip = rtheapptr(s->relation, &(s->currentItemData));
|
||||
|
||||
if (ItemPointerIsValid(ip))
|
||||
res = FormRetrieveIndexResult(&(s->currentItemData), ip);
|
||||
else
|
||||
res = (RetrieveIndexResult) NULL;
|
||||
|
||||
pfree(ip);
|
||||
|
||||
return (res);
|
||||
}
|
||||
|
||||
/*
|
||||
* rtheapptr returns the item pointer to the tuple in the heap relation
|
||||
* for which itemp is the index relation item pointer.
|
||||
* rtheapptr returns the item pointer to the tuple in the heap relation
|
||||
* for which itemp is the index relation item pointer.
|
||||
*/
|
||||
static ItemPointer
|
||||
static ItemPointer
|
||||
rtheapptr(Relation r, ItemPointer itemp)
|
||||
{
|
||||
Buffer b;
|
||||
Page p;
|
||||
IndexTuple it;
|
||||
ItemPointer ip;
|
||||
OffsetNumber n;
|
||||
|
||||
ip = (ItemPointer) palloc(sizeof(ItemPointerData));
|
||||
if (ItemPointerIsValid(itemp)) {
|
||||
b = ReadBuffer(r, ItemPointerGetBlockNumber(itemp));
|
||||
p = BufferGetPage(b);
|
||||
n = ItemPointerGetOffsetNumber(itemp);
|
||||
it = (IndexTuple) PageGetItem(p, PageGetItemId(p, n));
|
||||
memmove((char *) ip, (char *) &(it->t_tid),
|
||||
sizeof(ItemPointerData));
|
||||
ReleaseBuffer(b);
|
||||
} else {
|
||||
ItemPointerSetInvalid(ip);
|
||||
}
|
||||
|
||||
return (ip);
|
||||
Buffer b;
|
||||
Page p;
|
||||
IndexTuple it;
|
||||
ItemPointer ip;
|
||||
OffsetNumber n;
|
||||
|
||||
ip = (ItemPointer) palloc(sizeof(ItemPointerData));
|
||||
if (ItemPointerIsValid(itemp))
|
||||
{
|
||||
b = ReadBuffer(r, ItemPointerGetBlockNumber(itemp));
|
||||
p = BufferGetPage(b);
|
||||
n = ItemPointerGetOffsetNumber(itemp);
|
||||
it = (IndexTuple) PageGetItem(p, PageGetItemId(p, n));
|
||||
memmove((char *) ip, (char *) &(it->t_tid),
|
||||
sizeof(ItemPointerData));
|
||||
ReleaseBuffer(b);
|
||||
}
|
||||
else
|
||||
{
|
||||
ItemPointerSetInvalid(ip);
|
||||
}
|
||||
|
||||
return (ip);
|
||||
}
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
/*-------------------------------------------------------------------------
|
||||
*
|
||||
* rtproc.c--
|
||||
* pg_amproc entries for rtrees.
|
||||
* pg_amproc entries for rtrees.
|
||||
*
|
||||
* Copyright (c) 1994, Regents of the University of California
|
||||
*
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $Header: /cvsroot/pgsql/src/backend/access/rtree/Attic/rtproc.c,v 1.7 1997/04/22 17:31:23 scrappy Exp $
|
||||
* $Header: /cvsroot/pgsql/src/backend/access/rtree/Attic/rtproc.c,v 1.8 1997/09/07 04:39:16 momjian Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
@@ -17,136 +17,139 @@
|
||||
#include <utils/builtins.h>
|
||||
#include <utils/geo_decls.h>
|
||||
#ifndef HAVE_MEMMOVE
|
||||
# include <regex/utils.h>
|
||||
#include <regex/utils.h>
|
||||
#else
|
||||
# include <string.h>
|
||||
#include <string.h>
|
||||
#endif
|
||||
|
||||
BOX
|
||||
*rt_box_union(BOX *a, BOX *b)
|
||||
* rt_box_union(BOX * a, BOX * b)
|
||||
{
|
||||
BOX *n;
|
||||
|
||||
if ((n = (BOX *) palloc(sizeof (*n))) == (BOX *) NULL)
|
||||
elog(WARN, "Cannot allocate box for union");
|
||||
|
||||
n->high.x = Max(a->high.x, b->high.x);
|
||||
n->high.y = Max(a->high.y, b->high.y);
|
||||
n->low.x = Min(a->low.x, b->low.x);
|
||||
n->low.y = Min(a->low.y, b->low.y);
|
||||
|
||||
return (n);
|
||||
BOX *n;
|
||||
|
||||
if ((n = (BOX *) palloc(sizeof(*n))) == (BOX *) NULL)
|
||||
elog(WARN, "Cannot allocate box for union");
|
||||
|
||||
n->high.x = Max(a->high.x, b->high.x);
|
||||
n->high.y = Max(a->high.y, b->high.y);
|
||||
n->low.x = Min(a->low.x, b->low.x);
|
||||
n->low.y = Min(a->low.y, b->low.y);
|
||||
|
||||
return (n);
|
||||
}
|
||||
|
||||
BOX *
|
||||
rt_box_inter(BOX *a, BOX *b)
|
||||
BOX *
|
||||
rt_box_inter(BOX * a, BOX * b)
|
||||
{
|
||||
BOX *n;
|
||||
|
||||
if ((n = (BOX *) palloc(sizeof (*n))) == (BOX *) NULL)
|
||||
elog(WARN, "Cannot allocate box for union");
|
||||
|
||||
n->high.x = Min(a->high.x, b->high.x);
|
||||
n->high.y = Min(a->high.y, b->high.y);
|
||||
n->low.x = Max(a->low.x, b->low.x);
|
||||
n->low.y = Max(a->low.y, b->low.y);
|
||||
|
||||
if (n->high.x < n->low.x || n->high.y < n->low.y) {
|
||||
pfree(n);
|
||||
return ((BOX *) NULL);
|
||||
}
|
||||
|
||||
return (n);
|
||||
BOX *n;
|
||||
|
||||
if ((n = (BOX *) palloc(sizeof(*n))) == (BOX *) NULL)
|
||||
elog(WARN, "Cannot allocate box for union");
|
||||
|
||||
n->high.x = Min(a->high.x, b->high.x);
|
||||
n->high.y = Min(a->high.y, b->high.y);
|
||||
n->low.x = Max(a->low.x, b->low.x);
|
||||
n->low.y = Max(a->low.y, b->low.y);
|
||||
|
||||
if (n->high.x < n->low.x || n->high.y < n->low.y)
|
||||
{
|
||||
pfree(n);
|
||||
return ((BOX *) NULL);
|
||||
}
|
||||
|
||||
return (n);
|
||||
}
|
||||
|
||||
void
|
||||
rt_box_size(BOX *a, float *size)
|
||||
rt_box_size(BOX * a, float *size)
|
||||
{
|
||||
if (a == (BOX *) NULL || a->high.x <= a->low.x || a->high.y <= a->low.y)
|
||||
*size = 0.0;
|
||||
else
|
||||
*size = (float) ((a->high.x - a->low.x) * (a->high.y - a->low.y));
|
||||
|
||||
return;
|
||||
if (a == (BOX *) NULL || a->high.x <= a->low.x || a->high.y <= a->low.y)
|
||||
*size = 0.0;
|
||||
else
|
||||
*size = (float) ((a->high.x - a->low.x) * (a->high.y - a->low.y));
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
/*
|
||||
* rt_bigbox_size() -- Compute a size for big boxes.
|
||||
* rt_bigbox_size() -- Compute a size for big boxes.
|
||||
*
|
||||
* In an earlier release of the system, this routine did something
|
||||
* different from rt_box_size. We now use floats, rather than ints,
|
||||
* as the return type for the size routine, so we no longer need to
|
||||
* have a special return type for big boxes.
|
||||
* In an earlier release of the system, this routine did something
|
||||
* different from rt_box_size. We now use floats, rather than ints,
|
||||
* as the return type for the size routine, so we no longer need to
|
||||
* have a special return type for big boxes.
|
||||
*/
|
||||
void
|
||||
rt_bigbox_size(BOX *a, float *size)
|
||||
rt_bigbox_size(BOX * a, float *size)
|
||||
{
|
||||
rt_box_size(a, size);
|
||||
rt_box_size(a, size);
|
||||
}
|
||||
|
||||
POLYGON *
|
||||
rt_poly_union(POLYGON *a, POLYGON *b)
|
||||
POLYGON *
|
||||
rt_poly_union(POLYGON * a, POLYGON * b)
|
||||
{
|
||||
POLYGON *p;
|
||||
|
||||
p = (POLYGON *)PALLOCTYPE(POLYGON);
|
||||
|
||||
if (!PointerIsValid(p))
|
||||
elog(WARN, "Cannot allocate polygon for union");
|
||||
|
||||
memset((char *) p, 0, sizeof(POLYGON)); /* zero any holes */
|
||||
p->size = sizeof(POLYGON);
|
||||
p->npts = 0;
|
||||
p->boundbox.high.x = Max(a->boundbox.high.x, b->boundbox.high.x);
|
||||
p->boundbox.high.y = Max(a->boundbox.high.y, b->boundbox.high.y);
|
||||
p->boundbox.low.x = Min(a->boundbox.low.x, b->boundbox.low.x);
|
||||
p->boundbox.low.y = Min(a->boundbox.low.y, b->boundbox.low.y);
|
||||
return p;
|
||||
POLYGON *p;
|
||||
|
||||
p = (POLYGON *) PALLOCTYPE(POLYGON);
|
||||
|
||||
if (!PointerIsValid(p))
|
||||
elog(WARN, "Cannot allocate polygon for union");
|
||||
|
||||
memset((char *) p, 0, sizeof(POLYGON)); /* zero any holes */
|
||||
p->size = sizeof(POLYGON);
|
||||
p->npts = 0;
|
||||
p->boundbox.high.x = Max(a->boundbox.high.x, b->boundbox.high.x);
|
||||
p->boundbox.high.y = Max(a->boundbox.high.y, b->boundbox.high.y);
|
||||
p->boundbox.low.x = Min(a->boundbox.low.x, b->boundbox.low.x);
|
||||
p->boundbox.low.y = Min(a->boundbox.low.y, b->boundbox.low.y);
|
||||
return p;
|
||||
}
|
||||
|
||||
void
|
||||
rt_poly_size(POLYGON *a, float *size)
|
||||
rt_poly_size(POLYGON * a, float *size)
|
||||
{
|
||||
double xdim, ydim;
|
||||
|
||||
size = (float *) palloc(sizeof(float));
|
||||
if (a == (POLYGON *) NULL ||
|
||||
a->boundbox.high.x <= a->boundbox.low.x ||
|
||||
a->boundbox.high.y <= a->boundbox.low.y)
|
||||
*size = 0.0;
|
||||
else {
|
||||
xdim = (a->boundbox.high.x - a->boundbox.low.x);
|
||||
ydim = (a->boundbox.high.y - a->boundbox.low.y);
|
||||
|
||||
*size = (float) (xdim * ydim);
|
||||
}
|
||||
|
||||
return;
|
||||
double xdim,
|
||||
ydim;
|
||||
|
||||
size = (float *) palloc(sizeof(float));
|
||||
if (a == (POLYGON *) NULL ||
|
||||
a->boundbox.high.x <= a->boundbox.low.x ||
|
||||
a->boundbox.high.y <= a->boundbox.low.y)
|
||||
*size = 0.0;
|
||||
else
|
||||
{
|
||||
xdim = (a->boundbox.high.x - a->boundbox.low.x);
|
||||
ydim = (a->boundbox.high.y - a->boundbox.low.y);
|
||||
|
||||
*size = (float) (xdim * ydim);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
POLYGON *
|
||||
rt_poly_inter(POLYGON *a, POLYGON *b)
|
||||
POLYGON *
|
||||
rt_poly_inter(POLYGON * a, POLYGON * b)
|
||||
{
|
||||
POLYGON *p;
|
||||
|
||||
p = (POLYGON *) PALLOCTYPE(POLYGON);
|
||||
|
||||
if (!PointerIsValid(p))
|
||||
elog(WARN, "Cannot allocate polygon for intersection");
|
||||
|
||||
memset((char *) p, 0, sizeof(POLYGON)); /* zero any holes */
|
||||
p->size = sizeof(POLYGON);
|
||||
p->npts = 0;
|
||||
p->boundbox.high.x = Min(a->boundbox.high.x, b->boundbox.high.x);
|
||||
p->boundbox.high.y = Min(a->boundbox.high.y, b->boundbox.high.y);
|
||||
p->boundbox.low.x = Max(a->boundbox.low.x, b->boundbox.low.x);
|
||||
p->boundbox.low.y = Max(a->boundbox.low.y, b->boundbox.low.y);
|
||||
|
||||
if (p->boundbox.high.x < p->boundbox.low.x || p->boundbox.high.y < p->boundbox.low.y)
|
||||
POLYGON *p;
|
||||
|
||||
p = (POLYGON *) PALLOCTYPE(POLYGON);
|
||||
|
||||
if (!PointerIsValid(p))
|
||||
elog(WARN, "Cannot allocate polygon for intersection");
|
||||
|
||||
memset((char *) p, 0, sizeof(POLYGON)); /* zero any holes */
|
||||
p->size = sizeof(POLYGON);
|
||||
p->npts = 0;
|
||||
p->boundbox.high.x = Min(a->boundbox.high.x, b->boundbox.high.x);
|
||||
p->boundbox.high.y = Min(a->boundbox.high.y, b->boundbox.high.y);
|
||||
p->boundbox.low.x = Max(a->boundbox.low.x, b->boundbox.low.x);
|
||||
p->boundbox.low.y = Max(a->boundbox.low.y, b->boundbox.low.y);
|
||||
|
||||
if (p->boundbox.high.x < p->boundbox.low.x || p->boundbox.high.y < p->boundbox.low.y)
|
||||
{
|
||||
pfree(p);
|
||||
return ((POLYGON *) NULL);
|
||||
pfree(p);
|
||||
return ((POLYGON *) NULL);
|
||||
}
|
||||
|
||||
return (p);
|
||||
|
||||
return (p);
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,19 +1,19 @@
|
||||
/*-------------------------------------------------------------------------
|
||||
*
|
||||
* rtscan.c--
|
||||
* routines to manage scans on index relations
|
||||
* routines to manage scans on index relations
|
||||
*
|
||||
* Copyright (c) 1994, Regents of the University of California
|
||||
*
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $Header: /cvsroot/pgsql/src/backend/access/rtree/Attic/rtscan.c,v 1.10 1997/05/20 10:29:30 vadim Exp $
|
||||
* $Header: /cvsroot/pgsql/src/backend/access/rtree/Attic/rtscan.c,v 1.11 1997/09/07 04:39:24 momjian Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
#include <postgres.h>
|
||||
|
||||
|
||||
#include <storage/bufmgr.h>
|
||||
#include <access/genam.h>
|
||||
#include <storage/lmgr.h>
|
||||
@@ -21,377 +21,411 @@
|
||||
#include <access/rtree.h>
|
||||
#include <access/rtstrat.h>
|
||||
#ifndef HAVE_MEMMOVE
|
||||
# include <regex/utils.h>
|
||||
#include <regex/utils.h>
|
||||
#else
|
||||
# include <string.h>
|
||||
#include <string.h>
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
/* routines defined and used here */
|
||||
static void rtregscan(IndexScanDesc s);
|
||||
static void rtdropscan(IndexScanDesc s);
|
||||
static void rtadjone(IndexScanDesc s, int op, BlockNumber blkno,
|
||||
OffsetNumber offnum);
|
||||
static void adjuststack(RTSTACK *stk, BlockNumber blkno,
|
||||
static void rtregscan(IndexScanDesc s);
|
||||
static void rtdropscan(IndexScanDesc s);
|
||||
static void
|
||||
rtadjone(IndexScanDesc s, int op, BlockNumber blkno,
|
||||
OffsetNumber offnum);
|
||||
static void
|
||||
adjuststack(RTSTACK * stk, BlockNumber blkno,
|
||||
OffsetNumber offnum);
|
||||
static void adjustiptr(IndexScanDesc s, ItemPointer iptr,
|
||||
int op, BlockNumber blkno, OffsetNumber offnum);
|
||||
static void
|
||||
adjustiptr(IndexScanDesc s, ItemPointer iptr,
|
||||
int op, BlockNumber blkno, OffsetNumber offnum);
|
||||
|
||||
/*
|
||||
* Whenever we start an rtree scan in a backend, we register it in private
|
||||
* space. Then if the rtree index gets updated, we check all registered
|
||||
* scans and adjust them if the tuple they point at got moved by the
|
||||
* update. We only need to do this in private space, because when we update
|
||||
* an rtree we have a write lock on the tree, so no other process can have
|
||||
* any locks at all on it. A single transaction can have write and read
|
||||
* locks on the same object, so that's why we need to handle this case.
|
||||
* Whenever we start an rtree scan in a backend, we register it in private
|
||||
* space. Then if the rtree index gets updated, we check all registered
|
||||
* scans and adjust them if the tuple they point at got moved by the
|
||||
* update. We only need to do this in private space, because when we update
|
||||
* an rtree we have a write lock on the tree, so no other process can have
|
||||
* any locks at all on it. A single transaction can have write and read
|
||||
* locks on the same object, so that's why we need to handle this case.
|
||||
*/
|
||||
|
||||
typedef struct RTScanListData {
|
||||
IndexScanDesc rtsl_scan;
|
||||
struct RTScanListData *rtsl_next;
|
||||
} RTScanListData;
|
||||
typedef struct RTScanListData
|
||||
{
|
||||
IndexScanDesc rtsl_scan;
|
||||
struct RTScanListData *rtsl_next;
|
||||
} RTScanListData;
|
||||
|
||||
typedef RTScanListData *RTScanList;
|
||||
typedef RTScanListData *RTScanList;
|
||||
|
||||
/* pointer to list of local scans on rtrees */
|
||||
static RTScanList RTScans = (RTScanList) NULL;
|
||||
|
||||
|
||||
IndexScanDesc
|
||||
rtbeginscan(Relation r,
|
||||
bool fromEnd,
|
||||
uint16 nkeys,
|
||||
ScanKey key)
|
||||
bool fromEnd,
|
||||
uint16 nkeys,
|
||||
ScanKey key)
|
||||
{
|
||||
IndexScanDesc s;
|
||||
|
||||
RelationSetLockForRead(r);
|
||||
s = RelationGetIndexScan(r, fromEnd, nkeys, key);
|
||||
rtregscan(s);
|
||||
|
||||
return (s);
|
||||
IndexScanDesc s;
|
||||
|
||||
RelationSetLockForRead(r);
|
||||
s = RelationGetIndexScan(r, fromEnd, nkeys, key);
|
||||
rtregscan(s);
|
||||
|
||||
return (s);
|
||||
}
|
||||
|
||||
void
|
||||
rtrescan(IndexScanDesc s, bool fromEnd, ScanKey key)
|
||||
{
|
||||
RTreeScanOpaque p;
|
||||
RegProcedure internal_proc;
|
||||
int i;
|
||||
|
||||
if (!IndexScanIsValid(s)) {
|
||||
elog(WARN, "rtrescan: invalid scan.");
|
||||
return;
|
||||
}
|
||||
|
||||
/*
|
||||
* Clear all the pointers.
|
||||
*/
|
||||
|
||||
ItemPointerSetInvalid(&s->previousItemData);
|
||||
ItemPointerSetInvalid(&s->currentItemData);
|
||||
ItemPointerSetInvalid(&s->nextItemData);
|
||||
ItemPointerSetInvalid(&s->previousMarkData);
|
||||
ItemPointerSetInvalid(&s->currentMarkData);
|
||||
ItemPointerSetInvalid(&s->nextMarkData);
|
||||
|
||||
/*
|
||||
* Set flags.
|
||||
*/
|
||||
if (RelationGetNumberOfBlocks(s->relation) == 0) {
|
||||
s->flags = ScanUnmarked;
|
||||
} else if (fromEnd) {
|
||||
s->flags = ScanUnmarked | ScanUncheckedPrevious;
|
||||
} else {
|
||||
s->flags = ScanUnmarked | ScanUncheckedNext;
|
||||
}
|
||||
|
||||
s->scanFromEnd = fromEnd;
|
||||
|
||||
if (s->numberOfKeys > 0) {
|
||||
memmove(s->keyData,
|
||||
key,
|
||||
s->numberOfKeys * sizeof(ScanKeyData));
|
||||
}
|
||||
|
||||
p = (RTreeScanOpaque) s->opaque;
|
||||
if (p != (RTreeScanOpaque) NULL) {
|
||||
freestack(p->s_stack);
|
||||
freestack(p->s_markstk);
|
||||
p->s_stack = p->s_markstk = (RTSTACK *) NULL;
|
||||
p->s_flags = 0x0;
|
||||
for (i = 0; i < s->numberOfKeys; i++)
|
||||
RTreeScanOpaque p;
|
||||
RegProcedure internal_proc;
|
||||
int i;
|
||||
|
||||
if (!IndexScanIsValid(s))
|
||||
{
|
||||
p->s_internalKey[i].sk_argument = s->keyData[i].sk_argument;
|
||||
elog(WARN, "rtrescan: invalid scan.");
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
/* initialize opaque data */
|
||||
p = (RTreeScanOpaque) palloc(sizeof(RTreeScanOpaqueData));
|
||||
p->s_stack = p->s_markstk = (RTSTACK *) NULL;
|
||||
p->s_internalNKey = s->numberOfKeys;
|
||||
p->s_flags = 0x0;
|
||||
s->opaque = p;
|
||||
if (s->numberOfKeys > 0) {
|
||||
p->s_internalKey =
|
||||
(ScanKey) palloc(sizeof(ScanKeyData) * s->numberOfKeys);
|
||||
|
||||
/*
|
||||
* Scans on internal pages use different operators than they
|
||||
* do on leaf pages. For example, if the user wants all boxes
|
||||
* that exactly match (x1,y1,x2,y2), then on internal pages
|
||||
* we need to find all boxes that contain (x1,y1,x2,y2).
|
||||
*/
|
||||
|
||||
for (i = 0; i < s->numberOfKeys; i++) {
|
||||
p->s_internalKey[i].sk_argument = s->keyData[i].sk_argument;
|
||||
internal_proc = RTMapOperator(s->relation,
|
||||
s->keyData[i].sk_attno,
|
||||
s->keyData[i].sk_procedure);
|
||||
ScanKeyEntryInitialize(&(p->s_internalKey[i]),
|
||||
s->keyData[i].sk_flags,
|
||||
s->keyData[i].sk_attno,
|
||||
internal_proc,
|
||||
s->keyData[i].sk_argument);
|
||||
}
|
||||
|
||||
/*
|
||||
* Clear all the pointers.
|
||||
*/
|
||||
|
||||
ItemPointerSetInvalid(&s->previousItemData);
|
||||
ItemPointerSetInvalid(&s->currentItemData);
|
||||
ItemPointerSetInvalid(&s->nextItemData);
|
||||
ItemPointerSetInvalid(&s->previousMarkData);
|
||||
ItemPointerSetInvalid(&s->currentMarkData);
|
||||
ItemPointerSetInvalid(&s->nextMarkData);
|
||||
|
||||
/*
|
||||
* Set flags.
|
||||
*/
|
||||
if (RelationGetNumberOfBlocks(s->relation) == 0)
|
||||
{
|
||||
s->flags = ScanUnmarked;
|
||||
}
|
||||
else if (fromEnd)
|
||||
{
|
||||
s->flags = ScanUnmarked | ScanUncheckedPrevious;
|
||||
}
|
||||
else
|
||||
{
|
||||
s->flags = ScanUnmarked | ScanUncheckedNext;
|
||||
}
|
||||
|
||||
s->scanFromEnd = fromEnd;
|
||||
|
||||
if (s->numberOfKeys > 0)
|
||||
{
|
||||
memmove(s->keyData,
|
||||
key,
|
||||
s->numberOfKeys * sizeof(ScanKeyData));
|
||||
}
|
||||
|
||||
p = (RTreeScanOpaque) s->opaque;
|
||||
if (p != (RTreeScanOpaque) NULL)
|
||||
{
|
||||
freestack(p->s_stack);
|
||||
freestack(p->s_markstk);
|
||||
p->s_stack = p->s_markstk = (RTSTACK *) NULL;
|
||||
p->s_flags = 0x0;
|
||||
for (i = 0; i < s->numberOfKeys; i++)
|
||||
{
|
||||
p->s_internalKey[i].sk_argument = s->keyData[i].sk_argument;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
/* initialize opaque data */
|
||||
p = (RTreeScanOpaque) palloc(sizeof(RTreeScanOpaqueData));
|
||||
p->s_stack = p->s_markstk = (RTSTACK *) NULL;
|
||||
p->s_internalNKey = s->numberOfKeys;
|
||||
p->s_flags = 0x0;
|
||||
s->opaque = p;
|
||||
if (s->numberOfKeys > 0)
|
||||
{
|
||||
p->s_internalKey =
|
||||
(ScanKey) palloc(sizeof(ScanKeyData) * s->numberOfKeys);
|
||||
|
||||
/*
|
||||
* Scans on internal pages use different operators than they
|
||||
* do on leaf pages. For example, if the user wants all boxes
|
||||
* that exactly match (x1,y1,x2,y2), then on internal pages we
|
||||
* need to find all boxes that contain (x1,y1,x2,y2).
|
||||
*/
|
||||
|
||||
for (i = 0; i < s->numberOfKeys; i++)
|
||||
{
|
||||
p->s_internalKey[i].sk_argument = s->keyData[i].sk_argument;
|
||||
internal_proc = RTMapOperator(s->relation,
|
||||
s->keyData[i].sk_attno,
|
||||
s->keyData[i].sk_procedure);
|
||||
ScanKeyEntryInitialize(&(p->s_internalKey[i]),
|
||||
s->keyData[i].sk_flags,
|
||||
s->keyData[i].sk_attno,
|
||||
internal_proc,
|
||||
s->keyData[i].sk_argument);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
rtmarkpos(IndexScanDesc s)
|
||||
{
|
||||
RTreeScanOpaque p;
|
||||
RTSTACK *o, *n, *tmp;
|
||||
|
||||
s->currentMarkData = s->currentItemData;
|
||||
p = (RTreeScanOpaque) s->opaque;
|
||||
if (p->s_flags & RTS_CURBEFORE)
|
||||
p->s_flags |= RTS_MRKBEFORE;
|
||||
else
|
||||
p->s_flags &= ~RTS_MRKBEFORE;
|
||||
|
||||
o = (RTSTACK *) NULL;
|
||||
n = p->s_stack;
|
||||
|
||||
/* copy the parent stack from the current item data */
|
||||
while (n != (RTSTACK *) NULL) {
|
||||
tmp = (RTSTACK *) palloc(sizeof(RTSTACK));
|
||||
tmp->rts_child = n->rts_child;
|
||||
tmp->rts_blk = n->rts_blk;
|
||||
tmp->rts_parent = o;
|
||||
o = tmp;
|
||||
n = n->rts_parent;
|
||||
}
|
||||
|
||||
freestack(p->s_markstk);
|
||||
p->s_markstk = o;
|
||||
RTreeScanOpaque p;
|
||||
RTSTACK *o,
|
||||
*n,
|
||||
*tmp;
|
||||
|
||||
s->currentMarkData = s->currentItemData;
|
||||
p = (RTreeScanOpaque) s->opaque;
|
||||
if (p->s_flags & RTS_CURBEFORE)
|
||||
p->s_flags |= RTS_MRKBEFORE;
|
||||
else
|
||||
p->s_flags &= ~RTS_MRKBEFORE;
|
||||
|
||||
o = (RTSTACK *) NULL;
|
||||
n = p->s_stack;
|
||||
|
||||
/* copy the parent stack from the current item data */
|
||||
while (n != (RTSTACK *) NULL)
|
||||
{
|
||||
tmp = (RTSTACK *) palloc(sizeof(RTSTACK));
|
||||
tmp->rts_child = n->rts_child;
|
||||
tmp->rts_blk = n->rts_blk;
|
||||
tmp->rts_parent = o;
|
||||
o = tmp;
|
||||
n = n->rts_parent;
|
||||
}
|
||||
|
||||
freestack(p->s_markstk);
|
||||
p->s_markstk = o;
|
||||
}
|
||||
|
||||
void
|
||||
rtrestrpos(IndexScanDesc s)
|
||||
{
|
||||
RTreeScanOpaque p;
|
||||
RTSTACK *o, *n, *tmp;
|
||||
|
||||
s->currentItemData = s->currentMarkData;
|
||||
p = (RTreeScanOpaque) s->opaque;
|
||||
if (p->s_flags & RTS_MRKBEFORE)
|
||||
p->s_flags |= RTS_CURBEFORE;
|
||||
else
|
||||
p->s_flags &= ~RTS_CURBEFORE;
|
||||
|
||||
o = (RTSTACK *) NULL;
|
||||
n = p->s_markstk;
|
||||
|
||||
/* copy the parent stack from the current item data */
|
||||
while (n != (RTSTACK *) NULL) {
|
||||
tmp = (RTSTACK *) palloc(sizeof(RTSTACK));
|
||||
tmp->rts_child = n->rts_child;
|
||||
tmp->rts_blk = n->rts_blk;
|
||||
tmp->rts_parent = o;
|
||||
o = tmp;
|
||||
n = n->rts_parent;
|
||||
}
|
||||
|
||||
freestack(p->s_stack);
|
||||
p->s_stack = o;
|
||||
RTreeScanOpaque p;
|
||||
RTSTACK *o,
|
||||
*n,
|
||||
*tmp;
|
||||
|
||||
s->currentItemData = s->currentMarkData;
|
||||
p = (RTreeScanOpaque) s->opaque;
|
||||
if (p->s_flags & RTS_MRKBEFORE)
|
||||
p->s_flags |= RTS_CURBEFORE;
|
||||
else
|
||||
p->s_flags &= ~RTS_CURBEFORE;
|
||||
|
||||
o = (RTSTACK *) NULL;
|
||||
n = p->s_markstk;
|
||||
|
||||
/* copy the parent stack from the current item data */
|
||||
while (n != (RTSTACK *) NULL)
|
||||
{
|
||||
tmp = (RTSTACK *) palloc(sizeof(RTSTACK));
|
||||
tmp->rts_child = n->rts_child;
|
||||
tmp->rts_blk = n->rts_blk;
|
||||
tmp->rts_parent = o;
|
||||
o = tmp;
|
||||
n = n->rts_parent;
|
||||
}
|
||||
|
||||
freestack(p->s_stack);
|
||||
p->s_stack = o;
|
||||
}
|
||||
|
||||
void
|
||||
rtendscan(IndexScanDesc s)
|
||||
{
|
||||
RTreeScanOpaque p;
|
||||
|
||||
p = (RTreeScanOpaque) s->opaque;
|
||||
|
||||
if (p != (RTreeScanOpaque) NULL) {
|
||||
freestack(p->s_stack);
|
||||
freestack(p->s_markstk);
|
||||
pfree (s->opaque);
|
||||
}
|
||||
|
||||
rtdropscan(s);
|
||||
/* XXX don't unset read lock -- two-phase locking */
|
||||
RTreeScanOpaque p;
|
||||
|
||||
p = (RTreeScanOpaque) s->opaque;
|
||||
|
||||
if (p != (RTreeScanOpaque) NULL)
|
||||
{
|
||||
freestack(p->s_stack);
|
||||
freestack(p->s_markstk);
|
||||
pfree(s->opaque);
|
||||
}
|
||||
|
||||
rtdropscan(s);
|
||||
/* XXX don't unset read lock -- two-phase locking */
|
||||
}
|
||||
|
||||
static void
|
||||
rtregscan(IndexScanDesc s)
|
||||
{
|
||||
RTScanList l;
|
||||
|
||||
l = (RTScanList) palloc(sizeof(RTScanListData));
|
||||
l->rtsl_scan = s;
|
||||
l->rtsl_next = RTScans;
|
||||
RTScans = l;
|
||||
RTScanList l;
|
||||
|
||||
l = (RTScanList) palloc(sizeof(RTScanListData));
|
||||
l->rtsl_scan = s;
|
||||
l->rtsl_next = RTScans;
|
||||
RTScans = l;
|
||||
}
|
||||
|
||||
static void
|
||||
rtdropscan(IndexScanDesc s)
|
||||
{
|
||||
RTScanList l;
|
||||
RTScanList prev;
|
||||
|
||||
prev = (RTScanList) NULL;
|
||||
|
||||
for (l = RTScans;
|
||||
l != (RTScanList) NULL && l->rtsl_scan != s;
|
||||
l = l->rtsl_next) {
|
||||
prev = l;
|
||||
}
|
||||
|
||||
if (l == (RTScanList) NULL)
|
||||
elog(WARN, "rtree scan list corrupted -- cannot find 0x%lx", s);
|
||||
|
||||
if (prev == (RTScanList) NULL)
|
||||
RTScans = l->rtsl_next;
|
||||
else
|
||||
prev->rtsl_next = l->rtsl_next;
|
||||
|
||||
pfree(l);
|
||||
RTScanList l;
|
||||
RTScanList prev;
|
||||
|
||||
prev = (RTScanList) NULL;
|
||||
|
||||
for (l = RTScans;
|
||||
l != (RTScanList) NULL && l->rtsl_scan != s;
|
||||
l = l->rtsl_next)
|
||||
{
|
||||
prev = l;
|
||||
}
|
||||
|
||||
if (l == (RTScanList) NULL)
|
||||
elog(WARN, "rtree scan list corrupted -- cannot find 0x%lx", s);
|
||||
|
||||
if (prev == (RTScanList) NULL)
|
||||
RTScans = l->rtsl_next;
|
||||
else
|
||||
prev->rtsl_next = l->rtsl_next;
|
||||
|
||||
pfree(l);
|
||||
}
|
||||
|
||||
void
|
||||
rtadjscans(Relation r, int op, BlockNumber blkno, OffsetNumber offnum)
|
||||
{
|
||||
RTScanList l;
|
||||
Oid relid;
|
||||
|
||||
relid = r->rd_id;
|
||||
for (l = RTScans; l != (RTScanList) NULL; l = l->rtsl_next) {
|
||||
if (l->rtsl_scan->relation->rd_id == relid)
|
||||
rtadjone(l->rtsl_scan, op, blkno, offnum);
|
||||
}
|
||||
RTScanList l;
|
||||
Oid relid;
|
||||
|
||||
relid = r->rd_id;
|
||||
for (l = RTScans; l != (RTScanList) NULL; l = l->rtsl_next)
|
||||
{
|
||||
if (l->rtsl_scan->relation->rd_id == relid)
|
||||
rtadjone(l->rtsl_scan, op, blkno, offnum);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* rtadjone() -- adjust one scan for update.
|
||||
* rtadjone() -- adjust one scan for update.
|
||||
*
|
||||
* By here, the scan passed in is on a modified relation. Op tells
|
||||
* us what the modification is, and blkno and offind tell us what
|
||||
* block and offset index were affected. This routine checks the
|
||||
* current and marked positions, and the current and marked stacks,
|
||||
* to see if any stored location needs to be changed because of the
|
||||
* update. If so, we make the change here.
|
||||
* By here, the scan passed in is on a modified relation. Op tells
|
||||
* us what the modification is, and blkno and offind tell us what
|
||||
* block and offset index were affected. This routine checks the
|
||||
* current and marked positions, and the current and marked stacks,
|
||||
* to see if any stored location needs to be changed because of the
|
||||
* update. If so, we make the change here.
|
||||
*/
|
||||
static void
|
||||
rtadjone(IndexScanDesc s,
|
||||
int op,
|
||||
BlockNumber blkno,
|
||||
OffsetNumber offnum)
|
||||
int op,
|
||||
BlockNumber blkno,
|
||||
OffsetNumber offnum)
|
||||
{
|
||||
RTreeScanOpaque so;
|
||||
|
||||
adjustiptr(s, &(s->currentItemData), op, blkno, offnum);
|
||||
adjustiptr(s, &(s->currentMarkData), op, blkno, offnum);
|
||||
|
||||
so = (RTreeScanOpaque) s->opaque;
|
||||
|
||||
if (op == RTOP_SPLIT) {
|
||||
adjuststack(so->s_stack, blkno, offnum);
|
||||
adjuststack(so->s_markstk, blkno, offnum);
|
||||
}
|
||||
RTreeScanOpaque so;
|
||||
|
||||
adjustiptr(s, &(s->currentItemData), op, blkno, offnum);
|
||||
adjustiptr(s, &(s->currentMarkData), op, blkno, offnum);
|
||||
|
||||
so = (RTreeScanOpaque) s->opaque;
|
||||
|
||||
if (op == RTOP_SPLIT)
|
||||
{
|
||||
adjuststack(so->s_stack, blkno, offnum);
|
||||
adjuststack(so->s_markstk, blkno, offnum);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* adjustiptr() -- adjust current and marked item pointers in the scan
|
||||
* adjustiptr() -- adjust current and marked item pointers in the scan
|
||||
*
|
||||
* Depending on the type of update and the place it happened, we
|
||||
* need to do nothing, to back up one record, or to start over on
|
||||
* the same page.
|
||||
* Depending on the type of update and the place it happened, we
|
||||
* need to do nothing, to back up one record, or to start over on
|
||||
* the same page.
|
||||
*/
|
||||
static void
|
||||
adjustiptr(IndexScanDesc s,
|
||||
ItemPointer iptr,
|
||||
int op,
|
||||
BlockNumber blkno,
|
||||
OffsetNumber offnum)
|
||||
ItemPointer iptr,
|
||||
int op,
|
||||
BlockNumber blkno,
|
||||
OffsetNumber offnum)
|
||||
{
|
||||
OffsetNumber curoff;
|
||||
RTreeScanOpaque so;
|
||||
|
||||
if (ItemPointerIsValid(iptr)) {
|
||||
if (ItemPointerGetBlockNumber(iptr) == blkno) {
|
||||
curoff = ItemPointerGetOffsetNumber(iptr);
|
||||
so = (RTreeScanOpaque) s->opaque;
|
||||
|
||||
switch (op) {
|
||||
case RTOP_DEL:
|
||||
/* back up one if we need to */
|
||||
if (curoff >= offnum) {
|
||||
|
||||
if (curoff > FirstOffsetNumber) {
|
||||
/* just adjust the item pointer */
|
||||
ItemPointerSet(iptr, blkno, OffsetNumberPrev(curoff));
|
||||
} else {
|
||||
/* remember that we're before the current tuple */
|
||||
ItemPointerSet(iptr, blkno, FirstOffsetNumber);
|
||||
if (iptr == &(s->currentItemData))
|
||||
so->s_flags |= RTS_CURBEFORE;
|
||||
else
|
||||
so->s_flags |= RTS_MRKBEFORE;
|
||||
}
|
||||
OffsetNumber curoff;
|
||||
RTreeScanOpaque so;
|
||||
|
||||
if (ItemPointerIsValid(iptr))
|
||||
{
|
||||
if (ItemPointerGetBlockNumber(iptr) == blkno)
|
||||
{
|
||||
curoff = ItemPointerGetOffsetNumber(iptr);
|
||||
so = (RTreeScanOpaque) s->opaque;
|
||||
|
||||
switch (op)
|
||||
{
|
||||
case RTOP_DEL:
|
||||
/* back up one if we need to */
|
||||
if (curoff >= offnum)
|
||||
{
|
||||
|
||||
if (curoff > FirstOffsetNumber)
|
||||
{
|
||||
/* just adjust the item pointer */
|
||||
ItemPointerSet(iptr, blkno, OffsetNumberPrev(curoff));
|
||||
}
|
||||
else
|
||||
{
|
||||
/* remember that we're before the current tuple */
|
||||
ItemPointerSet(iptr, blkno, FirstOffsetNumber);
|
||||
if (iptr == &(s->currentItemData))
|
||||
so->s_flags |= RTS_CURBEFORE;
|
||||
else
|
||||
so->s_flags |= RTS_MRKBEFORE;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case RTOP_SPLIT:
|
||||
/* back to start of page on split */
|
||||
ItemPointerSet(iptr, blkno, FirstOffsetNumber);
|
||||
if (iptr == &(s->currentItemData))
|
||||
so->s_flags &= ~RTS_CURBEFORE;
|
||||
else
|
||||
so->s_flags &= ~RTS_MRKBEFORE;
|
||||
break;
|
||||
|
||||
default:
|
||||
elog(WARN, "Bad operation in rtree scan adjust: %d", op);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case RTOP_SPLIT:
|
||||
/* back to start of page on split */
|
||||
ItemPointerSet(iptr, blkno, FirstOffsetNumber);
|
||||
if (iptr == &(s->currentItemData))
|
||||
so->s_flags &= ~RTS_CURBEFORE;
|
||||
else
|
||||
so->s_flags &= ~RTS_MRKBEFORE;
|
||||
break;
|
||||
|
||||
default:
|
||||
elog(WARN, "Bad operation in rtree scan adjust: %d", op);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* adjuststack() -- adjust the supplied stack for a split on a page in
|
||||
* the index we're scanning.
|
||||
* adjuststack() -- adjust the supplied stack for a split on a page in
|
||||
* the index we're scanning.
|
||||
*
|
||||
* If a page on our parent stack has split, we need to back up to the
|
||||
* beginning of the page and rescan it. The reason for this is that
|
||||
* the split algorithm for rtrees doesn't order tuples in any useful
|
||||
* way on a single page. This means on that a split, we may wind up
|
||||
* looking at some heap tuples more than once. This is handled in the
|
||||
* access method update code for heaps; if we've modified the tuple we
|
||||
* are looking at already in this transaction, we ignore the update
|
||||
* request.
|
||||
* If a page on our parent stack has split, we need to back up to the
|
||||
* beginning of the page and rescan it. The reason for this is that
|
||||
* the split algorithm for rtrees doesn't order tuples in any useful
|
||||
* way on a single page. This means on that a split, we may wind up
|
||||
* looking at some heap tuples more than once. This is handled in the
|
||||
* access method update code for heaps; if we've modified the tuple we
|
||||
* are looking at already in this transaction, we ignore the update
|
||||
* request.
|
||||
*/
|
||||
/*ARGSUSED*/
|
||||
static void
|
||||
adjuststack(RTSTACK *stk,
|
||||
BlockNumber blkno,
|
||||
OffsetNumber offnum)
|
||||
adjuststack(RTSTACK * stk,
|
||||
BlockNumber blkno,
|
||||
OffsetNumber offnum)
|
||||
{
|
||||
while (stk != (RTSTACK *) NULL) {
|
||||
if (stk->rts_blk == blkno)
|
||||
stk->rts_child = FirstOffsetNumber;
|
||||
|
||||
stk = stk->rts_parent;
|
||||
}
|
||||
while (stk != (RTSTACK *) NULL)
|
||||
{
|
||||
if (stk->rts_blk == blkno)
|
||||
stk->rts_child = FirstOffsetNumber;
|
||||
|
||||
stk = stk->rts_parent;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,241 +1,243 @@
|
||||
/*-------------------------------------------------------------------------
|
||||
*
|
||||
* rtstrat.c--
|
||||
* strategy map data for rtrees.
|
||||
* strategy map data for rtrees.
|
||||
*
|
||||
* Copyright (c) 1994, Regents of the University of California
|
||||
*
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $Header: /cvsroot/pgsql/src/backend/access/rtree/Attic/rtstrat.c,v 1.6 1997/08/19 21:29:52 momjian Exp $
|
||||
* $Header: /cvsroot/pgsql/src/backend/access/rtree/Attic/rtstrat.c,v 1.7 1997/09/07 04:39:26 momjian Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
#include <postgres.h>
|
||||
|
||||
|
||||
#include <utils/rel.h>
|
||||
#include <access/rtree.h>
|
||||
#include <access/istrat.h>
|
||||
|
||||
static StrategyNumber RelationGetRTStrategy(Relation r,
|
||||
AttrNumber attnum, RegProcedure proc);
|
||||
static StrategyNumber
|
||||
RelationGetRTStrategy(Relation r,
|
||||
AttrNumber attnum, RegProcedure proc);
|
||||
|
||||
/*
|
||||
* Note: negate, commute, and negatecommute all assume that operators are
|
||||
* ordered as follows in the strategy map:
|
||||
* Note: negate, commute, and negatecommute all assume that operators are
|
||||
* ordered as follows in the strategy map:
|
||||
*
|
||||
* left, left-or-overlap, overlap, right-or-overlap, right, same,
|
||||
* contains, contained-by
|
||||
* left, left-or-overlap, overlap, right-or-overlap, right, same,
|
||||
* contains, contained-by
|
||||
*
|
||||
* The negate, commute, and negatecommute arrays are used by the planner
|
||||
* to plan indexed scans over data that appears in the qualificiation in
|
||||
* a boolean negation, or whose operands appear in the wrong order. For
|
||||
* example, if the operator "<%" means "contains", and the user says
|
||||
* The negate, commute, and negatecommute arrays are used by the planner
|
||||
* to plan indexed scans over data that appears in the qualificiation in
|
||||
* a boolean negation, or whose operands appear in the wrong order. For
|
||||
* example, if the operator "<%" means "contains", and the user says
|
||||
*
|
||||
* where not rel.box <% "(10,10,20,20)"::box
|
||||
* where not rel.box <% "(10,10,20,20)"::box
|
||||
*
|
||||
* the planner can plan an index scan by noting that rtree indices have
|
||||
* an operator in their operator class for negating <%.
|
||||
* the planner can plan an index scan by noting that rtree indices have
|
||||
* an operator in their operator class for negating <%.
|
||||
*
|
||||
* Similarly, if the user says something like
|
||||
* Similarly, if the user says something like
|
||||
*
|
||||
* where "(10,10,20,20)"::box <% rel.box
|
||||
* where "(10,10,20,20)"::box <% rel.box
|
||||
*
|
||||
* the planner can see that the rtree index on rel.box has an operator in
|
||||
* its opclass for commuting <%, and plan the scan using that operator.
|
||||
* This added complexity in the access methods makes the planner a lot easier
|
||||
* to write.
|
||||
* the planner can see that the rtree index on rel.box has an operator in
|
||||
* its opclass for commuting <%, and plan the scan using that operator.
|
||||
* This added complexity in the access methods makes the planner a lot easier
|
||||
* to write.
|
||||
*/
|
||||
|
||||
/* if a op b, what operator tells us if (not a op b)? */
|
||||
static StrategyNumber RTNegate[RTNStrategies] = {
|
||||
InvalidStrategy,
|
||||
InvalidStrategy,
|
||||
InvalidStrategy,
|
||||
InvalidStrategy,
|
||||
InvalidStrategy,
|
||||
InvalidStrategy,
|
||||
InvalidStrategy,
|
||||
InvalidStrategy
|
||||
};
|
||||
static StrategyNumber RTNegate[RTNStrategies] = {
|
||||
InvalidStrategy,
|
||||
InvalidStrategy,
|
||||
InvalidStrategy,
|
||||
InvalidStrategy,
|
||||
InvalidStrategy,
|
||||
InvalidStrategy,
|
||||
InvalidStrategy,
|
||||
InvalidStrategy
|
||||
};
|
||||
|
||||
/* if a op_1 b, what is the operator op_2 such that b op_2 a? */
|
||||
static StrategyNumber RTCommute[RTNStrategies] = {
|
||||
InvalidStrategy,
|
||||
InvalidStrategy,
|
||||
InvalidStrategy,
|
||||
InvalidStrategy,
|
||||
InvalidStrategy,
|
||||
InvalidStrategy,
|
||||
InvalidStrategy,
|
||||
InvalidStrategy
|
||||
};
|
||||
static StrategyNumber RTCommute[RTNStrategies] = {
|
||||
InvalidStrategy,
|
||||
InvalidStrategy,
|
||||
InvalidStrategy,
|
||||
InvalidStrategy,
|
||||
InvalidStrategy,
|
||||
InvalidStrategy,
|
||||
InvalidStrategy,
|
||||
InvalidStrategy
|
||||
};
|
||||
|
||||
/* if a op_1 b, what is the operator op_2 such that (b !op_2 a)? */
|
||||
static StrategyNumber RTNegateCommute[RTNStrategies] = {
|
||||
InvalidStrategy,
|
||||
InvalidStrategy,
|
||||
InvalidStrategy,
|
||||
InvalidStrategy,
|
||||
InvalidStrategy,
|
||||
InvalidStrategy,
|
||||
InvalidStrategy,
|
||||
InvalidStrategy
|
||||
};
|
||||
|
||||
/*
|
||||
* Now do the TermData arrays. These exist in case the user doesn't give
|
||||
* us a full set of operators for a particular operator class. The idea
|
||||
* is that by making multiple comparisons using any one of the supplied
|
||||
* operators, we can decide whether two n-dimensional polygons are equal.
|
||||
* For example, if a contains b and b contains a, we may conclude that
|
||||
* a and b are equal.
|
||||
*
|
||||
* The presence of the TermData arrays in all this is a historical accident.
|
||||
* Early in the development of the POSTGRES access methods, it was believed
|
||||
* that writing functions was harder than writing arrays. This is wrong;
|
||||
* TermData is hard to understand and hard to get right. In general, when
|
||||
* someone populates a new operator class, the populate it completely. If
|
||||
* Mike Hirohama had forced Cimarron Taylor to populate the strategy map
|
||||
* for btree int2_ops completely in 1988, you wouldn't have to deal with
|
||||
* all this now. Too bad for you.
|
||||
*
|
||||
* Since you can't necessarily do this in all cases (for example, you can't
|
||||
* do it given only "intersects" or "disjoint"), TermData arrays for some
|
||||
* operators don't appear below.
|
||||
*
|
||||
* Note that if you DO supply all the operators required in a given opclass
|
||||
* by inserting them into the pg_opclass system catalog, you can get away
|
||||
* without doing all this TermData stuff. Since the rtree code is intended
|
||||
* to be a reference for access method implementors, I'm doing TermData
|
||||
* correctly here.
|
||||
*
|
||||
* Note on style: these are all actually of type StrategyTermData, but
|
||||
* since those have variable-length data at the end of the struct we can't
|
||||
* properly initialize them if we declare them to be what they are.
|
||||
*/
|
||||
|
||||
/* if you only have "contained-by", how do you determine equality? */
|
||||
static uint16 RTContainedByTermData[] = {
|
||||
2, /* make two comparisons */
|
||||
RTContainedByStrategyNumber, /* use "a contained-by b" */
|
||||
0x0, /* without any magic */
|
||||
RTContainedByStrategyNumber, /* then use contained-by, */
|
||||
SK_COMMUTE /* swapping a and b */
|
||||
};
|
||||
|
||||
/* if you only have "contains", how do you determine equality? */
|
||||
static uint16 RTContainsTermData[] = {
|
||||
2, /* make two comparisons */
|
||||
RTContainsStrategyNumber, /* use "a contains b" */
|
||||
0x0, /* without any magic */
|
||||
RTContainsStrategyNumber, /* then use contains again, */
|
||||
SK_COMMUTE /* swapping a and b */
|
||||
};
|
||||
|
||||
/* now put all that together in one place for the planner */
|
||||
static StrategyTerm RTEqualExpressionData[] = {
|
||||
(StrategyTerm) RTContainedByTermData,
|
||||
(StrategyTerm) RTContainsTermData,
|
||||
NULL
|
||||
};
|
||||
|
||||
/*
|
||||
* If you were sufficiently attentive to detail, you would go through
|
||||
* the ExpressionData pain above for every one of the seven strategies
|
||||
* we defined. I am not. Now we declare the StrategyEvaluationData
|
||||
* structure that gets shipped around to help the planner and the access
|
||||
* method decide what sort of scan it should do, based on (a) what the
|
||||
* user asked for, (b) what operators are defined for a particular opclass,
|
||||
* and (c) the reams of information we supplied above.
|
||||
*
|
||||
* The idea of all of this initialized data is to make life easier on the
|
||||
* user when he defines a new operator class to use this access method.
|
||||
* By filling in all the data, we let him get away with leaving holes in his
|
||||
* operator class, and still let him use the index. The added complexity
|
||||
* in the access methods just isn't worth the trouble, though.
|
||||
*/
|
||||
|
||||
static StrategyEvaluationData RTEvaluationData = {
|
||||
RTNStrategies, /* # of strategies */
|
||||
(StrategyTransformMap) RTNegate, /* how to do (not qual) */
|
||||
(StrategyTransformMap) RTCommute, /* how to swap operands */
|
||||
(StrategyTransformMap) RTNegateCommute, /* how to do both */
|
||||
{
|
||||
NULL, /* express left */
|
||||
NULL, /* express overleft */
|
||||
NULL, /* express over */
|
||||
NULL, /* express overright */
|
||||
NULL, /* express right */
|
||||
(StrategyExpression) RTEqualExpressionData, /* express same */
|
||||
NULL, /* express contains */
|
||||
NULL, /* express contained-by */
|
||||
NULL,
|
||||
NULL,
|
||||
NULL
|
||||
}
|
||||
static StrategyNumber RTNegateCommute[RTNStrategies] = {
|
||||
InvalidStrategy,
|
||||
InvalidStrategy,
|
||||
InvalidStrategy,
|
||||
InvalidStrategy,
|
||||
InvalidStrategy,
|
||||
InvalidStrategy,
|
||||
InvalidStrategy,
|
||||
InvalidStrategy
|
||||
};
|
||||
|
||||
/*
|
||||
* Okay, now something peculiar to rtrees that doesn't apply to most other
|
||||
* indexing structures: When we're searching a tree for a given value, we
|
||||
* can't do the same sorts of comparisons on internal node entries as we
|
||||
* do at leaves. The reason is that if we're looking for (say) all boxes
|
||||
* that are the same as (0,0,10,10), then we need to find all leaf pages
|
||||
* that overlap that region. So internally we search for overlap, and at
|
||||
* the leaf we search for equality.
|
||||
* Now do the TermData arrays. These exist in case the user doesn't give
|
||||
* us a full set of operators for a particular operator class. The idea
|
||||
* is that by making multiple comparisons using any one of the supplied
|
||||
* operators, we can decide whether two n-dimensional polygons are equal.
|
||||
* For example, if a contains b and b contains a, we may conclude that
|
||||
* a and b are equal.
|
||||
*
|
||||
* This array maps leaf search operators to the internal search operators.
|
||||
* We assume the normal ordering on operators:
|
||||
* The presence of the TermData arrays in all this is a historical accident.
|
||||
* Early in the development of the POSTGRES access methods, it was believed
|
||||
* that writing functions was harder than writing arrays. This is wrong;
|
||||
* TermData is hard to understand and hard to get right. In general, when
|
||||
* someone populates a new operator class, the populate it completely. If
|
||||
* Mike Hirohama had forced Cimarron Taylor to populate the strategy map
|
||||
* for btree int2_ops completely in 1988, you wouldn't have to deal with
|
||||
* all this now. Too bad for you.
|
||||
*
|
||||
* left, left-or-overlap, overlap, right-or-overlap, right, same,
|
||||
* contains, contained-by
|
||||
* Since you can't necessarily do this in all cases (for example, you can't
|
||||
* do it given only "intersects" or "disjoint"), TermData arrays for some
|
||||
* operators don't appear below.
|
||||
*
|
||||
* Note that if you DO supply all the operators required in a given opclass
|
||||
* by inserting them into the pg_opclass system catalog, you can get away
|
||||
* without doing all this TermData stuff. Since the rtree code is intended
|
||||
* to be a reference for access method implementors, I'm doing TermData
|
||||
* correctly here.
|
||||
*
|
||||
* Note on style: these are all actually of type StrategyTermData, but
|
||||
* since those have variable-length data at the end of the struct we can't
|
||||
* properly initialize them if we declare them to be what they are.
|
||||
*/
|
||||
|
||||
/* if you only have "contained-by", how do you determine equality? */
|
||||
static uint16 RTContainedByTermData[] = {
|
||||
2, /* make two comparisons */
|
||||
RTContainedByStrategyNumber,/* use "a contained-by b" */
|
||||
0x0, /* without any magic */
|
||||
RTContainedByStrategyNumber,/* then use contained-by, */
|
||||
SK_COMMUTE /* swapping a and b */
|
||||
};
|
||||
|
||||
/* if you only have "contains", how do you determine equality? */
|
||||
static uint16 RTContainsTermData[] = {
|
||||
2, /* make two comparisons */
|
||||
RTContainsStrategyNumber, /* use "a contains b" */
|
||||
0x0, /* without any magic */
|
||||
RTContainsStrategyNumber, /* then use contains again, */
|
||||
SK_COMMUTE /* swapping a and b */
|
||||
};
|
||||
|
||||
/* now put all that together in one place for the planner */
|
||||
static StrategyTerm RTEqualExpressionData[] = {
|
||||
(StrategyTerm) RTContainedByTermData,
|
||||
(StrategyTerm) RTContainsTermData,
|
||||
NULL
|
||||
};
|
||||
|
||||
/*
|
||||
* If you were sufficiently attentive to detail, you would go through
|
||||
* the ExpressionData pain above for every one of the seven strategies
|
||||
* we defined. I am not. Now we declare the StrategyEvaluationData
|
||||
* structure that gets shipped around to help the planner and the access
|
||||
* method decide what sort of scan it should do, based on (a) what the
|
||||
* user asked for, (b) what operators are defined for a particular opclass,
|
||||
* and (c) the reams of information we supplied above.
|
||||
*
|
||||
* The idea of all of this initialized data is to make life easier on the
|
||||
* user when he defines a new operator class to use this access method.
|
||||
* By filling in all the data, we let him get away with leaving holes in his
|
||||
* operator class, and still let him use the index. The added complexity
|
||||
* in the access methods just isn't worth the trouble, though.
|
||||
*/
|
||||
|
||||
static StrategyEvaluationData RTEvaluationData = {
|
||||
RTNStrategies, /* # of strategies */
|
||||
(StrategyTransformMap) RTNegate, /* how to do (not qual) */
|
||||
(StrategyTransformMap) RTCommute, /* how to swap operands */
|
||||
(StrategyTransformMap) RTNegateCommute, /* how to do both */
|
||||
{
|
||||
NULL, /* express left */
|
||||
NULL, /* express overleft */
|
||||
NULL, /* express over */
|
||||
NULL, /* express overright */
|
||||
NULL, /* express right */
|
||||
(StrategyExpression) RTEqualExpressionData, /* express same */
|
||||
NULL, /* express contains */
|
||||
NULL, /* express contained-by */
|
||||
NULL,
|
||||
NULL,
|
||||
NULL
|
||||
}
|
||||
};
|
||||
|
||||
/*
|
||||
* Okay, now something peculiar to rtrees that doesn't apply to most other
|
||||
* indexing structures: When we're searching a tree for a given value, we
|
||||
* can't do the same sorts of comparisons on internal node entries as we
|
||||
* do at leaves. The reason is that if we're looking for (say) all boxes
|
||||
* that are the same as (0,0,10,10), then we need to find all leaf pages
|
||||
* that overlap that region. So internally we search for overlap, and at
|
||||
* the leaf we search for equality.
|
||||
*
|
||||
* This array maps leaf search operators to the internal search operators.
|
||||
* We assume the normal ordering on operators:
|
||||
*
|
||||
* left, left-or-overlap, overlap, right-or-overlap, right, same,
|
||||
* contains, contained-by
|
||||
*/
|
||||
static StrategyNumber RTOperMap[RTNStrategies] = {
|
||||
RTOverLeftStrategyNumber,
|
||||
RTOverLeftStrategyNumber,
|
||||
RTOverlapStrategyNumber,
|
||||
RTOverRightStrategyNumber,
|
||||
RTOverRightStrategyNumber,
|
||||
RTContainsStrategyNumber,
|
||||
RTContainsStrategyNumber,
|
||||
RTOverlapStrategyNumber
|
||||
};
|
||||
RTOverLeftStrategyNumber,
|
||||
RTOverLeftStrategyNumber,
|
||||
RTOverlapStrategyNumber,
|
||||
RTOverRightStrategyNumber,
|
||||
RTOverRightStrategyNumber,
|
||||
RTContainsStrategyNumber,
|
||||
RTContainsStrategyNumber,
|
||||
RTOverlapStrategyNumber
|
||||
};
|
||||
|
||||
static StrategyNumber
|
||||
static StrategyNumber
|
||||
RelationGetRTStrategy(Relation r,
|
||||
AttrNumber attnum,
|
||||
RegProcedure proc)
|
||||
AttrNumber attnum,
|
||||
RegProcedure proc)
|
||||
{
|
||||
return (RelationGetStrategy(r, attnum, &RTEvaluationData, proc));
|
||||
return (RelationGetStrategy(r, attnum, &RTEvaluationData, proc));
|
||||
}
|
||||
|
||||
#ifdef NOT_USED
|
||||
bool
|
||||
RelationInvokeRTStrategy(Relation r,
|
||||
AttrNumber attnum,
|
||||
StrategyNumber s,
|
||||
Datum left,
|
||||
Datum right)
|
||||
AttrNumber attnum,
|
||||
StrategyNumber s,
|
||||
Datum left,
|
||||
Datum right)
|
||||
{
|
||||
return (RelationInvokeStrategy(r, &RTEvaluationData, attnum, s,
|
||||
left, right));
|
||||
return (RelationInvokeStrategy(r, &RTEvaluationData, attnum, s,
|
||||
left, right));
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
RegProcedure
|
||||
RTMapOperator(Relation r,
|
||||
AttrNumber attnum,
|
||||
RegProcedure proc)
|
||||
AttrNumber attnum,
|
||||
RegProcedure proc)
|
||||
{
|
||||
StrategyNumber procstrat;
|
||||
StrategyMap strategyMap;
|
||||
|
||||
procstrat = RelationGetRTStrategy(r, attnum, proc);
|
||||
strategyMap = IndexStrategyGetStrategyMap(RelationGetIndexStrategy(r),
|
||||
RTNStrategies,
|
||||
attnum);
|
||||
|
||||
return (strategyMap->entry[RTOperMap[procstrat - 1] - 1].sk_procedure);
|
||||
StrategyNumber procstrat;
|
||||
StrategyMap strategyMap;
|
||||
|
||||
procstrat = RelationGetRTStrategy(r, attnum, proc);
|
||||
strategyMap = IndexStrategyGetStrategyMap(RelationGetIndexStrategy(r),
|
||||
RTNStrategies,
|
||||
attnum);
|
||||
|
||||
return (strategyMap->entry[RTOperMap[procstrat - 1] - 1].sk_procedure);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user