8
8
*
9
9
*
10
10
* 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 $
12
12
*
13
13
*
14
14
* INTERFACE ROUTINES
@@ -172,7 +172,8 @@ heapgetpage(HeapScanDesc scan, BlockNumber page)
172
172
* tuple as indicated by "dir"; return the next tuple in scan->rs_ctup,
173
173
* or set scan->rs_ctup.t_data = NULL if no more tuples.
174
174
*
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".
176
177
*
177
178
* Note: the reason nkeys/key are passed separately, even though they are
178
179
* kept in the scan descriptor, is that the caller may not want us to check
@@ -189,12 +190,13 @@ heapgetpage(HeapScanDesc scan, BlockNumber page)
189
190
*/
190
191
static void
191
192
heapgettup (HeapScanDesc scan ,
192
- int dir ,
193
+ ScanDirection dir ,
193
194
int nkeys ,
194
195
ScanKey key )
195
196
{
196
197
HeapTuple tuple = & (scan -> rs_ctup );
197
198
Snapshot snapshot = scan -> rs_snapshot ;
199
+ bool backward = ScanDirectionIsBackward (dir );
198
200
BlockNumber page ;
199
201
Page dp ;
200
202
int lines ;
@@ -205,11 +207,8 @@ heapgettup(HeapScanDesc scan,
205
207
/*
206
208
* calculate next starting lineoff, given scan direction
207
209
*/
208
- if (dir > 0 )
210
+ if (ScanDirectionIsForward ( dir ) )
209
211
{
210
- /*
211
- * forward scan direction
212
- */
213
212
if (!scan -> rs_inited )
214
213
{
215
214
/*
@@ -242,11 +241,8 @@ heapgettup(HeapScanDesc scan,
242
241
243
242
linesleft = lines - lineoff + 1 ;
244
243
}
245
- else if (dir < 0 )
244
+ else if (backward )
246
245
{
247
- /*
248
- * reverse scan direction
249
- */
250
246
if (!scan -> rs_inited )
251
247
{
252
248
/*
@@ -352,7 +348,7 @@ heapgettup(HeapScanDesc scan,
352
348
* otherwise move to the next item on the page
353
349
*/
354
350
-- linesleft ;
355
- if (dir < 0 )
351
+ if (backward )
356
352
{
357
353
-- lpp ; /* move back in this page's ItemId array */
358
354
-- lineoff ;
@@ -373,7 +369,7 @@ heapgettup(HeapScanDesc scan,
373
369
/*
374
370
* return NULL if we've exhausted all the pages
375
371
*/
376
- if (( dir < 0 ) ? (page == 0 ) : (page + 1 >= scan -> rs_nblocks ))
372
+ if (backward ? (page == 0 ) : (page + 1 >= scan -> rs_nblocks ))
377
373
{
378
374
if (BufferIsValid (scan -> rs_cbuf ))
379
375
ReleaseBuffer (scan -> rs_cbuf );
@@ -384,7 +380,7 @@ heapgettup(HeapScanDesc scan,
384
380
return ;
385
381
}
386
382
387
- page = ( dir < 0 ) ? (page - 1 ) : (page + 1 );
383
+ page = backward ? (page - 1 ) : (page + 1 );
388
384
389
385
heapgetpage (scan , page );
390
386
@@ -393,7 +389,7 @@ heapgettup(HeapScanDesc scan,
393
389
dp = (Page ) BufferGetPage (scan -> rs_cbuf );
394
390
lines = PageGetMaxOffsetNumber ((Page ) dp );
395
391
linesleft = lines ;
396
- if (dir < 0 )
392
+ if (backward )
397
393
{
398
394
lineoff = lines ;
399
395
lpp = PageGetItemId (dp , lines );
@@ -421,11 +417,12 @@ heapgettup(HeapScanDesc scan,
421
417
*/
422
418
static void
423
419
heapgettup_pagemode (HeapScanDesc scan ,
424
- int dir ,
420
+ ScanDirection dir ,
425
421
int nkeys ,
426
422
ScanKey key )
427
423
{
428
424
HeapTuple tuple = & (scan -> rs_ctup );
425
+ bool backward = ScanDirectionIsBackward (dir );
429
426
BlockNumber page ;
430
427
Page dp ;
431
428
int lines ;
@@ -437,11 +434,8 @@ heapgettup_pagemode(HeapScanDesc scan,
437
434
/*
438
435
* calculate next starting lineindex, given scan direction
439
436
*/
440
- if (dir > 0 )
437
+ if (ScanDirectionIsForward ( dir ) )
441
438
{
442
- /*
443
- * forward scan direction
444
- */
445
439
if (!scan -> rs_inited )
446
440
{
447
441
/*
@@ -471,11 +465,8 @@ heapgettup_pagemode(HeapScanDesc scan,
471
465
472
466
linesleft = lines - lineindex ;
473
467
}
474
- else if (dir < 0 )
468
+ else if (backward )
475
469
{
476
- /*
477
- * reverse scan direction
478
- */
479
470
if (!scan -> rs_inited )
480
471
{
481
472
/*
@@ -584,14 +575,10 @@ heapgettup_pagemode(HeapScanDesc scan,
584
575
* otherwise move to the next item on the page
585
576
*/
586
577
-- linesleft ;
587
- if (dir < 0 )
588
- {
578
+ if (backward )
589
579
-- lineindex ;
590
- }
591
580
else
592
- {
593
581
++ lineindex ;
594
- }
595
582
}
596
583
597
584
/*
@@ -602,7 +589,7 @@ heapgettup_pagemode(HeapScanDesc scan,
602
589
/*
603
590
* return NULL if we've exhausted all the pages
604
591
*/
605
- if (( dir < 0 ) ? (page == 0 ) : (page + 1 >= scan -> rs_nblocks ))
592
+ if (backward ? (page == 0 ) : (page + 1 >= scan -> rs_nblocks ))
606
593
{
607
594
if (BufferIsValid (scan -> rs_cbuf ))
608
595
ReleaseBuffer (scan -> rs_cbuf );
@@ -613,14 +600,13 @@ heapgettup_pagemode(HeapScanDesc scan,
613
600
return ;
614
601
}
615
602
616
- page = (dir < 0 ) ? (page - 1 ) : (page + 1 );
617
-
603
+ page = backward ? (page - 1 ) : (page + 1 );
618
604
heapgetpage (scan , page );
619
605
620
606
dp = (Page ) BufferGetPage (scan -> rs_cbuf );
621
607
lines = scan -> rs_ntuples ;
622
608
linesleft = lines ;
623
- if (dir < 0 )
609
+ if (backward )
624
610
lineindex = lines - 1 ;
625
611
else
626
612
lineindex = 0 ;
@@ -1008,15 +994,11 @@ heap_getnext(HeapScanDesc scan, ScanDirection direction)
1008
994
1009
995
HEAPDEBUG_1 ; /* heap_getnext( info ) */
1010
996
1011
- /*
1012
- * Note: we depend here on the -1/0/1 encoding of ScanDirection.
1013
- */
1014
997
if (scan -> rs_pageatatime )
1015
- heapgettup_pagemode (scan , ( int ) direction ,
998
+ heapgettup_pagemode (scan , direction ,
1016
999
scan -> rs_nkeys , scan -> rs_key );
1017
1000
else
1018
- heapgettup (scan , (int ) direction ,
1019
- scan -> rs_nkeys , scan -> rs_key );
1001
+ heapgettup (scan , direction , scan -> rs_nkeys , scan -> rs_key );
1020
1002
1021
1003
if (scan -> rs_ctup .t_data == NULL )
1022
1004
{
@@ -2745,13 +2727,13 @@ heap_restrpos(HeapScanDesc scan)
2745
2727
{
2746
2728
scan -> rs_cindex = scan -> rs_mindex ;
2747
2729
heapgettup_pagemode (scan ,
2748
- 0 , /* "no movement" */
2730
+ NoMovementScanDirection ,
2749
2731
0 , /* needn't recheck scan keys */
2750
2732
NULL );
2751
2733
}
2752
2734
else
2753
2735
heapgettup (scan ,
2754
- 0 , /* "no movement" */
2736
+ NoMovementScanDirection ,
2755
2737
0 , /* needn't recheck scan keys */
2756
2738
NULL );
2757
2739
}
0 commit comments