From 618a7ca5309f648eb5b67d515b77703824141773 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erik=20F=C3=B8lstad?= Date: Sun, 28 Jun 2020 15:18:35 +0200 Subject: [PATCH] Release/0.1.7 (#7) * Upgrade bigtable library * Use bigtable emulator * Refactor to use javafx services * Improve error description --- pom.xml | 17 +- .../java/com/erikmafo/btviewer/MainApp.java | 45 +---- .../components/BigtableTableView.java | 8 +- .../erikmafo/btviewer/config/AppConfig.java | 10 +- .../btviewer/controllers/MainController.java | 138 ++++++++------- .../controllers/MenuBarController.java | 64 +++++-- .../erikmafo/btviewer/model/BigtableCell.java | 2 +- .../btviewer/model/BigtableInstance.java | 16 ++ .../btviewer/model/BigtableTable.java | 13 +- .../model/BigtableValueConverter.java | 13 ++ .../btviewer/model/CellDefinition.java | 16 ++ .../btviewer/services/BigtableClient.java | 17 -- .../btviewer/services/BigtableClientImpl.java | 161 ------------------ .../services/BigtableResultScanner.java | 59 ------- .../btviewer/services/ListBigtableTables.java | 42 ----- .../btviewer/services/ListTablesService.java | 76 +++++++++ .../services/LoadCredentialsPathService.java | 28 +++ .../services/LoadInstancesService.java | 29 ++++ .../LoadTableConfigurationService.java | 34 ++++ .../btviewer/services/ReadBigtableRows.java | 29 ---- .../btviewer/services/ReadRowsService.java | 115 +++++++++++++ .../services/SaveCredentialsPathService.java | 35 ++++ .../services/SaveInstancesService.java | 44 +++++ .../SaveTableConfigurationService.java | 38 +++++ .../btviewer/services/ServicesModule.java | 48 ++++++ .../inmemory/InMemoryBigtableClient.java | 51 ------ .../services/{ => internal}/AppDataUtil.java | 4 +- .../BigtableInstanceManager.java | 2 +- .../BigtableInstanceManagerImpl.java | 2 +- .../internal/BigtableSettingsProvider.java | 14 ++ .../BigtableSettingsProviderImpl.java | 39 +++++ .../{ => internal}/CredentialsManager.java | 2 +- .../internal/DynamicCredentialsProvider.java | 36 ++++ .../{ => internal}/TableConfigManager.java | 2 +- .../TableConfigManagerImpl.java | 2 +- .../BigtableEmulatorSettingsProvider.java | 57 +++++++ .../inmemory/InMemoryInstanceManager.java | 5 +- .../inmemory/InMemoryTableConfigManager.java | 4 +- .../{ => internal}/inmemory/TestDataUtil.java | 47 +++-- src/main/resources/config.properties | 2 +- 40 files changed, 839 insertions(+), 527 deletions(-) delete mode 100644 src/main/java/com/erikmafo/btviewer/services/BigtableClient.java delete mode 100644 src/main/java/com/erikmafo/btviewer/services/BigtableClientImpl.java delete mode 100644 src/main/java/com/erikmafo/btviewer/services/BigtableResultScanner.java delete mode 100644 src/main/java/com/erikmafo/btviewer/services/ListBigtableTables.java create mode 100644 src/main/java/com/erikmafo/btviewer/services/ListTablesService.java create mode 100644 src/main/java/com/erikmafo/btviewer/services/LoadCredentialsPathService.java create mode 100644 src/main/java/com/erikmafo/btviewer/services/LoadInstancesService.java create mode 100644 src/main/java/com/erikmafo/btviewer/services/LoadTableConfigurationService.java delete mode 100644 src/main/java/com/erikmafo/btviewer/services/ReadBigtableRows.java create mode 100644 src/main/java/com/erikmafo/btviewer/services/ReadRowsService.java create mode 100644 src/main/java/com/erikmafo/btviewer/services/SaveCredentialsPathService.java create mode 100644 src/main/java/com/erikmafo/btviewer/services/SaveInstancesService.java create mode 100644 src/main/java/com/erikmafo/btviewer/services/SaveTableConfigurationService.java create mode 100644 src/main/java/com/erikmafo/btviewer/services/ServicesModule.java delete mode 100644 src/main/java/com/erikmafo/btviewer/services/inmemory/InMemoryBigtableClient.java rename src/main/java/com/erikmafo/btviewer/services/{ => internal}/AppDataUtil.java (90%) rename src/main/java/com/erikmafo/btviewer/services/{ => internal}/BigtableInstanceManager.java (85%) rename src/main/java/com/erikmafo/btviewer/services/{ => internal}/BigtableInstanceManagerImpl.java (97%) create mode 100644 src/main/java/com/erikmafo/btviewer/services/internal/BigtableSettingsProvider.java create mode 100644 src/main/java/com/erikmafo/btviewer/services/internal/BigtableSettingsProviderImpl.java rename src/main/java/com/erikmafo/btviewer/services/{ => internal}/CredentialsManager.java (96%) create mode 100644 src/main/java/com/erikmafo/btviewer/services/internal/DynamicCredentialsProvider.java rename src/main/java/com/erikmafo/btviewer/services/{ => internal}/TableConfigManager.java (89%) rename src/main/java/com/erikmafo/btviewer/services/{ => internal}/TableConfigManagerImpl.java (97%) create mode 100644 src/main/java/com/erikmafo/btviewer/services/internal/inmemory/BigtableEmulatorSettingsProvider.java rename src/main/java/com/erikmafo/btviewer/services/{ => internal}/inmemory/InMemoryInstanceManager.java (82%) rename src/main/java/com/erikmafo/btviewer/services/{ => internal}/inmemory/InMemoryTableConfigManager.java (83%) rename src/main/java/com/erikmafo/btviewer/services/{ => internal}/inmemory/TestDataUtil.java (50%) diff --git a/pom.xml b/pom.xml index 2f09d0c..daa64c5 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ com.erikmafo bigtableviewer - 0.1.6 + 0.1.7 jar bigtableviewer @@ -173,6 +173,16 @@ + + com.google.cloud + google-cloud-bigtable + 1.13.0 + + + com.google.cloud + google-cloud-bigtable-emulator + 0.122.0 + org.openjfx javafx-controls @@ -185,11 +195,6 @@ 11.0.1 compile - - com.google.cloud.bigtable - bigtable-hbase-1.x-shaded - 1.7.0 - javax.inject javax.inject diff --git a/src/main/java/com/erikmafo/btviewer/MainApp.java b/src/main/java/com/erikmafo/btviewer/MainApp.java index 19334fa..acd1cd3 100644 --- a/src/main/java/com/erikmafo/btviewer/MainApp.java +++ b/src/main/java/com/erikmafo/btviewer/MainApp.java @@ -1,14 +1,6 @@ package com.erikmafo.btviewer; -import com.erikmafo.btviewer.config.AppConfig; -import com.erikmafo.btviewer.config.ApplicationEnvironment; import com.erikmafo.btviewer.services.*; -import com.erikmafo.btviewer.services.inmemory.InMemoryBigtableClient; -import com.erikmafo.btviewer.services.inmemory.InMemoryInstanceManager; -import com.erikmafo.btviewer.services.inmemory.InMemoryTableConfigManager; -import com.erikmafo.btviewer.services.inmemory.TestDataUtil; -import com.google.inject.Binder; import com.google.inject.Guice; -import com.google.inject.Module; import javafx.application.Application; import javafx.fxml.FXMLLoader; import javafx.scene.Parent; @@ -18,7 +10,7 @@ /** * Created by erikmafo on 12.12.17. */ -public class MainApp extends Application implements Module { +public class MainApp extends Application { public static void main(String[] args) { launch(args); @@ -27,7 +19,7 @@ public static void main(String[] args) { @Override public void start(Stage primaryStage) throws Exception{ FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/main.fxml")); - loader.setControllerFactory(Guice.createInjector(this)::getInstance); + loader.setControllerFactory(Guice.createInjector(new ServicesModule())::getInstance); Parent root = loader.load(); primaryStage.setTitle("Bigtable viewer"); primaryStage.setScene(new Scene(root, 800, 700)); @@ -38,37 +30,4 @@ public void start(Stage primaryStage) throws Exception{ public void stop() throws Exception { super.stop(); } - - @Override - public void configure(Binder binder) { - - var config = AppConfig.load(ApplicationEnvironment.get()); - - if (config.useInMemoryBigtableClient()) { - var inMemoryBigtableClient = new InMemoryBigtableClient(); - TestDataUtil.injectWithTestData(inMemoryBigtableClient); - binder.bind(BigtableClient.class).toInstance(inMemoryBigtableClient); - } - else { - binder.bind(BigtableClient.class).toInstance(new BigtableClientImpl()); - } - - if (config.useInMemoryTableConfigManager()) { - var inMemoryTableConfigManager = new InMemoryTableConfigManager(); - binder.bind(TableConfigManager.class).toInstance(inMemoryTableConfigManager); - } - else { - binder.bind(TableConfigManager.class).toInstance(new TableConfigManagerImpl()); - } - - if (config.useInMemoryInstanceManager()) { - var inMemoryInstanceManager = new InMemoryInstanceManager(); - TestDataUtil.injectWithTestData(inMemoryInstanceManager); - binder.bind(BigtableInstanceManager.class).toInstance(inMemoryInstanceManager); - } else { - binder.bind(BigtableInstanceManager.class).toInstance(new BigtableInstanceManagerImpl()); - } - - binder.bind(CredentialsManager.class).toInstance(new CredentialsManager()); - } } diff --git a/src/main/java/com/erikmafo/btviewer/components/BigtableTableView.java b/src/main/java/com/erikmafo/btviewer/components/BigtableTableView.java index a05be62..be876e9 100644 --- a/src/main/java/com/erikmafo/btviewer/components/BigtableTableView.java +++ b/src/main/java/com/erikmafo/btviewer/components/BigtableTableView.java @@ -123,8 +123,8 @@ private void addColumn(String family, String qualifier) { private TableColumn getQualifierTableColumn(String family, String qualifier) { TableColumn qualifierColumn = new TableColumn<>(qualifier); qualifierColumn.setCellValueFactory(param -> { - BigtableRow bigtableRow = param.getValue(); - return new ReadOnlyObjectWrapper<>(bigtableRow.getCellValue(family, qualifier, valueConverter)); + var row = param.getValue(); + return new ReadOnlyObjectWrapper<>(row.getCellValue(family, qualifier, valueConverter)); }); return qualifierColumn; } @@ -150,6 +150,10 @@ private void setBigTableRows(ObservableList bigtableRows) { } public void setValueConverter(BigtableValueConverter valueConverter) { + if (valueConverter.equals(this.valueConverter)) { + return; + } + this.valueConverter = valueConverter; var rows = this.tableView.getItems(); clear(); diff --git a/src/main/java/com/erikmafo/btviewer/config/AppConfig.java b/src/main/java/com/erikmafo/btviewer/config/AppConfig.java index 8fe24c2..39585e2 100644 --- a/src/main/java/com/erikmafo/btviewer/config/AppConfig.java +++ b/src/main/java/com/erikmafo/btviewer/config/AppConfig.java @@ -15,21 +15,21 @@ private static String getConfigName(ApplicationEnvironment environment) { String.format("config.%s.properties", environment.getName()); } - private final boolean useInMemoryBigtableClient; + private final boolean useBigtableEmulator; private final boolean useInMemoryTableConfigManager; private final boolean useInMemoryInstanceManager; @Inject - public AppConfig(@Named("USE_IN_MEMORY_BIGTABLE_CLIENT") boolean useInMemoryBigtableClient, + public AppConfig(@Named("USE_BIGTABLE_EMULATOR") boolean useBigtableEmulator, @Named("USE_IN_MEMORY_TABLE_CONFIG_MANAGER") boolean useInMemoryTableConfigManager, @Named("USE_IN_MEMORY_INSTANCE_MANAGER") boolean useInMemoryInstanceManager) { - this.useInMemoryBigtableClient = useInMemoryBigtableClient; + this.useBigtableEmulator = useBigtableEmulator; this.useInMemoryTableConfigManager = useInMemoryTableConfigManager; this.useInMemoryInstanceManager = useInMemoryInstanceManager; } - public boolean useInMemoryBigtableClient() { - return useInMemoryBigtableClient; + public boolean useBigtableEmulator() { + return useBigtableEmulator; } public boolean useInMemoryTableConfigManager() { diff --git a/src/main/java/com/erikmafo/btviewer/controllers/MainController.java b/src/main/java/com/erikmafo/btviewer/controllers/MainController.java index bda2b5d..97e83ab 100644 --- a/src/main/java/com/erikmafo/btviewer/controllers/MainController.java +++ b/src/main/java/com/erikmafo/btviewer/controllers/MainController.java @@ -5,14 +5,14 @@ import com.erikmafo.btviewer.model.*; import com.erikmafo.btviewer.services.*; import javafx.beans.value.ObservableValue; +import javafx.concurrent.WorkerStateEvent; import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.scene.control.Alert; import javafx.scene.control.ButtonType; import javafx.scene.control.Label; + import javax.inject.Inject; -import java.io.IOException; -import java.util.List; /** * Created by erikmafo on 23.12.17. @@ -31,20 +31,30 @@ public class MainController { @FXML private BigtableTableView bigtableTableView; - private final CredentialsManager credentialsManager; - private final BigtableInstanceManager bigtableInstanceManager; - private final TableConfigManager tableConfigManager; - private final BigtableClient bigtableClient; + private final SaveCredentialsPathService saveCredentialsPathService; + private final SaveInstancesService saveInstancesService; + private final LoadInstancesService loadInstancesService; + private final SaveTableConfigurationService saveTableConfigurationService; + private final LoadTableConfigurationService loadTableConfigurationService; + private final ReadRowsService readRowsService; + private final ListTablesService listTablesService; @Inject - public MainController(CredentialsManager credentialsManager, - BigtableInstanceManager bigtableInstanceManager, - TableConfigManager tableConfigManager, - BigtableClient bigtableClient) { - this.credentialsManager = credentialsManager; - this.bigtableInstanceManager = bigtableInstanceManager; - this.tableConfigManager = tableConfigManager; - this.bigtableClient = bigtableClient; + public MainController( + SaveCredentialsPathService saveCredentialsPathService, + SaveInstancesService saveInstancesService, + LoadInstancesService loadInstancesService, + SaveTableConfigurationService saveTableConfigurationService, + LoadTableConfigurationService loadTableConfigurationService, + ReadRowsService readRowsService, + ListTablesService listTablesService) { + this.saveCredentialsPathService = saveCredentialsPathService; + this.saveInstancesService = saveInstancesService; + this.loadInstancesService = loadInstancesService; + this.saveTableConfigurationService = saveTableConfigurationService; + this.loadTableConfigurationService = loadTableConfigurationService; + this.readRowsService = readRowsService; + this.listTablesService = listTablesService; } public void initialize() { @@ -61,12 +71,10 @@ public void initialize() { } private void loadBigtableInstances() { - try { - tablesListView.addBigtableInstances(bigtableInstanceManager.getInstances()); - } catch (IOException e) { - e.printStackTrace(); - displayErrorInfo("Unable to load bigtable instances"); - } + loadInstancesService.setOnSucceeded(stateEvent -> + tablesListView.addBigtableInstances(loadInstancesService.getValue())); + loadInstancesService.setOnFailed(stateEvent -> displayErrorInfo("Failed to load instances", stateEvent)); + loadInstancesService.restart(); } private void onAddNewBigtableInstance(ActionEvent event) { @@ -79,76 +87,66 @@ private void onAddNewBigtableInstance(ActionEvent event) { } private void saveInstance(BigtableInstance instance) { - try { - List allInstances = bigtableInstanceManager.getInstances(); - allInstances.add(instance); - bigtableInstanceManager.setInstances(allInstances); - } catch (IOException e) { - e.printStackTrace(); - displayErrorInfo(String.format("Unable to save bigtable instance %s", instance.getInstanceId())); - } + saveInstancesService.addInstance(instance); + saveInstancesService.setOnFailed(stateEvent -> displayErrorInfo("Unable to save instance", stateEvent)); + saveInstancesService.restart(); } private void listBigtableTables(BigtableInstance instance) { - ListBigtableTables listBigtableTables = - new ListBigtableTables(bigtableClient, instance, credentialsManager.getCredentialsPath()); - listBigtableTables.setOnSucceeded(workerStateEvent -> - tablesListView.addBigtableTables(listBigtableTables.getValue())); - listBigtableTables.start(); + listTablesService.setInstance(instance); + listTablesService.setOnSucceeded(workerStateEvent -> + tablesListView.addBigtableTables(listTablesService.getValue())); + listTablesService.setOnFailed(stateEvent -> displayErrorInfo("Unable to list tables", stateEvent)); + listTablesService.restart(); } private void onScanTableAction(ScanTableAction actionEvent) { bigtableTableView.clear(); - BigtableTable currentTable = tablesListView.selectedTableProperty().get(); + var currentTable = tablesListView.selectedTableProperty().get(); loadTableConfiguration(currentTable); - BigtableReadRequest request = new BigtableReadRequestBuilder() - .setCredentialsPath(credentialsManager.getCredentialsPath()) + var request = new BigtableReadRequestBuilder() .setTable(currentTable) .setRowRange(new BigtableRowRange(actionEvent.getFrom(), actionEvent.getTo())) .build(); - readBigtableRows(request); } private void loadTableConfiguration(BigtableTable currentTable) { - var tableConfiguration = getTableConfiguration(currentTable); - if (tableConfiguration != null) { - bigtableTableView.setValueConverter(new BigtableValueConverter(tableConfiguration.getCellDefinitions())); - } + loadTableConfigurationService.setTable(currentTable); + loadTableConfigurationService.setOnSucceeded(event -> bigtableTableView.setValueConverter( + new BigtableValueConverter(loadTableConfigurationService.getValue().getCellDefinitions()))); + loadTableConfigurationService.setOnFailed(event -> displayErrorInfo("Unable to load table configuration", event)); + loadTableConfigurationService.restart(); } private void onConfigureRowValueTypes(ActionEvent event) { var table = tablesListView.selectedTableProperty().get(); - var currentTableConfig = getTableConfiguration(table); - BigtableValueTypesDialog.displayAndAwaitResult(bigtableTableView.getColumns(), currentTableConfig) - .whenComplete((configuration, throwable) -> { - bigtableTableView.setValueConverter(new BigtableValueConverter(configuration.getCellDefinitions())); - saveTableConfiguration(table, configuration); - }); + loadTableConfigurationService.setTable(table); + loadTableConfigurationService.setOnSucceeded(e -> BigtableValueTypesDialog + .displayAndAwaitResult(bigtableTableView.getColumns(), loadTableConfigurationService.getValue()) + .whenComplete((configuration, throwable) -> updateTableConfiguration(table, configuration)) + ); + loadTableConfigurationService.setOnFailed(e -> BigtableValueTypesDialog + .displayAndAwaitResult(bigtableTableView.getColumns(), null) + .whenComplete((configuration, throwable) -> updateTableConfiguration(table, configuration)) + ); + loadTableConfigurationService.restart(); } - private BigtableTableConfiguration getTableConfiguration(BigtableTable table) { - BigtableTableConfiguration currentTableConfig = null; - try { - currentTableConfig = tableConfigManager.getTableConfiguration(table); - } catch (IOException e) { - e.printStackTrace(); - displayErrorInfo(String.format("Unable load table configuration for table %s", table.getName())); - } - return currentTableConfig; + private void updateTableConfiguration(BigtableTable table, BigtableTableConfiguration configuration) { + bigtableTableView.setValueConverter(new BigtableValueConverter(configuration.getCellDefinitions())); + saveTableConfiguration(table, configuration); } private void saveTableConfiguration(BigtableTable table, BigtableTableConfiguration configuration) { - try { - tableConfigManager.saveTableConfiguration(table, configuration); - } catch (IOException e) { - e.printStackTrace(); - displayErrorInfo("Unable to save table configuration"); - } + saveTableConfigurationService.setTableConfiguration(table, configuration); + saveTableConfigurationService.setOnFailed(event -> displayErrorInfo("Failed to save table configuration", event)); + saveTableConfigurationService.restart(); } - private void displayErrorInfo(String errorText) { - var alert = new Alert(Alert.AlertType.ERROR, errorText, ButtonType.CLOSE); + private void displayErrorInfo(String errorText, WorkerStateEvent event) { + var exception = event.getSource().getException(); + var alert = new Alert(Alert.AlertType.ERROR, errorText + " " + exception.getLocalizedMessage(), ButtonType.CLOSE); alert.showAndWait(); } @@ -162,22 +160,22 @@ private void onBigtableTableSelected(ObservableValue ob private BigtableReadRequest createReadRequest(BigtableTable newValue) { return new BigtableReadRequestBuilder() - .setCredentialsPath(credentialsManager.getCredentialsPath()) .setTable(newValue) .setRowRange(BigtableRowRange.DEFAULT) .build(); } private void readBigtableRows(BigtableReadRequest request) { - var readBigtableRows = new ReadBigtableRows(bigtableClient, request); - readBigtableRows.setOnSucceeded(workerStateEvent -> { + readRowsService.setReadRequest(request); + readRowsService.setOnSucceeded(workerStateEvent -> { bigtableTableView.setVisible(true); rowSelectionView.getProgressBar().setVisible(false); loadTableConfiguration(request.getBigtableTable()); - readBigtableRows.getValue().forEach(row -> bigtableTableView.add(row)); + readRowsService.getValue().forEach(row -> bigtableTableView.add(row)); }); + readRowsService.setOnFailed(stateEvent -> displayErrorInfo("Unable to read bigtable rows", stateEvent)); rowSelectionView.getProgressBar().setVisible(true); - rowSelectionView.getProgressBar().progressProperty().bind(readBigtableRows.progressProperty()); - readBigtableRows.start(); + rowSelectionView.getProgressBar().progressProperty().bind(readRowsService.progressProperty()); + readRowsService.restart(); } } diff --git a/src/main/java/com/erikmafo/btviewer/controllers/MenuBarController.java b/src/main/java/com/erikmafo/btviewer/controllers/MenuBarController.java index 2cb1ab5..6c8331c 100644 --- a/src/main/java/com/erikmafo/btviewer/controllers/MenuBarController.java +++ b/src/main/java/com/erikmafo/btviewer/controllers/MenuBarController.java @@ -1,23 +1,24 @@ package com.erikmafo.btviewer.controllers; import com.erikmafo.btviewer.components.SpecifyCredentialsPathDialog; -import com.erikmafo.btviewer.services.CredentialsManager; +import com.erikmafo.btviewer.services.LoadCredentialsPathService; +import com.erikmafo.btviewer.services.SaveCredentialsPathService; import com.sun.javafx.PlatformUtil; +import javafx.concurrent.WorkerStateEvent; import javafx.event.ActionEvent; import javafx.fxml.FXML; +import javafx.scene.control.Alert; +import javafx.scene.control.ButtonType; import javafx.scene.control.MenuBar; import javafx.scene.control.MenuItem; + import javax.inject.Inject; import java.nio.file.Path; public class MenuBarController { - private final CredentialsManager credentialsManager; - - @Inject - public MenuBarController(CredentialsManager credentialsManager) { - this.credentialsManager = credentialsManager; - } + private final LoadCredentialsPathService loadCredentialsPathService; + private final SaveCredentialsPathService saveCredentialsPathService; @FXML private MenuItem credentialsMenu; @@ -25,6 +26,12 @@ public MenuBarController(CredentialsManager credentialsManager) { @FXML private MenuBar menuBar; + @Inject + public MenuBarController(LoadCredentialsPathService loadCredentialsPathService, SaveCredentialsPathService saveCredentialsPathService) { + this.loadCredentialsPathService = loadCredentialsPathService; + this.saveCredentialsPathService = saveCredentialsPathService; + } + public void initialize() { if (PlatformUtil.isMac()) { menuBar.useSystemMenuBarProperty().set(true); @@ -32,9 +39,46 @@ public void initialize() { } public void onManageCredentialsAction(ActionEvent event) { - Path currentPath = credentialsManager.getCredentialsPath(); + loadCredentialsPathService.setOnSucceeded(e -> + displaySpecifyCredentialsDialog(loadCredentialsPathService.getValue())); + loadCredentialsPathService.setOnFailed(e -> { + displayErrorInfo("Unable to load credentials.", e); + displaySpecifyCredentialsDialog(loadCredentialsPathService.getValue()); + }); + loadCredentialsPathService.restart(); + } + + private void displaySpecifyCredentialsDialog(Path currentPath) { SpecifyCredentialsPathDialog.displayAndAwaitResult(currentPath) - .whenComplete((newCredentialsPath, throwable) -> - credentialsManager.setCredentialsPath(newCredentialsPath)); + .whenComplete(this::onCredentialsPathDialogComplete); + } + + private void onCredentialsPathDialogComplete(Path path, Throwable throwable) { + if (throwable != null) { + displayError(throwable); + } + + if (path == null) { + return; + } + + saveCredentialsPathService.setCredentialsPath(path); + saveCredentialsPathService.setOnFailed(e -> displayErrorInfo("Unable to save credentials path.", e)); + saveCredentialsPathService.restart(); + } + + private void displayError(Throwable throwable) { + var alert = new Alert( + Alert.AlertType.ERROR, + "Something went wrong: " + throwable.getLocalizedMessage(), + ButtonType.CLOSE); + alert.showAndWait(); + } + + private void displayErrorInfo(String errorText, WorkerStateEvent event) { + var exception = event.getSource().getException(); + var alert = new Alert( + Alert.AlertType.ERROR, errorText + " " + exception.getLocalizedMessage(), ButtonType.CLOSE); + alert.showAndWait(); } } diff --git a/src/main/java/com/erikmafo/btviewer/model/BigtableCell.java b/src/main/java/com/erikmafo/btviewer/model/BigtableCell.java index 3df135a..c28a4fd 100644 --- a/src/main/java/com/erikmafo/btviewer/model/BigtableCell.java +++ b/src/main/java/com/erikmafo/btviewer/model/BigtableCell.java @@ -1,6 +1,6 @@ package com.erikmafo.btviewer.model; -import com.google.bigtable.repackaged.com.google.protobuf.ByteString; +import com.google.protobuf.ByteString; public class BigtableCell { diff --git a/src/main/java/com/erikmafo/btviewer/model/BigtableInstance.java b/src/main/java/com/erikmafo/btviewer/model/BigtableInstance.java index fd89aa2..21a1b34 100644 --- a/src/main/java/com/erikmafo/btviewer/model/BigtableInstance.java +++ b/src/main/java/com/erikmafo/btviewer/model/BigtableInstance.java @@ -1,5 +1,7 @@ package com.erikmafo.btviewer.model; +import java.util.Objects; + public class BigtableInstance { private String instanceId; @@ -35,4 +37,18 @@ public String getProjectId() { public String getInstanceId() { return instanceId; } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + BigtableInstance that = (BigtableInstance) o; + return Objects.equals(instanceId, that.instanceId) && + Objects.equals(projectId, that.projectId); + } + + @Override + public int hashCode() { + return Objects.hash(instanceId, projectId); + } } diff --git a/src/main/java/com/erikmafo/btviewer/model/BigtableTable.java b/src/main/java/com/erikmafo/btviewer/model/BigtableTable.java index 137ae77..98e05d1 100644 --- a/src/main/java/com/erikmafo/btviewer/model/BigtableTable.java +++ b/src/main/java/com/erikmafo/btviewer/model/BigtableTable.java @@ -1,8 +1,7 @@ package com.erikmafo.btviewer.model; -import com.google.bigtable.repackaged.com.google.cloud.bigtable.grpc.BigtableTableName; +import com.google.bigtable.admin.v2.Table; +import com.google.bigtable.admin.v2.TableName; -import java.util.Collections; -import java.util.List; import java.util.Objects; public class BigtableTable { @@ -12,10 +11,10 @@ public class BigtableTable { private final String tableId; public BigtableTable(String name) { - BigtableTableName tableName = new BigtableTableName(name); - projectId = tableName.getProjectId(); - instanceId = tableName.getInstanceId(); - tableId = tableName.getTableId(); + var tab = TableName.parse(name); + projectId = tab.getProject(); + instanceId = tab.getInstance(); + tableId = tab.getTable(); } public BigtableTable(String projectId, String instanceId, String tableId) { diff --git a/src/main/java/com/erikmafo/btviewer/model/BigtableValueConverter.java b/src/main/java/com/erikmafo/btviewer/model/BigtableValueConverter.java index 39c0a61..52ade4a 100644 --- a/src/main/java/com/erikmafo/btviewer/model/BigtableValueConverter.java +++ b/src/main/java/com/erikmafo/btviewer/model/BigtableValueConverter.java @@ -3,6 +3,7 @@ import java.nio.BufferUnderflowException; import java.nio.ByteBuffer; import java.util.List; +import java.util.Objects; /** * Created by erikmafo on 23.12.17. @@ -47,4 +48,16 @@ private Object convertUsingValueType(BigtableCell cell, String valueType) { } } + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + BigtableValueConverter that = (BigtableValueConverter) o; + return Objects.equals(cellDefinitions, that.cellDefinitions); + } + + @Override + public int hashCode() { + return Objects.hash(cellDefinitions); + } } diff --git a/src/main/java/com/erikmafo/btviewer/model/CellDefinition.java b/src/main/java/com/erikmafo/btviewer/model/CellDefinition.java index 1e0821c..00803f8 100644 --- a/src/main/java/com/erikmafo/btviewer/model/CellDefinition.java +++ b/src/main/java/com/erikmafo/btviewer/model/CellDefinition.java @@ -1,5 +1,6 @@ package com.erikmafo.btviewer.model; +import java.util.Objects; public class CellDefinition { @@ -36,4 +37,19 @@ public String getQualifier() { public void setQualifier(String qualifier) { this.qualifier = qualifier; } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + CellDefinition that = (CellDefinition) o; + return Objects.equals(valueType, that.valueType) && + Objects.equals(family, that.family) && + Objects.equals(qualifier, that.qualifier); + } + + @Override + public int hashCode() { + return Objects.hash(valueType, family, qualifier); + } } diff --git a/src/main/java/com/erikmafo/btviewer/services/BigtableClient.java b/src/main/java/com/erikmafo/btviewer/services/BigtableClient.java deleted file mode 100644 index dcd3583..0000000 --- a/src/main/java/com/erikmafo/btviewer/services/BigtableClient.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.erikmafo.btviewer.services; - -import com.erikmafo.btviewer.model.BigtableInstance; -import com.erikmafo.btviewer.model.BigtableReadRequest; -import com.erikmafo.btviewer.model.BigtableRow; - -import java.io.IOException; -import java.nio.file.Path; -import java.util.List; - -public interface BigtableClient { - List readRows(BigtableReadRequest readRequest) throws IOException; - - List listTables(BigtableInstance instance, Path credentialsPath) throws IOException; - - List listInstances(String projectId, String credentialsPath) throws IOException; -} diff --git a/src/main/java/com/erikmafo/btviewer/services/BigtableClientImpl.java b/src/main/java/com/erikmafo/btviewer/services/BigtableClientImpl.java deleted file mode 100644 index 4da351c..0000000 --- a/src/main/java/com/erikmafo/btviewer/services/BigtableClientImpl.java +++ /dev/null @@ -1,161 +0,0 @@ -package com.erikmafo.btviewer.services; - -import com.erikmafo.btviewer.model.*; -import com.google.bigtable.repackaged.com.google.bigtable.admin.v2.ListTablesRequest; -import com.google.bigtable.repackaged.com.google.bigtable.admin.v2.ProjectName; -import com.google.bigtable.repackaged.com.google.bigtable.admin.v2.Table; -import com.google.bigtable.repackaged.com.google.bigtable.v2.ReadRowsRequest; -import com.google.bigtable.repackaged.com.google.bigtable.v2.RowRange; -import com.google.bigtable.repackaged.com.google.bigtable.v2.RowSet; -import com.google.bigtable.repackaged.com.google.cloud.bigtable.admin.v2.BigtableInstanceAdminClient; -import com.google.bigtable.repackaged.com.google.cloud.bigtable.admin.v2.BigtableInstanceAdminSettings; -import com.google.bigtable.repackaged.com.google.cloud.bigtable.config.BigtableOptions; -import com.google.bigtable.repackaged.com.google.cloud.bigtable.config.CallOptionsConfig; -import com.google.bigtable.repackaged.com.google.cloud.bigtable.config.CredentialOptions; -import com.google.bigtable.repackaged.com.google.cloud.bigtable.grpc.BigtableInstanceName; -import com.google.bigtable.repackaged.com.google.cloud.bigtable.grpc.BigtableSession; -import com.google.bigtable.repackaged.com.google.cloud.bigtable.grpc.BigtableTableAdminClient; -import com.google.bigtable.repackaged.com.google.protobuf.ByteString; -import java.io.IOException; -import java.nio.charset.Charset; -import java.nio.file.Files; -import java.nio.file.Path; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.TimeUnit; -import java.util.stream.Collectors; - -/** - * Created by erikmafo on 23.12.17. - */ -public class BigtableClientImpl implements BigtableClient { - - private final Map credentialOptionsCache = new ConcurrentHashMap<>(); - private static final Charset DEFAULT_CHARSET = Charset.defaultCharset(); - - public List readRows(BigtableReadRequest readRequest) throws IOException { - var rows = new ArrayList(); - try (BigtableResultScanner scanner = execute(readRequest)) { - int count = 0; - BigtableRow row; - do { - count++; - row = scanner.next(); - if (row == null) { - break; - } - rows.add(row); - - } while (count < readRequest.getScan().getMaxRows()); - } - - return rows; - } - - @Override - public List listTables(BigtableInstance bigtableInstance, Path credentialsPath) throws IOException { - try (BigtableSession session = getBigtableSession( - bigtableInstance.getProjectId(), - bigtableInstance.getInstanceId(), - credentialsPath)) { - BigtableTableAdminClient adminClient = session.getTableAdminClient(); - return adminClient - .listTables(ListTablesRequest - .newBuilder() - .setParent(getName(bigtableInstance)) - .build()) - .getTablesList() - .stream() - .map(Table::getName) - .collect(Collectors.toList()); - } - } - - @Override - public List listInstances(String projectId, String credentialsPath) throws IOException { - try (BigtableInstanceAdminClient client = BigtableInstanceAdminClient.create( - BigtableInstanceAdminSettings.newBuilder() - .setProjectName(ProjectName.of(projectId)) - .build())) - { - return client.listInstances() - .stream() - .map(instance -> new BigtableInstance(projectId, instance.getId())) - .collect(Collectors.toList()); - } - } - - private BigtableResultScanner execute(BigtableReadRequest bigtableReadRequest) throws IOException { - - BigtableTable bigtableTable = bigtableReadRequest.getBigtableTable(); - BigtableRowRange bigtableRowRange = bigtableReadRequest.getScan(); - Path credentialsPath = bigtableReadRequest.getCredentialsPath(); - - BigtableSession bigtableSession = getBigtableSession( - bigtableTable.getProjectId(), - bigtableTable.getInstanceId(), - credentialsPath); - - RowRange rowRange = RowRange.newBuilder() - .setStartKeyClosed(ByteString.copyFrom(bigtableRowRange.getFrom(), DEFAULT_CHARSET)) - .setEndKeyClosed(ByteString.copyFrom(bigtableRowRange.getTo(), DEFAULT_CHARSET)) - .build(); - - RowSet rowSet = RowSet.newBuilder() - .addRowRanges(rowRange) - .build(); - - ReadRowsRequest readRowsRequest = ReadRowsRequest.newBuilder() - .setRowsLimit(bigtableRowRange.getMaxRows()) - .setRows(rowSet) - .setTableName(bigtableTable.getName()) - .build(); - - return new BigtableResultScanner( - bigtableSession, - bigtableSession.getDataClient().readFlatRows(readRowsRequest)); - } - - private BigtableSession getBigtableSession(String projectId, String instanceId, Path credentialsPath) throws IOException { - - BigtableOptions googleBigtableOptions = - BigtableOptions.builder() - .setCallOptionsConfig(CallOptionsConfig.builder() - .setUseTimeout(true) - .setShortRpcTimeoutMs((int)TimeUnit.SECONDS.toMillis(30)) - .setLongRpcTimeoutMs((int)TimeUnit.MINUTES.toMillis(1)) - .build()) - .setProjectId(projectId) - .setInstanceId(instanceId) - .setCredentialOptions(getCredentialOptions(credentialsPath)) - .setUserAgent("bigtable-viewer") - .build(); - - BigtableSession bigtableSession = new BigtableSession(googleBigtableOptions); - - return bigtableSession; - } - - private String getName(BigtableInstance instance) { - return new BigtableInstanceName( - instance.getProjectId(), - instance.getInstanceId()).getInstanceName(); - } - - private CredentialOptions getCredentialOptions(Path credentialsPath) throws IOException { - - if (credentialsPath == null) { - return CredentialOptions.defaultCredentials(); - } - - if (credentialOptionsCache.containsKey(credentialsPath.toString())) { - return credentialOptionsCache.get(credentialsPath.toString()); - } - - CredentialOptions credentialOptions = CredentialOptions.jsonCredentials(Files.newInputStream(credentialsPath)); - credentialOptionsCache.put(credentialsPath.toString(), credentialOptions); - return credentialOptions; - } -} diff --git a/src/main/java/com/erikmafo/btviewer/services/BigtableResultScanner.java b/src/main/java/com/erikmafo/btviewer/services/BigtableResultScanner.java deleted file mode 100644 index 47f8765..0000000 --- a/src/main/java/com/erikmafo/btviewer/services/BigtableResultScanner.java +++ /dev/null @@ -1,59 +0,0 @@ -package com.erikmafo.btviewer.services; - -import com.erikmafo.btviewer.model.BigtableCell; -import com.erikmafo.btviewer.model.BigtableRow; -import com.google.bigtable.repackaged.com.google.cloud.bigtable.grpc.BigtableSession; -import com.google.bigtable.repackaged.com.google.cloud.bigtable.grpc.scanner.FlatRow; -import com.google.bigtable.repackaged.com.google.cloud.bigtable.grpc.scanner.ResultScanner; -import java.io.Closeable; -import java.io.IOException; -import java.util.*; - -public class BigtableResultScanner implements Closeable { - - private final BigtableSession session; - private final ResultScanner resultScanner; - private boolean isEndOfStream; - - public BigtableResultScanner(BigtableSession session, ResultScanner resultScanner) { - this.session = session; - this.resultScanner = resultScanner; - } - - @Override - public void close() throws IOException { - session.close(); - } - - public BigtableRow next() throws IOException { - - if (isEndOfStream) - { - return null; - } - - BigtableRow row = toBigtableRow(resultScanner.next()); - if (row == null) - { - isEndOfStream = true; - } - return row; - } - - private BigtableRow toBigtableRow(FlatRow flatRow) { - - if (flatRow == null) { - return null; - } - - List cells = new LinkedList<>(); - - for (FlatRow.Cell cell : flatRow.getCells()) { - String family = cell.getFamily(); - String qualifier = cell.getQualifier().toStringUtf8(); - cells.add(new BigtableCell(family, qualifier, cell.getValue())); - } - - return new BigtableRow(flatRow.getRowKey().toStringUtf8(), cells); - } -} diff --git a/src/main/java/com/erikmafo/btviewer/services/ListBigtableTables.java b/src/main/java/com/erikmafo/btviewer/services/ListBigtableTables.java deleted file mode 100644 index 9841534..0000000 --- a/src/main/java/com/erikmafo/btviewer/services/ListBigtableTables.java +++ /dev/null @@ -1,42 +0,0 @@ -package com.erikmafo.btviewer.services; - -import com.erikmafo.btviewer.model.BigtableInstance; -import com.erikmafo.btviewer.model.BigtableTable; -import javafx.concurrent.Service; -import javafx.concurrent.Task; - -import java.nio.file.Path; -import java.util.List; -import java.util.stream.Collectors; - -public class ListBigtableTables extends Service> { - - private final BigtableClient client; - private final BigtableInstance instance; - private final Path credentialsPath; - - public ListBigtableTables(BigtableClient client, BigtableInstance instance, Path credentialsPath) { - this.client = client; - this.instance = instance; - this.credentialsPath = credentialsPath; - } - - @Override - protected Task> createTask() { - return new Task>() { - @Override - protected List call() throws Exception { - return client.listTables( - instance, - credentialsPath) - .stream() - .map(ListBigtableTables.this::getBigtableTable) - .collect(Collectors.toList()); - } - }; - } - - private BigtableTable getBigtableTable(String tableName) { - return new BigtableTable(tableName); - } -} diff --git a/src/main/java/com/erikmafo/btviewer/services/ListTablesService.java b/src/main/java/com/erikmafo/btviewer/services/ListTablesService.java new file mode 100644 index 0000000..aaa532b --- /dev/null +++ b/src/main/java/com/erikmafo/btviewer/services/ListTablesService.java @@ -0,0 +1,76 @@ +package com.erikmafo.btviewer.services; + +import com.erikmafo.btviewer.model.BigtableInstance; +import com.erikmafo.btviewer.model.BigtableTable; +import com.erikmafo.btviewer.services.internal.BigtableSettingsProvider; +import com.google.cloud.bigtable.admin.v2.BigtableTableAdminClient; +import javafx.concurrent.Service; +import javafx.concurrent.Task; + +import javax.inject.Inject; +import java.io.IOException; +import java.util.List; +import java.util.stream.Collectors; + +public class ListTablesService extends Service> { + + private final BigtableSettingsProvider settingsProvider; + + private BigtableTableAdminClient client; + private BigtableInstance instance; + + @Inject + public ListTablesService(BigtableSettingsProvider settingsProvider) { + this.settingsProvider = settingsProvider; + } + + public void setInstance(BigtableInstance instance) { + this.instance = instance; + } + + @Override + protected Task> createTask() { + return new Task<>() { + @Override + protected List call() throws Exception { + return getOrCreateNewClient() + .listTables() + .stream() + .map(ListTablesService.this::toBigtableTable) + .collect(Collectors.toList()); + } + }; + } + + private BigtableTable toBigtableTable(String tableId) { + return new BigtableTable(instance.getProjectId(), instance.getInstanceId(), tableId); + } + + private BigtableTableAdminClient getOrCreateNewClient() throws IOException { + if (instance == null) { + throw new IllegalStateException("Cannot list tables when bigtable instance is not specified"); + } + + if (instance.equals(getClientInstance())) { + return client; + } + + closeClient(); + client = BigtableTableAdminClient.create(settingsProvider.getTableAdminSettings(instance)); + return client; + } + + private void closeClient() { + if (client != null) { + client.close(); + } + } + + private BigtableInstance getClientInstance() { + if (client == null) { + return null; + } + + return new BigtableInstance(client.getProjectId(), client.getInstanceId()); + } +} diff --git a/src/main/java/com/erikmafo/btviewer/services/LoadCredentialsPathService.java b/src/main/java/com/erikmafo/btviewer/services/LoadCredentialsPathService.java new file mode 100644 index 0000000..6bbb32b --- /dev/null +++ b/src/main/java/com/erikmafo/btviewer/services/LoadCredentialsPathService.java @@ -0,0 +1,28 @@ +package com.erikmafo.btviewer.services; + +import com.erikmafo.btviewer.services.internal.CredentialsManager; +import javafx.concurrent.Service; +import javafx.concurrent.Task; + +import javax.inject.Inject; +import java.nio.file.Path; + +public class LoadCredentialsPathService extends Service { + + private final CredentialsManager credentialsManager; + + @Inject + public LoadCredentialsPathService(CredentialsManager credentialsManager) { + this.credentialsManager = credentialsManager; + } + + @Override + protected Task createTask() { + return new Task() { + @Override + protected Path call() throws Exception { + return credentialsManager.getCredentialsPath(); + } + }; + } +} diff --git a/src/main/java/com/erikmafo/btviewer/services/LoadInstancesService.java b/src/main/java/com/erikmafo/btviewer/services/LoadInstancesService.java new file mode 100644 index 0000000..e2d20b3 --- /dev/null +++ b/src/main/java/com/erikmafo/btviewer/services/LoadInstancesService.java @@ -0,0 +1,29 @@ +package com.erikmafo.btviewer.services; + +import com.erikmafo.btviewer.model.BigtableInstance; +import com.erikmafo.btviewer.services.internal.BigtableInstanceManager; +import javafx.concurrent.Service; +import javafx.concurrent.Task; + +import javax.inject.Inject; +import java.util.List; + +public class LoadInstancesService extends Service> { + + private final BigtableInstanceManager instanceManager; + + @Inject + public LoadInstancesService(BigtableInstanceManager instanceManager) { + this.instanceManager = instanceManager; + } + + @Override + protected Task> createTask() { + return new Task<>() { + @Override + protected List call() throws Exception { + return instanceManager.getInstances(); + } + }; + } +} diff --git a/src/main/java/com/erikmafo/btviewer/services/LoadTableConfigurationService.java b/src/main/java/com/erikmafo/btviewer/services/LoadTableConfigurationService.java new file mode 100644 index 0000000..b26089c --- /dev/null +++ b/src/main/java/com/erikmafo/btviewer/services/LoadTableConfigurationService.java @@ -0,0 +1,34 @@ +package com.erikmafo.btviewer.services; + +import com.erikmafo.btviewer.model.BigtableTable; +import com.erikmafo.btviewer.model.BigtableTableConfiguration; +import com.erikmafo.btviewer.services.internal.TableConfigManager; +import javafx.concurrent.Service; +import javafx.concurrent.Task; + +import javax.inject.Inject; + +public class LoadTableConfigurationService extends Service { + + private final TableConfigManager tableConfigManager; + private BigtableTable table; + + @Inject + public LoadTableConfigurationService(TableConfigManager tableConfigManager) { + this.tableConfigManager = tableConfigManager; + } + + public void setTable(BigtableTable table) { + this.table = table; + } + + @Override + protected Task createTask() { + return new Task<>() { + @Override + protected BigtableTableConfiguration call() throws Exception { + return tableConfigManager.getTableConfiguration(table); + } + }; + } +} diff --git a/src/main/java/com/erikmafo/btviewer/services/ReadBigtableRows.java b/src/main/java/com/erikmafo/btviewer/services/ReadBigtableRows.java deleted file mode 100644 index f95e4eb..0000000 --- a/src/main/java/com/erikmafo/btviewer/services/ReadBigtableRows.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.erikmafo.btviewer.services; - -import com.erikmafo.btviewer.model.*; -import javafx.concurrent.Service; -import javafx.concurrent.Task; -import java.util.List; - -public class ReadBigtableRows extends Service> { - - private final BigtableClient client; - private final BigtableReadRequest readRequest; - - public ReadBigtableRows(BigtableClient client, BigtableReadRequest readRequest) { - this.client = client; - this.readRequest = readRequest; - } - - @Override - protected Task> createTask() { - return new Task<>() { - @Override - protected List call() throws Exception { - return client.readRows(readRequest); - } - }; - } -} - - diff --git a/src/main/java/com/erikmafo/btviewer/services/ReadRowsService.java b/src/main/java/com/erikmafo/btviewer/services/ReadRowsService.java new file mode 100644 index 0000000..bd7468e --- /dev/null +++ b/src/main/java/com/erikmafo/btviewer/services/ReadRowsService.java @@ -0,0 +1,115 @@ +package com.erikmafo.btviewer.services; + +import com.erikmafo.btviewer.model.*; +import com.erikmafo.btviewer.services.internal.BigtableSettingsProvider; +import com.google.cloud.bigtable.data.v2.BigtableDataClient; +import com.google.cloud.bigtable.data.v2.BigtableDataSettings; +import com.google.cloud.bigtable.data.v2.models.Query; +import com.google.cloud.bigtable.data.v2.models.Row; +import com.google.cloud.bigtable.data.v2.models.RowCell; +import javafx.concurrent.Service; +import javafx.concurrent.Task; + +import javax.inject.Inject; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; + +public class ReadRowsService extends Service> { + + private final BigtableSettingsProvider settingsProvider; + + private BigtableDataClient client; + private BigtableDataSettings settings; + private BigtableReadRequest readRequest; + + @Inject + public ReadRowsService(BigtableSettingsProvider settingsProvider) { + this.settingsProvider = settingsProvider; + } + + @Override + protected Task> createTask() { + return new Task<>() { + @Override + protected List call() throws Exception { + var rowIterator = getOrCreateNewClient() + .readRows(createQuery(readRequest)) + .iterator(); + + var bigtableRows = new ArrayList(); + while (rowIterator.hasNext()) { + bigtableRows.add(toBigtableRow(rowIterator.next())); + updateProgress(bigtableRows.size(), readRequest.getScan().getMaxRows()); + } + return bigtableRows; + } + }; + } + + public void setReadRequest(BigtableReadRequest readRequest) { + this.readRequest = readRequest; + } + + private static Query createQuery(BigtableReadRequest request) { + return Query + .create(request.getBigtableTable().getTableId()) + .range(request.getScan().getFrom(), request.getScan().getTo()) + .limit(request.getScan().getMaxRows()); + } + + private static BigtableRow toBigtableRow(Row row) { + return new BigtableRow(row.getKey().toStringUtf8(), getBigtableCells(row)); + } + + private static List getBigtableCells(Row row) { + return row + .getCells() + .stream() + .map(ReadRowsService::toBigtableCell) + .collect(Collectors.toList()); + } + + private static BigtableCell toBigtableCell(RowCell cell) { + return new BigtableCell( + cell.getFamily(), + cell.getQualifier().toStringUtf8(), + cell.getValue()); + } + + private BigtableDataClient getOrCreateNewClient() throws IOException { + if (readRequest == null) { + throw new IllegalStateException("Cannot list tables when read request is not specified"); + } + + var instance = new BigtableInstance( + readRequest.getBigtableTable().getProjectId(), + readRequest.getBigtableTable().getInstanceId()); + + if (instance.equals(getClientInstance())) { + return client; + } + + closeClient(); + settings = settingsProvider.getDataSettings(instance); + client = BigtableDataClient.create(settings); + return client; + } + + private BigtableInstance getClientInstance() { + if (client == null) { + return null; + } + + return new BigtableInstance(settings.getProjectId(), settings.getInstanceId()); + } + + private void closeClient() { + if (client != null) { + client.close(); + } + } +} + + diff --git a/src/main/java/com/erikmafo/btviewer/services/SaveCredentialsPathService.java b/src/main/java/com/erikmafo/btviewer/services/SaveCredentialsPathService.java new file mode 100644 index 0000000..3a28a8d --- /dev/null +++ b/src/main/java/com/erikmafo/btviewer/services/SaveCredentialsPathService.java @@ -0,0 +1,35 @@ +package com.erikmafo.btviewer.services; + +import com.erikmafo.btviewer.services.internal.CredentialsManager; +import javafx.concurrent.Service; +import javafx.concurrent.Task; + +import javax.inject.Inject; +import java.nio.file.Path; + +public class SaveCredentialsPathService extends Service { + + private final CredentialsManager credentialsManager; + + private Path credentialsPath; + + @Inject + public SaveCredentialsPathService(CredentialsManager credentialsManager) { + this.credentialsManager = credentialsManager; + } + + public void setCredentialsPath(Path credentialsPath) { + this.credentialsPath = credentialsPath; + } + + @Override + protected Task createTask() { + return new Task<>() { + @Override + protected Void call() throws Exception { + credentialsManager.setCredentialsPath(credentialsPath); + return null; + } + }; + } +} diff --git a/src/main/java/com/erikmafo/btviewer/services/SaveInstancesService.java b/src/main/java/com/erikmafo/btviewer/services/SaveInstancesService.java new file mode 100644 index 0000000..5de9730 --- /dev/null +++ b/src/main/java/com/erikmafo/btviewer/services/SaveInstancesService.java @@ -0,0 +1,44 @@ +package com.erikmafo.btviewer.services; + +import com.erikmafo.btviewer.model.BigtableInstance; +import com.erikmafo.btviewer.services.internal.BigtableInstanceManager; +import javafx.concurrent.Service; +import javafx.concurrent.Task; + +import javax.inject.Inject; +import java.util.ArrayList; +import java.util.List; + +public class SaveInstancesService extends Service { + + private final BigtableInstanceManager instanceManager; + + private List instances; + + @Inject + public SaveInstancesService(BigtableInstanceManager instanceManager) { + this.instanceManager = instanceManager; + } + + public void addInstance(BigtableInstance instance) { + + if (this.instances == null) { + instances = new ArrayList<>(); + } + + if (!this.instances.contains(instance)) { + this.instances.add(instance); + } + } + + @Override + protected Task createTask() { + return new Task() { + @Override + protected Object call() throws Exception { + instanceManager.setInstances(instances); + return null; + } + }; + } +} diff --git a/src/main/java/com/erikmafo/btviewer/services/SaveTableConfigurationService.java b/src/main/java/com/erikmafo/btviewer/services/SaveTableConfigurationService.java new file mode 100644 index 0000000..f2df8ce --- /dev/null +++ b/src/main/java/com/erikmafo/btviewer/services/SaveTableConfigurationService.java @@ -0,0 +1,38 @@ +package com.erikmafo.btviewer.services; + +import com.erikmafo.btviewer.model.BigtableTable; +import com.erikmafo.btviewer.model.BigtableTableConfiguration; +import com.erikmafo.btviewer.services.internal.TableConfigManager; +import javafx.concurrent.Service; +import javafx.concurrent.Task; + +import javax.inject.Inject; + +public class SaveTableConfigurationService extends Service { + + private final TableConfigManager tableConfigManager; + + private BigtableTable table; + private BigtableTableConfiguration tableConfiguration; + + @Inject + public SaveTableConfigurationService(TableConfigManager tableConfigManager) { + this.tableConfigManager = tableConfigManager; + } + + public void setTableConfiguration(BigtableTable table, BigtableTableConfiguration tableConfiguration) { + this.table = table; + this.tableConfiguration = tableConfiguration; + } + + @Override + protected Task createTask() { + return new Task() { + @Override + protected Object call() throws Exception { + tableConfigManager.saveTableConfiguration(table, tableConfiguration); + return null; + } + }; + } +} diff --git a/src/main/java/com/erikmafo/btviewer/services/ServicesModule.java b/src/main/java/com/erikmafo/btviewer/services/ServicesModule.java new file mode 100644 index 0000000..9c8905f --- /dev/null +++ b/src/main/java/com/erikmafo/btviewer/services/ServicesModule.java @@ -0,0 +1,48 @@ +package com.erikmafo.btviewer.services; + +import com.erikmafo.btviewer.config.AppConfig; +import com.erikmafo.btviewer.config.ApplicationEnvironment; +import com.erikmafo.btviewer.services.internal.inmemory.BigtableEmulatorSettingsProvider; +import com.erikmafo.btviewer.services.internal.inmemory.InMemoryInstanceManager; +import com.erikmafo.btviewer.services.internal.inmemory.InMemoryTableConfigManager; +import com.erikmafo.btviewer.services.internal.inmemory.TestDataUtil; +import com.erikmafo.btviewer.services.internal.*; +import com.google.api.gax.core.CredentialsProvider; +import com.google.inject.Binder; +import com.google.inject.Module; + +public class ServicesModule implements Module { + + @Override + public void configure(Binder binder) { + var config = AppConfig.load(ApplicationEnvironment.get()); + + if (config.useBigtableEmulator()) { + var emulatorSettingsProvider = new BigtableEmulatorSettingsProvider(); + emulatorSettingsProvider.startEmulator(); + TestDataUtil.injectWithTestData(emulatorSettingsProvider); + binder.bind(BigtableSettingsProvider.class).toInstance(emulatorSettingsProvider); + } + else { + binder.bind(BigtableSettingsProvider.class).to(BigtableSettingsProviderImpl.class); + } + + binder.bind(CredentialsProvider.class).to(DynamicCredentialsProvider.class); + + if (config.useInMemoryTableConfigManager()) { + var inMemoryTableConfigManager = new InMemoryTableConfigManager(); + binder.bind(TableConfigManager.class).toInstance(inMemoryTableConfigManager); + } + else { + binder.bind(TableConfigManager.class).toInstance(new TableConfigManagerImpl()); + } + + if (config.useInMemoryInstanceManager()) { + var inMemoryInstanceManager = new InMemoryInstanceManager(); + TestDataUtil.injectWithTestData(inMemoryInstanceManager); + binder.bind(BigtableInstanceManager.class).toInstance(inMemoryInstanceManager); + } else { + binder.bind(BigtableInstanceManager.class).toInstance(new BigtableInstanceManagerImpl()); + } + } +} diff --git a/src/main/java/com/erikmafo/btviewer/services/inmemory/InMemoryBigtableClient.java b/src/main/java/com/erikmafo/btviewer/services/inmemory/InMemoryBigtableClient.java deleted file mode 100644 index 4931062..0000000 --- a/src/main/java/com/erikmafo/btviewer/services/inmemory/InMemoryBigtableClient.java +++ /dev/null @@ -1,51 +0,0 @@ -package com.erikmafo.btviewer.services.inmemory; - -import com.erikmafo.btviewer.model.BigtableInstance; -import com.erikmafo.btviewer.model.BigtableReadRequest; -import com.erikmafo.btviewer.model.BigtableRow; -import com.erikmafo.btviewer.model.BigtableTable; -import com.erikmafo.btviewer.services.BigtableClient; - -import java.io.IOException; -import java.nio.file.Path; -import java.util.*; -import java.util.stream.Collectors; - -public class InMemoryBigtableClient implements BigtableClient { - - private final Map> tables = new HashMap<>(); - - @Override - public List readRows(BigtableReadRequest readRequest) throws IOException { - - var table = tables.get(readRequest.getBigtableTable()); - - if (table == null) - { - return new ArrayList<>(); - } - - return table - .subMap(readRequest.getScan().getFrom(), readRequest.getScan().getTo()) - .values() - .stream() - .collect(Collectors.toList()); - } - - @Override - public List listTables(BigtableInstance bigtableInstance, Path credentialsPath) throws IOException { - return tables.keySet().stream().map(BigtableTable::getName).collect(Collectors.toList()); - } - - @Override - public List listInstances(String projectId, String credentialsPath) throws IOException { - return null; - } - - public void addRow(BigtableTable table, BigtableRow bigtableRow) - { - tables.putIfAbsent(table, new TreeMap<>()); - var rows = tables.get(table); - rows.put(bigtableRow.getRowKey(), bigtableRow); - } -} diff --git a/src/main/java/com/erikmafo/btviewer/services/AppDataUtil.java b/src/main/java/com/erikmafo/btviewer/services/internal/AppDataUtil.java similarity index 90% rename from src/main/java/com/erikmafo/btviewer/services/AppDataUtil.java rename to src/main/java/com/erikmafo/btviewer/services/internal/AppDataUtil.java index 47fb79a..a3229eb 100644 --- a/src/main/java/com/erikmafo/btviewer/services/AppDataUtil.java +++ b/src/main/java/com/erikmafo/btviewer/services/internal/AppDataUtil.java @@ -1,9 +1,9 @@ -package com.erikmafo.btviewer.services; +package com.erikmafo.btviewer.services.internal; import com.sun.javafx.PlatformUtil; import java.nio.file.Path; -public class AppDataUtil { +class AppDataUtil { private static final String BIGTABLE_VIEWER = "BigtableViewer"; diff --git a/src/main/java/com/erikmafo/btviewer/services/BigtableInstanceManager.java b/src/main/java/com/erikmafo/btviewer/services/internal/BigtableInstanceManager.java similarity index 85% rename from src/main/java/com/erikmafo/btviewer/services/BigtableInstanceManager.java rename to src/main/java/com/erikmafo/btviewer/services/internal/BigtableInstanceManager.java index 44167f1..5a4e833 100644 --- a/src/main/java/com/erikmafo/btviewer/services/BigtableInstanceManager.java +++ b/src/main/java/com/erikmafo/btviewer/services/internal/BigtableInstanceManager.java @@ -1,4 +1,4 @@ -package com.erikmafo.btviewer.services; +package com.erikmafo.btviewer.services.internal; import com.erikmafo.btviewer.model.BigtableInstance; diff --git a/src/main/java/com/erikmafo/btviewer/services/BigtableInstanceManagerImpl.java b/src/main/java/com/erikmafo/btviewer/services/internal/BigtableInstanceManagerImpl.java similarity index 97% rename from src/main/java/com/erikmafo/btviewer/services/BigtableInstanceManagerImpl.java rename to src/main/java/com/erikmafo/btviewer/services/internal/BigtableInstanceManagerImpl.java index 78a1b5a..a2a776a 100644 --- a/src/main/java/com/erikmafo/btviewer/services/BigtableInstanceManagerImpl.java +++ b/src/main/java/com/erikmafo/btviewer/services/internal/BigtableInstanceManagerImpl.java @@ -1,4 +1,4 @@ -package com.erikmafo.btviewer.services; +package com.erikmafo.btviewer.services.internal; import com.erikmafo.btviewer.model.BigtableInstance; import com.google.gson.Gson; diff --git a/src/main/java/com/erikmafo/btviewer/services/internal/BigtableSettingsProvider.java b/src/main/java/com/erikmafo/btviewer/services/internal/BigtableSettingsProvider.java new file mode 100644 index 0000000..00bf84b --- /dev/null +++ b/src/main/java/com/erikmafo/btviewer/services/internal/BigtableSettingsProvider.java @@ -0,0 +1,14 @@ +package com.erikmafo.btviewer.services.internal; + +import com.erikmafo.btviewer.model.BigtableInstance; +import com.google.cloud.bigtable.admin.v2.BigtableTableAdminSettings; +import com.google.cloud.bigtable.data.v2.BigtableDataSettings; + +import java.io.IOException; + +public interface BigtableSettingsProvider { + + BigtableTableAdminSettings getTableAdminSettings(BigtableInstance instance) throws IOException; + + BigtableDataSettings getDataSettings(BigtableInstance instance); +} diff --git a/src/main/java/com/erikmafo/btviewer/services/internal/BigtableSettingsProviderImpl.java b/src/main/java/com/erikmafo/btviewer/services/internal/BigtableSettingsProviderImpl.java new file mode 100644 index 0000000..964bc89 --- /dev/null +++ b/src/main/java/com/erikmafo/btviewer/services/internal/BigtableSettingsProviderImpl.java @@ -0,0 +1,39 @@ +package com.erikmafo.btviewer.services.internal; + +import com.erikmafo.btviewer.model.BigtableInstance; +import com.google.api.gax.core.CredentialsProvider; +import com.google.cloud.bigtable.admin.v2.BigtableTableAdminSettings; +import com.google.cloud.bigtable.data.v2.BigtableDataSettings; + +import javax.inject.Inject; +import java.io.IOException; + +public class BigtableSettingsProviderImpl implements BigtableSettingsProvider { + + private static final String BIGTABLE_VIEWER = "bigtable-viewer"; + private final CredentialsProvider credentialsProvider; + + @Inject + public BigtableSettingsProviderImpl(CredentialsProvider credentialsProvider) { + this.credentialsProvider = credentialsProvider; + } + + @Override + public BigtableTableAdminSettings getTableAdminSettings(BigtableInstance instance) throws IOException { + return BigtableTableAdminSettings.newBuilder() + .setCredentialsProvider(credentialsProvider) + .setProjectId(instance.getProjectId()) + .setInstanceId(instance.getInstanceId()) + .build(); + } + + @Override + public BigtableDataSettings getDataSettings(BigtableInstance instance) { + return BigtableDataSettings.newBuilder() + .setAppProfileId(BIGTABLE_VIEWER) + .setCredentialsProvider(credentialsProvider) + .setProjectId(instance.getProjectId()) + .setInstanceId(instance.getInstanceId()) + .build(); + } +} diff --git a/src/main/java/com/erikmafo/btviewer/services/CredentialsManager.java b/src/main/java/com/erikmafo/btviewer/services/internal/CredentialsManager.java similarity index 96% rename from src/main/java/com/erikmafo/btviewer/services/CredentialsManager.java rename to src/main/java/com/erikmafo/btviewer/services/internal/CredentialsManager.java index 55e4c30..42df517 100644 --- a/src/main/java/com/erikmafo/btviewer/services/CredentialsManager.java +++ b/src/main/java/com/erikmafo/btviewer/services/internal/CredentialsManager.java @@ -1,4 +1,4 @@ -package com.erikmafo.btviewer.services; +package com.erikmafo.btviewer.services.internal; import java.nio.file.Path; import java.util.prefs.Preferences; diff --git a/src/main/java/com/erikmafo/btviewer/services/internal/DynamicCredentialsProvider.java b/src/main/java/com/erikmafo/btviewer/services/internal/DynamicCredentialsProvider.java new file mode 100644 index 0000000..0733381 --- /dev/null +++ b/src/main/java/com/erikmafo/btviewer/services/internal/DynamicCredentialsProvider.java @@ -0,0 +1,36 @@ +package com.erikmafo.btviewer.services.internal; + +import com.google.api.gax.core.CredentialsProvider; +import com.google.auth.Credentials; +import com.google.auth.oauth2.ServiceAccountCredentials; + +import javax.inject.Inject; +import java.io.FileInputStream; +import java.io.IOException; +import java.nio.file.Path; + +public class DynamicCredentialsProvider implements CredentialsProvider { + + private final CredentialsManager credentialsManager; + + private Path path; + private Credentials credentials; + + @Inject + public DynamicCredentialsProvider(CredentialsManager credentialsManager) { + this.credentialsManager = credentialsManager; + } + + @Override + public Credentials getCredentials() throws IOException { + + var newPath = credentialsManager.getCredentialsPath(); + + if (path == null || !path.equals(newPath)) { + path = newPath; + credentials = ServiceAccountCredentials.fromStream(new FileInputStream(path.toFile())); + } + + return credentials; + } +} diff --git a/src/main/java/com/erikmafo/btviewer/services/TableConfigManager.java b/src/main/java/com/erikmafo/btviewer/services/internal/TableConfigManager.java similarity index 89% rename from src/main/java/com/erikmafo/btviewer/services/TableConfigManager.java rename to src/main/java/com/erikmafo/btviewer/services/internal/TableConfigManager.java index f3e3e36..e66cb97 100644 --- a/src/main/java/com/erikmafo/btviewer/services/TableConfigManager.java +++ b/src/main/java/com/erikmafo/btviewer/services/internal/TableConfigManager.java @@ -1,4 +1,4 @@ -package com.erikmafo.btviewer.services; +package com.erikmafo.btviewer.services.internal; import com.erikmafo.btviewer.model.BigtableTable; import com.erikmafo.btviewer.model.BigtableTableConfiguration; diff --git a/src/main/java/com/erikmafo/btviewer/services/TableConfigManagerImpl.java b/src/main/java/com/erikmafo/btviewer/services/internal/TableConfigManagerImpl.java similarity index 97% rename from src/main/java/com/erikmafo/btviewer/services/TableConfigManagerImpl.java rename to src/main/java/com/erikmafo/btviewer/services/internal/TableConfigManagerImpl.java index 11c81e1..76715d4 100644 --- a/src/main/java/com/erikmafo/btviewer/services/TableConfigManagerImpl.java +++ b/src/main/java/com/erikmafo/btviewer/services/internal/TableConfigManagerImpl.java @@ -1,4 +1,4 @@ -package com.erikmafo.btviewer.services; +package com.erikmafo.btviewer.services.internal; import com.erikmafo.btviewer.model.BigtableTable; import com.erikmafo.btviewer.model.BigtableTableConfiguration; diff --git a/src/main/java/com/erikmafo/btviewer/services/internal/inmemory/BigtableEmulatorSettingsProvider.java b/src/main/java/com/erikmafo/btviewer/services/internal/inmemory/BigtableEmulatorSettingsProvider.java new file mode 100644 index 0000000..0860ba9 --- /dev/null +++ b/src/main/java/com/erikmafo/btviewer/services/internal/inmemory/BigtableEmulatorSettingsProvider.java @@ -0,0 +1,57 @@ +package com.erikmafo.btviewer.services.internal.inmemory; + +import com.erikmafo.btviewer.model.BigtableInstance; +import com.erikmafo.btviewer.services.internal.BigtableSettingsProvider; +import com.google.cloud.bigtable.admin.v2.BigtableTableAdminSettings; +import com.google.cloud.bigtable.data.v2.BigtableDataSettings; +import com.google.cloud.bigtable.emulator.v2.Emulator; + +import java.io.IOException; +import java.util.concurrent.TimeoutException; + +public class BigtableEmulatorSettingsProvider implements BigtableSettingsProvider { + + private static final String LOCALHOST = "localhost"; + private Emulator emulator; + + public BigtableEmulatorSettingsProvider() { + } + + public void startEmulator() { + + if (emulator != null) { + throw new IllegalStateException("Emulator already started"); + } + + try { + this.emulator = Emulator.createBundled(); + emulator.start(); + } catch (IOException | TimeoutException | InterruptedException e) { + throw new RuntimeException("Failed to start emulator", e); + } + } + + public void stopEmulator() { + if (emulator != null) { + emulator.stop(); + } + } + + @Override + public BigtableTableAdminSettings getTableAdminSettings(BigtableInstance instance) throws IOException { + return BigtableTableAdminSettings + .newBuilderForEmulator(LOCALHOST, emulator.getPort()) + .setProjectId(instance.getProjectId()) + .setInstanceId(instance.getInstanceId()) + .build(); + } + + @Override + public BigtableDataSettings getDataSettings(BigtableInstance instance) { + return BigtableDataSettings + .newBuilderForEmulator(LOCALHOST, emulator.getPort()) + .setProjectId(instance.getProjectId()) + .setInstanceId(instance.getInstanceId()) + .build(); + } +} diff --git a/src/main/java/com/erikmafo/btviewer/services/inmemory/InMemoryInstanceManager.java b/src/main/java/com/erikmafo/btviewer/services/internal/inmemory/InMemoryInstanceManager.java similarity index 82% rename from src/main/java/com/erikmafo/btviewer/services/inmemory/InMemoryInstanceManager.java rename to src/main/java/com/erikmafo/btviewer/services/internal/inmemory/InMemoryInstanceManager.java index 14a8eb9..0eda788 100644 --- a/src/main/java/com/erikmafo/btviewer/services/inmemory/InMemoryInstanceManager.java +++ b/src/main/java/com/erikmafo/btviewer/services/internal/inmemory/InMemoryInstanceManager.java @@ -1,9 +1,8 @@ -package com.erikmafo.btviewer.services.inmemory; +package com.erikmafo.btviewer.services.internal.inmemory; import com.erikmafo.btviewer.model.BigtableInstance; -import com.erikmafo.btviewer.services.BigtableInstanceManager; +import com.erikmafo.btviewer.services.internal.BigtableInstanceManager; -import java.io.IOException; import java.util.ArrayList; import java.util.List; diff --git a/src/main/java/com/erikmafo/btviewer/services/inmemory/InMemoryTableConfigManager.java b/src/main/java/com/erikmafo/btviewer/services/internal/inmemory/InMemoryTableConfigManager.java similarity index 83% rename from src/main/java/com/erikmafo/btviewer/services/inmemory/InMemoryTableConfigManager.java rename to src/main/java/com/erikmafo/btviewer/services/internal/inmemory/InMemoryTableConfigManager.java index 73589bf..8abbe96 100644 --- a/src/main/java/com/erikmafo/btviewer/services/inmemory/InMemoryTableConfigManager.java +++ b/src/main/java/com/erikmafo/btviewer/services/internal/inmemory/InMemoryTableConfigManager.java @@ -1,8 +1,8 @@ -package com.erikmafo.btviewer.services.inmemory; +package com.erikmafo.btviewer.services.internal.inmemory; import com.erikmafo.btviewer.model.BigtableTable; import com.erikmafo.btviewer.model.BigtableTableConfiguration; -import com.erikmafo.btviewer.services.TableConfigManager; +import com.erikmafo.btviewer.services.internal.TableConfigManager; import java.util.HashMap; import java.util.Map; diff --git a/src/main/java/com/erikmafo/btviewer/services/inmemory/TestDataUtil.java b/src/main/java/com/erikmafo/btviewer/services/internal/inmemory/TestDataUtil.java similarity index 50% rename from src/main/java/com/erikmafo/btviewer/services/inmemory/TestDataUtil.java rename to src/main/java/com/erikmafo/btviewer/services/internal/inmemory/TestDataUtil.java index 2b57662..d18a291 100644 --- a/src/main/java/com/erikmafo/btviewer/services/inmemory/TestDataUtil.java +++ b/src/main/java/com/erikmafo/btviewer/services/internal/inmemory/TestDataUtil.java @@ -1,7 +1,13 @@ -package com.erikmafo.btviewer.services.inmemory; +package com.erikmafo.btviewer.services.internal.inmemory; import com.erikmafo.btviewer.model.*; -import com.google.bigtable.repackaged.com.google.protobuf.ByteString; +import com.google.cloud.bigtable.admin.v2.BigtableTableAdminClient; +import com.google.cloud.bigtable.admin.v2.models.CreateTableRequest; +import com.google.cloud.bigtable.data.v2.BigtableDataClient; +import com.google.cloud.bigtable.data.v2.models.RowMutation; +import com.google.protobuf.ByteString; + +import java.io.IOException; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.util.Arrays; @@ -12,6 +18,27 @@ public class TestDataUtil { private static final String INSTANCE_0 = "instance-0"; private static final String TABLE_0 = "table-0"; + public static void injectWithTestData(BigtableEmulatorSettingsProvider settingsProvider) { + try { + var instance = new BigtableInstance(PROJECT_0, INSTANCE_0); + var tableAdminSettings = settingsProvider.getTableAdminSettings(instance); + var adminClient = BigtableTableAdminClient.create(tableAdminSettings); + adminClient.createTable(CreateTableRequest + .of(TABLE_0) + .addFamily("f1") + .addFamily("f2") + .addFamily("f3")); + adminClient.close(); + + var dataSettings = settingsProvider.getDataSettings(instance); + var dataClient = BigtableDataClient.create(dataSettings); + injectWithTestData(dataClient); + dataClient.close(); + } catch (IOException e) { + throw new RuntimeException("Unable to start emulator", e); + } + } + public static void injectWithTestData(InMemoryInstanceManager instanceManager) { var instance = new BigtableInstance(PROJECT_0, INSTANCE_0); instanceManager.setInstances(Arrays.asList(instance)); @@ -23,17 +50,15 @@ public static void injectWithTestData(InMemoryTableConfigManager configManager) configManager.saveTableConfiguration(table, config); } - public static void injectWithTestData(InMemoryBigtableClient inMemoryBigtableClient) { - var table = new BigtableTable(PROJECT_0, INSTANCE_0, TABLE_0); - + private static void injectWithTestData(BigtableDataClient client) { for (int i = 0; i <1000; i++) { var rowKey = "row-" + String.format("%04d", i); - var cells = Arrays.asList( - new BigtableCell("f1", "q1", toByteString("string-" + i)), - new BigtableCell("f1", "q2", toByteString(i)), - new BigtableCell("f1", "q3", toByteString(i + 0.5))); - - inMemoryBigtableClient.addRow(table, new BigtableRow(rowKey, cells)); + var mutation = RowMutation + .create(TABLE_0, rowKey) + .setCell("f1", "q1", "string-" + i) + .setCell("f1", toByteString("q2"), toByteString(i)) + .setCell("f1", toByteString("q3"), toByteString(i + 0.5)); + client.mutateRow(mutation); } } diff --git a/src/main/resources/config.properties b/src/main/resources/config.properties index 5395040..03e0bf6 100644 --- a/src/main/resources/config.properties +++ b/src/main/resources/config.properties @@ -1,3 +1,3 @@ -USE_IN_MEMORY_BIGTABLE_CLIENT = false +USE_BIGTABLE_EMULATOR = false USE_IN_MEMORY_TABLE_CONFIG_MANAGER = false USE_IN_MEMORY_INSTANCE_MANAGER = false \ No newline at end of file