8000 DRY code for forming index value · postgrespro/pgvector@57c05c5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 57c05c5

Browse files
committed
DRY code for forming index value
1 parent 3126fbd commit 57c05c5

File tree

4 files changed

+43
-40
lines changed

4 files changed

+43
-40
lines changed

src/hnsw.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,10 +388,11 @@ void HnswSetNeighborTuple(char *base, HnswNeighborTuple ntup, HnswElement e, in
388388
void HnswAddHeapTid(HnswElement element, ItemPointer heaptid);
389389
HnswNeighborArray *HnswInitNeighborArray(int lm, HnswAllocator * allocator);
390390
void HnswInitNeighbors(char *base, HnswElement element, int m, HnswAllocator * alloc);
391-
bool HnswInsertTupleOnDisk(Relation index, Datum value, Datum *values, bool *isnull, ItemPointer heap_tid, bool building);
391+
bool HnswInsertTupleOnDisk(Relation index, Datum value, ItemPointer heaptid, bool building);
392392
void HnswUpdateNeighborsOnDisk(Relation index, FmgrInfo *procinfo, Oid collation, HnswElement e, int m, bool checkExisting, bool building);
393393
void HnswLoadElementFromTuple(HnswElement element, HnswElementTuple etup, bool loadHeaptids, bool loadVec);
394394
void HnswLoadElement(HnswElement element, double *distance, Datum *q, Relation index, FmgrInfo *procinfo, Oid collation, bool loadVec, double *maxDistance);
395+
bool HnswFormIndexValue(Datum *out, Datum *values, bool *isnull, const HnswTypeInfo * typeInfo, FmgrInfo *normprocinfo, Oid collation);
395396
void HnswSetElementTuple(char *base, HnswElementTuple etup, HnswElement element);
396397
void HnswUpdateConnection(char *base, HnswNeighborArray * neighbors, HnswElement newElement, float distance, int lm, int *updateIdx, Relation index, FmgrInfo *procinfo, Oid collation);
397398
bool HnswLoadNeighborTids(HnswElement element, ItemPointerData *indextids, Relation index, int m, int lm, int lc);

src/hnswbuild.c

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -473,30 +473,18 @@ InsertTupleInMemory(HnswBuildState * buildstate, HnswElement element)
473473
static bool
474474
InsertTuple(Relation index, Datum *values, bool *isnull, ItemPointer heaptid, HnswBuildState * buildstate)
475475
{
476-
const HnswTypeInfo *typeInfo = buildstate->typeInfo;
477476
HnswGraph *graph = buildstate->graph;
478477
HnswElement element;
479478
HnswAllocator *allocator = &buildstate->allocator;
480479
Size valueSize;
481480
Pointer valuePtr;
482481
LWLock *flushLock = &graph->flushLock;
483482
char *base = buildstate->hnswarea;
483+
Datum value;
484484

485-
/* Detoast once for all calls */
486-
Datum value = PointerGetDatum(PG_DETOAST_DATUM(values[0]));
487-
488-
/* Check value */
489-
if (typeInfo->checkValue != NULL)
490-
typeInfo->checkValue(DatumGetPointer(value));
491-
492-
/* Normalize if needed */
493-
if (buildstate->normprocinfo != NULL)
494-
{
495-
if (!HnswCheckNorm(buildstate->normprocinfo, buildstate->collation, value))
496-
return false;
497-
498-
value = HnswNormValue(typeInfo, buildstate->collation, value);
499-
}
485+
/* Form index value */
486+
if (!HnswFormIndexValue(& 8000 amp;value, values, isnull, buildstate->typeInfo, buildstate->normprocinfo, buildstate->collation))
487+
return false;
500488

501489
/* Get datum size */
502490
valueSize = VARSIZE_ANY(DatumGetPointer(value));
@@ -509,7 +497,7 @@ InsertTuple(Relation index, Datum *values, bool *isnull, ItemPointer heaptid, Hn
509497
{
510498
LWLockRelease(flushLock);
511499

512-
return HnswInsertTupleOnDisk(index, value, values, isnull, heaptid, true);
500+
return HnswInsertTupleOnDisk(index, value, heaptid, true);
513501
}
514502

515503
/*
@@ -541,7 +529,7 @@ InsertTuple(Relation index, Datum *values, bool *isnull, ItemPointer heaptid, Hn
541529

542530
LWLockRelease(flushLock);
543531

544-
return HnswInsertTupleOnDisk(index, value, values, isnull, heaptid, true);
532+
return HnswInsertTupleOnDisk(index, value, heaptid, true);
545533
}
546534

547535
/* Ok, we can proceed to allocate the element */

src/hnswinsert.c

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -679,7 +679,7 @@ UpdateGraphOnDisk(Relation index, FmgrInfo *procinfo, Oid collation, HnswElement
679679
* Insert a tuple into the index
680680
*/
681681
bool
682-
HnswInsertTupleOnDisk(Relation index, Datum value, Datum *values, bool *isnull, ItemPointer heap_tid, bool building)
682+
HnswInsertTupleOnDisk(Relation index, Datum value, ItemPointer heaptid, bool building)
683683
{
684684
HnswElement entryPoint;
685685
HnswElement element;
@@ -701,7 +701,7 @@ HnswInsertTupleOnDisk(Relation index, Datum value, Datum *values, bool *isnull,
701701
HnswGetMetaPageInfo(index, &m, &entryPoint);
702702

703703
/* Create an element */
704-
element = HnswInitElement(base, heap_tid, m, HnswGetMl(m), HnswGetMaxLevel(m), NULL);
704+
element = HnswInitElement(base, heaptid, m, HnswGetMl(m), HnswGetMaxLevel(m), NULL);
705705
HnswPtrStore(base, element->value, DatumGetPointer(value));
706706

707707
/* Prevent concurrent inserts when likely updating entry point */
@@ -734,31 +734,18 @@ HnswInsertTupleOnDisk(Relation index, Datum value, Datum *values, bool *isnull,
734734
* Insert a tuple into the index
735735
*/
736736
static void
737-
HnswInsertTuple(Relation index, Datum *values, bool *isnull, ItemPointer heap_tid)
737+
HnswInsertTuple(Relation index, Datum *values, bool *isnull, ItemPointer heaptid)
738738
{
739739
Datum value;
740740
const HnswTypeInfo *typeInfo = HnswGetTypeInfo(index);
741-
FmgrInfo *normprocinfo;
741+
FmgrInfo *normprocinfo = HnswOptionalProcInfo(index, HNSW_NORM_PROC);
742742
Oid collation = index->rd_indcollation[0];
743743

744-
/* Detoast once for all calls */
745-
value = PointerGetDatum(PG_DETOAST_DATUM(values[0]));
746-
747-
/* Check value */
748-
if (typeInfo->checkValue != NULL)
749-
typeInfo->checkValue(DatumGetPointer(value));
750-
751-
/* Normalize if needed */
752-
normprocinfo = HnswOptionalProcInfo(index, HNSW_NORM_PROC);
753-
if (normprocinfo != NULL)
754-
{
755-
if (!HnswCheckNorm(normprocinfo, collation, value))
756-
return;
757-
758-
value = HnswNormValue(typeInfo, collation, value);
759-
}
744+
/* Form index value */
745+
if (!HnswFormIndexValue(&value, values, isnull, typeInfo, normprocinfo, collation))
746+
return;
760747

761-
HnswInsertTupleOnDisk(index, value, values, isnull, heap_tid, false);
748+
HnswInsertTupleOnDisk(index, value, heaptid, false);
762749
}
763750

764751
/*

src/hnswutils.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,33 @@ HnswUpdateMetaPage(Relation index, int updateEntry, HnswElement entryPoint, Bloc
394394
UnlockReleaseBuffer(buf);
395395
}
396396

397+
/*
398+
* Form index value
399+
*/
400+
bool
401+
HnswFormIndexValue(Datum *out, Datum *values, bool *isnull, const HnswTypeInfo * typeInfo, FmgrInfo *normprocinfo, Oid collation)
402+
{
403+
/* Detoast once for all calls */
404+
Datum value = PointerGetDatum(PG_DETOAST_DATUM(values[0]));
405+
406+
/* Check value */
407+
if (typeInfo->checkValue != NULL)
408+
typeInfo->checkValue(DatumGetPointer(value));
409+
410+
/* Normalize if needed */
411+
if (normprocinfo != NULL)
412+
{
413+
if (!HnswCheckNorm(normprocinfo, collation, value))
414+
return false;
415+
416+
value = HnswNormValue(typeInfo, collation, value);
417+
}
418+
419+
*out = value;
420+
421+
return true;
422+
}
423+
397424
/*
398425
* Set element tuple, except for neighbor info
399426
*/

0 commit comments

Comments
 (0)
0