diff --git a/src/hnsw.h b/src/hnsw.h index c02c3ef..1f5b5c9 100644 --- a/src/hnsw.h +++ b/src/hnsw.h @@ -414,6 +414,7 @@ typedef struct HnswSearchState Relation index; HnswSupport *support; int m; + bool inserting; } HnswSearchState; typedef struct HnswScanOpaqueData @@ -471,7 +472,7 @@ bool HnswCheckNorm(HnswSupport * support, Datum value); Buffer HnswNewBuffer(Relation index, ForkNumber forkNum); void HnswInitPage(Buffer buf, Page page); void HnswInit(void); -List *HnswSearchLayer(HnswSearchState * searchState, List *ep, int ef, int lc, bool inserting, HnswElement skipElement, visited_hash * v, pairingheap **discarded, bool initVisited, int64 *tuples); +List *HnswSearchLayer(HnswSearchState * searchState, List *ep, int ef, int lc, HnswElement skipElement, visited_hash * v, pairingheap **discarded, bool initVisited, int64 *tuples); HnswElement HnswGetEntryPoint(Relation index); void HnswGetMetaPageInfo(Relation index, int *m, int *dimensions, HnswElement * entryPoint); void *HnswAlloc(HnswAllocator * allocator, Size size); diff --git a/src/hnswscan.c b/src/hnswscan.c index ee7ef05..859a640 100644 --- a/src/hnswscan.c +++ b/src/hnswscan.c @@ -48,6 +48,7 @@ GetScanItems(IndexScanDesc scan, Datum value) searchState->index = index; searchState->support = support; searchState->m = m; + searchState->inserting = false; if (entryPoint == NULL) return NIL; @@ -56,11 +57,11 @@ GetScanItems(IndexScanDesc scan, Datum value) for (int lc = entryPoint->level; lc >= 1; lc--) { - w = HnswSearchLayer(searchState, ep, 1, lc, false, NULL, NULL, NULL, true, NULL); + w = HnswSearchLayer(searchState, ep, 1, lc, NULL, NULL, NULL, true, NULL); ep = w; } - return HnswSearchLayer(searchState, ep, hnsw_ef_search, 0, false, NULL, &so->v, hnsw_iterative_scan != HNSW_ITERATIVE_SCAN_OFF ? &so->discarded : NULL, true, &so->tuples); + return HnswSearchLayer(searchState, ep, hnsw_ef_search, 0, NULL, &so->v, hnsw_iterative_scan != HNSW_ITERATIVE_SCAN_OFF ? &so->discarded : NULL, true, &so->tuples); } /* @@ -89,7 +90,7 @@ ResumeScanItems(IndexScanDesc scan) ep = lappend(ep, sc); } - return HnswSearchLayer(&so->searchState, ep, batch_size, 0, false, NULL, &so->v, &so->discarded, false, &so->tuples); + return HnswSearchLayer(&so->searchState, ep, batch_size, 0, NULL, &so->v, &so->discarded, false, &so->tuples); } /* diff --git a/src/hnswutils.c b/src/hnswutils.c index 3c4fbcc..fcdc886 100644 --- a/src/hnswutils.c +++ b/src/hnswutils.c @@ -827,11 +827,12 @@ HnswLoadUnvisitedFromDisk(HnswElement element, HnswUnvisited * unvisited, int *u * Algorithm 2 from paper */ List * -HnswSearchLayer(HnswSearchState * searchState, List *ep, int ef, int lc, bool inserting, HnswElement skipElement, visited_hash * v, pairingheap **discarded, bool initVisited, int64 *tuples) +HnswSearchLayer(HnswSearchState * searchState, List *ep, int ef, int lc, HnswElement skipElement, visited_hash * v, pairingheap **discarded, bool initVisited, int64 *tuples) { char *base = searchState->base; Relation index = searchState->index; int m = searchState->m; + bool inserting = searchState->inserting; List *w = NIL; pairingheap *C = pairingheap_allocate(CompareNearestCandidates, NULL); pairingheap *W = pairingheap_allocate(CompareFurthestCandidates, NULL); @@ -1301,6 +1302,7 @@ HnswFindElementNeighbors(char *base, HnswElement element, HnswElement entryPoint searchState.index = index; searchState.support = support; searchState.m = m; + searchState.inserting = true; /* Precompute hash */ if (inMemory) @@ -1317,7 +1319,7 @@ HnswFindElementNeighbors(char *base, HnswElement element, HnswElement entryPoint /* 1st phase: greedy search to insert level */ for (int lc = entryLevel; lc >= level + 1; lc--) { - w = HnswSearchLayer(&searchState, ep, 1, lc, true, skipElement, NULL, NULL, true, NULL); + w = HnswSearchLayer(&searchState, ep, 1, lc, skipElement, NULL, NULL, true, NULL); ep = w; } @@ -1336,7 +1338,7 @@ HnswFindElementNeighbors(char *base, HnswElement element, HnswElement entryPoint List *lw = NIL; ListCell *lc2; - w = HnswSearchLayer(&searchState, ep, efConstruction, lc, true, skipElement, NULL, NULL, true, NULL); + w = HnswSearchLayer(&searchState, ep, efConstruction, lc, skipElement, NULL, NULL, true, NULL); /* Convert search candidates to candidates */ foreach(lc2, w)