Search in sources :

Example 66 with Database

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

the class DatabaseAdminGaxTest method getDatabaseTest.

@Test
public void getDatabaseTest() {
    Exception exception = setupException();
    DatabaseName name2 = DatabaseName.of(PROJECT, INSTANCE, "DATABASE");
    com.google.spanner.admin.database.v1.Database expectedResponse = com.google.spanner.admin.database.v1.Database.newBuilder().setName(name2.toString()).build();
    if (exceptionAtCall == 0) {
        mockDatabaseAdmin.addException(exception);
    }
    mockDatabaseAdmin.addResponse(expectedResponse);
    if (exceptionAtCall == 1) {
        mockDatabaseAdmin.addException(exception);
    }
    mockDatabaseAdmin.addResponse(expectedResponse);
    for (int i = 0; i < 2; i++) {
        Database actualResponse = client.getDatabase(INSTANCE, "DATABASE");
        Assert.assertEquals(name2.toString(), actualResponse.getId().getName());
    }
    List<AbstractMessage> actualRequests = mockDatabaseAdmin.getRequests();
    Assert.assertEquals(2, actualRequests.size());
}
Also used : AbstractMessage(com.google.protobuf.AbstractMessage) DatabaseName(com.google.spanner.admin.database.v1.DatabaseName) ExpectedException(org.junit.rules.ExpectedException) IOException(java.io.IOException) StatusRuntimeException(io.grpc.StatusRuntimeException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 67 with Database

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

the class RemoteSpannerHelper method createTestDatabase.

/**
 * Creates a test database defined by {@code statements} in the test instance. A {@code CREATE
 * DATABASE ...} statement should not be included; an appropriate name will be chosen and the
 * statement generated accordingly.
 */
public Database createTestDatabase(Dialect dialect, Iterable<String> statements) throws SpannerException {
    String dbId = getUniqueDatabaseId();
    Database databaseToCreate = client.getDatabaseAdminClient().newDatabaseBuilder(DatabaseId.of(instanceId.getProject(), instanceId.getInstance(), dbId)).setDialect(dialect).build();
    try {
        Iterable<String> ddlStatements = dialect == Dialect.POSTGRESQL ? Collections.emptyList() : statements;
        OperationFuture<Database, CreateDatabaseMetadata> op = client.getDatabaseAdminClient().createDatabase(databaseToCreate, ddlStatements);
        Database db = op.get();
        if (dialect == Dialect.POSTGRESQL && Iterables.size(statements) > 0) {
            client.getDatabaseAdminClient().updateDatabaseDdl(instanceId.getInstance(), dbId, statements, null).get();
        }
        logger.log(Level.FINE, "Created test database {0}", db.getId());
        dbs.add(db);
        return db;
    } catch (Exception e) {
        throw SpannerExceptionFactory.newSpannerException(e);
    }
}
Also used : Database(com.google.cloud.spanner.Database) CreateDatabaseMetadata(com.google.spanner.admin.database.v1.CreateDatabaseMetadata) SpannerException(com.google.cloud.spanner.SpannerException)

Example 68 with Database

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

the class MockDatabaseAdminServiceImpl method matchesFilter.

private boolean matchesFilter(Object obj, String filter) throws Exception {
    if (!Strings.isNullOrEmpty(filter)) {
        Set<String> matches = filterMatches.get(filter);
        if (matches != null) {
            String name = (String) obj.getClass().getMethod("getName").invoke(obj);
            return matches.contains(name);
        }
        if (obj instanceof Operation) {
            Operation operation = (Operation) obj;
            Pattern pattern = Pattern.compile("(?:\\(metadata.@type:type.googleapis.com/(.*)\\)) AND (?:\\(metadata.(?:name|database):(.*)\\)|\\(name:(.*)/operations/\\))");
            Matcher matcher = pattern.matcher(filter);
            if (matcher.matches()) {
                String type = matcher.group(1);
                String objectName = matcher.group(2);
                if (objectName == null) {
                    objectName = matcher.group(3);
                }
                Any anyMetadata = operation.getMetadata();
                if (anyMetadata.getTypeUrl().endsWith(type)) {
                    if (type.equals(CreateBackupMetadata.getDescriptor().getFullName())) {
                        CreateBackupMetadata metadata = operation.getMetadata().unpack(CreateBackupMetadata.class);
                        return metadata.getName().equals(objectName);
                    } else if (type.equals(CreateDatabaseMetadata.getDescriptor().getFullName())) {
                        CreateDatabaseMetadata metadata = operation.getMetadata().unpack(CreateDatabaseMetadata.class);
                        return metadata.getDatabase().equals(objectName);
                    } else if (type.equals(RestoreDatabaseMetadata.getDescriptor().getFullName())) {
                        RestoreDatabaseMetadata metadata = operation.getMetadata().unpack(RestoreDatabaseMetadata.class);
                        return metadata.getName().equals(objectName);
                    }
                }
            }
        }
        return false;
    }
    return true;
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) CreateBackupMetadata(com.google.spanner.admin.database.v1.CreateBackupMetadata) RestoreDatabaseMetadata(com.google.spanner.admin.database.v1.RestoreDatabaseMetadata) Operation(com.google.longrunning.Operation) CreateDatabaseMetadata(com.google.spanner.admin.database.v1.CreateDatabaseMetadata) Any(com.google.protobuf.Any)

Example 69 with Database

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

the class MockDatabaseAdminServiceImpl method createBackup.

@Override
public void createBackup(CreateBackupRequest request, StreamObserver<Operation> responseObserver) {
    requests.add(request);
    try {
        createBackupStartupExecutionTime.simulateExecutionTime(exceptions, false, freezeLock);
        String name = String.format("%s/backups/%s", request.getParent(), request.getBackupId());
        MockDatabase db = databases.get(request.getBackup().getDatabase());
        if (db == null) {
            responseObserver.onError(Status.NOT_FOUND.withDescription(String.format("Database with name %s not found", request.getBackup().getDatabase())).asRuntimeException());
            return;
        }
        MockBackup bck = new MockBackup(name, request.getBackup(), db);
        if (backups.putIfAbsent(name, bck) == null) {
            CreateBackupMetadata metadata = CreateBackupMetadata.newBuilder().setName(name).setDatabase(bck.database).setProgress(OperationProgress.newBuilder().setStartTime(Timestamp.newBuilder().setSeconds(System.currentTimeMillis() / 1000L).build()).setProgressPercent(0)).build();
            Operation operation = Operation.newBuilder().setMetadata(Any.pack(metadata)).setResponse(Any.pack(bck.toProto())).setName(operations.generateOperationName(name)).build();
            operations.addOperation(operation, new CreateBackupCallable(operation.getName(), name));
            createBackupResponseExecutionTime.simulateExecutionTime(exceptions, false, freezeLock);
            responseObserver.onNext(operation);
            responseObserver.onCompleted();
        } else {
            responseObserver.onError(Status.ALREADY_EXISTS.withDescription(String.format("Backup with name %s already exists", name)).asRuntimeException());
        }
    } catch (Throwable t) {
        responseObserver.onError(t);
    }
}
Also used : CreateBackupMetadata(com.google.spanner.admin.database.v1.CreateBackupMetadata) Operation(com.google.longrunning.Operation)

Example 70 with Database

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

the class MockDatabaseAdminServiceImplTest method restoreDatabase.

@Test
public void restoreDatabase() throws InterruptedException, ExecutionException {
    createTestDb();
    createTestBackup();
    RestoreDatabaseRequest request = RestoreDatabaseRequest.newBuilder().setBackup(TEST_BCK_NAME).setDatabaseId("restored-db").setParent(TEST_PARENT).build();
    OperationFuture<Database, RestoreDatabaseMetadata> op = client.restoreDatabaseOperationCallable().futureCall(request);
    Database restoredDb = op.get();
    assertThat(restoredDb.getName()).isEqualTo(String.format("%s/databases/%s", TEST_PARENT, "restored-db"));
    assertThat(restoredDb.getRestoreInfo().getBackupInfo().getBackup()).isEqualTo(TEST_BCK_NAME);
    assertThat(restoredDb.getRestoreInfo().getBackupInfo().getSourceDatabase()).isEqualTo(TEST_DB_NAME);
}
Also used : RestoreDatabaseRequest(com.google.spanner.admin.database.v1.RestoreDatabaseRequest) Database(com.google.spanner.admin.database.v1.Database) RestoreDatabaseMetadata(com.google.spanner.admin.database.v1.RestoreDatabaseMetadata) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)53 CreateDatabaseMetadata (com.google.spanner.admin.database.v1.CreateDatabaseMetadata)31 Database (com.google.cloud.spanner.Database)29 AbstractMessage (com.google.protobuf.AbstractMessage)24 ArrayList (java.util.ArrayList)24 ExecutionException (java.util.concurrent.ExecutionException)22 Database (com.google.spanner.admin.database.v1.Database)21 Operation (com.google.longrunning.Operation)18 ByteString (com.google.protobuf.ByteString)16 DatabaseAdminClient (com.google.cloud.spanner.DatabaseAdminClient)12 SpannerException (com.google.cloud.spanner.SpannerException)12 Database (org.molgenis.emx2.Database)12 CreateBackupMetadata (com.google.spanner.admin.database.v1.CreateBackupMetadata)11 DatabaseName (com.google.spanner.admin.database.v1.DatabaseName)10 RestoreDatabaseMetadata (com.google.spanner.admin.database.v1.RestoreDatabaseMetadata)9 Timestamp (com.google.cloud.Timestamp)8 Spanner (com.google.cloud.spanner.Spanner)8 RestoreDatabaseRequest (com.google.spanner.admin.database.v1.RestoreDatabaseRequest)7 StatusRuntimeException (io.grpc.StatusRuntimeException)7 Backup (com.google.cloud.spanner.Backup)6