8000 Moved insert-specific code to hnswinsert.c · postgrespro/pgvector@8eb8cdf · GitHub
[go: up one dir, main page]

Skip to content

Commit 8eb8cdf

Browse files
committed
Moved insert-specific code to hnswinsert.c
1 parent 4c72f91 commit 8eb8cdf

File tree

2 files changed

+46
-45
lines changed

2 files changed

+46
-45
lines changed

src/hnswinsert.c

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,32 @@ HnswLoadNeighbors(HnswElement element, Relation index, int m, int lm, int lc)
364364
return neighbors;
365365
}
366366

367+
/*
368+
* Load elements for insert
369+
*/
370+
static void
371+
LoadElementsForInsert(HnswNeighborArray * neighbors, Datum q, int *idx, Relation index, FmgrInfo *procinfo, Oid collation)
372+
{
373+
char *base = NULL;
374+
375+
for (int i = 0; i < neighbors->length; i++)
376+
{
377+
HnswCandidate *hc = &neighbors->items[i];
378+
HnswElement element = HnswPtrAccess(base, hc->element);
379+
double distance;
380+
381+
HnswLoadElement(element, &distance, &q, index, procinfo, collation, true, NULL);
382+
hc->distance = distance;
383+
384+
/* Prune element if being deleted */
385+
if (element->heaptidsLength == 0)
386+
{
387+
*idx = i;
388+
break;
389+
}
390+
}
391+
}
392+
367393
/*
368394
* Check if connection already exists
369395
*/
@@ -426,7 +452,17 @@ HnswUpdateNeighborsOnDisk(Relation index, FmgrInfo *procinfo, Oid collation, Hns
426452
*/
427453

428454
/* Select neighbors */
429-
HnswUpdateConnection(NULL, e, hc, neighborNeighbors, lm, &idx, index, procinfo, collation);
455+
if (neighbors->length < lm)
456+
idx = -2;
457+
else
458+
{
459+
Datum q = HnswGetValue(base, neighborElement);
460+
461+
LoadElementsForInsert(neighborNeighbors, q, &idx, index, procinfo, collation);
462+
463+
if (idx == -1)
464+
HnswUpdateConnection(base, e, hc, neighborNeighbors, lm, &idx, index, procinfo, collation);
465+
}
430466

431467
/* New element was not selected as a neighbor */
432468
if (idx == -1)

src/hnswutils.c

Lines changed: 9 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1074,39 +1074,12 @@ AddConnections(char *base, HnswElement element, List *neighbors, int lc)
10741074
a->items[a->length++] = *((HnswCandidate *) lfirst(lc2));
10751075
}
10761076

1077-
/*
1078-
* Load elements for insert
1079-
*/
1080-
static void
1081-
LoadElementsForInsert(HnswNeighborArray * neighbors, Datum q, HnswCandidate * *pruned, Relation index, FmgrInfo *procinfo, Oid collation)
1082-
{
1083-
char *base = NULL;
1084-
1085-
for (int i = 0; i < neighbors->length; i++)
1086-
{
1087-
HnswCandidate *hc = &neighbors->items[i];
1088-
HnswElement element = HnswPtrAccess(base, hc->element);
1089-
double distance;
1090-
1091-
HnswLoadElement(element, &distance, &q, index, procinfo, collation, true, NULL);
1092-
hc->distance = distance;
1093-
1094-
/* Prune element if being deleted */
1095-
if (element->heaptidsLength == 0)
1096-
{
1097-
*pruned = &neighbors->items[i];
1098-
break;
1099-
}
1100-
}
1101-
}
1102-
11031077
/*
11041078
* Update connections
11051079
*/
11061080
void
11071081
HnswUpdateConnection(char *base, HnswElement newElement, HnswCandidate * hc, HnswNeighborArray * neighbors, int lm, int *updateIdx, Relation index, FmgrInfo *procinfo, Oid collation)
11081082
{
1109-
HnswElement hce = HnswPtrAccess(base, hc->element);
11101083
HnswCandidate newHc;
11111084

11121085
HnswPtrStore(base, newHc.element, newElement);
@@ -1123,27 +1096,19 @@ HnswUpdateConnection(char *base, HnswElement newElement, HnswCandidate * hc, Hns
11231096
else
11241097
{
11251098
/* Shrink connections */
1099+
List *c = NIL;
11261100
HnswCandidate *pruned = NULL;
11271101

1128-
/* Load elements on insert */
1129-
if (index != NULL)
1130-
LoadElementsForInsert(neighbors, HnswGetValue(base, hce), &pruned, index, procinfo, collation);
1131-
1132-
if (pruned == NULL)
1133-
{
1134-
List *c = NIL;
1135-
1136-
/* Add candidates */
1137-
for (int i = 0; i < neighbors->length; i++)
1138-
c = lappend(c, &neighbors->items[i]);
1139-
c = lappend(c, &newHc);
1102+
/* Add candidates */
1103+
for (int i = 0; i < neighbors->length; i++)
1104+
c = lappend(c, &neighbors->items[i]);
1105+
c = lappend(c, &newHc);
11401106

1141-
SelectNeighbors(base, c, lm, procinfo, collation, neighbors, &newHc, &pruned, true);
1107+
SelectNeighbors(base, c, lm, procinfo, collation, neighbors, &newHc, &pruned, true);
11421108

1143-
/* Should not happen */
1144-
if (pruned == NULL)
1145-
return;
1146-
}
1109+
/* Should not happen */
1110+
if (pruned == NULL)
1111+
return;
11471112

11481113
/* Find and replace the pruned element */
11491114
for (int i = 0; i < neighbors->length; i++)

0 commit comments

Comments
 (0)
0