com.xiaomi.infra.galaxy.sds.thrift.DatumUtil Maven / Gradle / Ivy
package com.xiaomi.infra.galaxy.sds.thrift;
import libthrift091.TBase;
import libthrift091.TDeserializer;
import libthrift091.TException;
import libthrift091.TSerializer;
import libthrift091.protocol.TCompactProtocol;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import static com.xiaomi.infra.galaxy.sds.thrift.Value.binarySetValue;
import static com.xiaomi.infra.galaxy.sds.thrift.Value.binaryValue;
import static com.xiaomi.infra.galaxy.sds.thrift.Value.boolSetValue;
import static com.xiaomi.infra.galaxy.sds.thrift.Value.boolValue;
import static com.xiaomi.infra.galaxy.sds.thrift.Value.doubleSetValue;
import static com.xiaomi.infra.galaxy.sds.thrift.Value.doubleValue;
import static com.xiaomi.infra.galaxy.sds.thrift.Value.int16SetValue;
import static com.xiaomi.infra.galaxy.sds.thrift.Value.int16Value;
import static com.xiaomi.infra.galaxy.sds.thrift.Value.int32SetValue;
import static com.xiaomi.infra.galaxy.sds.thrift.Value.int32Value;
import static com.xiaomi.infra.galaxy.sds.thrift.Value.int64SetValue;
import static com.xiaomi.infra.galaxy.sds.thrift.Value.int64Value;
import static com.xiaomi.infra.galaxy.sds.thrift.Value.int8SetValue;
import static com.xiaomi.infra.galaxy.sds.thrift.Value.int8Value;
import static com.xiaomi.infra.galaxy.sds.thrift.Value.stringSetValue;
import static com.xiaomi.infra.galaxy.sds.thrift.Value.stringValue;
public class DatumUtil {
public static Map toDatum(Map keyValues) {
return toDatum(keyValues, (Map) null);
}
public static Map toDatum(Map keyValues,
Map attributes) {
if (keyValues != null) {
try {
Map kvs = new HashMap();
for (Map.Entry kv : keyValues.entrySet()) {
if (attributes != null && kv.getValue() instanceof Set) {
kvs.put(kv.getKey(),
fromSetToDatum((Set) (kv.getValue()), attributes.get(kv.getKey())));
} else {
kvs.put(kv.getKey(), DatumUtil.toDatum(kv.getValue()));
}
}
return kvs;
} catch (IllegalArgumentException iae) {
throw new IllegalArgumentException("Failed to convert key values [" + keyValues + "]: "
+ iae.getMessage(), iae);
}
}
return null;
}
public static Datum fromSetToDatum(Set set, DataType dataType) {
List list = new ArrayList();
for (T e : set) {
list.add(e);
}
switch (dataType) {
case BOOL_SET:
return toDatum(list, DataType.BOOL);
case INT8_SET:
return toDatum(list, DataType.INT8);
case INT16_SET:
return toDatum(list, DataType.INT16);
case INT32_SET:
return toDatum(list, DataType.INT32);
case INT64_SET:
return toDatum(list, DataType.INT64);
case FLOAT_SET:
return toDatum(list, DataType.FLOAT);
case DOUBLE_SET:
return toDatum(list, DataType.DOUBLE);
case STRING_SET:
return toDatum(list, DataType.STRING);
case BINARY_SET:
return toDatum(list, DataType.BINARY);
default:
throw new RuntimeException("Unsupported repeated type " + dataType);
}
}
public static Datum toDatum(Object value) {
return toDatum(value, null);
}
public static Datum toDatum(Object value, DataType repeatedType) {
if (value == null) {
throw new IllegalArgumentException("Datum must not be null");
}
if (repeatedType == null) {
if (value instanceof Boolean) {
return newDatum(DataType.BOOL, boolValue((Boolean) value));
} else if (value instanceof Byte) {
return newDatum(DataType.INT8, int8Value((Byte) value));
} else if (value instanceof Short) {
return newDatum(DataType.INT16, int16Value((Short) value));
} else if (value instanceof Integer) {
return newDatum(DataType.INT32, int32Value((Integer) value));
} else if (value instanceof Long) {
return newDatum(DataType.INT64, int64Value((Long) value));
} else if (value instanceof Float) {
return newDatum(DataType.FLOAT, doubleValue((Float) value));
} else if (value instanceof Double) {
return newDatum(DataType.DOUBLE, doubleValue((Double) value));
} else if (value instanceof String) {
return newDatum(DataType.STRING, stringValue((String) value));
} else if (value instanceof byte[]) {
return newDatum(DataType.BINARY, binaryValue((byte[]) value));
} else if (value instanceof ByteBuffer) {
return newDatum(DataType.BINARY, binaryValue((ByteBuffer) value));
} else {
throw new RuntimeException("Unsupported datum type: " + value.getClass().getSimpleName()
+ ", value: " + value);
}
} else {
assert value instanceof List;
switch (repeatedType) {
case BOOL:
return newDatum(DataType.BOOL_SET, boolSetValue((List) value));
case INT8:
return newDatum(DataType.INT8_SET, int8SetValue((List) value));
case INT16:
return newDatum(DataType.INT16_SET, int16SetValue((List) value));
case INT32:
return newDatum(DataType.INT32_SET, int32SetValue((List) value));
case INT64:
return newDatum(DataType.INT64_SET, int64SetValue((List) value));
case FLOAT:
return newDatum(DataType.FLOAT_SET,
doubleSetValue(fromFloatListToDoubleList((List) value)));
case DOUBLE:
return newDatum(DataType.DOUBLE_SET, doubleSetValue((List) value));
case STRING:
return newDatum(DataType.STRING_SET, stringSetValue((List) value));
case BINARY:
if (((List) value).isEmpty()) {
return newDatum(DataType.BINARY_SET, binarySetValue(new ArrayList()));
}
if (((List) value).get(0) instanceof byte[]) {
return newDatum(DataType.BINARY_SET,
binarySetValue(fromByteArrayListToByteBufferList((List) value)));
} else if (((List) value).get(0) instanceof ByteBuffer) {
return newDatum(DataType.BINARY_SET, binarySetValue((List) value));
}
default:
throw new RuntimeException("Unsupported datum type: " + repeatedType);
}
}
}
private static Datum newDatum(DataType type, Value value) {
return new Datum().setType(type).setValue(value);
}
private static List fromFloatListToDoubleList(List floatList) {
List doubleList = new ArrayList();
for (float e : floatList) {
doubleList.add((double) e);
}
return doubleList;
}
private static List fromByteArrayListToByteBufferList(List byteArrayList) {
List byteBufferList = new ArrayList();
for (byte[] e : byteArrayList) {
byteBufferList.add(ByteBuffer.wrap(e));
}
return byteBufferList;
}
public static List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy