From 4aafb4d46832aedbd60517dc8d3589d21c32609b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erik=20F=C3=B8lstad?= Date: Wed, 24 Aug 2022 23:28:42 +0200 Subject: [PATCH] Add uuid value type fixes #157. (#165) --- pom.xml | 2 +- .../model/BigtableValueConverter.java | 3 ++ .../model/ByteStringConverterImpl.java | 8 +++- .../btviewer/model/ValueTypeConstants.java | 2 + .../internal/testdata/TestDataUtil.java | 16 ++++---- .../TableSettingsDialogController.java | 3 +- .../util/ByteStringConverterUtil.java | 7 ++++ .../btviewer/util/UUIDConverterUtil.java | 24 ++++++++++++ .../btviewer/util/UUIDConverterUtilTest.java | 38 +++++++++++++++++++ 9 files changed, 93 insertions(+), 10 deletions(-) create mode 100644 src/main/java/com/erikmafo/btviewer/util/UUIDConverterUtil.java create mode 100644 src/test/java/com/erikmafo/btviewer/util/UUIDConverterUtilTest.java diff --git a/pom.xml b/pom.xml index 707b45e..1fb63cb 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ com.erikmafo big-table-viewer - 1.0.7 + 1.0.9 jar Big Table Viewer diff --git a/src/main/java/com/erikmafo/btviewer/model/BigtableValueConverter.java b/src/main/java/com/erikmafo/btviewer/model/BigtableValueConverter.java index 51042b7..f27de90 100644 --- a/src/main/java/com/erikmafo/btviewer/model/BigtableValueConverter.java +++ b/src/main/java/com/erikmafo/btviewer/model/BigtableValueConverter.java @@ -1,6 +1,7 @@ package com.erikmafo.btviewer.model; import com.erikmafo.btviewer.util.ProtoUtil; +import com.erikmafo.btviewer.util.UUIDConverterUtil; import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.NotNull; @@ -127,6 +128,8 @@ private BigtableValue convertUsingValueType(BigtableCell cell, @NotNull CellDefi return toBigtableValue(ProtoUtil.toJson(cell.getByteString(), cellDefinition.getProtoObjectDefinition()), valueTypeUpper); case ValueTypeConstants.BYTE_STRING: return toBigtableValue(cell.getValueAsStringBase64(), valueTypeUpper); + case ValueTypeConstants.UUID: + return toBigtableValue(UUIDConverterUtil.convertBytesToUUID(cell.getBytes()), valueTypeUpper); default: return toBigtableValue(cell.getValueAsString(), ValueTypeConstants.STRING); } diff --git a/src/main/java/com/erikmafo/btviewer/model/ByteStringConverterImpl.java b/src/main/java/com/erikmafo/btviewer/model/ByteStringConverterImpl.java index f670a92..acfa298 100644 --- a/src/main/java/com/erikmafo/btviewer/model/ByteStringConverterImpl.java +++ b/src/main/java/com/erikmafo/btviewer/model/ByteStringConverterImpl.java @@ -9,6 +9,7 @@ import java.util.Collections; import java.util.List; +import java.util.UUID; public class ByteStringConverterImpl implements ByteStringConverter { private final List cellDefinitions; @@ -52,7 +53,12 @@ public ByteString toByteString(@NotNull Field field, Value value) { case ValueTypeConstants.BYTE_STRING: byteString = ByteStringConverterUtil.toByteStringFromBase64(value.asString()); break; - default: throw new IllegalArgumentException(String.format("Value type %s is not supported", valueType.toUpperCase())); + case ValueTypeConstants.UUID: + byteString = ByteStringConverterUtil.toByteString(UUID.fromString(value.asString())); + break; + default: + throw new IllegalArgumentException( + String.format("Value type %s is not supported", valueType.toUpperCase())); } return byteString; diff --git a/src/main/java/com/erikmafo/btviewer/model/ValueTypeConstants.java b/src/main/java/com/erikmafo/btviewer/model/ValueTypeConstants.java index 24e6a00..5b33006 100644 --- a/src/main/java/com/erikmafo/btviewer/model/ValueTypeConstants.java +++ b/src/main/java/com/erikmafo/btviewer/model/ValueTypeConstants.java @@ -19,4 +19,6 @@ public class ValueTypeConstants { public static final String PROTO = "PROTO"; public static final String BYTE_STRING = "BYTESTRING"; + + public static final String UUID = "UUID"; } diff --git a/src/main/java/com/erikmafo/btviewer/services/internal/testdata/TestDataUtil.java b/src/main/java/com/erikmafo/btviewer/services/internal/testdata/TestDataUtil.java index 68246ad..f5c3eb9 100644 --- a/src/main/java/com/erikmafo/btviewer/services/internal/testdata/TestDataUtil.java +++ b/src/main/java/com/erikmafo/btviewer/services/internal/testdata/TestDataUtil.java @@ -10,6 +10,7 @@ import org.jetbrains.annotations.NotNull; import java.io.IOException; +import java.util.UUID; import static com.erikmafo.btviewer.util.ByteStringConverterUtil.toByteString; @@ -67,13 +68,14 @@ private static void addData(String tableName, com.google.cloud.bigtable.data.v2. var rowKey = "row-000000000000000" + String.format("%04d", i); var mutation = RowMutation .create(tableName, rowKey) - .setCell("f1", "q1", "string-" + i) - .setCell("f1", toByteString("q2"), toByteString(i)) - .setCell("f1", toByteString("q3"), toByteString(i + 0.5)) - .setCell("f1", toByteString("q4"), toByteString(JSON_TEST_DATA)) - .setCell("f2", toByteString("q1"), toByteString("string-" + i)) - .setCell("f3", toByteString("q1"), BYTE_STRING_TEST_VALUE) - .setCell("f4", toByteString("q1"), getPerson(i).toByteString()); + .setCell("f1", "string-col", "string-" + i) + .setCell("f1", toByteString("int-col"), toByteString(i)) + .setCell("f1", toByteString("double-col"), toByteString(i + 0.5)) + .setCell("f1", toByteString("json-col"), toByteString(JSON_TEST_DATA)) + .setCell("f2", toByteString("string-col"), toByteString("string-" + i)) + .setCell("f3", toByteString("bytes-col"), BYTE_STRING_TEST_VALUE) + .setCell("f3", toByteString("uuid-col"),toByteString(UUID.randomUUID())) + .setCell("f4", toByteString("proto-col"), getPerson(i).toByteString()); dataClient.mutateRow(mutation); } } diff --git a/src/main/java/com/erikmafo/btviewer/ui/dialogs/tablesettings/TableSettingsDialogController.java b/src/main/java/com/erikmafo/btviewer/ui/dialogs/tablesettings/TableSettingsDialogController.java index 87345aa..9fb6f5b 100644 --- a/src/main/java/com/erikmafo/btviewer/ui/dialogs/tablesettings/TableSettingsDialogController.java +++ b/src/main/java/com/erikmafo/btviewer/ui/dialogs/tablesettings/TableSettingsDialogController.java @@ -196,7 +196,8 @@ private static ChoiceBox getValueTypeChoiceBox(@Nullable String valueTyp ChoiceBox choiceBox = new ChoiceBox<>(); choiceBox.setValue(valueType != null ? valueType : "ByteString"); - choiceBox.getItems().setAll(Arrays.asList("String", "Double", "Float", "Integer", "Long", "Short", "Json", "Proto", "ByteString")); + choiceBox.getItems().setAll(Arrays.asList( + "String", "Double", "Float", "Integer", "Long", "Short", "Json", "Proto", "ByteString", "UUID")); choiceBox.setPrefWidth(CHOICE_BOX_PREF_WIDTH); return choiceBox; } diff --git a/src/main/java/com/erikmafo/btviewer/util/ByteStringConverterUtil.java b/src/main/java/com/erikmafo/btviewer/util/ByteStringConverterUtil.java index 7924214..f83e1f6 100644 --- a/src/main/java/com/erikmafo/btviewer/util/ByteStringConverterUtil.java +++ b/src/main/java/com/erikmafo/btviewer/util/ByteStringConverterUtil.java @@ -7,6 +7,7 @@ import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.util.Base64; +import java.util.UUID; public class ByteStringConverterUtil { @@ -57,4 +58,10 @@ public static ByteString toByteString(double value) { .order(ByteOrder.BIG_ENDIAN); return ByteString.copyFrom(buffer.array()); } + + @NotNull + @Contract("_ -> new") + public static ByteString toByteString(UUID value) { + return ByteString.copyFrom(UUIDConverterUtil.convertUUIDToBytes(value)); + } } diff --git a/src/main/java/com/erikmafo/btviewer/util/UUIDConverterUtil.java b/src/main/java/com/erikmafo/btviewer/util/UUIDConverterUtil.java new file mode 100644 index 0000000..31173b8 --- /dev/null +++ b/src/main/java/com/erikmafo/btviewer/util/UUIDConverterUtil.java @@ -0,0 +1,24 @@ +package com.erikmafo.btviewer.util; + +import org.jetbrains.annotations.NotNull; +import java.nio.ByteBuffer; +import java.util.UUID; + +public class UUIDConverterUtil { + + @NotNull + public static byte[] convertUUIDToBytes(@NotNull UUID uuid) { + var buffer = ByteBuffer.wrap(new byte[16]); + buffer.putLong(uuid.getMostSignificantBits()); + buffer.putLong(uuid.getLeastSignificantBits()); + return buffer.array(); + } + + @NotNull + public static UUID convertBytesToUUID(byte[] bytes) { + var buffer = ByteBuffer.wrap(bytes); + var mostSignificantBits = buffer.getLong(); + var leastSignificantBits = buffer.getLong(); + return new UUID(mostSignificantBits, leastSignificantBits); + } +} diff --git a/src/test/java/com/erikmafo/btviewer/util/UUIDConverterUtilTest.java b/src/test/java/com/erikmafo/btviewer/util/UUIDConverterUtilTest.java new file mode 100644 index 0000000..3f0932e --- /dev/null +++ b/src/test/java/com/erikmafo/btviewer/util/UUIDConverterUtilTest.java @@ -0,0 +1,38 @@ +package com.erikmafo.btviewer.util; + +import org.junit.Test; + +import java.nio.ByteBuffer; +import java.util.UUID; + +import static org.junit.Assert.*; + +public class UUIDConverterUtilTest { + + @Test + public void convertUUIDToBytesReturnsMostSignificantBitsThenLeastSignificantBits() { + // given + var uuid = UUID.randomUUID(); + + // when + var bytes = UUIDConverterUtil.convertUUIDToBytes(uuid); + + // then + var buffer = ByteBuffer.wrap(bytes); + assertEquals(uuid.getMostSignificantBits(), buffer.getLong()); + assertEquals(uuid.getLeastSignificantBits(), buffer.getLong()); + } + + @Test + public void convertBytesToUUIDReturnsOriginalUUID() { + // given + var uuid = UUID.randomUUID(); + var bytes = UUIDConverterUtil.convertUUIDToBytes(uuid); + + // when + var convertedUUID = UUIDConverterUtil.convertBytesToUUID(bytes); + + // then + assertEquals(uuid, convertedUUID); + } +} \ No newline at end of file