Commit bb42e21b authored by Heikki Linnakangas's avatar Heikki Linnakangas

Change ginMergeItemPointers to return a palloc'd array.

That seems nicer than making it the caller's responsibility to pass a
suitable-sized array. All the callers were just palloc'ing an array anyway.
parent cf5d38f4
...@@ -1303,10 +1303,9 @@ addItemsToLeaf(disassembledLeaf *leaf, ItemPointer newItems, int nNewItems) ...@@ -1303,10 +1303,9 @@ addItemsToLeaf(disassembledLeaf *leaf, ItemPointer newItems, int nNewItems)
if (!cur->items) if (!cur->items)
cur->items = ginPostingListDecode(cur->seg, &cur->nitems); cur->items = ginPostingListDecode(cur->seg, &cur->nitems);
tmpitems = palloc((cur->nitems + nthis) * sizeof(ItemPointerData)); tmpitems = ginMergeItemPointers(cur->items, cur->nitems,
ntmpitems = ginMergeItemPointers(tmpitems, nextnew, nthis,
cur->items, cur->nitems, &ntmpitems);
nextnew, nthis);
if (ntmpitems != cur->nitems) if (ntmpitems != cur->nitems)
{ {
cur->items = tmpitems; cur->items = tmpitems;
......
...@@ -67,12 +67,9 @@ addItemPointersToLeafTuple(GinState *ginstate, ...@@ -67,12 +67,9 @@ addItemPointersToLeafTuple(GinState *ginstate,
/* merge the old and new posting lists */ /* merge the old and new posting lists */
oldItems = ginReadTuple(ginstate, attnum, old, &oldNPosting); oldItems = ginReadTuple(ginstate, attnum, old, &oldNPosting);
newNPosting = oldNPosting + nitem; newItems = ginMergeItemPointers(items, nitem,
newItems = (ItemPointerData *) palloc(sizeof(ItemPointerData) * newNPosting); oldItems, oldNPosting,
&newNPosting);
newNPosting = ginMergeItemPointers(newItems,
items, nitem,
oldItems, oldNPosting);
/* Compress the posting list, and try to a build tuple with room for it */ /* Compress the posting list, and try to a build tuple with room for it */
res = NULL; res = NULL;
......
...@@ -346,20 +346,18 @@ ginPostingListDecodeAllSegmentsToTbm(GinPostingList *ptr, int len, ...@@ -346,20 +346,18 @@ ginPostingListDecodeAllSegmentsToTbm(GinPostingList *ptr, int len,
/* /*
* Merge two ordered arrays of itempointers, eliminating any duplicates. * Merge two ordered arrays of itempointers, eliminating any duplicates.
* Returns the number of items in the result.
* Caller is responsible that there is enough space at *dst.
* *
* It's OK if 'dst' overlaps with the *beginning* of one of the arguments. * Returns a palloc'd array, and *nmerged is set to the number of items in
* the result, after eliminating duplicates.
*/ */
int ItemPointer
ginMergeItemPointers(ItemPointerData *dst, ginMergeItemPointers(ItemPointerData *a, uint32 na,
ItemPointerData *a, uint32 na, ItemPointerData *b, uint32 nb,
ItemPointerData *b, uint32 nb) int *nmerged)
{ {
ItemPointerData *dptr = dst; ItemPointerData *dst;
ItemPointerData *aptr = a,
*bptr = b; dst = (ItemPointer) palloc((na + nb) * sizeof(ItemPointerData));
int result;
/* /*
* If the argument arrays don't overlap, we can just append them to * If the argument arrays don't overlap, we can just append them to
...@@ -367,18 +365,22 @@ ginMergeItemPointers(ItemPointerData *dst, ...@@ -367,18 +365,22 @@ ginMergeItemPointers(ItemPointerData *dst,
*/ */
if (na == 0 || nb == 0 || ginCompareItemPointers(&a[na - 1], &b[0]) < 0) if (na == 0 || nb == 0 || ginCompareItemPointers(&a[na - 1], &b[0]) < 0)
{ {
memmove(dst, a, na * sizeof(ItemPointerData)); memcpy(dst, a, na * sizeof(ItemPointerData));
memmove(&dst[na], b, nb * sizeof(ItemPointerData)); memcpy(&dst[na], b, nb * sizeof(ItemPointerData));
result = na + nb; *nmerged = na + nb;
} }
else if (ginCompareItemPointers(&b[nb - 1], &a[0]) < 0) else if (ginCompareItemPointers(&b[nb - 1], &a[0]) < 0)
{ {
memmove(dst, b, nb * sizeof(ItemPointerData)); memcpy(dst, b, nb * sizeof(ItemPointerData));
memmove(&dst[nb], a, na * sizeof(ItemPointerData)); memcpy(&dst[nb], a, na * sizeof(ItemPointerData));
result = na + nb; *nmerged = na + nb;
} }
else else
{ {
ItemPointerData *dptr = dst;
ItemPointerData *aptr = a;
ItemPointerData *bptr = b;
while (aptr - a < na && bptr - b < nb) while (aptr - a < na && bptr - b < nb)
{ {
int cmp = ginCompareItemPointers(aptr, bptr); int cmp = ginCompareItemPointers(aptr, bptr);
...@@ -401,8 +403,8 @@ ginMergeItemPointers(ItemPointerData *dst, ...@@ -401,8 +403,8 @@ ginMergeItemPointers(ItemPointerData *dst,
while (bptr - b < nb) while (bptr - b < nb)
*dptr++ = *bptr++; *dptr++ = *bptr++;
result = dptr - dst; *nmerged = dptr - dst;
} }
return result; return dst;
} }
...@@ -919,9 +919,9 @@ extern int ginPostingListDecodeAllSegmentsToTbm(GinPostingList *ptr, int totalsi ...@@ -919,9 +919,9 @@ extern int ginPostingListDecodeAllSegmentsToTbm(GinPostingList *ptr, int totalsi
extern ItemPointer ginPostingListDecodeAllSegments(GinPostingList *ptr, int len, int *ndecoded); extern ItemPointer ginPostingListDecodeAllSegments(GinPostingList *ptr, int len, int *ndecoded);
extern ItemPointer ginPostingListDecode(GinPostingList *ptr, int *ndecoded); extern ItemPointer ginPostingListDecode(GinPostingList *ptr, int *ndecoded);
extern int ginMergeItemPointers(ItemPointerData *dst, extern ItemPointer ginMergeItemPointers(ItemPointerData *a, uint32 na,
ItemPointerData *a, uint32 na, ItemPointerData *b, uint32 nb,
ItemPointerData *b, uint32 nb); int *nmerged);
/* /*
* Merging the results of several gin scans compares item pointers a lot, * Merging the results of several gin scans compares item pointers a lot,
......
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