@@ -1074,6 +1074,32 @@ AddConnections(char *base, HnswElement element, List *neighbors, int lc)
1074
1074
a -> items [a -> length ++ ] = * ((HnswCandidate * ) lfirst (lc2 ));
1075
1075
}
1076
1076
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
+
1077
1103
/*
1078
1104
* Update connections
1079
1105
*/
@@ -1101,32 +1127,7 @@ HnswUpdateConnection(char *base, HnswElement element, HnswCandidate * hc, HnswNe
1101
1127
1102
1128
/* Load elements on insert */
1103
1129
if (index != NULL )
1104
- {
1105
- Datum q = HnswGetValue (base , hce );
1106
-
1107
- for (int i = 0 ; i < currentNeighbors -> length ; i ++ )
1108
- {
1109
- HnswCandidate * hc3 = & currentNeighbors -> items [i ];
1110
- HnswElement hc3Element = HnswPtrAccess (base , hc3 -> element );
1111
-
1112
- if (HnswPtrIsNull (base , hc3Element -> value ))
1113
- {
1114
- double distance ;
1115
-
1116
- HnswLoadElement (hc3Element , & distance , & q , index , procinfo , collation , true, NULL );
1117
- hc3 -> distance = distance ;
1118
- }
1119
- else
1120
- hc3 -> distance = GetElementDistance (base , hc3Element , q , procinfo , collation );
1121
-
1122
- /* Prune element if being deleted */
1123
- if (hc3Element -> heaptidsLength == 0 )
1124
- {
1125
- pruned = & currentNeighbors -> items [i ];
1126
- break ;
1127
- }
1128
- }
1129
- }
1130
+ LoadElementsForInsert (currentNeighbors , HnswGetValue (base , hce ), & pruned , index , procinfo , collation );
1130
1131
1131
1132
if (pruned == NULL )
1132
1133
{
0 commit comments