-
Notifications
You must be signed in to change notification settings - Fork 143
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix bytes offset bug and duplicate readers and add uTs for derived source #2494
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
|
||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.log4j.Log4j2; | ||
import org.apache.lucene.codecs.DocValuesProducer; | ||
import org.apache.lucene.codecs.FieldsProducer; | ||
import org.apache.lucene.codecs.KnnVectorsReader; | ||
|
@@ -23,7 +24,8 @@ | |
*/ | ||
@RequiredArgsConstructor | ||
@Getter | ||
public class DerivedSourceReaders implements Closeable { | ||
@Log4j2 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [nit-pick] you are not using Log4j2 in the code |
||
public class DerivedSourceReaders implements Cloneable, Closeable { | ||
@Nullable | ||
private final KnnVectorsReader knnVectorsReader; | ||
@Nullable | ||
|
@@ -33,8 +35,17 @@ public class DerivedSourceReaders implements Closeable { | |
@Nullable | ||
private final NormsProducer normsProducer; | ||
|
||
private final boolean isCloned; | ||
|
||
@Override | ||
public void close() throws IOException { | ||
IOUtils.close(knnVectorsReader, docValuesProducer, fieldsProducer, normsProducer); | ||
if (isCloned == false) { | ||
IOUtils.close(knnVectorsReader, docValuesProducer, fieldsProducer, normsProducer); | ||
} | ||
} | ||
|
||
@Override | ||
public DerivedSourceReaders clone() { | ||
return new DerivedSourceReaders(knnVectorsReader, docValuesProducer, fieldsProducer, normsProducer, true); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
...ava/org/opensearch/knn/index/codec/KNN9120Codec/DerivedSourceStoredFieldsWriterTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.knn.index.codec.KNN9120Codec; | ||
|
||
import lombok.SneakyThrows; | ||
import org.apache.lucene.codecs.StoredFieldsWriter; | ||
import org.apache.lucene.index.FieldInfo; | ||
import org.apache.lucene.util.BytesRef; | ||
import org.opensearch.common.io.stream.BytesStreamOutput; | ||
import org.opensearch.core.xcontent.MediaTypeRegistry; | ||
import org.opensearch.core.xcontent.XContentBuilder; | ||
import org.opensearch.knn.KNNTestCase; | ||
import org.opensearch.knn.index.codec.KNNCodecTestUtil; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import static org.mockito.Mockito.mock; | ||
|
||
public class DerivedSourceStoredFieldsWriterTests extends KNNTestCase { | ||
|
||
@SneakyThrows | ||
public void testWriteField() { | ||
StoredFieldsWriter delegate = mock(StoredFieldsWriter.class); | ||
FieldInfo fieldInfo = KNNCodecTestUtil.FieldInfoBuilder.builder("_source").build(); | ||
List<String> fields = List.of("test"); | ||
|
||
DerivedSourceStoredFieldsWriter derivedSourceStoredFieldsWriter = new DerivedSourceStoredFieldsWriter(delegate, fields); | ||
|
||
Map<String, Object> source = Map.of("test", new float[] { 1.0f, 2.0f, 3.0f }, "text_field", "text_value"); | ||
BytesStreamOutput bStream = new BytesStreamOutput(); | ||
XContentBuilder builder = MediaTypeRegistry.contentBuilder(MediaTypeRegistry.JSON, bStream).map(source); | ||
builder.close(); | ||
byte[] originalBytes = bStream.bytes().toBytesRef().bytes; | ||
byte[] shiftedBytes = new byte[originalBytes.length + 2]; | ||
System.arraycopy(originalBytes, 0, shiftedBytes, 1, originalBytes.length); | ||
derivedSourceStoredFieldsWriter.writeField(fieldInfo, new BytesRef(shiftedBytes, 1, originalBytes.length)); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
...va/org/opensearch/knn/index/codec/derivedsource/DerivedSourceStoredFieldVisitorTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.knn.index.codec.derivedsource; | ||
|
||
import org.apache.lucene.index.StoredFieldVisitor; | ||
import org.opensearch.knn.KNNTestCase; | ||
import org.opensearch.knn.index.codec.KNNCodecTestUtil; | ||
|
||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.ArgumentMatchers.anyInt; | ||
import static org.mockito.Mockito.doAnswer; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.times; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
public class DerivedSourceStoredFieldVisitorTests extends KNNTestCase { | ||
|
||
public void testBinaryField() throws Exception { | ||
StoredFieldVisitor delegate = mock(StoredFieldVisitor.class); | ||
doAnswer(invocationOnMock -> null).when(delegate).binaryField(any(), any()); | ||
DerivedSourceVectorInjector derivedSourceVectorInjector = mock(DerivedSourceVectorInjector.class); | ||
when(derivedSourceVectorInjector.injectVectors(anyInt(), any())).thenReturn(new byte[0]); | ||
DerivedSourceStoredFieldVisitor derivedSourceStoredFieldVisitor = new DerivedSourceStoredFieldVisitor( | ||
delegate, | ||
0, | ||
derivedSourceVectorInjector | ||
); | ||
|
||
// When field is not _source, then do not call the injector | ||
derivedSourceStoredFieldVisitor.binaryField(KNNCodecTestUtil.FieldInfoBuilder.builder("test").build(), null); | ||
verify(derivedSourceVectorInjector, times(0)).injectVectors(anyInt(), any()); | ||
verify(delegate, times(1)).binaryField(any(), any()); | ||
|
||
// When field is not _source, then do call the injector | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: When the field is _source, then do call the injector |
||
derivedSourceStoredFieldVisitor.binaryField(KNNCodecTestUtil.FieldInfoBuilder.builder("_source").build(), null); | ||
verify(derivedSourceVectorInjector, times(1)).injectVectors(anyInt(), any()); | ||
verify(delegate, times(2)).binaryField(any(), any()); | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we add a UT for this case?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
makes sense - added and validated fix before and after