Skip to content
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

[ML] Hold ML filter items in sorted set #31338

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@
import org.elasticsearch.xpack.core.ml.MlMetaIndex;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.SortedSet;
import java.util.TreeSet;

public class MlFilter implements ToXContentObject, Writeable {

Expand Down Expand Up @@ -53,9 +54,9 @@ private static ObjectParser<Builder, Void> createParser(boolean ignoreUnknownFie

private final String id;
private final String description;
private final List<String> items;
private final SortedSet<String> items;

public MlFilter(String id, String description, List<String> items) {
public MlFilter(String id, String description, SortedSet<String> items) {
this.id = Objects.requireNonNull(id, ID.getPreferredName() + " must not be null");
this.description = description;
this.items = Objects.requireNonNull(items, ITEMS.getPreferredName() + " must not be null");
Expand All @@ -68,7 +69,8 @@ public MlFilter(StreamInput in) throws IOException {
} else {
description = null;
}
items = Arrays.asList(in.readStringArray());
items = new TreeSet<>();
items.addAll(Arrays.asList(in.readStringArray()));
}

@Override
Expand Down Expand Up @@ -103,8 +105,8 @@ public String getDescription() {
return description;
}

public List<String> getItems() {
return new ArrayList<>(items);
public SortedSet<String> getItems() {
return Collections.unmodifiableSortedSet(items);
}

@Override
Expand Down Expand Up @@ -142,7 +144,7 @@ public static class Builder {

private String id;
private String description;
private List<String> items = Collections.emptyList();
private SortedSet<String> items = new TreeSet<>();

private Builder() {}

Expand All @@ -162,12 +164,13 @@ public Builder setDescription(String description) {
}

public Builder setItems(List<String> items) {
this.items = items;
this.items = new TreeSet<>();
this.items.addAll(items);
return this;
}

public Builder setItems(String... items) {
this.items = Arrays.asList(items);
setItems(Arrays.asList(items));
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@
import org.elasticsearch.test.AbstractSerializingTestCase;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.TreeSet;

import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;

Expand All @@ -40,7 +39,7 @@ public static MlFilter createRandom(String filterId) {
}

int size = randomInt(10);
List<String> items = new ArrayList<>(size);
TreeSet<String> items = new TreeSet<>();
for (int i = 0; i < size; i++) {
items.add(randomAlphaOfLengthBetween(1, 20));
}
Expand All @@ -58,7 +57,7 @@ protected MlFilter doParseInstance(XContentParser parser) {
}

public void testNullId() {
NullPointerException ex = expectThrows(NullPointerException.class, () -> new MlFilter(null, "", Collections.emptyList()));
NullPointerException ex = expectThrows(NullPointerException.class, () -> new MlFilter(null, "", new TreeSet<>()));
assertEquals(MlFilter.ID.getPreferredName() + " must not be null", ex.getMessage());
}

Expand Down Expand Up @@ -88,4 +87,14 @@ public void testLenientParser() throws IOException {
MlFilter.LENIENT_PARSER.apply(parser, null);
}
}

public void testItemsAreSorted() {
MlFilter filter = MlFilter.builder("foo").setItems("c", "b", "a").build();
assertThat(filter.getItems(), contains("a", "b", "c"));
}

public void testGetItemsReturnsUnmodifiable() {
MlFilter filter = MlFilter.builder("foo").setItems("c", "b", "a").build();
expectThrows(UnsupportedOperationException.class, () -> filter.getItems().add("x"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ setup:
filter_id: filter-foo
body: >
{
"items": ["abc", "xyz"]
"items": ["xyz", "abc"]
}

- do:
Expand Down