Cleanup the usage of ScanDirection: use the symbolic names for the

possible ScanDirection alternatives rather than magic numbers
(-1, 0, 1).  Also, use the ScanDirection macros in a few places
rather than directly checking whether `dir == ForwardScanDirection'
and the like. Per patch from James William Pye. His patch also
changed ScanDirection to be a "char" rather than an enum, which
I haven't applied.
This commit is contained in:
Neil Conway 2006-02-21 23:01:54 +00:00
parent 3666260ffd
commit 737651f6be
3 changed files with 30 additions and 48 deletions

View File

@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/access/heap/heapam.c,v 1.206 2006/01/11 08:43:11 neilc Exp $ * $PostgreSQL: pgsql/src/backend/access/heap/heapam.c,v 1.207 2006/02/21 23:01:53 neilc Exp $
* *
* *
* INTERFACE ROUTINES * INTERFACE ROUTINES
@ -172,7 +172,8 @@ heapgetpage(HeapScanDesc scan, BlockNumber page)
* tuple as indicated by "dir"; return the next tuple in scan->rs_ctup, * tuple as indicated by "dir"; return the next tuple in scan->rs_ctup,
* or set scan->rs_ctup.t_data = NULL if no more tuples. * or set scan->rs_ctup.t_data = NULL if no more tuples.
* *
* dir == 0 means "re-fetch the tuple indicated by scan->rs_ctup". * dir == NoMovementScanDirection means "re-fetch the tuple indicated
* by scan->rs_ctup".
* *
* Note: the reason nkeys/key are passed separately, even though they are * Note: the reason nkeys/key are passed separately, even though they are
* kept in the scan descriptor, is that the caller may not want us to check * kept in the scan descriptor, is that the caller may not want us to check
@ -189,12 +190,13 @@ heapgetpage(HeapScanDesc scan, BlockNumber page)
*/ */
static void static void
heapgettup(HeapScanDesc scan, heapgettup(HeapScanDesc scan,
int dir, ScanDirection dir,
int nkeys, int nkeys,
ScanKey key) ScanKey key)
{ {
HeapTuple tuple = &(scan->rs_ctup); HeapTuple tuple = &(scan->rs_ctup);
Snapshot snapshot = scan->rs_snapshot; Snapshot snapshot = scan->rs_snapshot;
bool backward = ScanDirectionIsBackward(dir);
BlockNumber page; BlockNumber page;
Page dp; Page dp;
int lines; int lines;
@ -205,11 +207,8 @@ heapgettup(HeapScanDesc scan,
/* /*
* calculate next starting lineoff, given scan direction * calculate next starting lineoff, given scan direction
*/ */
if (dir > 0) if (ScanDirectionIsForward(dir))
{ {
/*
* forward scan direction
*/
if (!scan->rs_inited) if (!scan->rs_inited)
{ {
/* /*
@ -242,11 +241,8 @@ heapgettup(HeapScanDesc scan,
linesleft = lines - lineoff + 1; linesleft = lines - lineoff + 1;
} }
else if (dir < 0) else if (backward)
{ {
/*
* reverse scan direction
*/
if (!scan->rs_inited) if (!scan->rs_inited)
{ {
/* /*
@ -352,7 +348,7 @@ heapgettup(HeapScanDesc scan,
* otherwise move to the next item on the page * otherwise move to the next item on the page
*/ */
--linesleft; --linesleft;
if (dir < 0) if (backward)
{ {
--lpp; /* move back in this page's ItemId array */ --lpp; /* move back in this page's ItemId array */
--lineoff; --lineoff;
@ -373,7 +369,7 @@ heapgettup(HeapScanDesc scan,
/* /*
* return NULL if we've exhausted all the pages * return NULL if we've exhausted all the pages
*/ */
if ((dir < 0) ? (page == 0) : (page + 1 >= scan->rs_nblocks)) if (backward ? (page == 0) : (page + 1 >= scan->rs_nblocks))
{ {
if (BufferIsValid(scan->rs_cbuf)) if (BufferIsValid(scan->rs_cbuf))
ReleaseBuffer(scan->rs_cbuf); ReleaseBuffer(scan->rs_cbuf);
@ -384,7 +380,7 @@ heapgettup(HeapScanDesc scan,
return; return;
} }
page = (dir < 0) ? (page - 1) : (page + 1); page = backward ? (page - 1) : (page + 1);
heapgetpage(scan, page); heapgetpage(scan, page);
@ -393,7 +389,7 @@ heapgettup(HeapScanDesc scan,
dp = (Page) BufferGetPage(scan->rs_cbuf); dp = (Page) BufferGetPage(scan->rs_cbuf);
lines = PageGetMaxOffsetNumber((Page) dp); lines = PageGetMaxOffsetNumber((Page) dp);
linesleft = lines; linesleft = lines;
if (dir < 0) if (backward)
{ {
lineoff = lines; lineoff = lines;
lpp = PageGetItemId(dp, lines); lpp = PageGetItemId(dp, lines);
@ -421,11 +417,12 @@ heapgettup(HeapScanDesc scan,
*/ */
static void static void
heapgettup_pagemode(HeapScanDesc scan, heapgettup_pagemode(HeapScanDesc scan,
int dir, ScanDirection dir,
int nkeys, int nkeys,
ScanKey key) ScanKey key)
{ {
HeapTuple tuple = &(scan->rs_ctup); HeapTuple tuple = &(scan->rs_ctup);
bool backward = ScanDirectionIsBackward(dir);
BlockNumber page; BlockNumber page;
Page dp; Page dp;
int lines; int lines;
@ -437,11 +434,8 @@ heapgettup_pagemode(HeapScanDesc scan,
/* /*
* calculate next starting lineindex, given scan direction * calculate next starting lineindex, given scan direction
*/ */
if (dir > 0) if (ScanDirectionIsForward(dir))
{ {
/*
* forward scan direction
*/
if (!scan->rs_inited) if (!scan->rs_inited)
{ {
/* /*
@ -471,11 +465,8 @@ heapgettup_pagemode(HeapScanDesc scan,
linesleft = lines - lineindex; linesleft = lines - lineindex;
} }
else if (dir < 0) else if (backward)
{ {
/*
* reverse scan direction
*/
if (!scan->rs_inited) if (!scan->rs_inited)
{ {
/* /*
@ -584,14 +575,10 @@ heapgettup_pagemode(HeapScanDesc scan,
* otherwise move to the next item on the page * otherwise move to the next item on the page
*/ */
--linesleft; --linesleft;
if (dir < 0) if (backward)
{
--lineindex; --lineindex;
}
else else
{
++lineindex; ++lineindex;
}
} }
/* /*
@ -602,7 +589,7 @@ heapgettup_pagemode(HeapScanDesc scan,
/* /*
* return NULL if we've exhausted all the pages * return NULL if we've exhausted all the pages
*/ */
if ((dir < 0) ? (page == 0) : (page + 1 >= scan->rs_nblocks)) if (backward ? (page == 0) : (page + 1 >= scan->rs_nblocks))
{ {
if (BufferIsValid(scan->rs_cbuf)) if (BufferIsValid(scan->rs_cbuf))
ReleaseBuffer(scan->rs_cbuf); ReleaseBuffer(scan->rs_cbuf);
@ -613,14 +600,13 @@ heapgettup_pagemode(HeapScanDesc scan,
return; return;
} }
page = (dir < 0) ? (page - 1) : (page + 1); page = backward ? (page - 1) : (page + 1);
heapgetpage(scan, page); heapgetpage(scan, page);
dp = (Page) BufferGetPage(scan->rs_cbuf); dp = (Page) BufferGetPage(scan->rs_cbuf);
lines = scan->rs_ntuples; lines = scan->rs_ntuples;
linesleft = lines; linesleft = lines;
if (dir < 0) if (backward)
lineindex = lines - 1; lineindex = lines - 1;
else else
lineindex = 0; lineindex = 0;
@ -1008,15 +994,11 @@ heap_getnext(HeapScanDesc scan, ScanDirection direction)
HEAPDEBUG_1; /* heap_getnext( info ) */ HEAPDEBUG_1; /* heap_getnext( info ) */
/*
* Note: we depend here on the -1/0/1 encoding of ScanDirection.
*/
if (scan->rs_pageatatime) if (scan->rs_pageatatime)
heapgettup_pagemode(scan, (int) direction, heapgettup_pagemode(scan, direction,
scan->rs_nkeys, scan->rs_key); scan->rs_nkeys, scan->rs_key);
else else
heapgettup(scan, (int) direction, heapgettup(scan, direction, scan->rs_nkeys, scan->rs_key);
scan->rs_nkeys, scan->rs_key);
if (scan->rs_ctup.t_data == NULL) if (scan->rs_ctup.t_data == NULL)
{ {
@ -2745,13 +2727,13 @@ heap_restrpos(HeapScanDesc scan)
{ {
scan->rs_cindex = scan->rs_mindex; scan->rs_cindex = scan->rs_mindex;
heapgettup_pagemode(scan, heapgettup_pagemode(scan,
0, /* "no movement" */ NoMovementScanDirection,
0, /* needn't recheck scan keys */ 0, /* needn't recheck scan keys */
NULL); NULL);
} }
else else
heapgettup(scan, heapgettup(scan,
0, /* "no movement" */ NoMovementScanDirection,
0, /* needn't recheck scan keys */ 0, /* needn't recheck scan keys */
NULL); NULL);
} }

View File

@ -26,7 +26,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/executor/execMain.c,v 1.266 2006/02/19 00:04:26 neilc Exp $ * $PostgreSQL: pgsql/src/backend/executor/execMain.c,v 1.267 2006/02/21 23:01:54 neilc Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
@ -218,7 +218,7 @@ ExecutorRun(QueryDesc *queryDesc,
/* /*
* run plan * run plan
*/ */
if (direction == NoMovementScanDirection) if (ScanDirectionIsNoMovement(direction))
result = NULL; result = NULL;
else else
result = ExecutePlan(estate, result = ExecutePlan(estate,

View File

@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/tcop/pquery.c,v 1.98 2005/11/22 18:17:21 momjian Exp $ * $PostgreSQL: pgsql/src/backend/tcop/pquery.c,v 1.99 2006/02/21 23:01:54 neilc Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
@ -795,7 +795,7 @@ PortalRunSelect(Portal portal,
nprocessed = queryDesc->estate->es_processed; nprocessed = queryDesc->estate->es_processed;
} }
if (direction != NoMovementScanDirection) if (!ScanDirectionIsNoMovement(direction))
{ {
long oldPos; long oldPos;
@ -837,7 +837,7 @@ PortalRunSelect(Portal portal,
nprocessed = queryDesc->estate->es_processed; nprocessed = queryDesc->estate->es_processed;
} }
if (direction != NoMovementScanDirection) if (!ScanDirectionIsNoMovement(direction))
{ {
if (nprocessed > 0 && portal->atEnd) if (nprocessed > 0 && portal->atEnd)
{ {
@ -890,13 +890,13 @@ RunFromStore(Portal portal, ScanDirection direction, long count,
(*dest->rStartup) (dest, CMD_SELECT, portal->tupDesc); (*dest->rStartup) (dest, CMD_SELECT, portal->tupDesc);
if (direction == NoMovementScanDirection) if (ScanDirectionIsNoMovement(direction))
{ {
/* do nothing except start/stop the destination */ /* do nothing except start/stop the destination */
} }
else else
{ {
bool forward = (direction == ForwardScanDirection); bool forward = ScanDirectionIsForward(direction);
for (;;) for (;;)
{ {