Search in sources :

Example 1 with OptimizeRestoredDatabaseMetadata

use of com.google.spanner.admin.database.v1.OptimizeRestoredDatabaseMetadata in project java-spanner by googleapis.

the class DatabaseAdminClientTest method instanceListDatabaseOperationsWithMetadata.

@Test
public void instanceListDatabaseOperationsWithMetadata() throws Exception {
    Instance instance = spanner.getInstanceAdminClient().newInstanceBuilder(InstanceId.of(PROJECT_ID, INSTANCE_ID)).build();
    String filter = "(metadata.@type:type.googleapis.com/" + "google.spanner.admin.database.v1.OptimizeRestoredDatabaseMetadata)";
    mockDatabaseAdmin.addFilterMatches(filter, restoreDatabaseOperation.getMetadata().get().getOptimizeDatabaseOperationName());
    Iterable<Operation> operations = instance.listDatabaseOperations(Options.filter(filter)).iterateAll();
    assertThat(operations).hasSize(1);
    for (Operation op : operations) {
        OptimizeRestoredDatabaseMetadata metadata = op.getMetadata().unpack(OptimizeRestoredDatabaseMetadata.class);
        String progress = String.format("Restored database %s is optimized %d%%", metadata.getName(), metadata.getProgress().getProgressPercent());
        assertThat(progress.contains("100%"));
    }
}
Also used : OptimizeRestoredDatabaseMetadata(com.google.spanner.admin.database.v1.OptimizeRestoredDatabaseMetadata) Operation(com.google.longrunning.Operation) Test(org.junit.Test)

Example 2 with OptimizeRestoredDatabaseMetadata

use of com.google.spanner.admin.database.v1.OptimizeRestoredDatabaseMetadata in project java-spanner by googleapis.

the class SpannerSample method listDatabaseOperations.

// [END spanner_list_backup_operations]
// [START spanner_list_database_operations]
static void listDatabaseOperations(InstanceAdminClient instanceAdminClient, DatabaseAdminClient dbAdminClient, InstanceId instanceId) {
    Instance instance = instanceAdminClient.getInstance(instanceId.getInstance());
    // Get optimize restored database operations.
    Timestamp last24Hours = Timestamp.ofTimeSecondsAndNanos(TimeUnit.SECONDS.convert(TimeUnit.HOURS.convert(Timestamp.now().getSeconds(), TimeUnit.SECONDS) - 24, TimeUnit.HOURS), 0);
    String filter = String.format("(metadata.@type:type.googleapis.com/" + "google.spanner.admin.database.v1.OptimizeRestoredDatabaseMetadata) AND " + "(metadata.progress.start_time > \"%s\")", last24Hours);
    for (Operation op : instance.listDatabaseOperations(Options.filter(filter)).iterateAll()) {
        try {
            OptimizeRestoredDatabaseMetadata metadata = op.getMetadata().unpack(OptimizeRestoredDatabaseMetadata.class);
            System.out.println(String.format("Database %s restored from backup is %d%% optimized", metadata.getName(), metadata.getProgress().getProgressPercent()));
        } catch (InvalidProtocolBufferException e) {
            // The returned operation does not contain OptimizeRestoredDatabaseMetadata.
            System.err.println(e.getMessage());
        }
    }
}
Also used : Instance(com.google.cloud.spanner.Instance) OptimizeRestoredDatabaseMetadata(com.google.spanner.admin.database.v1.OptimizeRestoredDatabaseMetadata) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) Operation(com.google.longrunning.Operation) Timestamp(com.google.cloud.Timestamp)

Aggregations

Operation (com.google.longrunning.Operation)2 OptimizeRestoredDatabaseMetadata (com.google.spanner.admin.database.v1.OptimizeRestoredDatabaseMetadata)2 Timestamp (com.google.cloud.Timestamp)1 Instance (com.google.cloud.spanner.Instance)1 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)1 Test (org.junit.Test)1