Skip to content

Instantly share code, notes, and snippets.

@clement-tourriere
Created August 28, 2015 15:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save clement-tourriere/2aa7219bd1da96393cbd to your computer and use it in GitHub Desktop.
Save clement-tourriere/2aa7219bd1da96393cbd to your computer and use it in GitHub Desktop.
Add geo_point type in input.java and output.java
@SuppressWarnings({"unchecked"})
@Nullable
public Object readGenericValue() throws IOException {
byte type = readByte();
switch (type) {
case -1:
return null;
case 0:
return readString();
case 1:
return readInt();
case 2:
return readLong();
case 3:
return readFloat();
case 4:
return readDouble();
case 5:
return readBoolean();
case 6:
int bytesSize = readVInt();
byte[] value = new byte[bytesSize];
readBytes(value, 0, bytesSize);
return value;
case 7:
int size = readVInt();
List list = new ArrayList(size);
for (int i = 0; i < size; i++) {
list.add(readGenericValue());
}
return list;
case 8:
int size8 = readVInt();
Object[] list8 = new Object[size8];
for (int i = 0; i < size8; i++) {
list8[i] = readGenericValue();
}
return list8;
case 9:
int size9 = readVInt();
Map map9 = new LinkedHashMap(size9);
for (int i = 0; i < size9; i++) {
map9.put(readSharedString(), readGenericValue());
}
return map9;
case 10:
int size10 = readVInt();
Map map10 = new HashMap(size10);
for (int i = 0; i < size10; i++) {
map10.put(readSharedString(), readGenericValue());
}
return map10;
case 11:
return readByte();
case 12:
return new Date(readLong());
case 13:
return new DateTime(readLong());
case 14:
return readBytesReference();
case 15:
return readText();
case 16:
return readShort();
case 17:
return readIntArray();
case 18:
return readLongArray();
case 19:
return readFloatArray();
case 20:
return readDoubleArray();
case 21:
return readGeoPoint();
default:
throw new IOException("Can't read unknown type [" + type + "]");
}
}
public GeoPoint readGeoPoint() throws IOException {
return new GeoPoint(readDouble(), readDouble());
}
public void writeGenericValue(@Nullable Object value) throws IOException {
if (value == null) {
writeByte((byte) -1);
return;
}
Class type = value.getClass();
if (type == String.class) {
writeByte((byte) 0);
writeString((String) value);
} else if (type == Integer.class) {
writeByte((byte) 1);
writeInt((Integer) value);
} else if (type == Long.class) {
writeByte((byte) 2);
writeLong((Long) value);
} else if (type == Float.class) {
writeByte((byte) 3);
writeFloat((Float) value);
} else if (type == Double.class) {
writeByte((byte) 4);
writeDouble((Double) value);
} else if (type == Boolean.class) {
writeByte((byte) 5);
writeBoolean((Boolean) value);
} else if (type == byte[].class) {
writeByte((byte) 6);
writeVInt(((byte[]) value).length);
writeBytes(((byte[]) value));
} else if (value instanceof List) {
writeByte((byte) 7);
List list = (List) value;
writeVInt(list.size());
for (Object o : list) {
writeGenericValue(o);
}
} else if (value instanceof Object[]) {
writeByte((byte) 8);
Object[] list = (Object[]) value;
writeVInt(list.length);
for (Object o : list) {
writeGenericValue(o);
}
} else if (value instanceof Map) {
if (value instanceof LinkedHashMap) {
writeByte((byte) 9);
} else {
writeByte((byte) 10);
}
Map<String, Object> map = (Map<String, Object>) value;
writeVInt(map.size());
for (Map.Entry<String, Object> entry : map.entrySet()) {
writeSharedString(entry.getKey());
writeGenericValue(entry.getValue());
}
} else if (type == Byte.class) {
writeByte((byte) 11);
writeByte((Byte) value);
} else if (type == Date.class) {
writeByte((byte) 12);
writeLong(((Date) value).getTime());
} else if (value instanceof ReadableInstant) {
writeByte((byte) 13);
writeLong(((ReadableInstant) value).getMillis());
} else if (value instanceof BytesReference) {
writeByte((byte) 14);
writeBytesReference((BytesReference) value);
} else if (value instanceof Text) {
writeByte((byte) 15);
writeText((Text) value);
} else if (type == Short.class) {
writeByte((byte) 16);
writeShort((Short) value);
} else if (type == int[].class) {
writeByte((byte) 17);
writeIntArray((int[]) value);
} else if (type == long[].class) {
writeByte((byte) 18);
writeLongArray((long[]) value);
} else if (type == float[].class) {
writeByte((byte) 19);
writeFloatArray((float[]) value);
} else if (type == double[].class) {
writeByte((byte) 20);
writeDoubleArray((double[]) value);
} else if (type == GeoPoint.class) {
writeByte((byte) 21);
writeDouble(((GeoPoint)value).lat());
writeDouble(((GeoPoint)value).lon());
} else {
throw new IOException("Can't write type [" + type + "]");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment