From 284eb2af1ab106cdf2fa066364ce3afdf95eac6e Mon Sep 17 00:00:00 2001 From: John Mazanec Date: Thu, 17 Mar 2022 15:57:56 -0700 Subject: [PATCH] Spotless applied Signed-off-by: John Mazanec --- .../codec/KNN80Codec/KNN80CompoundFormat.java | 7 +- .../KNN80Codec/KNN80DocValuesConsumer.java | 75 +++++---- .../index/codec/KNN87Codec/KNN87Codec.java | 2 +- .../index/codec/util/BinaryDocValuesSub.java | 2 +- .../org/opensearch/knn/plugin/KNNPlugin.java | 89 +++++----- .../KNN80Codec/KNN80CompoundFormatTests.java | 17 +- .../KNN80DocValuesConsumerTests.java | 154 ++++++++---------- .../knn/index/codec/KNNCodecTestCase.java | 67 ++++---- .../knn/index/codec/KNNCodecTestUtil.java | 62 +++++-- .../codec/util/KNNVectorSerializerTests.java | 30 ++-- 10 files changed, 259 insertions(+), 246 deletions(-) diff --git a/src/main/java/org/opensearch/knn/index/codec/KNN80Codec/KNN80CompoundFormat.java b/src/main/java/org/opensearch/knn/index/codec/KNN80Codec/KNN80CompoundFormat.java index a5ad1e2e4..8a8ed558e 100644 --- a/src/main/java/org/opensearch/knn/index/codec/KNN80Codec/KNN80CompoundFormat.java +++ b/src/main/java/org/opensearch/knn/index/codec/KNN80Codec/KNN80CompoundFormat.java @@ -26,7 +26,6 @@ public class KNN80CompoundFormat extends CompoundFormat { private final CompoundFormat delegate; - public KNN80CompoundFormat() { this.delegate = new Lucene50CompoundFormat(); } @@ -53,14 +52,12 @@ public void write(Directory dir, SegmentInfo si, IOContext context) throws IOExc delegate.write(dir, si, context); } - private void writeEngineFiles(Directory dir, SegmentInfo si, IOContext context, String engineExtension) - throws IOException { + private void writeEngineFiles(Directory dir, SegmentInfo si, IOContext context, String engineExtension) throws IOException { /* * If engine file present, remove it from the compounding file list to avoid header/footer checks * and create a new compounding file format with extension engine + c. */ - Set engineFiles = si.files().stream().filter(file -> file.endsWith(engineExtension)) - .collect(Collectors.toSet()); + Set engineFiles = si.files().stream().filter(file -> file.endsWith(engineExtension)).collect(Collectors.toSet()); Set segmentFiles = new HashSet<>(si.files()); diff --git a/src/main/java/org/opensearch/knn/index/codec/KNN80Codec/KNN80DocValuesConsumer.java b/src/main/java/org/opensearch/knn/index/codec/KNN80Codec/KNN80DocValuesConsumer.java index d0b64b5a5..0bbd6e9ac 100644 --- a/src/main/java/org/opensearch/knn/index/codec/KNN80Codec/KNN80DocValuesConsumer.java +++ b/src/main/java/org/opensearch/knn/index/codec/KNN80Codec/KNN80DocValuesConsumer.java @@ -97,10 +97,14 @@ public void addKNNBinaryField(FieldInfo field, DocValuesProducer valuesProducer) KNNEngine knnEngine = model.getModelMetadata().getKnnEngine(); - engineFileName = buildEngineFileName(state.segmentInfo.name, knnEngine.getLatestBuildVersion(), - field.name, knnEngine.getExtension()); - indexPath = Paths.get(((FSDirectory) (FilterDirectory.unwrap(state.directory))).getDirectory().toString(), - engineFileName).toString(); + engineFileName = buildEngineFileName( + state.segmentInfo.name, + knnEngine.getLatestBuildVersion(), + field.name, + knnEngine.getExtension() + ); + indexPath = Paths.get(((FSDirectory) (FilterDirectory.unwrap(state.directory))).getDirectory().toString(), engineFileName) + .toString(); tmpEngineFileName = engineFileName + TEMP_SUFFIX; String tempIndexPath = indexPath + TEMP_SUFFIX; @@ -115,10 +119,14 @@ public void addKNNBinaryField(FieldInfo field, DocValuesProducer valuesProducer) String engineName = field.attributes().getOrDefault(KNNConstants.KNN_ENGINE, KNNEngine.DEFAULT.getName()); KNNEngine knnEngine = KNNEngine.getEngine(engineName); - engineFileName = buildEngineFileName(state.segmentInfo.name, knnEngine.getLatestBuildVersion(), - field.name, knnEngine.getExtension()); - indexPath = Paths.get(((FSDirectory) (FilterDirectory.unwrap(state.directory))).getDirectory().toString(), - engineFileName).toString(); + engineFileName = buildEngineFileName( + state.segmentInfo.name, + knnEngine.getLatestBuildVersion(), + field.name, + knnEngine.getExtension() + ); + indexPath = Paths.get(((FSDirectory) (FilterDirectory.unwrap(state.directory))).getDirectory().toString(), engineFileName) + .toString(); tmpEngineFileName = engineFileName + TEMP_SUFFIX; String tempIndexPath = indexPath + TEMP_SUFFIX; @@ -134,10 +142,12 @@ public void addKNNBinaryField(FieldInfo field, DocValuesProducer valuesProducer) * existing file will miss calculating checksum for the serialized graph * bytes and result in index corruption issues. */ - //TODO: I think this can be refactored to avoid this copy and then write + // TODO: I think this can be refactored to avoid this copy and then write // https://github.com/opendistro-for-elasticsearch/k-NN/issues/330 - try (IndexInput is = state.directory.openInput(tmpEngineFileName, state.context); - IndexOutput os = state.directory.createOutput(engineFileName, state.context)) { + try ( + IndexInput is = state.directory.openInput(tmpEngineFileName, state.context); + IndexOutput os = state.directory.createOutput(engineFileName, state.context) + ) { os.copyBytes(is, is.length()); CodecUtil.writeFooter(os); } catch (Exception ex) { @@ -148,29 +158,26 @@ public void addKNNBinaryField(FieldInfo field, DocValuesProducer valuesProducer) } } - private void createKNNIndexFromTemplate(byte[] model, KNNCodecUtil.Pair pair, KNNEngine knnEngine, - String indexPath) { - Map parameters = ImmutableMap.of(KNNConstants.INDEX_THREAD_QTY, KNNSettings.state().getSettingValue( - KNNSettings.KNN_ALGO_PARAM_INDEX_THREAD_QTY)); - AccessController.doPrivileged( - (PrivilegedAction) () -> { - JNIService.createIndexFromTemplate(pair.docs, pair.vectors, indexPath, model, parameters, - knnEngine.getName()); - return null; - } + private void createKNNIndexFromTemplate(byte[] model, KNNCodecUtil.Pair pair, KNNEngine knnEngine, String indexPath) { + Map parameters = ImmutableMap.of( + KNNConstants.INDEX_THREAD_QTY, + KNNSettings.state().getSettingValue(KNNSettings.KNN_ALGO_PARAM_INDEX_THREAD_QTY) ); + AccessController.doPrivileged((PrivilegedAction) () -> { + JNIService.createIndexFromTemplate(pair.docs, pair.vectors, indexPath, model, parameters, knnEngine.getName()); + return null; + }); } - private void createKNNIndexFromScratch(FieldInfo fieldInfo, KNNCodecUtil.Pair pair, KNNEngine knnEngine, - String indexPath) throws IOException { + private void createKNNIndexFromScratch(FieldInfo fieldInfo, KNNCodecUtil.Pair pair, KNNEngine knnEngine, String indexPath) + throws IOException { Map parameters = new HashMap<>(); Map fieldAttributes = fieldInfo.attributes(); String parametersString = fieldAttributes.get(KNNConstants.PARAMETERS); // parametersString will be null when legacy mapper is used if (parametersString == null) { - parameters.put(KNNConstants.SPACE_TYPE, fieldAttributes.getOrDefault(KNNConstants.SPACE_TYPE, - SpaceType.DEFAULT.getValue())); + parameters.put(KNNConstants.SPACE_TYPE, fieldAttributes.getOrDefault(KNNConstants.SPACE_TYPE, SpaceType.DEFAULT.getValue())); String efConstruction = fieldAttributes.get(KNNConstants.HNSW_ALGO_EF_CONSTRUCTION); Map algoParams = new HashMap<>(); @@ -185,22 +192,20 @@ private void createKNNIndexFromScratch(FieldInfo fieldInfo, KNNCodecUtil.Pair pa parameters.put(PARAMETERS, algoParams); } else { parameters.putAll( - XContentFactory.xContent(XContentType.JSON).createParser(NamedXContentRegistry.EMPTY, - DeprecationHandler.THROW_UNSUPPORTED_OPERATION, parametersString).map() + XContentFactory.xContent(XContentType.JSON) + .createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, parametersString) + .map() ); } // Used to determine how many threads to use when indexing - parameters.put(KNNConstants.INDEX_THREAD_QTY, KNNSettings.state().getSettingValue( - KNNSettings.KNN_ALGO_PARAM_INDEX_THREAD_QTY)); + parameters.put(KNNConstants.INDEX_THREAD_QTY, KNNSettings.state().getSettingValue(KNNSettings.KNN_ALGO_PARAM_INDEX_THREAD_QTY)); // Pass the path for the nms library to save the file - AccessController.doPrivileged( - (PrivilegedAction) () -> { - JNIService.createIndex(pair.docs, pair.vectors, indexPath, parameters, knnEngine.getName()); - return null; - } - ); + AccessController.doPrivileged((PrivilegedAction) () -> { + JNIService.createIndex(pair.docs, pair.vectors, indexPath, parameters, knnEngine.getName()); + return null; + }); } /** diff --git a/src/main/java/org/opensearch/knn/index/codec/KNN87Codec/KNN87Codec.java b/src/main/java/org/opensearch/knn/index/codec/KNN87Codec/KNN87Codec.java index 31d1546c3..6ec5ec05c 100644 --- a/src/main/java/org/opensearch/knn/index/codec/KNN87Codec/KNN87Codec.java +++ b/src/main/java/org/opensearch/knn/index/codec/KNN87Codec/KNN87Codec.java @@ -41,7 +41,7 @@ public KNN87Codec(Codec delegate) { super(KNN_87, delegate); // Note that DocValuesFormat can use old Codec's DocValuesFormat. For instance Lucene84 uses Lucene80 // DocValuesFormat. Refer to defaultDVFormat in LuceneXXCodec.java to find out which version it uses - this.docValuesFormat = new KNN80DocValuesFormat(delegate.docValuesFormat()); + this.docValuesFormat = new KNN80DocValuesFormat(delegate.docValuesFormat()); this.compoundFormat = new KNN80CompoundFormat(delegate.compoundFormat()); } diff --git a/src/main/java/org/opensearch/knn/index/codec/util/BinaryDocValuesSub.java b/src/main/java/org/opensearch/knn/index/codec/util/BinaryDocValuesSub.java index 4e4ec34d6..c47aa85d3 100644 --- a/src/main/java/org/opensearch/knn/index/codec/util/BinaryDocValuesSub.java +++ b/src/main/java/org/opensearch/knn/index/codec/util/BinaryDocValuesSub.java @@ -35,4 +35,4 @@ public int nextDoc() throws IOException { public BinaryDocValues getValues() { return values; } -} \ No newline at end of file +} diff --git a/src/main/java/org/opensearch/knn/plugin/KNNPlugin.java b/src/main/java/org/opensearch/knn/plugin/KNNPlugin.java index fc9560c09..9cf0696f2 100644 --- a/src/main/java/org/opensearch/knn/plugin/KNNPlugin.java +++ b/src/main/java/org/opensearch/knn/plugin/KNNPlugin.java @@ -139,8 +139,10 @@ public class KNNPlugin extends Plugin implements MapperPlugin, SearchPlugin, Act @Override public Map getMappers() { - return Collections.singletonMap(KNNVectorFieldMapper.CONTENT_TYPE, new KNNVectorFieldMapper.TypeParser( - ModelDao.OpenSearchKNNModelDao::getInstance)); + return Collections.singletonMap( + KNNVectorFieldMapper.CONTENT_TYPE, + new KNNVectorFieldMapper.TypeParser(ModelDao.OpenSearchKNNModelDao::getInstance) + ); } @Override @@ -149,12 +151,19 @@ public List> getQueries() { } @Override - public Collection createComponents(Client client, ClusterService clusterService, ThreadPool threadPool, - ResourceWatcherService resourceWatcherService, ScriptService scriptService, - NamedXContentRegistry xContentRegistry, Environment environment, - NodeEnvironment nodeEnvironment, NamedWriteableRegistry namedWriteableRegistry, - IndexNameExpressionResolver indexNameExpressionResolver, - Supplier repositoriesServiceSupplier) { + public Collection createComponents( + Client client, + ClusterService clusterService, + ThreadPool threadPool, + ResourceWatcherService resourceWatcherService, + ScriptService scriptService, + NamedXContentRegistry xContentRegistry, + Environment environment, + NodeEnvironment nodeEnvironment, + NamedWriteableRegistry namedWriteableRegistry, + IndexNameExpressionResolver indexNameExpressionResolver, + Supplier repositoriesServiceSupplier + ) { this.clusterService = clusterService; // Initialize Native Memory loading strategies @@ -179,25 +188,35 @@ public List> getSettings() { return KNNSettings.state().getSettings(); } - public List getRestHandlers(Settings settings, - RestController restController, - ClusterSettings clusterSettings, - IndexScopedSettings indexScopedSettings, - SettingsFilter settingsFilter, - IndexNameExpressionResolver indexNameExpressionResolver, - Supplier nodesInCluster) { + public List getRestHandlers( + Settings settings, + RestController restController, + ClusterSettings clusterSettings, + IndexScopedSettings indexScopedSettings, + SettingsFilter settingsFilter, + IndexNameExpressionResolver indexNameExpressionResolver, + Supplier nodesInCluster + ) { RestKNNStatsHandler restKNNStatsHandler = new RestKNNStatsHandler(settings, restController, knnStats); - RestKNNWarmupHandler restKNNWarmupHandler = new RestKNNWarmupHandler(settings, restController, clusterService, - indexNameExpressionResolver); + RestKNNWarmupHandler restKNNWarmupHandler = new RestKNNWarmupHandler( + settings, + restController, + clusterService, + indexNameExpressionResolver + ); RestGetModelHandler restGetModelHandler = new RestGetModelHandler(); RestDeleteModelHandler restDeleteModelHandler = new RestDeleteModelHandler(); RestTrainModelHandler restTrainModelHandler = new RestTrainModelHandler(); RestSearchModelHandler restSearchModelHandler = new RestSearchModelHandler(); return ImmutableList.of( - restKNNStatsHandler, restKNNWarmupHandler, restGetModelHandler, restDeleteModelHandler, - restTrainModelHandler, restSearchModelHandler + restKNNStatsHandler, + restKNNWarmupHandler, + restGetModelHandler, + restDeleteModelHandler, + restTrainModelHandler, + restSearchModelHandler ); } @@ -207,17 +226,16 @@ public List getRestHandlers(Settings settings, @Override public List> getActions() { return Arrays.asList( - new ActionHandler<>(KNNStatsAction.INSTANCE, KNNStatsTransportAction.class), - new ActionHandler<>(KNNWarmupAction.INSTANCE, KNNWarmupTransportAction.class), - new ActionHandler<>(UpdateModelMetadataAction.INSTANCE, UpdateModelMetadataTransportAction.class), - new ActionHandler<>(TrainingJobRouteDecisionInfoAction.INSTANCE, - TrainingJobRouteDecisionInfoTransportAction.class), - new ActionHandler<>(GetModelAction.INSTANCE, GetModelTransportAction.class), - new ActionHandler<>(DeleteModelAction.INSTANCE, DeleteModelTransportAction.class), - new ActionHandler<>(TrainingJobRouterAction.INSTANCE, TrainingJobRouterTransportAction.class), - new ActionHandler<>(TrainingModelAction.INSTANCE, TrainingModelTransportAction.class), - new ActionHandler<>(RemoveModelFromCacheAction.INSTANCE, RemoveModelFromCacheTransportAction.class), - new ActionHandler<>(SearchModelAction.INSTANCE, SearchModelTransportAction.class) + new ActionHandler<>(KNNStatsAction.INSTANCE, KNNStatsTransportAction.class), + new ActionHandler<>(KNNWarmupAction.INSTANCE, KNNWarmupTransportAction.class), + new ActionHandler<>(UpdateModelMetadataAction.INSTANCE, UpdateModelMetadataTransportAction.class), + new ActionHandler<>(TrainingJobRouteDecisionInfoAction.INSTANCE, TrainingJobRouteDecisionInfoTransportAction.class), + new ActionHandler<>(GetModelAction.INSTANCE, GetModelTransportAction.class), + new ActionHandler<>(DeleteModelAction.INSTANCE, DeleteModelTransportAction.class), + new ActionHandler<>(TrainingJobRouterAction.INSTANCE, TrainingJobRouterTransportAction.class), + new ActionHandler<>(TrainingModelAction.INSTANCE, TrainingModelTransportAction.class), + new ActionHandler<>(RemoveModelFromCacheAction.INSTANCE, RemoveModelFromCacheTransportAction.class), + new ActionHandler<>(SearchModelAction.INSTANCE, SearchModelTransportAction.class) ); } @@ -273,15 +291,6 @@ public ScriptEngine getScriptEngine(Settings settings, Collection> getExecutorBuilders(Settings settings) { - return ImmutableList.of( - new FixedExecutorBuilder( - settings, - TRAIN_THREAD_POOL, - 1, - 1, - KNN_THREAD_POOL_PREFIX, - false - ) - ); + return ImmutableList.of(new FixedExecutorBuilder(settings, TRAIN_THREAD_POOL, 1, 1, KNN_THREAD_POOL_PREFIX, false)); } } diff --git a/src/test/java/org/opensearch/knn/index/codec/KNN80Codec/KNN80CompoundFormatTests.java b/src/test/java/org/opensearch/knn/index/codec/KNN80Codec/KNN80CompoundFormatTests.java index d7db12427..a2f18cdfb 100644 --- a/src/test/java/org/opensearch/knn/index/codec/KNN80Codec/KNN80CompoundFormatTests.java +++ b/src/test/java/org/opensearch/knn/index/codec/KNN80Codec/KNN80CompoundFormatTests.java @@ -30,7 +30,6 @@ public class KNN80CompoundFormatTests extends KNNTestCase { - private static Directory directory; private static Codec codec; @@ -45,7 +44,6 @@ public static void closeStaticVariables() throws IOException { directory.close(); } - public void testGetCompoundReader() throws IOException { CompoundDirectory dir = mock(CompoundDirectory.class); CompoundFormat delegate = mock(CompoundFormat.class); @@ -59,16 +57,15 @@ public void testWrite() throws IOException { String segmentName = "_test"; Set segmentFiles = Sets.newHashSet( - String.format("%s_nmslib1%s", segmentName, KNNEngine.NMSLIB.getExtension()), - String.format("%s_nmslib2%s", segmentName, KNNEngine.NMSLIB.getExtension()), - String.format("%s_nmslib3%s", segmentName, KNNEngine.NMSLIB.getExtension()), - String.format("%s_faiss1%s", segmentName, KNNEngine.FAISS.getExtension()), - String.format("%s_faiss2%s", segmentName, KNNEngine.FAISS.getExtension()), - String.format("%s_faiss3%s", segmentName, KNNEngine.FAISS.getExtension()) + String.format("%s_nmslib1%s", segmentName, KNNEngine.NMSLIB.getExtension()), + String.format("%s_nmslib2%s", segmentName, KNNEngine.NMSLIB.getExtension()), + String.format("%s_nmslib3%s", segmentName, KNNEngine.NMSLIB.getExtension()), + String.format("%s_faiss1%s", segmentName, KNNEngine.FAISS.getExtension()), + String.format("%s_faiss2%s", segmentName, KNNEngine.FAISS.getExtension()), + String.format("%s_faiss3%s", segmentName, KNNEngine.FAISS.getExtension()) ); - SegmentInfo segmentInfo = KNNCodecTestUtil.SegmentInfoBuilder.builder(directory, segmentName, - segmentFiles.size(), codec).build(); + SegmentInfo segmentInfo = KNNCodecTestUtil.SegmentInfoBuilder.builder(directory, segmentName, segmentFiles.size(), codec).build(); for (String name : segmentFiles) { IndexOutput indexOutput = directory.createOutput(name, IOContext.DEFAULT); diff --git a/src/test/java/org/opensearch/knn/index/codec/KNN80Codec/KNN80DocValuesConsumerTests.java b/src/test/java/org/opensearch/knn/index/codec/KNN80Codec/KNN80DocValuesConsumerTests.java index 756e5c31c..b632b2f3f 100644 --- a/src/test/java/org/opensearch/knn/index/codec/KNN80Codec/KNN80DocValuesConsumerTests.java +++ b/src/test/java/org/opensearch/knn/index/codec/KNN80Codec/KNN80DocValuesConsumerTests.java @@ -81,14 +81,14 @@ public static void closeStaticVariables() throws IOException { public void testAddBinaryField_withKNN() throws IOException { // Confirm that addKNNBinaryField will get called if the k-NN parameter is true FieldInfo fieldInfo = KNNCodecTestUtil.FieldInfoBuilder.builder("test-field") - .addAttribute(KNNVectorFieldMapper.KNN_FIELD, "true") - .build(); + .addAttribute(KNNVectorFieldMapper.KNN_FIELD, "true") + .build(); DocValuesProducer docValuesProducer = mock(DocValuesProducer.class); DocValuesConsumer delegate = mock(DocValuesConsumer.class); doNothing().when(delegate).addBinaryField(fieldInfo, docValuesProducer); - final boolean[] called = {false}; + final boolean[] called = { false }; KNN80DocValuesConsumer knn80DocValuesConsumer = new KNN80DocValuesConsumer(delegate, null) { @Override @@ -112,7 +112,7 @@ public void testAddBinaryField_withoutKNN() throws IOException { DocValuesConsumer delegate = mock(DocValuesConsumer.class); doNothing().when(delegate).addBinaryField(fieldInfo, docValuesProducer); - final boolean[] called = {false}; + final boolean[] called = { false }; KNN80DocValuesConsumer knn80DocValuesConsumer = new KNN80DocValuesConsumer(delegate, null) { @Override @@ -146,33 +146,23 @@ public void testAddKNNBinaryField_fromScratch_nmslibCurrent() throws IOException SpaceType spaceType = SpaceType.COSINESIMIL; int dimension = 16; - SegmentInfo segmentInfo = KNNCodecTestUtil.SegmentInfoBuilder - .builder(directory, segmentName, docsInSegment, codec) - .build(); + SegmentInfo segmentInfo = KNNCodecTestUtil.SegmentInfoBuilder.builder(directory, segmentName, docsInSegment, codec).build(); KNNMethodContext knnMethodContext = new KNNMethodContext( - knnEngine, - spaceType, - new MethodComponentContext( - METHOD_HNSW, - ImmutableMap.of( - METHOD_PARAMETER_M, 16, - METHOD_PARAMETER_EF_CONSTRUCTION, 512 - ) - ) + knnEngine, + spaceType, + new MethodComponentContext(METHOD_HNSW, ImmutableMap.of(METHOD_PARAMETER_M, 16, METHOD_PARAMETER_EF_CONSTRUCTION, 512)) ); - String parameterString = Strings.toString(XContentFactory.jsonBuilder() - .map(knnEngine.getMethodAsMap(knnMethodContext))); + String parameterString = Strings.toString(XContentFactory.jsonBuilder().map(knnEngine.getMethodAsMap(knnMethodContext))); FieldInfo[] fieldInfoArray = new FieldInfo[] { - KNNCodecTestUtil.FieldInfoBuilder.builder(fieldName) - .addAttribute(KNNVectorFieldMapper.KNN_FIELD, "true") - .addAttribute(KNNConstants.KNN_ENGINE, knnEngine.getName()) - .addAttribute(KNNConstants.SPACE_TYPE, spaceType.getValue()) - .addAttribute(KNNConstants.PARAMETERS, parameterString) - .build() - }; + KNNCodecTestUtil.FieldInfoBuilder.builder(fieldName) + .addAttribute(KNNVectorFieldMapper.KNN_FIELD, "true") + .addAttribute(KNNConstants.KNN_ENGINE, knnEngine.getName()) + .addAttribute(KNNConstants.SPACE_TYPE, spaceType.getValue()) + .addAttribute(KNNConstants.PARAMETERS, parameterString) + .build() }; FieldInfos fieldInfos = new FieldInfos(fieldInfoArray); SegmentWriteState state = new SegmentWriteState(null, directory, segmentInfo, fieldInfos, null, IOContext.DEFAULT); @@ -183,8 +173,12 @@ public void testAddKNNBinaryField_fromScratch_nmslibCurrent() throws IOException knn80DocValuesConsumer.addKNNBinaryField(fieldInfoArray[0], randomVectorDocValuesProducer); // The document should be created in the correct location - String expectedFile = KNNCodecUtil.buildEngineFileName(segmentName, knnEngine.getLatestBuildVersion(), - fieldName, knnEngine.getExtension()); + String expectedFile = KNNCodecUtil.buildEngineFileName( + segmentName, + knnEngine.getLatestBuildVersion(), + fieldName, + knnEngine.getExtension() + ); assertFileInCorrectLocation(state, expectedFile); // The footer should be valid @@ -204,18 +198,15 @@ public void testAddKNNBinaryField_fromScratch_nmslibLegacy() throws IOException SpaceType spaceType = SpaceType.COSINESIMIL; int dimension = 16; - SegmentInfo segmentInfo = KNNCodecTestUtil.SegmentInfoBuilder - .builder(directory, segmentName, docsInSegment, codec) - .build(); + SegmentInfo segmentInfo = KNNCodecTestUtil.SegmentInfoBuilder.builder(directory, segmentName, docsInSegment, codec).build(); FieldInfo[] fieldInfoArray = new FieldInfo[] { - KNNCodecTestUtil.FieldInfoBuilder.builder(fieldName) - .addAttribute(KNNVectorFieldMapper.KNN_FIELD, "true") - .addAttribute(KNNConstants.HNSW_ALGO_EF_CONSTRUCTION, "512") - .addAttribute(KNNConstants.HNSW_ALGO_M, "16") - .addAttribute(KNNConstants.SPACE_TYPE, spaceType.getValue()) - .build() - }; + KNNCodecTestUtil.FieldInfoBuilder.builder(fieldName) + .addAttribute(KNNVectorFieldMapper.KNN_FIELD, "true") + .addAttribute(KNNConstants.HNSW_ALGO_EF_CONSTRUCTION, "512") + .addAttribute(KNNConstants.HNSW_ALGO_M, "16") + .addAttribute(KNNConstants.SPACE_TYPE, spaceType.getValue()) + .build() }; FieldInfos fieldInfos = new FieldInfos(fieldInfoArray); SegmentWriteState state = new SegmentWriteState(null, directory, segmentInfo, fieldInfos, null, IOContext.DEFAULT); @@ -226,8 +217,12 @@ public void testAddKNNBinaryField_fromScratch_nmslibLegacy() throws IOException knn80DocValuesConsumer.addKNNBinaryField(fieldInfoArray[0], randomVectorDocValuesProducer); // The document should be created in the correct location - String expectedFile = KNNCodecUtil.buildEngineFileName(segmentName, knnEngine.getLatestBuildVersion(), - fieldName, knnEngine.getExtension()); + String expectedFile = KNNCodecUtil.buildEngineFileName( + segmentName, + knnEngine.getLatestBuildVersion(), + fieldName, + knnEngine.getExtension() + ); assertFileInCorrectLocation(state, expectedFile); // The footer should be valid @@ -246,33 +241,23 @@ public void testAddKNNBinaryField_fromScratch_faissCurrent() throws IOException SpaceType spaceType = SpaceType.INNER_PRODUCT; int dimension = 16; - SegmentInfo segmentInfo = KNNCodecTestUtil.SegmentInfoBuilder - .builder(directory, segmentName, docsInSegment, codec) - .build(); + SegmentInfo segmentInfo = KNNCodecTestUtil.SegmentInfoBuilder.builder(directory, segmentName, docsInSegment, codec).build(); KNNMethodContext knnMethodContext = new KNNMethodContext( - knnEngine, - spaceType, - new MethodComponentContext( - METHOD_HNSW, - ImmutableMap.of( - METHOD_PARAMETER_M, 16, - METHOD_PARAMETER_EF_CONSTRUCTION, 512 - ) - ) + knnEngine, + spaceType, + new MethodComponentContext(METHOD_HNSW, ImmutableMap.of(METHOD_PARAMETER_M, 16, METHOD_PARAMETER_EF_CONSTRUCTION, 512)) ); - String parameterString = Strings.toString(XContentFactory.jsonBuilder() - .map(knnEngine.getMethodAsMap(knnMethodContext))); + String parameterString = Strings.toString(XContentFactory.jsonBuilder().map(knnEngine.getMethodAsMap(knnMethodContext))); FieldInfo[] fieldInfoArray = new FieldInfo[] { - KNNCodecTestUtil.FieldInfoBuilder.builder(fieldName) - .addAttribute(KNNVectorFieldMapper.KNN_FIELD, "true") - .addAttribute(KNNConstants.KNN_ENGINE, knnEngine.getName()) - .addAttribute(KNNConstants.SPACE_TYPE, spaceType.getValue()) - .addAttribute(KNNConstants.PARAMETERS, parameterString) - .build() - }; + KNNCodecTestUtil.FieldInfoBuilder.builder(fieldName) + .addAttribute(KNNVectorFieldMapper.KNN_FIELD, "true") + .addAttribute(KNNConstants.KNN_ENGINE, knnEngine.getName()) + .addAttribute(KNNConstants.SPACE_TYPE, spaceType.getValue()) + .addAttribute(KNNConstants.PARAMETERS, parameterString) + .build() }; FieldInfos fieldInfos = new FieldInfos(fieldInfoArray); SegmentWriteState state = new SegmentWriteState(null, directory, segmentInfo, fieldInfos, null, IOContext.DEFAULT); @@ -283,8 +268,12 @@ public void testAddKNNBinaryField_fromScratch_faissCurrent() throws IOException knn80DocValuesConsumer.addKNNBinaryField(fieldInfoArray[0], randomVectorDocValuesProducer); // The document should be created in the correct location - String expectedFile = KNNCodecUtil.buildEngineFileName(segmentName, knnEngine.getLatestBuildVersion(), - fieldName, knnEngine.getExtension()); + String expectedFile = KNNCodecUtil.buildEngineFileName( + segmentName, + knnEngine.getLatestBuildVersion(), + fieldName, + knnEngine.getExtension() + ); assertFileInCorrectLocation(state, expectedFile); // The footer should be valid @@ -305,16 +294,17 @@ public void testAddKNNBinaryField_fromModel_faiss() throws IOException, Executio long trainingPtr = JNIService.transferVectors(0, trainingData); Map parameters = ImmutableMap.of( - INDEX_DESCRIPTION_PARAMETER, "IVF4,Flat", - KNNConstants.SPACE_TYPE, SpaceType.L2.getValue() + INDEX_DESCRIPTION_PARAMETER, + "IVF4,Flat", + KNNConstants.SPACE_TYPE, + SpaceType.L2.getValue() ); byte[] modelBytes = JNIService.trainIndex(parameters, dimension, trainingPtr, knnEngine.getName()); Model model = new Model( - new ModelMetadata(knnEngine, spaceType, dimension, ModelState.CREATED, "timestamp", - "Empty description", ""), - modelBytes, - modelId + new ModelMetadata(knnEngine, spaceType, dimension, ModelState.CREATED, "timestamp", "Empty description", ""), + modelBytes, + modelId ); JNIService.freeVectors(trainingPtr); @@ -325,10 +315,8 @@ public void testAddKNNBinaryField_fromModel_faiss() throws IOException, Executio when(clusterService.getSettings()).thenReturn(Settings.EMPTY); ClusterSettings clusterSettings = new ClusterSettings( - Settings.builder() - .put(MODEL_CACHE_SIZE_LIMIT_SETTING.getKey(), "10kb") - .build(), - ImmutableSet.of(MODEL_CACHE_SIZE_LIMIT_SETTING) + Settings.builder().put(MODEL_CACHE_SIZE_LIMIT_SETTING.getKey(), "10kb").build(), + ImmutableSet.of(MODEL_CACHE_SIZE_LIMIT_SETTING) ); when(clusterService.getClusterSettings()).thenReturn(clusterSettings); @@ -339,17 +327,13 @@ public void testAddKNNBinaryField_fromModel_faiss() throws IOException, Executio int docsInSegment = 100; String fieldName = String.format("test_field%s", randomAlphaOfLength(4)); - SegmentInfo segmentInfo = KNNCodecTestUtil.SegmentInfoBuilder - .builder(directory, segmentName, docsInSegment, codec) - .build(); - + SegmentInfo segmentInfo = KNNCodecTestUtil.SegmentInfoBuilder.builder(directory, segmentName, docsInSegment, codec).build(); FieldInfo[] fieldInfoArray = new FieldInfo[] { - KNNCodecTestUtil.FieldInfoBuilder.builder(fieldName) - .addAttribute(KNNVectorFieldMapper.KNN_FIELD, "true") - .addAttribute(MODEL_ID, modelId) - .build() - }; + KNNCodecTestUtil.FieldInfoBuilder.builder(fieldName) + .addAttribute(KNNVectorFieldMapper.KNN_FIELD, "true") + .addAttribute(MODEL_ID, modelId) + .build() }; FieldInfos fieldInfos = new FieldInfos(fieldInfoArray); SegmentWriteState state = new SegmentWriteState(null, directory, segmentInfo, fieldInfos, null, IOContext.DEFAULT); @@ -360,8 +344,12 @@ public void testAddKNNBinaryField_fromModel_faiss() throws IOException, Executio knn80DocValuesConsumer.addKNNBinaryField(fieldInfoArray[0], randomVectorDocValuesProducer); // The document should be created in the correct location - String expectedFile = KNNCodecUtil.buildEngineFileName(segmentName, knnEngine.getLatestBuildVersion(), - fieldName, knnEngine.getExtension()); + String expectedFile = KNNCodecUtil.buildEngineFileName( + segmentName, + knnEngine.getLatestBuildVersion(), + fieldName, + knnEngine.getExtension() + ); assertFileInCorrectLocation(state, expectedFile); // The footer should be valid diff --git a/src/test/java/org/opensearch/knn/index/codec/KNNCodecTestCase.java b/src/test/java/org/opensearch/knn/index/codec/KNNCodecTestCase.java index 6d8f16a5a..3454c7d52 100644 --- a/src/test/java/org/opensearch/knn/index/codec/KNNCodecTestCase.java +++ b/src/test/java/org/opensearch/knn/index/codec/KNNCodecTestCase.java @@ -20,20 +20,14 @@ import org.opensearch.knn.index.VectorField; import org.opensearch.knn.index.codec.KNN87Codec.KNN87Codec; import org.apache.lucene.codecs.Codec; -import org.apache.lucene.codecs.CodecUtil; import org.apache.lucene.document.Document; import org.apache.lucene.document.FieldType; -import org.apache.lucene.index.FilterLeafReader; import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.IndexWriterConfig; -import org.apache.lucene.index.LeafReaderContext; import org.apache.lucene.index.RandomIndexWriter; -import org.apache.lucene.index.SegmentReader; import org.apache.lucene.index.SerialMergeScheduler; import org.apache.lucene.search.IndexSearcher; -import org.apache.lucene.store.ChecksumIndexInput; import org.apache.lucene.store.Directory; -import org.apache.lucene.store.IOContext; import org.opensearch.cluster.service.ClusterService; import org.opensearch.common.settings.Settings; import org.opensearch.knn.index.memory.NativeMemoryLoadStrategy; @@ -65,7 +59,7 @@ /** * Test used for testing Codecs */ -public class KNNCodecTestCase extends KNNTestCase { +public class KNNCodecTestCase extends KNNTestCase { private static FieldType sampleFieldType; static { @@ -86,13 +80,8 @@ protected void setUpMockClusterService() { } protected ResourceWatcherService createDisabledResourceWatcherService() { - final Settings settings = Settings.builder() - .put("resource.reload.enabled", false) - .build(); - return new ResourceWatcherService( - settings, - null - ); + final Settings settings = Settings.builder().put("resource.reload.enabled", false).build(); + return new ResourceWatcherService(settings, null); } public void testMultiFieldsKnnIndex(Codec codec) throws Exception { @@ -105,7 +94,7 @@ public void testMultiFieldsKnnIndex(Codec codec) throws Exception { /** * Add doc with field "test_vector" */ - float[] array = {1.0f, 3.0f, 4.0f}; + float[] array = { 1.0f, 3.0f, 4.0f }; VectorField vectorField = new VectorField("test_vector", array, sampleFieldType); RandomIndexWriter writer = new RandomIndexWriter(random(), dir, iwc); Document doc = new Document(); @@ -120,7 +109,7 @@ public void testMultiFieldsKnnIndex(Codec codec) throws Exception { iwc1.setMergeScheduler(new SerialMergeScheduler()); iwc1.setCodec(new KNN87Codec()); writer = new RandomIndexWriter(random(), dir, iwc1); - float[] array1 = {6.0f, 14.0f}; + float[] array1 = { 6.0f, 14.0f }; VectorField vectorField1 = new VectorField("my_vector", array1, sampleFieldType); Document doc1 = new Document(); doc1.add(vectorField1); @@ -138,14 +127,14 @@ public void testMultiFieldsKnnIndex(Codec codec) throws Exception { // query to verify distance for each of the field IndexSearcher searcher = new IndexSearcher(reader); - float score = searcher.search(new KNNQuery("test_vector", new float[] {1.0f, 0.0f, 0.0f}, 1, "dummy"), 10).scoreDocs[0].score; - float score1 = searcher.search(new KNNQuery("my_vector", new float[] {1.0f, 2.0f}, 1, "dummy"), 10).scoreDocs[0].score; - assertEquals(1.0f/(1 + 25), score, 0.01f); - assertEquals(1.0f/(1 + 169), score1, 0.01f); + float score = searcher.search(new KNNQuery("test_vector", new float[] { 1.0f, 0.0f, 0.0f }, 1, "dummy"), 10).scoreDocs[0].score; + float score1 = searcher.search(new KNNQuery("my_vector", new float[] { 1.0f, 2.0f }, 1, "dummy"), 10).scoreDocs[0].score; + assertEquals(1.0f / (1 + 25), score, 0.01f); + assertEquals(1.0f / (1 + 169), score1, 0.01f); // query to determine the hits - assertEquals(1, searcher.count(new KNNQuery("test_vector", new float[] {1.0f, 0.0f, 0.0f}, 1, "dummy"))); - assertEquals(1, searcher.count(new KNNQuery("my_vector", new float[] {1.0f, 1.0f}, 1, "dummy"))); + assertEquals(1, searcher.count(new KNNQuery("test_vector", new float[] { 1.0f, 0.0f, 0.0f }, 1, "dummy"))); + assertEquals(1, searcher.count(new KNNQuery("my_vector", new float[] { 1.0f, 1.0f }, 1, "dummy"))); reader.close(); dir.close(); @@ -162,25 +151,33 @@ public void testBuildFromModelTemplate(Codec codec) throws IOException, Executio // "Train" a faiss flat index - this really just creates an empty index that does brute force k-NN long vectorsPointer = JNIService.transferVectors(0, new float[0][0]); - byte [] modelBlob = JNIService.trainIndex(ImmutableMap.of( - INDEX_DESCRIPTION_PARAMETER, "Flat", - SPACE_TYPE, spaceType.getValue()), dimension, vectorsPointer, - KNNEngine.FAISS.getName()); + byte[] modelBlob = JNIService.trainIndex( + ImmutableMap.of(INDEX_DESCRIPTION_PARAMETER, "Flat", SPACE_TYPE, spaceType.getValue()), + dimension, + vectorsPointer, + KNNEngine.FAISS.getName() + ); // Setup model cache ModelDao modelDao = mock(ModelDao.class); // Set model state to created - ModelMetadata modelMetadata1 = new ModelMetadata(knnEngine, spaceType, dimension, ModelState.CREATED, - ZonedDateTime.now(ZoneOffset.UTC).toString(), "", ""); + ModelMetadata modelMetadata1 = new ModelMetadata( + knnEngine, + spaceType, + dimension, + ModelState.CREATED, + ZonedDateTime.now(ZoneOffset.UTC).toString(), + "", + "" + ); Model mockModel = new Model(modelMetadata1, modelBlob, modelId); when(modelDao.get(modelId)).thenReturn(mockModel); when(modelDao.getMetadata(modelId)).thenReturn(modelMetadata1); Settings settings = settings(CURRENT).put(MODEL_CACHE_SIZE_LIMIT_SETTING.getKey(), "10%").build(); - ClusterSettings clusterSettings = new ClusterSettings(settings, - ImmutableSet.of(MODEL_CACHE_SIZE_LIMIT_SETTING)); + ClusterSettings clusterSettings = new ClusterSettings(settings, ImmutableSet.of(MODEL_CACHE_SIZE_LIMIT_SETTING)); ClusterService clusterService = mock(ClusterService.class); when(clusterService.getSettings()).thenReturn(settings); @@ -201,12 +198,7 @@ public void testBuildFromModelTemplate(Codec codec) throws IOException, Executio fieldType.freeze(); // Add the documents to the index - float[][] arrays = { - {1.0f, 3.0f, 4.0f}, - {2.0f, 5.0f, 8.0f}, - {3.0f, 6.0f, 9.0f}, - {4.0f, 7.0f, 10.0f} - }; + float[][] arrays = { { 1.0f, 3.0f, 4.0f }, { 2.0f, 5.0f, 8.0f }, { 3.0f, 6.0f, 9.0f }, { 4.0f, 7.0f, 10.0f } }; RandomIndexWriter writer = new RandomIndexWriter(random(), dir, iwc); String fieldName = "test_vector"; @@ -224,7 +216,7 @@ public void testBuildFromModelTemplate(Codec codec) throws IOException, Executio KNNWeight.initialize(modelDao); ResourceWatcherService resourceWatcherService = createDisabledResourceWatcherService(); NativeMemoryLoadStrategy.IndexLoadStrategy.initialize(resourceWatcherService); - float [] query = {10.0f, 10.0f, 10.0f}; + float[] query = { 10.0f, 10.0f, 10.0f }; IndexSearcher searcher = new IndexSearcher(reader); TopDocs topDocs = searcher.search(new KNNQuery(fieldName, query, 4, "dummy"), 10); @@ -239,4 +231,3 @@ public void testBuildFromModelTemplate(Codec codec) throws IOException, Executio NativeMemoryLoadStrategy.IndexLoadStrategy.getInstance().close(); } } - diff --git a/src/test/java/org/opensearch/knn/index/codec/KNNCodecTestUtil.java b/src/test/java/org/opensearch/knn/index/codec/KNNCodecTestUtil.java index c9560cb89..de335a115 100644 --- a/src/test/java/org/opensearch/knn/index/codec/KNNCodecTestUtil.java +++ b/src/test/java/org/opensearch/knn/index/codec/KNNCodecTestUtil.java @@ -52,7 +52,7 @@ public class KNNCodecTestUtil { // Utility class to help build SegmentInfo with reasonable defaults - public static class SegmentInfoBuilder { + public static class SegmentInfoBuilder { private final Directory directory; private final String segmentName; @@ -66,8 +66,7 @@ public static class SegmentInfoBuilder { private final Map attributes; private Sort indexSort; - public static SegmentInfoBuilder builder(Directory directory, String segmentName, int docsInSegment, - Codec codec) { + public static SegmentInfoBuilder builder(Directory directory, String segmentName, int docsInSegment, Codec codec) { return new SegmentInfoBuilder(directory, segmentName, docsInSegment, codec); } @@ -116,8 +115,19 @@ public SegmentInfoBuilder indexSort(Sort indexSort) { } public SegmentInfo build() { - return new SegmentInfo(directory, version, minVersion, segmentName, docsInSegment, - isCompoundFile, codec, Collections.emptyMap(), segmentId, attributes, indexSort); + return new SegmentInfo( + directory, + version, + minVersion, + segmentName, + docsInSegment, + isCompoundFile, + codec, + Collections.emptyMap(), + segmentId, + attributes, + indexSort + ); } } @@ -172,7 +182,6 @@ public FieldInfoBuilder omitNorms(boolean omitNorms) { return this; } - public FieldInfoBuilder storePayloads(boolean storePayloads) { this.storePayloads = storePayloads; return this; @@ -202,10 +211,12 @@ public FieldInfoBuilder pointDimensionCount(int pointDimensionCount) { this.pointDimensionCount = pointDimensionCount; return this; } + public FieldInfoBuilder pointIndexDimensionCount(int pointIndexDimensionCount) { this.pointIndexDimensionCount = pointIndexDimensionCount; return this; } + public FieldInfoBuilder pointNumBytes(int pointNumBytes) { this.pointNumBytes = pointNumBytes; return this; @@ -217,9 +228,21 @@ public FieldInfoBuilder softDeletes(boolean softDeletes) { } public FieldInfo build() { - return new FieldInfo(fieldName, fieldNumber, storeTermVector, omitNorms, storePayloads, indexOptions, - docValuesType, dvGen, attributes, pointDimensionCount, pointIndexDimensionCount, pointNumBytes, - softDeletes); + return new FieldInfo( + fieldName, + fieldNumber, + storeTermVector, + omitNorms, + storePayloads, + indexOptions, + docValuesType, + dvGen, + attributes, + pointDimensionCount, + pointIndexDimensionCount, + pointNumBytes, + softDeletes + ); } } @@ -359,13 +382,20 @@ public static void assertValidFooter(Directory dir, String filename) throws IOEx indexInput.close(); } - public static void assertLoadableByEngine(SegmentWriteState state, String fileName, KNNEngine knnEngine, - SpaceType spaceType, int dimension) { - String filePath = Paths.get(((FSDirectory) (FilterDirectory.unwrap(state.directory))).getDirectory() - .toString(), fileName).toString(); - long indexPtr = JNIService.loadIndex(filePath, Maps.newHashMap(ImmutableMap.of( - SPACE_TYPE, spaceType.getValue() - )), knnEngine.getName()); + public static void assertLoadableByEngine( + SegmentWriteState state, + String fileName, + KNNEngine knnEngine, + SpaceType spaceType, + int dimension + ) { + String filePath = Paths.get(((FSDirectory) (FilterDirectory.unwrap(state.directory))).getDirectory().toString(), fileName) + .toString(); + long indexPtr = JNIService.loadIndex( + filePath, + Maps.newHashMap(ImmutableMap.of(SPACE_TYPE, spaceType.getValue())), + knnEngine.getName() + ); int k = 2; float[] queryVector = new float[dimension]; KNNQueryResult[] results = JNIService.queryIndex(indexPtr, queryVector, k, knnEngine.getName()); diff --git a/src/test/java/org/opensearch/knn/index/codec/util/KNNVectorSerializerTests.java b/src/test/java/org/opensearch/knn/index/codec/util/KNNVectorSerializerTests.java index 67664f750..1d08df6a0 100644 --- a/src/test/java/org/opensearch/knn/index/codec/util/KNNVectorSerializerTests.java +++ b/src/test/java/org/opensearch/knn/index/codec/util/KNNVectorSerializerTests.java @@ -6,9 +6,6 @@ package org.opensearch.knn.index.codec.util; import org.opensearch.knn.KNNTestCase; -import org.opensearch.knn.index.codec.util.KNNVectorSerializer; -import org.opensearch.knn.index.codec.util.KNNVectorSerializerFactory; -import org.opensearch.knn.index.codec.util.SerializationMode; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; @@ -22,8 +19,8 @@ public class KNNVectorSerializerTests extends KNNTestCase { Random random = new Random(); public void testVectorSerializerFactory() throws Exception { - //check that default serializer can work with array of floats - //setup + // check that default serializer can work with array of floats + // setup final float[] vector = getArrayOfRandomFloats(20); final ByteArrayOutputStream bas = new ByteArrayOutputStream(); final DataOutputStream ds = new DataOutputStream(bas); @@ -40,19 +37,18 @@ public void testVectorSerializerFactory() throws Exception { assertNotNull(actualDeserializedVector); assertArrayEquals(vector, actualDeserializedVector, 0.1f); - final KNNVectorSerializer arraySerializer = - KNNVectorSerializerFactory.getSerializerBySerializationMode(SerializationMode.ARRAY); + final KNNVectorSerializer arraySerializer = KNNVectorSerializerFactory.getSerializerBySerializationMode(SerializationMode.ARRAY); assertNotNull(arraySerializer); - final KNNVectorSerializer collectionOfFloatsSerializer = - KNNVectorSerializerFactory.getSerializerBySerializationMode(SerializationMode.COLLECTION_OF_FLOATS); + final KNNVectorSerializer collectionOfFloatsSerializer = KNNVectorSerializerFactory.getSerializerBySerializationMode( + SerializationMode.COLLECTION_OF_FLOATS + ); assertNotNull(collectionOfFloatsSerializer); } - public void testVectorSerializerFactory_throwExceptionForStreamWithUnsupportedDataType() throws Exception { - //prepare array of chars that is not supported by serializer factory. expected behavior is to fail - final char[] arrayOfChars = new char[] {'a', 'b', 'c'}; + // prepare array of chars that is not supported by serializer factory. expected behavior is to fail + final char[] arrayOfChars = new char[] { 'a', 'b', 'c' }; final ByteArrayOutputStream bas = new ByteArrayOutputStream(); final DataOutputStream ds = new DataOutputStream(bas); for (char ch : arrayOfChars) @@ -75,14 +71,14 @@ public void testVectorAsArraySerializer() throws Exception { final KNNVectorSerializer vectorSerializer = KNNVectorSerializerFactory.getSerializerByStreamContent(bais); - //testing serialization + // testing serialization bais.reset(); final byte[] actualSerializedVector = vectorSerializer.floatToByteArray(vector); assertNotNull(actualSerializedVector); assertArrayEquals(serializedVector, actualSerializedVector); - //testing deserialization + // testing deserialization bais.reset(); final float[] actualDeserializedVector = vectorSerializer.byteToFloatArray(bais); @@ -91,7 +87,7 @@ public void testVectorAsArraySerializer() throws Exception { } public void testVectorAsCollectionOfFloatsSerializer() throws Exception { - //setup + // setup final float[] vector = getArrayOfRandomFloats(20); final ByteArrayOutputStream bas = new ByteArrayOutputStream(); @@ -103,14 +99,14 @@ public void testVectorAsCollectionOfFloatsSerializer() throws Exception { final KNNVectorSerializer vectorSerializer = KNNVectorSerializerFactory.getSerializerByStreamContent(bais); - //testing serialization + // testing serialization bais.reset(); final byte[] actualSerializedVector = vectorSerializer.floatToByteArray(vector); assertNotNull(actualSerializedVector); assertArrayEquals(vectorAsCollectionOfFloats, actualSerializedVector); - //testing deserialization + // testing deserialization bais.reset(); final float[] actualDeserializedVector = vectorSerializer.byteToFloatArray(bais);