Skip to content

Commit

Permalink
Add support for byte string columns. Fixes #141.
Browse files Browse the repository at this point in the history
  • Loading branch information
erikmafo committed Aug 8, 2022
1 parent 77e3799 commit f2f9ee1
Show file tree
Hide file tree
Showing 11 changed files with 53 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>littletableviewer</artifactId>
<version>1.0.1</version>
<version>1.0.2</version>
<packaging>jar</packaging>
<name>Little Table Viewer</name>

Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/erikmafo/ltviewer/model/BigtableCell.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package com.erikmafo.ltviewer.model;

import com.erikmafo.ltviewer.util.HexUtil;
import com.google.cloud.bigtable.data.v2.models.RowCell;
import com.google.protobuf.ByteString;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;

import java.util.HexFormat;

/**
* A simple representation of a bigtable row cell.
*/
Expand Down Expand Up @@ -70,6 +73,8 @@ public String getQualifier() {
*/
public String getValueAsString() { return bytes.toStringUtf8(); }

public String getValueAsHexString() { return HexUtil.toHex(bytes.toByteArray()); }

/**
* Gets the value of the cell as a byte array.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ private BigtableValue convertUsingValueType(BigtableCell cell, @NotNull CellDefi
return toBigtableValue(cell.getValueAsString(), valueTypeUpper);
case ValueTypeConstants.PROTO:
return toBigtableValue(ProtoUtil.toJson(cell.getByteString(), cellDefinition.getProtoObjectDefinition()), valueTypeUpper);
case ValueTypeConstants.BYTE_STRING:
return toBigtableValue(cell.getValueAsHexString(), valueTypeUpper);
default:
return toBigtableValue(cell.getValueAsString(), ValueTypeConstants.STRING);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ public ByteString toByteString(Field field, Value value) {
case ValueTypeConstants.LONG:
byteString = ByteStringConverterUtil.toByteString(value.asLong());
break;
case ValueTypeConstants.BYTE_STRING:
byteString = ByteStringConverterUtil.toByteStringFromHex(value.asString());
break;
default: throw new IllegalArgumentException(String.format("Value type %s is not supported", valueType.toUpperCase()));
}

Expand Down
12 changes: 8 additions & 4 deletions src/main/java/com/erikmafo/ltviewer/model/CellDefinition.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@ public void setQualifier(String qualifier) {
this.qualifier = qualifier;
}

public ProtoObjectDefinition getProtoObjectDefinition() {
return protoObjectDefinition;
}

public void setProtoObjectDefinition(ProtoObjectDefinition protoObjectDefinition) {
this.protoObjectDefinition = protoObjectDefinition;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand All @@ -85,8 +93,4 @@ public String toString() {
", qualifier='" + qualifier + '\'' +
'}';
}

public ProtoObjectDefinition getProtoObjectDefinition() {
return protoObjectDefinition;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import org.jetbrains.annotations.NotNull;

public class ProtoObjectDefinition {
private final String descriptorSetFile;
private final String protoFile;
private final String messageType;
private String descriptorSetFile;
private String protoFile;
private String messageType;

public ProtoObjectDefinition(String descriptorSetFile, String protoFile, String messageType) {
Check.notNullOrEmpty(descriptorSetFile, "descriptorSetFile");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@ public class ValueTypeConstants {
public static final String JSON = "JSON";

public static final String PROTO = "PROTO";

public static final String BYTE_STRING = "BYTESTRING";
}
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ private void addSchemaRow(@NotNull BigtableColumn column, @Nullable String value
HBox hbox = new HBox();
ChoiceBox<String> choiceBox = new ChoiceBox<>();
choiceBox.setValue(valueType != null ? valueType : "String");
choiceBox.getItems().setAll(Arrays.asList("String", "Double", "Float", "Integer", "Long", "Short", "Json", "Proto"));
choiceBox.getItems().setAll(Arrays.asList("String", "Double", "Float", "Integer", "Long", "Short", "Json", "Proto", "ByteString"));
choiceBox.setPrefWidth(CHOICE_BOX_PREF_WIDTH);
GlyphFont fontAwesome = GlyphFontRegistry.font("FontAwesome");
Button configureProtoObjectButton = new Button("", fontAwesome.create(FontAwesome.Glyph.COG));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import javafx.fxml.FXML;
import javafx.scene.Node;
import javafx.scene.control.Label;
import javafx.scene.control.TreeView;
import javafx.scene.layout.*;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.HexFormat;

public class ByteStringConverterUtil {

Expand All @@ -15,6 +16,12 @@ public static ByteString toByteString(String stringUtf8) {
return ByteString.copyFromUtf8(stringUtf8);
}

@NotNull
@Contract("_ -> new")
public static ByteString toByteStringFromHex(String hex) {
return ByteString.copyFrom(HexUtil.toBytes(hex));
}

@NotNull
public static ByteString toByteString(long value) {
var buffer = ByteBuffer
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/com/erikmafo/ltviewer/util/HexUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.erikmafo.ltviewer.util;

import org.jetbrains.annotations.NotNull;

import java.util.HexFormat;

public class HexUtil {

public static String toHex(byte[] bytes) {
return getHexFormat().formatHex(bytes);
}

public static byte[] toBytes(String hex) {
return getHexFormat().parseHex(hex);
}

@NotNull
private static HexFormat getHexFormat() {
return HexFormat.of().withUpperCase();
}
}

0 comments on commit f2f9ee1

Please sign in to comment.