Skip to content

Commit

Permalink
remove get all chunkers in chunker factory
Browse files Browse the repository at this point in the history
Signed-off-by: yuye-aws <[email protected]>
  • Loading branch information
yuye-aws committed Mar 14, 2024
1 parent a574980 commit 87679ad
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.Locale;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -101,18 +100,6 @@ private void validateAndParseAlgorithmMap(final Map<String, Object> algorithmMap
Entry<String, Object> algorithmEntry = algorithmMap.entrySet().iterator().next();
String algorithmKey = algorithmEntry.getKey();
Object algorithmValue = algorithmEntry.getValue();
Set<String> supportedChunkers = ChunkerFactory.getAllChunkers();
if (!supportedChunkers.contains(algorithmKey)) {
throw new IllegalArgumentException(
String.format(
Locale.ROOT,
"Unable to create %s processor as chunker algorithm [%s] is not supported. Supported chunkers types are %s",
TYPE,
algorithmKey,
supportedChunkers
)
);
}
if (!(algorithmValue instanceof Map)) {
throw new IllegalArgumentException(
String.format(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@
package org.opensearch.neuralsearch.processor.chunker;

import com.google.common.collect.ImmutableMap;
import lombok.Getter;

import java.util.Map;
import java.util.Set;
import java.util.Locale;
import java.util.function.Function;

Expand All @@ -24,16 +22,13 @@ public class ChunkerFactory {
DelimiterChunker::new
);

@Getter
private static final Set<String> allChunkers = chunkers.keySet();

public static Chunker create(final String type, final Map<String, Object> parameters) {
Function<Map<String, Object>, Chunker> chunkerConstructionFunction = chunkers.get(type);
if (chunkerConstructionFunction == null) {
throw new IllegalArgumentException(
String.format(
Locale.ROOT,
"chunking algorithm [%s] is not supported. Supported chunking algorithms are %s",
"Chunking algorithm [%s] is not supported. Supported chunking algorithms are %s",
type,
chunkers.keySet()
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,14 +248,7 @@ public void testCreate_whenAlgorithmFieldInvalidAlgorithmName_thenFail() {
() -> textChunkingProcessorFactory.create(registry, PROCESSOR_TAG, DESCRIPTION, config)
);
assert (illegalArgumentException.getMessage()
.contains(
String.format(
Locale.ROOT,
"Unable to create %s processor as chunker algorithm [%s] is not supported.",
TYPE,
invalid_algorithm_type
)
));
.contains(String.format(Locale.ROOT, "Chunking algorithm [%s] is not supported.", invalid_algorithm_type)));
}

public void testCreate_whenAlgorithmFieldInvalidAlgorithmContent_thenFail() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Set;

import static org.opensearch.neuralsearch.processor.chunker.FixedTokenLengthChunker.ANALYSIS_REGISTRY_FIELD;

Expand All @@ -20,11 +19,6 @@ public class ChunkerFactoryTests extends OpenSearchTestCase {
@Mock
private AnalysisRegistry analysisRegistry;

public void testGetAllChunkers() {
Set<String> expected = Set.of(FixedTokenLengthChunker.ALGORITHM_NAME, DelimiterChunker.ALGORITHM_NAME);
assertEquals(expected, ChunkerFactory.getAllChunkers());
}

public void testCreate_FixedTokenLength() {
Chunker chunker = ChunkerFactory.create(FixedTokenLengthChunker.ALGORITHM_NAME, createChunkParameters());
assertNotNull(chunker);
Expand All @@ -38,13 +32,13 @@ public void testCreate_Delimiter() {
}

public void testCreate_Invalid() {
String invalidChunkerType = "Invalid Chunker Type";
String invalidChunkerName = "Invalid Chunker Algorithm";
IllegalArgumentException illegalArgumentException = assertThrows(
IllegalArgumentException.class,
() -> ChunkerFactory.create(invalidChunkerType, createChunkParameters())
() -> ChunkerFactory.create(invalidChunkerName, createChunkParameters())
);
assert (illegalArgumentException.getMessage()
.contains(String.format(Locale.ROOT, "chunking algorithm [%s] is not supported.", invalidChunkerType)));
.contains(String.format(Locale.ROOT, "Chunking algorithm [%s] is not supported.", invalidChunkerName)));
}

private Map<String, Object> createChunkParameters() {
Expand Down

0 comments on commit 87679ad

Please sign in to comment.