forked from googleapis/google-cloud-php-datastore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Transaction.php
357 lines (338 loc) · 11.3 KB
/
Transaction.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
<?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;
/**
* Represents a Transaction
*
* A transaction is a set of Datastore operations on one or more entities. Each
* transaction is guaranteed to be atomic, which means that transactions are
* never partially applied. Either all of the operations in the transaction are
* applied, or none of them are applied.
*
* It is highly recommended that users read and understand the underlying
* concepts in [Transactions](https://cloud.google.com/datastore/docs/concepts/transactions)
* before beginning.
*
* Mutations (i.e. insert, update and delete) are not executed immediately.
* Calls to those methods (and their batch equivalents) will enqueue a new
* mutation. Calling {@see Google\Cloud\Datastore\Transaction::commit()} will
* execute all the mutationsin the order they were enqueued, and end the
* transaction.
*
* Lookups and queries can be run in a transaction, so long as they are run
* prior to committing or rolling back the transaction.
*
* Transactions are an **optional** feature of Google Cloud Datastore. Queries,
* lookups and mutations can be executed outside of a Transaction from
* {@see Google\Cloud\Datastore\DatastoreClient}.
*
* Example:
* ```
* use Google\Cloud\Datastore\DatastoreClient;
*
* $datastore = new DatastoreClient();
*
* $transaction = $datastore->transaction();
* ```
*
* @see https://cloud.google.com/datastore/docs/concepts/transactions Transactions
*/
class Transaction
{
use TransactionTrait;
/**
* @var array
*/
private $mutations = [];
/**
* Insert an entity.
*
* Changes are not immediately committed to Cloud Datastore when calling
* this method. Use {@see Google\Cloud\Datastore\Transaction::commit()} to
* commit changes and end the transaction.
*
* If entities with incomplete keys are provided, this method will immediately
* trigger a service call to allocate IDs to the entities.
*
* Example:
* ```
* $key = $datastore->key('Person', 'Bob');
* $entity = $datastore->entity($key, ['firstName' => 'Bob']);
*
* $transaction->insert($entity);
* $transaction->commit();
* ```
*
* @param Entity $entity The entity to insert.
* @return Transaction
*/
public function insert(Entity $entity)
{
return $this->insertBatch([$entity]);
}
/**
* Insert multiple entities.
*
* Changes are not immediately committed to Cloud Datastore when calling
* this method. Use {@see Google\Cloud\Datastore\Transaction::commit()} to
* commit changes and end the transaction.
*
* If entities with incomplete keys are provided, this method will immediately
* trigger a service call to allocate IDs to the entities.
*
* Example:
* ```
* $entities = [
* $datastore->entity('Person', ['firstName' => 'Bob']),
* $datastore->entity('Person', ['firstName' => 'John'])
* ];
*
* $transaction->insertBatch($entities);
* $transaction->commit();
* ```
*
* @param Entity[] $entities The entities to insert.
* @return Transaction
*/
public function insertBatch(array $entities)
{
$entities = $this->operation->allocateIdsToEntities($entities);
foreach ($entities as $entity) {
$this->mutations[] = $this->operation->mutation('insert', $entity, Entity::class);
}
return $this;
}
/**
* Update an entity.
*
* Changes are not immediately committed to Cloud Datastore when calling
* this method. Use {@see Google\Cloud\Datastore\Transaction::commit()} to
* commit changes and end the transaction.
*
* Example:
* ```
* $entity['firstName'] = 'Bob';
*
* $transaction->update($entity);
* $transaction->commit();
* ```
*
* @param Entity $entity The entity to update.
* @param array $options [optional] {
* Configuration Options
*
* @type bool $allowOverwrite Entities must be updated as an entire
* resource. Patch operations are not supported. Because entities
* can be created manually, or obtained by a lookup or query, it
* is possible to accidentally overwrite an existing record with a
* new one when manually creating an entity. To provide additional
* safety, this flag must be set to `true` in order to update a
* record when the entity provided was not obtained through a
* lookup or query. **Defaults to** `false`.
* }
* @return Transaction
*/
public function update(Entity $entity, array $options = [])
{
$options += [
'allowOverwrite' => false
];
return $this->updateBatch([$entity], $options);
}
/**
* Update multiple entities.
*
* Changes are not immediately committed to Cloud Datastore when calling
* this method. Use {@see Google\Cloud\Datastore\Transaction::commit()} to
* commit changes and end the transaction.
*
* Example:
* ```
* $entities[0]['firstName'] = 'Bob';
* $entities[1]['firstName'] = 'John';
*
* $transaction->updateBatch($entities);
* $transaction->commit();
* ```
*
* @param Entity[] $entities The entities to update.
* @param array $options [optional] {
* Configuration Options
*
* @type bool $allowOverwrite Entities must be updated as an entire
* resource. Patch operations are not supported. Because entities
* can be created manually, or obtained by a lookup or query, it
* is possible to accidentally overwrite an existing record with a
* new one when manually creating an entity. To provide additional
* safety, this flag must be set to `true` in order to update a
* record when the entity provided was not obtained through a
* lookup or query. **Defaults to** `false`.
* }
* @return Transaction
*/
public function updateBatch(array $entities, array $options = [])
{
$options += [
'allowOverwrite' => false
];
$this->operation->checkOverwrite($entities, $options['allowOverwrite']);
foreach ($entities as $entity) {
$this->mutations[] = $this->operation->mutation('update', $entity, Entity::class);
}
return $this;
}
/**
* Upsert an entity.
*
* Changes are not immediately committed to Cloud Datastore when calling
* this method. Use {@see Google\Cloud\Datastore\Transaction::commit()} to
* commit changes and end the transaction.
*
* Upsert will create a record if one does not already exist, or overwrite
* existing record if one already exists.
*
* If entities with incomplete keys are provided, this method will immediately
* trigger a service call to allocate IDs to the entities.
*
* Example:
* ```
* $key = $datastore->key('Person', 'Bob');
* $entity = $datastore->entity($key, ['firstName' => 'Bob']);
*
* $transaction->upsert($entity);
* $transaction->commit();
* ```
*
* @param Entity $entity The entity to upsert.
* @return Transaction
*/
public function upsert(Entity $entity)
{
return $this->upsertBatch([$entity]);
}
/**
* Upsert multiple entities.
*
* Changes are not immediately committed to Cloud Datastore when calling
* this method. Use {@see Google\Cloud\Datastore\Transaction::commit()} to
* commit changes and end the transaction.
*
* Upsert will create a record if one does not already exist, or overwrite
* existing record if one already exists.
*
* If entities with incomplete keys are provided, this method will immediately
* trigger a service call to allocate IDs to the entities.
*
* Example:
* ```
* $keys = [
* $datastore->key('Person', 'Bob'),
* $datastore->key('Person', 'John')
* ];
*
* $entities = [
* $datastore->entity($keys[0], ['firstName' => 'Bob']),
* $datastore->entity($keys[1], ['firstName' => 'John'])
* ];
*
* $transaction->upsertBatch($entities);
* $transaction->commit();
* ```
*
* @param Entity[] $entities The entities to upsert.
* @return Transaction
*/
public function upsertBatch(array $entities)
{
$entities = $this->operation->allocateIdsToEntities($entities);
foreach ($entities as $entity) {
$this->mutations[] = $this->operation->mutation('upsert', $entity, Entity::class);
}
return $this;
}
/**
* Delete a record.
*
* Changes are not immediately committed to Cloud Datastore when calling
* this method. Use {@see Google\Cloud\Datastore\Transaction::commit()} to
* commit changes and end the transaction.
*
* Example:
* ```
* $key = $datastore->key('Person', 'Bob');
*
* $transaction->delete($key);
* $transaction->commit();
* ```
*
* @param Key $key The key to delete
* @return Transaction
*/
public function delete(Key $key)
{
return $this->deleteBatch([$key]);
}
/**
* Delete multiple records.
*
* Changes are not immediately committed to Cloud Datastore when calling
* this method. Use {@see Google\Cloud\Datastore\Transaction::commit()} to
* commit changes and end the transaction.
*
* Example:
* ```
* $keys = [
* $datastore->key('Person', 'Bob'),
* $datastore->key('Person', 'John')
* ];
*
* $transaction->deleteBatch($keys);
* $transaction->commit();
* ```
*
* @param Key[] $keys The keys to delete.
* @return Transaction
*/
public function deleteBatch(array $keys)
{
foreach ($keys as $key) {
$this->mutations[] = $this->operation->mutation('delete', $key, Key::class);
}
return $this;
}
/**
* Commit all mutations.
*
* Calling this method will end the operation (and close the transaction,
* if one is specified).
*
* Example:
* ```
* $transaction->commit();
* ```
*
* @see https://cloud.google.com/datastore/docs/reference/rest/v1/projects/commit Commit API documentation
*
* @param array $options [optional] Configuration Options.
* @return array [Response Body](https://cloud.google.com/datastore/reference/rest/v1/projects/commit#response-body)
*/
public function commit(array $options = [])
{
$options['transaction'] = $this->transactionId;
return $this->operation->commit($this->mutations, $options);
}
}