Skip to content

Commit 2da69a8

Browse files
committed
Use abstract classes
1 parent 1d0c731 commit 2da69a8

File tree

3 files changed

+24
-91
lines changed

3 files changed

+24
-91
lines changed

server/src/main/java/org/elasticsearch/index/codec/vectors/es92/ES92BFloat16FlatVectorsFormat.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
*/
2020
package org.elasticsearch.index.codec.vectors.es92;
2121

22-
import org.apache.lucene.codecs.hnsw.FlatVectorsFormat;
2322
import org.apache.lucene.codecs.hnsw.FlatVectorsReader;
2423
import org.apache.lucene.codecs.hnsw.FlatVectorsScorer;
2524
import org.apache.lucene.codecs.hnsw.FlatVectorsWriter;
@@ -29,15 +28,15 @@
2928
import org.apache.lucene.store.IOContext;
3029
import org.apache.lucene.store.MergeInfo;
3130
import org.elasticsearch.common.util.set.Sets;
31+
import org.elasticsearch.index.codec.vectors.AbstractFlatVectorsFormat;
3232
import org.elasticsearch.index.codec.vectors.MergeReaderWrapper;
3333
import org.elasticsearch.index.codec.vectors.es818.DirectIOHint;
34-
import org.elasticsearch.index.codec.vectors.es818.ES818BinaryQuantizedVectorsFormat;
3534
import org.elasticsearch.index.store.FsDirectoryFactory;
3635

3736
import java.io.IOException;
3837
import java.util.Set;
3938

40-
public final class ES92BFloat16FlatVectorsFormat extends FlatVectorsFormat {
39+
public final class ES92BFloat16FlatVectorsFormat extends AbstractFlatVectorsFormat {
4140

4241
static final String NAME = "ES92BFloat16FlatVectorsFormat";
4342
static final String META_CODEC_NAME = "ES92BFloat16FlatVectorsFormatMeta";
@@ -56,13 +55,18 @@ public ES92BFloat16FlatVectorsFormat(FlatVectorsScorer vectorsScorer) {
5655
this.vectorsScorer = vectorsScorer;
5756
}
5857

58+
@Override
59+
protected FlatVectorsScorer flatVectorsScorer() {
60+
return vectorsScorer;
61+
}
62+
5963
@Override
6064
public FlatVectorsWriter fieldsWriter(SegmentWriteState state) throws IOException {
6165
return new ES92BFloat16FlatVectorsWriter(state, vectorsScorer);
6266
}
6367

6468
static boolean shouldUseDirectIO(SegmentReadState state) {
65-
return ES818BinaryQuantizedVectorsFormat.USE_DIRECT_IO && FsDirectoryFactory.isHybridFs(state.directory);
69+
return USE_DIRECT_IO && FsDirectoryFactory.isHybridFs(state.directory);
6670
}
6771

6872
@Override
@@ -87,11 +91,6 @@ public FlatVectorsReader fieldsReader(SegmentReadState state) throws IOException
8791
}
8892
}
8993

