google-cloud-datastore 1.8.1 → 2.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. checksums.yaml +4 -4
  2. data/AUTHENTICATION.md +4 -2
  3. data/CHANGELOG.md +47 -0
  4. data/CONTRIBUTING.md +3 -3
  5. data/LOGGING.md +1 -1
  6. data/OVERVIEW.md +1 -1
  7. data/TROUBLESHOOTING.md +2 -8
  8. data/lib/google-cloud-datastore.rb +13 -18
  9. data/lib/google/cloud/datastore.rb +12 -11
  10. data/lib/google/cloud/datastore/commit.rb +4 -4
  11. data/lib/google/cloud/datastore/convert.rb +4 -4
  12. data/lib/google/cloud/datastore/credentials.rb +2 -2
  13. data/lib/google/cloud/datastore/dataset.rb +3 -3
  14. data/lib/google/cloud/datastore/dataset/lookup_results.rb +5 -5
  15. data/lib/google/cloud/datastore/dataset/query_results.rb +6 -6
  16. data/lib/google/cloud/datastore/entity.rb +4 -4
  17. data/lib/google/cloud/datastore/gql_query.rb +19 -23
  18. data/lib/google/cloud/datastore/key.rb +9 -8
  19. data/lib/google/cloud/datastore/properties.rb +1 -1
  20. data/lib/google/cloud/datastore/query.rb +15 -14
  21. data/lib/google/cloud/datastore/service.rb +34 -86
  22. data/lib/google/cloud/datastore/transaction.rb +1 -1
  23. data/lib/google/cloud/datastore/version.rb +1 -1
  24. metadata +11 -66
  25. data/lib/google/cloud/datastore/v1.rb +0 -16
  26. data/lib/google/cloud/datastore/v1/credentials.rb +0 -39
  27. data/lib/google/cloud/datastore/v1/datastore_client.rb +0 -577
  28. data/lib/google/cloud/datastore/v1/datastore_client_config.json +0 -61
  29. data/lib/google/cloud/datastore/v1/doc/google/datastore/v1/datastore.rb +0 -283
  30. data/lib/google/cloud/datastore/v1/doc/google/datastore/v1/entity.rb +0 -189
  31. data/lib/google/cloud/datastore/v1/doc/google/datastore/v1/query.rb +0 -297
  32. data/lib/google/cloud/datastore/v1/doc/google/protobuf/struct.rb +0 -74
  33. data/lib/google/cloud/datastore/v1/doc/google/protobuf/timestamp.rb +0 -109
  34. data/lib/google/cloud/datastore/v1/doc/google/protobuf/wrappers.rb +0 -26
  35. data/lib/google/cloud/datastore/v1/doc/google/type/latlng.rb +0 -65
  36. data/lib/google/datastore/v1/datastore_pb.rb +0 -147
  37. data/lib/google/datastore/v1/datastore_services_pb.rb +0 -65
  38. data/lib/google/datastore/v1/entity_pb.rb +0 -64
  39. data/lib/google/datastore/v1/query_pb.rb +0 -132
@@ -293,7 +293,7 @@ module Google
293
293
  # task.persisted? #=> true
294
294
  #
295
295
  def persisted?
296
- @key && @key.frozen?
296
+ @key&.frozen?
297
297
  end
298
298
 
299
299
  ##
@@ -427,10 +427,10 @@ module Google
427
427
  end
428
428
 
429
429
  ##
430
- # @private Convert the Entity to a Google::Datastore::V1::Entity
430
+ # @private Convert the Entity to a Google::Cloud::Datastore::V1::Entity
431
431
  # object.
432
432
  def to_grpc
433
- grpc = Google::Datastore::V1::Entity.new(
433
+ grpc = Google::Cloud::Datastore::V1::Entity.new(
434
434
  properties: @properties.to_grpc
435
435
  )
436
436
  grpc.key = @key.to_grpc unless @key.nil?
@@ -439,7 +439,7 @@ module Google
439
439
  end
440
440
 
441
441
  ##
442
- # @private Create a new Entity from a Google::Datastore::V1::Key
442
+ # @private Create a new Entity from a Google::Cloud::Datastore::V1::Key
443
443
  # object.
444
444
  def self.from_grpc grpc
445
445
  entity = Entity.new
@@ -49,7 +49,7 @@ module Google
49
49
  # gql_query = Google::Cloud::Datastore::GqlQuery.new
50
50
  #
51
51
  def initialize
52
- @grpc = Google::Datastore::V1::GqlQuery.new
52
+ @grpc = Google::Cloud::Datastore::V1::GqlQuery.new
53
53
  end
54
54
 
55
55
  ##
@@ -157,17 +157,15 @@ module Google
157
157
  def named_bindings= new_named_bindings
158
158
  @grpc.named_bindings.clear
159
159
  new_named_bindings.map do |name, value|
160
- if value.is_a? Google::Cloud::Datastore::Cursor
161
- @grpc.named_bindings[name.to_s] = \
162
- Google::Datastore::V1::GqlQueryParameter.new(
163
- cursor: value.to_grpc
164
- )
165
- else
166
- @grpc.named_bindings[name.to_s] = \
167
- Google::Datastore::V1::GqlQueryParameter.new(
168
- value: Convert.to_value(value)
169
- )
170
- end
160
+ @grpc.named_bindings[name.to_s] = if value.is_a? Google::Cloud::Datastore::Cursor
161
+ Google::Cloud::Datastore::V1::GqlQueryParameter.new(
162
+ cursor: value.to_grpc
163
+ )
164
+ else
165
+ Google::Cloud::Datastore::V1::GqlQueryParameter.new(
166
+ value: Convert.to_value(value)
167
+ )
168
+ end
171
169
  end
172
170
  end
173
171
 
@@ -208,17 +206,15 @@ module Google
208
206
  def positional_bindings= new_positional_bindings
209
207
  @grpc.positional_bindings.clear
210
208
  new_positional_bindings.map do |value|
211
- if value.is_a? Google::Cloud::Datastore::Cursor
212
- @grpc.positional_bindings << \
213
- Google::Datastore::V1::GqlQueryParameter.new(
214
- cursor: value.to_grpc
215
- )
216
- else
217
- @grpc.positional_bindings << \
218
- Google::Datastore::V1::GqlQueryParameter.new(
219
- value: Convert.to_value(value)
220
- )
221
- end
209
+ @grpc.positional_bindings << if value.is_a? Google::Cloud::Datastore::Cursor
210
+ Google::Cloud::Datastore::V1::GqlQueryParameter.new(
211
+ cursor: value.to_grpc
212
+ )
213
+ else
214
+ Google::Cloud::Datastore::V1::GqlQueryParameter.new(
215
+ value: Convert.to_value(value)
216
+ )
217
+ end
222
218
  end
223
219
  end
224
220
 
@@ -268,20 +268,21 @@ module Google
268
268
  end
269
269
 
270
270
  ##
271
- # @private Convert the Key to a Google::Datastore::V1::Key object.
271
+ # @private Convert the Key to a Google::Cloud::Datastore::V1::Key object.
272
272
  def to_grpc
273
273
  grpc_path = path.map do |pe_kind, pe_id_or_name|
274
274
  path_args = { kind: pe_kind }
275
- if pe_id_or_name.is_a? Integer
275
+ case pe_id_or_name
276
+ when Integer
276
277
  path_args[:id] = pe_id_or_name
277
- elsif pe_id_or_name.is_a? String
278
+ when String
278
279
  path_args[:name] = pe_id_or_name unless pe_id_or_name.empty?
279
280
  end
280
- Google::Datastore::V1::Key::PathElement.new path_args
281
+ Google::Cloud::Datastore::V1::Key::PathElement.new path_args
281
282
  end
282
- grpc = Google::Datastore::V1::Key.new path: grpc_path
283
+ grpc = Google::Cloud::Datastore::V1::Key.new path: grpc_path
283
284
  if project || namespace
284
- grpc.partition_id = Google::Datastore::V1::PartitionId.new(
285
+ grpc.partition_id = Google::Cloud::Datastore::V1::PartitionId.new(
285
286
  project_id: project.to_s, namespace_id: namespace.to_s
286
287
  )
287
288
  end
@@ -289,7 +290,7 @@ module Google
289
290
  end
290
291
 
291
292
  ##
292
- # @private Create a new Key from a Google::Datastore::V1::Key
293
+ # @private Create a new Key from a Google::Cloud::Datastore::V1::Key
293
294
  # object.
294
295
  def self.from_grpc grpc
295
296
  return nil if grpc.nil?
@@ -305,7 +306,7 @@ module Google
305
306
  key.project = key_grpc.partition_id.project_id
306
307
  key.namespace = key_grpc.partition_id.namespace_id
307
308
  end
308
- key.parent = Key.from_grpc key_grpc if key_grpc.path.count > 0
309
+ key.parent = Key.from_grpc key_grpc if key_grpc.path.count.positive?
309
310
  # Freeze the key to make it immutable.
310
311
  key.freeze
311
312
  key
@@ -73,7 +73,7 @@ module Google
73
73
  alias to_hash to_h
74
74
 
75
75
  def to_grpc
76
- # Convert to Hash with Google::Datastore::V1::Value values.
76
+ # Convert to Hash with Google::Cloud::Datastore::V1::Value values.
77
77
  Hash[@hash.map { |k, v| [k.to_s, Convert.to_value(v)] }]
78
78
  end
79
79
 
@@ -61,7 +61,7 @@ module Google
61
61
  # query = Google::Cloud::Datastore::Query.new
62
62
  #
63
63
  def initialize
64
- @grpc = Google::Datastore::V1::Query.new
64
+ @grpc = Google::Cloud::Datastore::V1::Query.new
65
65
  end
66
66
 
67
67
  ##
@@ -83,7 +83,7 @@ module Google
83
83
  #
84
84
  def kind *kinds
85
85
  kinds.each do |kind|
86
- grpc_kind = Google::Datastore::V1::KindExpression.new(
86
+ grpc_kind = Google::Cloud::Datastore::V1::KindExpression.new(
87
87
  name: kind
88
88
  )
89
89
  @grpc.kind << grpc_kind
@@ -177,15 +177,15 @@ module Google
177
177
  # tasks = datastore.run query
178
178
  #
179
179
  def where name, operator, value
180
- @grpc.filter ||= Google::Datastore::V1::Filter.new(
181
- composite_filter: Google::Datastore::V1::CompositeFilter.new(
180
+ @grpc.filter ||= Google::Cloud::Datastore::V1::Filter.new(
181
+ composite_filter: Google::Cloud::Datastore::V1::CompositeFilter.new(
182
182
  op: :AND
183
183
  )
184
184
  )
185
185
  @grpc.filter.composite_filter.filters << \
186
- Google::Datastore::V1::Filter.new(
187
- property_filter: Google::Datastore::V1::PropertyFilter.new(
188
- property: Google::Datastore::V1::PropertyReference.new(
186
+ Google::Cloud::Datastore::V1::Filter.new(
187
+ property_filter: Google::Cloud::Datastore::V1::PropertyFilter.new(
188
+ property: Google::Cloud::Datastore::V1::PropertyReference.new(
189
189
  name: name
190
190
  ),
191
191
  op: Convert.to_prop_filter_op(operator),
@@ -273,8 +273,8 @@ module Google
273
273
  # tasks = datastore.run query
274
274
  #
275
275
  def order name, direction = :asc
276
- @grpc.order << Google::Datastore::V1::PropertyOrder.new(
277
- property: Google::Datastore::V1::PropertyReference.new(
276
+ @grpc.order << Google::Cloud::Datastore::V1::PropertyOrder.new(
277
+ property: Google::Cloud::Datastore::V1::PropertyReference.new(
278
278
  name: name
279
279
  ),
280
280
  direction: prop_order_direction(direction)
@@ -340,9 +340,10 @@ module Google
340
340
  # tasks = datastore.run query
341
341
  #
342
342
  def start cursor
343
- if cursor.is_a? Cursor
343
+ case cursor
344
+ when Cursor
344
345
  @grpc.start_cursor = cursor.to_grpc
345
- elsif cursor.is_a? String
346
+ when String
346
347
  @grpc.start_cursor = Convert.decode_bytes cursor
347
348
  else
348
349
  raise ArgumentError, "Can't set a cursor using a #{cursor.class}."
@@ -384,8 +385,8 @@ module Google
384
385
  #
385
386
  def select *names
386
387
  names.each do |name|
387
- grpc_projection = Google::Datastore::V1::Projection.new(
388
- property: Google::Datastore::V1::PropertyReference.new(
388
+ grpc_projection = Google::Cloud::Datastore::V1::Projection.new(
389
+ property: Google::Cloud::Datastore::V1::PropertyReference.new(
389
390
  name: name
390
391
  )
391
392
  )
@@ -414,7 +415,7 @@ module Google
414
415
  #
415
416
  def group_by *names
416
417
  names.each do |name|
417
- grpc_property = Google::Datastore::V1::PropertyReference.new(
418
+ grpc_property = Google::Cloud::Datastore::V1::PropertyReference.new(
418
419
  name: name
419
420
  )
420
421
  @grpc.distinct_on << grpc_property
@@ -17,7 +17,6 @@ require "google/cloud/errors"
17
17
  require "google/cloud/datastore/credentials"
18
18
  require "google/cloud/datastore/version"
19
19
  require "google/cloud/datastore/v1"
20
- require "google/gax/errors"
21
20
 
22
21
  module Google
23
22
  module Cloud
@@ -26,59 +25,38 @@ module Google
26
25
  # @private Represents the GAX Datastore service, including all the API
27
26
  # methods.
28
27
  class Service
29
- attr_accessor :project, :credentials, :host, :timeout, :client_config
28
+ attr_accessor :project
29
+ attr_accessor :credentials
30
+ attr_accessor :host
31
+ attr_accessor :timeout
30
32
 
31
33
  ##
32
34
  # Creates a new Service instance.
33
- def initialize project, credentials, host: nil, timeout: nil,
34
- client_config: nil
35
+ def initialize project, credentials, host: nil, timeout: nil
35
36
  @project = project
36
37
  @credentials = credentials
37
- @host = host || V1::DatastoreClient::SERVICE_ADDRESS
38
+ @host = host
38
39
  @timeout = timeout
39
- @client_config = client_config || {}
40
- end
41
-
42
- def channel
43
- require "grpc"
44
- GRPC::Core::Channel.new host, chan_args, chan_creds
45
- end
46
-
47
- def chan_args
48
- { "grpc.service_config_disable_resolution" => 1 }
49
- end
50
-
51
- def chan_creds
52
- return credentials if insecure?
53
- require "grpc"
54
- GRPC::Core::ChannelCredentials.new.compose \
55
- GRPC::Core::CallCredentials.new credentials.client.updater_proc
56
40
  end
57
41
 
58
42
  def service
59
43
  return mocked_service if mocked_service
60
- @service ||= V1::DatastoreClient.new(
61
- credentials: channel,
62
- timeout: timeout,
63
- client_config: client_config,
64
- lib_name: "gccl",
65
- lib_version: Google::Cloud::Datastore::VERSION
66
- )
44
+ @service ||= V1::Datastore::Client.new do |config|
45
+ config.credentials = credentials if credentials
46
+ config.timeout = timeout if timeout
47
+ config.endpoint = host if host
48
+ config.lib_name = "gccl"
49
+ config.lib_version = Google::Cloud::Datastore::VERSION
50
+ config.metadata = { "google-cloud-resource-prefix": "projects/#{@project}" }
51
+ end
67
52
  end
68
53
  attr_accessor :mocked_service
69
54
 
70
- def insecure?
71
- credentials == :this_channel_is_insecure
72
- end
73
-
74
55
  ##
75
56
  # Allocate IDs for incomplete keys.
76
57
  # (This is useful for referencing an entity before it is inserted.)
77
58
  def allocate_ids *incomplete_keys
78
- execute do
79
- service.allocate_ids project, incomplete_keys,
80
- options: default_options
81
- end
59
+ service.allocate_ids project_id: project, keys: incomplete_keys
82
60
  end
83
61
 
84
62
  ##
@@ -86,76 +64,61 @@ module Google
86
64
  def lookup *keys, consistency: nil, transaction: nil
87
65
  read_options = generate_read_options consistency, transaction
88
66
 
89
- execute do
90
- service.lookup project, keys,
91
- read_options: read_options, options: default_options
92
- end
67
+ service.lookup project_id: project, keys: keys, read_options: read_options
93
68
  end
94
69
 
95
70
  # Query for entities.
96
71
  def run_query query, namespace = nil, consistency: nil, transaction: nil
97
72
  gql_query = nil
98
- if query.is_a? Google::Datastore::V1::GqlQuery
73
+ if query.is_a? Google::Cloud::Datastore::V1::GqlQuery
99
74
  gql_query = query
100
75
  query = nil
101
76
  end
102
77
  read_options = generate_read_options consistency, transaction
103
78
  if namespace
104
- partition_id = Google::Datastore::V1::PartitionId.new(
79
+ partition_id = Google::Cloud::Datastore::V1::PartitionId.new(
105
80
  namespace_id: namespace
106
81
  )
107
82
  end
108
83
 
109
- execute do
110
- service.run_query project, partition_id: partition_id,
111
- read_options: read_options,
112
- query: query,
113
- gql_query: gql_query,
114
- options: default_options
115
- end
84
+ service.run_query project_id: project,
85
+ partition_id: partition_id,
86
+ read_options: read_options,
87
+ query: query,
88
+ gql_query: gql_query
116
89
  end
117
90
 
118
91
  ##
119
92
  # Begin a new transaction.
120
93
  def begin_transaction read_only: nil, previous_transaction: nil
121
94
  if read_only
122
- transaction_options = Google::Datastore::V1::TransactionOptions.new
95
+ transaction_options = Google::Cloud::Datastore::V1::TransactionOptions.new
123
96
  transaction_options.read_only = \
124
- Google::Datastore::V1::TransactionOptions::ReadOnly.new
97
+ Google::Cloud::Datastore::V1::TransactionOptions::ReadOnly.new
125
98
  end
126
99
  if previous_transaction
127
100
  transaction_options ||= \
128
- Google::Datastore::V1::TransactionOptions.new
129
- rw = Google::Datastore::V1::TransactionOptions::ReadWrite.new(
101
+ Google::Cloud::Datastore::V1::TransactionOptions.new
102
+ rw = Google::Cloud::Datastore::V1::TransactionOptions::ReadWrite.new(
130
103
  previous_transaction: previous_transaction.encode("ASCII-8BIT")
131
104
  )
132
105
  transaction_options.read_write = rw
133
106
  end
134
- execute do
135
- service.begin_transaction project,
136
- transaction_options: transaction_options
137
- end
107
+ service.begin_transaction project_id: project, transaction_options: transaction_options
138
108
  end
139
109
 
140
110
  ##
141
111
  # Commit a transaction, optionally creating, deleting or modifying
142
112
  # some entities.
143
113
  def commit mutations, transaction: nil
144
- mode = transaction.nil? ? :NON_TRANSACTIONAL : :TRANSACTIONAL
145
- execute do
146
- service.commit project, mode: mode,
147
- mutations: mutations,
148
- transaction: transaction,
149
- options: default_options
150
- end
114
+ mode = transaction.nil? ? :NON_TRANSACTIONAL : :TRANSACTIONAL
115
+ service.commit project_id: project, mode: mode, mutations: mutations, transaction: transaction
151
116
  end
152
117
 
153
118
  ##
154
119
  # Roll back a transaction.
155
120
  def rollback transaction
156
- execute do
157
- service.rollback project, transaction, options: default_options
158
- end
121
+ service.rollback project_id: project, transaction: transaction
159
122
  end
160
123
 
161
124
  def inspect
@@ -166,35 +129,20 @@ module Google
166
129
 
167
130
  def generate_read_options consistency, transaction
168
131
  if consistency == :eventual
169
- return Google::Datastore::V1::ReadOptions.new(
132
+ return Google::Cloud::Datastore::V1::ReadOptions.new(
170
133
  read_consistency: :EVENTUAL
171
134
  )
172
135
  elsif consistency == :strong
173
- return Google::Datastore::V1::ReadOptions.new(
136
+ return Google::Cloud::Datastore::V1::ReadOptions.new(
174
137
  read_consistency: :STRONG
175
138
  )
176
139
  elsif transaction
177
- return Google::Datastore::V1::ReadOptions.new(
140
+ return Google::Cloud::Datastore::V1::ReadOptions.new(
178
141
  transaction: transaction
179
142
  )
180
143
  end
181
144
  nil
182
145
  end
183
-
184
- def default_headers
185
- { "google-cloud-resource-prefix" => "projects/#{@project}" }
186
- end
187
-
188
- def default_options
189
- Google::Gax::CallOptions.new kwargs: default_headers
190
- end
191
-
192
- def execute
193
- yield
194
- rescue Google::Gax::GaxError => e
195
- # GaxError wraps BadStatus, but exposes it as #cause
196
- raise Google::Cloud::Error.from_error(e.cause)
197
- end
198
146
  end
199
147
  end
200
148
  end
@@ -50,7 +50,7 @@ module Google
50
50
  # @private Creates a new Transaction instance.
51
51
  # Takes a Service instead of project and Credentials.
52
52
  def initialize service, previous_transaction: nil
53
- @service = service
53
+ super service
54
54
  @previous_transaction = previous_transaction
55
55
  reset!
56
56
  start