Retrieving byte type stored field fails, closes #1702.

This commit is contained in:
Shay Banon 2012-02-15 15:28:01 +02:00
parent 103f587be1
commit e1cca90006
5 changed files with 20 additions and 127 deletions

View File

@ -224,16 +224,14 @@ public abstract class StreamInput extends InputStream {
// return len;
// }
public
@Nullable
Map<String, Object> readMap() throws IOException {
return (Map<String, Object>) readFieldValue();
public Map<String, Object> readMap() throws IOException {
return (Map<String, Object>) 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 + "]");
}

View File

@ -202,10 +202,10 @@ public abstract class StreamOutput extends OutputStream {
}
public void writeMap(@Nullable Map<String, Object> 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<String, Object> 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 + "]");
}

View File

@ -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<String, Object> map = (Map<String, Object>) value;
out.writeVInt(map.size());
for (Map.Entry<String, Object> 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 {

View File

@ -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<Object> {
int size = in.readVInt();
values = new ArrayList<Object>(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<Object> {
out.writeUTF(name);
out.writeVInt(values.size());
for (Object obj : values) {
Lucene.writeFieldValue(out, obj);
out.writeGenericValue(obj);
}
}
}

View File

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