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. (#156)
  • Loading branch information
erikmafo authored Aug 21, 2022
1 parent 410390f commit 0918e5f
Show file tree
Hide file tree
Showing 41 changed files with 639 additions and 448 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,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
22 changes: 15 additions & 7 deletions src/main/java/com/erikmafo/btviewer/sql/DateTimeFormatUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class DateTimeFormatUtil {
private static final String DATE_HOUR_MINUTE = "uuuu-MM-dd HH:mm";
private static final String DATE_HOUR_MINUTE_SECOND = "uuuu-MM-dd HH:mm:ss";
private static final String DATE_HOUR_MINUTE_SECOND_MILLIS = "uuuu-MM-dd HH:mm:ss:SS";
private static final LocalTime MIDNIGHT_LOCAL_TIME = LocalTime.of(0, 0, 0);

/**
* Converts a date time string to the number of microseconds since 1970-01-01T00:00:00Z.
Expand All @@ -30,10 +31,13 @@ public static long toMicros(@NotNull String dateTime) {
}

private static long toEpochMilli(@NotNull String dateTime) {
return toMillis(dateTime, getDateFormat(dateTime));
}

@NotNull
private static String getDateFormat(@NotNull String dateTime) {
String format;
var isDate = false;
if (dateTime.length() == DATE.length()) {
isDate = true;
format = DATE;
} else if (dateTime.length() == DATE_HOUR.length()) {
format = DATE_HOUR;
Expand All @@ -46,16 +50,16 @@ private static long toEpochMilli(@NotNull String dateTime) {
} else {
throw new IllegalArgumentException(getErrorMessage(dateTime));
}
return toMillis(dateTime, format, isDate);
return format;
}

private static long toMillis(@NotNull String dateTime, String format, boolean isDate) {
private static long toMillis(@NotNull String dateTime, String format) {
var trimmedDateTime = dateTime.trim();
var offset = ZoneOffset.ofHours(0);
var formatter = DateTimeFormatter.ofPattern(format);
var dt = isDate ?
LocalDateTime.of(LocalDate.parse(trimmedDateTime, formatter), LocalTime.of(0, 0, 0)) :
LocalDateTime.parse(trimmedDateTime, formatter);
var dt = isDate(format)
? LocalDateTime.of(LocalDate.parse(trimmedDateTime, formatter), MIDNIGHT_LOCAL_TIME)
: LocalDateTime.parse(trimmedDateTime, formatter);
return dt.toInstant(offset).toEpochMilli();
}

Expand All @@ -75,4 +79,8 @@ private static List<String> supportedFormats() {
DATE_HOUR_MINUTE_SECOND_MILLIS
);
}

private static boolean isDate(String format) {
return DATE.equals(format);
}
}

This file was deleted.

This file was deleted.

Loading

0 comments on commit 0918e5f

Please sign in to comment.