Skip to content

Commit d4ace23

Browse files
committed
Updated GET views to use new array format
1 parent fe9f92d commit d4ace23

File tree

5 files changed

+23
-24
lines changed

5 files changed

+23
-24
lines changed

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/view/GetViewAction.java

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,10 @@
2121
import org.elasticsearch.xcontent.XContentBuilder;
2222

2323
import java.io.IOException;
24+
import java.util.Collections;
2425
import java.util.List;
2526
import java.util.Map;
2627
import java.util.Objects;
27-
import java.util.TreeMap;
28-
29-
import static org.elasticsearch.xpack.esql.view.ViewMetadata.VIEWS;
3028

3129
public class GetViewAction extends ActionType<GetViewAction.Response> {
3230

@@ -75,15 +73,14 @@ public int hashCode() {
7573

7674
public static class Response extends ActionResponse implements ToXContentObject {
7775

78-
private final Map<String, View> views;
76+
private final List<View> views;
7977

80-
public Response(Map<String, View> views) {
78+
public Response(List<View> views) {
8179
Objects.requireNonNull(views, "views cannot be null");
82-
// use a treemap to guarantee ordering in the set, then transform it to the list of named policies
83-
this.views = new TreeMap<>(views);
80+
this.views = Collections.unmodifiableList(views);
8481
}
8582

86-
public Map<String, View> getViews() {
83+
public List<View> getViews() {
8784
return views;
8885
}
8986

@@ -95,7 +92,7 @@ public void writeTo(StreamOutput out) throws IOException {
9592
@Override
9693
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
9794
builder.startObject();
98-
var v = ChunkedToXContentHelper.xContentObjectFieldObjects(VIEWS.getPreferredName(), views);
95+
var v = ChunkedToXContentHelper.array("views", new ViewMetadata.ViewIterator(views));
9996
while (v.hasNext()) {
10097
v.next().toXContent(builder, params);
10198
}
@@ -121,7 +118,7 @@ public int hashCode() {
121118

122119
@Override
123120
public String toString() {
124-
return "GetViewAction.Response" + views.keySet();
121+
return "GetViewAction.Response" + views.stream().map(View::name).toList();
125122
}
126123
}
127124
}

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/view/TransportGetViewAction.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import java.util.Collection;
2828
import java.util.Collections;
2929
import java.util.List;
30-
import java.util.TreeMap;
3130

3231
public class TransportGetViewAction extends TransportLocalProjectMetadataAction<GetViewAction.Request, GetViewAction.Response> {
3332
public static final ActionType<RemoteInfoResponse> TYPE = new ActionType<>(GetViewAction.NAME);
@@ -60,7 +59,7 @@ protected void localClusterStateOperation(
6059
ActionListener<GetViewAction.Response> listener
6160
) {
6261
ProjectId projectId = project.projectId();
63-
TreeMap<String, View> views = new TreeMap<>();
62+
List<View> views = new ArrayList<>();
6463
List<String> missing = new ArrayList<>();
6564
Collection<String> names = request.names();
6665
if (names.isEmpty()) {
@@ -71,7 +70,7 @@ protected void localClusterStateOperation(
7170
if (view == null) {
7271
missing.add(name);
7372
} else {
74-
views.put(name, view);
73+
views.add(view);
7574
}
7675
}
7776
if (missing.isEmpty() == false) {

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/view/ViewMetadata.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,15 @@ public void writeTo(StreamOutput out) throws IOException {
113113

114114
@Override
115115
public Iterator<? extends ToXContent> toXContentChunked(ToXContent.Params ignored) {
116-
return ChunkedToXContentHelper.array(VIEWS.getPreferredName(), new ViewIterator());
116+
return ChunkedToXContentHelper.array(VIEWS.getPreferredName(), new ViewIterator(views));
117117
}
118118

119-
private class ViewIterator implements Iterator<ToXContent> {
120-
private final Iterator<View> internal = views.iterator();
119+
static class ViewIterator implements Iterator<ToXContent> {
120+
private final Iterator<View> internal;
121+
122+
ViewIterator(List<View> views) {
123+
this.internal = views.iterator();
124+
}
121125

122126
@Override
123127
public boolean hasNext() {

x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/view/AbstractViewTestCase.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import java.util.Collection;
2020
import java.util.List;
21-
import java.util.Map;
2221
import java.util.concurrent.CountDownLatch;
2322
import java.util.concurrent.atomic.AtomicReference;
2423

@@ -74,7 +73,7 @@ protected void delete(String name) throws Exception {
7473
}
7574
}
7675

77-
public Map<String, View> get(String... names) throws Exception {
76+
public List<View> get(String... names) throws Exception {
7877
if (names == null || (names.length == 1 && names[0] == null)) {
7978
// This is only for consistent testing, in production this is already checked in the REST API
8079
throw new IllegalArgumentException("name is missing or empty");

x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/view/ViewCrudTests.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@
1515
import org.junit.rules.TestRule;
1616
import org.junit.runners.model.Statement;
1717

18-
import java.util.Map;
18+
import java.util.List;
19+
import java.util.Optional;
1920
import java.util.concurrent.atomic.AtomicReference;
2021

2122
import static org.elasticsearch.xpack.esql.plugin.EsqlFeatures.ESQL_VIEWS_FEATURE_FLAG;
2223
import static org.elasticsearch.xpack.esql.view.ViewTests.randomName;
2324
import static org.elasticsearch.xpack.esql.view.ViewTests.randomView;
2425
import static org.hamcrest.Matchers.containsString;
2526
import static org.hamcrest.Matchers.equalTo;
26-
import static org.hamcrest.Matchers.not;
2727
import static org.hamcrest.Matchers.nullValue;
2828

2929
public class ViewCrudTests extends AbstractViewTestCase {
@@ -167,11 +167,11 @@ public void testGetValidation() throws Exception {
167167
);
168168
}
169169

170-
private void assertView(Map<String, View> result, String name, View view) {
170+
private void assertView(List<View> result, String name, View view) {
171171
assertThat(result.size(), equalTo(1));
172-
View found = result.get(name);
173-
assertThat(found, not(nullValue()));
174-
assertThat(found, equalTo(view));
172+
Optional<View> found = result.stream().filter(v -> v.name().equals(name)).findFirst();
173+
assertFalse(found.isEmpty());
174+
assertThat(found.get(), equalTo(view));
175175
}
176176

177177
private void assertViewMissing(TestViewsApi viewsApi, String name, int viewCount) throws Exception {

0 commit comments

Comments
 (0)