90-
@Override
91-
public String toString() {
92-
return "ES92BFloat16FlatVectorsFormat(" + "vectorsScorer=" + vectorsScorer + ')';
93-
}
94-
9594
static class DirectIOContext implements IOContext {
9695

9796
final Set<FileOpenHint> hints;

server/src/main/java/org/elasticsearch/index/codec/vectors/es92/ES92BinaryQuantizedBFloat16VectorsFormat.java

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@
2222
import org.apache.lucene.codecs.hnsw.FlatVectorScorerUtil;
2323
import org.apache.lucene.codecs.hnsw.FlatVectorsFormat;
2424
import org.apache.lucene.codecs.hnsw.FlatVectorsReader;
25+
import org.apache.lucene.codecs.hnsw.FlatVectorsScorer;
2526
import org.apache.lucene.codecs.hnsw.FlatVectorsWriter;
2627
import org.apache.lucene.index.SegmentReadState;
2728
import org.apache.lucene.index.SegmentWriteState;
29+
import org.elasticsearch.index.codec.vectors.AbstractFlatVectorsFormat;
2830
import org.elasticsearch.index.codec.vectors.OptimizedScalarQuantizer;
2931
import org.elasticsearch.index.codec.vectors.es818.ES818BinaryFlatVectorsScorer;
3032
import org.elasticsearch.index.codec.vectors.es818.ES818BinaryQuantizedVectorsReader;
@@ -87,19 +89,10 @@
8789
* <li>The sparse vector information, if required, mapping vector ordinal to doc ID
8890
* </ul>
8991
*/
90-
public class ES92BinaryQuantizedBFloat16VectorsFormat extends FlatVectorsFormat {
92+
public class ES92BinaryQuantizedBFloat16VectorsFormat extends AbstractFlatVectorsFormat {
9193

92-
public static final String BINARIZED_VECTOR_COMPONENT = "BVEC";
9394
public static final String NAME = "ES92BinaryQuantizedBFloat16VectorsFormat";
9495

95-
static final int VERSION_START = 0;
96-
static final int VERSION_CURRENT = VERSION_START;
97-
static final String META_CODEC_NAME = "ES92BinaryQuantizedBFloat16VectorsFormatMeta";
98-
static final String VECTOR_DATA_CODEC_NAME = "ES92BinaryQuantizedBFloat16VectorsFormatData";
99-
static final String META_EXTENSION = "vemb";
100-
static final String VECTOR_DATA_EXTENSION = "veb";
101-
static final int DIRECT_MONOTONIC_BLOCK_SHIFT = 16;
102-
10396
private static final FlatVectorsFormat rawVectorFormat = new ES92BFloat16FlatVectorsFormat(
10497
FlatVectorScorerUtil.getLucene99FlatVectorsScorer()
10598
);
@@ -113,6 +106,11 @@ public ES92BinaryQuantizedBFloat16VectorsFormat() {
113106
super(NAME);
114107
}
115108

109+
@Override
110+
protected FlatVectorsScorer flatVectorsScorer() {
111+
return scorer;
112+
}
113+
116114
@Override
117115
public FlatVectorsWriter fieldsWriter(SegmentWriteState state) throws IOException {
118116
return new ES818BinaryQuantizedVectorsWriter(scorer, rawVectorFormat.fieldsWriter(state), state);
@@ -122,14 +120,4 @@ public FlatVectorsWriter fieldsWriter(SegmentWriteState state) throws IOExceptio
122120
public FlatVectorsReader fieldsReader(SegmentReadState state) throws IOException {
123121
return new ES818BinaryQuantizedVectorsReader(state, rawVectorFormat.fieldsReader(state), scorer);
124122
}
125-
126-
@Override
127-
public int getMaxDimensions(String fieldName) {
128-
return MAX_DIMS_COUNT;
129-
}
130-
131-
@Override
132-
public String toString() {
133-
return "ES92BinaryQuantizedBFloat16VectorsFormat(name=" + NAME + ", flatVectorScorer=" + scorer + ")";
134-
}
135123
}

server/src/main/java/org/elasticsearch/index/codec/vectors/es92/ES92HnswBinaryQuantizedBFloat16VectorsFormat.java

Lines changed: 8 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -19,54 +19,32 @@
1919
*/
2020
package org.elasticsearch.index.codec.vectors.es92;
2121

22-
import org.apache.lucene.codecs.KnnVectorsFormat;
2322
import org.apache.lucene.codecs.KnnVectorsReader;
2423
import org.apache.lucene.codecs.KnnVectorsWriter;
2524
import org.apache.lucene.codecs.hnsw.FlatVectorsFormat;
26-
import org.apache.lucene.codecs.lucene99.Lucene99HnswVectorsFormat;
2725
import org.apache.lucene.codecs.lucene99.Lucene99HnswVectorsReader;
2826
import org.apache.lucene.codecs.lucene99.Lucene99HnswVectorsWriter;
2927
import org.apache.lucene.index.SegmentReadState;
3028
import org.apache.lucene.index.SegmentWriteState;
31-
import org.apache.lucene.search.TaskExecutor;
32-
import org.apache.lucene.util.hnsw.HnswGraph;
29+
import org.elasticsearch.index.codec.vectors.AbstractHnswVectorsFormat;
3330

3431
import java.io.IOException;
3532
import java.util.concurrent.ExecutorService;
3633

3734
import static org.apache.lucene.codecs.lucene99.Lucene99HnswVectorsFormat.DEFAULT_BEAM_WIDTH;
3835
import static org.apache.lucene.codecs.lucene99.Lucene99HnswVectorsFormat.DEFAULT_MAX_CONN;
3936
import static org.apache.lucene.codecs.lucene99.Lucene99HnswVectorsFormat.DEFAULT_NUM_MERGE_WORKER;
40-
import static org.apache.lucene.codecs.lucene99.Lucene99HnswVectorsFormat.MAXIMUM_BEAM_WIDTH;
41-
import static org.apache.lucene.codecs.lucene99.Lucene99HnswVectorsFormat.MAXIMUM_MAX_CONN;
42-
import static org.elasticsearch.index.mapper.vectors.DenseVectorFieldMapper.MAX_DIMS_COUNT;
4337

4438
/**
4539
* Copied from Lucene, replace with Lucene's implementation sometime after Lucene 10
4640
*/
47-
public class ES92HnswBinaryQuantizedBFloat16VectorsFormat extends KnnVectorsFormat {
41+
public class ES92HnswBinaryQuantizedBFloat16VectorsFormat extends AbstractHnswVectorsFormat {
4842

4943
public static final String NAME = "ES92HnswBinaryQuantizedBFloat16VectorsFormat";
5044

51-
/**
52-
* Controls how many of the nearest neighbor candidates are connected to the new node. Defaults to
53-
* {@link Lucene99HnswVectorsFormat#DEFAULT_MAX_CONN}. See {@link HnswGraph} for more details.
54-
*/
55-
private final int maxConn;
56-
57-
/**
58-
* The number of candidate neighbors to track while searching the graph for each newly inserted
59-
* node. Defaults to {@link Lucene99HnswVectorsFormat#DEFAULT_BEAM_WIDTH}. See {@link HnswGraph}
60-
* for details.
61-
*/
62-
private final int beamWidth;
63-
6445
/** The format for storing, reading, merging vectors on disk */
6546
private static final FlatVectorsFormat flatVectorsFormat = new ES92BinaryQuantizedBFloat16VectorsFormat();
6647

67-
private final int numMergeWorkers;
68-
private final TaskExecutor mergeExec;
69-
7048
/** Constructs a format using default graph construction parameters */
7149
public ES92HnswBinaryQuantizedBFloat16VectorsFormat() {
7250
this(DEFAULT_MAX_CONN, DEFAULT_BEAM_WIDTH, DEFAULT_NUM_MERGE_WORKER, null);
@@ -93,28 +71,12 @@ public ES92HnswBinaryQuantizedBFloat16VectorsFormat(int maxConn, int beamWidth)
9371
* generated by this format to do the merge
9472
*/
9573
public ES92HnswBinaryQuantizedBFloat16VectorsFormat(int maxConn, int beamWidth, int numMergeWorkers, ExecutorService mergeExec) {
96-
super(NAME);
97-
if (maxConn <= 0 || maxConn > MAXIMUM_MAX_CONN) {
98-
throw new IllegalArgumentException(
99-
"maxConn must be positive and less than or equal to " + MAXIMUM_MAX_CONN + "; maxConn=" + maxConn
100-
);
101-
}
102-
if (beamWidth <= 0 || beamWidth > MAXIMUM_BEAM_WIDTH) {
103-
throw new IllegalArgumentException(
104-
"beamWidth must be positive and less than or equal to " + MAXIMUM_BEAM_WIDTH + "; beamWidth=" + beamWidth
105-
);
106-
}
107-
this.maxConn = maxConn;
108-
this.beamWidth = beamWidth;
109-
if (numMergeWorkers == 1 && mergeExec != null) {
110-
throw new IllegalArgumentException("No executor service is needed as we'll use single thread to merge");
111-
}
112-
this.numMergeWorkers = numMergeWorkers;
113-
if (mergeExec != null) {
114-
this.mergeExec = new TaskExecutor(mergeExec);
115-
} else {
116-
this.mergeExec = null;
117-
}
74+
super(NAME, maxConn, beamWidth, numMergeWorkers, mergeExec);
75+
}
76+
77+
@Override
78+
protected FlatVectorsFormat flatVectorsFormat() {
79+
return flatVectorsFormat;
11880
}
11981

12082
@Override
@@ -126,20 +88,4 @@ public KnnVectorsWriter fieldsWriter(SegmentWriteState state) throws IOException
12688
public KnnVectorsReader fieldsReader(SegmentReadState state) throws IOException {
12789
return new Lucene99HnswVectorsReader(state, flatVectorsFormat.fieldsReader(state));
12890
}
129-
130-
@Override
131-
public int getMaxDimensions(String fieldName) {
132-
return MAX_DIMS_COUNT;
133-
}
134-
135-
@Override
136-
public String toString() {
137-
return "ES92HnswBinaryQuantizedBFloat16VectorsFormat(name=ES92HnswBinaryQuantizedBFloat16VectorsFormat, maxConn="
138-
+ maxConn
139-
+ ", beamWidth="
140-
+ beamWidth
141-
+ ", flatVectorFormat="
142-
+ flatVectorsFormat
143-
+ ")";
144-
}
14591
}

0 commit comments

Comments
 (0)