Merge branch 'merge/named_writeable' into feature/query-refactoring

This commit is contained in:
javanna 2015-08-07 10:49:02 +02:00 committed by Luca Cavanna
commit d79faab0f5
82 changed files with 423 additions and 236 deletions

View File

@ -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);
}
/**

View File

@ -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);
}
}

View File

@ -29,5 +29,5 @@ public interface NamedWriteable<T> extends Writeable<T> {
/**
* Returns the name of the writeable object
*/
String getName();
String getWriteableName();
}

View File

@ -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> C readNamedWriteable(Class<C> categoryClass) throws IOException {
String name = readString();
NamedWriteable<? extends C> namedWriteable = namedWriteableRegistry.getPrototype(categoryClass, name);
return namedWriteable.readFrom(this);
}
}

View File

@ -28,28 +28,57 @@ import java.util.Map;
*/
public class NamedWriteableRegistry {
private Map<String, NamedWriteable> registry = new HashMap<>();
private final Map<Class<?>, 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 <T> void registerPrototype(Class<T> categoryClass, NamedWriteable<? extends T> namedWriteable) {
@SuppressWarnings("unchecked")
InnerRegistry<T> innerRegistry = (InnerRegistry<T>)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 <C> NamedWriteable<C> getPrototype(String name) {
public synchronized <T> NamedWriteable<? extends T> getPrototype(Class<T> categoryClass, String name) {
@SuppressWarnings("unchecked")
NamedWriteable<C> namedWriteable = (NamedWriteable<C>)registry.get(name);
if (namedWriteable == null) {
throw new IllegalArgumentException("unknown named writeable with name [" + name + "]");
InnerRegistry<T> innerRegistry = (InnerRegistry<T>)registry.get(categoryClass);
if (innerRegistry == null) {
throw new IllegalArgumentException("unknown named writeable category [" + categoryClass.getName() + "]");
}
return innerRegistry.getPrototype(name);
}
private static class InnerRegistry<T> {
private final Map<String, NamedWriteable<? extends T>> registry = new HashMap<>();
private final Class<T> categoryClass;
private InnerRegistry(Class<T> categoryClass) {
this.categoryClass = categoryClass;
}
private void registerPrototype(NamedWriteable<? extends T> namedWriteable) {
NamedWriteable<? extends T> 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<? extends T> getPrototype(String name) {
NamedWriteable<? extends T> namedWriteable = registry.get(name);
if (namedWriteable == null) {
throw new IllegalArgumentException("unknown named writeable with name [" + name + "] within category [" + categoryClass.getName() + "]");
}
return namedWriteable;
}
return namedWriteable;
}
}

View File

@ -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> C readNamedWriteable() throws IOException {
String name = readString();
NamedWriteable<C> namedWriteable = namedWriteableRegistry.getPrototype(name);
return namedWriteable.readFrom(this);
<C> C readNamedWriteable(@SuppressWarnings("unused") Class<C> 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 <C> List<C> readNamedWriteableList() throws IOException {
List<C> 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));
}
}

View File

@ -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<? extends NamedWriteable> list) throws IOException {
writeInt(list.size());
for (NamedWriteable obj : list) {
writeNamedWriteable(obj);
}
public void writeQuery(QueryBuilder queryBuilder) throws IOException {
writeNamedWriteable(queryBuilder);
}
}

View File

@ -257,4 +257,42 @@ public abstract class AbstractQueryBuilder<QB extends 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<? extends QueryBuilder> queries) throws IOException {
out.writeVInt(queries.size());
for (QueryBuilder query : queries) {
out.writeQuery(query);
}
}
protected final List<QueryBuilder> readQueries(StreamInput in) throws IOException {
List<QueryBuilder> 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;
}
}

View File

@ -111,7 +111,7 @@ public class AndQueryBuilder extends AbstractQueryBuilder<AndQueryBuilder> {
}
@Override
public String getName() {
public String getWriteableName() {
return NAME;
}
@ -128,7 +128,7 @@ public class AndQueryBuilder extends AbstractQueryBuilder<AndQueryBuilder> {
@Override
protected AndQueryBuilder doReadFrom(StreamInput in) throws IOException {
AndQueryBuilder andQueryBuilder = new AndQueryBuilder();
List<QueryBuilder> queryBuilders = in.readNamedWriteableList();
List<QueryBuilder> queryBuilders = readQueries(in);
for (QueryBuilder queryBuilder : queryBuilders) {
andQueryBuilder.add(queryBuilder);
}
@ -138,6 +138,6 @@ public class AndQueryBuilder extends AbstractQueryBuilder<AndQueryBuilder> {
@Override
protected void doWriteTo(StreamOutput out) throws IOException {
out.writeNamedWriteableList(filters);
writeQueries(out, filters);
}
}

View File

@ -249,7 +249,7 @@ public class BoolQueryBuilder extends AbstractQueryBuilder<BoolQueryBuilder> {
}
@Override
public String getName() {
public String getWriteableName() {
return NAME;
}
@ -308,13 +308,13 @@ public class BoolQueryBuilder extends AbstractQueryBuilder<BoolQueryBuilder> {
@Override
protected BoolQueryBuilder doReadFrom(StreamInput in) throws IOException {
BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
List<QueryBuilder> queryBuilders = in.readNamedWriteableList();
List<QueryBuilder> 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<BoolQueryBuilder> {
@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);

View File

@ -124,7 +124,7 @@ public class BoostingQueryBuilder extends AbstractQueryBuilder<BoostingQueryBuil
}
@Override
public String getName() {
public String getWriteableName() {
return NAME;
}
@ -155,8 +155,8 @@ public class BoostingQueryBuilder extends AbstractQueryBuilder<BoostingQueryBuil
@Override
protected BoostingQueryBuilder doReadFrom(StreamInput in) throws IOException {
QueryBuilder positiveQuery = in.readNamedWriteable();
QueryBuilder negativeQuery = in.readNamedWriteable();
QueryBuilder positiveQuery = in.readQuery();
QueryBuilder negativeQuery = in.readQuery();
BoostingQueryBuilder boostingQuery = new BoostingQueryBuilder(positiveQuery, negativeQuery);
boostingQuery.negativeBoost = in.readFloat();
return boostingQuery;
@ -164,8 +164,8 @@ public class BoostingQueryBuilder extends AbstractQueryBuilder<BoostingQueryBuil
@Override
protected void doWriteTo(StreamOutput out) throws IOException {
out.writeNamedWriteable(positiveQuery);
out.writeNamedWriteable(negativeQuery);
out.writeQuery(positiveQuery);
out.writeQuery(negativeQuery);
out.writeFloat(negativeBoost);
}
}

View File

@ -223,7 +223,7 @@ public class CommonTermsQueryBuilder extends AbstractQueryBuilder<CommonTermsQue
}
@Override
public String getName() {
public String getWriteableName() {
return NAME;
}

View File

@ -88,7 +88,7 @@ public class ConstantScoreQueryBuilder extends AbstractQueryBuilder<ConstantScor
}
@Override
public String getName() {
public String getWriteableName() {
return NAME;
}
@ -104,12 +104,12 @@ public class ConstantScoreQueryBuilder extends AbstractQueryBuilder<ConstantScor
@Override
protected ConstantScoreQueryBuilder doReadFrom(StreamInput in) throws IOException {
QueryBuilder innerFilterBuilder = in.readNamedWriteable();
QueryBuilder innerFilterBuilder = in.readQuery();
return new ConstantScoreQueryBuilder(innerFilterBuilder);
}
@Override
protected void doWriteTo(StreamOutput out) throws IOException {
out.writeNamedWriteable(filterBuilder);
out.writeQuery(filterBuilder);
}
}

View File

@ -114,7 +114,7 @@ public class DisMaxQueryBuilder extends AbstractQueryBuilder<DisMaxQueryBuilder>
@Override
protected DisMaxQueryBuilder doReadFrom(StreamInput in) throws IOException {
DisMaxQueryBuilder disMax = new DisMaxQueryBuilder();
List<QueryBuilder> queryBuilders = in.readNamedWriteableList();
List<QueryBuilder> queryBuilders = readQueries(in);
disMax.queries.addAll(queryBuilders);
disMax.tieBreaker = in.readFloat();
return disMax;
@ -122,7 +122,7 @@ public class DisMaxQueryBuilder extends AbstractQueryBuilder<DisMaxQueryBuilder>
@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<DisMaxQueryBuilder>
}
@Override
public String getName() {
public String getWriteableName() {
return NAME;
}
}

View File

@ -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();

View File

@ -138,7 +138,7 @@ public class ExistsQueryBuilder extends AbstractQueryBuilder<ExistsQueryBuilder>
}
@Override
public String getName() {
public String getWriteableName() {
return NAME;
}
}

View File

@ -95,18 +95,18 @@ public class FQueryFilterBuilder extends AbstractQueryBuilder<FQueryFilterBuilde
@Override
protected FQueryFilterBuilder doReadFrom(StreamInput in) throws IOException {
QueryBuilder innerQueryBuilder = in.readNamedWriteable();
QueryBuilder innerQueryBuilder = in.readQuery();
FQueryFilterBuilder fquery = new FQueryFilterBuilder(innerQueryBuilder);
return fquery;
}
@Override
protected void doWriteTo(StreamOutput out) throws IOException {
out.writeNamedWriteable(queryBuilder);
out.writeQuery(queryBuilder);
}
@Override
public String getName() {
public String getWriteableName() {
return NAME;
}
}

View File

@ -103,13 +103,13 @@ public class FieldMaskingSpanQueryBuilder extends AbstractQueryBuilder<FieldMask
@Override
protected FieldMaskingSpanQueryBuilder doReadFrom(StreamInput in) throws IOException {
QueryBuilder innerQueryBuilder = in.readNamedWriteable();
QueryBuilder innerQueryBuilder = in.readQuery();
return new FieldMaskingSpanQueryBuilder((SpanQueryBuilder) innerQueryBuilder, in.readString());
}
@Override
protected void doWriteTo(StreamOutput out) throws IOException {
out.writeNamedWriteable(queryBuilder);
out.writeQuery(queryBuilder);
out.writeString(fieldName);
}
@ -125,7 +125,7 @@ public class FieldMaskingSpanQueryBuilder extends AbstractQueryBuilder<FieldMask
}
@Override
public String getName() {
public String getWriteableName() {
return NAME;
}
}

View File

@ -138,21 +138,21 @@ public class FilteredQueryBuilder extends AbstractQueryBuilder<FilteredQueryBuil
}
@Override
public String getName() {
public String getWriteableName() {
return NAME;
}
@Override
public FilteredQueryBuilder doReadFrom(StreamInput in) throws IOException {
QueryBuilder query = in.readNamedWriteable();
QueryBuilder filter = in.readNamedWriteable();
QueryBuilder query = in.readQuery();
QueryBuilder filter = in.readQuery();
FilteredQueryBuilder qb = new FilteredQueryBuilder(query, filter);
return qb;
}
@Override
public void doWriteTo(StreamOutput out) throws IOException {
out.writeNamedWriteable(queryBuilder);
out.writeNamedWriteable(filterBuilder);
out.writeQuery(queryBuilder);
out.writeQuery(filterBuilder);
}
}

View File

@ -171,7 +171,7 @@ public class FuzzyQueryBuilder extends AbstractQueryBuilder<FuzzyQueryBuilder> i
}
@Override
public String getName() {
public String getWriteableName() {
return NAME;
}
}

View File

@ -168,7 +168,7 @@ public class GeoBoundingBoxQueryBuilder extends AbstractQueryBuilder<GeoBounding
}
@Override
public String getName() {
public String getWriteableName() {
return NAME;
}
}

View File

@ -111,7 +111,7 @@ public class GeoDistanceQueryBuilder extends AbstractQueryBuilder<GeoDistanceQue
}
@Override
public String getName() {
public String getWriteableName() {
return NAME;
}
}

View File

@ -150,7 +150,7 @@ public class GeoDistanceRangeQueryBuilder extends AbstractQueryBuilder<GeoDistan
}
@Override
public String getName() {
public String getWriteableName() {
return NAME;
}
}

View File

@ -82,7 +82,7 @@ public class GeoPolygonQueryBuilder extends AbstractQueryBuilder<GeoPolygonQuery
}
@Override
public String getName() {
public String getWriteableName() {
return NAME;
}
}

View File

@ -174,7 +174,7 @@ public class GeoShapeQueryBuilder extends AbstractQueryBuilder<GeoShapeQueryBuil
}
@Override
public String getName() {
public String getWriteableName() {
return NAME;
}
}

View File

@ -169,7 +169,7 @@ public class GeohashCellQuery {
}
@Override
public String getName() {
public String getWriteableName() {
return NAME;
}
}

View File

@ -117,7 +117,7 @@ public class HasChildQueryBuilder extends AbstractQueryBuilder<HasChildQueryBuil
}
@Override
public String getName() {
public String getWriteableName() {
return NAME;
}
}

View File

@ -79,7 +79,7 @@ public class HasParentQueryBuilder extends AbstractQueryBuilder<HasParentQueryBu
}
@Override
public String getName() {
public String getWriteableName() {
return NAME;
}
}

View File

@ -119,7 +119,7 @@ public class IdsQueryBuilder extends AbstractQueryBuilder<IdsQueryBuilder> {
}
@Override
public String getName() {
public String getWriteableName() {
return NAME;
}

View File

@ -78,7 +78,7 @@ public class IndicesQueryBuilder extends AbstractQueryBuilder<IndicesQueryBuilde
}
@Override
public String getName() {
public String getWriteableName() {
return NAME;
}
}

View File

@ -87,7 +87,7 @@ public class LimitQueryBuilder extends AbstractQueryBuilder<LimitQueryBuilder> {
}
@Override
public String getName() {
public String getWriteableName() {
return NAME;
}
}

View File

@ -75,7 +75,7 @@ public class MatchAllQueryBuilder extends AbstractQueryBuilder<MatchAllQueryBuil
}
@Override
public String getName() {
public String getWriteableName() {
return NAME;
}
}

View File

@ -249,7 +249,7 @@ public class MatchQueryBuilder extends AbstractQueryBuilder<MatchQueryBuilder> {
}
@Override
public String getName() {
public String getWriteableName() {
return NAME;
}
}

View File

