google-cloud-datastore 1.2.1 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (30) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE +2 -2
  3. data/lib/google-cloud-datastore.rb +2 -2
  4. data/lib/google/cloud/datastore.rb +26 -6
  5. data/lib/google/cloud/datastore/commit.rb +2 -2
  6. data/lib/google/cloud/datastore/convert.rb +2 -2
  7. data/lib/google/cloud/datastore/credentials.rb +2 -2
  8. data/lib/google/cloud/datastore/cursor.rb +2 -2
  9. data/lib/google/cloud/datastore/dataset.rb +109 -4
  10. data/lib/google/cloud/datastore/dataset/lookup_results.rb +2 -2
  11. data/lib/google/cloud/datastore/dataset/query_results.rb +2 -2
  12. data/lib/google/cloud/datastore/entity.rb +2 -2
  13. data/lib/google/cloud/datastore/errors.rb +2 -2
  14. data/lib/google/cloud/datastore/gql_query.rb +2 -2
  15. data/lib/google/cloud/datastore/key.rb +2 -2
  16. data/lib/google/cloud/datastore/properties.rb +2 -2
  17. data/lib/google/cloud/datastore/query.rb +2 -2
  18. data/lib/google/cloud/datastore/read_only_transaction.rb +249 -0
  19. data/lib/google/cloud/datastore/service.rb +20 -4
  20. data/lib/google/cloud/datastore/transaction.rb +9 -18
  21. data/lib/google/cloud/datastore/v1.rb +2 -2
  22. data/lib/google/cloud/datastore/v1/datastore_client.rb +2 -2
  23. data/lib/google/cloud/datastore/v1/doc/google/datastore/v1/datastore.rb +2 -2
  24. data/lib/google/cloud/datastore/v1/doc/google/datastore/v1/entity.rb +2 -2
  25. data/lib/google/cloud/datastore/v1/doc/google/datastore/v1/query.rb +2 -2
  26. data/lib/google/cloud/datastore/v1/doc/google/protobuf/wrappers.rb +2 -2
  27. data/lib/google/cloud/datastore/v1/doc/overview.rb +2 -2
  28. data/lib/google/cloud/datastore/version.rb +3 -3
  29. data/lib/google/datastore/v1/datastore_services_pb.rb +1 -1
  30. metadata +6 -5
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7452a328fd9cb6018d65778f74c89de342ad6c54990da49b9978d7a8b53e271e
4
- data.tar.gz: 14f3ab5913d8923dfef6f0c37eeba948859fe851536a0f65807846a0cc91c884
3
+ metadata.gz: ee669c1788eabbeab4c54a4e3c70ef523eaf29ea39d67fff4b8907c128c06a86
4
+ data.tar.gz: e3ed585c57076f65a53901f1a02027ddeda91e54ec7a0526e85c1e74f15c63aa
5
5
  SHA512:
6
- metadata.gz: f391045ca39e4899d30254bb7c441344da56a01840ce6fb979025442ebad5cae2f646a37ab7fd7da994966f29f6d961e6f8921c1553b4df6149398fe0793983a
7
- data.tar.gz: '012294aba883d1292fd1948ff38f1d50ac872046f6083ddc6b120ae3991b5e6fb973062b8b5c99a4c0864a22e5c3ac1444878a21987537c6a3f1ad6a8f29ab6f'
6
+ metadata.gz: 4cb9965dda5f5255a7cafa68516a3a9dbbdd03c0ea00ce2a64d22acfcbfd2bca476adf03871c56508ace442d7fb0c58013ad35b2f3d02895f071cdc5fe9be030
7
+ data.tar.gz: ce4faf4d6a03e3cd9f952689a78b7a440076ec82fdbb1d1cb09278f8c2039fb5b56bbf92e9785e08599abaf1790c994700d52555808a9579c44447bd797c7276
data/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  Apache License
2
2
  Version 2.0, January 2004
3
- http://www.apache.org/licenses/
3
+ https://www.apache.org/licenses/
4
4
 
5
5
  TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
6
 
@@ -192,7 +192,7 @@
192
192
  you may not use this file except in compliance with the License.
193
193
  You may obtain a copy of the License at
194
194
 
195
- http://www.apache.org/licenses/LICENSE-2.0
195
+ https://www.apache.org/licenses/LICENSE-2.0
196
196
 
197
197
  Unless required by applicable law or agreed to in writing, software
198
198
  distributed under the License is distributed on an "AS IS" BASIS,
@@ -1,10 +1,10 @@
1
- # Copyright 2016 Google Inc. All rights reserved.
1
+ # Copyright 2016 Google LLC
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
5
5
  # You may obtain a copy of the License at
6
6
  #
7
- # http://www.apache.org/licenses/LICENSE-2.0
7
+ # https://www.apache.org/licenses/LICENSE-2.0
8
8
  #
9
9
  # Unless required by applicable law or agreed to in writing, software
10
10
  # distributed under the License is distributed on an "AS IS" BASIS,
@@ -1,10 +1,10 @@
1
- # Copyright 2014 Google Inc. All rights reserved.
1
+ # Copyright 2014 Google LLC
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
5
5
  # You may obtain a copy of the License at
6
6
  #
7
- # http://www.apache.org/licenses/LICENSE-2.0
7
+ # https://www.apache.org/licenses/LICENSE-2.0
8
8
  #
9
9
  # Unless required by applicable law or agreed to in writing, software
10
10
  # distributed under the License is distributed on an "AS IS" BASIS,
@@ -369,6 +369,30 @@ module Google
369
369
  # end
370
370
  # ```
371
371
  #
372
+ # A read-only transaction cannot modify entities; in return they do not
373
+ # contend with other read-write or read-only transactions. Using a read-only
374
+ # transaction for transactions that only read data will potentially improve
375
+ # throughput.
376
+ #
377
+ # ```ruby
378
+ # require "google/cloud/datastore"
379
+ #
380
+ # datastore = Google::Cloud::Datastore.new
381
+ #
382
+ # task_list_key = datastore.key "TaskList", "default"
383
+ # query = datastore.query("Task").
384
+ # ancestor(task_list_key)
385
+ #
386
+ # tasks = nil
387
+ #
388
+ # datastore.transaction read_only: true do |tx|
389
+ # task_list = tx.find task_list_key
390
+ # if task_list
391
+ # tasks = tx.run query
392
+ # end
393
+ # end
394
+ # ```
395
+ #
372
396
  # See {Google::Cloud::Datastore::Transaction} and
373
397
  # {Google::Cloud::Datastore::Dataset#transaction}
374
398
  #
@@ -537,8 +561,6 @@ module Google
537
561
  # ```
538
562
  #
539
563
  module Datastore
540
- # rubocop:disable MethodLength
541
-
542
564
  ##
543
565
  # Creates a new object for connecting to the Datastore service.
544
566
  # Each call creates a new connection.
@@ -614,8 +636,6 @@ module Google
614
636
  project_id, credentials,
615
637
  timeout: timeout, client_config: client_config))
616
638
  end
617
-
618
- # rubocop:enable MethodLength
619
639
  end
620
640
  end
621
641
  end
@@ -1,10 +1,10 @@
1
- # Copyright 2016 Google Inc. All rights reserved.
1
+ # Copyright 2016 Google LLC
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
5
5
  # You may obtain a copy of the License at
6
6
  #
7
- # http://www.apache.org/licenses/LICENSE-2.0
7
+ # https://www.apache.org/licenses/LICENSE-2.0
8
8
  #
9
9
  # Unless required by applicable law or agreed to in writing, software
10
10
  # distributed under the License is distributed on an "AS IS" BASIS,
@@ -1,10 +1,10 @@
1
- # Copyright 2017 Google Inc. All rights reserved.
1
+ # Copyright 2017 Google LLC
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
5
5
  # You may obtain a copy of the License at
6
6
  #
7
- # http://www.apache.org/licenses/LICENSE-2.0
7
+ # https://www.apache.org/licenses/LICENSE-2.0
8
8
  #
9
9
  # Unless required by applicable law or agreed to in writing, software
10
10
  # distributed under the License is distributed on an "AS IS" BASIS,
@@ -1,10 +1,10 @@
1
- # Copyright 2014 Google Inc. All rights reserved.
1
+ # Copyright 2014 Google LLC
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
5
5
  # You may obtain a copy of the License at
6
6
  #
7
- # http://www.apache.org/licenses/LICENSE-2.0
7
+ # https://www.apache.org/licenses/LICENSE-2.0
8
8
  #
9
9
  # Unless required by applicable law or agreed to in writing, software
10
10
  # distributed under the License is distributed on an "AS IS" BASIS,
@@ -1,10 +1,10 @@
1
- # Copyright 2016 Google Inc. All rights reserved.
1
+ # Copyright 2016 Google LLC
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
5
5
  # You may obtain a copy of the License at
6
6
  #
7
- # http://www.apache.org/licenses/LICENSE-2.0
7
+ # https://www.apache.org/licenses/LICENSE-2.0
8
8
  #
9
9
  # Unless required by applicable law or agreed to in writing, software
10
10
  # distributed under the License is distributed on an "AS IS" BASIS,
@@ -1,10 +1,10 @@
1
- # Copyright 2014 Google Inc. All rights reserved.
1
+ # Copyright 2014 Google LLC
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
5
5
  # You may obtain a copy of the License at
6
6
  #
7
- # http://www.apache.org/licenses/LICENSE-2.0
7
+ # https://www.apache.org/licenses/LICENSE-2.0
8
8
  #
