Skip to content

Commit 1208349

Browse files
HNSW index: add model builder API
1 parent c169d6d commit 1208349

File tree

1 file changed

+53
-1
lines changed

1 file changed

+53
-1
lines changed

objectbox-java/src/main/java/io/objectbox/ModelBuilder.java

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017 ObjectBox Ltd. All rights reserved.
2+
* Copyright 2017-2024 ObjectBox Ltd. All rights reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,8 +21,12 @@
2121

2222
import javax.annotation.Nullable;
2323

24+
import io.objectbox.annotation.HnswIndex;
2425
import io.objectbox.annotation.apihint.Internal;
2526
import io.objectbox.flatbuffers.FlatBufferBuilder;
27+
import io.objectbox.model.HnswDistanceType;
28+
import io.objectbox.model.HnswFlags;
29+
import io.objectbox.model.HnswParams;
2630
import io.objectbox.model.IdUid;
2731
import io.objectbox.model.Model;
2832
import io.objectbox.model.ModelEntity;
@@ -63,6 +67,7 @@ public class PropertyBuilder {
6367
private int indexId;
6468
private long indexUid;
6569
private int indexMaxValueLength;
70+
private int hnswParamsOffset;
6671

6772
PropertyBuilder(String name, @Nullable String targetEntityName, @Nullable String virtualTarget, int type) {
6873
this.type = type;
@@ -91,6 +96,50 @@ public PropertyBuilder indexMaxValueLength(int indexMaxValueLength) {
9196
return this;
9297
}
9398

99+
/**
100+
* Set parameters for {@link HnswIndex}.
101+
*
102+
* @param dimensions see {@link HnswIndex#dimensions()}.
103+
* @param neighborsPerNode see {@link HnswIndex#neighborsPerNode()}.
104+
* @param indexingSearchCount see {@link HnswIndex#indexingSearchCount()}.
105+
* @param flags see {@link HnswIndex#flags()}, mapped to {@link HnswFlags}.
106+
* @param distanceType see {@link HnswIndex#distanceType()}, mapped to {@link HnswDistanceType}.
107+
* @param reparationBacklinkProbability see {@link HnswIndex#reparationBacklinkProbability()}.
108+
* @param vectorCacheHintSizeKb see {@link HnswIndex#vectorCacheHintSizeKB()}.
109+
* @return this builder.
110+
*/
111+
public PropertyBuilder hnswParams(long dimensions,
112+
@Nullable Long neighborsPerNode,
113+
@Nullable Long indexingSearchCount,
114+
@Nullable Integer flags,
115+
@Nullable Short distanceType,
116+
@Nullable Float reparationBacklinkProbability,
117+
@Nullable Long vectorCacheHintSizeKb) {
118+
checkNotFinished();
119+
HnswParams.startHnswParams(fbb);
120+
HnswParams.addDimensions(fbb, dimensions);
121+
if (neighborsPerNode != null) {
122+
HnswParams.addNeighborsPerNode(fbb, neighborsPerNode);
123+
}
124+
if (indexingSearchCount != null) {
125+
HnswParams.addIndexingSearchCount(fbb, indexingSearchCount);
126+
}
127+
if (flags != null) {
128+
HnswParams.addFlags(fbb, flags);
129+
}
130+
if (distanceType != null) {
131+
HnswParams.addDistanceType(fbb, distanceType);
132+
}
133+
if (reparationBacklinkProbability != null) {
134+
HnswParams.addReparationBacklinkProbability(fbb, reparationBacklinkProbability);
135+
}
136+
if (vectorCacheHintSizeKb != null) {
137+
HnswParams.addVectorCacheHintSizeKb(fbb, vectorCacheHintSizeKb);
138+
}
139+
hnswParamsOffset = HnswParams.endHnswParams(fbb);
140+
return this;
141+
}
142+
94143
public PropertyBuilder flags(int flags) {
95144
checkNotFinished();
96145
this.flags = flags;
@@ -134,6 +183,9 @@ public int finish() {
134183
if (indexMaxValueLength > 0) {
135184
ModelProperty.addMaxIndexValueLength(fbb, indexMaxValueLength);
136185
}
186+
if (hnswParamsOffset != 0) {
187+
ModelProperty.addHnswParams(fbb, hnswParamsOffset);
188+
}
137189
ModelProperty.addType(fbb, type);
138190
if (flags != 0) {
139191
ModelProperty.addFlags(fbb, flags);

0 commit comments

Comments
 (0)