Skip to content

Commit

Permalink
Specify column qualifier as regex pattern when defining data types. F…
Browse files Browse the repository at this point in the history
…ixes #121.
  • Loading branch information
erikmafo committed Aug 21, 2022
1 parent 410390f commit 4e6f9ce
Show file tree
Hide file tree
Showing 40 changed files with 635 additions and 421 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,10 @@ private Object getValueOrNull(BigtableValue value) {

@NotNull
private CellDefinition getCellDefinition(@NotNull BigtableCell cell) {
return cellDefinitions
.stream()
.filter(c -> c.getFamily().equals(cell.getFamily())
&& c.getQualifier().equals(cell.getQualifier()))
.findFirst()
.orElse(new CellDefinition(ValueTypeConstants.STRING, cell.getFamily(), cell.getQualifier()));
return CellDefinitionMatcherUtil
.findBestMatch(cellDefinitions, new BigtableColumn(cell.getFamily(), cell.getQualifier()))
.orElse(new CellDefinition(ValueTypeConstants.STRING, cell.getFamily(), cell.getQualifier(), null));
}

@NotNull
private BigtableValue convertUsingValueType(BigtableCell cell, @NotNull CellDefinition cellDefinition) throws IOException {
var valueTypeUpper = cellDefinition.getValueType().toUpperCase();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,10 @@ public ByteStringConverterImpl(List<CellDefinition> cellDefinitions) {
}

@Override
public ByteString toByteString(Field field, Value value) {
var valueType = cellDefinitions
.stream()
.filter(c -> c.getFamily().equals(field.getFamily()))
.filter(c -> c.getQualifier().equals(field.getQualifier()))
public ByteString toByteString(@NotNull Field field, Value value) {
var valueType = CellDefinitionMatcherUtil
.findBestMatch(cellDefinitions, new BigtableColumn(field.getFamily(), field.getQualifier()))
.map(CellDefinition::getValueType)
.findFirst()
.orElse(ValueTypeConstants.STRING);

ByteString byteString;
Expand Down
56 changes: 45 additions & 11 deletions src/main/java/com/erikmafo/btviewer/model/CellDefinition.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.erikmafo.btviewer.model;
import org.jetbrains.annotations.NotNull;

import java.util.Objects;
import java.util.regex.Pattern;

/**
* Specifies how the value of a cell should be interpreted by assigning a value type.
Expand All @@ -10,18 +12,8 @@ public class CellDefinition {
private String valueType;
private String family;
private String qualifier;
private ProtoObjectDefinition protoObjectDefinition;

/**
* Creates a new instance of {@code CellDefinition}.
*
* @param valueType the value type of the cell.
* @param family the name of the column family that the cell belongs to.
* @param qualifier the name of the column qualifier that the cell belong to.
*/
public CellDefinition(String valueType, String family, String qualifier) {
this(valueType, family, qualifier, null);
}
private ProtoObjectDefinition protoObjectDefinition;

/**
* Creates a new instance of {@code CellDefinition}.
Expand Down Expand Up @@ -70,6 +62,22 @@ public void setProtoObjectDefinition(ProtoObjectDefinition protoObjectDefinition
this.protoObjectDefinition = protoObjectDefinition;
}

public boolean matchesExact(@NotNull BigtableColumn column) {
return column.getFamily().equals(family) && column.getQualifier().equals(qualifier);
}

public boolean matches(@NotNull BigtableColumn column) {
return matches(column.getFamily(), column.getQualifier());
}

public boolean matches(String family, String qualifier) {
if (!Objects.equals(family, this.family)) {
return false;
}

return getQualifierPattern().matcher(qualifier).matches();
}

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

public boolean isValid() {
return isValidQualifierPatter() && isValidValueType();
}

private boolean isValidValueType() {
if (ValueTypeConstants.PROTO.equals(valueType.toUpperCase())) {
return protoObjectDefinition != null;
}

return true;
}

private boolean isValidQualifierPatter() {
try {
getQualifierPattern();
return true;
} catch (Exception ex) {
return false;
}
}

@NotNull
private Pattern getQualifierPattern() {
return Pattern.compile(qualifier);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.erikmafo.btviewer.model;

import org.jetbrains.annotations.NotNull;

import java.util.Collection;
import java.util.Optional;

public class CellDefinitionMatcherUtil {

@NotNull
public static Optional<CellDefinition> findBestMatch(
@NotNull Collection<CellDefinition> cellDefinitions,
BigtableColumn column) {
return cellDefinitions
.stream()
.filter(c -> c.matchesExact(column))
.findFirst()
.or(() -> cellDefinitions
.stream()
.sorted(SortUtil::byQualifierLengthDescending)
.filter(c -> c.matches(column))
.findFirst());
}
}
16 changes: 10 additions & 6 deletions src/main/java/com/erikmafo/btviewer/model/SortUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,24 @@ public class SortUtil {

@Contract(pure = true)
public static int byFamilyThenQualifier(@NotNull BigtableColumn o1, @NotNull BigtableColumn o2) {
return String.CASE_INSENSITIVE_ORDER.compare(
concat(o1.getFamily(), o1.getQualifier()),
concat(o2.getFamily(), o2.getQualifier()));
return compare(o1.getFamily(), o1.getQualifier(), o2.getFamily(), o2.getQualifier());
}

@Contract(pure = true)
public static int byFamilyThenQualifier(@NotNull CellDefinition o1, @NotNull CellDefinition o2) {
return String.CASE_INSENSITIVE_ORDER.compare(
concat(o1.getFamily(), o1.getQualifier()),
concat(o2.getFamily(), o2.getQualifier()));
return compare(o1.getFamily(), o1.getQualifier(), o2.getFamily(), o2.getQualifier());
}

public static int byQualifierLengthDescending(@NotNull CellDefinition o1, @NotNull CellDefinition o2) {
return Integer.compare(o1.getQualifier().length(), o2.getQualifier().length());
}

@Contract(pure = true)
private static String concat(String s1, String s2) {
return String.format("%s%s", s1, s2);
}

private static int compare(String family1, String qualifier1, String family2, String qualifier2) {
return String.CASE_INSENSITIVE_ORDER.compare(concat(family1, qualifier1), concat(family2, qualifier2));
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package com.erikmafo.btviewer.model;

import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;

import java.util.Arrays;
import java.util.List;

public class ValueTypeConstants {

public static final String STRING = "STRING";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.erikmafo.btviewer.services.internal;

import com.erikmafo.btviewer.util.OperatingSystemUtil;
import com.erikmafo.btviewer.ui.util.OperatingSystemUtil;
import org.jetbrains.annotations.NotNull;

import java.nio.file.Path;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.erikmafo.btviewer.model.BigtableTable;
import com.erikmafo.btviewer.model.BigtableTableSettings;
import com.erikmafo.btviewer.services.internal.AppDataStorage;
import com.erikmafo.btviewer.util.AlertUtil;
import com.erikmafo.btviewer.ui.util.AlertUtil;
import javafx.concurrent.Service;
import javafx.concurrent.Task;

Expand Down

This file was deleted.

This file was deleted.

Loading

0 comments on commit 4e6f9ce

Please sign in to comment.