@@ -533,7 +533,7 @@ HnswGetDistance(Datum a, Datum b, HnswSupport * support)
533
533
* Load an element and optionally get its distance from q
534
534
*/
535
535
static void
536
- HnswLoadElementImpl (BlockNumber blkno , OffsetNumber offno , double * distance , Datum * q , Relation index , HnswSupport * support , bool loadVec , double * maxDistance , HnswElement * element )
536
+ HnswLoadElementImpl (BlockNumber blkno , OffsetNumber offno , double * distance , HnswQuery * q , Relation index , HnswSupport * support , bool loadVec , double * maxDistance , HnswElement * element )
537
537
{
538
538
Buffer buf ;
539
539
Page page ;
@@ -551,10 +551,10 @@ HnswLoadElementImpl(BlockNumber blkno, OffsetNumber offno, double *distance, Dat
551
551
/* Calculate distance */
552
552
if (distance != NULL )
553
553
{
554
- if (DatumGetPointer (* q ) == NULL )
554
+ if (DatumGetPointer (q -> value ) == NULL )
555
555
* distance = 0 ;
556
556
else
557
- * distance = HnswGetDistance (* q , PointerGetDatum (& etup -> data ), support );
557
+ * distance = HnswGetDistance (q -> value , PointerGetDatum (& etup -> data ), support );
558
558
}
559
559
560
560
/* Load element */
@@ -573,7 +573,7 @@ HnswLoadElementImpl(BlockNumber blkno, OffsetNumber offno, double *distance, Dat
573
573
* Load an element and optionally get its distance from q
574
574
*/
575
575
void
576
- HnswLoadElement (HnswElement element , double * distance , Datum * q , Relation index , HnswSupport * support , bool loadVec , double * maxDistance )
576
+ HnswLoadElement (HnswElement element , double * distance , HnswQuery * q , Relation index , HnswSupport * support , bool loadVec , double * maxDistance )
577
577
{
578
578
HnswLoadElementImpl (element -> blkno , element -> offno , distance , q , index , support , loadVec , maxDistance , & element );
579
579
}
@@ -582,18 +582,18 @@ HnswLoadElement(HnswElement element, double *distance, Datum *q, Relation index,
582
582
* Get the distance for an element
583
583
*/
584
584
static double
585
- GetElementDistance (char * base , HnswElement element , Datum q , HnswSupport * support )
585
+ GetElementDistance (char * base , HnswElement element , HnswQuery * q , HnswSupport * support )
586
586
{
587
587
Datum value = HnswGetValue (base , element );
588
588
589
- return HnswGetDistance (q , value , support );
589
+ return HnswGetDistance (q -> value , value , support );
590
590
}
591
591
592
592
/*
593
593
* Create a candidate for the entry point
594
594
*/
595
595
HnswSearchCandidate *
596
- HnswEntryCandidate (char * base , HnswElement entryPoint , Datum q , Relation index , HnswSupport * support , bool loadVec )
596
+ HnswEntryCandidate (char * base , HnswElement entryPoint , HnswQuery * q , Relation index , HnswSupport * support , bool loadVec )
597
597
{
598
598
HnswSearchCandidate * sc = palloc (sizeof (HnswSearchCandidate ));
599
599
bool inMemory = index == NULL ;
@@ -602,7 +602,7 @@ HnswEntryCandidate(char *base, HnswElement entryPoint, Datum q, Relation index,
602
602
if (inMemory )
603
603
sc -> distance = GetElementDistance (base , entryPoint , q , support );
604
604
else
605
- HnswLoadElement (entryPoint , & sc -> distance , & q , index , support , loadVec , NULL );
605
+ HnswLoadElement (entryPoint , & sc -> distance , q , index , support , loadVec , NULL );
606
606
return sc ;
607
607
}
608
608
@@ -791,7 +791,7 @@ HnswLoadUnvisitedFromDisk(HnswElement element, HnswUnvisited * unvisited, int *u
791
791
* Algorithm 2 from paper
792
792
*/
793
793
List *
794
- HnswSearchLayer (char * base , Datum q , List * ep , int ef , int lc , Relation index , HnswSupport * support , int m , bool inserting , HnswElement skipElement )
794
+ HnswSearchLayer (char * base , HnswQuery * q , List * ep , int ef , int lc , Relation index , HnswSupport * support , int m , bool i
EED3
nserting , HnswElement skipElement )
795
795
{
796
796
List * w = NIL ;
797
797
pairingheap * C = pairingheap_allocate (CompareNearestCandidates , NULL );
@@ -873,7 +873,7 @@ HnswSearchLayer(char *base, Datum q, List *ep, int ef, int lc, Relation index, H
873
873
874
874
/* Avoid any allocations if not adding */
875
875
eElement = NULL ;
876
- HnswLoadElementImpl (blkno , offno , & eDistance , & q , index , support , inserting , alwaysAdd ? NULL : & f -> distance , & eElement );
876
+ HnswLoadElementImpl (blkno , offno , & eDistance , q , index , support , inserting , alwaysAdd ? NULL : & f -> distance , & eElement );
877
877
878
878
if (eElement == NULL )
879
879
continue ;
@@ -1220,10 +1220,12 @@ HnswFindElementNeighbors(char *base, HnswElement element, HnswElement entryPoint
1220
1220
List * w ;
1221
1221
int level = element -> level ;
1222
1222
int entryLevel ;
1223
- Datum q = HnswGetValue ( base , element ) ;
1223
+ HnswQuery q ;
1224
1224
HnswElement skipElement = existing ? element : NULL ;
1225
1225
bool inMemory = index == NULL ;
1226
1226
1227
+ q .value = HnswGetValue (base , element );
1228
+
1227
1229
/* Precompute hash */
1228
1230
if (inMemory )
1229
1231
PrecomputeHash (base , element );
@@ -1233,13 +1235,13 @@ HnswFindElementNeighbors(char *base, HnswElement element, HnswElement entryPoint
1233
1235
return ;
1234
1236
1235
1237
/* Get entry point and level */
1236
- ep = list_make1 (HnswEntryCandidate (base , entryPoint , q , index , support , true));
1238
+ ep = list_make1 (HnswEntryCandidate (base , entryPoint , & q , index , support , true));
1237
1239
entryLevel = entryPoint -> level ;
1238
1240
1239
1241
/* 1st phase: greedy search to insert level */
1240
1242
for (int lc = entryLevel ; lc >= level + 1 ; lc -- )
1241
1243
{
1242
- w = HnswSearchLayer (base , q , ep , 1 , lc , index , support , m , true, skipElement );
1244
+ w = HnswSearchLayer (base , & q , ep , 1 , lc , index , support , m , true, skipElement );
1243
1245
ep = w ;
1244
1246
}
1245
1247
@@ -1258,7 +1260,7 @@ HnswFindElementNeighbors(char *base, HnswElement element, HnswElement entryPoint
1258
1260
List * lw = NIL ;
1259
1261
ListCell * lc2 ;
1260
1262
1261
- w = HnswSearchLayer (base , q , ep , efConstruction , lc , index , support , m , true, skipElement );
1263
+ w = HnswSearchLayer (base , & q , ep , efConstruction , lc , index , support , m , true, skipElement );
1262
1264
1263
1265
/* Convert search candidates to candidates */
1264
1266
foreach (lc2 , w )
0 commit comments