Moved logic to get update neighbor on disk to separate function

This commit is contained in:
Andrew Kane
2024-09-30 10:30:01 -07:00
parent a8b4b6675a
commit ff6da4fcea

View File

@@ -450,38 +450,20 @@ ConnectionExists(HnswElement e, HnswNeighborTuple ntup, int startIdx, int lm)
} }
/* /*
* Update neighbors * Update neighbor
*/ */
void static void
HnswUpdateNeighborsOnDisk(Relation index, FmgrInfo *procinfo, Oid collation, HnswElement e, int m, bool checkExisting, bool building) UpdateNeighborOnDisk(HnswElement element, HnswElement newElement, int idx, int m, int lm, int lc, Relation index, bool checkExisting, bool building)
{ {
char *base = NULL;
for (int lc = e->level; lc >= 0; lc--)
{
int lm = HnswGetLayerM(m, lc);
HnswNeighborArray *neighbors = HnswGetNeighbors(base, e, lc);
for (int i = 0; i < neighbors->length; i++)
{
HnswCandidate *hc = &neighbors->items[i];
Buffer buf; Buffer buf;
Page page; Page page;
GenericXLogState *state; GenericXLogState *state;
HnswNeighborTuple ntup; HnswNeighborTuple ntup;
int idx;
int startIdx; int startIdx;
HnswElement neighborElement = HnswPtrAccess(base, hc->element); OffsetNumber offno = element->neighborOffno;
OffsetNumber offno = neighborElement->neighborOffno;
idx = GetUpdateIndex(neighborElement, e, hc->distance, m, lm, lc, index, procinfo, collation);
/* New element was not selected as a neighbor */
if (idx == -1)
continue;
/* Register page */ /* Register page */
buf = ReadBuffer(index, neighborElement->neighborPage); buf = ReadBuffer(index, element->neighborPage);
LockBuffer(buf, BUFFER_LOCK_EXCLUSIVE); LockBuffer(buf, BUFFER_LOCK_EXCLUSIVE);
if (building) if (building)
{ {
@@ -498,10 +480,10 @@ HnswUpdateNeighborsOnDisk(Relation index, FmgrInfo *procinfo, Oid collation, Hns
ntup = (HnswNeighborTuple) PageGetItem(page, PageGetItemId(page, offno)); ntup = (HnswNeighborTuple) PageGetItem(page, PageGetItemId(page, offno));
/* Calculate index for update */ /* Calculate index for update */
startIdx = (neighborElement->level - lc) * m; startIdx = (element->level - lc) * m;
/* Check for existing connection */ /* Check for existing connection */
if (checkExisting && ConnectionExists(e, ntup, startIdx, lm)) if (checkExisting && ConnectionExists(newElement, ntup, startIdx, lm))
idx = -1; idx = -1;
else if (idx == -2) else if (idx == -2)
{ {
@@ -525,7 +507,7 @@ HnswUpdateNeighborsOnDisk(Relation index, FmgrInfo *procinfo, Oid collation, Hns
ItemPointer indextid = &ntup->indextids[idx]; ItemPointer indextid = &ntup->indextids[idx];
/* Update neighbor on the buffer */ /* Update neighbor on the buffer */
ItemPointerSet(indextid, e->blkno, e->offno); ItemPointerSet(indextid, newElement->blkno, newElement->offno);
/* Commit */ /* Commit */
if (building) if (building)
@@ -538,6 +520,34 @@ HnswUpdateNeighborsOnDisk(Relation index, FmgrInfo *procinfo, Oid collation, Hns
UnlockReleaseBuffer(buf); UnlockReleaseBuffer(buf);
} }
/*
* Update neighbors
*/
void
HnswUpdateNeighborsOnDisk(Relation index, FmgrInfo *procinfo, Oid collation, HnswElement e, int m, bool checkExisting, bool building)
{
char *base = NULL;
for (int lc = e->level; lc >= 0; lc--)
{
int lm = HnswGetLayerM(m, lc);
HnswNeighborArray *neighbors = HnswGetNeighbors(base, e, lc);
for (int i = 0; i < neighbors->length; i++)
{
HnswCandidate *hc = &neighbors->items[i];
HnswElement neighborElement = HnswPtrAccess(base, hc->element);
int idx;
idx = GetUpdateIndex(neighborElement, e, hc->distance, m, lm, lc, index, procinfo, collation);
/* New element was not selected as a neighbor */
if (idx == -1)
continue;
UpdateNeighborOnDisk(neighborElement, e, idx, m, lm, lc, index, checkExisting, building);
}
} }
} }