/** Gets the result of an async RPC call, handling any exceptions encountered. */ private static <T> T get(final Future<T> future) throws SpannerException { final Context context = Context.current(); try { return future.get(); } catch (InterruptedException e) { // We are the sole consumer of the future, so cancel it. future.cancel(true); throw SpannerExceptionFactory.propagateInterrupt(e); } catch (ExecutionException | CancellationException e) { throw newSpannerException(context, e); } }
/** Example to update the database DDL. */ // [TARGET updateDatabaseDdl(String, String, Iterable, String)] // [VARIABLE my_instance_id] // [VARIABLE my_database_id] public void updateDatabaseDdl(String instanceId, String databaseId) { // [START updateDatabaseDdl] try { dbAdminClient .updateDatabaseDdl( instanceId, databaseId, Arrays.asList("ALTER TABLE Albums ADD COLUMN MarketingBudget INT64"), null) .get(); } catch (ExecutionException e) { throw (SpannerException) e.getCause(); } catch (InterruptedException e) { throw SpannerExceptionFactory.propagateInterrupt(e); } // [END updateDatabaseDdl] }
throw (SpannerException) e.getCause(); } catch (InterruptedException e) { throw SpannerExceptionFactory.propagateInterrupt(e);
@Override protected final PartialResultSet computeNext() { PartialResultSet next; try { // TODO: Ideally honor io.grpc.Context while blocking here. In practice, // cancellation/deadline results in an error being delivered to "stream", which // should mean that we do not block significantly longer afterwards, but it would // be more robust to use poll() with a timeout. next = stream.take(); } catch (InterruptedException e) { // Treat interrupt as a request to cancel the read. throw SpannerExceptionFactory.propagateInterrupt(e); } if (next != END_OF_STREAM) { call.request(1); return next; } // All done - close() no longer needs to cancel the call. call = null; if (error != null) { throw SpannerExceptionFactory.newSpannerException(error); } endOfData(); return null; }
throw (SpannerException) e.getCause(); } catch (InterruptedException e) { throw SpannerExceptionFactory.propagateInterrupt(e);
/** Example to create an instance. */ public void createInstance( final String my_instance_id, final String my_config_id, final String my_client_project) { // [START instance_admin_client_create_instance] final String instanceId = my_instance_id; final String configId = my_config_id; final String clientProject = my_client_project; OperationFuture<Instance, CreateInstanceMetadata> op = instanceAdminClient.createInstance( InstanceInfo.newBuilder(InstanceId.of(clientProject, instanceId)) .setInstanceConfigId(InstanceConfigId.of(clientProject, configId)) .setDisplayName(instanceId) .setNodeCount(1) .build()); try { op.get(); } catch (ExecutionException e) { throw (SpannerException) e.getCause(); } catch (InterruptedException e) { throw SpannerExceptionFactory.propagateInterrupt(e); } // [END instance_admin_client_create_instance] }
return Operation.create(rpc, proto, parser); } catch (InterruptedException e) { throw SpannerExceptionFactory.propagateInterrupt(e); } catch (ExecutionException e) { Throwable cause = e.getCause();
static void addMarketingBudget(DatabaseAdminClient adminClient, DatabaseId dbId) { OperationFuture<Void, UpdateDatabaseDdlMetadata> op = adminClient .updateDatabaseDdl( dbId.getInstanceId().getInstance(), dbId.getDatabase(), Arrays.asList("ALTER TABLE Albums ADD COLUMN MarketingBudget INT64"), null); try { // Initiate the request which returns an OperationFuture. op.get(); System.out.println("Added MarketingBudget column"); } catch (ExecutionException e) { // If the operation failed during execution, expose the cause. throw (SpannerException) e.getCause(); } catch (InterruptedException e) { // Throw when a thread is waiting, sleeping, or otherwise occupied, // and the thread is interrupted, either before or during the activity. throw SpannerExceptionFactory.propagateInterrupt(e); } } // [END spanner_add_column]
static void addIndex(DatabaseAdminClient adminClient, DatabaseId dbId) { OperationFuture<Void, UpdateDatabaseDdlMetadata> op = adminClient .updateDatabaseDdl( dbId.getInstanceId().getInstance(), dbId.getDatabase(), Arrays.asList("CREATE INDEX AlbumsByAlbumTitle ON Albums(AlbumTitle)"), null); try { // Initiate the request which returns an OperationFuture. op.get(); System.out.println("Added AlbumsByAlbumTitle index"); } catch (ExecutionException e) { // If the operation failed during execution, expose the cause. throw (SpannerException) e.getCause(); } catch (InterruptedException e) { // Throw when a thread is waiting, sleeping, or otherwise occupied, // and the thread is interrupted, either before or during the activity. throw SpannerExceptionFactory.propagateInterrupt(e); } } // [END spanner_create_index]
static void addStoringIndex(DatabaseAdminClient adminClient, DatabaseId dbId) { OperationFuture<Void, UpdateDatabaseDdlMetadata> op = adminClient .updateDatabaseDdl( dbId.getInstanceId().getInstance(), dbId.getDatabase(), Arrays.asList( "CREATE INDEX AlbumsByAlbumTitle2 ON Albums(AlbumTitle) " + "STORING (MarketingBudget)"), null); try { // Initiate the request which returns an OperationFuture. op.get(); System.out.println("Added AlbumsByAlbumTitle2 index"); } catch (ExecutionException e) { // If the operation failed during execution, expose the cause. throw (SpannerException) e.getCause(); } catch (InterruptedException e) { // Throw when a thread is waiting, sleeping, or otherwise occupied, // and the thread is interrupted, either before or during the activity. throw SpannerExceptionFactory.propagateInterrupt(e); } } // [END spanner_create_storing_index]
static void addCommitTimestamp(DatabaseAdminClient adminClient, DatabaseId dbId) { OperationFuture<Void, UpdateDatabaseDdlMetadata> op = adminClient .updateDatabaseDdl( dbId.getInstanceId().getInstance(), dbId.getDatabase(), Arrays.asList( "ALTER TABLE Albums ADD COLUMN LastUpdateTime TIMESTAMP " + "OPTIONS (allow_commit_timestamp=true)"), null); try { // Initiate the request which returns an OperationFuture. op.get(); System.out.println("Added LastUpdateTime as a commit timestamp column in Albums table."); } catch (ExecutionException e) { // If the operation failed during execution, expose the cause. throw (SpannerException) e.getCause(); } catch (InterruptedException e) { // Throw when a thread is waiting, sleeping, or otherwise occupied, // and the thread is interrupted, either before or during the activity. throw SpannerExceptionFactory.propagateInterrupt(e); } } // [END spanner_add_timestamp_column]
static void createTableWithTimestamp(DatabaseAdminClient dbAdminClient, DatabaseId id) { OperationFuture<Void, UpdateDatabaseDdlMetadata> op = dbAdminClient.updateDatabaseDdl( id.getInstanceId().getInstance(), id.getDatabase(), Arrays.asList( "CREATE TABLE Performances (\n" + " SingerId INT64 NOT NULL,\n" + " VenueId INT64 NOT NULL,\n" + " EventDate Date,\n" + " Revenue INT64, \n" + " LastUpdateTime TIMESTAMP NOT NULL OPTIONS (allow_commit_timestamp=true)\n" + ") PRIMARY KEY (SingerId, VenueId, EventDate),\n" + " INTERLEAVE IN PARENT Singers ON DELETE CASCADE"), null); try { // Initiate the request which returns an OperationFuture. op.get(); System.out.println("Created Performances table in database: [" + id + "]"); } catch (ExecutionException e) { // If the operation failed during execution, expose the cause. throw (SpannerException) e.getCause(); } catch (InterruptedException e) { // Throw when a thread is waiting, sleeping, or otherwise occupied, // and the thread is interrupted, either before or during the activity. throw SpannerExceptionFactory.propagateInterrupt(e); } } // [END spanner_create_table_with_timestamp_column]
throw SpannerExceptionFactory.propagateInterrupt(e);
/** Gets the result of an async RPC call, handling any exceptions encountered. */ private static <T> T get(final Future<T> future) throws SpannerException { final Context context = Context.current(); try { return future.get(); } catch (InterruptedException e) { // We are the sole consumer of the future, so cancel it. future.cancel(true); throw SpannerExceptionFactory.propagateInterrupt(e); } catch (ExecutionException | CancellationException e) { throw newSpannerException(context, e); } }
@Override protected final PartialResultSet computeNext() { PartialResultSet next; try { // TODO: Ideally honor io.grpc.Context while blocking here. In practice, // cancellation/deadline results in an error being delivered to "stream", which // should mean that we do not block significantly longer afterwards, but it would // be more robust to use poll() with a timeout. next = stream.take(); } catch (InterruptedException e) { // Treat interrupt as a request to cancel the read. throw SpannerExceptionFactory.propagateInterrupt(e); } if (next != END_OF_STREAM) { call.request(1); return next; } // All done - close() no longer needs to cancel the call. call = null; if (error != null) { throw SpannerExceptionFactory.newSpannerException(error); } endOfData(); return null; }
return Operation.create(rpc, proto, parser); } catch (InterruptedException e) { throw SpannerExceptionFactory.propagateInterrupt(e); } catch (ExecutionException e) { Throwable cause = e.getCause();