Skip to content

Commit

Permalink
LRO cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
bshaffer committed Nov 16, 2024
1 parent f3872d1 commit 390ba8b
Show file tree
Hide file tree
Showing 12 changed files with 250 additions and 158 deletions.
41 changes: 10 additions & 31 deletions Spanner/src/Backup.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
use Google\Cloud\Spanner\Admin\Database\V1\GetBackupRequest;
use Google\Cloud\Spanner\Admin\Database\V1\UpdateBackupRequest;
use Google\LongRunning\ListOperationsRequest;
use Google\LongRunning\Operation;

/**
* Represents a Cloud Spanner Backup.
Expand Down Expand Up @@ -104,7 +103,7 @@ public function __construct(
*/
public function create($database, DateTimeInterface $expireTime, array $options = [])
{
list($data, $callOptions) = $this->splitOptionalArgs($options);
[$data, $callOptions] = $this->splitOptionalArgs($options);
$data = $this->validateAndFormatVersionTime($data);

$data += [
Expand Down Expand Up @@ -153,7 +152,7 @@ public function create($database, DateTimeInterface $expireTime, array $options
*/
public function createCopy(Backup $newBackup, DateTimeInterface $expireTime, array $options = [])
{
list($data, $callOptions) = $this->splitOptionalArgs($options);
[$data, $callOptions] = $this->splitOptionalArgs($options);
$data += [
'parent' => $newBackup->instance->name(),
'backupId' => DatabaseAdminClient::parseName($newBackup->name)['backup'],
Expand Down Expand Up @@ -181,7 +180,7 @@ public function createCopy(Backup $newBackup, DateTimeInterface $expireTime, arr
*/
public function delete(array $options = [])
{
list($data, $callOptions) = $this->splitOptionalArgs($options);
[$data, $callOptions] = $this->splitOptionalArgs($options);
$data += [
'name' => $this->name
];
Expand Down Expand Up @@ -265,7 +264,7 @@ public function name()
*/
public function reload(array $options = [])
{
list($data, $callOptions) = $this->splitOptionalArgs($options);
[$data, $callOptions] = $this->splitOptionalArgs($options);
$data += [
'name' => $this->name
];
Expand Down Expand Up @@ -322,7 +321,7 @@ public function state(array $options = [])
*/
public function updateExpireTime(DateTimeInterface $newTimestamp, array $options = [])
{
list($data, $callOptions) = $this->splitOptionalArgs($options);
[$data, $callOptions] = $this->splitOptionalArgs($options);
$data += [
'backup' => [
'name' => $this->name(),
Expand Down Expand Up @@ -386,34 +385,14 @@ public function resumeOperation($operationName, array $options = [])
*/
public function longRunningOperations(array $options = [])
{
$options += ['name' => $this->name . '/operations'];
$resultLimit = $this->pluck('resultLimit', $options, false) ?: 0;
[$data, $callOptions] = $this->splitOptionalArgs($options);
$request = $this->serializer->decodeMessage(new ListOperationsRequest(), $data);
$request->setName($this->name . '/operations');

return new ItemIterator(
new PageIterator(
function (Operation $operation) {
return $this->resumeOperation(
$operation->getName(),
['lastProtoResponse' => $operation]
);
},
function (array $args) {
$nextPageToken = $this->pluck('pageToken', $args, false) ?: null;
$operationsClient = $this->databaseAdminClient->getOperationsClient();
$page = $operationsClient->listOperations(...$args)->getPage();
return [
'operations' => iterator_to_array($page->getResponseObject()->getOperations()),
'nextResultTokenPath' => $page->getNextPageToken(),
];
},
[$request, $callOptions],
[
'itemsKey' => 'operations',
'resultLimit' => $resultLimit
]
)
return $this->buildLongRunningIterator(
[$this->databaseAdminClient->getOperationsClient(), 'listOperations'],
$request,
$callOptions
);
}

Expand Down
139 changes: 66 additions & 73 deletions Spanner/src/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
use Google\Cloud\Spanner\V1\Mutation\Delete;
use Google\Cloud\Spanner\V1\Mutation\Write;
use Google\Cloud\Spanner\V1\TypeCode;
use Google\LongRunning\ListOperationsRequest;
use Google\Protobuf\Duration;
use Google\Protobuf\ListValue;
use Google\Protobuf\Struct;
Expand Down Expand Up @@ -353,7 +354,7 @@ public function info(array $options = []): array
*/
public function reload(array $options = []): array
{
list($data, $callOptions) = $this->splitOptionalArgs($options);
[$data, $callOptions] = $this->splitOptionalArgs($options);
$data['name'] = $this->name;

$request = $this->serializer->decodeMessage(new GetDatabaseRequest(), $data);
Expand Down Expand Up @@ -412,7 +413,7 @@ public function exists(array $options = []): bool
*/
public function create(array $options = []): OperationResponse
{
list($data, $callOptions) = $this->splitOptionalArgs($options);
[$data, $callOptions] = $this->splitOptionalArgs($options);
$dialect = $data['databaseDialect'] ?? DatabaseDialect::DATABASE_DIALECT_UNSPECIFIED;

$data += [
Expand Down Expand Up @@ -471,7 +472,7 @@ public function restore($backup, array $options = []): OperationResponse
*/
public function updateDatabase(array $options = []): OperationResponse
{
list($data, $callOptions) = $this->splitOptionalArgs($options);
[$data, $callOptions] = $this->splitOptionalArgs($options);
$fieldMask = [];

if (isset($data['enableDropProtection'])) {
Expand Down Expand Up @@ -555,7 +556,7 @@ public function updateDdl($statement, array $options = []): OperationResponse
*/
public function updateDdlBatch(array $statements, array $options = []): OperationResponse
{
list($data, $callOptions) = $this->splitOptionalArgs($options);
[$data, $callOptions] = $this->splitOptionalArgs($options);
$data += [
'database' => $this->name,
'statements' => $statements
Expand Down Expand Up @@ -591,7 +592,7 @@ public function updateDdlBatch(array $statements, array $options = []): Operatio
*/
public function drop(array $options = []): void
{
list($data, $callOptions) = $this->splitOptionalArgs($options);
[$data, $callOptions] = $this->splitOptionalArgs($options);
$data['database'] = $this->name;

$request = $this->serializer->decodeMessage(new DropDatabaseRequest(), $data);
Expand Down Expand Up @@ -628,7 +629,7 @@ public function drop(array $options = []): void
*/
public function ddl(array $options = []): array
{
list($data, $callOptions) = $this->splitOptionalArgs($options);
[$data, $callOptions] = $this->splitOptionalArgs($options);
$data['database'] = $this->name;

$request = $this->serializer->decodeMessage(new GetDatabaseDdlRequest(), $data);
Expand Down Expand Up @@ -1774,7 +1775,7 @@ public function batchWrite(array $mutationGroups, array $options = []): \Generat
);

try {
list($data, $callOptions) = $this->splitOptionalArgs($options);
[$data, $callOptions] = $this->splitOptionalArgs($options);
$data += [
'session' => $session->name(),
'mutationGroups' => $mutationGroups
Expand Down Expand Up @@ -2198,7 +2199,7 @@ public function identity(): array
*/
public function batchCreateSessions(array $options): array
{
list($data, $callOptions) = $this->splitOptionalArgs($options);
[$data, $callOptions] = $this->splitOptionalArgs($options);
$data['database'] = $this->name;

$request = $this->serializer->decodeMessage(new BatchCreateSessionsRequest(), $data);
Expand All @@ -2221,7 +2222,7 @@ public function batchCreateSessions(array $options): array
*/
public function deleteSessionAsync(array $options): PromiseInterface
{
list($data, $callOptions) = $this->splitOptionalArgs($options);
[$data, $callOptions] = $this->splitOptionalArgs($options);

$request = $this->serializer->decodeMessage(new DeleteSessionRequest(), $data);
$callOptions = $this->addResourcePrefixHeader($callOptions, $this->name);
Expand Down Expand Up @@ -2252,35 +2253,15 @@ public function deleteSessionAsync(array $options): PromiseInterface
*/
public function backupOperations(array $options = []): ItemIterator
{
list($data, $callOptions) = $this->splitOptionalArgs($options);
$data['parent'] = $this->instance->name();

$resultLimit = $this->pluck('resultLimit', $options, false);
return new ItemIterator(
new PageIterator(
function (array $operation) {
return new OperationResponse(
$operation['name'],
$this->databaseAdminClient->getOperationsClient(),
);
},
function ($callOptions) use ($data) {
if (isset($callOptions['pageToken'])) {
$data['pageToken'] = $callOptions['pageToken'];
}
[$data, $callOptions] = $this->splitOptionalArgs($options);
$callOptions = $this->addResourcePrefixHeader($callOptions, $this->name);
$request = $this->serializer->decodeMessage(new ListBackupOperationsRequest(), $data);
$request->setParent($this->instance->name());

$request = $this->serializer->decodeMessage(new ListBackupOperationsRequest(), $data);
$callOptions = $this->addResourcePrefixHeader($callOptions, $this->name);

$response = $this->databaseAdminClient->listBackupOperations($request, $callOptions);
return $this->handleResponse($response);
},
$callOptions,
[
'itemsKey' => 'operations',
'resultLimit' => $resultLimit
]
)
return $this->buildLongRunningIterator(
[$this->databaseAdminClient, 'listBackupOperations'],
$request,
$callOptions
);
}

Expand All @@ -2297,7 +2278,7 @@ function ($callOptions) use ($data) {
*/
public function createDatabaseFromBackup($name, $backup, array $options = []): OperationResponse
{
list($data, $callOptions) = $this->splitOptionalArgs($options);
[$data, $callOptions] = $this->splitOptionalArgs($options);
$data += [
'parent' => $this->instance->name(),
'databaseId' => $this->databaseIdOnly($name),
Expand Down Expand Up @@ -2334,35 +2315,15 @@ public function createDatabaseFromBackup($name, $backup, array $options = []): O
*/
public function databaseOperations(array $options = []): ItemIterator
{
list($data, $callOptions) = $this->splitOptionalArgs($options);
$data['parent'] = $this->instance->name();

$resultLimit = $this->pluck('resultLimit', $options, false);
return new ItemIterator(
new PageIterator(
function (array $operation) {
return new OperationResponse(
$operation['name'],
$this->databaseAdminClient->getOperationsClient(),
);
},
function ($callOptions) use ($data) {
if (isset($callOptions['pageToken'])) {
$data['pageToken'] = $callOptions['pageToken'];
}
[$data, $callOptions] = $this->splitOptionalArgs($options);
$callOptions = $this->addResourcePrefixHeader($callOptions, $this->name);
$request = $this->serializer->decodeMessage(new ListDatabaseOperationsRequest(), $data);
$request->setParent($this->instance->name());

$request = $this->serializer->decodeMessage(new ListDatabaseOperationsRequest(), $data);
$callOptions = $this->addResourcePrefixHeader($callOptions, $this->instance->name());

$response = $this->databaseAdminClient->listDatabaseOperations($request, $callOptions);
return $this->handleResponse($response);
},
$callOptions,
[
'itemsKey' => 'operations',
'resultLimit' => $resultLimit
]
)
return $this->buildLongRunningIterator(
[$this->databaseAdminClient, 'listDatabaseOperations'],
$request,
$callOptions
);
}

Expand All @@ -2377,11 +2338,47 @@ function ($callOptions) use ($data) {
* @param string $operationName The Long Running Operation name.
* @return OperationResponse
*/
public function resumeOperation($operationName): OperationResponse
public function resumeOperation($operationName, array $options = [])
{
return new OperationResponse(
return (new OperationResponse(
$operationName,
$this->databaseAdminClient->getOperationsClient()
$this->databaseAdminClient->getOperationsClient(),
$options
))->withResultFunction($this->databaseResultFunction());
}

/**
* List long running operations.
*
* Example:
* ```
* $operations = $backup->longRunningOperations();
* ```
*
* @param array $options [optional] {
* Configuration Options.
*
* @type string $name The name of the operation collection.
* @type string $filter The standard list filter.
* @type int $pageSize Maximum number of results to return per
* request.
* @type int $resultLimit Limit the number of results returned in total.
* **Defaults to** `0` (return all results).
* @type string $pageToken A previously-returned page token used to
* resume the loading of results from a specific point.
* }
* @return PagedListResponse<OperationResponse>
*/
public function longRunningOperations(array $options = [])
{
[$data, $callOptions] = $this->splitOptionalArgs($options);
$request = $this->serializer->decodeMessage(new ListOperationsRequest(), $data);
$request->setName($this->name . '/operations');

return $this->buildLongRunningIterator(
[$this->databaseAdminClient->getOperationsClient(), 'listOperations'],
$request,
$callOptions
);
}

Expand Down Expand Up @@ -2507,10 +2504,6 @@ private function parseMutations(array $rawMutations): array

switch ($type) {
case Operation::OP_DELETE:
if (isset($data['keySet'])) {
$data['keySet'] = $this->formatKeySet($data['keySet']);
}

$operation = $this->serializer->decodeMessage(
new Delete(),
$data
Expand Down
Loading

0 comments on commit 390ba8b

Please sign in to comment.