88 *
99 *
1010 * IDENTIFICATION
11- * $PostgreSQL: pgsql/src/backend/access/heap/heapam.c,v 1.206 2006/01/11 08:43:11 neilc Exp $
11+ * $PostgreSQL: pgsql/src/backend/access/heap/heapam.c,v 1.207 2006/02/21 23:01:53 neilc Exp $
1212 *
1313 *
1414 * INTERFACE ROUTINES
@@ -172,7 +172,8 @@ heapgetpage(HeapScanDesc scan, BlockNumber page)
172172 *tuple as indicated by "dir"; return the next tuple in scan->rs_ctup,
173173 *or set scan->rs_ctup.t_data = NULL if no more tuples.
174174 *
175- * dir == 0 means "re-fetch the tuple indicated by scan->rs_ctup".
175+ * dir == NoMovementScanDirection means "re-fetch the tuple indicated
176+ * by scan->rs_ctup".
176177 *
177178 * Note: the reason nkeys/key are passed separately, even though they are
178179 * kept in the scan descriptor, is that the caller may not want us to check
@@ -189,12 +190,13 @@ heapgetpage(HeapScanDesc scan, BlockNumber page)
189190 */
190191static void
191192heapgettup (HeapScanDesc scan ,
192- int dir ,
193+ ScanDirection dir ,
193194int nkeys ,
194195ScanKey key )
195196{
196197HeapTuple tuple = & (scan -> rs_ctup );
197198Snapshot snapshot = scan -> rs_snapshot ;
199+ bool backward = ScanDirectionIsBackward (dir );
198200BlockNumber page ;
199201Page dp ;
200202int lines ;
@@ -205,11 +207,8 @@ heapgettup(HeapScanDesc scan,
205207/*
206208 * calculate next starting lineoff, given scan direction
207209 */
208- if (dir > 0 )
210+ if (ScanDirectionIsForward ( dir ) )
209211{
210- /*
211- * forward scan direction
212- */
213212if (!scan -> rs_inited )
214213{
215214/*
@@ -242,11 +241,8 @@ heapgettup(HeapScanDesc scan,
242241
243242linesleft = lines - lineoff + 1 ;
244243}
245- else if (dir < 0 )
244+ else if (backward )
246245{
247- /*
248- * reverse scan direction
249- */
250246if (!scan -> rs_inited )
251247{
252248/*
@@ -352,7 +348,7 @@ heapgettup(HeapScanDesc scan,
352348 * otherwise move to the next item on the page
353349 */
354350-- linesleft ;
355- if (dir < 0 )
351+ if (backward )
356352{
357353-- lpp ;/* move back in this page's ItemId array */
358354-- lineoff ;
@@ -373,7 +369,7 @@ heapgettup(HeapScanDesc scan,
373369/*
374370 * return NULL if we've exhausted all the pages
375371 */
376- if (( dir < 0 ) ? (page == 0 ) : (page + 1 >=scan -> rs_nblocks ))
372+ if (backward ? (page == 0 ) : (page + 1 >=scan -> rs_nblocks ))
377373{
378374if (BufferIsValid (scan -> rs_cbuf ))
379375ReleaseBuffer (scan -> rs_cbuf );
@@ -384,7 +380,7 @@ heapgettup(HeapScanDesc scan,
384380return ;
385381}
386382
387- page = ( dir < 0 ) ? (page - 1 ) : (page + 1 );
383+ page = backward ? (page - 1 ) : (page + 1 );
388384
389385heapgetpage (scan ,page );
390386
@@ -393,7 +389,7 @@ heapgettup(HeapScanDesc scan,
393389dp = (Page )BufferGetPage (scan -> rs_cbuf );
394390lines = PageGetMaxOffsetNumber ((Page )dp );
395391linesleft = lines ;
396- if (dir < 0 )
392+ if (backward )
397393{
398394lineoff = lines ;
399395lpp = PageGetItemId (dp ,lines );
@@ -421,11 +417,12 @@ heapgettup(HeapScanDesc scan,
421417 */
422418static void
423419heapgettup_pagemode (HeapScanDesc scan ,
424- int dir ,
420+ ScanDirection dir ,
425421int nkeys ,
426422ScanKey key )
427423{
428424HeapTuple tuple = & (scan -> rs_ctup );
425+ bool backward = ScanDirectionIsBackward (dir );
429426BlockNumber page ;
430427Page dp ;
431428int lines ;
@@ -437,11 +434,8 @@ heapgettup_pagemode(HeapScanDesc scan,
437434/*
438435 * calculate next starting lineindex, given scan direction
439436 */
440- if (dir > 0 )
437+ if (ScanDirectionIsForward ( dir ) )
441438{
442- /*
443- * forward scan direction
444- */
445439if (!scan -> rs_inited )
446440{
447441/*
@@ -471,11 +465,8 @@ heapgettup_pagemode(HeapScanDesc scan,
471465
472466linesleft = lines - lineindex ;
473467}
474- else if (dir < 0 )
468+ else if (backward )
475469{
476- /*
477- * reverse scan direction
478- */
479470if (!scan -> rs_inited )
480471{
481472/*
@@ -584,14 +575,10 @@ heapgettup_pagemode(HeapScanDesc scan,
584575 * otherwise move to the next item on the page
585576 */
586577-- linesleft ;
587- if (dir < 0 )
588- {
578+ if (backward )
589579-- lineindex ;
590- }
591580else
592- {
593581++ lineindex ;
594- }
595582}
596583
597584/*
@@ -602,7 +589,7 @@ heapgettup_pagemode(HeapScanDesc scan,
602589/*
603590 * return NULL if we've exhausted all the pages
604591 */
605- if (( dir < 0 ) ? (page == 0 ) : (page + 1 >=scan -> rs_nblocks ))
592+ if (backward ? (page == 0 ) : (page + 1 >=scan -> rs_nblocks ))
606593{
607594if (BufferIsValid (scan -> rs_cbuf ))
608595ReleaseBuffer (scan -> rs_cbuf );
@@ -613,14 +600,13 @@ heapgettup_pagemode(HeapScanDesc scan,
613600return ;
614601}
615602
616- page = (dir < 0 ) ? (page - 1 ) : (page + 1 );
617-
603+ page = backward ? (page - 1 ) : (page + 1 );
618604heapgetpage (scan ,page );
619605
620606dp = (Page )BufferGetPage (scan -> rs_cbuf );
621607lines = scan -> rs_ntuples ;
622608linesleft = lines ;
623- if (dir < 0 )
609+ if (backward )
624610lineindex = lines - 1 ;
625611else
626612lineindex = 0 ;
@@ -1008,15 +994,11 @@ heap_getnext(HeapScanDesc scan, ScanDirection direction)
1008994
1009995HEAPDEBUG_1 ;/* heap_getnext( info ) */
1010996
1011- /*
1012- * Note: we depend here on the -1/0/1 encoding of ScanDirection.
1013- */
1014997if (scan -> rs_pageatatime )
1015- heapgettup_pagemode (scan ,( int ) direction ,
998+ heapgettup_pagemode (scan ,direction ,
1016999scan -> rs_nkeys ,scan -> rs_key );
10171000else
1018- heapgettup (scan , (int )direction ,
1019- scan -> rs_nkeys ,scan -> rs_key );
1001+ heapgettup (scan ,direction ,scan -> rs_nkeys ,scan -> rs_key );
10201002
10211003if (scan -> rs_ctup .t_data == NULL )
10221004{
@@ -2745,13 +2727,13 @@ heap_restrpos(HeapScanDesc scan)
27452727{
27462728scan -> rs_cindex = scan -> rs_mindex ;
27472729heapgettup_pagemode (scan ,
2748- 0 , /* "no movement" */
2730+ NoMovementScanDirection ,
274927310 ,/* needn't recheck scan keys */
27502732NULL );
27512733}
27522734else
27532735heapgettup (scan ,
2754- 0 , /* "no movement" */
2736+ NoMovementScanDirection ,
275527370 ,/* needn't recheck scan keys */
27562738NULL );
27572739}