提交 58382255 authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov

Extract shared methods for conversions between UUID, ValueUuid, and byte[]

上级 54e205c0
...@@ -36,8 +36,8 @@ import org.h2.message.DbException; ...@@ -36,8 +36,8 @@ import org.h2.message.DbException;
import org.h2.util.JdbcUtils; import org.h2.util.JdbcUtils;
import org.h2.util.MathUtils; import org.h2.util.MathUtils;
import org.h2.util.New; import org.h2.util.New;
import org.h2.util.Utils;
import org.h2.value.DataType; import org.h2.value.DataType;
import org.h2.value.ValueUuid;
/** /**
* This class is a simple result set and meta data implementation. * This class is a simple result set and meta data implementation.
...@@ -534,8 +534,7 @@ public class SimpleResultSet implements ResultSet, ResultSetMetaData, ...@@ -534,8 +534,7 @@ public class SimpleResultSet implements ResultSet, ResultSetMetaData,
return (byte[]) o; return (byte[]) o;
} }
if (o instanceof UUID) { if (o instanceof UUID) {
final UUID u = (UUID) o; return Utils.uuidToBytes((UUID) o);
return ValueUuid.get(u.getMostSignificantBits(), u.getLeastSignificantBits()).getBytes();
} }
return JdbcUtils.serialize(o, null); return JdbcUtils.serialize(o, null);
} }
......
...@@ -18,6 +18,7 @@ import java.util.Arrays; ...@@ -18,6 +18,7 @@ import java.util.Arrays;
import java.util.Comparator; import java.util.Comparator;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.zip.ZipEntry; import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream; import java.util.zip.ZipInputStream;
...@@ -92,6 +93,28 @@ public class Utils { ...@@ -92,6 +93,28 @@ public class Utils {
(readInt(buff, pos + 4) & 0xffffffffL); (readInt(buff, pos + 4) & 0xffffffffL);
} }
/**
* @param uuid UUID value
* @return byte array representation
*/
public static byte[] uuidToBytes(UUID uuid) {
return uuidToBytes(uuid.getMostSignificantBits(), uuid.getLeastSignificantBits());
}
/**
* @param msb most significant part of UUID
* @param lsb least significant part of UUID
* @return byte array representation
*/
public static byte[] uuidToBytes(long msb, long lsb) {
byte[] buff = new byte[16];
for (int i = 0; i < 8; i++) {
buff[i] = (byte) ((msb >> (8 * (7 - i))) & 255);
buff[8 + i] = (byte) ((lsb >> (8 * (7 - i))) & 255);
}
return buff;
}
/** /**
* Calculate the index of the first occurrence of the pattern in the byte * Calculate the index of the first occurrence of the pattern in the byte
* array, starting with the given index. This methods returns -1 if the * array, starting with the given index. This methods returns -1 if the
......
...@@ -522,8 +522,7 @@ public class DataType { ...@@ -522,8 +522,7 @@ public class DataType {
if (o instanceof byte[]) { if (o instanceof byte[]) {
v = ValueBytes.getNoCopy((byte[]) o); v = ValueBytes.getNoCopy((byte[]) o);
} else if (o != null) { } else if (o != null) {
UUID u = (UUID) o; v = ValueUuid.get((UUID) o);
v = ValueUuid.get(u.getMostSignificantBits(), u.getLeastSignificantBits());
} else } else
v = ValueNull.INSTANCE; v = ValueNull.INSTANCE;
break; break;
...@@ -531,8 +530,7 @@ public class DataType { ...@@ -531,8 +530,7 @@ public class DataType {
case Value.UUID: { case Value.UUID: {
Object o = rs.getObject(columnIndex); Object o = rs.getObject(columnIndex);
if (o instanceof UUID) { if (o instanceof UUID) {
UUID u = (UUID) o; v = ValueUuid.get((UUID) o);
v = ValueUuid.get(u.getMostSignificantBits(), u.getLeastSignificantBits());
} else if (o != null) } else if (o != null)
v = ValueUuid.get((byte[]) o); v = ValueUuid.get((byte[]) o);
else else
...@@ -1114,8 +1112,7 @@ public class DataType { ...@@ -1114,8 +1112,7 @@ public class DataType {
} }
return ValueResultSet.getCopy((ResultSet) x, Integer.MAX_VALUE); return ValueResultSet.getCopy((ResultSet) x, Integer.MAX_VALUE);
} else if (x instanceof UUID) { } else if (x instanceof UUID) {
UUID u = (UUID) x; return ValueUuid.get((UUID) x);
return ValueUuid.get(u.getMostSignificantBits(), u.getLeastSignificantBits());
} else if (x instanceof Object[]) { } else if (x instanceof Object[]) {
// (a.getClass().isArray()); // (a.getClass().isArray());
// (a.getClass().getComponentType().isPrimitive()); // (a.getClass().getComponentType().isPrimitive());
......
...@@ -944,9 +944,7 @@ public abstract class Value { ...@@ -944,9 +944,7 @@ public abstract class Value {
Object object = JdbcUtils.deserialize(getBytesNoCopy(), Object object = JdbcUtils.deserialize(getBytesNoCopy(),
getDataHandler()); getDataHandler());
if (object instanceof java.util.UUID) { if (object instanceof java.util.UUID) {
java.util.UUID uuid = (java.util.UUID) object; return ValueUuid.get((java.util.UUID) object);
return ValueUuid.get(uuid.getMostSignificantBits(),
uuid.getLeastSignificantBits());
} }
throw DbException.get(ErrorCode.DATA_CONVERSION_ERROR_1, getString()); throw DbException.get(ErrorCode.DATA_CONVERSION_ERROR_1, getString());
case TIMESTAMP_TZ: case TIMESTAMP_TZ:
......
...@@ -84,6 +84,16 @@ public class ValueUuid extends Value { ...@@ -84,6 +84,16 @@ public class ValueUuid extends Value {
return (ValueUuid) Value.cache(new ValueUuid(high, low)); return (ValueUuid) Value.cache(new ValueUuid(high, low));
} }
/**
* Get or create a UUID for the given Java UUID.
*
* @param uuid Java UUID
* @return the UUID
*/
public static ValueUuid get(UUID uuid) {
return get(uuid.getMostSignificantBits(), uuid.getLeastSignificantBits());
}
/** /**
* Get or create a UUID for the given text representation. * Get or create a UUID for the given text representation.
* *
...@@ -176,12 +186,7 @@ public class ValueUuid extends Value { ...@@ -176,12 +186,7 @@ public class ValueUuid extends Value {
@Override @Override
public byte[] getBytes() { public byte[] getBytes() {
byte[] buff = new byte[16]; return Utils.uuidToBytes(high, low);
for (int i = 0; i < 8; i++) {
buff[i] = (byte) ((high >> (8 * (7 - i))) & 255);
buff[8 + i] = (byte) ((low >> (8 * (7 - i))) & 255);
}
return buff;
} }
@Override @Override
......
...@@ -22,6 +22,7 @@ import org.h2.message.DbException; ...@@ -22,6 +22,7 @@ import org.h2.message.DbException;
import org.h2.test.TestBase; import org.h2.test.TestBase;
import org.h2.test.utils.AssertThrows; import org.h2.test.utils.AssertThrows;
import org.h2.tools.SimpleResultSet; import org.h2.tools.SimpleResultSet;
import org.h2.util.Utils;
import org.h2.value.DataType; import org.h2.value.DataType;
import org.h2.value.Value; import org.h2.value.Value;
import org.h2.value.ValueArray; import org.h2.value.ValueArray;
...@@ -130,9 +131,7 @@ public class TestValue extends TestBase { ...@@ -130,9 +131,7 @@ public class TestValue extends TestBase {
prep.setObject(1, new Object[] { uuid }); prep.setObject(1, new Object[] { uuid });
rs = prep.executeQuery(); rs = prep.executeQuery();
rs.next(); rs.next();
assertTrue(Arrays.equals( assertTrue(Arrays.equals(Utils.uuidToBytes(uuid), (byte[]) rs.getObject(1)));
ValueUuid.get(uuid.getMostSignificantBits(), uuid.getLeastSignificantBits()).getBytes(),
(byte[]) rs.getObject(1)));
// Check that type is not changed // Check that type is not changed
prep = conn.prepareStatement("SELECT * FROM TABLE(X UUID=?)"); prep = conn.prepareStatement("SELECT * FROM TABLE(X UUID=?)");
prep.setObject(1, new Object[] { uuid }); prep.setObject(1, new Object[] { uuid });
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论