Skip to content

Commit bef90d4

Browse files
committed
untange options vars and traits by
- explicitly selecting valid fields - better names for variables - documenting missing options in phpdoc - removing pass-by-reference
1 parent d310de0 commit bef90d4

32 files changed

Lines changed: 707 additions & 790 deletions

Core/src/LongRunning/LongRunningClientConnection.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,7 @@
2020
use Google\ApiCore\OperationResponse;
2121
use Google\ApiCore\Serializer;
2222
use Google\Cloud\Core\RequestProcessorTrait;
23-
use Google\LongRunning\Operation;
2423
use Google\LongRunning\ListOperationsRequest;
25-
use Google\LongRunning\GetOperationRequest;
26-
use Google\LongRunning\CancelOperationRequest;
27-
use Google\LongRunning\DeleteOperationRequest;
2824
use Google\Protobuf\Any;
2925

3026
/**

Spanner/src/Backup.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,11 @@
1919

2020
use Closure;
2121
use DateTimeInterface;
22-
use Google\Cloud\Core\LongRunning\LongRunningClientConnection;
23-
use Google\Cloud\Core\LongRunning\LongRunningOperation;
2422
use Google\ApiCore\ValidationException;
2523
use Google\Cloud\Core\Exception\NotFoundException;
2624
use Google\Cloud\Core\Iterator\ItemIterator;
27-
use Google\Cloud\Spanner\Admin\Database\V1\Backup as BackupProto;
25+
use Google\Cloud\Core\LongRunning\LongRunningClientConnection;
26+
use Google\Cloud\Core\LongRunning\LongRunningOperation;
2827
use Google\Cloud\Spanner\Admin\Database\V1\Backup\State;
2928
use Google\Cloud\Spanner\Admin\Database\V1\Client\DatabaseAdminClient;
3029
use Google\Cloud\Spanner\Admin\Database\V1\CopyBackupRequest;
@@ -33,6 +32,7 @@
3332
use Google\Cloud\Spanner\Admin\Database\V1\GetBackupRequest;
3433
use Google\Cloud\Spanner\Admin\Database\V1\UpdateBackupRequest;
3534
use Google\LongRunning\ListOperationsRequest;
35+
use Google\LongRunning\Operation as OperationProto;
3636

3737
/**
3838
* Represents a Cloud Spanner Backup.
@@ -408,7 +408,13 @@ public function longRunningOperations(array $options = []): ItemIterator
408408
return $this->buildLongRunningIterator(
409409
[$this->databaseAdminClient->getOperationsClient(), 'listOperations'],
410410
$request,
411-
$callOptions
411+
$callOptions,
412+
function (OperationProto $operation) {
413+
return $this->resumeOperation(
414+
$operation->getName(),
415+
$this->handleResponse($operation)
416+
);
417+
}
412418
);
413419
}
414420

Spanner/src/Batch/BatchClient.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ public function snapshot(array $options = [])
187187
$transactionOptions = $this->pluck('transactionOptions', $options);
188188
$transactionOptions['returnReadTimestamp'] = true;
189189

190-
$transactionOptions = $this->configureSnapshotOptions($transactionOptions);
190+
$transactionOptions = $this->configureReadOnlyTransactionOptions($transactionOptions);
191191

192192
if ($this->databaseRole !== null) {
193193
$sessionOptions['creator_role'] = $this->databaseRole;

Spanner/src/Database.php

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,12 @@
2727
use Google\Cloud\Core\Exception\ServiceException;
2828
use Google\Cloud\Core\Iam\IamManager;
2929
use Google\Cloud\Core\Iterator\ItemIterator;
30-
use Google\Cloud\Core\RequestHandler;
31-
use Google\Cloud\Core\Retry;
3230
use Google\Cloud\Core\LongRunning\LongRunningClientConnection;
3331
use Google\Cloud\Core\LongRunning\LongRunningOperation;
32+
use Google\Cloud\Core\RequestHandler;
33+
use Google\Cloud\Core\Retry;
3434
use Google\Cloud\Spanner\Admin\Database\V1\Client\DatabaseAdminClient;
3535
use Google\Cloud\Spanner\Admin\Database\V1\CreateDatabaseRequest;
36-
use Google\Cloud\Spanner\Admin\Database\V1\Database as DatabaseProto;
3736
use Google\Cloud\Spanner\Admin\Database\V1\Database\State;
3837
use Google\Cloud\Spanner\Admin\Database\V1\DatabaseDialect;
3938
use Google\Cloud\Spanner\Admin\Database\V1\DropDatabaseRequest;
@@ -55,6 +54,7 @@
5554
use Google\Cloud\Spanner\V1\Mutation\Write;
5655
use Google\Cloud\Spanner\V1\TypeCode;
5756
use Google\LongRunning\ListOperationsRequest;
57+
use Google\LongRunning\Operation as OperationProto;
5858
use Google\Protobuf\Duration;
5959
use Google\Protobuf\ListValue;
6060
use Google\Protobuf\Struct;
@@ -766,7 +766,19 @@ public function snapshot(array $options = []): Snapshot
766766
'singleUse' => false
767767
];
768768

769-
$options['transactionOptions'] = $this->configureSnapshotOptions($options);
769+
$options['transactionOptions'] = $this->configureReadOnlyTransactionOptions($options);
770+
771+
// For backwards compatibility - remove all PBReadOnly fields
772+
// This was previously being done in configureReadOnlyTransactionOptions
773+
// @TODO: clean this up
774+
unset(
775+
$options['returnReadTimestamp'],
776+
$options['strong'],
777+
$options['readTimestamp'],
778+
$options['exactStaleness'],
779+
$options['minReadTimestamp'],
780+
$options['maxStaleness'],
781+
);
770782

771783
$session = $this->selectSession(
772784
SessionPoolInterface::CONTEXT_READ,
@@ -827,8 +839,7 @@ public function transaction(array $options = []): Transaction
827839
throw new \BadMethodCallException('Nested transactions are not supported by this client.');
828840
}
829841

830-
// There isn't anything configurable here.
831-
$options['transactionOptions'] = $this->configureTransactionOptions();
842+
$options['transactionOptions'] = $this->configureReadWriteTransactionOptions();
832843

833844
$session = $this->selectSession(
834845
SessionPoolInterface::CONTEXT_READWRITE,
@@ -944,7 +955,9 @@ public function runTransaction(callable $operation, array $options = [])
944955
}
945956

946957
// There isn't anything configurable here.
947-
$options['transactionOptions'] = $this->configureTransactionOptions($options['transactionOptions'] ?? []);
958+
$options['transactionOptions'] = $this->configureReadWriteTransactionOptions(
959+
$options['transactionOptions'] ?? []
960+
);
948961

949962
$session = $this->selectSession(
950963
SessionPoolInterface::CONTEXT_READWRITE,
@@ -1933,6 +1946,7 @@ public function batchWrite(array $mutationGroups, array $options = []): \Generat
19331946
* Please note, if using the `priority` setting you may utilize the constants available
19341947
* on {@see \Google\Cloud\Spanner\V1\RequestOptions\Priority} to set a value.
19351948
* Please note, the `transactionTag` setting will be ignored as it is not supported for partitioned DML.
1949+
* @type array $transactionOptions Transaction options ({@see V1\TransactionOptions}).
19361950
* }
19371951
* @return int The number of rows modified.
19381952
*/
@@ -2285,7 +2299,8 @@ public function backupOperations(array $options = []): ItemIterator
22852299
return $this->buildLongRunningIterator(
22862300
[$this->databaseAdminClient, 'listBackupOperations'],
22872301
$request,
2288-
$callOptions + ['resource-prefix' => $this->name]
2302+
$callOptions + ['resource-prefix' => $this->name],
2303+
$this->getResultMapper()
22892304
);
22902305
}
22912306

