pgvector.git / summary / log / commit / refs
commit 57c05c59a2c5ba0f786d55a650da7517170fdf7e
Author: Andrew Kane <andrew@ankane.org>
Date: Thu Oct 10 03:50:17 2024 +0000
DRY code for forming index value
src/hnsw.h | 3 ++-
src/hnswbuild.c | 24 ++++++------------------
src/hnswinsert.c | 29 ++++++++---------------------
src/hnswutils.c | 27 +++++++++++++++++++++++++++
4 files changed, 43 insertions(+), 40 deletions(-)
diff --git a/src/hnsw.h b/src/hnsw.h
index 116d9bca..f9e56211 100644
--- a/src/hnsw.h
+++ b/src/hnsw.h
@@ -388,10 +388,11 @@ void HnswSetNeighborTuple(char *base, HnswNeighborTuple ntup, HnswElement e, in
void HnswAddHeapTid(HnswElement element, ItemPointer heaptid);
HnswNeighborArray *HnswInitNeighborArray(int lm, HnswAllocator * allocator);
void HnswInitNeighbors(char *base, HnswElement element, int m, HnswAllocator * alloc);
-bool HnswInsertTupleOnDisk(Relation index, Datum value, Datum *values, bool *isnull, ItemPointer heap_tid, bool building);
+bool HnswInsertTupleOnDisk(Relation index, Datum value, ItemPointer heaptid, bool building);
void HnswUpdateNeighborsOnDisk(Relation index, FmgrInfo *procinfo, Oid collation, HnswElement e, int m, bool checkExisting, bool building);
void HnswLoadElementFromTuple(HnswElement element, HnswElementTuple etup, bool loadHeaptids, bool loadVec);
void HnswLoadElement(HnswElement element, double *distance, Datum *q, Relation index, FmgrInfo *procinfo, Oid collation, bool loadVec, double *maxDistance);
+bool HnswFormIndexValue(Datum *out, Datum *values, bool *isnull, const HnswTypeInfo * typeInfo, FmgrInfo *normprocinfo, Oid collation);
void HnswSetElementTuple(char *base, HnswElementTuple etup, HnswElement element);
void HnswUpdateConnection(char *base, HnswNeighborArray * neighbors, HnswElement newElement, float distance, int lm, int *updateIdx, Relation index, FmgrInfo *procinfo, Oid collation);
bool HnswLoadNeighborTids(HnswElement element, ItemPointerData *indextids, Relation index, int m, int lm, int lc);
diff --git a/src/hnswbuild.c b/src/hnswbuild.c
index 87d48231..02e1749f 100644
--- a/src/hnswbuild.c
+++ b/src/hnswbuild.c
@@ -473,7 +473,6 @@ InsertTupleInMemory(HnswBuildState * buildstate, HnswElement element)
static bool
InsertTuple(Relation index, Datum *values, bool *isnull, ItemPointer heaptid, HnswBuildState * buildstate)
{
- const HnswTypeInfo *typeInfo = buildstate->typeInfo;
HnswGraph *graph = buildstate->graph;
HnswElement element;
HnswAllocator *allocator = &buildstate->allocator;
@@ -481,22 +480,11 @@ InsertTuple(Relation index, Datum *values, bool *isnull, ItemPointer heaptid, Hn
Pointer valuePtr;
LWLock *flushLock = &graph->flushLock;
char *base = buildstate->hnswarea;
+ Datum value;
- /* Detoast once for all calls */
- Datum value = PointerGetDatum(PG_DETOAST_DATUM(values[0]));
-
- /* Check value */
- if (typeInfo->checkValue != NULL)
- typeInfo->checkValue(DatumGetPointer(value));
-
- /* Normalize if needed */
- if (buildstate->normprocinfo != NULL)
- {
- if (!HnswCheckNorm(buildstate->normprocinfo, buildstate->collation, value))
- return false;
-
- value = HnswNormValue(typeInfo, buildstate->collation, value);
- }
+ /* Form index value */
+ if (!HnswFormIndexValue(&value, values, isnull, buildstate->typeInfo, buildstate->normprocinfo, buildstate->collation))
+ return false;
/* Get datum size */
valueSize = VARSIZE_ANY(DatumGetPointer(value));
@@ -509,7 +497,7 @@ InsertTuple(Relation index, Datum *values, bool *isnull, ItemPointer heaptid, Hn
{
LWLockRelease(flushLock);
- return HnswInsertTupleOnDisk(index, value, values, isnull, heaptid, true);
+ return HnswInsertTupleOnDisk(index, value, heaptid, true);
}
/*
@@ -541,7 +529,7 @@ InsertTuple(Relation index, Datum *values, bool *isnull, ItemPointer heaptid, Hn
LWLockRelease(flushLock);
- return HnswInsertTupleOnDisk(index, value, values, isnull, heaptid, true);
+ return HnswInsertTupleOnDisk(index, value, heaptid, true);
}
/* Ok, we can proceed to allocate the element */
diff --git a/src/hnswinsert.c b/src/hnswinsert.c
index 2dfd8d3f..9c5b1907 100644
--- a/src/hnswinsert.c
+++ b/src/hnswinsert.c
@@ -679,7 +679,7 @@ UpdateGraphOnDisk(Relation index, FmgrInfo *procinfo, Oid collation, HnswElement
* Insert a tuple into the index
*/
bool
-HnswInsertTupleOnDisk(Relation index, Datum value, Datum *values, bool *isnull, ItemPointer heap_tid, bool building)
+HnswInsertTupleOnDisk(Relation index, Datum value, ItemPointer heaptid, bool building)
{
HnswElement entryPoint;
HnswElement element;
@@ -701,7 +701,7 @@ HnswInsertTupleOnDisk(Relation index, Datum value, Datum *values, bool *isnull,
HnswGetMetaPageInfo(index, &m, &entryPoint);
/* Create an element */
- element = HnswInitElement(base, heap_tid, m, HnswGetMl(m), HnswGetMaxLevel(m), NULL);
+ element = HnswInitElement(base, heaptid, m, HnswGetMl(m), HnswGetMaxLevel(m), NULL);
HnswPtrStore(base, element->value, DatumGetPointer(value));
/* Prevent concurrent inserts when likely updating entry point */
@@ -734,31 +734,18 @@ HnswInsertTupleOnDisk(Relation index, Datum value, Datum *values, bool *isnull,
* Insert a tuple into the index
*/
static void
-HnswInsertTuple(Relation index, Datum *values, bool *isnull, ItemPointer heap_tid)
+HnswInsertTuple(Relation index, Datum *values, bool *isnull, ItemPointer heaptid)
{
Datum value;
const HnswTypeInfo *typeInfo = HnswGetTypeInfo(index);
- FmgrInfo *normprocinfo;
+ FmgrInfo *normprocinfo = HnswOptionalProcInfo(index, HNSW_NORM_PROC);
Oid collation = index->rd_indcollation[0];
- /* Detoast once for all calls */
- value = PointerGetDatum(PG_DETOAST_DATUM(values[0]));
-
- /* Check value */
- if (typeInfo->checkValue != NULL)
- typeInfo->checkValue(DatumGetPointer(value));
-
- /* Normalize if needed */
- normprocinfo = HnswOptionalProcInfo(index, HNSW_NORM_PROC);
- if (normprocinfo != NULL)
- {
- if (!HnswCheckNorm(normprocinfo, collation, value))
- return;
-
- value = HnswNormValue(typeInfo, collation, value);
- }
+ /* Form index value */
+ if (!HnswFormIndexValue(&value, values, isnull, typeInfo, normprocinfo, collation))
+ return;
- HnswInsertTupleOnDisk(index, value, values, isnull, heap_tid, false);
+ HnswInsertTupleOnDisk(index, value, heaptid, false);
}
/*
diff --git a/src/hnswutils.c b/src/hnswutils.c
index 856c3098..743fa87e 100644
--- a/src/hnswutils.c
+++ b/src/hnswutils.c
@@ -394,6 +394,33 @@ HnswUpdateMetaPage(Relation index, int updateEntry, HnswElement entryPoint, Bloc
UnlockReleaseBuffer(buf);
}
+/*
+ * Form index value
+ */
+bool
+HnswFormIndexValue(Datum *out, Datum *values, bool *isnull, const HnswTypeInfo * typeInfo, FmgrInfo *normprocinfo, Oid collation)
+{
+ /* Detoast once for all calls */
+ Datum value = PointerGetDatum(PG_DETOAST_DATUM(values[0]));
+
+ /* Check value */
+ if (typeInfo->checkValue != NULL)
+ typeInfo->checkValue(DatumGetPointer(value));
+
+ /* Normalize if needed */
+ if (normprocinfo != NULL)
+ {
+ if (!HnswCheckNorm(normprocinfo, collation, value))
+ return false;
+
+ value = HnswNormValue(typeInfo, collation, value);
+ }
+
+ *out = value;
+
+ return true;
+}
+
/*
* Set element tuple, except for neighbor info
*/
[parent: 3126fbdb6f24]