Skip to content

Commit d978c8b

Browse files
committed
move forward to order by addinfo in posting tree
1 parent f6ae370 commit d978c8b

File tree

4 files changed

+89
-49
lines changed

4 files changed

+89
-49
lines changed

rum.h

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -303,11 +303,7 @@ typedef struct RumOptions
303303
#define RUM_SHARE BUFFER_LOCK_SHARE
304304
#define RUM_EXCLUSIVE BUFFER_LOCK_EXCLUSIVE
305305

306-
typedef struct RumKey {
307-
ItemPointerData ipd;
308-
bool isNull;
309-
Datum addToCompare;
310-
} RumKey;
306+
typedef struct RumEntryAccumulatorItem RumKey;
311307

312308
/*
313309
* RumState: working data structure describing the index being worked on
@@ -492,18 +488,19 @@ extern ItemPointerData updateItemIndexes(Page page, OffsetNumber attnum, RumStat
492488
extern void checkLeafDataPage(RumState *rumstate, AttrNumber attrnum, Page page);
493489

494490
/* rumdatapage.c */
495-
extern int rumCompareItemPointers(ItemPointer a, ItemPointer b);
496-
extern int compareRumKey(RumState *state, RumKey *a, RumKey *b);
491+
extern int rumCompareItemPointers(const ItemPointerData * a, const ItemPointerData * b);
492+
extern int compareRumKey(RumState *state, const RumKey *a, const RumKey *b);
497493
extern char *rumDataPageLeafWriteItemPointer(char *ptr, ItemPointer iptr, ItemPointer prev, bool addInfoIsNull);
498494
extern Pointer rumPlaceToDataPageLeaf(Pointer ptr, OffsetNumber attnum,
499495
ItemPointer iptr, Datum addInfo, bool addInfoIsNull, ItemPointer prev,
500496
RumState *rumstate);
501497
extern Size rumCheckPlaceToDataPageLeaf(OffsetNumber attnum,
502498
ItemPointer iptr, Datum addInfo, bool addInfoIsNull, ItemPointer prev,
503499
RumState *rumstate, Size size);
504-
extern uint32 rumMergeItemPointers(ItemPointerData *dst, Datum *dst2, bool *dst3,
505-
ItemPointerData *a, Datum *a2, bool *a3, uint32 na,
506-
ItemPointerData *b, Datum * b2, bool *b3, uint32 nb);
500+
extern uint32 rumMergeItemPointers(RumState *rumstate,
501+
ItemPointerData *dst, Datum *dst2, bool *dst3,
502+
ItemPointerData *a, Datum *a2, bool *a3, uint32 na,
503+
ItemPointerData *b, Datum * b2, bool *b3, uint32 nb);
507504
extern void RumDataPageAddItem(Page page, void *data, OffsetNumber offset);
508505
extern void RumPageDeletePostingItem(Page page, OffsetNumber offset);
509506

@@ -683,7 +680,7 @@ extern IndexBulkDeleteResult *rumbulkdelete(IndexVacuumInfo *info,
683680
extern IndexBulkDeleteResult *rumvacuumcleanup(IndexVacuumInfo *info,
684681
IndexBulkDeleteResult *stats);
685682

686-
typedef struct
683+
typedef struct RumEntryAccumulatorItem
687684
{
688685
ItemPointerData iptr;
689686
bool addInfoIsNull;
@@ -713,6 +710,8 @@ typedef struct
713710
RumEntryAccumulator *entryallocator;
714711
uint32 eas_used;
715712
RBTree *tree;
713+
RumKey *sortSpace;
714+
uint32 sortSpaceN;
716715
} BuildAccumulator;
717716

718717
extern void rumInitBA(BuildAccumulator *accum);

rumbulk.c

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,17 @@ rumCombineData(RBNode *existing, const RBNode *newdata, void *arg)
4242
accum->allocatedMemory += GetMemoryChunkSpace(eo->list);
4343
}
4444

