diff --git a/src/hnsw.h b/src/hnsw.h index e23cb49..5581ca8 100644 --- a/src/hnsw.h +++ b/src/hnsw.h @@ -232,19 +232,27 @@ typedef HnswScanOpaqueData * HnswScanOpaque; typedef struct HnswVacuumState { + /* Info */ Relation index; IndexBulkDeleteResult *stats; IndexBulkDeleteCallback callback; void *callback_state; + + /* Settings */ int m; int efConstruction; - HTAB *deleted; - BufferAccessStrategy bas; + + /* Support functions */ FmgrInfo *procinfo; Oid collation; + + /* Variables */ + HTAB *deleted; + BufferAccessStrategy bas; HnswNeighborTuple ntup; - Size nsize; HnswElementData highestPoint; + + /* Memory */ MemoryContext tmpCtx; } HnswVacuumState; @@ -258,13 +266,13 @@ Buffer HnswNewBuffer(Relation index, ForkNumber forkNum); void HnswInitPage(Buffer buf, Page page); void HnswInitRegisterPage(Relation index, Buffer *buf, Page *page, GenericXLogState **state); void HnswInit(void); -List *SearchLayer(Datum q, List *ep, int ef, int lc, Relation index, FmgrInfo *procinfo, Oid collation, bool inserting, BlockNumber *skipPage, OffsetNumber *skipOffno); -HnswElement GetEntryPoint(Relation index); +List *HnswSearchLayer(Datum q, List *ep, int ef, int lc, Relation index, FmgrInfo *procinfo, Oid collation, bool inserting, BlockNumber *skipPage, OffsetNumber *skipOffno); +HnswElement HnswGetEntryPoint(Relation index); HnswElement HnswInitElement(ItemPointer tid, int m, double ml, int maxLevel); void HnswFreeElement(HnswElement element); HnswElement HnswInsertElement(HnswElement element, HnswElement entryPoint, Relation index, FmgrInfo *procinfo, Oid collation, int m, int efConstruction, List **updates, bool vacuuming); -HnswCandidate *EntryCandidate(HnswElement em, Datum q, Relation rel, FmgrInfo *procinfo, Oid collation, bool loadvec); -void UpdateMetaPage(Relation index, bool updateEntry, HnswElement entryPoint, BlockNumber insertPage, ForkNumber forkNum); +HnswCandidate *HnswEntryCandidate(HnswElement em, Datum q, Relation rel, FmgrInfo *procinfo, Oid collation, bool loadvec); +void HnswUpdateMetaPage(Relation index, bool updateEntry, HnswElement entryPoint, BlockNumber insertPage, ForkNumber forkNum); void HnswSetNeighborTuple(HnswNeighborTuple ntup, HnswElement e, int m); void HnswAddHeapTid(HnswElement element, ItemPointer heaptid); void HnswInitNeighbors(HnswElement element, int m); diff --git a/src/hnswbuild.c b/src/hnswbuild.c index ecdd11e..8e79db5 100644 --- a/src/hnswbuild.c +++ b/src/hnswbuild.c @@ -182,7 +182,7 @@ CreateElementPages(HnswBuildState * buildstate) GenericXLogFinish(state); UnlockReleaseBuffer(buf); - UpdateMetaPage(index, true, buildstate->entryPoint, insertPage, forkNum); + HnswUpdateMetaPage(index, true, buildstate->entryPoint, insertPage, forkNum); } /* diff --git a/src/hnswinsert.c b/src/hnswinsert.c index 6f62384..0ba7d50 100644 --- a/src/hnswinsert.c +++ b/src/hnswinsert.c @@ -266,7 +266,7 @@ WriteNewElementPages(Relation index, HnswElement e, int m) /* Update the insert page */ if (insertPage != originalInsertPage && (!OffsetNumberIsValid(freeOffno) || firstFreePage == insertPage)) - UpdateMetaPage(index, false, NULL, insertPage, MAIN_FORKNUM); + HnswUpdateMetaPage(index, false, NULL, insertPage, MAIN_FORKNUM); } /* @@ -405,7 +405,7 @@ WriteElement(Relation index, HnswElement element, int m, List *updates, HnswElem /* Update metapage if needed */ if (entryPoint == NULL || element->level > entryPoint->level) - UpdateMetaPage(index, true, element, InvalidBlockNumber, MAIN_FORKNUM); + HnswUpdateMetaPage(index, true, element, InvalidBlockNumber, MAIN_FORKNUM); } /* @@ -442,7 +442,7 @@ HnswInsertTuple(Relation index, Datum *values, bool *isnull, ItemPointer heap_ti element->vec = DatumGetVector(value); /* Get entry point */ - entryPoint = GetEntryPoint(index); + entryPoint = HnswGetEntryPoint(index); /* Insert element in graph */ dup = HnswInsertElement(element, entryPoint, index, procinfo, collation, m, efConstruction, &updates, false); diff --git a/src/hnswscan.c b/src/hnswscan.c index c01909a..365c6e3 100644 --- a/src/hnswscan.c +++ b/src/hnswscan.c @@ -18,20 +18,20 @@ GetScanItems(IndexScanDesc scan, Datum q) Oid collation = so->collation; List *ep = NIL; List *w; - HnswElement entryPoint = GetEntryPoint(index); + HnswElement entryPoint = HnswGetEntryPoint(index); if (entryPoint == NULL) return; - ep = lappend(ep, EntryCandidate(entryPoint, q, index, procinfo, collation, false)); + ep = lappend(ep, HnswEntryCandidate(entryPoint, q, index, procinfo, collation, false)); for (int lc = entryPoint->level; lc >= 1; lc--) { - w = SearchLayer(q, ep, 1, lc, index, procinfo, collation, false, NULL, NULL); + w = HnswSearchLayer(q, ep, 1, lc, index, procinfo, collation, false, NULL, NULL); ep = w; } - so->w = SearchLayer(q, ep, hnsw_ef_search, 0, index, procinfo, collation, false, NULL, NULL); + so->w = HnswSearchLayer(q, ep, hnsw_ef_search, 0, index, procinfo, collation, false, NULL, NULL); } /* diff --git a/src/hnswutils.c b/src/hnswutils.c index cff954b..0acad00 100644 --- a/src/hnswutils.c +++ b/src/hnswutils.c @@ -141,7 +141,7 @@ CreateElementFromBlock(BlockNumber blkno, OffsetNumber offno) * Get the entry point */ HnswElement -GetEntryPoint(Relation index) +HnswGetEntryPoint(Relation index) { Buffer buf; Page page; @@ -668,7 +668,7 @@ AddToVisited(HTAB *v, HnswCandidate * hc, Relation index, bool *found) * Algorithm 2 from paper */ List * -SearchLayer(Datum q, List *ep, int ef, int lc, Relation index, FmgrInfo *procinfo, Oid collation, bool inserting, BlockNumber *skipPage, OffsetNumber *skipOffno) +HnswSearchLayer(Datum q, List *ep, int ef, int lc, Relation index, FmgrInfo *procinfo, Oid collation, bool inserting, BlockNumber *skipPage, OffsetNumber *skipOffno) { ListCell *lc2; @@ -791,7 +791,7 @@ SearchLayer(Datum q, List *ep, int ef, int lc, Relation index, FmgrInfo *procinf * Create a candidate for the entry point */ HnswCandidate * -EntryCandidate(HnswElement entryPoint, Datum q, Relation index, FmgrInfo *procinfo, Oid collation, bool loadvec) +HnswEntryCandidate(HnswElement entryPoint, Datum q, Relation index, FmgrInfo *procinfo, Oid collation, bool loadvec) { HnswCandidate *hc = palloc(sizeof(HnswCandidate)); @@ -848,7 +848,7 @@ HnswInsertElement(HnswElement element, HnswElement entryPoint, Relation index, F /* Get entry point and level */ if (entryPoint != NULL) { - entryCandidate = EntryCandidate(entryPoint, q, index, procinfo, collation, true); + entryCandidate = HnswEntryCandidate(entryPoint, q, index, procinfo, collation, true); ep = lappend(ep, entryCandidate); entryLevel = entryPoint->level; removeEntryPoint = vacuuming && list_length(entryPoint->heaptids) == 0; @@ -861,7 +861,7 @@ HnswInsertElement(HnswElement element, HnswElement entryPoint, Relation index, F for (int lc = entryLevel; lc >= level + 1; lc--) { - w = SearchLayer(q, ep, 1, lc, index, procinfo, collation, true, skipPage, skipOffno); + w = HnswSearchLayer(q, ep, 1, lc, index, procinfo, collation, true, skipPage, skipOffno); ep = w; } @@ -872,7 +872,7 @@ HnswInsertElement(HnswElement element, HnswElement entryPoint, Relation index, F { int lm = HnswGetLayerM(m, lc); - w = SearchLayer(q, ep, efConstruction, lc, index, procinfo, collation, true, skipPage, skipOffno); + w = HnswSearchLayer(q, ep, efConstruction, lc, index, procinfo, collation, true, skipPage, skipOffno); if (removeEntryPoint) w = list_delete_ptr(w, entryCandidate); newNeighbors[lc] = SelectNeighbors(w, lm, lc, procinfo, collation, NULL); @@ -904,7 +904,7 @@ HnswInsertElement(HnswElement element, HnswElement entryPoint, Relation index, F * Update the metapage */ void -UpdateMetaPage(Relation index, bool updateEntry, HnswElement entryPoint, BlockNumber insertPage, ForkNumber forkNum) +HnswUpdateMetaPage(Relation index, bool updateEntry, HnswElement entryPoint, BlockNumber insertPage, ForkNumber forkNum) { Buffer buf; Page page; diff --git a/src/hnswvacuum.c b/src/hnswvacuum.c index 742356a..756ab1d 100644 --- a/src/hnswvacuum.c +++ b/src/hnswvacuum.c @@ -31,7 +31,7 @@ RemoveHeapTids(HnswVacuumState * vacuumstate) HnswElement highestPoint = &vacuumstate->highestPoint; Relation index = vacuumstate->index; BufferAccessStrategy bas = vacuumstate->bas; - HnswElement entryPoint = GetEntryPoint(vacuumstate->index); + HnswElement entryPoint = HnswGetEntryPoint(vacuumstate->index); /* Store separately since highestPoint.level is uint8 */ int highestLevel = -1; @@ -201,7 +201,7 @@ RepairGraphElement(HnswVacuumState * vacuumstate, HnswElement element) return; /* Refresh entry point for each element */ - entryPoint = GetEntryPoint(index); + entryPoint = HnswGetEntryPoint(index); /* Special case for entry point */ if (element->blkno == entryPoint->blkno && element->offno == entryPoint->offno) @@ -261,7 +261,7 @@ RepairGraphEntryPoint(HnswVacuumState * vacuumstate) RepairGraphElement(vacuumstate, highestPoint); } - entryPoint = GetEntryPoint(index); + entryPoint = HnswGetEntryPoint(index); if (entryPoint != NULL) { ItemPointerData epData; @@ -269,7 +269,7 @@ RepairGraphEntryPoint(HnswVacuumState * vacuumstate) ItemPointerSet(&epData, entryPoint->blkno, entryPoint->offno); if (DeletedContains(vacuumstate->deleted, &epData)) - UpdateMetaPage(index, true, highestPoint, InvalidBlockNumber, MAIN_FORKNUM); + HnswUpdateMetaPage(index, true, highestPoint, InvalidBlockNumber, MAIN_FORKNUM); else { /* Highest point will be used to repair */ @@ -484,7 +484,7 @@ MarkDeleted(HnswVacuumState * vacuumstate) UnlockReleaseBuffer(buf); } - UpdateMetaPage(index, false, NULL, insertPage, MAIN_FORKNUM); + HnswUpdateMetaPage(index, false, NULL, insertPage, MAIN_FORKNUM); } /*