forked from googleapis/google-cloud-php-datastore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOperation.php
721 lines (648 loc) · 24.6 KB
/
Operation.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
<?php
/**
* Copyright 2016 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace Google\Cloud\Datastore;
use Google\Cloud\Core\ValidateTrait;
use Google\Cloud\Datastore\Connection\ConnectionInterface;
use Google\Cloud\Datastore\Connection\Rest;
use Google\Cloud\Datastore\Query\QueryInterface;
use InvalidArgumentException;
/**
* Run lookups and queries and commit changes.
*
* This class is used by {@see Google\Cloud\Datastore\DatastoreClient}
* and {@see Google\Cloud\Datastore\Transaction} and is not intended to be used
* directly.
*
* Examples are omitted for brevity. Detailed usage examples can be found in
* {@see Google\Cloud\Datastore\DatastoreClient} and
* {@see Google\Cloud\Datastore\Transaction}.
*/
class Operation
{
use DatastoreTrait;
use ValidateTrait;
/**
* @var ConnectionInterface
*/
protected $connection;
/**
* @var string
*/
private $projectId;
/**
* @var string
*/
private $namespaceId;
/**
* @var string
*/
private $entityMapper;
/**
* Create an operation
*
* @param ConnectionInterface $connection A connection to Google Cloud Platform's Datastore API.
* @param string $projectId The Google Cloud Platform project ID.
* @param string $namespaceId The namespace to use for all service requests.
*/
public function __construct(
ConnectionInterface $connection,
$projectId,
$namespaceId,
EntityMapper $entityMapper
) {
$this->connection = $connection;
$this->projectId = $projectId;
$this->namespaceId = $namespaceId;
$this->entityMapper = $entityMapper;
}
/**
* Create a single Key instance
*
* @see https://cloud.google.com/datastore/reference/rest/v1/Key Key
* @see https://cloud.google.com/datastore/reference/rest/v1/Key#PathElement PathElement
*
* @param string $kind The kind.
* @param string|int $identifier [optional] The ID or name.
* @param array $options [optional] {
* Configuration Options
*
* @type string $identifierType If omitted, type will be determined
* internally. In cases where any ambiguity can be expected (i.e.
* if you want to create keys with `name` but your values may
* pass PHP's `is_numeric()` check), this value may be
* explicitly set using `Key::TYPE_ID` or `Key::TYPE_NAME`.
* }
* @return Key
*/
public function key($kind, $identifier = null, array $options = [])
{
$options += [
'namespaceId' => $this->namespaceId
];
$key = new Key($this->projectId, $options);
$key->pathElement($kind, $identifier, $options);
return $key;
}
/**
* Create multiple keys with the same configuration.
*
* When inserting multiple entities, creating a set of keys at once can be
* useful. By defining the Key's kind and any ancestors up front, and
* allowing Cloud Datastore to allocate IDs, you can be sure that your
* entity identity and ancestry are correct and that there will be no
* collisions during the insert operation.
*
* @see https://cloud.google.com/datastore/reference/rest/v1/Key Key
* @see https://cloud.google.com/datastore/reference/rest/v1/Key#PathElement PathElement
*
* @param string $kind The kind to use in the final path element.
* @param array $options [optional] {
* Configuration Options
*
* @type array[] $ancestors An array of
* [PathElement](https://cloud.google.com/datastore/reference/rest/v1/Key#PathElement) arrays. Use to
* create [ancestor paths](https://cloud.google.com/datastore/docs/concepts/entities#ancestor_paths).
* @type int $number The number of keys to generate.
* @type string|int $id The ID for the last pathElement.
* @type string $name The Name for the last pathElement.
* }
* @return Key[]
*/
public function keys($kind, array $options = [])
{
$options += [
'number' => 1,
'ancestors' => [],
'id' => null,
'name' => null
];
$path = [];
if (count($options['ancestors']) > 0) {
$path = $options['ancestors'];
}
$path[] = array_filter([
'kind' => $kind,
'id' => $options['id'],
'name' => $options['name']
]);
$key = new Key($this->projectId, [
'path' => $path,
'namespaceId' => $this->namespaceId
]);
$keys = array_fill(0, $options['number'], $key);
return $keys;
}
/**
* Create an entity
*
* This method does not execute any service requests.
*
* Entities are created with a Datastore Key, or by specifying a Kind. Kinds
* are only allowed for insert operations. For any other case, you must
* specify a named key. If a kind is given, an ID will be automatically
* allocated for the entity upon insert. Additionally, if your entity
* requires a complex key elementPath, you must create the key separately.
*
* In complex applications you may want to create your own entity types.
* Google Cloud PHP supports subclassing of {@see Google\Cloud\Datastore\Entity}.
* If the name of a subclass of Entity is given in the options array, an
* instance of the subclass will be returned instead of Entity.
*
* @see https://cloud.google.com/datastore/reference/rest/v1/Entity Entity
*
* @param Key|string $key The key used to identify the record, or a string $kind.
* @param array $entity [optional] The data to fill the entity with.
* @param array $options [optional] {
* Configuration Options
*
* @type string $className The name of a class extending {@see Google\Cloud\Datastore\Entity}.
* If provided, an instance of that class will be returned instead of Entity.
* If not set, {@see Google\Cloud\Datastore\Entity} will be used.
* @type array $excludeFromIndexes A list of entity keys to exclude from
* datastore indexes.
* }
* @return Entity
* @throws InvalidArgumentException
*/
public function entity($key, array $entity = [], array $options = [])
{
$options += [
'className' => null
];
if (!is_string($key) && !($key instanceof Key)) {
throw new InvalidArgumentException(
'$key must be an instance of Key or a string'
);
}
if (is_string($key)) {
$key = $this->key($key);
}
$className = $options['className'];
if (!is_null($className) && !is_subclass_of($className, Entity::class)) {
throw new InvalidArgumentException(sprintf(
'Given classname %s is not a subclass of Entity',
$className
));
}
if (is_null($className)) {
$className = Entity::class;
}
return new $className($key, $entity, $options);
}
/**
* Begin a Datastore Transaction.
*
* @param array $transactionOptions
* [Transaction Options](https://cloud.google.com/datastore/docs/reference/data/rest/v1/projects/beginTransaction#TransactionOptions)
* @param array $options Configuration options.
* @return string
*/
public function beginTransaction($transactionOptions, array $options = [])
{
$res = $this->connection->beginTransaction([
'projectId' => $this->projectId,
'transactionOptions' => $transactionOptions
] + $options);
return $res['transaction'];
}
/**
* Allocate available IDs to a set of keys
*
* Keys MUST be in an incomplete state (i.e. including a kind but not an ID
* or name in their final pathElement).
*
* This method will execute a service request.
*
* @see https://cloud.google.com/datastore/reference/rest/v1/projects/allocateIds allocateIds
*
* @param Key[] $keys The incomplete keys.
* @param array $options [optional] Configuration Options.
* @return Key[]
* @throws InvalidArgumentException
*/
public function allocateIds(array $keys, array $options = [])
{
// Validate the given keys. First check types, then state of each.
// The API will throw a 400 if the key is named, but it's an easy
// check we can handle before going to the API to save a request.
// @todo replace with json schema
$this->validateBatch($keys, Key::class, function ($key) {
if ($key->state() !== Key::STATE_INCOMPLETE) {
throw new InvalidArgumentException(sprintf(
'Given $key is in an invalid state. Can only allocate IDs for incomplete keys. ' .
'Given path was %s',
(string) $key
));
}
if (!isset($key->pathEnd()['kind'])) {
throw new InvalidArgumentException(sprintf(
'Cannot allocate IDs because a path element was missing a kind. ' .
'This usually means a key was created but no path elements were defined. ' .
'Given path was %s',
(string) $key
));
}
});
$serviceKeys = [];
foreach ($keys as $key) {
$serviceKeys[] = $key->keyObject();
}
$res = $this->connection->allocateIds([
'projectId' => $this->projectId,
'keys' => $serviceKeys
] + $options);
if (isset($res['keys'])) {
foreach ($res['keys'] as $index => $key) {
if (!isset($keys[$index])) {
continue;
}
$end = end($key['path']);
$id = $end['id'];
$keys[$index]->setLastElementIdentifier($id);
}
}
return $keys;
}
/**
* Lookup records by key
*
* @param Key[] $keys The identifiers to look up.
* @param array $options [optional] {
* Configuration Options
*
* @type string $readConsistency See
* [ReadConsistency](https://cloud.google.com/datastore/reference/rest/v1/ReadOptions#ReadConsistency).
* @type string $transaction The transaction ID, if the query should be
* run in a transaction.
* @type string|array $className If a string, the name of the class to return results as.
* Must be a subclass of {@see Google\Cloud\Datastore\Entity}.
* If not set, {@see Google\Cloud\Datastore\Entity} will be used.
* If an array is given, it must be an associative array, where
* the key is a Kind and the value is the name of a subclass of
* {@see Google\Cloud\Datastore\Entity}.
* @type bool $sort If set to true, results in each set will be sorted
* to match the order given in $keys. **Defaults to** `false`.
* }
* @return array Returns an array with keys [`found`, `missing`, and `deferred`].
* Members of `found` will be instance of
* {@see Google\Cloud\Datastore\Entity}. Members of `missing` and
* `deferred` will be instance of {@see Google\Cloud\Datastore\Key}.
* @throws InvalidArgumentException
*/
public function lookup(array $keys, array $options = [])
{
$options += [
'className' => null,
'sort' => false
];
$serviceKeys = [];
$this->validateBatch($keys, Key::class, function ($key) use (&$serviceKeys) {
if ($key->state() !== Key::STATE_NAMED) {
throw new InvalidArgumentException(sprintf(
'Given $key is in an invalid state. Can only lookup records when given a complete key. ' .
'Given path was %s',
(string) $key
));
}
$serviceKeys[] = $key->keyObject();
});
$res = $this->connection->lookup($options + $this->readOptions($options) + [
'projectId' => $this->projectId,
'keys' => $serviceKeys
]);
$result = [];
if (isset($res['found'])) {
foreach ($res['found'] as $found) {
$result['found'][] = $this->mapEntityResult($found, $options['className']);
}
if ($options['sort']) {
$result['found'] = $this->sortEntities($result['found'], $keys);
}
}
if (isset($res['missing'])) {
$result['missing'] = [];
foreach ($res['missing'] as $missing) {
$key = $this->key(
$missing['entity']['key']['path'],
$missing['entity']['key']['partitionId']
);
$result['missing'][] = $key;
}
}
if (isset($res['deferred'])) {
$result['deferred'] = [];
foreach ($res['deferred'] as $deferred) {
$key = $this->key(
$deferred['path'],
$deferred['partitionId']
);
$result['deferred'][] = $key;
}
}
return $result;
}
/**
* Run a query and return entities
*
* @param QueryInterface $query The query object.
* @param array $options [optional] {
* Configuration Options
*
* @type string $transaction The transaction ID, if the query should be
* run in a transaction.
* @type string $className The name of the class to return results as.
* Must be a subclass of {@see Google\Cloud\Datastore\Entity}.
* If not set, {@see Google\Cloud\Datastore\Entity} will be used.
* @type string $readConsistency See
* [ReadConsistency](https://cloud.google.com/datastore/reference/rest/v1/ReadOptions#ReadConsistency).
* }
* @return EntityIterator<Entity>
*/
public function runQuery(QueryInterface $query, array $options = [])
{
$options += [
'className' => null,
'namespaceId' => $this->namespaceId
];
$request = $options + $this->readOptions($options) + [
'projectId' => $this->projectId,
'partitionId' => $this->partitionId($this->projectId, $options['namespaceId']),
$query->queryKey() => $query->queryObject()
];
$iteratorConfig = [
'itemsKey' => 'batch.entityResults',
'resultTokenKey' => 'query.startCursor',
'nextResultTokenKey' => 'batch.endCursor',
'setNextResultTokenCondition' => function ($res) use ($query) {
if (isset($res['batch']['moreResults'])) {
return $query->canPaginate() && $res['batch']['moreResults'] === 'NOT_FINISHED';
}
return false;
}
];
return new EntityIterator(
new EntityPageIterator(
function (array $entityResult) use ($options) {
return $this->mapEntityResult($entityResult, $options['className']);
},
[$this->connection, 'runQuery'],
$request,
$iteratorConfig
)
);
}
/**
* Commit all mutations
*
* Calling this method will end the operation (and close the transaction,
* if one is specified).
*
* @codingStandardsIgnoreStart
* @param array $mutations [Mutation[]](https://cloud.google.com/datastore/docs/reference/rest/v1/projects/commit#Mutation).
* @param array $options [optional] {
* Configuration Options
*
* @type string $transaction The transaction ID, if the query should be
* run in a transaction.
* }
* @return array [Response Body](https://cloud.google.com/datastore/reference/rest/v1/projects/commit#response-body)
* @codingStandardsIgnoreEnd
*/
public function commit(array $mutations, array $options = [])
{
$options += [
'transaction' => null
];
$res = $this->connection->commit($options + [
'mode' => ($options['transaction']) ? 'TRANSACTIONAL' : 'NON_TRANSACTIONAL',
'mutations' => $mutations,
'projectId' => $this->projectId
]);
return $res;
}
/**
* Patch any incomplete keys in the given array of entities
*
* Any incomplete keys will be allocated an ID. Named keys in the input
* will remain unchanged.
*
* @param Entity[] $entities A list of entities
* @return Entity[]
*/
public function allocateIdsToEntities(array $entities)
{
$this->validateBatch($entities, Entity::class);
$incompleteKeys = [];
foreach ($entities as $entity) {
if ($entity->key()->state() === Key::STATE_INCOMPLETE) {
$incompleteKeys[] = $entity->key();
}
}
if (!empty($incompleteKeys)) {
$keys = $this->allocateIds($incompleteKeys);
}
return $entities;
}
/**
* Enqueue a mutation
*
* A mutation is a change to the datastore. Create, Update and Delete are
* examples of mutations, while Read is not.
*
* Google Cloud Datastore supports multiple mutations in a single API call,
* subject to the limits of the service. Adding mutations separately from
* committing the changes allows you to create complex operations, both
* inside a transaction and not.
*
* @see https://cloud.google.com/datastore/docs/concepts/limits Limits
*
* @param string $operation The operation to execute. "Insert", "Upsert",
* "Update" or "Delete".
* @param Entity|Key $input The entity or key to mutate.
* @param string $type The type of the input array.
* @param string $baseVersion [optional] The version of the entity that this mutation
* is being applied to. If this does not match the current version on
* the server, the mutation conflicts.
* @return array [Mutation](https://cloud.google.com/datastore/docs/reference/rest/v1/projects/commit#Mutation).
* @throws InvalidArgumentException
*/
public function mutation(
$operation,
$input,
$type,
$baseVersion = null
) {
// If the given element is an Entity, it will use that baseVersion.
if ($input instanceof Entity) {
$baseVersion = $input->baseVersion();
$data = $this->entityMapper->objectToRequest($input);
} elseif ($input instanceof Key) {
$data = $input->keyObject();
} else {
throw new InvalidArgumentException(sprintf(
'Input must be a Key or Entity, %s given',
get_class($input)
));
}
return array_filter([
$operation => $data,
'baseVersion' => $baseVersion
]);
}
/**
* Roll back a transaction
*
* @param string $transactionId The transaction to roll back
* @return void
*/
public function rollback($transactionId)
{
$this->connection->rollback([
'projectId' => $this->projectId,
'transaction' => $transactionId
]);
}
/**
* Check whether an update or upsert operation may proceed safely
*
* @param Entity[] $entities the entities to be updated or upserted.
* @param bool $allowOverwrite If `true`, entities may be overwritten.
* **Defaults to** `false`.
* @throws InvalidArgumentException
* @return void
*/
public function checkOverwrite(array $entities, $allowOverwrite = false)
{
$this->validateBatch($entities, Entity::class);
foreach ($entities as $entity) {
if (!$entity->populatedByService() && !$allowOverwrite) {
throw new InvalidArgumentException(sprintf(
'Given entity cannot be saved because it may overwrite an '.
'existing record. When updating manually created entities, '.
'please set the options `$allowOverwrite` flag to `true`. '.
'Invalid entity key was %s',
(string) $entity->key()
));
}
}
}
/**
* Convert an EntityResult into an array of entities
*
* @see https://cloud.google.com/datastore/reference/rest/v1/EntityResult EntityResult
*
* @param array $result The EntityResult from a Lookup.
* @param string|array $class If a string, the name of the class to return results as.
* Must be a subclass of {@see Google\Cloud\Datastore\Entity}.
* If not set, {@see Google\Cloud\Datastore\Entity} will be used.
* If an array is given, it must be an associative array, where
* the key is a Kind and the value is the name of a subclass of
* {@see Google\Cloud\Datastore\Entity}.
* @return Entity[]
*/
private function mapEntityResult(array $result, $class)
{
$entity = $result['entity'];
$properties = [];
$excludes = [];
$meanings = [];
if (isset($entity['properties'])) {
$res = $this->entityMapper->responseToEntityProperties($entity['properties']);
$properties = $res['properties'];
$excludes = $res['excludes'];
$meanings = $res['meanings'];
}
$namespaceId = (isset($entity['key']['partitionId']['namespaceId']))
? $entity['key']['partitionId']['namespaceId']
: null;
$key = new Key($this->projectId, [
'path' => $entity['key']['path'],
'namespaceId' => $namespaceId
]);
if (is_array($class)) {
$lastPathElement = $key->pathEnd();
if (!array_key_exists($lastPathElement['kind'], $class)) {
throw new InvalidArgumentException(sprintf(
'No class found for kind %s',
$lastPathElement['kind']
));
}
$className = $class[$lastPathElement['kind']];
} else {
$className = $class;
}
return $this->entity($key, $properties, [
'cursor' => (isset($result['cursor']))
? $result['cursor']
: null,
'baseVersion' => (isset($result['version']))
? $result['version']
: null,
'className' => $className,
'populatedByService' => true,
'excludeFromIndexes' => $excludes,
'meanings' => $meanings
]);
}
/**
* Format the readOptions
*
* @param array $options [optional] {
* Read Options
*
* @type string $transaction If set, query or lookup will run in transaction.
* @type string $readConsistency See
* [ReadConsistency](https://cloud.google.com/datastore/reference/rest/v1/ReadOptions#ReadConsistency).
* }
* @return array
*/
private function readOptions(array $options = [])
{
$options += [
'readConsistency' => null,
'transaction' => null
];
$readOptions = array_filter([
'readConsistency' => $options['readConsistency'],
'transaction' => $options['transaction']
]);
return array_filter([
'readOptions' => $readOptions
]);
}
/**
* Sort entities into the order given in $keys.
*
* @param Entity[] $entities
* @param Key[] $keys
* @return Entity[]
*/
private function sortEntities(array $entities, array $keys)
{
$map = [];
foreach ($entities as $entity) {
$map[(string) $entity->key()] = $entity;
}
$ret = [];
foreach ($keys as $key) {
if (isset($map[(string) $key])) {
$ret[] = $map[(string) $key];
}
}
return $ret;
}
}