45-
/* If item pointers are not ordered, they will need to be sorted later */
45+
/*
46+
* If item pointers are not ordered, they will need to be sorted later
47+
* Note: if useAlternativeOrder == true then shouldSort should be true
48+
* because anyway list isn't right ordered and code below could not check it
49+
* correctly
50+
*/
4651
if (eo->shouldSort == FALSE)
4752
{
4853
int res;
4954

55+
/* FIXME RumKey */
5056
res = rumCompareItemPointers(&eo->list[eo->count - 1].iptr,
5157
&en->list->iptr);
5258
Assert(res != 0);
@@ -172,7 +178,13 @@ rumInsertBAEntry(BuildAccumulator *accum,
172178
ea->key = getDatumCopy(accum, attnum, key);
173179
ea->maxcount = DEF_NPTR;
174180
ea->count = 1;
175-
ea->shouldSort = FALSE;
181+
182+
/*
183+
* if useAlternativeOrder = true then anyway we need to sort list,
184+
* but by setting shouldSort we prevent incorrect comparison in
185+
* rumCombineData()
186+
*/
187+
ea->shouldSort = accum->rumstate->useAlternativeOrder;
176188
ea->list =
177189
(RumEntryAccumulatorItem *) palloc(sizeof(RumEntryAccumulatorItem) * DEF_NPTR);
178190
ea->list[0].iptr = *heapptr;
@@ -250,6 +262,12 @@ qsortCompareItemPointers(const void *a, const void *b)
250262
return res;
251263
}
252264

265+
static int
266+
qsortCompareRumKey(const void *a, const void *b, void *arg)
267+
{
268+
return compareRumKey(arg, a, b);
269+
}
270+
253271
/* Prepare to read out the rbtree contents using rumGetBAEntry */
254272
void
255273
rumBeginBAScan(BuildAccumulator *accum)
@@ -283,9 +301,15 @@ rumGetBAEntry(BuildAccumulator *accum,
283301

284302
Assert(list != NULL && entry->count > 0);
285303

286-
if (entry->shouldSort && entry->count > 1)
287-
qsort(list, entry->count, sizeof(RumEntryAccumulatorItem),
288-
qsortCompareItemPointers);
304+
if (entry->count > 1)
305+
{
306+
if (accum->rumstate->useAlternativeOrder)
307+
qsort_arg(list, entry->count, sizeof(RumEntryAccumulatorItem),
308+
qsortCompareRumKey, accum->rumstate);
309+
else if (entry->shouldSort)
310+
qsort(list, entry->count, sizeof(RumEntryAccumulatorItem),
311+
qsortCompareItemPointers);
312+
}
289313

290314
return list;
291315
}

rumdatapage.c

Lines changed: 46 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ rumCheckPlaceToDataPageLeaf(OffsetNumber attnum,
281281
}
282282

283283
int
284-
rumCompareItemPointers(ItemPointer a, ItemPointer b)
284+
rumCompareItemPointers(const ItemPointerData *a, const ItemPointerData *b)
285285
{
286286
BlockNumber ba = RumItemPointerGetBlockNumber(a);
287287
BlockNumber bb = RumItemPointerGetBlockNumber(b);
@@ -300,39 +300,36 @@ rumCompareItemPointers(ItemPointer a, ItemPointer b)
300300
}
301301

302302
int
303-
compareRumKey(RumState *state, RumKey *a, RumKey *b)
303+
compareRumKey(RumState *state, const RumKey *a, const RumKey *b)
304304
{
305305

306306
/* assume NULL is greate than any real value */
307-
if (state->useAlternativeOrder)
307+
if (a->addInfoIsNull == false && b->addInfoIsNull == false)
308308
{
309-
if (a->isNull == false && b->isNull == false)
310-
{
311-
int res;
312-
AttrNumber attnum = state->attrnOrderByColumn;
313-
314-
res = DatumGetInt32(FunctionCall2Coll(
315-
&state->compareFn[attnum - 1],
316-
state->supportCollation[attnum - 1],
317-
a->addToCompare, b->addToCompare));
318-
if (res != 0)
319-
return res;
320-
/* fallback to ItemPointerCompare */
321-
}
322-
else if (a->isNull == true)
323-
{
324-
if (b->isNull == false)
325-
return 1;
326-
/* fallback to ItemPointerCompare */
327-
}
328-
else
329-
{
330-
Assert(b->isNull == true);
331-
return -1;
332-
}
309+
int res;
310+
AttrNumber attnum = state->attrnOrderByColumn;
311+
312+
res = DatumGetInt32(FunctionCall2Coll(
313+
&state->compareFn[attnum - 1],
314+
state->supportCollation[attnum - 1],
315+
a->addInfo, b->addInfo));
316+
if (res != 0)
317+
return res;
318+
/* fallback to ItemPointerCompare */
319+
}
320+
else if (a->addInfoIsNull == true)
321+
{
322+
if (b->addInfoIsNull == false)
323+
return 1;
324+
/* fallback to ItemPointerCompare */
325+
}
326+
else
327+
{
328+
Assert(b->addInfoIsNull == true);
329+
return -1;
333330
}
334331

335-
return rumCompareItemPointers(&a->ipd, &b->ipd);
332+
return rumCompareItemPointers(&a->iptr, &b->iptr);
336333
}
337334

338335
/*
@@ -341,7 +338,8 @@ compareRumKey(RumState *state, RumKey *a, RumKey *b)
341338
* Caller is responsible that there is enough space at *dst.
342339
*/
343340
uint32
344-
rumMergeItemPointers(ItemPointerData *dst, Datum *dstAddInfo, bool *dstAddInfoIsNull,
341+
rumMergeItemPointers(RumState *rumstate,
342+
ItemPointerData *dst, Datum *dstAddInfo, bool *dstAddInfoIsNull,
345343
ItemPointerData *a, Datum *aAddInfo, bool *aAddInfoIsNull, uint32 na,
346344
ItemPointerData *b, Datum *bAddInfo, bool *bAddInfoIsNull, uint32 nb)
347345
{
@@ -351,7 +349,25 @@ rumMergeItemPointers(ItemPointerData *dst, Datum *dstAddInfo, bool *dstAddInfoIs
351349

352350
while (aptr - a < na && bptr - b < nb)
353351
{
354-
int cmp = rumCompareItemPointers(aptr, bptr);
352+
int cmp;
353+
354+
if (rumstate->useAlternativeOrder)
355+
{
356+
RumKey a, b;
357+
358+
a.iptr = *aptr;
359+
a.addInfoIsNull = *aAddInfoIsNull;
360+
a.addInfo = *aAddInfo;
361+
b.iptr = *bptr;
362+
b.addInfoIsNull = *bAddInfoIsNull;
363+
b.addInfo = *bAddInfo;
364+
365+
cmp = compareRumKey(rumstate, &a, &b);
366+
}
367+
else
368+
{
369+
cmp = rumCompareItemPointers(aptr, bptr);
370+
}
355371

356372
if (cmp > 0)
357373
{

ruminsert.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -259,9 +259,10 @@ addItemPointersToLeafTuple(RumState *rumstate,
259259

260260
rumReadTuple(rumstate, attnum, old, oldItems, oldAddInfo, oldAddInfoIsNull);
261261

262-
newNPosting = rumMergeItemPointers(newItems, newAddInfo, newAddInfoIsNull,
263-
items, addInfo, addInfoIsNull, nitem,
264-
oldItems, oldAddInfo, oldAddInfoIsNull, oldNPosting);
262+
newNPosting = rumMergeItemPointers(rumstate,
263+
newItems, newAddInfo, newAddInfoIsNull,
264+
items, addInfo, addInfoIsNull, nitem,
265+
oldItems, oldAddInfo, oldAddInfoIsNull, oldNPosting);
265266

266267

267268
/* try to build tuple with room for all the items */

0 commit comments

Comments
 (0)