Skip to content

Commit

Permalink
Add uuid value type fixes #157. (#165)
Browse files Browse the repository at this point in the history
  • Loading branch information
erikmafo authored Aug 24, 2022
1 parent 36f0924 commit 4aafb4d
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 10 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>com.erikmafo</groupId>
<artifactId>big-table-viewer</artifactId>
<version>1.0.7</version>
<version>1.0.9</version>
<packaging>jar</packaging>
<name>Big Table Viewer</name>

Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import java.util.Collections;
import java.util.List;
import java.util.UUID;

public class ByteStringConverterImpl implements ByteStringConverter {
private final List<CellDefinition> cellDefinitions;
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,8 @@ private static ChoiceBox<String> getValueTypeChoiceBox(@Nullable String valueTyp
ChoiceBox<String> 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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Base64;
import java.util.UUID;

public class ByteStringConverterUtil {

Expand Down Expand Up @@ -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));
}
}
24 changes: 24 additions & 0 deletions src/main/java/com/erikmafo/btviewer/util/UUIDConverterUtil.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit 4aafb4d

Please sign in to comment.