9
9
  # Unless required by applicable law or agreed to in writing, software
10
10
  # distributed under the License is distributed on an "AS IS" BASIS,
@@ -25,6 +25,8 @@ require "google/cloud/datastore/gql_query"
25
25
  require "google/cloud/datastore/cursor"
26
26
  require "google/cloud/datastore/dataset/lookup_results"
27
27
  require "google/cloud/datastore/dataset/query_results"
28
+ require "google/cloud/datastore/transaction"
29
+ require "google/cloud/datastore/read_only_transaction"
28
30
 
29
31
  module Google
30
32
  module Cloud
@@ -466,6 +468,30 @@ module Google
466
468
  ##
467
469
  # Creates a Datastore Transaction.
468
470
  #
471
+ # Transactions using the block syntax are committed upon block
472
+ # completion and are automatically retried when known errors are raised
473
+ # during commit. All other errors will be passed on.
474
+ #
475
+ # All changes are accumulated in memory until the block completes.
476
+ # Transactions will be automatically retried when possible, until
477
+ # `deadline` is reached. This operation makes separate API requests to
478
+ # begin and commit the transaction.
479
+ #
480
+ # @see https://cloud.google.com/datastore/docs/concepts/transactions
481
+ # Transactions
482
+ #
483
+ # @param [Numeric] deadline The total amount of time in seconds the
484
+ # transaction has to succeed. The default is `60`.
485
+ # @param [String] previous_transaction The transaction identifier of a
486
+ # transaction that is being retried. Read-write transactions may fail
487
+ # due to contention. A read-write transaction can be retried by
488
+ # specifying `previous_transaction` when creating the new transaction.
489
+ #
490
+ # Specifying `previous_transaction` provides information that can be
491
+ # used to improve throughput. In particular, if transactional
492
+ # operations A and B conflict, specifying the `previous_transaction`
493
+ # can help to prevent livelock. (See {Transaction#id})
494
+ #
469
495
  # @yield [tx] a block yielding a new transaction
470
496
  # @yieldparam [Transaction] tx the transaction object
471
497
  #
@@ -509,8 +535,80 @@ module Google
509
535
  # tx.rollback
510
536
  # end
511
537
  #
512
- def transaction
513
- tx = Transaction.new service
538
+ def transaction deadline: nil, previous_transaction: nil
539
+ deadline = validate_deadline deadline
540
+ backoff = 1.0
541
+ start_time = Time.now
542
+
543
+ tx = Transaction.new \
544
+ service, previous_transaction: previous_transaction
545
+ return tx unless block_given?
546
+
547
+ begin
548
+ yield tx
549
+ tx.commit
550
+ rescue Google::Cloud::UnavailableError => err
551
+ # Re-raise if deadline has passed
552
+ raise err if Time.now - start_time > deadline
553
+
554
+ # Sleep with incremental backoff
555
+ sleep(backoff *= 1.3)
556
+
557
+ # Create new transaction and retry the block
558
+ tx = Transaction.new service, previous_transaction: tx.id
559
+ retry
560
+ rescue
561
+ begin
562
+ tx.rollback
563
+ rescue
564
+ raise TransactionError,
565
+ "Transaction failed to commit and rollback."
566
+ end
567
+ raise TransactionError, "Transaction failed to commit."
568
+ end
569
+ end
570
+
571
+ ##
572
+ # Creates a read-only transaction that provides a consistent snapshot of
573
+ # Cloud Datastore. This can be useful when multiple reads are needed to
574
+ # render a page or export data that must be consistent.
575
+ #
576
+ # A read-only transaction cannot modify entities; in return they do not
577
+ # contend with other read-write or read-only transactions. Using a
578
+ # read-only transaction for transactions that only read data will
579
+ # potentially improve throughput.
580
+ #
581
+ # Read-only single-group transactions never fail due to concurrent
582
+ # modifications, so you don't have to implement retries upon failure.
583
+ # However, multi-entity-group transactions can fail due to concurrent
584
+ # modifications, so these should have retries.
585
+ #
586
+ # @see https://cloud.google.com/datastore/docs/concepts/transactions
587
+ # Transactions
588
+ #
589
+ # @yield [tx] a block yielding a new transaction
590
+ # @yieldparam [ReadOnlyTransaction] tx the transaction object
591
+ #
592
+ # @example
593
+ # require "google/cloud/datastore"
594
+ #
595
+ # datastore = Google::Cloud::Datastore.new
596
+ #
597
+ # task_list_key = datastore.key "TaskList", "default"
598
+ # query = datastore.query("Task").
599
+ # ancestor(task_list_key)
600
+ #
601
+ # tasks = nil
602
+ #
603
+ # datastore.read_only_transaction do |tx|
604
+ # task_list = tx.find task_list_key
605
+ # if task_list
606
+ # tasks = tx.run query
607
+ # end
608
+ # end
609
+ #
610
+ def read_only_transaction
611
+ tx = ReadOnlyTransaction.new service
514
612
  return tx unless block_given?
