Commit c2b175b9 authored by Heikki Linnakangas's avatar Heikki Linnakangas

Minor GIN code refactoring.

It makes for cleaner code to have separate Get/Add functions for PostingItems
and ItemPointers.  A few callsites that have to deal with both types need to
be duplicated because of this, but all the callers have to know which one
they're dealing with anyway. Overall, this reduces the amount of casting
required.

Extracted from Alexander Korotkov's larger patch to change the data page
format.
parent 627882d0
...@@ -105,7 +105,7 @@ dataLocateItem(GinBtree btree, GinBtreeStack *stack) ...@@ -105,7 +105,7 @@ dataLocateItem(GinBtree btree, GinBtreeStack *stack)
{ {
OffsetNumber mid = low + ((high - low) / 2); OffsetNumber mid = low + ((high - low) / 2);
pitem = (PostingItem *) GinDataPageGetItem(page, mid); pitem = GinDataPageGetPostingItem(page, mid);
if (mid == maxoff) if (mid == maxoff)
{ {
...@@ -117,7 +117,7 @@ dataLocateItem(GinBtree btree, GinBtreeStack *stack) ...@@ -117,7 +117,7 @@ dataLocateItem(GinBtree btree, GinBtreeStack *stack)
} }
else else
{ {
pitem = (PostingItem *) GinDataPageGetItem(page, mid); pitem = GinDataPageGetPostingItem(page, mid);
result = ginCompareItemPointers(btree->items + btree->curitem, &(pitem->key)); result = ginCompareItemPointers(btree->items + btree->curitem, &(pitem->key));
} }
...@@ -135,7 +135,7 @@ dataLocateItem(GinBtree btree, GinBtreeStack *stack) ...@@ -135,7 +135,7 @@ dataLocateItem(GinBtree btree, GinBtreeStack *stack)
Assert(high >= FirstOffsetNumber && high <= maxoff); Assert(high >= FirstOffsetNumber && high <= maxoff);
stack->off = high; stack->off = high;
pitem = (PostingItem *) GinDataPageGetItem(page, high); pitem = GinDataPageGetPostingItem(page, high);
return PostingItemGetBlockNumber(pitem); return PostingItemGetBlockNumber(pitem);
} }
...@@ -176,7 +176,8 @@ dataLocateLeafItem(GinBtree btree, GinBtreeStack *stack) ...@@ -176,7 +176,8 @@ dataLocateLeafItem(GinBtree btree, GinBtreeStack *stack)
{ {
OffsetNumber mid = low + ((high - low) / 2); OffsetNumber mid = low + ((high - low) / 2);
result = ginCompareItemPointers(btree->items + btree->curitem, (ItemPointer) GinDataPageGetItem(page, mid)); result = ginCompareItemPointers(btree->items + btree->curitem,
GinDataPageGetItemPointer(page, mid));
if (result == 0) if (result == 0)
{ {
...@@ -210,7 +211,7 @@ dataFindChildPtr(GinBtree btree, Page page, BlockNumber blkno, OffsetNumber stor ...@@ -210,7 +211,7 @@ dataFindChildPtr(GinBtree btree, Page page, BlockNumber blkno, OffsetNumber stor
/* if page isn't changed, we return storedOff */ /* if page isn't changed, we return storedOff */
if (storedOff >= FirstOffsetNumber && storedOff <= maxoff) if (storedOff >= FirstOffsetNumber && storedOff <= maxoff)
{ {
pitem = (PostingItem *) GinDataPageGetItem(page, storedOff); pitem = GinDataPageGetPostingItem(page, storedOff);
if (PostingItemGetBlockNumber(pitem) == blkno) if (PostingItemGetBlockNumber(pitem) == blkno)
return storedOff; return storedOff;
...@@ -220,7 +221,7 @@ dataFindChildPtr(GinBtree btree, Page page, BlockNumber blkno, OffsetNumber stor ...@@ -220,7 +221,7 @@ dataFindChildPtr(GinBtree btree, Page page, BlockNumber blkno, OffsetNumber stor
*/ */
for (i = storedOff + 1; i <= maxoff; i++) for (i = storedOff + 1; i <= maxoff; i++)
{ {
pitem = (PostingItem *) GinDataPageGetItem(page, i); pitem = GinDataPageGetPostingItem(page, i);
if (PostingItemGetBlockNumber(pitem) == blkno) if (PostingItemGetBlockNumber(pitem) == blkno)
return i; return i;
} }
...@@ -231,7 +232,7 @@ dataFindChildPtr(GinBtree btree, Page page, BlockNumber blkno, OffsetNumber stor ...@@ -231,7 +232,7 @@ dataFindChildPtr(GinBtree btree, Page page, BlockNumber blkno, OffsetNumber stor
/* last chance */ /* last chance */
for (i = FirstOffsetNumber; i <= maxoff; i++) for (i = FirstOffsetNumber; i <= maxoff; i++)
{ {
pitem = (PostingItem *) GinDataPageGetItem(page, i); pitem = GinDataPageGetPostingItem(page, i);
if (PostingItemGetBlockNumber(pitem) == blkno) if (PostingItemGetBlockNumber(pitem) == blkno)
return i; return i;
} }
...@@ -251,33 +252,62 @@ dataGetLeftMostPage(GinBtree btree, Page page) ...@@ -251,33 +252,62 @@ dataGetLeftMostPage(GinBtree btree, Page page)
Assert(GinPageIsData(page)); Assert(GinPageIsData(page));
Assert(GinPageGetOpaque(page)->maxoff >= FirstOffsetNumber); Assert(GinPageGetOpaque(page)->maxoff >= FirstOffsetNumber);
pitem = (PostingItem *) GinDataPageGetItem(page, FirstOffsetNumber); pitem = GinDataPageGetPostingItem(page, FirstOffsetNumber);
return PostingItemGetBlockNumber(pitem); return PostingItemGetBlockNumber(pitem);
} }
/* /*
* add ItemPointer or PostingItem to page. data should point to * add ItemPointer to a leaf page.
* correct value! depending on leaf or non-leaf page
*/ */
void void
GinDataPageAddItem(Page page, void *data, OffsetNumber offset) GinDataPageAddItemPointer(Page page, ItemPointer data, OffsetNumber offset)
{ {
OffsetNumber maxoff = GinPageGetOpaque(page)->maxoff; OffsetNumber maxoff = GinPageGetOpaque(page)->maxoff;
char *ptr; char *ptr;
Assert(GinPageIsLeaf(page));
if (offset == InvalidOffsetNumber)
{
ptr = (char *) GinDataPageGetItemPointer(page, maxoff + 1);
}
else
{
ptr = (char *) GinDataPageGetItemPointer(page, offset);
if (maxoff + 1 - offset != 0)
memmove(ptr + sizeof(ItemPointerData),
ptr,
(maxoff - offset + 1) * sizeof(ItemPointerData));
}
memcpy(ptr, data, sizeof(ItemPointerData));
GinPageGetOpaque(page)->maxoff++;
}
/*
* add PostingItem to a non-leaf page.
*/
void
GinDataPageAddPostingItem(Page page, PostingItem *data, OffsetNumber offset)
{
OffsetNumber maxoff = GinPageGetOpaque(page)->maxoff;
char *ptr;
Assert(!GinPageIsLeaf(page));
if (offset == InvalidOffsetNumber) if (offset == InvalidOffsetNumber)
{ {
ptr = GinDataPageGetItem(page, maxoff + 1); ptr = (char *) GinDataPageGetPostingItem(page, maxoff + 1);
} }
else else
{ {
ptr = GinDataPageGetItem(page, offset); ptr = (char *) GinDataPageGetPostingItem(page, offset);
if (maxoff + 1 - offset != 0) if (maxoff + 1 - offset != 0)
memmove(ptr + GinSizeOfDataPageItem(page), memmove(ptr + sizeof(PostingItem),
ptr, ptr,
(maxoff - offset + 1) * GinSizeOfDataPageItem(page)); (maxoff - offset + 1) * sizeof(PostingItem));
} }
memcpy(ptr, data, GinSizeOfDataPageItem(page)); memcpy(ptr, data, sizeof(PostingItem));
GinPageGetOpaque(page)->maxoff++; GinPageGetOpaque(page)->maxoff++;
} }
...@@ -294,7 +324,8 @@ GinPageDeletePostingItem(Page page, OffsetNumber offset) ...@@ -294,7 +324,8 @@ GinPageDeletePostingItem(Page page, OffsetNumber offset)
Assert(offset >= FirstOffsetNumber && offset <= maxoff); Assert(offset >= FirstOffsetNumber && offset <= maxoff);
if (offset != maxoff) if (offset != maxoff)
memmove(GinDataPageGetItem(page, offset), GinDataPageGetItem(page, offset + 1), memmove(GinDataPageGetPostingItem(page, offset),
GinDataPageGetPostingItem(page, offset + 1),
sizeof(PostingItem) * (maxoff - offset)); sizeof(PostingItem) * (maxoff - offset));
GinPageGetOpaque(page)->maxoff--; GinPageGetOpaque(page)->maxoff--;
...@@ -342,7 +373,7 @@ dataPrepareData(GinBtree btree, Page page, OffsetNumber off) ...@@ -342,7 +373,7 @@ dataPrepareData(GinBtree btree, Page page, OffsetNumber off)
if (!GinPageIsLeaf(page) && btree->rightblkno != InvalidBlockNumber) if (!GinPageIsLeaf(page) && btree->rightblkno != InvalidBlockNumber)
{ {
PostingItem *pitem = (PostingItem *) GinDataPageGetItem(page, off); PostingItem *pitem = GinDataPageGetPostingItem(page, off);
PostingItemSetBlockNumber(pitem, btree->rightblkno); PostingItemSetBlockNumber(pitem, btree->rightblkno);
ret = btree->rightblkno; ret = btree->rightblkno;
...@@ -418,7 +449,7 @@ dataPlaceToPage(GinBtree btree, Buffer buf, OffsetNumber off, XLogRecData **prda ...@@ -418,7 +449,7 @@ dataPlaceToPage(GinBtree btree, Buffer buf, OffsetNumber off, XLogRecData **prda
while (btree->curitem < btree->nitem) while (btree->curitem < btree->nitem)
{ {
GinDataPageAddItem(page, btree->items + btree->curitem, off); GinDataPageAddItemPointer(page, btree->items + btree->curitem, off);
off++; off++;
btree->curitem++; btree->curitem++;
} }
...@@ -427,12 +458,12 @@ dataPlaceToPage(GinBtree btree, Buffer buf, OffsetNumber off, XLogRecData **prda ...@@ -427,12 +458,12 @@ dataPlaceToPage(GinBtree btree, Buffer buf, OffsetNumber off, XLogRecData **prda
} }
else else
{ {
GinDataPageAddItem(page, btree->items + btree->curitem, off); GinDataPageAddItemPointer(page, btree->items + btree->curitem, off);
btree->curitem++; btree->curitem++;
} }
} }
else else
GinDataPageAddItem(page, &(btree->pitem), off); GinDataPageAddPostingItem(page, &(btree->pitem), off);
} }
/* /*
...@@ -448,6 +479,7 @@ dataSplitPage(GinBtree btree, Buffer lbuf, Buffer rbuf, OffsetNumber off, XLogRe ...@@ -448,6 +479,7 @@ dataSplitPage(GinBtree btree, Buffer lbuf, Buffer rbuf, OffsetNumber off, XLogRe
OffsetNumber separator; OffsetNumber separator;
ItemPointer bound; ItemPointer bound;
Page lpage = PageGetTempPageCopy(BufferGetPage(lbuf)); Page lpage = PageGetTempPageCopy(BufferGetPage(lbuf));
bool isleaf = GinPageIsLeaf(lpage);
ItemPointerData oldbound = *GinDataPageGetRightBound(lpage); ItemPointerData oldbound = *GinDataPageGetRightBound(lpage);
int sizeofitem = GinSizeOfDataPageItem(lpage); int sizeofitem = GinSizeOfDataPageItem(lpage);
OffsetNumber maxoff = GinPageGetOpaque(lpage)->maxoff; OffsetNumber maxoff = GinPageGetOpaque(lpage)->maxoff;
...@@ -469,10 +501,20 @@ dataSplitPage(GinBtree btree, Buffer lbuf, Buffer rbuf, OffsetNumber off, XLogRe ...@@ -469,10 +501,20 @@ dataSplitPage(GinBtree btree, Buffer lbuf, Buffer rbuf, OffsetNumber off, XLogRe
InvalidOffsetNumber : PostingItemGetBlockNumber(&(btree->pitem)); InvalidOffsetNumber : PostingItemGetBlockNumber(&(btree->pitem));
data.updateBlkno = dataPrepareData(btree, lpage, off); data.updateBlkno = dataPrepareData(btree, lpage, off);
memcpy(vector, GinDataPageGetItem(lpage, FirstOffsetNumber), if (isleaf)
maxoff * sizeofitem); {
memcpy(vector,
GinDataPageGetItemPointer(lpage, FirstOffsetNumber),
maxoff * sizeof(ItemPointerData));
}
else
{
memcpy(vector,
GinDataPageGetPostingItem(lpage, FirstOffsetNumber),
maxoff * sizeof(PostingItem));
}
if (GinPageIsLeaf(lpage) && GinPageRightMost(lpage) && off > GinPageGetOpaque(lpage)->maxoff) if (isleaf && GinPageRightMost(lpage) && off > GinPageGetOpaque(lpage)->maxoff)
{ {
nCopied = 0; nCopied = 0;
while (btree->curitem < btree->nitem && while (btree->curitem < btree->nitem &&
...@@ -491,7 +533,7 @@ dataSplitPage(GinBtree btree, Buffer lbuf, Buffer rbuf, OffsetNumber off, XLogRe ...@@ -491,7 +533,7 @@ dataSplitPage(GinBtree btree, Buffer lbuf, Buffer rbuf, OffsetNumber off, XLogRe
ptr = vector + (off - 1) * sizeofitem; ptr = vector + (off - 1) * sizeofitem;
if (maxoff + 1 - off != 0) if (maxoff + 1 - off != 0)
memmove(ptr + sizeofitem, ptr, (maxoff - off + 1) * sizeofitem); memmove(ptr + sizeofitem, ptr, (maxoff - off + 1) * sizeofitem);
if (GinPageIsLeaf(lpage)) if (isleaf)
{ {
memcpy(ptr, btree->items + btree->curitem, sizeofitem); memcpy(ptr, btree->items + btree->curitem, sizeofitem);
btree->curitem++; btree->curitem++;
...@@ -514,19 +556,32 @@ dataSplitPage(GinBtree btree, Buffer lbuf, Buffer rbuf, OffsetNumber off, XLogRe ...@@ -514,19 +556,32 @@ dataSplitPage(GinBtree btree, Buffer lbuf, Buffer rbuf, OffsetNumber off, XLogRe
GinInitPage(rpage, GinPageGetOpaque(lpage)->flags, pageSize); GinInitPage(rpage, GinPageGetOpaque(lpage)->flags, pageSize);
GinInitPage(lpage, GinPageGetOpaque(rpage)->flags, pageSize); GinInitPage(lpage, GinPageGetOpaque(rpage)->flags, pageSize);
memcpy(GinDataPageGetItem(lpage, FirstOffsetNumber), vector, separator * sizeofitem); if (isleaf)
memcpy(GinDataPageGetItemPointer(lpage, FirstOffsetNumber),
vector, separator * sizeof(ItemPointerData));
else
memcpy(GinDataPageGetPostingItem(lpage, FirstOffsetNumber),
vector, separator * sizeof(PostingItem));
GinPageGetOpaque(lpage)->maxoff = separator; GinPageGetOpaque(lpage)->maxoff = separator;
memcpy(GinDataPageGetItem(rpage, FirstOffsetNumber), if (isleaf)
vector + separator * sizeofitem, (maxoff - separator) * sizeofitem); memcpy(GinDataPageGetItemPointer(rpage, FirstOffsetNumber),
vector + separator * sizeof(ItemPointerData),
(maxoff - separator) * sizeof(ItemPointerData));
else
memcpy(GinDataPageGetPostingItem(rpage, FirstOffsetNumber),
vector + separator * sizeof(PostingItem),
(maxoff - separator) * sizeof(PostingItem));
GinPageGetOpaque(rpage)->maxoff = maxoff - separator; GinPageGetOpaque(rpage)->maxoff = maxoff - separator;
PostingItemSetBlockNumber(&(btree->pitem), BufferGetBlockNumber(lbuf)); PostingItemSetBlockNumber(&(btree->pitem), BufferGetBlockNumber(lbuf));
if (GinPageIsLeaf(lpage)) if (GinPageIsLeaf(lpage))
btree->pitem.key = *(ItemPointerData *) GinDataPageGetItem(lpage, btree->pitem.key = *GinDataPageGetItemPointer(lpage,
GinPageGetOpaque(lpage)->maxoff); GinPageGetOpaque(lpage)->maxoff);
else else
btree->pitem.key = ((PostingItem *) GinDataPageGetItem(lpage, btree->pitem.key = GinDataPageGetPostingItem(lpage,
GinPageGetOpaque(lpage)->maxoff))->key; GinPageGetOpaque(lpage)->maxoff)->key;
btree->rightblkno = BufferGetBlockNumber(rbuf); btree->rightblkno = BufferGetBlockNumber(rbuf);
/* set up right bound for left page */ /* set up right bound for left page */
...@@ -576,11 +631,11 @@ ginDataFillRoot(GinBtree btree, Buffer root, Buffer lbuf, Buffer rbuf) ...@@ -576,11 +631,11 @@ ginDataFillRoot(GinBtree btree, Buffer root, Buffer lbuf, Buffer rbuf)
li.key = *GinDataPageGetRightBound(lpage); li.key = *GinDataPageGetRightBound(lpage);
PostingItemSetBlockNumber(&li, BufferGetBlockNumber(lbuf)); PostingItemSetBlockNumber(&li, BufferGetBlockNumber(lbuf));
GinDataPageAddItem(page, &li, InvalidOffsetNumber); GinDataPageAddPostingItem(page, &li, InvalidOffsetNumber);
ri.key = *GinDataPageGetRightBound(rpage); ri.key = *GinDataPageGetRightBound(rpage);
PostingItemSetBlockNumber(&ri, BufferGetBlockNumber(rbuf)); PostingItemSetBlockNumber(&ri, BufferGetBlockNumber(rbuf));
GinDataPageAddItem(page, &ri, InvalidOffsetNumber); GinDataPageAddPostingItem(page, &ri, InvalidOffsetNumber);
} }
void void
......
...@@ -83,7 +83,7 @@ findItemInPostingPage(Page page, ItemPointer item, OffsetNumber *off) ...@@ -83,7 +83,7 @@ findItemInPostingPage(Page page, ItemPointer item, OffsetNumber *off)
*/ */
for (*off = FirstOffsetNumber; *off <= maxoff; (*off)++) for (*off = FirstOffsetNumber; *off <= maxoff; (*off)++)
{ {
res = ginCompareItemPointers(item, (ItemPointer) GinDataPageGetItem(page, *off)); res = ginCompareItemPointers(item, GinDataPageGetItemPointer(page, *off));
if (res <= 0) if (res <= 0)
return true; return true;
...@@ -154,7 +154,7 @@ scanPostingTree(Relation index, GinScanEntry scanEntry, ...@@ -154,7 +154,7 @@ scanPostingTree(Relation index, GinScanEntry scanEntry,
GinPageGetOpaque(page)->maxoff >= FirstOffsetNumber) GinPageGetOpaque(page)->maxoff >= FirstOffsetNumber)
{ {
tbm_add_tuples(scanEntry->matchBitmap, tbm_add_tuples(scanEntry->matchBitmap,
(ItemPointer) GinDataPageGetItem(page, FirstOffsetNumber), GinDataPageGetItemPointer(page, FirstOffsetNumber),
GinPageGetOpaque(page)->maxoff, false); GinPageGetOpaque(page)->maxoff, false);
scanEntry->predictNumberResult += GinPageGetOpaque(page)->maxoff; scanEntry->predictNumberResult += GinPageGetOpaque(page)->maxoff;
} }
...@@ -467,7 +467,8 @@ restartScanEntry: ...@@ -467,7 +467,8 @@ restartScanEntry:
*/ */
entry->list = (ItemPointerData *) palloc(BLCKSZ); entry->list = (ItemPointerData *) palloc(BLCKSZ);
entry->nlist = GinPageGetOpaque(page)->maxoff; entry->nlist = GinPageGetOpaque(page)->maxoff;
memcpy(entry->list, GinDataPageGetItem(page, FirstOffsetNumber), memcpy(entry->list,
GinDataPageGetItemPointer(page, FirstOffsetNumber),
GinPageGetOpaque(page)->maxoff * sizeof(ItemPointerData)); GinPageGetOpaque(page)->maxoff * sizeof(ItemPointerData));
LockBuffer(entry->buffer, GIN_UNLOCK); LockBuffer(entry->buffer, GIN_UNLOCK);
...@@ -587,8 +588,9 @@ entryGetNextItem(GinState *ginstate, GinScanEntry entry) ...@@ -587,8 +588,9 @@ entryGetNextItem(GinState *ginstate, GinScanEntry entry)
* Found position equal to or greater than stored * Found position equal to or greater than stored
*/ */
entry->nlist = GinPageGetOpaque(page)->maxoff; entry->nlist = GinPageGetOpaque(page)->maxoff;
memcpy(entry->list, GinDataPageGetItem(page, FirstOffsetNumber), memcpy(entry->list,
GinPageGetOpaque(page)->maxoff * sizeof(ItemPointerData)); GinDataPageGetItemPointer(page, FirstOffsetNumber),
GinPageGetOpaque(page)->maxoff * sizeof(ItemPointerData));
LockBuffer(entry->buffer, GIN_UNLOCK); LockBuffer(entry->buffer, GIN_UNLOCK);
......
...@@ -210,7 +210,7 @@ ginVacuumPostingTreeLeaves(GinVacuumState *gvs, BlockNumber blkno, bool isRoot, ...@@ -210,7 +210,7 @@ ginVacuumPostingTreeLeaves(GinVacuumState *gvs, BlockNumber blkno, bool isRoot,
for (i = FirstOffsetNumber; i <= GinPageGetOpaque(page)->maxoff; i++) for (i = FirstOffsetNumber; i <= GinPageGetOpaque(page)->maxoff; i++)
{ {
PostingItem *pitem = (PostingItem *) GinDataPageGetItem(page, i); PostingItem *pitem = GinDataPageGetPostingItem(page, i);
if (ginVacuumPostingTreeLeaves(gvs, PostingItemGetBlockNumber(pitem), FALSE, NULL)) if (ginVacuumPostingTreeLeaves(gvs, PostingItemGetBlockNumber(pitem), FALSE, NULL))
isChildHasVoid = TRUE; isChildHasVoid = TRUE;
...@@ -283,7 +283,7 @@ ginDeletePage(GinVacuumState *gvs, BlockNumber deleteBlkno, BlockNumber leftBlkn ...@@ -283,7 +283,7 @@ ginDeletePage(GinVacuumState *gvs, BlockNumber deleteBlkno, BlockNumber leftBlkn
#ifdef USE_ASSERT_CHECKING #ifdef USE_ASSERT_CHECKING
do do
{ {
PostingItem *tod = (PostingItem *) GinDataPageGetItem(parentPage, myoff); PostingItem *tod = GinDataPageGetPostingItem(parentPage, myoff);
Assert(PostingItemGetBlockNumber(tod) == deleteBlkno); Assert(PostingItemGetBlockNumber(tod) == deleteBlkno);
} while (0); } while (0);
...@@ -422,7 +422,7 @@ ginScanToDelete(GinVacuumState *gvs, BlockNumber blkno, bool isRoot, DataPageDel ...@@ -422,7 +422,7 @@ ginScanToDelete(GinVacuumState *gvs, BlockNumber blkno, bool isRoot, DataPageDel
me->blkno = blkno; me->blkno = blkno;
for (i = FirstOffsetNumber; i <= GinPageGetOpaque(page)->maxoff; i++) for (i = FirstOffsetNumber; i <= GinPageGetOpaque(page)->maxoff; i++)
{ {
PostingItem *pitem = (PostingItem *) GinDataPageGetItem(page, i); PostingItem *pitem = GinDataPageGetPostingItem(page, i);
if (ginScanToDelete(gvs, PostingItemGetBlockNumber(pitem), FALSE, me, i)) if (ginScanToDelete(gvs, PostingItemGetBlockNumber(pitem), FALSE, me, i))
i--; i--;
......
...@@ -189,7 +189,7 @@ ginRedoInsert(XLogRecPtr lsn, XLogRecord *record) ...@@ -189,7 +189,7 @@ ginRedoInsert(XLogRecPtr lsn, XLogRecord *record)
Assert(data->updateBlkno == InvalidBlockNumber); Assert(data->updateBlkno == InvalidBlockNumber);
for (i = 0; i < data->nitem; i++) for (i = 0; i < data->nitem; i++)
GinDataPageAddItem(page, items + i, data->offset + i); GinDataPageAddItemPointer(page, &items[i], data->offset + i);
} }
else else
{ {
...@@ -200,13 +200,13 @@ ginRedoInsert(XLogRecPtr lsn, XLogRecord *record) ...@@ -200,13 +200,13 @@ ginRedoInsert(XLogRecPtr lsn, XLogRecord *record)
if (data->updateBlkno != InvalidBlockNumber) if (data->updateBlkno != InvalidBlockNumber)
{ {
/* update link to right page after split */ /* update link to right page after split */
pitem = (PostingItem *) GinDataPageGetItem(page, data->offset); pitem = GinDataPageGetPostingItem(page, data->offset);
PostingItemSetBlockNumber(pitem, data->updateBlkno); PostingItemSetBlockNumber(pitem, data->updateBlkno);
} }
pitem = (PostingItem *) (XLogRecGetData(record) + sizeof(ginxlogInsert)); pitem = (PostingItem *) (XLogRecGetData(record) + sizeof(ginxlogInsert));
GinDataPageAddItem(page, pitem, data->offset); GinDataPageAddPostingItem(page, pitem, data->offset);
} }
} }
else else
...@@ -286,22 +286,28 @@ ginRedoSplit(XLogRecPtr lsn, XLogRecord *record) ...@@ -286,22 +286,28 @@ ginRedoSplit(XLogRecPtr lsn, XLogRecord *record)
for (i = 0; i < data->separator; i++) for (i = 0; i < data->separator; i++)
{ {
GinDataPageAddItem(lpage, ptr, InvalidOffsetNumber); if (data->isLeaf)
GinDataPageAddItemPointer(lpage, (ItemPointer) ptr, InvalidOffsetNumber);
else
GinDataPageAddPostingItem(lpage, (PostingItem *) ptr, InvalidOffsetNumber);
ptr += sizeofitem; ptr += sizeofitem;
} }
for (i = data->separator; i < data->nitem; i++) for (i = data->separator; i < data->nitem; i++)
{ {
GinDataPageAddItem(rpage, ptr, InvalidOffsetNumber); if (data->isLeaf)
GinDataPageAddItemPointer(rpage, (ItemPointer) ptr, InvalidOffsetNumber);
else
GinDataPageAddPostingItem(rpage, (PostingItem *) ptr, InvalidOffsetNumber);
ptr += sizeofitem; ptr += sizeofitem;
} }
/* set up right key */ /* set up right key */
bound = GinDataPageGetRightBound(lpage); bound = GinDataPageGetRightBound(lpage);
if (data->isLeaf) if (data->isLeaf)
*bound = *(ItemPointerData *) GinDataPageGetItem(lpage, GinPageGetOpaque(lpage)->maxoff); *bound = *GinDataPageGetItemPointer(lpage, GinPageGetOpaque(lpage)->maxoff);
else else
*bound = ((PostingItem *) GinDataPageGetItem(lpage, GinPageGetOpaque(lpage)->maxoff))->key; *bound = GinDataPageGetPostingItem(lpage, GinPageGetOpaque(lpage)->maxoff)->key;
bound = GinDataPageGetRightBound(rpage); bound = GinDataPageGetRightBound(rpage);
*bound = data->rightbound; *bound = data->rightbound;
...@@ -803,11 +809,11 @@ ginContinueSplit(ginIncompleteSplit *split) ...@@ -803,11 +809,11 @@ ginContinueSplit(ginIncompleteSplit *split)
PostingItemSetBlockNumber(&(btree.pitem), split->leftBlkno); PostingItemSetBlockNumber(&(btree.pitem), split->leftBlkno);
if (GinPageIsLeaf(page)) if (GinPageIsLeaf(page))
btree.pitem.key = *(ItemPointerData *) GinDataPageGetItem(page, btree.pitem.key = *GinDataPageGetItemPointer(page,
GinPageGetOpaque(page)->maxoff); GinPageGetOpaque(page)->maxoff);
else else
btree.pitem.key = ((PostingItem *) GinDataPageGetItem(page, btree.pitem.key = GinDataPageGetPostingItem(page,
GinPageGetOpaque(page)->maxoff))->key; GinPageGetOpaque(page)->maxoff)->key;
} }
btree.rightblkno = split->rightBlkno; btree.rightblkno = split->rightBlkno;
......
...@@ -232,10 +232,14 @@ typedef signed char GinNullCategory; ...@@ -232,10 +232,14 @@ typedef signed char GinNullCategory;
#define GinDataPageGetRightBound(page) ((ItemPointer) PageGetContents(page)) #define GinDataPageGetRightBound(page) ((ItemPointer) PageGetContents(page))
#define GinDataPageGetData(page) \ #define GinDataPageGetData(page) \
(PageGetContents(page) + MAXALIGN(sizeof(ItemPointerData))) (PageGetContents(page) + MAXALIGN(sizeof(ItemPointerData)))
/* non-leaf pages contain PostingItems */
#define GinDataPageGetPostingItem(page, i) \
((PostingItem *) (GinDataPageGetData(page) + ((i)-1) * sizeof(PostingItem)))
/* leaf pages contain ItemPointers */
#define GinDataPageGetItemPointer(page, i) \
((ItemPointer) (GinDataPageGetData(page) + ((i)-1) * sizeof(ItemPointerData)))
#define GinSizeOfDataPageItem(page) \ #define GinSizeOfDataPageItem(page) \
(GinPageIsLeaf(page) ? sizeof(ItemPointerData) : sizeof(PostingItem)) (GinPageIsLeaf(page) ? sizeof(ItemPointerData) : sizeof(PostingItem))
#define GinDataPageGetItem(page,i) \
(GinDataPageGetData(page) + ((i)-1) * GinSizeOfDataPageItem(page))
#define GinDataPageGetFreeSpace(page) \ #define GinDataPageGetFreeSpace(page) \
(BLCKSZ - MAXALIGN(SizeOfPageHeaderData) \ (BLCKSZ - MAXALIGN(SizeOfPageHeaderData) \
...@@ -534,7 +538,8 @@ extern uint32 ginMergeItemPointers(ItemPointerData *dst, ...@@ -534,7 +538,8 @@ extern uint32 ginMergeItemPointers(ItemPointerData *dst,
ItemPointerData *a, uint32 na, ItemPointerData *a, uint32 na,
ItemPointerData *b, uint32 nb); ItemPointerData *b, uint32 nb);
extern void GinDataPageAddItem(Page page, void *data, OffsetNumber offset); extern void GinDataPageAddItemPointer(Page page, ItemPointer data, OffsetNumber offset);
extern void GinDataPageAddPostingItem(Page page, PostingItem *data, OffsetNumber offset);
extern void GinPageDeletePostingItem(Page page, OffsetNumber offset); extern void GinPageDeletePostingItem(Page page, OffsetNumber offset);
typedef struct typedef struct
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment