diff --git a/src/main/java/org/elasticsearch/common/io/stream/StreamInput.java b/src/main/java/org/elasticsearch/common/io/stream/StreamInput.java index 297963a35e7..09a5cbae651 100644 --- a/src/main/java/org/elasticsearch/common/io/stream/StreamInput.java +++ b/src/main/java/org/elasticsearch/common/io/stream/StreamInput.java @@ -224,16 +224,14 @@ public abstract class StreamInput extends InputStream { // return len; // } - public @Nullable - Map readMap() throws IOException { - return (Map) readFieldValue(); + public Map readMap() throws IOException { + return (Map) readGenericValue(); } @SuppressWarnings({"unchecked"}) - private @Nullable - Object readFieldValue() throws IOException { + public Object readGenericValue() throws IOException { byte type = readByte(); if (type == -1) { return null; @@ -258,14 +256,14 @@ public abstract class StreamInput extends InputStream { int size = readVInt(); List list = new ArrayList(size); for (int i = 0; i < size; i++) { - list.add(readFieldValue()); + list.add(readGenericValue()); } return list; } else if (type == 8) { int size = readVInt(); Object[] list = new Object[size]; for (int i = 0; i < size; i++) { - list[i] = readFieldValue(); + list[i] = readGenericValue(); } return list; } else if (type == 9 || type == 10) { @@ -277,9 +275,11 @@ public abstract class StreamInput extends InputStream { map = new HashMap(size); } for (int i = 0; i < size; i++) { - map.put(readUTF(), readFieldValue()); + map.put(readUTF(), readGenericValue()); } return map; + } else if (type == 11) { + return readByte(); } else { throw new IOException("Can't read unknown type [" + type + "]"); } diff --git a/src/main/java/org/elasticsearch/common/io/stream/StreamOutput.java b/src/main/java/org/elasticsearch/common/io/stream/StreamOutput.java index 9e5f7169b0a..0e92e4957ab 100644 --- a/src/main/java/org/elasticsearch/common/io/stream/StreamOutput.java +++ b/src/main/java/org/elasticsearch/common/io/stream/StreamOutput.java @@ -202,10 +202,10 @@ public abstract class StreamOutput extends OutputStream { } public void writeMap(@Nullable Map map) throws IOException { - writeValue(map); + writeGenericValue(map); } - private void writeValue(@Nullable Object value) throws IOException { + public void writeGenericValue(@Nullable Object value) throws IOException { if (value == null) { writeByte((byte) -1); return; @@ -238,14 +238,14 @@ public abstract class StreamOutput extends OutputStream { List list = (List) value; writeVInt(list.size()); for (Object o : list) { - writeValue(o); + writeGenericValue(o); } } else if (value instanceof Object[]) { writeByte((byte) 8); Object[] list = (Object[]) value; writeVInt(list.length); for (Object o : list) { - writeValue(o); + writeGenericValue(o); } } else if (value instanceof Map) { if (value instanceof LinkedHashMap) { @@ -257,8 +257,11 @@ public abstract class StreamOutput extends OutputStream { writeVInt(map.size()); for (Map.Entry entry : map.entrySet()) { writeUTF(entry.getKey()); - writeValue(entry.getValue()); + writeGenericValue(entry.getValue()); } + } else if (type == Byte.class) { + writeByte((byte) 11); + writeByte((Byte) value); } else { throw new IOException("Can't write type [" + type + "]"); } diff --git a/src/main/java/org/elasticsearch/common/lucene/Lucene.java b/src/main/java/org/elasticsearch/common/lucene/Lucene.java index 48a8b6db8a7..a3f6b980bd5 100644 --- a/src/main/java/org/elasticsearch/common/lucene/Lucene.java +++ b/src/main/java/org/elasticsearch/common/lucene/Lucene.java @@ -34,10 +34,6 @@ import org.elasticsearch.index.field.data.FieldDataType; import java.io.IOException; import java.lang.reflect.Field; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; /** * @@ -298,110 +294,6 @@ public class Lucene { } } - @SuppressWarnings({"unchecked"}) - public static Object readFieldValue(StreamInput in) throws IOException { - byte type = in.readByte(); - if (type == -1) { - return null; - } else if (type == 0) { - return in.readUTF(); - } else if (type == 1) { - return in.readInt(); - } else if (type == 2) { - return in.readLong(); - } else if (type == 3) { - return in.readFloat(); - } else if (type == 4) { - return in.readDouble(); - } else if (type == 5) { - return in.readBoolean(); - } else if (type == 6) { - int bytesSize = in.readVInt(); - byte[] value = new byte[bytesSize]; - in.readFully(value); - return value; - } else if (type == 7) { - int size = in.readVInt(); - List list = new ArrayList(size); - for (int i = 0; i < size; i++) { - list.add(readFieldValue(in)); - } - return list; - } else if (type == 8) { - int size = in.readVInt(); - Object[] list = new Object[size]; - for (int i = 0; i < size; i++) { - list[i] = readFieldValue(in); - } - return list; - } else if (type == 9) { - int size = in.readVInt(); - Map map = new HashMap(size); - for (int i = 0; i < size; i++) { - map.put(in.readUTF(), readFieldValue(in)); - } - return map; - } else { - throw new IOException("Can't read unknown type [" + type + "]"); - } - } - - @SuppressWarnings({"unchecked"}) - public static void writeFieldValue(StreamOutput out, Object value) throws IOException { - if (value == null) { - out.writeByte((byte) -1); - return; - } - Class type = value.getClass(); - if (type == String.class) { - out.writeByte((byte) 0); - out.writeUTF((String) value); - } else if (type == Integer.class) { - out.writeByte((byte) 1); - out.writeInt((Integer) value); - } else if (type == Long.class) { - out.writeByte((byte) 2); - out.writeLong((Long) value); - } else if (type == Float.class) { - out.writeByte((byte) 3); - out.writeFloat((Float) value); - } else if (type == Double.class) { - out.writeByte((byte) 4); - out.writeDouble((Double) value); - } else if (type == Boolean.class) { - out.writeByte((byte) 5); - out.writeBoolean((Boolean) value); - } else if (type == byte[].class) { - out.writeByte((byte) 6); - out.writeVInt(((byte[]) value).length); - out.writeBytes(((byte[]) value)); - } else if (value instanceof List) { - out.writeByte((byte) 7); - List list = (List) value; - out.writeVInt(list.size()); - for (Object o : list) { - writeFieldValue(out, o); - } - } else if (value instanceof Object[]) { - out.writeByte((byte) 8); - Object[] list = (Object[]) value; - out.writeVInt(list.length); - for (Object o : list) { - writeFieldValue(out, o); - } - } else if (value instanceof Map) { - out.writeByte((byte) 9); - Map map = (Map) value; - out.writeVInt(map.size()); - for (Map.Entry entry : map.entrySet()) { - out.writeUTF(entry.getKey()); - writeFieldValue(out, entry.getValue()); - } - } else { - throw new IOException("Can't write type [" + type + "]"); - } - } - private static final Field segmentReaderSegmentInfoField; static { diff --git a/src/main/java/org/elasticsearch/index/get/GetField.java b/src/main/java/org/elasticsearch/index/get/GetField.java index a3ecdfc36f4..cea0f93887e 100644 --- a/src/main/java/org/elasticsearch/index/get/GetField.java +++ b/src/main/java/org/elasticsearch/index/get/GetField.java @@ -22,7 +22,6 @@ package org.elasticsearch.index.get; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Streamable; -import org.elasticsearch.common.lucene.Lucene; import java.io.IOException; import java.util.ArrayList; @@ -91,7 +90,7 @@ public class GetField implements Streamable, Iterable { int size = in.readVInt(); values = new ArrayList(size); for (int i = 0; i < size; i++) { - values.add(Lucene.readFieldValue(in)); + values.add(in.readGenericValue()); } } @@ -100,7 +99,7 @@ public class GetField implements Streamable, Iterable { out.writeUTF(name); out.writeVInt(values.size()); for (Object obj : values) { - Lucene.writeFieldValue(out, obj); + out.writeGenericValue(obj); } } } diff --git a/src/main/java/org/elasticsearch/search/internal/InternalSearchHitField.java b/src/main/java/org/elasticsearch/search/internal/InternalSearchHitField.java index 20bd5fd2cec..5c5f092205c 100644 --- a/src/main/java/org/elasticsearch/search/internal/InternalSearchHitField.java +++ b/src/main/java/org/elasticsearch/search/internal/InternalSearchHitField.java @@ -21,7 +21,6 @@ package org.elasticsearch.search.internal; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; -import org.elasticsearch.common.lucene.Lucene; import org.elasticsearch.search.SearchHitField; import java.io.IOException; @@ -96,7 +95,7 @@ public class InternalSearchHitField implements SearchHitField { int size = in.readVInt(); values = new ArrayList(size); for (int i = 0; i < size; i++) { - values.add(Lucene.readFieldValue(in)); + values.add(in.readGenericValue()); } } @@ -105,7 +104,7 @@ public class InternalSearchHitField implements SearchHitField { out.writeUTF(name); out.writeVInt(values.size()); for (Object value : values) { - Lucene.writeFieldValue(out, value); + out.writeGenericValue(value); } } } \ No newline at end of file