diff --git a/core/src/main/java/org/elasticsearch/common/compress/CompressedStreamInput.java b/core/src/main/java/org/elasticsearch/common/compress/CompressedStreamInput.java index 82eefe13a4c..44690e66efa 100644 --- a/core/src/main/java/org/elasticsearch/common/compress/CompressedStreamInput.java +++ b/core/src/main/java/org/elasticsearch/common/compress/CompressedStreamInput.java @@ -44,9 +44,9 @@ public abstract class CompressedStreamInput extends StreamInput { } @Override - public StreamInput setVersion(Version version) { + public void setVersion(Version version) { in.setVersion(version); - return super.setVersion(version); + super.setVersion(version); } /** diff --git a/core/src/main/java/org/elasticsearch/common/io/stream/FilterStreamInput.java b/core/src/main/java/org/elasticsearch/common/io/stream/FilterStreamInput.java index a017c80c87c..5f3bd011dd9 100644 --- a/core/src/main/java/org/elasticsearch/common/io/stream/FilterStreamInput.java +++ b/core/src/main/java/org/elasticsearch/common/io/stream/FilterStreamInput.java @@ -19,17 +19,18 @@ package org.elasticsearch.common.io.stream; +import org.elasticsearch.Version; + import java.io.IOException; /** - * Wraps a {@link StreamInput} and associates it with a {@link NamedWriteableRegistry} + * Wraps a {@link StreamInput} and delegates to it. To be used to add functionality to an existing stream by subclassing. */ -public class FilterStreamInput extends StreamInput { +public abstract class FilterStreamInput extends StreamInput { private final StreamInput delegate; - public FilterStreamInput(StreamInput delegate, NamedWriteableRegistry namedWriteableRegistry) { - super(namedWriteableRegistry); + protected FilterStreamInput(StreamInput delegate) { this.delegate = delegate; } @@ -57,4 +58,14 @@ public class FilterStreamInput extends StreamInput { public void close() throws IOException { delegate.close(); } + + @Override + public Version getVersion() { + return delegate.getVersion(); + } + + @Override + public void setVersion(Version version) { + delegate.setVersion(version); + } } diff --git a/core/src/main/java/org/elasticsearch/common/io/stream/NamedWriteable.java b/core/src/main/java/org/elasticsearch/common/io/stream/NamedWriteable.java index e41d8cdeddc..9396d50a986 100644 --- a/core/src/main/java/org/elasticsearch/common/io/stream/NamedWriteable.java +++ b/core/src/main/java/org/elasticsearch/common/io/stream/NamedWriteable.java @@ -29,5 +29,5 @@ public interface NamedWriteable extends Writeable { /** * Returns the name of the writeable object */ - String getName(); + String getWriteableName(); } diff --git a/core/src/main/java/org/elasticsearch/common/io/stream/NamedWriteableAwareStreamInput.java b/core/src/main/java/org/elasticsearch/common/io/stream/NamedWriteableAwareStreamInput.java new file mode 100644 index 00000000000..a6d17089652 --- /dev/null +++ b/core/src/main/java/org/elasticsearch/common/io/stream/NamedWriteableAwareStreamInput.java @@ -0,0 +1,42 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.elasticsearch.common.io.stream; + +import java.io.IOException; + +/** + * Wraps a {@link StreamInput} and associates it with a {@link NamedWriteableRegistry} + */ +public class NamedWriteableAwareStreamInput extends FilterStreamInput { + + private final NamedWriteableRegistry namedWriteableRegistry; + + public NamedWriteableAwareStreamInput(StreamInput delegate, NamedWriteableRegistry namedWriteableRegistry) { + super(delegate); + this.namedWriteableRegistry = namedWriteableRegistry; + } + + @Override + C readNamedWriteable(Class categoryClass) throws IOException { + String name = readString(); + NamedWriteable namedWriteable = namedWriteableRegistry.getPrototype(categoryClass, name); + return namedWriteable.readFrom(this); + } +} diff --git a/core/src/main/java/org/elasticsearch/common/io/stream/NamedWriteableRegistry.java b/core/src/main/java/org/elasticsearch/common/io/stream/NamedWriteableRegistry.java index 9047bbb02ac..42014786749 100644 --- a/core/src/main/java/org/elasticsearch/common/io/stream/NamedWriteableRegistry.java +++ b/core/src/main/java/org/elasticsearch/common/io/stream/NamedWriteableRegistry.java @@ -28,28 +28,57 @@ import java.util.Map; */ public class NamedWriteableRegistry { - private Map registry = new HashMap<>(); + private final Map, InnerRegistry> registry = new HashMap<>(); /** - * Registers a {@link NamedWriteable} prototype + * Registers a {@link NamedWriteable} prototype given its category */ - public synchronized void registerPrototype(NamedWriteable namedWriteable) { - if (registry.containsKey(namedWriteable.getName())) { - throw new IllegalArgumentException("named writeable of type [" + namedWriteable.getClass().getName() + "] with name [" + namedWriteable.getName() + "] " + - "is already registered by type [" + registry.get(namedWriteable.getName()).getClass().getName() + "]"); + public synchronized void registerPrototype(Class categoryClass, NamedWriteable namedWriteable) { + @SuppressWarnings("unchecked") + InnerRegistry innerRegistry = (InnerRegistry)registry.get(categoryClass); + if (innerRegistry == null) { + innerRegistry = new InnerRegistry<>(categoryClass); + registry.put(categoryClass, innerRegistry); } - registry.put(namedWriteable.getName(), namedWriteable); + innerRegistry.registerPrototype(namedWriteable); } /** - * Returns a prototype of the {@link NamedWriteable} object identified by the name provided as argument + * Returns a prototype of the {@link NamedWriteable} object identified by the name provided as argument and its category */ - public NamedWriteable getPrototype(String name) { + public synchronized NamedWriteable getPrototype(Class categoryClass, String name) { @SuppressWarnings("unchecked") - NamedWriteable namedWriteable = (NamedWriteable)registry.get(name); - if (namedWriteable == null) { - throw new IllegalArgumentException("unknown named writeable with name [" + name + "]"); + InnerRegistry innerRegistry = (InnerRegistry)registry.get(categoryClass); + if (innerRegistry == null) { + throw new IllegalArgumentException("unknown named writeable category [" + categoryClass.getName() + "]"); + } + return innerRegistry.getPrototype(name); + } + + private static class InnerRegistry { + + private final Map> registry = new HashMap<>(); + private final Class categoryClass; + + private InnerRegistry(Class categoryClass) { + this.categoryClass = categoryClass; + } + + private void registerPrototype(NamedWriteable namedWriteable) { + NamedWriteable existingNamedWriteable = registry.get(namedWriteable.getWriteableName()); + if (existingNamedWriteable != null) { + throw new IllegalArgumentException("named writeable of type [" + namedWriteable.getClass().getName() + "] with name [" + namedWriteable.getWriteableName() + "] " + + "is already registered by type [" + existingNamedWriteable.getClass().getName() + "] within category [" + categoryClass.getName() + "]"); + } + registry.put(namedWriteable.getWriteableName(), namedWriteable); + } + + private NamedWriteable getPrototype(String name) { + NamedWriteable namedWriteable = registry.get(name); + if (namedWriteable == null) { + throw new IllegalArgumentException("unknown named writeable with name [" + name + "] within category [" + categoryClass.getName() + "]"); + } + return namedWriteable; } - return namedWriteable; } } diff --git a/core/src/main/java/org/elasticsearch/common/io/stream/StreamInput.java b/core/src/main/java/org/elasticsearch/common/io/stream/StreamInput.java index 5ea3444ade5..ecef8045015 100644 --- a/core/src/main/java/org/elasticsearch/common/io/stream/StreamInput.java +++ b/core/src/main/java/org/elasticsearch/common/io/stream/StreamInput.java @@ -19,8 +19,6 @@ package org.elasticsearch.common.io.stream; -import com.fasterxml.jackson.core.JsonLocation; -import com.fasterxml.jackson.core.JsonParseException; import org.apache.lucene.index.CorruptIndexException; import org.apache.lucene.index.IndexFormatTooNewException; import org.apache.lucene.index.IndexFormatTooOldException; @@ -28,7 +26,6 @@ import org.apache.lucene.store.AlreadyClosedException; import org.apache.lucene.store.LockObtainFailedException; import org.apache.lucene.util.BytesRef; import org.apache.lucene.util.CharsRefBuilder; -import org.elasticsearch.ElasticsearchException; import org.elasticsearch.Version; import org.elasticsearch.common.Nullable; import org.elasticsearch.common.Strings; @@ -36,20 +33,17 @@ import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.text.StringAndBytesText; import org.elasticsearch.common.text.Text; +import org.elasticsearch.index.query.QueryBuilder; import org.joda.time.DateTime; import org.joda.time.DateTimeZone; import java.io.*; import java.nio.file.NoSuchFileException; import java.util.*; -import java.util.regex.Pattern; import static org.elasticsearch.ElasticsearchException.readException; import static org.elasticsearch.ElasticsearchException.readStackTrace; -/** - * - */ public abstract class StreamInput extends InputStream { private final NamedWriteableRegistry namedWriteableRegistry; @@ -68,9 +62,8 @@ public abstract class StreamInput extends InputStream { return this.version; } - public StreamInput setVersion(Version version) { + public void setVersion(Version version) { this.version = version; - return this; } /** @@ -570,25 +563,18 @@ public abstract class StreamInput extends InputStream { /** * Reads a {@link NamedWriteable} from the current stream, by first reading its name and then looking for * the corresponding entry in the registry by name, so that the proper object can be read and returned. + * Default implementation throws {@link UnsupportedOperationException} as StreamInput doesn't hold a registry. + * Use {@link FilterInputStream} instead which wraps a stream and supports a {@link NamedWriteableRegistry} too. */ - public C readNamedWriteable() throws IOException { - String name = readString(); - NamedWriteable namedWriteable = namedWriteableRegistry.getPrototype(name); - return namedWriteable.readFrom(this); + C readNamedWriteable(@SuppressWarnings("unused") Class categoryClass) throws IOException { + throw new UnsupportedOperationException(); } /** - * Reads a list of {@link NamedWriteable} from the current stream, by first reading its size and then - * reading the individual objects using {@link #readNamedWriteable()}. + * Reads a {@link QueryBuilder} from the current stream */ - public List readNamedWriteableList() throws IOException { - List list = new ArrayList<>(); - int size = readInt(); - for (int i = 0; i < size; i++) { - C obj = readNamedWriteable(); - list.add(obj); - } - return list; + public QueryBuilder readQuery() throws IOException { + return readNamedWriteable(QueryBuilder.class); } public static StreamInput wrap(BytesReference reference) { @@ -605,5 +591,4 @@ public abstract class StreamInput extends InputStream { public static StreamInput wrap(byte[] bytes, int offset, int length) { return new InputStreamStreamInput(new ByteArrayInputStream(bytes, offset, length)); } - } diff --git a/core/src/main/java/org/elasticsearch/common/io/stream/StreamOutput.java b/core/src/main/java/org/elasticsearch/common/io/stream/StreamOutput.java index b9e6a46070f..84cd303ebc2 100644 --- a/core/src/main/java/org/elasticsearch/common/io/stream/StreamOutput.java +++ b/core/src/main/java/org/elasticsearch/common/io/stream/StreamOutput.java @@ -19,7 +19,6 @@ package org.elasticsearch.common.io.stream; -import com.vividsolutions.jts.util.Assert; import org.apache.lucene.index.CorruptIndexException; import org.apache.lucene.index.IndexFormatTooNewException; import org.apache.lucene.index.IndexFormatTooOldException; @@ -33,6 +32,7 @@ import org.elasticsearch.common.Nullable; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.regex.Regex; import org.elasticsearch.common.text.Text; +import org.elasticsearch.index.query.QueryBuilder; import org.joda.time.ReadableInstant; import java.io.EOFException; @@ -618,19 +618,15 @@ public abstract class StreamOutput extends OutputStream { /** * Writes a {@link NamedWriteable} to the current stream, by first writing its name and then the object itself */ - public void writeNamedWriteable(NamedWriteable namedWriteable) throws IOException { - writeString(namedWriteable.getName()); + void writeNamedWriteable(NamedWriteable namedWriteable) throws IOException { + writeString(namedWriteable.getWriteableName()); namedWriteable.writeTo(this); } /** - * Writes a list of {@link NamedWriteable} to the current stream, by first writing its size and then iterating over the objects - * in the list + * Writes a {@link QueryBuilder} to the current stream */ - public void writeNamedWriteableList(List list) throws IOException { - writeInt(list.size()); - for (NamedWriteable obj : list) { - writeNamedWriteable(obj); - } + public void writeQuery(QueryBuilder queryBuilder) throws IOException { + writeNamedWriteable(queryBuilder); } } diff --git a/core/src/main/java/org/elasticsearch/index/query/AbstractQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/AbstractQueryBuilder.java index 9e3628fb51f..e54b4844412 100644 --- a/core/src/main/java/org/elasticsearch/index/query/AbstractQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/AbstractQueryBuilder.java @@ -257,4 +257,42 @@ public abstract class AbstractQueryBuilder exte } return validationException; } + + @Override + public String getName() { + //default impl returns the same as writeable name, but we keep the distinction between the two just to make sure + return getWriteableName(); + } + + protected final void writeQueries(StreamOutput out, List queries) throws IOException { + out.writeVInt(queries.size()); + for (QueryBuilder query : queries) { + out.writeQuery(query); + } + } + + protected final List readQueries(StreamInput in) throws IOException { + List queries = new ArrayList<>(); + int size = in.readVInt(); + for (int i = 0; i < size; i++) { + queries.add(in.readQuery()); + } + return queries; + } + + protected final void writeOptionalQuery(StreamOutput out, QueryBuilder query) throws IOException { + if (query == null) { + out.writeBoolean(false); + } else { + out.writeBoolean(true); + out.writeQuery(query); + } + } + + protected final QueryBuilder readOptionalQuery(StreamInput in) throws IOException { + if (in.readBoolean()) { + return in.readQuery(); + } + return null; + } } diff --git a/core/src/main/java/org/elasticsearch/index/query/AndQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/AndQueryBuilder.java index 90879401fea..05ef359f7ef 100644 --- a/core/src/main/java/org/elasticsearch/index/query/AndQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/AndQueryBuilder.java @@ -111,7 +111,7 @@ public class AndQueryBuilder extends AbstractQueryBuilder { } @Override - public String getName() { + public String getWriteableName() { return NAME; } @@ -128,7 +128,7 @@ public class AndQueryBuilder extends AbstractQueryBuilder { @Override protected AndQueryBuilder doReadFrom(StreamInput in) throws IOException { AndQueryBuilder andQueryBuilder = new AndQueryBuilder(); - List queryBuilders = in.readNamedWriteableList(); + List queryBuilders = readQueries(in); for (QueryBuilder queryBuilder : queryBuilders) { andQueryBuilder.add(queryBuilder); } @@ -138,6 +138,6 @@ public class AndQueryBuilder extends AbstractQueryBuilder { @Override protected void doWriteTo(StreamOutput out) throws IOException { - out.writeNamedWriteableList(filters); + writeQueries(out, filters); } } \ No newline at end of file diff --git a/core/src/main/java/org/elasticsearch/index/query/BoolQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/BoolQueryBuilder.java index f0076e4c6fd..9ce5a396735 100644 --- a/core/src/main/java/org/elasticsearch/index/query/BoolQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/BoolQueryBuilder.java @@ -249,7 +249,7 @@ public class BoolQueryBuilder extends AbstractQueryBuilder { } @Override - public String getName() { + public String getWriteableName() { return NAME; } @@ -308,13 +308,13 @@ public class BoolQueryBuilder extends AbstractQueryBuilder { @Override protected BoolQueryBuilder doReadFrom(StreamInput in) throws IOException { BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder(); - List queryBuilders = in.readNamedWriteableList(); + List queryBuilders = readQueries(in); boolQueryBuilder.mustClauses.addAll(queryBuilders); - queryBuilders = in.readNamedWriteableList(); + queryBuilders = readQueries(in); boolQueryBuilder.mustNotClauses.addAll(queryBuilders); - queryBuilders = in.readNamedWriteableList(); + queryBuilders = readQueries(in); boolQueryBuilder.shouldClauses.addAll(queryBuilders); - queryBuilders = in.readNamedWriteableList(); + queryBuilders = readQueries(in); boolQueryBuilder.filterClauses.addAll(queryBuilders); boolQueryBuilder.adjustPureNegative = in.readBoolean(); boolQueryBuilder.disableCoord = in.readBoolean(); @@ -325,10 +325,10 @@ public class BoolQueryBuilder extends AbstractQueryBuilder { @Override protected void doWriteTo(StreamOutput out) throws IOException { - out.writeNamedWriteableList(mustClauses); - out.writeNamedWriteableList(mustNotClauses); - out.writeNamedWriteableList(shouldClauses); - out.writeNamedWriteableList(filterClauses); + writeQueries(out, mustClauses); + writeQueries(out, mustNotClauses); + writeQueries(out, shouldClauses); + writeQueries(out, filterClauses); out.writeBoolean(adjustPureNegative); out.writeBoolean(disableCoord); out.writeOptionalString(minimumShouldMatch); diff --git a/core/src/main/java/org/elasticsearch/index/query/BoostingQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/BoostingQueryBuilder.java index 561b81a5e6f..a9cab5f656b 100644 --- a/core/src/main/java/org/elasticsearch/index/query/BoostingQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/BoostingQueryBuilder.java @@ -124,7 +124,7 @@ public class BoostingQueryBuilder extends AbstractQueryBuilder @Override protected DisMaxQueryBuilder doReadFrom(StreamInput in) throws IOException { DisMaxQueryBuilder disMax = new DisMaxQueryBuilder(); - List queryBuilders = in.readNamedWriteableList(); + List queryBuilders = readQueries(in); disMax.queries.addAll(queryBuilders); disMax.tieBreaker = in.readFloat(); return disMax; @@ -122,7 +122,7 @@ public class DisMaxQueryBuilder extends AbstractQueryBuilder @Override protected void doWriteTo(StreamOutput out) throws IOException { - out.writeNamedWriteableList(queries); + writeQueries(out, queries); out.writeFloat(tieBreaker); } @@ -138,7 +138,7 @@ public class DisMaxQueryBuilder extends AbstractQueryBuilder } @Override - public String getName() { + public String getWriteableName() { return NAME; } } diff --git a/core/src/main/java/org/elasticsearch/index/query/EmptyQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/EmptyQueryBuilder.java index 901584173d5..de5d5bb400b 100644 --- a/core/src/main/java/org/elasticsearch/index/query/EmptyQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/EmptyQueryBuilder.java @@ -50,10 +50,15 @@ public class EmptyQueryBuilder extends ToXContentToBytes implements QueryBuilder } @Override - public String getName() { + public String getWriteableName() { return NAME; } + @Override + public String getName() { + return getWriteableName(); + } + @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { builder.startObject(); diff --git a/core/src/main/java/org/elasticsearch/index/query/ExistsQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/ExistsQueryBuilder.java index 0ef9bfb35cf..4e1f54ec959 100644 --- a/core/src/main/java/org/elasticsearch/index/query/ExistsQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/ExistsQueryBuilder.java @@ -138,7 +138,7 @@ public class ExistsQueryBuilder extends AbstractQueryBuilder } @Override - public String getName() { + public String getWriteableName() { return NAME; } } diff --git a/core/src/main/java/org/elasticsearch/index/query/FQueryFilterBuilder.java b/core/src/main/java/org/elasticsearch/index/query/FQueryFilterBuilder.java index 5ec6318baa7..40511c48c73 100644 --- a/core/src/main/java/org/elasticsearch/index/query/FQueryFilterBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/FQueryFilterBuilder.java @@ -95,18 +95,18 @@ public class FQueryFilterBuilder extends AbstractQueryBuilder i } @Override - public String getName() { + public String getWriteableName() { return NAME; } } diff --git a/core/src/main/java/org/elasticsearch/index/query/GeoBoundingBoxQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/GeoBoundingBoxQueryBuilder.java index e5ca83066cc..4e61528687e 100644 --- a/core/src/main/java/org/elasticsearch/index/query/GeoBoundingBoxQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/GeoBoundingBoxQueryBuilder.java @@ -168,7 +168,7 @@ public class GeoBoundingBoxQueryBuilder extends AbstractQueryBuilder { } @Override - public String getName() { + public String getWriteableName() { return NAME; } diff --git a/core/src/main/java/org/elasticsearch/index/query/IndicesQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/IndicesQueryBuilder.java index 55ce9eeb47a..d7de7c137c7 100644 --- a/core/src/main/java/org/elasticsearch/index/query/IndicesQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/IndicesQueryBuilder.java @@ -78,7 +78,7 @@ public class IndicesQueryBuilder extends AbstractQueryBuilder { } @Override - public String getName() { + public String getWriteableName() { return NAME; } } diff --git a/core/src/main/java/org/elasticsearch/index/query/MatchAllQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/MatchAllQueryBuilder.java index bb6b666b904..00c50190d18 100644 --- a/core/src/main/java/org/elasticsearch/index/query/MatchAllQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/MatchAllQueryBuilder.java @@ -75,7 +75,7 @@ public class MatchAllQueryBuilder extends AbstractQueryBuilder { } @Override - public String getName() { + public String getWriteableName() { return NAME; } } diff --git a/core/src/main/java/org/elasticsearch/index/query/MissingQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/MissingQueryBuilder.java index 08ef241505d..253af16cc64 100644 --- a/core/src/main/java/org/elasticsearch/index/query/MissingQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/MissingQueryBuilder.java @@ -105,7 +105,7 @@ public class MissingQueryBuilder extends AbstractQueryBuilder } @Override - public final String getName() { + public final String getWriteableName() { return NAME; } } diff --git a/core/src/main/java/org/elasticsearch/index/query/NotQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/NotQueryBuilder.java index 7f3fa0dd2e9..70aec44747a 100644 --- a/core/src/main/java/org/elasticsearch/index/query/NotQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/NotQueryBuilder.java @@ -85,17 +85,17 @@ public class NotQueryBuilder extends AbstractQueryBuilder { @Override protected NotQueryBuilder doReadFrom(StreamInput in) throws IOException { - QueryBuilder queryBuilder = in.readNamedWriteable(); + QueryBuilder queryBuilder = in.readQuery(); return new NotQueryBuilder(queryBuilder); } @Override protected void doWriteTo(StreamOutput out) throws IOException { - out.writeNamedWriteable(filter); + out.writeQuery(filter); } @Override - public String getName() { + public String getWriteableName() { return NAME; } } diff --git a/core/src/main/java/org/elasticsearch/index/query/OrQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/OrQueryBuilder.java index 600b622c5eb..84452f64ccb 100644 --- a/core/src/main/java/org/elasticsearch/index/query/OrQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/OrQueryBuilder.java @@ -108,7 +108,7 @@ public class OrQueryBuilder extends AbstractQueryBuilder { } @Override - public String getName() { + public String getWriteableName() { return NAME; } @@ -125,7 +125,7 @@ public class OrQueryBuilder extends AbstractQueryBuilder { @Override protected OrQueryBuilder doReadFrom(StreamInput in) throws IOException { OrQueryBuilder orQueryBuilder = new OrQueryBuilder(); - List queryBuilders = in.readNamedWriteableList(); + List queryBuilders = readQueries(in); for (QueryBuilder queryBuilder : queryBuilders) { orQueryBuilder.add(queryBuilder); } @@ -135,6 +135,6 @@ public class OrQueryBuilder extends AbstractQueryBuilder { @Override protected void doWriteTo(StreamOutput out) throws IOException { - out.writeNamedWriteableList(filters); + writeQueries(out, filters); } } diff --git a/core/src/main/java/org/elasticsearch/index/query/PrefixQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/PrefixQueryBuilder.java index cf5c0084086..a49580cb3f9 100644 --- a/core/src/main/java/org/elasticsearch/index/query/PrefixQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/PrefixQueryBuilder.java @@ -91,7 +91,7 @@ public class PrefixQueryBuilder extends AbstractQueryBuilder } @Override - public String getName() { + public String getWriteableName() { return NAME; } diff --git a/core/src/main/java/org/elasticsearch/index/query/QueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/QueryBuilder.java index cb4933c1cd3..02bbfb68f1b 100644 --- a/core/src/main/java/org/elasticsearch/index/query/QueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/QueryBuilder.java @@ -58,12 +58,12 @@ public interface QueryBuilder extends NamedWriteable extends NamedWriteable @Override protected QueryFilterBuilder doReadFrom(StreamInput in) throws IOException { - QueryBuilder innerQueryBuilder = in.readNamedWriteable(); + QueryBuilder innerQueryBuilder = in.readQuery(); return new QueryFilterBuilder(innerQueryBuilder); } @Override protected void doWriteTo(StreamOutput out) throws IOException { - out.writeNamedWriteable(queryBuilder); + out.writeQuery(queryBuilder); } @Override - public String getName() { + public String getWriteableName() { return NAME; } } diff --git a/core/src/main/java/org/elasticsearch/index/query/QueryStringQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/QueryStringQueryBuilder.java index a762b7ce01a..baff082cd2b 100644 --- a/core/src/main/java/org/elasticsearch/index/query/QueryStringQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/QueryStringQueryBuilder.java @@ -406,7 +406,7 @@ public class QueryStringQueryBuilder extends AbstractQueryBuilder i } @Override - public String getName() { + public String getWriteableName() { return NAME; } diff --git a/core/src/main/java/org/elasticsearch/index/query/RegexpQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/RegexpQueryBuilder.java index cc3f40bdccf..6399089b34e 100644 --- a/core/src/main/java/org/elasticsearch/index/query/RegexpQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/RegexpQueryBuilder.java @@ -144,7 +144,7 @@ public class RegexpQueryBuilder extends AbstractQueryBuilder } @Override - public String getName() { + public String getWriteableName() { return NAME; } diff --git a/core/src/main/java/org/elasticsearch/index/query/ScriptQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/ScriptQueryBuilder.java index b2f4375bccc..519f065b917 100644 --- a/core/src/main/java/org/elasticsearch/index/query/ScriptQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/ScriptQueryBuilder.java @@ -52,7 +52,7 @@ public class ScriptQueryBuilder extends AbstractQueryBuilder } @Override - public String getName() { + public String getWriteableName() { return NAME; } diff --git a/core/src/main/java/org/elasticsearch/index/query/SimpleQueryStringBuilder.java b/core/src/main/java/org/elasticsearch/index/query/SimpleQueryStringBuilder.java index 4adb967cbfa..087fedd67ba 100644 --- a/core/src/main/java/org/elasticsearch/index/query/SimpleQueryStringBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/SimpleQueryStringBuilder.java @@ -349,7 +349,7 @@ public class SimpleQueryStringBuilder extends AbstractQueryBuilder clauses = new ArrayList<>(); + private final List clauses = new ArrayList<>(); private final int slop; @@ -159,9 +159,9 @@ public class SpanNearQueryBuilder extends AbstractQueryBuilder clauses = in.readNamedWriteableList(); - for (SpanQueryBuilder subClause : clauses) { - queryBuilder.clause(subClause); + List clauses = readQueries(in); + for (QueryBuilder subClause : clauses) { + queryBuilder.clauses.add((SpanQueryBuilder)subClause); } queryBuilder.collectPayloads = in.readBoolean(); queryBuilder.inOrder = in.readBoolean(); @@ -172,7 +172,7 @@ public class SpanNearQueryBuilder extends AbstractQueryBuilder public static final String NAME = "span_or"; - private final ArrayList clauses = new ArrayList<>(); + private final List clauses = new ArrayList<>(); static final SpanOrQueryBuilder PROTOTYPE = new SpanOrQueryBuilder(); @@ -96,9 +96,9 @@ public class SpanOrQueryBuilder extends AbstractQueryBuilder @Override protected SpanOrQueryBuilder doReadFrom(StreamInput in) throws IOException { SpanOrQueryBuilder queryBuilder = new SpanOrQueryBuilder(); - List clauses = in.readNamedWriteableList(); - for (SpanQueryBuilder subClause : clauses) { - queryBuilder.clause(subClause); + List clauses = readQueries(in); + for (QueryBuilder subClause : clauses) { + queryBuilder.clauses.add((SpanQueryBuilder)subClause); } return queryBuilder; @@ -106,7 +106,7 @@ public class SpanOrQueryBuilder extends AbstractQueryBuilder @Override protected void doWriteTo(StreamOutput out) throws IOException { - out.writeNamedWriteableList(clauses); + writeQueries(out, clauses); } @Override @@ -120,7 +120,7 @@ public class SpanOrQueryBuilder extends AbstractQueryBuilder } @Override - public String getName() { + public String getWriteableName() { return NAME; } } diff --git a/core/src/main/java/org/elasticsearch/index/query/SpanTermQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/SpanTermQueryBuilder.java index 787926221b6..24cd8167b36 100644 --- a/core/src/main/java/org/elasticsearch/index/query/SpanTermQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/SpanTermQueryBuilder.java @@ -88,7 +88,7 @@ public class SpanTermQueryBuilder extends BaseTermQueryBuilder { } @Override - public String getName() { + public String getWriteableName() { return NAME; } } diff --git a/core/src/main/java/org/elasticsearch/index/query/TermsLookupQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/TermsLookupQueryBuilder.java index 2318b7d10ea..7e82f1f88bd 100644 --- a/core/src/main/java/org/elasticsearch/index/query/TermsLookupQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/TermsLookupQueryBuilder.java @@ -32,7 +32,7 @@ public class TermsLookupQueryBuilder extends TermsQueryBuilder { } @Override - public String getName() { + public String getWriteableName() { return TermsQueryBuilder.NAME; } } \ No newline at end of file diff --git a/core/src/main/java/org/elasticsearch/index/query/TermsQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/TermsQueryBuilder.java index b7d2b61dad9..987e97a4605 100644 --- a/core/src/main/java/org/elasticsearch/index/query/TermsQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/TermsQueryBuilder.java @@ -204,7 +204,7 @@ public class TermsQueryBuilder extends AbstractQueryBuilder { } @Override - public String getName() { + public String getWriteableName() { return NAME; } } diff --git a/core/src/main/java/org/elasticsearch/index/query/TypeQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/TypeQueryBuilder.java index c441f438588..173daf21e76 100644 --- a/core/src/main/java/org/elasticsearch/index/query/TypeQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/TypeQueryBuilder.java @@ -62,7 +62,7 @@ public class TypeQueryBuilder extends AbstractQueryBuilder { } @Override - public String getName() { + public String getWriteableName() { return NAME; } diff --git a/core/src/main/java/org/elasticsearch/index/query/WildcardQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/WildcardQueryBuilder.java index d337c7af936..89b753e316a 100644 --- a/core/src/main/java/org/elasticsearch/index/query/WildcardQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/WildcardQueryBuilder.java @@ -88,7 +88,7 @@ public class WildcardQueryBuilder extends AbstractQueryBuilder implem Transports.assertTransportThread(); try { transportServiceAdapter.received(data.length); - StreamInput stream = new FilterStreamInput(StreamInput.wrap(data), namedWriteableRegistry); + StreamInput stream = StreamInput.wrap(data); stream.setVersion(version); long requestId = stream.readLong(); @@ -259,6 +260,7 @@ public class LocalTransport extends AbstractLifecycleComponent implem } private void handleRequest(StreamInput stream, long requestId, LocalTransport sourceTransport, Version version) throws Exception { + stream = new NamedWriteableAwareStreamInput(stream, namedWriteableRegistry); final String action = stream.readString(); transportServiceAdapter.onRequestReceived(requestId, action); final LocalTransportChannel transportChannel = new LocalTransportChannel(this, transportServiceAdapter, sourceTransport, action, requestId, version); diff --git a/core/src/main/java/org/elasticsearch/transport/netty/MessageChannelHandler.java b/core/src/main/java/org/elasticsearch/transport/netty/MessageChannelHandler.java index 32a6221e8af..df860ecdf97 100644 --- a/core/src/main/java/org/elasticsearch/transport/netty/MessageChannelHandler.java +++ b/core/src/main/java/org/elasticsearch/transport/netty/MessageChannelHandler.java @@ -27,6 +27,7 @@ import org.elasticsearch.common.compress.CompressorFactory; import org.elasticsearch.common.compress.NotCompressedException; import org.elasticsearch.common.io.stream.FilterStreamInput; import org.elasticsearch.common.io.stream.NamedWriteableRegistry; +import org.elasticsearch.common.io.stream.NamedWriteableAwareStreamInput; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.logging.ESLogger; import org.elasticsearch.common.transport.InetSocketTransportAddress; @@ -115,7 +116,6 @@ public class MessageChannelHandler extends SimpleChannelUpstreamHandler { } streamIn = compressor.streamInput(streamIn); } - streamIn = new FilterStreamInput(streamIn, namedWriteableRegistry); streamIn.setVersion(version); if (TransportStatus.isRequest(status)) { @@ -235,6 +235,7 @@ public class MessageChannelHandler extends SimpleChannelUpstreamHandler { } protected String handleRequest(Channel channel, StreamInput buffer, long requestId, Version version) throws IOException { + buffer = new NamedWriteableAwareStreamInput(buffer, transport.namedWriteableRegistry); final String action = buffer.readString(); transportServiceAdapter.onRequestReceived(requestId, action); final NettyTransportChannel transportChannel = new NettyTransportChannel(transport, transportServiceAdapter, action, channel, requestId, version, profileName); diff --git a/core/src/main/java/org/elasticsearch/transport/netty/NettyTransport.java b/core/src/main/java/org/elasticsearch/transport/netty/NettyTransport.java index daf7721ca61..7bb73228c1e 100644 --- a/core/src/main/java/org/elasticsearch/transport/netty/NettyTransport.java +++ b/core/src/main/java/org/elasticsearch/transport/netty/NettyTransport.java @@ -24,7 +24,8 @@ import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.common.collect.Lists; import com.google.common.collect.Maps; -import org.elasticsearch.*; +import org.elasticsearch.ExceptionsHelper; +import org.elasticsearch.Version; import org.elasticsearch.cluster.node.DiscoveryNode; import org.elasticsearch.common.Booleans; import org.elasticsearch.common.Strings; @@ -150,6 +151,7 @@ public class NettyTransport extends AbstractLifecycleComponent implem protected volatile TransportServiceAdapter transportServiceAdapter; protected volatile BoundTransportAddress boundAddress; protected final KeyedLock connectionLock = new KeyedLock<>(); + protected final NamedWriteableRegistry namedWriteableRegistry; // this lock is here to make sure we close this transport and disconnect all the client nodes // connections while no connect operations is going on... (this might help with 100% CPU when stopping the transport?) @@ -158,8 +160,6 @@ public class NettyTransport extends AbstractLifecycleComponent implem // package visibility for tests final ScheduledPing scheduledPing; - protected final NamedWriteableRegistry namedWriteableRegistry; - @Inject public NettyTransport(Settings settings, ThreadPool threadPool, NetworkService networkService, BigArrays bigArrays, Version version, NamedWriteableRegistry namedWriteableRegistry) { super(settings); @@ -973,7 +973,7 @@ public class NettyTransport extends AbstractLifecycleComponent implem } protected static class ClientChannelPipelineFactory implements ChannelPipelineFactory { - protected NettyTransport nettyTransport; + protected final NettyTransport nettyTransport; public ClientChannelPipelineFactory(NettyTransport nettyTransport) { this.nettyTransport = nettyTransport; diff --git a/core/src/test/java/org/elasticsearch/common/io/streams/BytesStreamsTests.java b/core/src/test/java/org/elasticsearch/common/io/stream/BytesStreamsTests.java similarity index 73% rename from core/src/test/java/org/elasticsearch/common/io/streams/BytesStreamsTests.java rename to core/src/test/java/org/elasticsearch/common/io/stream/BytesStreamsTests.java index 544cfb36d68..afc17ce004b 100644 --- a/core/src/test/java/org/elasticsearch/common/io/streams/BytesStreamsTests.java +++ b/core/src/test/java/org/elasticsearch/common/io/stream/BytesStreamsTests.java @@ -17,23 +17,18 @@ * under the License. */ -package org.elasticsearch.common.io.streams; +package org.elasticsearch.common.io.stream; import org.apache.lucene.util.Constants; -import org.elasticsearch.common.io.stream.BytesStreamOutput; -import org.elasticsearch.common.io.stream.NamedWriteableRegistry; -import org.elasticsearch.common.io.stream.StreamInput; -import org.elasticsearch.common.io.stream.FilterStreamInput; import org.elasticsearch.common.lucene.BytesRefs; import org.elasticsearch.common.util.BigArrays; -import org.elasticsearch.index.query.MatchAllQueryBuilder; -import org.elasticsearch.index.query.QueryBuilder; -import org.elasticsearch.index.query.TermQueryBuilder; import org.elasticsearch.test.ESTestCase; import org.junit.Test; import java.io.IOException; +import java.util.Objects; + import static org.hamcrest.Matchers.closeTo; import static org.hamcrest.Matchers.equalTo; @@ -314,43 +309,124 @@ public class BytesStreamsTests extends ESTestCase { public void testNamedWriteable() throws IOException { BytesStreamOutput out = new BytesStreamOutput(); NamedWriteableRegistry namedWriteableRegistry = new NamedWriteableRegistry(); - namedWriteableRegistry.registerPrototype(new TermQueryBuilder(null, null)); - TermQueryBuilder termQueryBuilder = new TermQueryBuilder(randomAsciiOfLengthBetween(1, 10), randomAsciiOfLengthBetween(1, 10)); - out.writeNamedWriteable(termQueryBuilder); - StreamInput in = new FilterStreamInput(StreamInput.wrap(out.bytes().toBytes()), namedWriteableRegistry); - QueryBuilder queryBuilder = in.readNamedWriteable(); - assertThat(queryBuilder, equalTo((QueryBuilder)termQueryBuilder)); + namedWriteableRegistry.registerPrototype(BaseNamedWriteable.class, new TestNamedWriteable(null, null)); + TestNamedWriteable namedWriteableIn = new TestNamedWriteable(randomAsciiOfLengthBetween(1, 10), randomAsciiOfLengthBetween(1, 10)); + out.writeNamedWriteable(namedWriteableIn); + StreamInput in = new NamedWriteableAwareStreamInput(StreamInput.wrap(out.bytes().toBytes()), namedWriteableRegistry); + BaseNamedWriteable namedWriteableOut = in.readNamedWriteable(BaseNamedWriteable.class); + assertEquals(namedWriteableOut, namedWriteableIn); } @Test public void testNamedWriteableDuplicates() throws IOException { - BytesStreamOutput out = new BytesStreamOutput(); NamedWriteableRegistry namedWriteableRegistry = new NamedWriteableRegistry(); - namedWriteableRegistry.registerPrototype(new TermQueryBuilder(null, null)); + namedWriteableRegistry.registerPrototype(BaseNamedWriteable.class, new TestNamedWriteable(null, null)); try { - //wrong class, no registry available - namedWriteableRegistry.registerPrototype(new TermQueryBuilder(null, null)); + namedWriteableRegistry.registerPrototype(BaseNamedWriteable.class, new TestNamedWriteable(null, null)); fail("registerPrototype should have failed"); } catch(IllegalArgumentException e) { - assertThat(e.getMessage(), equalTo("named writeable of type [" + TermQueryBuilder.class.getName() + "] with name [" + TermQueryBuilder.NAME + "] is already registered by type [" + TermQueryBuilder.class.getName() + "]")); + assertThat(e.getMessage(), equalTo("named writeable of type [" + TestNamedWriteable.class.getName() + "] with name [" + TestNamedWriteable.NAME + "] is already registered by type [" + + TestNamedWriteable.class.getName() + "] within category [" + BaseNamedWriteable.class.getName() + "]")); + } + } + + @Test + public void testNamedWriteableUnknownCategory() throws IOException { + BytesStreamOutput out = new BytesStreamOutput(); + out.writeNamedWriteable(new TestNamedWriteable("test1", "test2")); + StreamInput in = new NamedWriteableAwareStreamInput(StreamInput.wrap(out.bytes().toBytes()), new NamedWriteableRegistry()); + try { + //no named writeable registered with given name, can write but cannot read it back + in.readNamedWriteable(BaseNamedWriteable.class); + fail("read should have failed"); + } catch(IllegalArgumentException e) { + assertThat(e.getMessage(), equalTo("unknown named writeable category [" + BaseNamedWriteable.class.getName() + "]")); } } @Test public void testNamedWriteableUnknownNamedWriteable() throws IOException { - BytesStreamOutput out = new BytesStreamOutput(); NamedWriteableRegistry namedWriteableRegistry = new NamedWriteableRegistry(); - out.writeNamedWriteable(new MatchAllQueryBuilder()); - StreamInput in = StreamInput.wrap(out.bytes().toBytes()); - if (randomBoolean()) { - in = new FilterStreamInput(in, namedWriteableRegistry); - } + namedWriteableRegistry.registerPrototype(BaseNamedWriteable.class, new TestNamedWriteable(null, null)); + BytesStreamOutput out = new BytesStreamOutput(); + out.writeNamedWriteable(new NamedWriteable() { + @Override + public String getWriteableName() { + return "unknown"; + } + + @Override + public void writeTo(StreamOutput out) throws IOException { + } + + @Override + public Object readFrom(StreamInput in) throws IOException { + return null; + } + }); + StreamInput in = new NamedWriteableAwareStreamInput(StreamInput.wrap(out.bytes().toBytes()), namedWriteableRegistry); try { - //no match_all named writeable registered, can write but cannot read it back - in.readNamedWriteable(); + //no named writeable registered with given name under test category, can write but cannot read it back + in.readNamedWriteable(BaseNamedWriteable.class); fail("read should have failed"); } catch(IllegalArgumentException e) { - assertThat(e.getMessage(), equalTo("unknown named writeable with name [" + MatchAllQueryBuilder.NAME + "]")); + assertThat(e.getMessage(), equalTo("unknown named writeable with name [unknown] within category [" + BaseNamedWriteable.class.getName() + "]")); + } + } + + @Test(expected = UnsupportedOperationException.class) + public void testNamedWriteableNotSupportedWithoutWrapping() throws IOException { + BytesStreamOutput out = new BytesStreamOutput(); + TestNamedWriteable testNamedWriteable = new TestNamedWriteable("test1", "test2"); + out.writeNamedWriteable(testNamedWriteable); + StreamInput in = StreamInput.wrap(out.bytes().toBytes()); + in.readNamedWriteable(BaseNamedWriteable.class); + } + + private static abstract class BaseNamedWriteable implements NamedWriteable { + + } + + private static class TestNamedWriteable extends BaseNamedWriteable { + + private static final String NAME = "test-named-writeable"; + + private final String field1; + private final String field2; + + TestNamedWriteable(String field1, String field2) { + this.field1 = field1; + this.field2 = field2; + } + + @Override + public String getWriteableName() { + return NAME; + } + + @Override + public void writeTo(StreamOutput out) throws IOException { + out.writeString(field1); + out.writeString(field2); + } + + @Override + public TestNamedWriteable readFrom(StreamInput in) throws IOException { + return new TestNamedWriteable(in.readString(), in.readString()); + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + TestNamedWriteable that = (TestNamedWriteable) o; + return Objects.equals(field1, that.field1) && + Objects.equals(field2, that.field2); + } + + @Override + public int hashCode() { + return Objects.hash(field1, field2); } } diff --git a/core/src/test/java/org/elasticsearch/index/query/AndQueryBuilderTest.java b/core/src/test/java/org/elasticsearch/index/query/AndQueryBuilderTest.java index 5b7289f793b..3944ddc6e16 100644 --- a/core/src/test/java/org/elasticsearch/index/query/AndQueryBuilderTest.java +++ b/core/src/test/java/org/elasticsearch/index/query/AndQueryBuilderTest.java @@ -93,8 +93,8 @@ public class AndQueryBuilderTest extends BaseQueryTestCase { String queryString = "{ \"and\" : {}"; XContentParser parser = XContentFactory.xContent(queryString).createParser(queryString); context.reset(parser); - assertQueryHeader(parser, AndQueryBuilder.PROTOTYPE.getName()); - context.queryParser(AndQueryBuilder.PROTOTYPE.getName()).fromXContent(context); + assertQueryHeader(parser, AndQueryBuilder.NAME); + context.queryParser(AndQueryBuilder.NAME).fromXContent(context); } @Test diff --git a/core/src/test/java/org/elasticsearch/index/query/BaseQueryTestCase.java b/core/src/test/java/org/elasticsearch/index/query/BaseQueryTestCase.java index 249d8341d9e..e04201f2530 100644 --- a/core/src/test/java/org/elasticsearch/index/query/BaseQueryTestCase.java +++ b/core/src/test/java/org/elasticsearch/index/query/BaseQueryTestCase.java @@ -31,10 +31,7 @@ import org.elasticsearch.common.inject.AbstractModule; import org.elasticsearch.common.inject.Injector; import org.elasticsearch.common.inject.ModulesBuilder; import org.elasticsearch.common.inject.util.Providers; -import org.elasticsearch.common.io.stream.BytesStreamOutput; -import org.elasticsearch.common.io.stream.FilterStreamInput; -import org.elasticsearch.common.io.stream.NamedWriteableRegistry; -import org.elasticsearch.common.io.stream.StreamInput; +import org.elasticsearch.common.io.stream.*; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.SettingsModule; import org.elasticsearch.common.xcontent.XContentFactory; @@ -237,8 +234,8 @@ public abstract class BaseQueryTestCase> ext try (BytesStreamOutput output = new BytesStreamOutput()) { firstQuery.writeTo(output); - try (StreamInput in = new FilterStreamInput(StreamInput.wrap(output.bytes()), namedWriteableRegistry)) { - QueryBuilder prototype = queryParserService.queryParser(firstQuery.getName()).getBuilderPrototype(); + try (StreamInput in = new NamedWriteableAwareStreamInput(StreamInput.wrap(output.bytes()), namedWriteableRegistry)) { + QueryBuilder prototype = queryParserService.queryParser(firstQuery.getWriteableName()).getBuilderPrototype(); @SuppressWarnings("unchecked") QB secondQuery = (QB)prototype.readFrom(in); //query _name never should affect the result of toQuery, we randomly set it to make sure @@ -301,8 +298,8 @@ public abstract class BaseQueryTestCase> ext QB testQuery = createTestQueryBuilder(); try (BytesStreamOutput output = new BytesStreamOutput()) { testQuery.writeTo(output); - try (StreamInput in = new FilterStreamInput(StreamInput.wrap(output.bytes()), namedWriteableRegistry)) { - QueryBuilder prototype = queryParserService.queryParser(testQuery.getName()).getBuilderPrototype(); + try (StreamInput in = new NamedWriteableAwareStreamInput(StreamInput.wrap(output.bytes()), namedWriteableRegistry)) { + QueryBuilder prototype = queryParserService.queryParser(testQuery.getWriteableName()).getBuilderPrototype(); QueryBuilder deserializedQuery = prototype.readFrom(in); assertEquals(deserializedQuery, testQuery); assertEquals(deserializedQuery.hashCode(), testQuery.hashCode()); diff --git a/core/src/test/java/org/elasticsearch/index/query/ConstantScoreQueryBuilderTest.java b/core/src/test/java/org/elasticsearch/index/query/ConstantScoreQueryBuilderTest.java index b2391f179f8..e05eb26eac0 100644 --- a/core/src/test/java/org/elasticsearch/index/query/ConstantScoreQueryBuilderTest.java +++ b/core/src/test/java/org/elasticsearch/index/query/ConstantScoreQueryBuilderTest.java @@ -59,7 +59,7 @@ public class ConstantScoreQueryBuilderTest extends BaseQueryTestCase { String queryString = "{ \"not\" : {}"; XContentParser parser = XContentFactory.xContent(queryString).createParser(queryString); context.reset(parser); - assertQueryHeader(parser, NotQueryBuilder.PROTOTYPE.getName()); - context.queryParser(NotQueryBuilder.PROTOTYPE.getName()).fromXContent(context); + assertQueryHeader(parser, NotQueryBuilder.NAME); + context.queryParser(NotQueryBuilder.NAME).fromXContent(context); } @Test diff --git a/core/src/test/java/org/elasticsearch/index/query/OrQueryBuilderTest.java b/core/src/test/java/org/elasticsearch/index/query/OrQueryBuilderTest.java index 47ee4b5d944..efb9e643057 100644 --- a/core/src/test/java/org/elasticsearch/index/query/OrQueryBuilderTest.java +++ b/core/src/test/java/org/elasticsearch/index/query/OrQueryBuilderTest.java @@ -115,8 +115,8 @@ public class OrQueryBuilderTest extends BaseQueryTestCase { String queryString = "{ \"or\" : {}"; XContentParser parser = XContentFactory.xContent(queryString).createParser(queryString); context.reset(parser); - assertQueryHeader(parser, OrQueryBuilder.PROTOTYPE.getName()); - context.queryParser(OrQueryBuilder.PROTOTYPE.getName()).fromXContent(context); + assertQueryHeader(parser, OrQueryBuilder.NAME); + context.queryParser(OrQueryBuilder.NAME).fromXContent(context); } @Test diff --git a/core/src/test/java/org/elasticsearch/index/query/QueryFilterBuilderTest.java b/core/src/test/java/org/elasticsearch/index/query/QueryFilterBuilderTest.java index 9be40f2f61d..6e3b7d8da3c 100644 --- a/core/src/test/java/org/elasticsearch/index/query/QueryFilterBuilderTest.java +++ b/core/src/test/java/org/elasticsearch/index/query/QueryFilterBuilderTest.java @@ -68,8 +68,8 @@ public class QueryFilterBuilderTest extends BaseQueryTestCaseof(), version0); serviceB = build( Settings.builder().put("name", "TS_B", TransportService.SETTING_TRACE_LOG_INCLUDE, "", TransportService.SETTING_TRACE_LOG_EXCLUDE, "NOTHING").build(), - version1, - namedWriteableRegistry + version1, new NamedWriteableRegistry() ); nodeB = new DiscoveryNode("TS_B", "TS_B", serviceB.boundAddress().publishAddress(), ImmutableMap.of(), version1); diff --git a/core/src/test/java/org/elasticsearch/transport/netty/NettyTransportIT.java b/core/src/test/java/org/elasticsearch/transport/netty/NettyTransportIT.java index 8fd2d3038e5..d440ae7c52e 100644 --- a/core/src/test/java/org/elasticsearch/transport/netty/NettyTransportIT.java +++ b/core/src/test/java/org/elasticsearch/transport/netty/NettyTransportIT.java @@ -35,7 +35,10 @@ import org.elasticsearch.common.util.BigArrays; import org.elasticsearch.common.util.concurrent.AbstractRunnable; import org.elasticsearch.test.ESIntegTestCase; import org.elasticsearch.threadpool.ThreadPool; -import org.elasticsearch.transport.*; +import org.elasticsearch.transport.ActionNotFoundTransportException; +import org.elasticsearch.transport.RequestHandlerRegistry; +import org.elasticsearch.transport.TransportModule; +import org.elasticsearch.transport.TransportRequest; import org.jboss.netty.channel.Channel; import org.jboss.netty.channel.ChannelPipeline; import org.jboss.netty.channel.ChannelPipelineFactory; diff --git a/core/src/test/java/org/elasticsearch/transport/netty/NettyTransportMultiPortTests.java b/core/src/test/java/org/elasticsearch/transport/netty/NettyTransportMultiPortTests.java index 26d02c60d2c..704dbe9e85e 100644 --- a/core/src/test/java/org/elasticsearch/transport/netty/NettyTransportMultiPortTests.java +++ b/core/src/test/java/org/elasticsearch/transport/netty/NettyTransportMultiPortTests.java @@ -20,7 +20,6 @@ package org.elasticsearch.transport.netty; import com.carrotsearch.hppc.IntHashSet; import com.google.common.base.Charsets; - import org.elasticsearch.Version; import org.elasticsearch.cache.recycler.PageCacheRecycler; import org.elasticsearch.common.component.Lifecycle;