@@ -2347,7 +2362,8 @@ public function databaseOperations(array $options = []): ItemIterator
23472362
return $this->buildLongRunningIterator(
23482363
[$this->databaseAdminClient, 'listDatabaseOperations'],
23492364
$request,
2350-
$callOptions + ['resource-prefix' => $this->name]
2365+
$callOptions + ['resource-prefix' => $this->name],
2366+
$this->getResultMapper()
23512367
);
23522368
}
23532369

@@ -2412,7 +2428,8 @@ public function longRunningOperations(array $options = []): ItemIterator
24122428
return $this->buildLongRunningIterator(
24132429
[$this->databaseAdminClient->getOperationsClient(), 'listOperations'],
24142430
$request,
2415-
$callOptions
2431+
$callOptions,
2432+
$this->getResultMapper()
24162433
);
24172434
}
24182435

@@ -2637,6 +2654,16 @@ private function databaseResultFunction(): Closure
26372654
};
26382655
}
26392656

2657+
private function getResultMapper()
2658+
{
2659+
return function (OperationProto $operation) {
2660+
return $this->resumeOperation(
2661+
$operation->getName(),
2662+
$this->handleResponse($operation)
2663+
);
2664+
};
2665+
}
2666+
26402667
/**
26412668
* Represent the class in a more readable and digestable fashion.
26422669
*

Spanner/src/Instance.php

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@
1818
namespace Google\Cloud\Spanner;
1919

2020
use Closure;
21-
use Google\Cloud\Core\LongRunning\LongRunningOperation;
22-
use Google\Cloud\Core\LongRunning\LongRunningClientConnection;
2321
use Google\Cloud\Core\Exception\NotFoundException;
2422
use Google\Cloud\Core\Iam\IamManager;
2523
use Google\Cloud\Core\Iterator\ItemIterator;
24+
use Google\Cloud\Core\LongRunning\LongRunningClientConnection;
25+
use Google\Cloud\Core\LongRunning\LongRunningOperation;
2626
use Google\Cloud\Core\RequestHandler;
2727
use Google\Cloud\Spanner\Admin\Database\V1\Client\DatabaseAdminClient;
2828
use Google\Cloud\Spanner\Admin\Database\V1\ListBackupsRequest;
@@ -31,12 +31,12 @@
3131
use Google\Cloud\Spanner\Admin\Instance\V1\CreateInstanceRequest;
3232
use Google\Cloud\Spanner\Admin\Instance\V1\DeleteInstanceRequest;
3333
use Google\Cloud\Spanner\Admin\Instance\V1\GetInstanceRequest;
34-
use Google\Cloud\Spanner\Admin\Instance\V1\Instance as InstanceProto;
3534
use Google\Cloud\Spanner\Admin\Instance\V1\Instance\State;
3635
use Google\Cloud\Spanner\Admin\Instance\V1\UpdateInstanceRequest;
3736
use Google\Cloud\Spanner\Session\SessionPoolInterface;
3837
use Google\Cloud\Spanner\V1\Client\SpannerClient as GapicSpannerClient;
3938
use Google\LongRunning\ListOperationsRequest;
39+
use Google\LongRunning\Operation as OperationProto;
4040

4141
/**
4242
* Represents a Cloud Spanner instance
@@ -797,7 +797,7 @@ private function fieldMask(array $instanceArray): array
797797
*/
798798
public function createInstanceArray(
799799
array $instanceArray,
800-
InstanceConfiguration $config = null
800+
?InstanceConfiguration $config = null
801801
): array {
802802
return $instanceArray + [
803803
'name' => $this->name,
@@ -812,7 +812,7 @@ public function createInstanceArray(
812812
*
813813
* Example:
814814
* ```
815-
* $operation = $spanner->resumeOperation($operationName);
815+
* $operation = $instance->resumeOperation($operationName);
816816
* ```
817817
*
818818
* @param string $operationName The Long Running Operation name.
@@ -842,7 +842,7 @@ public function resumeOperation($operationName, array $options = []): LongRunnin
842842
*
843843
* Example:
844844
* ```
845-
* $operations = $backup->longRunningOperations();
845+
* $operations = $instance->longRunningOperations();
846846
* ```
847847
*
848848
* @param array $options [optional] {
@@ -868,7 +868,13 @@ public function longRunningOperations(array $options = []): ItemIterator
868868
return $this->buildLongRunningIterator(
869869
[$this->instanceAdminClient->getOperationsClient(), 'listOperations'],
870870
$request,
871-
$callOptions
871+
$callOptions,
872+
function (OperationProto $operation) {
873+
return $this->resumeOperation(
874+
$operation->getName(),
875+
$this->handleResponse($operation)
876+
);
877+
}
872878
);
873879
}
874880

Spanner/src/InstanceConfiguration.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919

2020
use Closure;
2121
use Google\ApiCore\ApiException;
22-
use Google\Cloud\Core\LongRunning\LongRunningOperation;
23-
use Google\Cloud\Core\LongRunning\LongRunningClientConnection;
2422
use Google\ApiCore\ValidationException;
23+
use Google\Cloud\Core\LongRunning\LongRunningClientConnection;
24+
use Google\Cloud\Core\LongRunning\LongRunningOperation;
2525
use Google\Cloud\Spanner\Admin\Instance\V1\Client\InstanceAdminClient;
2626
use Google\Cloud\Spanner\Admin\Instance\V1\CreateInstanceConfigRequest;
2727
use Google\Cloud\Spanner\Admin\Instance\V1\DeleteInstanceConfigRequest;

Spanner/src/Middleware/SpannerMiddleware.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,15 @@
3232

3333
namespace Google\Cloud\Spanner\Middleware;
3434

35-
use Google\ApiCore\ArrayTrait;
3635
use Google\ApiCore\ApiException;
36+
use Google\ApiCore\ArrayTrait;
3737
use Google\ApiCore\BidiStream;
3838
use Google\ApiCore\Call;
3939
use Google\ApiCore\ClientStream;
4040
use Google\ApiCore\Middleware\MiddlewareInterface;
41-
use Google\Cloud\Spanner\Serializer;
4241
use Google\ApiCore\ServerStream;
4342
use Google\Cloud\Core\RequestProcessorTrait;
43+
use Google\Cloud\Spanner\Serializer;
4444
use GuzzleHttp\Promise\PromiseInterface;
4545
use Throwable;
4646

0 commit comments

Comments
 (0)