From 4ea787a665afb6190119c16c0251c02e04d4b767 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erik=20F=C3=B8lstad?= Date: Tue, 13 Apr 2021 00:43:16 +0200 Subject: [PATCH] Fix count column family (#69) --- .../services/query/QueryResultConverter.java | 24 ++++++++++++------- .../java/com/erikmafo/btviewer/sql/Field.java | 5 ++++ .../sql/functions/AggregationExpression.java | 3 ++- .../ui/menubar/MenuBarController.java | 3 ++- .../ui/projectexplorer/AddInstanceDialog.java | 3 ++- .../ui/projectexplorer/InstanceTreeItem.java | 3 ++- .../ProjectExplorerController.java | 10 ++++++-- .../ui/projectexplorer/ProjectTreeItem.java | 5 ++-- .../ui/projectexplorer/RootTreeItem.java | 5 ++-- .../ui/projectexplorer/TreeItemData.java | 7 +++++- .../ui/querybox/QueryBoxController.java | 3 +++ .../ui/querybox/SyntaxHighlightingUtil.java | 6 ++--- .../btviewer/ui/queryresult/CellView.java | 3 +++ .../QueryResultViewController.java | 12 +++++++--- .../ui/shared/CredentialsPathDialog.java | 5 ++-- .../ui/shared/TableSettingsDialog.java | 10 +++++--- 16 files changed, 77 insertions(+), 30 deletions(-) diff --git a/src/main/java/com/erikmafo/btviewer/services/query/QueryResultConverter.java b/src/main/java/com/erikmafo/btviewer/services/query/QueryResultConverter.java index be46e1a..1fbd22b 100644 --- a/src/main/java/com/erikmafo/btviewer/services/query/QueryResultConverter.java +++ b/src/main/java/com/erikmafo/btviewer/services/query/QueryResultConverter.java @@ -138,17 +138,25 @@ private Aggregation createAggregationEntry(AggregationExpression aggregationExpr } private double getDouble(BigtableCell cell) { - if (valueConverter.isNumberCellDefinition(cell)) { - return ((Number)valueConverter.convert(cell)).doubleValue(); - } else { - return 0; - } + return valueConverter.isNumberCellDefinition(cell) ? ((Number)valueConverter.convert(cell)).doubleValue() : 0; } private boolean matches(@NotNull Field field, @NotNull RowCell cell) { - return field.isAsterisk() || (field.hasQualifier() - && cell.getFamily().equalsIgnoreCase(field.getFamily()) - && cell.getQualifier().toStringUtf8().equalsIgnoreCase(field.getQualifier())); + if (field.isAsterisk()) { + return true; + } + + return field.hasQualifier() + ? matchesFamily(field, cell) && matchesQualifier(field, cell) + : matchesFamily(field, cell); + } + + private boolean matchesFamily(@NotNull Field field, @NotNull RowCell cell) { + return cell.getFamily().equalsIgnoreCase(field.getFamily()); + } + + private boolean matchesQualifier(@NotNull Field field, @NotNull RowCell cell) { + return cell.getQualifier().toStringUtf8().equalsIgnoreCase(field.getQualifier()); } private List getBigtableCells(@NotNull Row row) { diff --git a/src/main/java/com/erikmafo/btviewer/sql/Field.java b/src/main/java/com/erikmafo/btviewer/sql/Field.java index e406260..5ba7291 100644 --- a/src/main/java/com/erikmafo/btviewer/sql/Field.java +++ b/src/main/java/com/erikmafo/btviewer/sql/Field.java @@ -1,5 +1,8 @@ package com.erikmafo.btviewer.sql; +import org.jetbrains.annotations.Contract; +import org.jetbrains.annotations.NotNull; + import java.util.Objects; public class Field { @@ -64,6 +67,8 @@ public int hashCode() { return Objects.hash(name); } + @NotNull + @Contract(pure = true) private String[] getFamilyAndQualifier() { return name.split("\\."); } diff --git a/src/main/java/com/erikmafo/btviewer/sql/functions/AggregationExpression.java b/src/main/java/com/erikmafo/btviewer/sql/functions/AggregationExpression.java index 4ca237f..57486e3 100644 --- a/src/main/java/com/erikmafo/btviewer/sql/functions/AggregationExpression.java +++ b/src/main/java/com/erikmafo/btviewer/sql/functions/AggregationExpression.java @@ -2,6 +2,7 @@ import com.erikmafo.btviewer.sql.Field; import com.erikmafo.btviewer.sql.SqlToken; +import org.jetbrains.annotations.NotNull; /** * Represents an aggregation of a field in a sql query. @@ -26,7 +27,7 @@ public enum Type { * @return an {@link AggregationExpression} * @throws IllegalArgumentException if the sql token cannot be evaluated into an AggregationExpression. */ - public static AggregationExpression from(SqlToken token) { + public static AggregationExpression from(@NotNull SqlToken token) { return AggregationExpressionParser.parse(token.getSubTokens()); } diff --git a/src/main/java/com/erikmafo/btviewer/ui/menubar/MenuBarController.java b/src/main/java/com/erikmafo/btviewer/ui/menubar/MenuBarController.java index b1b0e57..195c356 100644 --- a/src/main/java/com/erikmafo/btviewer/ui/menubar/MenuBarController.java +++ b/src/main/java/com/erikmafo/btviewer/ui/menubar/MenuBarController.java @@ -12,6 +12,7 @@ import javafx.scene.control.MenuBar; import javafx.scene.control.MenuItem; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import javax.inject.Inject; import java.nio.file.Path; @@ -58,7 +59,7 @@ private void displaySpecifyCredentialsDialog(Path currentPath) { .whenComplete(this::onCredentialsPathDialogComplete); } - private void onCredentialsPathDialogComplete(Path path, Throwable throwable) { + private void onCredentialsPathDialogComplete(@Nullable Path path, @Nullable Throwable throwable) { if (throwable != null) { displayError(throwable); } diff --git a/src/main/java/com/erikmafo/btviewer/ui/projectexplorer/AddInstanceDialog.java b/src/main/java/com/erikmafo/btviewer/ui/projectexplorer/AddInstanceDialog.java index cbf9fbc..23240b1 100644 --- a/src/main/java/com/erikmafo/btviewer/ui/projectexplorer/AddInstanceDialog.java +++ b/src/main/java/com/erikmafo/btviewer/ui/projectexplorer/AddInstanceDialog.java @@ -9,6 +9,7 @@ import javafx.scene.control.TextField; import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.util.concurrent.CompletableFuture; @@ -30,7 +31,7 @@ public static CompletableFuture displayAndAwaitResult() { } @NotNull - public static CompletableFuture displayAndAwaitResult(String projectId) { + public static CompletableFuture displayAndAwaitResult(@Nullable String projectId) { CompletableFuture future = new CompletableFuture<>(); Dialog dialog = new Dialog<>(); diff --git a/src/main/java/com/erikmafo/btviewer/ui/projectexplorer/InstanceTreeItem.java b/src/main/java/com/erikmafo/btviewer/ui/projectexplorer/InstanceTreeItem.java index a72648b..de45dd5 100644 --- a/src/main/java/com/erikmafo/btviewer/ui/projectexplorer/InstanceTreeItem.java +++ b/src/main/java/com/erikmafo/btviewer/ui/projectexplorer/InstanceTreeItem.java @@ -10,11 +10,12 @@ public class InstanceTreeItem extends TreeItem { + @NotNull private final ListTablesService listTablesService; private boolean loadedChildren; @Inject - public InstanceTreeItem(ListTablesService listTablesService) { + public InstanceTreeItem(@NotNull ListTablesService listTablesService) { this.listTablesService = listTablesService; this.expandedProperty().addListener((observable, prev, isExpanded) -> { if (isExpanded && !loadedChildren && !listTablesService.isRunning()) { diff --git a/src/main/java/com/erikmafo/btviewer/ui/projectexplorer/ProjectExplorerController.java b/src/main/java/com/erikmafo/btviewer/ui/projectexplorer/ProjectExplorerController.java index fe8c167..c07911a 100644 --- a/src/main/java/com/erikmafo/btviewer/ui/projectexplorer/ProjectExplorerController.java +++ b/src/main/java/com/erikmafo/btviewer/ui/projectexplorer/ProjectExplorerController.java @@ -18,6 +18,7 @@ import javafx.scene.control.TreeCell; import javafx.scene.control.TreeView; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import javax.inject.Inject; @@ -31,7 +32,9 @@ public class ProjectExplorerController { private final Provider rootTreeItemProvider; + @NotNull private final SimpleObjectProperty selectedTableProperty; + @NotNull private final SimpleObjectProperty selectedInstanceProperty; private final SaveInstanceService saveInstanceService; @@ -55,7 +58,7 @@ public void initialize() { treeView.setCellFactory(tableInfoTreeView -> new TreeCell<>() { @Override - protected void updateItem(TreeItemData item, boolean empty) { + protected void updateItem(@Nullable TreeItemData item, boolean empty) { super.updateItem(item, empty); if (empty || item == null) { setText(null); @@ -91,14 +94,17 @@ protected void updateItem(TreeItemData item, boolean empty) { addInstanceButton.setOnAction(this::handleAddInstanceAction); } + @NotNull public ReadOnlyObjectProperty selectedInstanceProperty() { return selectedInstanceProperty; } + @NotNull public ReadOnlyObjectProperty selectedTableProperty() { return selectedTableProperty; } + @Nullable public ContextMenu createContextMenu(@NotNull TreeItemData item){ ContextMenu menu = null; if (item.isProject()) { @@ -129,7 +135,7 @@ private void handleAddInstanceAction(ActionEvent ignore) { AddInstanceDialog.displayAndAwaitResult().whenComplete(this::handleAddInstanceResult); } - private void handleAddInstanceResult(BigtableInstance instance, Throwable throwable) { + private void handleAddInstanceResult(@Nullable BigtableInstance instance, Throwable throwable) { if (instance == null) { return; } diff --git a/src/main/java/com/erikmafo/btviewer/ui/projectexplorer/ProjectTreeItem.java b/src/main/java/com/erikmafo/btviewer/ui/projectexplorer/ProjectTreeItem.java index bda5659..7fe9c45 100644 --- a/src/main/java/com/erikmafo/btviewer/ui/projectexplorer/ProjectTreeItem.java +++ b/src/main/java/com/erikmafo/btviewer/ui/projectexplorer/ProjectTreeItem.java @@ -12,13 +12,14 @@ public class ProjectTreeItem extends TreeItem { + @NotNull private final LoadInstancesService loadInstancesService; private final Provider instanceTreeItemProvider; private boolean loadedChildren; @Inject - public ProjectTreeItem(LoadInstancesService loadInstancesService, + public ProjectTreeItem(@NotNull LoadInstancesService loadInstancesService, Provider instanceTreeItemProvider) { this.loadInstancesService = loadInstancesService; this.instanceTreeItemProvider = instanceTreeItemProvider; @@ -53,7 +54,7 @@ private void onLoadInstancesSucceeded(WorkerStateEvent event) { } @NotNull - private InstanceTreeItem createChild(TreeItemData treeItemData) { + private InstanceTreeItem createChild(@NotNull TreeItemData treeItemData) { var instanceItem = instanceTreeItemProvider.get(); instanceItem.setValue(treeItemData); treeItemData.setTreeItem(instanceItem); diff --git a/src/main/java/com/erikmafo/btviewer/ui/projectexplorer/RootTreeItem.java b/src/main/java/com/erikmafo/btviewer/ui/projectexplorer/RootTreeItem.java index 59b4322..f27a2c8 100644 --- a/src/main/java/com/erikmafo/btviewer/ui/projectexplorer/RootTreeItem.java +++ b/src/main/java/com/erikmafo/btviewer/ui/projectexplorer/RootTreeItem.java @@ -11,12 +11,13 @@ public class RootTreeItem extends TreeItem { + @NotNull private final LoadProjectsService loadProjectsService; private final Provider projectTreeItemProvider; @Inject public RootTreeItem( - LoadProjectsService loadProjectsService, + @NotNull LoadProjectsService loadProjectsService, Provider projectTreeItemProvider) { this.projectTreeItemProvider = projectTreeItemProvider; this.loadProjectsService = loadProjectsService; @@ -44,7 +45,7 @@ public boolean isLeaf() { return false; } - private void loadChildren(LoadProjectsService loadProjectsService) { + private void loadChildren(@NotNull LoadProjectsService loadProjectsService) { this.loadProjectsService.setOnSucceeded(event -> { var children = loadProjectsService .getValue() diff --git a/src/main/java/com/erikmafo/btviewer/ui/projectexplorer/TreeItemData.java b/src/main/java/com/erikmafo/btviewer/ui/projectexplorer/TreeItemData.java index 53f2721..c18bc1d 100644 --- a/src/main/java/com/erikmafo/btviewer/ui/projectexplorer/TreeItemData.java +++ b/src/main/java/com/erikmafo/btviewer/ui/projectexplorer/TreeItemData.java @@ -8,6 +8,7 @@ import javafx.beans.property.SimpleObjectProperty; import javafx.scene.control.TreeItem; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.util.Objects; @@ -74,10 +75,12 @@ public String getDisplayName() { return displayName; } + @NotNull public BigtableTable toTable() { return new BigtableTable(projectId, instanceId, tableId); } + @NotNull public BigtableInstance toInstance() { return new BigtableInstance(projectId, instanceId); } @@ -91,7 +94,7 @@ public String getInstanceId() { } @Override - public boolean equals(Object o) { + public boolean equals(@Nullable Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; @@ -114,6 +117,7 @@ public boolean isLoading() { return loading.get(); } + @NotNull public BooleanProperty loadingProperty() { return loading; } @@ -126,6 +130,7 @@ public TreeItem getTreeItem() { return treeItem.get(); } + @NotNull public ObjectProperty> treeItemProperty() { return treeItem; } diff --git a/src/main/java/com/erikmafo/btviewer/ui/querybox/QueryBoxController.java b/src/main/java/com/erikmafo/btviewer/ui/querybox/QueryBoxController.java index d576c64..b544c8a 100644 --- a/src/main/java/com/erikmafo/btviewer/ui/querybox/QueryBoxController.java +++ b/src/main/java/com/erikmafo/btviewer/ui/querybox/QueryBoxController.java @@ -80,8 +80,10 @@ public void initialize() { bigtableQueryService.runningProperty().addListener((obs, wasRunning, isRunning) -> updateTimer(isRunning)); } + @NotNull public ObjectProperty tableProperty() { return table; } + @NotNull public ObservableList getQueryResult() { return queryResult; } public void setQuery(String sql) { @@ -89,6 +91,7 @@ public void setQuery(String sql) { codeArea.replaceText(0, 0, sql); } + @NotNull public ObjectProperty instanceProperty() { return instance; } diff --git a/src/main/java/com/erikmafo/btviewer/ui/querybox/SyntaxHighlightingUtil.java b/src/main/java/com/erikmafo/btviewer/ui/querybox/SyntaxHighlightingUtil.java index 8bf64ba..2846d2f 100644 --- a/src/main/java/com/erikmafo/btviewer/ui/querybox/SyntaxHighlightingUtil.java +++ b/src/main/java/com/erikmafo/btviewer/ui/querybox/SyntaxHighlightingUtil.java @@ -20,7 +20,7 @@ public class SyntaxHighlightingUtil { private static final String FUNCTION_STYLE_CLASS = "function"; private static final String OPERATOR_STYLE_CLASS = "logical-operator"; - public static StyleSpans> computeSyntaxHighlighting(String queryText) { + public static StyleSpans> computeSyntaxHighlighting(@NotNull String queryText) { var sqlTokenizer = new SqlTokenizer(queryText); var token = sqlTokenizer.next(); int lastHighlightEnd = 0; @@ -35,7 +35,7 @@ public static StyleSpans> computeSyntaxHighlighting(String qu return spansBuilder.create(); } - private static int applyStyleClass(SqlToken token, int lastHighlightEnd, StyleSpansBuilder> spansBuilder) { + private static int applyStyleClass(@NotNull SqlToken token, int lastHighlightEnd, @NotNull StyleSpansBuilder> spansBuilder) { var styleClass = getStyleClass(token); if (styleClass != null) { spansBuilder.add(Collections.emptyList(), token.getStart() - lastHighlightEnd); @@ -46,7 +46,7 @@ private static int applyStyleClass(SqlToken token, int lastHighlightEnd, StyleSp } @NotNull - private static List getTokenAndSubTokens(SqlToken token) { + private static List getTokenAndSubTokens(@NotNull SqlToken token) { var tokens = new ArrayList(); tokens.add(token); if (token.getSubTokens() != null) { diff --git a/src/main/java/com/erikmafo/btviewer/ui/queryresult/CellView.java b/src/main/java/com/erikmafo/btviewer/ui/queryresult/CellView.java index 2d6c4cd..7471050 100644 --- a/src/main/java/com/erikmafo/btviewer/ui/queryresult/CellView.java +++ b/src/main/java/com/erikmafo/btviewer/ui/queryresult/CellView.java @@ -11,6 +11,7 @@ import javafx.fxml.FXML; import javafx.scene.control.Label; import javafx.scene.layout.BorderPane; +import org.jetbrains.annotations.NotNull; import java.time.Instant; import java.time.ZoneId; @@ -53,10 +54,12 @@ public boolean getDisplayTimestamp() { return displayTimestamp.get(); } + @NotNull public BooleanProperty displayTimestampProperty() { return displayTimestamp; } + @NotNull public ObjectProperty valueConverterProperty() { return valueConverter; } private String getDisplayValue() { diff --git a/src/main/java/com/erikmafo/btviewer/ui/queryresult/QueryResultViewController.java b/src/main/java/com/erikmafo/btviewer/ui/queryresult/QueryResultViewController.java index 0862392..358942e 100644 --- a/src/main/java/com/erikmafo/btviewer/ui/queryresult/QueryResultViewController.java +++ b/src/main/java/com/erikmafo/btviewer/ui/queryresult/QueryResultViewController.java @@ -39,6 +39,7 @@ import javafx.scene.layout.VBox; import javafx.util.Callback; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import javax.inject.Inject; import java.util.Collections; @@ -61,6 +62,7 @@ public class QueryResultViewController { @FXML private TreeTableView tableView; + @Nullable private final BigtableRowTreeItem root; private final SimpleObjectProperty table = new SimpleObjectProperty<>(); @@ -106,7 +108,7 @@ public void handleTableSettingsButtonPressed(ActionEvent actionEvent) { } @FXML - public void onTableViewKeyPressed(KeyEvent keyEvent) { + public void onTableViewKeyPressed(@NotNull KeyEvent keyEvent) { if (isCopyOperation(keyEvent)) { copySelectedCellsToClipboard(); } @@ -126,6 +128,7 @@ public void setRows(@NotNull ObservableList rows) { rows.addListener(this::onBigtableRowsChange); } + @NotNull public SimpleObjectProperty tableProperty() { return table; } @@ -142,7 +145,7 @@ private void onBigtableRowsChange(@NotNull ListChangeListener.Change getQualifierTableColumn(St .orElse(null); } - private void updateTableConfiguration(BigtableTable table, BigtableTableSettings configuration) { + private void updateTableConfiguration(BigtableTable table, @Nullable BigtableTableSettings configuration) { if (configuration == null) { return; } @@ -284,6 +287,7 @@ private static class CellValueFactory implements Callback call(@NotNull TreeTableColumn.CellDataFeatures param) { return new ReadOnlyObjectWrapper<>(param.getValue().getValue().getLatestCell(family, qualifier)); @@ -292,6 +296,7 @@ public ObservableValue call(@NotNull TreeTableColumn.CellDataFeatu private static class RowKeyTableCellFactory implements Callback, TreeTableCell> { + @NotNull @Override public TreeTableCell call(TreeTableColumn column) { return new TreeTableCell<>() { @@ -312,6 +317,7 @@ protected void updateItem(String rowKey, boolean empty) { private class CellFactory implements Callback, TreeTableCell> { + @NotNull @Override public TreeTableCell call(TreeTableColumn column) { return new TreeTableCell<>() { diff --git a/src/main/java/com/erikmafo/btviewer/ui/shared/CredentialsPathDialog.java b/src/main/java/com/erikmafo/btviewer/ui/shared/CredentialsPathDialog.java index d995144..0ef7234 100644 --- a/src/main/java/com/erikmafo/btviewer/ui/shared/CredentialsPathDialog.java +++ b/src/main/java/com/erikmafo/btviewer/ui/shared/CredentialsPathDialog.java @@ -10,6 +10,7 @@ import javafx.scene.control.TextField; import javafx.stage.FileChooser; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.nio.file.Files; import java.nio.file.InvalidPathException; @@ -29,7 +30,7 @@ public CredentialsPathDialog() { } @NotNull - public static CompletableFuture displayAndAwaitResult(Path currentPath) { + public static CompletableFuture displayAndAwaitResult(@Nullable Path currentPath) { CompletableFuture result = new CompletableFuture<>(); Dialog dialog = new Dialog<>(); var credentialsPathDialog = new CredentialsPathDialog(); @@ -68,7 +69,7 @@ public void handleEditCredentialsPathAction(ActionEvent event) { } } - private static boolean validatePath(String pathAsString) { + private static boolean validatePath(@Nullable String pathAsString) { if (pathAsString == null) { return false; } diff --git a/src/main/java/com/erikmafo/btviewer/ui/shared/TableSettingsDialog.java b/src/main/java/com/erikmafo/btviewer/ui/shared/TableSettingsDialog.java index d328757..59c4449 100644 --- a/src/main/java/com/erikmafo/btviewer/ui/shared/TableSettingsDialog.java +++ b/src/main/java/com/erikmafo/btviewer/ui/shared/TableSettingsDialog.java @@ -16,6 +16,7 @@ import javafx.scene.layout.GridPane; import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.util.ArrayList; import java.util.Arrays; @@ -42,7 +43,7 @@ private TableSettingsDialog() { @NotNull public static CompletableFuture displayAndAwaitResult( @NotNull List columns, - BigtableTableSettings currentSettings) { + @Nullable BigtableTableSettings currentSettings) { var settings = currentSettings != null ? currentSettings : new BigtableTableSettings(); CompletableFuture future = new CompletableFuture<>(); @@ -97,7 +98,7 @@ private void addSchemaRow() { addSchemaRow(new BigtableColumn("", "")); } - private void addSchemaRow(BigtableColumn column) { + private void addSchemaRow(@NotNull BigtableColumn column) { addSchemaRow(column, null); } @@ -106,7 +107,7 @@ private void addSchemaRow(@NotNull CellDefinition cellDefinition) { cellDefinition.getFamily(), cellDefinition.getQualifier()), cellDefinition.getValueType()); } - private void addSchemaRow(BigtableColumn column, String valueType) { + private void addSchemaRow(@NotNull BigtableColumn column, @Nullable String valueType) { var alreadyAdded = observableCells.stream().anyMatch(cell -> cell.getFamily().equals(column.getFamily()) && cell.getQualifier().equals(column.getQualifier())); @@ -141,6 +142,7 @@ public String getValueType() { return valueType.get(); } + @NotNull public StringProperty valueTypeProperty() { return valueType; } @@ -153,6 +155,7 @@ public String getFamily() { return family.get(); } + @NotNull public StringProperty familyProperty() { return family; } @@ -165,6 +168,7 @@ public String getQualifier() { return qualifier.get(); } + @NotNull public StringProperty qualifierProperty() { return qualifier; }