@@ -120,8 +120,8 @@ callConsistentFn(RumState * rumstate, RumScanKey key)
120
120
*/
121
121
static bool
122
122
findItemInPostingPage (Page page , RumKey * item , OffsetNumber * off ,
123
- OffsetNumber attno ,
124
- OffsetNumber attnum , RumState * rumstate )
123
+ OffsetNumber attno , OffsetNumber attnum ,
124
+ ScanDirection scanDirection , RumState * rumstate )
125
125
{
126
126
OffsetNumber maxoff = RumPageGetOpaque (page )-> maxoff ;
127
127
int res ;
@@ -144,8 +144,17 @@ findItemInPostingPage(Page page, RumKey *item, OffsetNumber *off,
144
144
ptr = rumDataPageLeafRead (ptr , attnum , & iter_item , rumstate );
145
145
res = compareRumKey (rumstate , attno , item , & iter_item );
146
146
147
- if (res <= 0 )
147
+ if (res == 0 )
148
+ {
149
+ return true;
150
+ }
151
+ else if (res < 0 )
152
+ {
153
+ if (ScanDirectionIsBackward (scanDirection ) &&
154
+ * off > FirstOffsetNumber )
155
+ (* off )-- ;
148
156
return true;
157
+ }
149
158
}
150
159
151
160
return false;
@@ -167,7 +176,8 @@ moveRightIfItNeeded(RumBtreeData * btree, RumBtreeStack * stack)
167
176
if (RumPageRightMost (page ))
168
177
return false; /* no more pages */
169
178
170
- stack -> buffer = rumStepRight (stack -> buffer , btree -> index , RUM_SHARE );
179
+ stack -> buffer = rumStep (stack -> buffer , btree -> index , RUM_SHARE ,
180
+ ForwardScanDirection );
171
181
stack -> blkno = BufferGetBlockNumber (stack -> buffer );
172
182
stack -> off = FirstOffsetNumber ;
173
183
}
@@ -187,8 +197,10 @@ scanPostingTree(Relation index, RumScanEntry scanEntry,
187
197
Buffer buffer ;
188
198
Page page ;
189
199
200
+ Assert (ScanDirectionIsForward (scanEntry -> scanDirection ));
190
201
/* Descend to the leftmost leaf page */
191
- gdi = rumPrepareScanPostingTree (index , rootPostingTree , TRUE, attnum , rumstate );
202
+ gdi = rumPrepareScanPostingTree (index , rootPostingTree , TRUE,
203
+ ForwardScanDirection , attnum , rumstate );
192
204
193
205
buffer = rumScanBeginPostingTree (gdi , NULL );
194
206
IncrBufferRefCount (buffer ); /* prevent unpin in freeRumBtreeStack */
@@ -228,7 +240,7 @@ scanPostingTree(Relation index, RumScanEntry scanEntry,
228
240
if (RumPageRightMost (page ))
229
241
break ; /* no more pages */
230
242
231
- buffer = rumStepRight (buffer , index , RUM_SHARE );
243
+ buffer = rumStep (buffer , index , RUM_SHARE , ForwardScanDirection );
232
244
}
233
245
234
246
UnlockReleaseBuffer (buffer );
@@ -564,14 +576,15 @@ startScanEntry(RumState * rumstate, RumScanEntry entry)
564
576
*/
565
577
LockBuffer (stackEntry -> buffer , RUM_UNLOCK );
566
578
needUnlock = FALSE;
567
- gdi = rumPrepareScanPostingTree (rumstate -> index , rootPostingTree , TRUE, entry -> attnum , rumstate );
579
+ gdi = rumPrepareScanPostingTree (rumstate -> index , rootPostingTree , TRUE,
580
+ entry -> scanDirection , entry -> attnum , rumstate );
568
581
569
582
entry -> buffer = rumScanBeginPostingTree (gdi , entry -> useMarkAddInfo ?
570
583
& entry -> markAddInfo : NULL );
571
584
572
585
entry -> gdi = gdi ;
573
586
entry -> context = AllocSetContextCreate (CurrentMemoryContext ,
574
- "GiST temporary context" ,
587
+ "RUM entry temporary context" ,
575
588
ALLOCSET_DEFAULT_MINSIZE ,
576
589
ALLOCSET_DEFAULT_INITSIZE ,
577
590
ALLOCSET_DEFAULT_MAXSIZE );
@@ -801,7 +814,11 @@ entryGetNextItem(RumState * rumstate, RumScanEntry entry)
801
814
* It's needed to go by right link. During that we should refind
802
815
* first ItemPointer greater that stored
803
816
*/
804
- if (RumPageRightMost (page ))
817
+ if ((ScanDirectionIsForward (entry -> scanDirection ) && RumPageRightMost (page ))
818
+ ||
819
+ (ScanDirectionIsBackward (entry -> scanDirection ) && RumPageLeftMost (page )))
820
+
821
+
805
822
{
806
823
UnlockReleaseBuffer (entry -> buffer );
807
824
ItemPointerSetInvalid (& entry -> curRumKey .iptr );
@@ -812,17 +829,17 @@ entryGetNextItem(RumState * rumstate, RumScanEntry entry)
812
829
return ;
813
830
}
814
831
815
- entry -> buffer = rumStepRight (entry -> buffer ,
816
- rumstate -> index ,
817
- RUM_SHARE );
832
+ entry -> buffer = rumStep (entry -> buffer , rumstate -> index ,
833
+ RUM_SHARE , entry -> scanDirection );
818
834
entry -> gdi -> stack -> buffer = entry -> buffer ;
819
835
entry -> gdi -> stack -> blkno = BufferGetBlockNumber (entry -> buffer );
820
836
page = BufferGetPage (entry -> buffer );
821
837
822
838
entry -> offset = InvalidOffsetNumber ;
823
839
if (!ItemPointerIsValid (& entry -> curRumKey .iptr ) ||
824
840
findItemInPostingPage (page , & entry -> curRumKey , & entry -> offset ,
825
- entry -> attnumOrig , entry -> attnum , rumstate ))
841
+ entry -> attnumOrig , entry -> attnum ,
842
+ entry -> scanDirection , rumstate ))
826
843
{
827
844
OffsetNumber maxoff ,
828
845
i ;
@@ -960,12 +977,13 @@ entryGetNextItemList(RumState * rumstate, RumScanEntry entry)
960
977
LockBuffer (entry -> stack -> buffer , RUM_UNLOCK );
961
978
needUnlock = false;
962
979
gdi = rumPrepareScanPostingTree (rumstate -> index ,
963
- rootPostingTree , TRUE, entry -> attnumOrig , rumstate );
980
+ rootPostingTree , TRUE, entry -> scanDirection ,
981
+ entry -> attnumOrig , rumstate );
964
982
965
983
entry -> buffer = rumScanBeginPostingTree (gdi , NULL );
966
984
entry -> gdi = gdi ;
967
985
entry -> context = AllocSetContextCreate (CurrentMemoryContext ,
968
- "GiST temporary context" ,
986
+ "RUM entry temporary context" ,
969
987
ALLOCSET_DEFAULT_MINSIZE ,
970
988
ALLOCSET_DEFAULT_INITSIZE ,
971
989
ALLOCSET_DEFAULT_MAXSIZE );
@@ -1652,34 +1670,21 @@ entryFindItem(RumState * rumstate, RumScanEntry entry, RumKey * item)
1652
1670
return ;
1653
1671
}
1654
1672
1655
- /* At last try to traverse by right links */
1673
+ /* At last try to traverse by direction */
1656
1674
for (;;)
1657
1675
{
1658
- /*
1659
- * It's needed to go by right link. During that we should refind first
1660
- * ItemPointer greater that stored
1661
- */
1662
- BlockNumber blkno ;
1663
-
1664
- blkno = RumPageGetOpaque (page )-> rightlink ;
1676
+ entry -> buffer = rumStep (entry -> buffer , rumstate -> index ,
1677
+ RUM_SHARE , entry -> scanDirection );
1678
+ entry -> gdi -> stack -> buffer = entry -> buffer ;
1665
1679
1666
- LockBuffer (entry -> buffer , RUM_UNLOCK );
1667
- if (blkno == InvalidBlockNumber )
1680
+ if (entry -> buffer == InvalidBuffer )
1668
1681
{
1669
- ReleaseBuffer (entry -> buffer );
1670
1682
ItemPointerSetInvalid (& entry -> curRumKey .iptr );
1671
- entry -> buffer = InvalidBuffer ;
1672
- entry -> gdi -> stack -> buffer = InvalidBuffer ;
1673
1683
entry -> isFinished = TRUE;
1674
1684
return ;
1675
1685
}
1676
1686
1677
- entry -> buffer = ReleaseAndReadBuffer (entry -> buffer ,
1678
- rumstate -> index ,
1679
- blkno );
1680
- entry -> gdi -> stack -> buffer = entry -> buffer ;
1681
- entry -> gdi -> stack -> blkno = blkno ;
1682
- LockBuffer (entry -> buffer , RUM_SHARE );
1687
+ entry -> gdi -> stack -> blkno = BufferGetBlockNumber (entry -> buffer );
1683
1688
page = BufferGetPage (entry -> buffer );
1684
1689
1685
1690
if (scanPage (rumstate , entry , item ,
0 commit comments