Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit53a8734

Browse files
committed
Added IndexTuple to HNSW elements (first step to support multiple attributes)
1 parent7484625 commit53a8734

File tree

4 files changed

+51
-23
lines changed

4 files changed

+51
-23
lines changed

‎src/hnsw.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ HnswPtrDeclare(HnswElementData, HnswElementRelptr, HnswElementPtr);
133133
HnswPtrDeclare(HnswNeighborArray,HnswNeighborArrayRelptr,HnswNeighborArrayPtr);
134134
HnswPtrDeclare(HnswNeighborArrayPtr,HnswNeighborsRelptr,HnswNeighborsPtr);
135135
HnswPtrDeclare(char,DatumRelptr,DatumPtr);
136+
HnswPtrDeclare(IndexTupleData,IndexTupleRelptr,IndexTuplePtr);
136137

137138
structHnswElementData
138139
{
@@ -149,6 +150,7 @@ struct HnswElementData
149150
OffsetNumberneighborOffno;
150151
BlockNumberneighborPage;
151152
DatumPtrvalue;
153+
IndexTuplePtritup;
152154
LWLocklock;
153155
};
154156

@@ -288,6 +290,7 @@ typedef struct HnswBuildState
288290
HnswGraph*graph;
289291
doubleml;
290292
intmaxLevel;
293+
TupleDesctupdesc;
291294