@ -105,7 +105,7 @@ public class MissingQueryBuilder extends AbstractQueryBuilder<MissingQueryBuilde
}
@Override
public String getName() {
public String getWriteableName() {
return NAME;
}

View File

@ -418,7 +418,7 @@ public class MoreLikeThisQueryBuilder extends AbstractQueryBuilder<MoreLikeThisQ
}
@Override
public String getName() {
public String getWriteableName() {
return NAME;
}
}

View File

@ -380,7 +380,7 @@ public class MultiMatchQueryBuilder extends AbstractQueryBuilder<MultiMatchQuery
}
@Override
public String getName() {
public String getWriteableName() {
return NAME;
}
}

View File

@ -87,7 +87,7 @@ public class NestedQueryBuilder extends AbstractQueryBuilder<NestedQueryBuilder>
}
@Override
public final String getName() {
public final String getWriteableName() {
return NAME;
}
}

View File

@ -85,17 +85,17 @@ public class NotQueryBuilder extends AbstractQueryBuilder<NotQueryBuilder> {
@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;
}
}

View File

@ -108,7 +108,7 @@ public class OrQueryBuilder extends AbstractQueryBuilder<OrQueryBuilder> {
}
@Override
public String getName() {
public String getWriteableName() {
return NAME;
}
@ -125,7 +125,7 @@ public class OrQueryBuilder extends AbstractQueryBuilder<OrQueryBuilder> {
@Override
protected OrQueryBuilder doReadFrom(StreamInput in) throws IOException {
OrQueryBuilder orQueryBuilder = new OrQueryBuilder();
List<QueryBuilder> queryBuilders = in.readNamedWriteableList();
List<QueryBuilder> queryBuilders = readQueries(in);
for (QueryBuilder queryBuilder : queryBuilders) {
orQueryBuilder.add(queryBuilder);
}
@ -135,6 +135,6 @@ public class OrQueryBuilder extends AbstractQueryBuilder<OrQueryBuilder> {
@Override
protected void doWriteTo(StreamOutput out) throws IOException {
out.writeNamedWriteableList(filters);
writeQueries(out, filters);
}
}

View File

@ -91,7 +91,7 @@ public class PrefixQueryBuilder extends AbstractQueryBuilder<PrefixQueryBuilder>
}
@Override
public String getName() {
public String getWriteableName() {
return NAME;
}

View File

@ -58,12 +58,12 @@ public interface QueryBuilder<QB extends QueryBuilder> extends NamedWriteable<QB
BytesReference buildAsBytes();
/**
* Sets the query name for the query.
* Sets the arbitrary name to be assigned to the query (see named queries).
*/
QB queryName(String queryName);
/**
* Returns the query name for the query.
* Returns the arbitrary name assigned to the query (see named queries).
*/
String queryName();
@ -77,4 +77,9 @@ public interface QueryBuilder<QB extends QueryBuilder> extends NamedWriteable<QB
* weightings) have their score multiplied by the boost provided.
*/
QB boost(float boost);
/**
* Returns the name that identifies uniquely the query
*/
String getName();
}

View File

@ -91,17 +91,17 @@ public class QueryFilterBuilder extends AbstractQueryBuilder<QueryFilterBuilder>
@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;
}
}

View File

@ -406,7 +406,7 @@ public class QueryStringQueryBuilder extends AbstractQueryBuilder<QueryStringQue
}
@Override
public String getName() {
public String getWriteableName() {
return NAME;
}
}

View File

@ -52,7 +52,7 @@ public class QueryWrappingQueryBuilder extends AbstractQueryBuilder<QueryWrappin
}
@Override
public String getName() {
public String getWriteableName() {
// this should not be called since we overwrite BaseQueryBuilder#toQuery() in this class
throw new UnsupportedOperationException();
}

View File

@ -238,7 +238,7 @@ public class RangeQueryBuilder extends AbstractQueryBuilder<RangeQueryBuilder> i
}
@Override
public String getName() {
public String getWriteableName() {
return NAME;
}

View File

@ -144,7 +144,7 @@ public class RegexpQueryBuilder extends AbstractQueryBuilder<RegexpQueryBuilder>
}
@Override
public String getName() {
public String getWriteableName() {
return NAME;
}

View File

@ -52,7 +52,7 @@ public class ScriptQueryBuilder extends AbstractQueryBuilder<ScriptQueryBuilder>
}
@Override
public String getName() {
public String getWriteableName() {
return NAME;
}

View File

@ -349,7 +349,7 @@ public class SimpleQueryStringBuilder extends AbstractQueryBuilder<SimpleQuerySt
}
@Override
public String getName() {
public String getWriteableName() {
return NAME;
}

View File

@ -100,15 +100,15 @@ public class SpanContainingQueryBuilder extends AbstractQueryBuilder<SpanContain
@Override
protected SpanContainingQueryBuilder doReadFrom(StreamInput in) throws IOException {
SpanQueryBuilder big = in.readNamedWriteable();
SpanQueryBuilder little = in.readNamedWriteable();
SpanQueryBuilder big = (SpanQueryBuilder)in.readQuery();
SpanQueryBuilder little = (SpanQueryBuilder)in.readQuery();
return new SpanContainingQueryBuilder(big, little);
}
@Override
protected void doWriteTo(StreamOutput out) throws IOException {
out.writeNamedWriteable(big);
out.writeNamedWriteable(little);
out.writeQuery(big);
out.writeQuery(little);
}
@Override
@ -123,7 +123,7 @@ public class SpanContainingQueryBuilder extends AbstractQueryBuilder<SpanContain
}
@Override
public String getName() {
public String getWriteableName() {
return NAME;
}
}

View File

@ -98,14 +98,14 @@ public class SpanFirstQueryBuilder extends AbstractQueryBuilder<SpanFirstQueryBu
@Override
protected SpanFirstQueryBuilder doReadFrom(StreamInput in) throws IOException {
SpanQueryBuilder matchBuilder = in.readNamedWriteable();
SpanQueryBuilder matchBuilder = (SpanQueryBuilder)in.readQuery();
int end = in.readInt();
return new SpanFirstQueryBuilder(matchBuilder, end);
}
@Override
protected void doWriteTo(StreamOutput out) throws IOException {
out.writeNamedWriteable(matchBuilder);
out.writeQuery(matchBuilder);
out.writeInt(end);
}
@ -121,7 +121,7 @@ public class SpanFirstQueryBuilder extends AbstractQueryBuilder<SpanFirstQueryBu
}
@Override
public String getName() {
public String getWriteableName() {
return NAME;
}
}

View File

@ -79,13 +79,13 @@ public class SpanMultiTermQueryBuilder extends AbstractQueryBuilder<SpanMultiTer
@Override
protected SpanMultiTermQueryBuilder doReadFrom(StreamInput in) throws IOException {
MultiTermQueryBuilder multiTermBuilder = in.readNamedWriteable();
MultiTermQueryBuilder multiTermBuilder = (MultiTermQueryBuilder)in.readQuery();
return new SpanMultiTermQueryBuilder(multiTermBuilder);
}
@Override
protected void doWriteTo(StreamOutput out) throws IOException {
out.writeNamedWriteable(multiTermQueryBuilder);
out.writeQuery(multiTermQueryBuilder);
}
@Override
@ -99,7 +99,7 @@ public class SpanMultiTermQueryBuilder extends AbstractQueryBuilder<SpanMultiTer
}
@Override
public String getName() {
public String getWriteableName() {
return NAME;
}
}

View File

@ -46,7 +46,7 @@ public class SpanNearQueryBuilder extends AbstractQueryBuilder<SpanNearQueryBuil
/** Default for flag controlling whether payloads are collected */
public static boolean DEFAULT_COLLECT_PAYLOADS = true;
private final ArrayList<SpanQueryBuilder> clauses = new ArrayList<>();
private final List<SpanQueryBuilder> clauses = new ArrayList<>();
private final int slop;
@ -159,9 +159,9 @@ public class SpanNearQueryBuilder extends AbstractQueryBuilder<SpanNearQueryBuil
@Override
protected SpanNearQueryBuilder doReadFrom(StreamInput in) throws IOException {
SpanNearQueryBuilder queryBuilder = new SpanNearQueryBuilder(in.readVInt());
List<SpanQueryBuilder> clauses = in.readNamedWriteableList();
for (SpanQueryBuilder subClause : clauses) {
queryBuilder.clause(subClause);
List<QueryBuilder> 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<SpanNearQueryBuil
@Override
protected void doWriteTo(StreamOutput out) throws IOException {
out.writeVInt(slop);
out.writeNamedWriteableList(clauses);
writeQueries(out, clauses);
out.writeBoolean(collectPayloads);
out.writeBoolean(inOrder);
}
@ -191,7 +191,7 @@ public class SpanNearQueryBuilder extends AbstractQueryBuilder<SpanNearQueryBuil
}
@Override
public String getName() {
public String getWriteableName() {
return NAME;
}
}

View File

@ -159,8 +159,8 @@ public class SpanNotQueryBuilder extends AbstractQueryBuilder<SpanNotQueryBuilde
@Override
protected SpanNotQueryBuilder doReadFrom(StreamInput in) throws IOException {
SpanQueryBuilder include = in.readNamedWriteable();
SpanQueryBuilder exclude = in.readNamedWriteable();
SpanQueryBuilder include = (SpanQueryBuilder)in.readQuery();
SpanQueryBuilder exclude = (SpanQueryBuilder)in.readQuery();
SpanNotQueryBuilder queryBuilder = new SpanNotQueryBuilder(include, exclude);
queryBuilder.pre(in.readVInt());
queryBuilder.post(in.readVInt());
@ -169,8 +169,8 @@ public class SpanNotQueryBuilder extends AbstractQueryBuilder<SpanNotQueryBuilde
@Override
protected void doWriteTo(StreamOutput out) throws IOException {
out.writeNamedWriteable(include);
out.writeNamedWriteable(exclude);
out.writeQuery(include);
out.writeQuery(exclude);
out.writeVInt(pre);
out.writeVInt(post);
}
@ -189,7 +189,7 @@ public class SpanNotQueryBuilder extends AbstractQueryBuilder<SpanNotQueryBuilde
}
@Override
public String getName() {
public String getWriteableName() {
return NAME;
}
}

View File

@ -38,7 +38,7 @@ public class SpanOrQueryBuilder extends AbstractQueryBuilder<SpanOrQueryBuilder>
public static final String NAME = "span_or";
private final ArrayList<SpanQueryBuilder> clauses = new ArrayList<>();
private final List<SpanQueryBuilder> clauses = new ArrayList<>();
static final SpanOrQueryBuilder PROTOTYPE = new SpanOrQueryBuilder();
@ -96,9 +96,9 @@ public class SpanOrQueryBuilder extends AbstractQueryBuilder<SpanOrQueryBuilder>
@Override
protected SpanOrQueryBuilder doReadFrom(StreamInput in) throws IOException {
SpanOrQueryBuilder queryBuilder = new SpanOrQueryBuilder();
List<SpanQueryBuilder> clauses = in.readNamedWriteableList();
for (SpanQueryBuilder subClause : clauses) {
queryBuilder.clause(subClause);
List<QueryBuilder> clauses = readQueries(in);
for (QueryBuilder subClause : clauses) {
queryBuilder.clauses.add((SpanQueryBuilder)subClause);
}
return queryBuilder;
@ -106,7 +106,7 @@ public class SpanOrQueryBuilder extends AbstractQueryBuilder<SpanOrQueryBuilder>
@Override
protected void doWriteTo(StreamOutput out) throws IOException {
out.writeNamedWriteableList(clauses);
writeQueries(out, clauses);
}
@Override
@ -120,7 +120,7 @@ public class SpanOrQueryBuilder extends AbstractQueryBuilder<SpanOrQueryBuilder>
}
@Override
public String getName() {
public String getWriteableName() {
return NAME;
}
}

View File

@ -88,7 +88,7 @@ public class SpanTermQueryBuilder extends BaseTermQueryBuilder<SpanTermQueryBuil
}
@Override
public String getName() {
public String getWriteableName() {
return NAME;
}
}

View File

@ -105,15 +105,15 @@ public class SpanWithinQueryBuilder extends AbstractQueryBuilder<SpanWithinQuery
@Override
protected SpanWithinQueryBuilder doReadFrom(StreamInput in) throws IOException {
SpanQueryBuilder big = in.readNamedWriteable();
SpanQueryBuilder little = in.readNamedWriteable();
SpanQueryBuilder big = (SpanQueryBuilder)in.readQuery();
SpanQueryBuilder little = (SpanQueryBuilder)in.readQuery();
return new SpanWithinQueryBuilder(big, little);
}
@Override
protected void doWriteTo(StreamOutput out) throws IOException {
out.writeNamedWriteable(big);
out.writeNamedWriteable(little);
out.writeQuery(big);
out.writeQuery(little);
}
@Override
@ -128,7 +128,7 @@ public class SpanWithinQueryBuilder extends AbstractQueryBuilder<SpanWithinQuery
}
@Override
public String getName() {
public String getWriteableName() {
return NAME;
}
}

View File

@ -91,7 +91,7 @@ public class TemplateQueryBuilder extends AbstractQueryBuilder<TemplateQueryBuil
}
@Override
public String getName() {
public String getWriteableName() {
return NAME;
}
}

View File

@ -89,7 +89,7 @@ public class TermQueryBuilder extends BaseTermQueryBuilder<TermQueryBuilder> {
}
@Override
public String getName() {
public String getWriteableName() {
return NAME;
}
}

View File

@ -32,7 +32,7 @@ public class TermsLookupQueryBuilder extends TermsQueryBuilder {
}
@Override
public String getName() {
public String getWriteableName() {
return TermsQueryBuilder.NAME;
}
}

View File

@ -204,7 +204,7 @@ public class TermsQueryBuilder extends AbstractQueryBuilder<TermsQueryBuilder> {
}
@Override
public String getName() {
public String getWriteableName() {
return NAME;
}
}

View File

@ -62,7 +62,7 @@ public class TypeQueryBuilder extends AbstractQueryBuilder<TypeQueryBuilder> {
}
@Override
public String getName() {
public String getWriteableName() {
return NAME;
}

View File

@ -88,7 +88,7 @@ public class WildcardQueryBuilder extends AbstractQueryBuilder<WildcardQueryBuil
}
@Override
public String getName() {
public String getWriteableName() {
return NAME;
}

View File

@ -83,7 +83,7 @@ public class WrapperQueryBuilder extends AbstractQueryBuilder<WrapperQueryBuilde
}
@Override
public String getName() {
public String getWriteableName() {
return NAME;
}
}

View File

@ -188,7 +188,7 @@ public class FunctionScoreQueryBuilder extends AbstractQueryBuilder<FunctionScor
}
@Override
public String getName() {
public String getWriteableName() {
return FunctionScoreQueryParser.NAME;
}
}

View File

@ -27,6 +27,7 @@ import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.index.query.EmptyQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryParser;
import java.util.Map;
@ -44,11 +45,11 @@ public class IndicesQueriesRegistry extends AbstractComponent {
for (String name : queryParser.names()) {
queryParsers.put(name, queryParser);
}
namedWriteableRegistry.registerPrototype(queryParser.getBuilderPrototype());
namedWriteableRegistry.registerPrototype(QueryBuilder.class, queryParser.getBuilderPrototype());
}
// EmptyQueryBuilder is not registered as query parser but used internally.
// We need to register it with the NamedWriteableRegistry in order to serialize it
namedWriteableRegistry.registerPrototype(EmptyQueryBuilder.PROTOTYPE);
namedWriteableRegistry.registerPrototype(QueryBuilder.class, EmptyQueryBuilder.PROTOTYPE);
this.queryParsers = ImmutableMap.copyOf(queryParsers);
}

View File

@ -31,7 +31,6 @@ import org.elasticsearch.cluster.ClusterNameModule;
import org.elasticsearch.cluster.ClusterService;
import org.elasticsearch.cluster.action.index.MappingUpdatedAction;
import org.elasticsearch.cluster.routing.RoutingService;
import org.elasticsearch.cluster.routing.allocation.AllocationService;
import org.elasticsearch.common.StopWatch;
import org.elasticsearch.common.collect.Tuple;
import org.elasticsearch.common.component.Lifecycle;
@ -85,8 +84,8 @@ import org.elasticsearch.script.ScriptModule;
import org.elasticsearch.script.ScriptService;
import org.elasticsearch.search.SearchModule;
import org.elasticsearch.search.SearchService;
import org.elasticsearch.snapshots.SnapshotsService;
import org.elasticsearch.snapshots.SnapshotShardsService;
import org.elasticsearch.snapshots.SnapshotsService;
import org.elasticsearch.threadpool.ThreadPool;
import org.elasticsearch.threadpool.ThreadPoolModule;
import org.elasticsearch.transport.TransportModule;
@ -254,7 +253,7 @@ public class Node implements Releasable {
injector.getInstance(MonitorService.class).start();
injector.getInstance(RestController.class).start();
// TODO hack around circular dependecncies problems
// TODO hack around circular dependencies problems
injector.getInstance(GatewayAllocator.class).setReallocation(injector.getInstance(ClusterService.class), injector.getInstance(RoutingService.class));
DiscoveryService discoService = injector.getInstance(DiscoveryService.class).start();

View File

@ -26,8 +26,9 @@ import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.component.AbstractLifecycleComponent;
import org.elasticsearch.common.component.Lifecycle;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.io.stream.*;
import org.elasticsearch.common.io.stream.BytesStreamOutput;
import org.elasticsearch.common.io.stream.NamedWriteableAwareStreamInput;
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.BoundTransportAddress;
@ -226,7 +227,7 @@ public class LocalTransport extends AbstractLifecycleComponent<Transport> 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<Transport> 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);

View File

@ -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);

View File

@ -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<Transport> implem
protected volatile TransportServiceAdapter transportServiceAdapter;
protected volatile BoundTransportAddress boundAddress;
protected final KeyedLock<String> 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<Transport> 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<Transport> implem
}
protected static class ClientChannelPipelineFactory implements ChannelPipelineFactory {
protected NettyTransport nettyTransport;
protected final NettyTransport nettyTransport;
public ClientChannelPipelineFactory(NettyTransport nettyTransport) {
this.nettyTransport = nettyTransport;

View File

@ -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<T> implements NamedWriteable<T> {
}
private static class TestNamedWriteable extends BaseNamedWriteable<TestNamedWriteable> {
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);
}
}

View File

@ -93,8 +93,8 @@ public class AndQueryBuilderTest extends BaseQueryTestCase<AndQueryBuilder> {
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

View File

@ -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<QB extends AbstractQueryBuilder<QB>> ext
try (BytesStreamOutput output = new BytesStreamOutput()) {
firstQuery.writeTo(output);
try (StreamInput in = new FilterStreamInput(StreamInput.wrap(output.bytes()), namedWriteableRegistry)) {
QueryBuilder<? extends QueryBuilder> prototype = queryParserService.queryParser(firstQuery.getName()).getBuilderPrototype();
try (StreamInput in = new NamedWriteableAwareStreamInput(StreamInput.wrap(output.bytes()), namedWriteableRegistry)) {
QueryBuilder<? extends 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<QB extends AbstractQueryBuilder<QB>> ext
QB testQuery = createTestQueryBuilder();
try (BytesStreamOutput output = new BytesStreamOutput()) {
testQuery.writeTo(output);
try (StreamInput in = new FilterStreamInput(StreamInput.wrap(output.bytes()), namedWriteableRegistry)) {
QueryBuilder<? extends QueryBuilder> prototype = queryParserService.queryParser(testQuery.getName()).getBuilderPrototype();
try (StreamInput in = new NamedWriteableAwareStreamInput(StreamInput.wrap(output.bytes()), namedWriteableRegistry)) {
QueryBuilder<? extends QueryBuilder> prototype = queryParserService.queryParser(testQuery.getWriteableName()).getBuilderPrototype();
QueryBuilder deserializedQuery = prototype.readFrom(in);
assertEquals(deserializedQuery, testQuery);
assertEquals(deserializedQuery.hashCode(), testQuery.hashCode());

View File

@ -59,7 +59,7 @@ public class ConstantScoreQueryBuilderTest extends BaseQueryTestCase<ConstantSco
@Test(expected=QueryParsingException.class)
public void testFilterElement() throws IOException {
QueryParseContext context = createParseContext();
String queryId = ConstantScoreQueryBuilder.PROTOTYPE.getName();
String queryId = ConstantScoreQueryBuilder.NAME;
String queryString = "{ \""+queryId+"\" : {}";
XContentParser parser = XContentFactory.xContent(queryString).createParser(queryString);
context.reset(parser);

View File

@ -88,7 +88,7 @@ public class DisMaxQueryBuilderTest extends BaseQueryTestCase<DisMaxQueryBuilder
@Test
public void testInnerQueryReturnsNull() throws IOException {
QueryParseContext context = createParseContext();
String queryId = ConstantScoreQueryBuilder.PROTOTYPE.getName();
String queryId = ConstantScoreQueryBuilder.NAME;
String queryString = "{ \""+queryId+"\" : { \"filter\" : { } }";
XContentParser parser = XContentFactory.xContent(queryString).createParser(queryString);
context.reset(parser);

View File

@ -75,8 +75,8 @@ public class FQueryFilterBuilderTest extends BaseQueryTestCase<FQueryFilterBuild
String queryString = "{ \"constant_score\" : { \"filter\" : {} }";
XContentParser parser = XContentFactory.xContent(queryString).createParser(queryString);
context.reset(parser);
assertQueryHeader(parser, ConstantScoreQueryBuilder.PROTOTYPE.getName());
QueryBuilder innerQuery = context.queryParser(ConstantScoreQueryBuilder.PROTOTYPE.getName()).fromXContent(context);
assertQueryHeader(parser, ConstantScoreQueryBuilder.NAME);
QueryBuilder innerQuery = context.queryParser(ConstantScoreQueryBuilder.NAME).fromXContent(context);
// check that when wrapping this filter, toQuery() returns null
FQueryFilterBuilder queryFilterQuery = new FQueryFilterBuilder(innerQuery);

View File

@ -68,8 +68,8 @@ public class NotQueryBuilderTest extends BaseQueryTestCase<NotQueryBuilder> {
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

View File

@ -115,8 +115,8 @@ public class OrQueryBuilderTest extends BaseQueryTestCase<OrQueryBuilder> {
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

View File

@ -68,8 +68,8 @@ public class QueryFilterBuilderTest extends BaseQueryTestCase<QueryFilterBuilder
String queryString = "{ \"constant_score\" : { \"filter\" : {} }";
XContentParser parser = XContentFactory.xContent(queryString).createParser(queryString);
context.reset(parser);
assertQueryHeader(parser, ConstantScoreQueryBuilder.PROTOTYPE.getName());
QueryBuilder innerQuery = context.queryParser(ConstantScoreQueryBuilder.PROTOTYPE.getName()).fromXContent(context);
assertQueryHeader(parser, ConstantScoreQueryBuilder.NAME);
QueryBuilder innerQuery = context.queryParser(ConstantScoreQueryBuilder.NAME).fromXContent(context);
// check that when wrapping this filter, toQuery() returns null
QueryFilterBuilder queryFilterQuery = new QueryFilterBuilder(innerQuery);

View File

@ -66,7 +66,7 @@ public class DummyQueryParserPlugin extends AbstractPlugin {
}
@Override
public String getName() {
public String getWriteableName() {
return NAME;
}
}

View File

@ -70,14 +70,12 @@ public abstract class AbstractSimpleTransportTests extends ESTestCase {
threadPool = new ThreadPool(getClass().getName());
serviceA = build(
Settings.builder().put("name", "TS_A", TransportService.SETTING_TRACE_LOG_INCLUDE, "", TransportService.SETTING_TRACE_LOG_EXCLUDE, "NOTHING").build(),
version0,
namedWriteableRegistry
version0, new NamedWriteableRegistry()
);
nodeA = new DiscoveryNode("TS_A", "TS_A", serviceA.boundAddress().publishAddress(), ImmutableMap.<String, String>of(), 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.<String, String>of(), version1);

View File

@ -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;

View File

@ -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;