@@ -122,8 +122,8 @@ class SpannerImpl extends BaseService<SpannerOptions> implements Spanner {
122122 @ GuardedBy ("this" )
123123 private final Map <DatabaseId , DatabaseClientImpl > dbClients = new HashMap <>();
124124
125- private final DatabaseAdminClient dbAdminClient = new DatabaseAdminClientImpl () ;
126- private final InstanceAdminClient instanceClient = new InstanceAdminClientImpl ( dbAdminClient ) ;
125+ private final DatabaseAdminClient dbAdminClient ;
126+ private final InstanceAdminClient instanceClient ;
127127
128128 @ GuardedBy ("this" )
129129 private boolean spannerIsClosed = false ;
@@ -132,6 +132,8 @@ class SpannerImpl extends BaseService<SpannerOptions> implements Spanner {
132132 super (options );
133133 this .rpc = rpc ;
134134 this .defaultPrefetchChunks = defaultPrefetchChunks ;
135+ this .dbAdminClient = new DatabaseAdminClientImpl (options .getProjectId (), rpc );
136+ this .instanceClient = new InstanceAdminClientImpl (options .getProjectId (), rpc , dbAdminClient );
135137 }
136138
137139 SpannerImpl (SpannerOptions options ) {
@@ -205,7 +207,8 @@ static <T> T runWithRetries(Callable<T> callable) {
205207 logger .log (Level .FINE , "Retryable exception, will sleep and retry" , e );
206208 backoffSleep (context , backOff );
207209 } catch (Exception e ) {
208- throw Throwables .propagate (e );
210+ Throwables .throwIfUnchecked (e );
211+ throw newSpannerException (ErrorCode .INTERNAL , "Unexpected exception thrown" , e );
209212 }
210213 }
211214 }
@@ -323,19 +326,7 @@ Object value() {
323326 return ImmutableMap .copyOf (tmp );
324327 }
325328
326- private String getProjectId () {
327- return getOptions ().getProjectId ();
328- }
329-
330- private String getInstanceName (String instanceId ) {
331- return new InstanceId (getProjectId (), instanceId ).getName ();
332- }
333-
334- private String getDatabaseName (String instanceId , String databaseId ) {
335- return new DatabaseId (new InstanceId (getProjectId (), instanceId ), databaseId ).getName ();
336- }
337-
338- private <T extends Message > T unpack (Any response , Class <T > clazz ) throws SpannerException {
329+ private static <T extends Message > T unpack (Any response , Class <T > clazz ) throws SpannerException {
339330 try {
340331 return response .unpack (clazz );
341332 } catch (InvalidProtocolBufferException e ) {
@@ -344,7 +335,7 @@ private <T extends Message> T unpack(Any response, Class<T> clazz) throws Spanne
344335 }
345336 }
346337
347- private abstract class PageFetcher <S , T > implements NextPageFetcher <S > {
338+ private static abstract class PageFetcher <S , T > implements NextPageFetcher <S > {
348339 private String nextPageToken ;
349340
350341 @ Override
@@ -370,12 +361,21 @@ public Paginated<T> call() {
370361 abstract S fromProto (T proto );
371362 }
372363
373- private String randomOperationId () {
364+ private static String randomOperationId () {
374365 UUID uuid = UUID .randomUUID ();
375366 return ("r" + uuid .toString ()).replace ("-" , "_" );
376367 }
377368
378- class DatabaseAdminClientImpl implements DatabaseAdminClient {
369+ static class DatabaseAdminClientImpl implements DatabaseAdminClient {
370+
371+ private final String projectId ;
372+ private final SpannerRpc rpc ;
373+
374+ DatabaseAdminClientImpl (String projectId , SpannerRpc rpc ) {
375+ this .projectId = projectId ;
376+ this .rpc = rpc ;
377+ }
378+
379379 @ Override
380380 public Operation <Database , CreateDatabaseMetadata > createDatabase (
381381 String instanceId , String databaseId , Iterable <String > statements ) throws SpannerException {
@@ -436,7 +436,7 @@ public Operation<Void, UpdateDatabaseDdlMetadata> call() {
436436 String opName =
437437 OP_NAME_TEMPLATE .instantiate (
438438 "project" ,
439- getProjectId () ,
439+ projectId ,
440440 "instance" ,
441441 instanceId ,
442442 "database" ,
@@ -519,18 +519,30 @@ public Database fromProto(com.google.spanner.admin.database.v1.Database proto) {
519519 }
520520 return pageFetcher .getNextPage ();
521521 }
522+
523+ private String getInstanceName (String instanceId ) {
524+ return new InstanceId (projectId , instanceId ).getName ();
525+ }
526+
527+ private String getDatabaseName (String instanceId , String databaseId ) {
528+ return new DatabaseId (new InstanceId (projectId , instanceId ), databaseId ).getName ();
529+ }
522530 }
523531
524- class InstanceAdminClientImpl implements InstanceAdminClient {
532+ static class InstanceAdminClientImpl implements InstanceAdminClient {
525533 final DatabaseAdminClient dbClient ;
534+ final String projectId ;
535+ final SpannerRpc rpc ;
526536
527- InstanceAdminClientImpl (DatabaseAdminClient dbClient ) {
537+ InstanceAdminClientImpl (String projectId , SpannerRpc rpc , DatabaseAdminClient dbClient ) {
538+ this .projectId = projectId ;
539+ this .rpc = rpc ;
528540 this .dbClient = dbClient ;
529541 }
530542
531543 @ Override
532544 public InstanceConfig getInstanceConfig (String configId ) throws SpannerException {
533- final String instanceConfigName = new InstanceConfigId (getProjectId () , configId ).getName ();
545+ final String instanceConfigName = new InstanceConfigId (projectId , configId ).getName ();
534546 return runWithRetries (
535547 new Callable <InstanceConfig >() {
536548 @ Override
@@ -570,7 +582,7 @@ public InstanceConfig fromProto(
570582 @ Override
571583 public Operation <Instance , CreateInstanceMetadata > createInstance (InstanceInfo instance )
572584 throws SpannerException {
573- String projectName = PROJECT_NAME_TEMPLATE .instantiate ("project" , getProjectId () );
585+ String projectName = PROJECT_NAME_TEMPLATE .instantiate ("project" , projectId );
574586 com .google .longrunning .Operation op =
575587 rpc .createInstance (projectName , instance .getId ().getInstance (), instance .toProto ());
576588 return Operation .create (
@@ -594,7 +606,7 @@ public CreateInstanceMetadata parseMetadata(Any metadata) {
594606
595607 @ Override
596608 public Instance getInstance (String instanceId ) throws SpannerException {
597- final String instanceName = new InstanceId (getProjectId () , instanceId ).getName ();
609+ final String instanceName = new InstanceId (projectId , instanceId ).getName ();
598610 return runWithRetries (
599611 new Callable <Instance >() {
600612 @ Override
@@ -635,7 +647,7 @@ public void deleteInstance(final String instanceId) throws SpannerException {
635647 new Callable <Void >() {
636648 @ Override
637649 public Void call () {
638- rpc .deleteInstance (new InstanceId (getProjectId () , instanceId ).getName ());
650+ rpc .deleteInstance (new InstanceId (projectId , instanceId ).getName ());
639651 return null ;
640652 }
641653 });
@@ -2006,7 +2018,6 @@ protected long[] getLongArrayInternal(int columnIndex) {
20062018 }
20072019
20082020 @ Override
2009- @ SuppressWarnings ("unchecked" ) // We know ARRAY<INT64> produces an Int64Array.
20102021 protected Int64Array getLongListInternal (int columnIndex ) {
20112022 return (Int64Array ) rowData .get (columnIndex );
20122023 }
@@ -2017,7 +2028,6 @@ protected double[] getDoubleArrayInternal(int columnIndex) {
20172028 }
20182029
20192030 @ Override
2020- @ SuppressWarnings ("unchecked" ) // We know ARRAY<FLOAT64> produces a Float64Array.
20212031 protected Float64Array getDoubleListInternal (int columnIndex ) {
20222032 return (Float64Array ) rowData .get (columnIndex );
20232033 }
0 commit comments