515
613
 
516
614
  begin
@@ -526,6 +624,7 @@ module Google
526
624
  raise TransactionError, "Transaction failed to commit."
527
625
  end
528
626
  end
627
+ alias_method :snapshot, :read_only_transaction
529
628
 
530
629
  ##
531
630
  # Create a new Query instance. This is a convenience method to make the
@@ -789,6 +888,12 @@ module Google
789
888
  fail "Must have active connection to service" unless service
790
889
  end
791
890
 
891
+ def validate_deadline deadline
892
+ return 60 unless deadline.is_a? Numeric
893
+ return 60 if deadline < 0
894
+ deadline
895
+ end
896
+
792
897
  def check_consistency! consistency
793
898
  fail(ArgumentError,
794
899
  format("Consistency must be :eventual or :strong, not %s.",
@@ -1,10 +1,10 @@
1
- # Copyright 2014 Google Inc. All rights reserved.
1
+ # Copyright 2014 Google LLC
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
5
5
  # You may obtain a copy of the License at
6
6
  #
7
- # http://www.apache.org/licenses/LICENSE-2.0
7
+ # https://www.apache.org/licenses/LICENSE-2.0
8
8
  #
9
9
  # Unless required by applicable law or agreed to in writing, software
10
10
  # distributed under the License is distributed on an "AS IS" BASIS,
@@ -1,10 +1,10 @@
1
- # Copyright 2014 Google Inc. All rights reserved.
1
+ # Copyright 2014 Google LLC
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
5
5
  # You may obtain a copy of the License at
6
6
  #
7
- # http://www.apache.org/licenses/LICENSE-2.0
7
+ # https://www.apache.org/licenses/LICENSE-2.0
8
8
  #
9
9
  # Unless required by applicable law or agreed to in writing, software
10
10
  # distributed under the License is distributed on an "AS IS" BASIS,
@@ -1,10 +1,10 @@
1
- # Copyright 2014 Google Inc. All rights reserved.
1
+ # Copyright 2014 Google LLC
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
5
5
  # You may obtain a copy of the License at
6
6
  #
7
- # http://www.apache.org/licenses/LICENSE-2.0
7
+ # https://www.apache.org/licenses/LICENSE-2.0
8
8
  #
9
9
  # Unless required by applicable law or agreed to in writing, software
10
10
  # distributed under the License is distributed on an "AS IS" BASIS,
@@ -1,10 +1,10 @@
1
- # Copyright 2014 Google Inc. All rights reserved.
1
+ # Copyright 2014 Google LLC
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
5
5
  # You may obtain a copy of the License at
6
6
  #
7
- # http://www.apache.org/licenses/LICENSE-2.0
7
+ # https://www.apache.org/licenses/LICENSE-2.0
8
8
  #
9
9
  # Unless required by applicable law or agreed to in writing, software
10
10
  # distributed under the License is distributed on an "AS IS" BASIS,
@@ -1,10 +1,10 @@
1
- # Copyright 2016 Google Inc. All rights reserved.
1
+ # Copyright 2016 Google LLC
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
5
5
  # You may obtain a copy of the License at
6
6
  #
7
- # http://www.apache.org/licenses/LICENSE-2.0
7
+ # https://www.apache.org/licenses/LICENSE-2.0
8
8
  #
9
9
  # Unless required by applicable law or agreed to in writing, software
10
10
  # distributed under the License is distributed on an "AS IS" BASIS,
@@ -1,10 +1,10 @@
1
- # Copyright 2014 Google Inc. All rights reserved.
1
+ # Copyright 2014 Google LLC
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
5
5
  # You may obtain a copy of the License at
6
6
  #
7
- # http://www.apache.org/licenses/LICENSE-2.0
7
+ # https://www.apache.org/licenses/LICENSE-2.0
8
8
  #
9
9
  # Unless required by applicable law or agreed to in writing, software
10
10
  # distributed under the License is distributed on an "AS IS" BASIS,
@@ -1,10 +1,10 @@
1
- # Copyright 2015 Google Inc. All rights reserved.
1
+ # Copyright 2015 Google LLC
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
5
5
  # You may obtain a copy of the License at
6
6
  #
7
- # http://www.apache.org/licenses/LICENSE-2.0
7
+ # https://www.apache.org/licenses/LICENSE-2.0
8
8
  #
9
9
  # Unless required by applicable law or agreed to in writing, software
10
10
  # distributed under the License is distributed on an "AS IS" BASIS,
@@ -1,10 +1,10 @@
1
- # Copyright 2014 Google Inc. All rights reserved.
1
+ # Copyright 2014 Google LLC
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
5
5
  # You may obtain a copy of the License at
6
6
  #
7
- # http://www.apache.org/licenses/LICENSE-2.0
7
+ # https://www.apache.org/licenses/LICENSE-2.0
8
8
  #
9
9
  # Unless required by applicable law or agreed to in writing, software
10
10
  # distributed under the License is distributed on an "AS IS" BASIS,
@@ -0,0 +1,249 @@
1
+ # Copyright 2014 Google LLC
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # https://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+
16
+ require "google/cloud/datastore/dataset/lookup_results"
17
+ require "google/cloud/datastore/dataset/query_results"
18
+
19
+ module Google
20
+ module Cloud
21
+ module Datastore
22
+ ##
23
+ # # ReadOnlyTransaction
24
+ #
25
+ # Represents a read-only Datastore transaction that only allows reads.
26
+ #
27
+ # A read-only transaction cannot modify entities; in return they do not
28
+ # contend with other read-write or read-only transactions. Using a
29
+ # read-only transaction for transactions that only read data will
30
+ # potentially improve throughput.
31
+ #
32
+ # See {Google::Cloud::Datastore::Dataset#transaction}
33
+ #
34
+ # @see https://cloud.google.com/datastore/docs/concepts/transactions
35
+ # Transactions
36
+ #
37
+ # @example
38
+ # require "google/cloud/datastore"
39
+ #
40
+ # datastore = Google::Cloud::Datastore.new
41
+ #
42
+ # task_list_key = datastore.key "TaskList", "default"
43
+ # query = datastore.query("Task").
44
+ # ancestor(task_list_key)
45
+ #
46
+ # tasks = nil
47
+ #
48
+ # datastore.read_only_transaction do |tx|
49
+ # task_list = tx.find task_list_key
50
+ # if task_list
51
+ # tasks = tx.run query
52
+ # end
53
+ # end
54
+ #
55
+ class ReadOnlyTransaction
56
+ attr_reader :id
57
+
58
+ ##
59
+ # @private The Service object.
60
+ attr_accessor :service
61
+
62
+ ##
63
+ # @private Creates a new ReadOnlyTransaction instance.
64
+ # Takes a Service instead of project and Credentials.
65
+ #
66
+ def initialize service
67
+ @service = service
68
+ reset!
69
+ start
70
+ end
71
+
72
+ ##
73
+ # Retrieve an entity by providing key information. The lookup is run
74
+ # within the transaction.
75
+ #
76
+ # @param [Key, String] key_or_kind A Key object or `kind` string value.
77
+ #
78
+ # @return [Google::Cloud::Datastore::Entity, nil]
79
+ #
80
+ # @example
81
+ # require "google/cloud/datastore"
82
+ #
83
+ # datastore = Google::Cloud::Datastore.new
84
+ #
85
+ # task_list_key = datastore.key "TaskList", "default"
86
+ #
87
+ # datastore.read_only_transaction do |tx|
88
+ # task_list = tx.find task_list_key
89
+ # end
90
+ #
91
+ def find key_or_kind, id_or_name = nil
92
+ key = key_or_kind
93
+ unless key.is_a? Google::Cloud::Datastore::Key
94
+ key = Key.new key_or_kind, id_or_name
95
+ end
96
+ find_all(key).first
97
+ end
98
+ alias_method :get, :find
99
+
100
+ ##
101
+ # Retrieve the entities for the provided keys. The lookup is run within
102
+ # the transaction.
103
+ #
104
+ # @param [Key] keys One or more Key objects to find records for.
105
+ #
106
+ # @return [Google::Cloud::Datastore::Dataset::LookupResults]
107
+ #
108
+ # @example
109
+ # require "google/cloud/datastore"
110
+ #
111
+ # datastore = Google::Cloud::Datastore.new
112
+ #
113
+ # task_key1 = datastore.key "Task", 123456
114
+ # task_key2 = datastore.key "Task", 987654
115
+ #
116
+ # datastore.read_only_transaction do |tx|
117
+ # tasks = tx.find_all task_key1, task_key2
118
+ # end
119
+ #
120
+ def find_all *keys
121
+ ensure_service!
122
+ lookup_res = service.lookup(*Array(keys).flatten.map(&:to_grpc),
123
+ transaction: @id)
124
+ Dataset::LookupResults.from_grpc lookup_res, service, nil, @id
125
+ end
126
+ alias_method :lookup, :find_all
127
+
128
+ ##
129
+ # Retrieve entities specified by a Query. The query is run within the
130
+ # transaction.
131
+ #
132
+ # @param [Query] query The Query object with the search criteria.
133
+ # @param [String] namespace The namespace the query is to run within.
134
+ #
135
+ # @return [Google::Cloud::Datastore::Dataset::QueryResults]
136
+ #
137
+ # @example
138
+ # require "google/cloud/datastore"
139
+ #
140
+ # datastore = Google::Cloud::Datastore.new
141
+ #
142
+ # query = datastore.query("Task").
143
+ # where("done", "=", false)
144
+ # datastore.read_only_transaction do |tx|
145
+ # tasks = tx.run query
146
+ # end
147
+ #
148
+ def run query, namespace: nil
149
+ ensure_service!
150
+ unless query.is_a?(Query) || query.is_a?(GqlQuery)
151
+ fail ArgumentError, "Cannot run a #{query.class} object."
152
+ end
153
+ query_res = service.run_query query.to_grpc, namespace,
154
+ transaction: @id
155
+ Dataset::QueryResults.from_grpc query_res, service, namespace,
156
+ query.to_grpc.dup
157
+ end
158
+ alias_method :run_query, :run
159
+
160
+ ##
161
+ # Begins a transaction.
162
+ # This method is run when a new ReadOnlyTransaction is created.
163
+ #
164
+ def start
165
+ fail TransactionError, "Transaction already opened." unless @id.nil?
166
+
167
+ ensure_service!
168
+ tx_res = service.begin_transaction read_only: true
169
+ @id = tx_res.transaction
170
+ end
171
+ alias_method :begin_transaction, :start
172
+
173
+ ##
174
+ # Commits the transaction.
175
+ #
176
+ # @example
177
+ # require "google/cloud/datastore"
178
+ #
179
+ # datastore = Google::Cloud::Datastore.new
180
+ #
181
+ # task_list_key = datastore.key "TaskList", "default"
182
+ # query = datastore.query("Task").
183
+ # ancestor(task_list_key)
184
+ #
185
+ # tx = datastore.transaction
186
+ # task_list = tx.find task_list_key
187
+ # if task_list
188
+ # tasks = tx.run query
189
+ # end
190
+ # tx.commit
191
+ #
192
+ def commit
193
+ fail TransactionError,
194
+ "Cannot commit when not in a transaction." if @id.nil?
195
+
196
+ ensure_service!
197
+
198
+ service.commit [], transaction: @id
199
+ true
200
+ end
201
+
202
+ ##
203
+ # Rolls back the transaction.
204
+ #
205
+ # @example
206
+ # require "google/cloud/datastore"
207
+ #
208
+ # datastore = Google::Cloud::Datastore.new
209
+ #
210
+ # task_list_key = datastore.key "TaskList", "default"
211
+ # query = datastore.query("Task").
212
+ # ancestor(task_list_key)
213
+ #
214
+ # tx = datastore.transaction
215
+ # task_list = tx.find task_list_key
216
+ # if task_list
217
+ # tasks = tx.run query
218
+ # end
219
+ # tx.rollback
220
+ #
221
+ def rollback
222
+ if @id.nil?
223
+ fail TransactionError, "Cannot rollback when not in a transaction."
224
+ end
225
+
226
+ ensure_service!
227
+ service.rollback @id
228
+ true
229
+ end
230
+
231
+ ##
232
+ # Reset the transaction.
233
+ # {ReadOnlyTransaction#start} must be called afterwards.
234
+ def reset!
235
+ @id = nil
236
+ end
237
+
238
+ protected
239
+
240
+ ##
241
+ # @private Raise an error unless an active connection to the service is
242
+ # available.
243
+ def ensure_service!
244
+ fail "Must have active connection to service" unless service
245
+ end
246
+ end
247
+ end
248
+ end
249
+ end
@@ -1,10 +1,10 @@
1
- # Copyright 2016 Google Inc. All rights reserved.
1
+ # Copyright 2016 Google LLC
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
5
5
  # You may obtain a copy of the License at
6
6
  #
7
- # http://www.apache.org/licenses/LICENSE-2.0
7
+ # https://www.apache.org/licenses/LICENSE-2.0
8
8
  #
9
9
  # Unless required by applicable law or agreed to in writing, software
10
10
  # distributed under the License is distributed on an "AS IS" BASIS,
@@ -110,8 +110,24 @@ module Google
110
110
 
111
111
  ##
112
112
  # Begin a new transaction.
113
- def begin_transaction
114
- execute { service.begin_transaction project }
113
+ def begin_transaction read_only: nil, previous_transaction: nil
114
+ if read_only
115
+ transaction_options = Google::Datastore::V1::TransactionOptions.new
116
+ transaction_options.read_only = \
117
+ Google::Datastore::V1::TransactionOptions::ReadOnly.new
118
+ end
119
+ if previous_transaction
120
+ transaction_options ||= \
121
+ Google::Datastore::V1::TransactionOptions.new
122
+ rw = Google::Datastore::V1::TransactionOptions::ReadWrite.new(
123
+ previous_transaction: previous_transaction.encode("ASCII-8BIT")
124
+ )
125
+ transaction_options.read_write = rw
126
+ end
127
+ execute do
128
+ service.begin_transaction project,
129
+ transaction_options: transaction_options
130
+ end
115
131
  end
116
132
 
117
133
  ##
@@ -1,10 +1,10 @@
1
- # Copyright 2014 Google Inc. All rights reserved.
1
+ # Copyright 2014 Google LLC
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
5
5
  # You may obtain a copy of the License at
6
6
  #
7
- # http://www.apache.org/licenses/LICENSE-2.0
7
+ # https://www.apache.org/licenses/LICENSE-2.0
8
8
  #
9
9
  # Unless required by applicable law or agreed to in writing, software
10
10
  # distributed under the License is distributed on an "AS IS" BASIS,
@@ -26,6 +26,8 @@ module Google
26
26
  # @see https://cloud.google.com/datastore/docs/concepts/transactions
27
27
  # Transactions
28
28
  #
29
+ # @attr_reader [String] id The identifier of the transaction.
30
+ #
29
31
  # @example Transactional update:
30
32
  # require "google/cloud/datastore"
31
33
  #
@@ -41,26 +43,15 @@ module Google
41
43
  # end
42
44
  # end
43
45
  #
44
- # @example Transactional read:
45
- # require "google/cloud/datastore"
46
- #
47
- # datastore = Google::Cloud::Datastore.new
48
- #
49
- # task_list_key = datastore.key "TaskList", "default"
50
- # datastore.transaction do |tx|
51
- # task_list = tx.find task_list_key
52
- # query = tx.query("Task").ancestor(task_list)
53
- # tasks_in_list = tx.run query
54
- # end
55
- #
56
46
  class Transaction < Dataset
57
47
  attr_reader :id
58
48
 
59
49
  ##
60
50
  # @private Creates a new Transaction instance.
61
51
  # Takes a Service instead of project and Credentials.
62
- def initialize service
52
+ def initialize service, previous_transaction: nil
63
53
  @service = service
54
+ @previous_transaction = previous_transaction
64
55
  reset!
65
56
  start
66
57
  end
@@ -244,9 +235,8 @@ module Google
244
235
  #
245
236
  # datastore = Google::Cloud::Datastore.new
246
237
  #
247
- # query = datastore.query("Task").
248
- # where("done", "=", false)
249
238
  # datastore.transaction do |tx|
239
+ # query = datastore.query("Task")
250
240
  # tasks = tx.run query
251
241
  # end
252
242
  #
@@ -280,7 +270,8 @@ module Google
280
270
  fail TransactionError, "Transaction already opened." unless @id.nil?
281
271
 
282
272
  ensure_service!
283
- tx_res = service.begin_transaction
273
+ tx_res = service.begin_transaction \
274
+ previous_transaction: @previous_transaction
284
275
  @id = tx_res.transaction
285
276
  end
286
277
  alias_method :begin_transaction, :start
@@ -1,10 +1,10 @@
1
- # Copyright 2016 Google Inc. All rights reserved.
1
+ # Copyright 2016 Google LLC
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
5
5
  # You may obtain a copy of the License at
6
6
  #
7
- # http://www.apache.org/licenses/LICENSE-2.0
7
+ # https://www.apache.org/licenses/LICENSE-2.0
8
8
  #
9
9
  # Unless required by applicable law or agreed to in writing, software
10
10
  # distributed under the License is distributed on an "AS IS" BASIS,
@@ -1,10 +1,10 @@
1
- # Copyright 2017, Google Inc. All rights reserved.
1
+ # Copyright 2017 Google LLC
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
5
5
  # You may obtain a copy of the License at
6
6
  #
7
- # http://www.apache.org/licenses/LICENSE-2.0
7
+ # https://www.apache.org/licenses/LICENSE-2.0
8
8
  #
9
9
  # Unless required by applicable law or agreed to in writing, software
10
10
  # distributed under the License is distributed on an "AS IS" BASIS,
@@ -1,10 +1,10 @@
1
- # Copyright 2017, Google Inc. All rights reserved.
1
+ # Copyright 2017 Google LLC
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
5
5
  # You may obtain a copy of the License at
6
6
  #
7
- # http://www.apache.org/licenses/LICENSE-2.0
7
+ # https://www.apache.org/licenses/LICENSE-2.0
8
8
  #
9
9
  # Unless required by applicable law or agreed to in writing, software
10
10
  # distributed under the License is distributed on an "AS IS" BASIS,
@@ -1,10 +1,10 @@
1
- # Copyright 2017, Google Inc. All rights reserved.
1
+ # Copyright 2017 Google LLC
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
5
5
  # You may obtain a copy of the License at
6
6
  #
7
- # http://www.apache.org/licenses/LICENSE-2.0
7
+ # https://www.apache.org/licenses/LICENSE-2.0
8
8
  #
9
9
  # Unless required by applicable law or agreed to in writing, software
10
10
  # distributed under the License is distributed on an "AS IS" BASIS,
@@ -1,10 +1,10 @@
1
- # Copyright 2017, Google Inc. All rights reserved.
1
+ # Copyright 2017 Google LLC
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
5
5
  # You may obtain a copy of the License at
6
6
  #
7
- # http://www.apache.org/licenses/LICENSE-2.0
7
+ # https://www.apache.org/licenses/LICENSE-2.0
8
8
  #
9
9
  # Unless required by applicable law or agreed to in writing, software
10
10
  # distributed under the License is distributed on an "AS IS" BASIS,
@@ -1,10 +1,10 @@
1
- # Copyright 2017, Google Inc. All rights reserved.
1
+ # Copyright 2017 Google LLC
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
5
5
  # You may obtain a copy of the License at
6
6
  #
7
- # http://www.apache.org/licenses/LICENSE-2.0
7
+ # https://www.apache.org/licenses/LICENSE-2.0
8
8
  #
9
9
  # Unless required by applicable law or agreed to in writing, software
10
10
  # distributed under the License is distributed on an "AS IS" BASIS,
@@ -1,10 +1,10 @@
1
- # Copyright 2017, Google Inc. All rights reserved.
1
+ # Copyright 2017 Google LLC
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
5
5
  # You may obtain a copy of the License at
6
6
  #
7
- # http://www.apache.org/licenses/LICENSE-2.0
7
+ # https://www.apache.org/licenses/LICENSE-2.0
8
8
  #
9
9
  # Unless required by applicable law or agreed to in writing, software
10
10
  # distributed under the License is distributed on an "AS IS" BASIS,
@@ -1,10 +1,10 @@
1
- # Copyright 2016 Google Inc. All rights reserved.
1
+ # Copyright 2016 Google LLC
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
5
5
  # You may obtain a copy of the License at
6
6
  #
7
- # http://www.apache.org/licenses/LICENSE-2.0
7
+ # https://www.apache.org/licenses/LICENSE-2.0
8
8
  #
9
9
  # Unless required by applicable law or agreed to in writing, software
10
10
  # distributed under the License is distributed on an "AS IS" BASIS,
@@ -16,7 +16,7 @@
16
16
  module Google
17
17
  module Cloud
18
18
  module Datastore
19
- VERSION = "1.2.1"
19
+ VERSION = "1.3.0"
20
20
  end
21
21
  end
22
22
  end
@@ -7,7 +7,7 @@
7
7
  # you may not use this file except in compliance with the License.
8
8
  # You may obtain a copy of the License at
9
9
  #
10
- # http://www.apache.org/licenses/LICENSE-2.0
10
+ # https://www.apache.org/licenses/LICENSE-2.0
11
11
  #
12
12
  # Unless required by applicable law or agreed to in writing, software
13
13
  # distributed under the License is distributed on an "AS IS" BASIS,
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: google-cloud-datastore
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.1
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mike Moore
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2017-11-21 00:00:00.000000000 Z
12
+ date: 2017-12-19 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: google-cloud-core
@@ -31,14 +31,14 @@ dependencies:
31
31
  requirements:
32
32
  - - "~>"
33
33
  - !ruby/object:Gem::Version
34
- version: 0.10.1
34
+ version: '1.0'
35
35
  type: :runtime
36
36
  prerelease: false
37
37
  version_requirements: !ruby/object:Gem::Requirement
38
38
  requirements:
39
39
  - - "~>"
40
40
  - !ruby/object:Gem::Version
41
- version: 0.10.1
41
+ version: '1.0'
42
42
  - !ruby/object:Gem::Dependency
43
43
  name: google-protobuf
44
44
  requirement: !ruby/object:Gem::Requirement
@@ -205,6 +205,7 @@ files:
205
205
  - lib/google/cloud/datastore/key.rb
206
206
  - lib/google/cloud/datastore/properties.rb
207
207
  - lib/google/cloud/datastore/query.rb
208
+ - lib/google/cloud/datastore/read_only_transaction.rb
208
209
  - lib/google/cloud/datastore/service.rb
209
210
  - lib/google/cloud/datastore/transaction.rb
210
211
  - lib/google/cloud/datastore/v1.rb
@@ -240,7 +241,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
240
241
  version: '0'
241
242
  requirements: []
242
243
  rubyforge_project:
243
- rubygems_version: 2.7.2
244
+ rubygems_version: 2.7.3
244
245
  signing_key:
245
246
  specification_version: 4
246
247
  summary: API Client library for Google Cloud Datastore