|
1 | 1 | /*
|
2 |
| - * Copyright 2017 ObjectBox Ltd. All rights reserved. |
| 2 | + * Copyright 2017-2024 ObjectBox Ltd. All rights reserved. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
21 | 21 |
|
22 | 22 | import javax.annotation.Nullable;
|
23 | 23 |
|
| 24 | +import io.objectbox.annotation.HnswIndex; |
24 | 25 | import io.objectbox.annotation.apihint.Internal;
|
25 | 26 | import io.objectbox.flatbuffers.FlatBufferBuilder;
|
| 27 | +import io.objectbox.model.HnswDistanceType; |
| 28 | +import io.objectbox.model.HnswFlags; |
| 29 | +import io.objectbox.model.HnswParams; |
26 | 30 | import io.objectbox.model.IdUid;
|
27 | 31 | import io.objectbox.model.Model;
|
28 | 32 | import io.objectbox.model.ModelEntity;
|
@@ -63,6 +67,7 @@ public class PropertyBuilder {
|
63 | 67 | private int indexId;
|
64 | 68 | private long indexUid;
|
65 | 69 | private int indexMaxValueLength;
|
| 70 | + private int hnswParamsOffset; |
66 | 71 |
|
67 | 72 | PropertyBuilder(String name, @Nullable String targetEntityName, @Nullable String virtualTarget, int type) {
|
68 | 73 | this.type = type;
|
@@ -91,6 +96,50 @@ public PropertyBuilder indexMaxValueLength(int indexMaxValueLength) {
|
91 | 96 | return this;
|
92 | 97 | }
|
93 | 98 |
|
| 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 | + |
94 | 143 | public PropertyBuilder flags(int flags) {
|
95 | 144 | checkNotFinished();
|
96 | 145 | this.flags = flags;
|
@@ -134,6 +183,9 @@ public int finish() {
|
134 | 183 | if (indexMaxValueLength > 0) {
|
135 | 184 | ModelProperty.addMaxIndexValueLength(fbb, indexMaxValueLength);
|
136 | 185 | }
|
| 186 | + if (hnswParamsOffset != 0) { |
| 187 | + ModelProperty.addHnswParams(fbb, hnswParamsOffset); |
| 188 | + } |
137 | 189 | ModelProperty.addType(fbb, type);
|
138 | 190 | if (flags != 0) {
|
139 | 191 | ModelProperty.addFlags(fbb, flags);
|
|
0 commit comments