Skip to content

Commit db72a37

Browse files
HNSW index: update Flatbuffers generated model files
1 parent f4228fd commit db72a37

File tree

4 files changed

+231
-2
lines changed

4 files changed

+231
-2
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2024 ObjectBox Ltd. All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
// automatically generated by the FlatBuffers compiler, do not modify
18+
19+
package io.objectbox.model;
20+
21+
/**
22+
* The distance algorithm used by an HNSW index (vector search).
23+
*/
24+
@SuppressWarnings("unused")
25+
public final class HnswDistanceType {
26+
private HnswDistanceType() { }
27+
/**
28+
* Not a real type, just best practice (e.g. forward compatibility)
29+
*/
30+
public static final short Unknown = 0;
31+
/**
32+
* The default; typically "Euclidean squared" internally.
33+
*/
34+
public static final short Euclidean = 1;
35+
36+
public static final String[] names = { "Unknown", "Euclidean", };
37+
38+
public static String name(int e) { return names[e]; }
39+
}
40+
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright 2024 ObjectBox Ltd. All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
// automatically generated by the FlatBuffers compiler, do not modify
18+
19+
package io.objectbox.model;
20+
21+
/**
22+
* Flags as a part of the HNSW configuration.
23+
*/
24+
@SuppressWarnings("unused")
25+
public final class HnswFlags {
26+
private HnswFlags() { }
27+
/**
28+
* Enables debug logs.
29+
*/
30+
public static final int DebugLogs = 1;
31+
/**
32+
* Enables "high volume" debug logs, e.g. individual gets/puts.
33+
*/
34+
public static final int DebugLogsDetailed = 2;
35+
/**
36+
* Padding for SIMD is enabled by default, which uses more memory but may be faster. This flag turns it off.
37+
*/
38+
public static final int VectorCacheSimdPaddingOff = 4;
39+
/**
40+
* If the speed of removing nodes becomes a concern in your use case, you can speed it up by setting this flag.
41+
* By default, repairing the graph after node removals creates more connections to improve the graph's quality.
42+
* The extra costs for this are relatively low (e.g. vs. regular indexing), and thus the default is recommended.
43+
*/
44+
public static final int ReparationLimitCandidates = 8;
45+
}
46+
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
/*
2+
* Copyright 2024 ObjectBox Ltd. All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
// automatically generated by the FlatBuffers compiler, do not modify
18+
19+
package io.objectbox.model;
20+
21+
import io.objectbox.flatbuffers.BaseVector;
22+
import io.objectbox.flatbuffers.BooleanVector;
23+
import io.objectbox.flatbuffers.ByteVector;
24+
import io.objectbox.flatbuffers.Constants;
25+
import io.objectbox.flatbuffers.DoubleVector;
26+
import io.objectbox.flatbuffers.FlatBufferBuilder;
27+
import io.objectbox.flatbuffers.FloatVector;
28+
import io.objectbox.flatbuffers.IntVector;
29+
import io.objectbox.flatbuffers.LongVector;
30+
import io.objectbox.flatbuffers.ShortVector;
31+
import io.objectbox.flatbuffers.StringVector;
32+
import io.objectbox.flatbuffers.Struct;
33+
import io.objectbox.flatbuffers.Table;
34+
import io.objectbox.flatbuffers.UnionVector;
35+
import java.nio.ByteBuffer;
36+
import java.nio.ByteOrder;
37+
38+
/**
39+
* Parameters to configure HNSW-based approximate nearest neighbor (ANN) search.
40+
* Some of the parameters can influence index construction and searching.
41+
* Changing these values causes re-indexing, which can take a while due to the complex nature of HNSW.
42+
*/
43+
@SuppressWarnings("unused")
44+
public final class HnswParams extends Table {
45+
public static void ValidateVersion() { Constants.FLATBUFFERS_23_5_26(); }
46+
public static HnswParams getRootAsHnswParams(ByteBuffer _bb) { return getRootAsHnswParams(_bb, new HnswParams()); }
47+
public static HnswParams getRootAsHnswParams(ByteBuffer _bb, HnswParams obj) { _bb.order(ByteOrder.LITTLE_ENDIAN); return (obj.__assign(_bb.getInt(_bb.position()) + _bb.position(), _bb)); }
48+
public void __init(int _i, ByteBuffer _bb) { __reset(_i, _bb); }
49+
public HnswParams __assign(int _i, ByteBuffer _bb) { __init(_i, _bb); return this; }
50+
51+
/**
52+
* Dimensions of vectors; vector data with less dimensions are ignored.
53+
* Vectors with more dimensions than specified here are only evaluated up to the given dimension value.
54+
* Changing this value causes re-indexing.
55+
*/
56+
public long dimensions() { int o = __offset(4); return o != 0 ? (long)bb.getInt(o + bb_pos) & 0xFFFFFFFFL : 0L; }
57+
/**
58+
* Aka "M": the max number of connections per node (default: 30).
59+
* Higher numbers increase the graph connectivity, which can lead to more accurate search results.
60+
* However, higher numbers also increase the indexing time and resource usage.
61+
* Try e.g. 16 for faster but less accurate results, or 64 for more accurate results.
62+
* Changing this value causes re-indexing.
63+
*/
64+
public long neighborsPerNode() { int o = __offset(6); return o != 0 ? (long)bb.getInt(o + bb_pos) & 0xFFFFFFFFL : 0L; }
65+
/**
66+
* Aka "efConstruction": the number of neighbor searched for while indexing (default: 100).
67+
* The higher the value, the more accurate the search, but the longer the indexing.
68+
* If indexing time is not a major concern, a value of at least 200 is recommended to improve search quality.
69+
* Changing this value causes re-indexing.
70+
*/
71+
public long indexingSearchCount() { int o = __offset(8); return o != 0 ? (long)bb.getInt(o + bb_pos) & 0xFFFFFFFFL : 0L; }
72+
public long flags() { int o = __offset(10); return o != 0 ? (long)bb.getInt(o + bb_pos) & 0xFFFFFFFFL : 0L; }
73+
/**
74+
* The distance type used for the HNSW index; for now only "Euclidean" is supported.
75+
* Changing this value causes re-indexing.
76+
*/
77+
public int distanceType() { int o = __offset(12); return o != 0 ? bb.getShort(o + bb_pos) & 0xFFFF : 0; }
78+
/**
79+
* When repairing the graph after a node was removed, this gives the probability of adding backlinks to the
80+
* repaired neighbors.
81+
* The default is 1.0 (aka "always") as this should be worth a bit of extra costs as it improves the graph's
82+
* quality.
83+
*/
84+
public float reparationBacklinkProbability() { int o = __offset(14); return o != 0 ? bb.getFloat(o + bb_pos) : 0.0f; }
85+
/**
86+
* A non-binding hint at the maximum size of the vector cache in KB (default: 2097152 or 2 GB/GiB).
87+
* The actual size max cache size may be altered according to device and/or runtime settings.
88+
* The vector cache is used to store vectors in memory to speed up search and indexing.
89+
* Note 1: cache chunks are allocated only on demand, when they are actually used.
90+
* Thus, smaller datasets will use less memory.
91+
* Note 2: the cache is for one specific HNSW index; e.g. each index has its own cache.
92+
* Note 3: the memory consumption can temporarily exceed the cache size,
93+
* e.g. for large changes, it can double due to multi-version transactions.
94+
*/
95+
public long vectorCacheHintSizeKb() { int o = __offset(16); return o != 0 ? bb.getLong(o + bb_pos) : 0L; }
96+
97+
public static int createHnswParams(FlatBufferBuilder builder,
98+
long dimensions,
99+
long neighborsPerNode,
100+
long indexingSearchCount,
101+
long flags,
102+
int distanceType,
103+
float reparationBacklinkProbability,
104+
long vectorCacheHintSizeKb) {
105+
builder.startTable(7);
106+
HnswParams.addVectorCacheHintSizeKb(builder, vectorCacheHintSizeKb);
107+
HnswParams.addReparationBacklinkProbability(builder, reparationBacklinkProbability);
108+
HnswParams.addFlags(builder, flags);
109+
HnswParams.addIndexingSearchCount(builder, indexingSearchCount);
110+
HnswParams.addNeighborsPerNode(builder, neighborsPerNode);
111+
HnswParams.addDimensions(builder, dimensions);
112+
HnswParams.addDistanceType(builder, distanceType);
113+
return HnswParams.endHnswParams(builder);
114+
}
115+
116+
public static void startHnswParams(FlatBufferBuilder builder) { builder.startTable(7); }
117+
public static void addDimensions(FlatBufferBuilder builder, long dimensions) { builder.addInt(0, (int) dimensions, (int) 0L); }
118+
public static void addNeighborsPerNode(FlatBufferBuilder builder, long neighborsPerNode) { builder.addInt(1, (int) neighborsPerNode, (int) 0L); }
119+
public static void addIndexingSearchCount(FlatBufferBuilder builder, long indexingSearchCount) { builder.addInt(2, (int) indexingSearchCount, (int) 0L); }
120+
public static void addFlags(FlatBufferBuilder builder, long flags) { builder.addInt(3, (int) flags, (int) 0L); }
121+
public static void addDistanceType(FlatBufferBuilder builder, int distanceType) { builder.addShort(4, (short) distanceType, (short) 0); }
122+
public static void addReparationBacklinkProbability(FlatBufferBuilder builder, float reparationBacklinkProbability) { builder.addFloat(5, reparationBacklinkProbability, 0.0f); }
123+
public static void addVectorCacheHintSizeKb(FlatBufferBuilder builder, long vectorCacheHintSizeKb) { builder.addLong(6, vectorCacheHintSizeKb, 0L); }
124+
public static int endHnswParams(FlatBufferBuilder builder) {
125+
int o = builder.endTable();
126+
return o;
127+
}
128+
129+
public static final class Vector extends BaseVector {
130+
public Vector __assign(int _vector, int _element_size, ByteBuffer _bb) { __reset(_vector, _element_size, _bb); return this; }
131+
132+
public HnswParams get(int j) { return get(new HnswParams(), j); }
133+
public HnswParams get(HnswParams obj, int j) { return obj.__assign(__indirect(__element(j), bb), bb); }
134+
}
135+
}
136+

objectbox-java/src/main/java/io/objectbox/model/ModelProperty.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2023 ObjectBox Ltd. All rights reserved.
2+
* Copyright 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.
@@ -84,8 +84,14 @@ public final class ModelProperty extends Table {
8484
* For value-based indexes, this defines the maximum length of the value stored for indexing
8585
*/
8686
public long maxIndexValueLength() { int o = __offset(20); return o != 0 ? (long)bb.getInt(o + bb_pos) & 0xFFFFFFFFL : 0L; }
87+
/**
88+
* For float vectors properties and nearest neighbor search, you can index the property with HNSW.
89+
* This is the configuration for the HNSW index, e.g. dimensions and parameters affecting quality/speed tradeoff.
90+
*/
91+
public io.objectbox.model.HnswParams hnswParams() { return hnswParams(new io.objectbox.model.HnswParams()); }
92+
public io.objectbox.model.HnswParams hnswParams(io.objectbox.model.HnswParams obj) { int o = __offset(22); return o != 0 ? obj.__assign(__indirect(o + bb_pos), bb) : null; }
8793

88-
public static void startModelProperty(FlatBufferBuilder builder) { builder.startTable(9); }
94+
public static void startModelProperty(FlatBufferBuilder builder) { builder.startTable(10); }
8995
public static void addId(FlatBufferBuilder builder, int idOffset) { builder.addStruct(0, idOffset, 0); }
9096
public static void addName(FlatBufferBuilder builder, int nameOffset) { builder.addOffset(1, nameOffset, 0); }
9197
public static void addType(FlatBufferBuilder builder, int type) { builder.addShort(2, (short) type, (short) 0); }
@@ -95,6 +101,7 @@ public final class ModelProperty extends Table {
95101
public static void addVirtualTarget(FlatBufferBuilder builder, int virtualTargetOffset) { builder.addOffset(6, virtualTargetOffset, 0); }
96102
public static void addNameSecondary(FlatBufferBuilder builder, int nameSecondaryOffset) { builder.addOffset(7, nameSecondaryOffset, 0); }
97103
public static void addMaxIndexValueLength(FlatBufferBuilder builder, long maxIndexValueLength) { builder.addInt(8, (int) maxIndexValueLength, (int) 0L); }
104+
public static void addHnswParams(FlatBufferBuilder builder, int hnswParamsOffset) { builder.addOffset(9, hnswParamsOffset, 0); }
98105
public static int endModelProperty(FlatBufferBuilder builder) {
99106
int o = builder.endTable();
100107
return o;

0 commit comments

Comments
 (0)