zhaih commented on code in PR #12657: URL: https://github.com/apache/lucene/pull/12657#discussion_r1355784178
########## lucene/core/src/java/org/apache/lucene/codecs/lucene95/IncrementalHnswGraphMerger.java: ########## @@ -0,0 +1,226 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.lucene.codecs.lucene95; + +import static org.apache.lucene.search.DocIdSetIterator.NO_MORE_DOCS; + +import java.io.IOException; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import org.apache.lucene.codecs.HnswGraphProvider; +import org.apache.lucene.codecs.KnnVectorsReader; +import org.apache.lucene.codecs.KnnVectorsWriter; +import org.apache.lucene.codecs.perfield.PerFieldKnnVectorsFormat; +import org.apache.lucene.index.ByteVectorValues; +import org.apache.lucene.index.FieldInfo; +import org.apache.lucene.index.FloatVectorValues; +import org.apache.lucene.index.MergeState; +import org.apache.lucene.search.DocIdSetIterator; +import org.apache.lucene.util.Bits; +import org.apache.lucene.util.hnsw.HnswGraph; +import org.apache.lucene.util.hnsw.HnswGraphBuilder; +import org.apache.lucene.util.hnsw.InitializedHnswGraphBuilder; +import org.apache.lucene.util.hnsw.RandomVectorScorerSupplier; + +/** + * This selects the biggest Hnsw graph from the provided merge state and initializes a new + * HnswGraphBuilder with that graph as a starting point. + */ +public class IncrementalHnswGraphMerger { + + private final MergeState mergeState; + private final FieldInfo fieldInfo; + + /** + * @param mergeState MergeState containing the readers to merge + * @param fieldInfo FieldInfo for the field being merged + */ + public IncrementalHnswGraphMerger(MergeState mergeState, FieldInfo fieldInfo) { + this.mergeState = mergeState; + this.fieldInfo = fieldInfo; + } + + /** + * Selects the biggest Hnsw graph from the provided merge state and initializes a new + * HnswGraphBuilder with that graph as a starting point. + * + * @param scorerSupplier ScorerSupplier to use for scoring vectors + * @param M The number of connections to allow per node + * @param beamWidth The number of nodes to consider when searching for the nearest neighbor + * @return HnswGraphBuilder initialized with the biggest graph from the merge state + * @throws IOException If an error occurs while reading from the merge state + */ + public HnswGraphBuilder build(RandomVectorScorerSupplier scorerSupplier, int M, int beamWidth) + throws IOException { + // Find the KnnVectorReader with the most docs that meets the following criteria: Review Comment: Can we abstract this part as a separate method as well? Then the overall flow will be more clear I guess: * find init graph * if not found, use default builder * if found use initializer ########## lucene/core/src/java/org/apache/lucene/codecs/lucene95/IncrementalHnswGraphMerger.java: ########## @@ -0,0 +1,226 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.lucene.codecs.lucene95; + +import static org.apache.lucene.search.DocIdSetIterator.NO_MORE_DOCS; + +import java.io.IOException; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import org.apache.lucene.codecs.HnswGraphProvider; +import org.apache.lucene.codecs.KnnVectorsReader; +import org.apache.lucene.codecs.KnnVectorsWriter; +import org.apache.lucene.codecs.perfield.PerFieldKnnVectorsFormat; +import org.apache.lucene.index.ByteVectorValues; +import org.apache.lucene.index.FieldInfo; +import org.apache.lucene.index.FloatVectorValues; +import org.apache.lucene.index.MergeState; +import org.apache.lucene.search.DocIdSetIterator; +import org.apache.lucene.util.Bits; +import org.apache.lucene.util.hnsw.HnswGraph; +import org.apache.lucene.util.hnsw.HnswGraphBuilder; +import org.apache.lucene.util.hnsw.InitializedHnswGraphBuilder; +import org.apache.lucene.util.hnsw.RandomVectorScorerSupplier; + +/** + * This selects the biggest Hnsw graph from the provided merge state and initializes a new + * HnswGraphBuilder with that graph as a starting point. + */ +public class IncrementalHnswGraphMerger { + + private final MergeState mergeState; + private final FieldInfo fieldInfo; + + /** + * @param mergeState MergeState containing the readers to merge + * @param fieldInfo FieldInfo for the field being merged + */ + public IncrementalHnswGraphMerger(MergeState mergeState, FieldInfo fieldInfo) { + this.mergeState = mergeState; + this.fieldInfo = fieldInfo; + } + + /** + * Selects the biggest Hnsw graph from the provided merge state and initializes a new + * HnswGraphBuilder with that graph as a starting point. + * + * @param scorerSupplier ScorerSupplier to use for scoring vectors + * @param M The number of connections to allow per node + * @param beamWidth The number of nodes to consider when searching for the nearest neighbor + * @return HnswGraphBuilder initialized with the biggest graph from the merge state + * @throws IOException If an error occurs while reading from the merge state + */ + public HnswGraphBuilder build(RandomVectorScorerSupplier scorerSupplier, int M, int beamWidth) Review Comment: rename to `builder` or `getBuilder`? ########## lucene/core/src/java/org/apache/lucene/codecs/lucene95/IncrementalHnswGraphMerger.java: ########## @@ -0,0 +1,226 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.lucene.codecs.lucene95; + +import static org.apache.lucene.search.DocIdSetIterator.NO_MORE_DOCS; + +import java.io.IOException; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import org.apache.lucene.codecs.HnswGraphProvider; +import org.apache.lucene.codecs.KnnVectorsReader; +import org.apache.lucene.codecs.KnnVectorsWriter; +import org.apache.lucene.codecs.perfield.PerFieldKnnVectorsFormat; +import org.apache.lucene.index.ByteVectorValues; +import org.apache.lucene.index.FieldInfo; +import org.apache.lucene.index.FloatVectorValues; +import org.apache.lucene.index.MergeState; +import org.apache.lucene.search.DocIdSetIterator; +import org.apache.lucene.util.Bits; +import org.apache.lucene.util.hnsw.HnswGraph; +import org.apache.lucene.util.hnsw.HnswGraphBuilder; +import org.apache.lucene.util.hnsw.InitializedHnswGraphBuilder; +import org.apache.lucene.util.hnsw.RandomVectorScorerSupplier; + +/** + * This selects the biggest Hnsw graph from the provided merge state and initializes a new + * HnswGraphBuilder with that graph as a starting point. + */ +public class IncrementalHnswGraphMerger { + + private final MergeState mergeState; + private final FieldInfo fieldInfo; + + /** + * @param mergeState MergeState containing the readers to merge + * @param fieldInfo FieldInfo for the field being merged + */ + public IncrementalHnswGraphMerger(MergeState mergeState, FieldInfo fieldInfo) { + this.mergeState = mergeState; + this.fieldInfo = fieldInfo; + } + + /** + * Selects the biggest Hnsw graph from the provided merge state and initializes a new + * HnswGraphBuilder with that graph as a starting point. + * + * @param scorerSupplier ScorerSupplier to use for scoring vectors + * @param M The number of connections to allow per node + * @param beamWidth The number of nodes to consider when searching for the nearest neighbor + * @return HnswGraphBuilder initialized with the biggest graph from the merge state + * @throws IOException If an error occurs while reading from the merge state + */ + public HnswGraphBuilder build(RandomVectorScorerSupplier scorerSupplier, int M, int beamWidth) Review Comment: This method seems can be a static method if we make the two member fields part of the parameter as this is the only public method? But I guess we're expecting more different mergers (like a concurrent one?) such that you make the class look like this? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org For additional commands, e-mail: issues-h...@lucene.apache.org