292295
/* Memory */
293296
MemoryContextgraphCtx;
@@ -428,11 +431,12 @@ voidHnswSetNeighborTuple(char *base, HnswNeighborTuple ntup, HnswElement e, in
428431
voidHnswAddHeapTid(HnswElementelement,ItemPointerheaptid);
429432
HnswNeighborArray*HnswInitNeighborArray(intlm,HnswAllocator*allocator);
430433
voidHnswInitNeighbors(char*base,HnswElementelement,intm,HnswAllocator*alloc);
431-
boolHnswInsertTupleOnDisk(Relationindex,HnswSupport*support,Datumvalue,ItemPointerheaptid,boolbuilding);
434+
boolHnswInsertTupleOnDisk(Relationindex,HnswSupport*support,TupleDesctupdesc,IndexTupleitup,ItemPointerheaptid,boolbuilding);
432435
voidHnswUpdateNeighborsOnDisk(Relationindex,HnswSupport*support,HnswElemente,intm,boolcheckExisting,boolbuilding);
433436
voidHnswLoadElementFromTuple(HnswElementelement,HnswElementTupleetup,boolloadHeaptids,boolloadVec);
434437
voidHnswLoadElement(HnswElementelement,double*distance,HnswQuery*q,Relationindex,HnswSupport*support,boolloadVec,double*maxDistance);
435-
boolHnswFormIndexValue(Datum*out,Datum*values,bool*isnull,constHnswTypeInfo*typeInfo,HnswSupport*support);
438+
TupleDescHnswTupleDesc(Relationindex);
439+
boolHnswFormIndexTuple(IndexTuple*out,Datum*values,bool*isnull,constHnswTypeInfo*typeInfo,HnswSupport*support,TupleDesctupdesc);
436440
voidHnswSetElementTuple(char*base,HnswElementTupleetup,HnswElementelement);
437441
voidHnswUpdateConnection(char*base,HnswNeighborArray*neighbors,HnswElementnewElement,floatdistance,intlm,int*updateIdx,Relationindex,HnswSupport*support);
438442
boolHnswLoadNeighborTids(HnswElementelement,ItemPointerData*indextids,Relationindex,intm,intlm,intlc);

‎src/hnswbuild.c

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -476,18 +476,20 @@ InsertTuple(Relation index, Datum *values, bool *isnull, ItemPointer heaptid, Hn
476476
HnswElementelement;
477477
HnswAllocator*allocator=&buildstate->allocator;
478478
HnswSupport*support=&buildstate->support;
479-
SizevalueSize;
480-
PointervaluePtr;
481479
LWLock*flushLock=&graph->flushLock;
482480
char*base=buildstate->hnswarea;
483-
Datumvalue;
481+
TupleDesctupdesc=buildstate->tupdesc;
482+
IndexTupleitup;
483+
SizeitupSize;
484+
IndexTupleitupShared;
485+
boolunused;
484486

485487
/* Form index value */
486-
if (!HnswFormIndexValue(&value,values,isnull,buildstate->typeInfo,support))
488+
if (!HnswFormIndexTuple(&itup,values,isnull,buildstate->typeInfo,support,tupdesc))
487489
return false;
488490

489-
/* Getdatum size */
490-
valueSize=VARSIZE_ANY(DatumGetPointer(value));
491+
/* Gettuple size */
492+
itupSize=IndexTupleSize(itup);
491493

492494
/* Ensure graph not flushed when inserting */
493495
LWLockAcquire(flushLock,LW_SHARED);
@@ -497,7 +499,7 @@ InsertTuple(Relation index, Datum *values, bool *isnull, ItemPointer heaptid, Hn
497499
{
498500
LWLockRelease(flushLock);
499501

500-
returnHnswInsertTupleOnDisk(index,support,value,heaptid, true);
502+
returnHnswInsertTupleOnDisk(index,support,tupdesc,itup,heaptid, true);
501503
}
502504

503505
/*
@@ -529,12 +531,12 @@ InsertTuple(Relation index, Datum *values, bool *isnull, ItemPointer heaptid, Hn
529531

530532
LWLockRelease(flushLock);
531533

532-
returnHnswInsertTupleOnDisk(index,support,value,heaptid, true);
534+
returnHnswInsertTupleOnDisk(index,support,tupdesc,itup,heaptid, true);
533535
}
534536

535537
/* Ok, we can proceed to allocate the element */
536538
element=HnswInitElement(base,heaptid,buildstate->m,buildstate->ml,buildstate->maxLevel,allocator);
537-
valuePtr=HnswAlloc(allocator,valueSize);
539+
itupShared=HnswAlloc(allocator,itupSize);
538540

539541
/*
540542
* We have now allocated the space needed for the element, so we don't
@@ -543,9 +545,10 @@ InsertTuple(Relation index, Datum *values, bool *isnull, ItemPointer heaptid, Hn
543545
*/
544546
LWLockRelease(&graph->allocatorLock);
545547

546-
/* Copy the datum */
547-
memcpy(valuePtr,DatumGetPointer(value),valueSize);
548-
HnswPtrStore(base,element->value,valuePtr);
548+
/* Copy the tuple */
549+
memcpy(itupShared,itup,itupSize);
550+
HnswPtrStore(base,element->itup,itupShared);
551+
HnswPtrStore(base,element->value,DatumGetPointer(index_getattr(itupShared,1,tupdesc,&unused)));
549552

550553
/* Create a lock for the element */
551554
LWLockInitialize(&element->lock,hnsw_lock_tranche_id);
@@ -698,6 +701,7 @@ InitBuildState(HnswBuildState * buildstate, Relation heap, Relation index, Index
698701
buildstate->graph=&buildstate->graphData;
699702
buildstate->ml=HnswGetMl(buildstate->m);
700703
buildstate->maxLevel=HnswGetMaxLevel(buildstate->m);
704+
buildstate->tupdesc=HnswTupleDesc(index);
701705

702706
buildstate->graphCtx=GenerationContextCreate(CurrentMemoryContext,
703707
"Hnsw build graph context",
@@ -722,6 +726,7 @@ InitBuildState(HnswBuildState * buildstate, Relation heap, Relation index, Index
722726
staticvoid
723727
FreeBuildState(HnswBuildState*buildstate)
724728
{
729+
pfree(buildstate->tupdesc);
725730
MemoryContextDelete(buildstate->graphCtx);
726731
MemoryContextDelete(buildstate->tmpCtx);
727732
}

‎src/hnswinsert.c

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -687,14 +687,15 @@ UpdateGraphOnDisk(Relation index, HnswSupport * support, HnswElement element, in
687687
* Insert a tuple into the index
688688
*/
689689
bool
690-
HnswInsertTupleOnDisk(Relationindex,HnswSupport*support,Datumvalue,ItemPointerheaptid,boolbuilding)
690+
HnswInsertTupleOnDisk(Relationindex,HnswSupport*support,TupleDesctupdesc,IndexTupleitup,ItemPointerheaptid,boolbuilding)
691691
{
692692
HnswElemententryPoint;
693693
HnswElementelement;
694694
intm;
695695
intefConstruction=HnswGetEfConstruction(index);
696696
LOCKMODElockmode=ShareLock;
697697
char*base=NULL;
698+
boolunused;
698699

699700
/*
700701
* Get a shared lock. This allows vacuum to ensure no in-flight inserts
@@ -708,7 +709,8 @@ HnswInsertTupleOnDisk(Relation index, HnswSupport * support, Datum value, ItemPo
708709

709710
/* Create an element */
710711
element=HnswInitElement(base,heaptid,m,HnswGetMl(m),HnswGetMaxLevel(m),NULL);
711-
HnswPtrStore(base,element->value,DatumGetPointer(value));
712+
HnswPtrStore(base,element->itup,itup);
713+
HnswPtrStore(base,element->value,DatumGetPointer(index_getattr(itup,1,tupdesc,&unused)));
712714

713715
/* Prevent concurrent inserts when likely updating entry point */
714716
if (entryPoint==NULL||element->level>entryPoint->level)
@@ -742,17 +744,18 @@ HnswInsertTupleOnDisk(Relation index, HnswSupport * support, Datum value, ItemPo
742744
staticvoid
743745
HnswInsertTuple(Relationindex,Datum*values,bool*isnull,ItemPointerheaptid)
744746
{
745-
Datumvalue;
747+
IndexTupleitup;
746748
constHnswTypeInfo*typeInfo=HnswGetTypeInfo(index);
747749
HnswSupportsupport;
750+
TupleDesctupdesc=HnswTupleDesc(index);
748751

749752
HnswInitSupport(&support,index);
750753

751-
/* Form indexvalue */
752-
if (!HnswFormIndexValue(&value,values,isnull,typeInfo,&support))
754+
/* Form indextuple */
755+
if (!HnswFormIndexTuple(&itup,values,isnull,typeInfo,&support,tupdesc))
753756
return;
754757

755-
HnswInsertTupleOnDisk(index,&support,value,heaptid, false);
758+
HnswInsertTupleOnDisk(index,&support,tupdesc,itup,heaptid, false);
756759
}
757760

758761
/*

‎src/hnswutils.c

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -395,10 +395,24 @@ HnswUpdateMetaPage(Relation index, int updateEntry, HnswElement entryPoint, Bloc
395395
}
396396

397397
/*
398-
* Form index value
398+
* Get the tuple descriptor
399+
*/
400+
TupleDesc
401+
HnswTupleDesc(Relationindex)
402+
{
403+
TupleDesctupdesc=CreateTupleDescCopyConstr(RelationGetDescr(index));
404+
405+
/* Prevent compression */
406+
TupleDescAttr(tupdesc,0)->attstorage=TYPSTORAGE_PLAIN;
407+
408+
returntupdesc;
409+
}
410+
411+
/*
412+
* Form index tuple
399413
*/
400414
bool
401-
HnswFormIndexValue(Datum*out,Datum*values,bool*isnull,constHnswTypeInfo*typeInfo,HnswSupport*support)
415+
HnswFormIndexTuple(IndexTuple*out,Datum*values,bool*isnull,constHnswTypeInfo*typeInfo,HnswSupport*support,TupleDesctupdesc)
402416
{
403417
/* Detoast once for all calls */
404418
Datumvalue=PointerGetDatum(PG_DETOAST_DATUM(values[0]));
@@ -416,7 +430,9 @@ HnswFormIndexValue(Datum *out, Datum *values, bool *isnull, const HnswTypeInfo *
416430
value=HnswNormValue(typeInfo,support->collation,value);
417431
}
418432

419-
*out=value;
433+
/* TODO Combine value with values to support multiple attributes */
434+
Assert(tupdesc->natts==1);
435+
*out=index_form_tuple(tupdesc,&value,isnull);
420436

421437
return true;
422438
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp