mirror of
https://github.com/pgvector/pgvector.git
synced 2026-07-12 23:56:55 +08:00
Moved logic to get update index to separate function
This commit is contained in:
@@ -390,6 +390,45 @@ LoadElementsForInsert(HnswNeighborArray * neighbors, Datum q, int *idx, Relation
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Get update index
|
||||||
|
*/
|
||||||
|
static int
|
||||||
|
GetUpdateIndex(HnswElement element, HnswElement newElement, float distance, int m, int lm, int lc, Relation index, FmgrInfo *procinfo, Oid collation)
|
||||||
|
{
|
||||||
|
char *base = NULL;
|
||||||
|
int idx = -1;
|
||||||
|
HnswNeighborArray *neighbors;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Get latest neighbors since they may have changed. Do not lock yet since
|
||||||
|
* selecting neighbors can take time. Could use optimistic locking to
|
||||||
|
* retry if another update occurs before getting exclusive lock.
|
||||||
|
*/
|
||||||
|
neighbors = HnswLoadNeighbors(element, index, m, lm, lc);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Could improve performance for vacuuming by checking neighbors against
|
||||||
|
* list of elements being deleted to find index. It's important to exclude
|
||||||
|
* already deleted elements for this since they can be replaced at any
|
||||||
|
* time.
|
||||||
|
*/
|
||||||
|
|
||||||
|
if (neighbors->length < lm)
|
||||||
|
idx = -2;
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Datum q = HnswGetValue(base, element);
|
||||||
|
|
||||||
|
LoadElementsForInsert(neighbors, q, &idx, index, procinfo, collation);
|
||||||
|
|
||||||
|
if (idx == -1)
|
||||||
|
HnswUpdateConnection(base, neighbors, newElement, distance, lm, &idx, index, procinfo, collation);
|
||||||
|
}
|
||||||
|
|
||||||
|
return idx;
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Check if connection already exists
|
* Check if connection already exists
|
||||||
*/
|
*/
|
||||||
@@ -430,39 +469,12 @@ HnswUpdateNeighborsOnDisk(Relation index, FmgrInfo *procinfo, Oid collation, Hns
|
|||||||
Page page;
|
Page page;
|
||||||
GenericXLogState *state;
|
GenericXLogState *state;
|
||||||
HnswNeighborTuple ntup;
|
HnswNeighborTuple ntup;
|
||||||
int idx = -1;
|
int idx;
|
||||||
int startIdx;
|
int startIdx;
|
||||||
HnswElement neighborElement = HnswPtrAccess(base, hc->element);
|
HnswElement neighborElement = HnswPtrAccess(base, hc->element);
|
||||||
OffsetNumber offno = neighborElement->neighborOffno;
|
OffsetNumber offno = neighborElement->neighborOffno;
|
||||||
HnswNeighborArray *neighborNeighbors;
|
|
||||||
|
|
||||||
/*
|
idx = GetUpdateIndex(neighborElement, e, hc->distance, m, lm, lc, index, procinfo, collation);
|
||||||
* Get latest neighbors since they may have changed. Do not lock
|
|
||||||
* yet since selecting neighbors can take time. Could use
|
|
||||||
* optimistic locking to retry if another update occurs before
|
|
||||||
* getting exclusive lock.
|
|
||||||
*/
|
|
||||||
neighborNeighbors = HnswLoadNeighbors(neighborElement, index, m, lm, lc);
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Could improve performance for vacuuming by checking neighbors
|
|
||||||
* against list of elements being deleted to find index. It's
|
|
||||||
* important to exclude already deleted elements for this since
|
|
||||||
* they can be replaced at any time.
|
|
||||||
*/
|
|
||||||
|
|
||||||
/* Select neighbors */
|
|
||||||
if (neighborNeighbors->length < lm)
|
|
||||||
idx = -2;
|
|
||||||
else
|
|
||||||
{
|
|
||||||
Datum q = HnswGetValue(base, neighborElement);
|
|
||||||
|
|
||||||
LoadElementsForInsert(neighborNeighbors, q, &idx, index, procinfo, collation);
|
|
||||||
|
|
||||||
if (idx == -1)
|
|
||||||
HnswUpdateConnection(base, neighborNeighbors, e, hc->distance, lm, &idx, index, procinfo, collation);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* New element was not selected as a neighbor */
|
/* New element was not selected as a neighbor */
|
||||||
if (idx == -1)
|
if (idx == -1)
|
||||||
|
|||||||
Reference in New Issue
Block a user