google-cloud-bigtable 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (103) hide show
  1. checksums.yaml +7 -0
  2. data/.yardopts +19 -0
  3. data/AUTHENTICATION.md +177 -0
  4. data/CHANGELOG.md +223 -0
  5. data/CODE_OF_CONDUCT.md +40 -0
  6. data/CONTRIBUTING.md +188 -0
  7. data/EMULATOR.md +30 -0
  8. data/LICENSE +201 -0
  9. data/LOGGING.md +32 -0
  10. data/OVERVIEW.md +400 -0
  11. data/TROUBLESHOOTING.md +31 -0
  12. data/lib/google-cloud-bigtable.rb +171 -0
  13. data/lib/google/bigtable/admin/v2/bigtable_instance_admin_pb.rb +145 -0
  14. data/lib/google/bigtable/admin/v2/bigtable_instance_admin_services_pb.rb +90 -0
  15. data/lib/google/bigtable/admin/v2/bigtable_table_admin_pb.rb +208 -0
  16. data/lib/google/bigtable/admin/v2/bigtable_table_admin_services_pb.rb +154 -0
  17. data/lib/google/bigtable/admin/v2/common_pb.rb +30 -0
  18. data/lib/google/bigtable/admin/v2/instance_pb.rb +74 -0
  19. data/lib/google/bigtable/admin/v2/table_pb.rb +127 -0
  20. data/lib/google/bigtable/v2/bigtable_pb.rb +113 -0
  21. data/lib/google/bigtable/v2/bigtable_services_pb.rb +68 -0
  22. data/lib/google/bigtable/v2/data_pb.rb +156 -0
  23. data/lib/google/cloud/bigtable.rb +184 -0
  24. data/lib/google/cloud/bigtable/admin.rb +202 -0
  25. data/lib/google/cloud/bigtable/admin/credentials.rb +27 -0
  26. data/lib/google/cloud/bigtable/admin/v2.rb +223 -0
  27. data/lib/google/cloud/bigtable/admin/v2/bigtable_instance_admin_client.rb +1451 -0
  28. data/lib/google/cloud/bigtable/admin/v2/bigtable_instance_admin_client_config.json +139 -0
  29. data/lib/google/cloud/bigtable/admin/v2/bigtable_table_admin_client.rb +1734 -0
  30. data/lib/google/cloud/bigtable/admin/v2/bigtable_table_admin_client_config.json +163 -0
  31. data/lib/google/cloud/bigtable/admin/v2/credentials.rb +51 -0
  32. data/lib/google/cloud/bigtable/admin/v2/doc/google/bigtable/admin/v2/bigtable_instance_admin.rb +297 -0
  33. data/lib/google/cloud/bigtable/admin/v2/doc/google/bigtable/admin/v2/bigtable_table_admin.rb +587 -0
  34. data/lib/google/cloud/bigtable/admin/v2/doc/google/bigtable/admin/v2/instance.rb +193 -0
  35. data/lib/google/cloud/bigtable/admin/v2/doc/google/bigtable/admin/v2/table.rb +303 -0
  36. data/lib/google/cloud/bigtable/admin/v2/doc/google/iam/v1/iam_policy.rb +64 -0
  37. data/lib/google/cloud/bigtable/admin/v2/doc/google/iam/v1/options.rb +33 -0
  38. data/lib/google/cloud/bigtable/admin/v2/doc/google/iam/v1/policy.rb +151 -0
  39. data/lib/google/cloud/bigtable/admin/v2/doc/google/longrunning/operations.rb +51 -0
  40. data/lib/google/cloud/bigtable/admin/v2/doc/google/protobuf/any.rb +131 -0
  41. data/lib/google/cloud/bigtable/admin/v2/doc/google/protobuf/duration.rb +91 -0
  42. data/lib/google/cloud/bigtable/admin/v2/doc/google/protobuf/empty.rb +29 -0
  43. data/lib/google/cloud/bigtable/admin/v2/doc/google/protobuf/field_mask.rb +222 -0
  44. data/lib/google/cloud/bigtable/admin/v2/doc/google/protobuf/timestamp.rb +113 -0
  45. data/lib/google/cloud/bigtable/admin/v2/doc/google/rpc/status.rb +39 -0
  46. data/lib/google/cloud/bigtable/admin/v2/doc/google/type/expr.rb +45 -0
  47. data/lib/google/cloud/bigtable/app_profile.rb +439 -0
  48. data/lib/google/cloud/bigtable/app_profile/job.rb +99 -0
  49. data/lib/google/cloud/bigtable/app_profile/list.rb +165 -0
  50. data/lib/google/cloud/bigtable/backup.rb +324 -0
  51. data/lib/google/cloud/bigtable/backup/job.rb +87 -0
  52. data/lib/google/cloud/bigtable/backup/list.rb +167 -0
  53. data/lib/google/cloud/bigtable/chunk_processor.rb +241 -0
  54. data/lib/google/cloud/bigtable/cluster.rb +390 -0
  55. data/lib/google/cloud/bigtable/cluster/job.rb +88 -0
  56. data/lib/google/cloud/bigtable/cluster/list.rb +171 -0
  57. data/lib/google/cloud/bigtable/column_family.rb +73 -0
  58. data/lib/google/cloud/bigtable/column_family_map.rb +426 -0
  59. data/lib/google/cloud/bigtable/column_range.rb +194 -0
  60. data/lib/google/cloud/bigtable/convert.rb +83 -0
  61. data/lib/google/cloud/bigtable/credentials.rb +25 -0
  62. data/lib/google/cloud/bigtable/errors.rb +38 -0
  63. data/lib/google/cloud/bigtable/gc_rule.rb +334 -0
  64. data/lib/google/cloud/bigtable/instance.rb +935 -0
  65. data/lib/google/cloud/bigtable/instance/cluster_map.rb +74 -0
  66. data/lib/google/cloud/bigtable/instance/job.rb +98 -0
  67. data/lib/google/cloud/bigtable/instance/list.rb +164 -0
  68. data/lib/google/cloud/bigtable/longrunning_job.rb +122 -0
  69. data/lib/google/cloud/bigtable/mutation_entry.rb +256 -0
  70. data/lib/google/cloud/bigtable/mutation_operations.rb +357 -0
  71. data/lib/google/cloud/bigtable/policy.rb +167 -0
  72. data/lib/google/cloud/bigtable/project.rb +471 -0
  73. data/lib/google/cloud/bigtable/read_modify_write_rule.rb +134 -0
  74. data/lib/google/cloud/bigtable/read_operations.rb +328 -0
  75. data/lib/google/cloud/bigtable/routing_policy.rb +172 -0
  76. data/lib/google/cloud/bigtable/row.rb +136 -0
  77. data/lib/google/cloud/bigtable/row_filter.rb +639 -0
  78. data/lib/google/cloud/bigtable/row_filter/chain_filter.rb +590 -0
  79. data/lib/google/cloud/bigtable/row_filter/condition_filter.rb +114 -0
  80. data/lib/google/cloud/bigtable/row_filter/interleave_filter.rb +621 -0
  81. data/lib/google/cloud/bigtable/row_filter/simple_filter.rb +287 -0
  82. data/lib/google/cloud/bigtable/row_range.rb +179 -0
  83. data/lib/google/cloud/bigtable/rows_mutator.rb +113 -0
  84. data/lib/google/cloud/bigtable/rows_reader.rb +200 -0
  85. data/lib/google/cloud/bigtable/sample_row_key.rb +85 -0
  86. data/lib/google/cloud/bigtable/service.rb +913 -0
  87. data/lib/google/cloud/bigtable/status.rb +76 -0
  88. data/lib/google/cloud/bigtable/table.rb +686 -0
  89. data/lib/google/cloud/bigtable/table/cluster_state.rb +125 -0
  90. data/lib/google/cloud/bigtable/table/list.rb +154 -0
  91. data/lib/google/cloud/bigtable/table/restore_job.rb +117 -0
  92. data/lib/google/cloud/bigtable/v2.rb +146 -0
  93. data/lib/google/cloud/bigtable/v2/bigtable_client.rb +591 -0
  94. data/lib/google/cloud/bigtable/v2/bigtable_client_config.json +83 -0
  95. data/lib/google/cloud/bigtable/v2/credentials.rb +46 -0
  96. data/lib/google/cloud/bigtable/v2/doc/google/bigtable/v2/bigtable.rb +290 -0
  97. data/lib/google/cloud/bigtable/v2/doc/google/bigtable/v2/data.rb +493 -0
  98. data/lib/google/cloud/bigtable/v2/doc/google/protobuf/any.rb +131 -0
  99. data/lib/google/cloud/bigtable/v2/doc/google/protobuf/wrappers.rb +34 -0
  100. data/lib/google/cloud/bigtable/v2/doc/google/rpc/status.rb +39 -0
  101. data/lib/google/cloud/bigtable/value_range.rb +181 -0
  102. data/lib/google/cloud/bigtable/version.rb +22 -0
  103. metadata +337 -0
@@ -0,0 +1,76 @@
1
+ # Copyright 2019 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
+ module Google
17
+ module Cloud
18
+ module Bigtable
19
+ ##
20
+ # # Status
21
+ #
22
+ # Represents a logical error model from the Bigtable service, containing an
23
+ # error code, an error message, and optional error details.
24
+ #
25
+ # @attr [Integer] code The status code, which should be an enum value of
26
+ # [google.rpc.Code](https://github.com/googleapis/googleapis/blob/master/google/rpc/code.proto).
27
+ # @attr [String] description The human-readable description for the status code, which should be an enum value of
28
+ # [google.rpc.Code](https://github.com/googleapis/googleapis/blob/master/google/rpc/code.proto). For example,
29
+ # `INVALID_ARGUMENT`.
30
+ # @attr [String] message A developer-facing error message, which should be in English.
31
+ # @attr [Array<String>] details A list of messages that carry the error details.
32
+ #
33
+ # @example
34
+ # require "google/cloud/bigtable"
35
+ #
36
+ # bigtable = Google::Cloud::Bigtable.new
37
+ #
38
+ # table = bigtable.table("my-instance", "my-table")
39
+ #
40
+ # entries = []
41
+ # entries << table.new_mutation_entry("row-1").set_cell("cf1", "field1", "XYZ")
42
+ # entries << table.new_mutation_entry("row-2").set_cell("cf1", "field1", "ABC")
43
+ # responses = table.mutate_rows(entries)
44
+ #
45
+ # responses.each do |response|
46
+ # puts response.status.description
47
+ # end
48
+ #
49
+ class Status
50
+ attr_reader :code, :description, :message, :details
51
+
52
+ ##
53
+ # @private Creates a Status object.
54
+ def initialize code, description, message, details
55
+ @code = code
56
+ @description = description
57
+ @message = message
58
+ @details = details
59
+ end
60
+
61
+ ##
62
+ # @private New Status from a Google::Rpc::Status object.
63
+ def self.from_grpc grpc
64
+ new grpc.code, description_for(grpc.code), grpc.message, grpc.details
65
+ end
66
+
67
+ # @private Get a descriptive symbol for a google.rpc.Code integer
68
+ def self.description_for code
69
+ ["OK", "CANCELLED", "UNKNOWN", "INVALID_ARGUMENT", "DEADLINE_EXCEEDED", "NOT_FOUND", "ALREADY_EXISTS",
70
+ "PERMISSION_DENIED", "RESOURCE_EXHAUSTED", "FAILED_PRECONDITION", "ABORTED", "OUT_OF_RANGE", "UNIMPLEMENTED",
71
+ "INTERNAL", "UNAVAILABLE", "DATA_LOSS", "UNAUTHENTICATED"][code]
72
+ end
73
+ end
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,686 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright 2018 Google LLC
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # https://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+
18
+ require "google/cloud/bigtable/table/list"
19
+ require "google/cloud/bigtable/table/cluster_state"
20
+ require "google/cloud/bigtable/column_family_map"
21
+ require "google/cloud/bigtable/gc_rule"
22
+ require "google/cloud/bigtable/mutation_operations"
23
+ require "google/cloud/bigtable/policy"
24
+ require "google/cloud/bigtable/read_operations"
25
+
26
+ module Google
27
+ module Cloud
28
+ module Bigtable
29
+ ##
30
+ # # Table
31
+ #
32
+ # A collection of user data indexed by row, column, and timestamp.
33
+ # Each table is served using the resources of its parent cluster.
34
+ #
35
+ # @example
36
+ # require "google/cloud/bigtable"
37
+ #
38
+ # bigtable = Google::Cloud::Bigtable.new
39
+ #
40
+ # table = bigtable.table("my-instance", "my-table")
41
+ #
42
+ # if table.exists?
43
+ # p "Table exists."
44
+ # else
45
+ # p "Table does not exist"
46
+ # end
47
+ #
48
+ class Table
49
+ # @!parse extend MutationOperations
50
+ include MutationOperations
51
+
52
+ # @!parse extend ReadOperations
53
+ include ReadOperations
54
+
55
+ # @private
56
+ # The gRPC Service object.
57
+ attr_accessor :service
58
+
59
+ ##
60
+ # @return [String] App profile ID for request routing.
61
+ #
62
+ attr_accessor :app_profile_id
63
+
64
+ # @private
65
+ #
66
+ # Creates a new Table instance.
67
+ def initialize grpc, service, view: nil
68
+ @grpc = grpc
69
+ @service = service
70
+ @view = view || :SCHEMA_VIEW
71
+ end
72
+
73
+ ##
74
+ # The unique identifier for the project to which the table belongs.
75
+ #
76
+ # @return [String]
77
+ #
78
+ def project_id
79
+ @grpc.name.split("/")[1]
80
+ end
81
+
82
+ ##
83
+ # The unique identifier for the instance to which the table belongs.
84
+ #
85
+ # @return [String]
86
+ #
87
+ def instance_id
88
+ @grpc.name.split("/")[3]
89
+ end
90
+
91
+ ##
92
+ # The unique identifier for the table.
93
+ #
94
+ # @return [String]
95
+ #
96
+ def name
97
+ @grpc.name.split("/")[5]
98
+ end
99
+ alias table_id name
100
+
101
+ ##
102
+ # The full path for the table resource. Values are of the form
103
+ # `projects/<project_id>/instances/<instance_id>/table/<table_id>`.
104
+ #
105
+ # @return [String]
106
+ #
107
+ def path
108
+ @grpc.name
109
+ end
110
+
111
+ ##
112
+ # Reloads table data.
113
+ #
114
+ # @param view [Symbol] Table view type.
115
+ # Default view type is `:SCHEMA_VIEW`.
116
+ # Valid view types are:
117
+ #
118
+ # * `:NAME_ONLY` - Only populates `name`.
119
+ # * `:SCHEMA_VIEW` - Only populates `name` and fields related to the table's schema.
120
+ # * `:REPLICATION_VIEW` - Only populates `name` and fields related to the table's replication state.
121
+ # * `:FULL` - Populates all fields.
122
+ #
123
+ # @return [Google::Cloud::Bigtable::Table]
124
+ #
125
+ def reload! view: nil
126
+ @view = view || :SCHEMA_VIEW
127
+ @grpc = service.get_table instance_id, name, view: view
128
+ self
129
+ end
130
+
131
+ ##
132
+ # Map from cluster ID to per-cluster table state.
133
+ # If it could not be determined whether or not the table has data in a
134
+ # particular cluster (for example, if its zone is unavailable), then
135
+ # there will be an entry for the cluster with UNKNOWN `replication_status`.
136
+ # Views: `FULL`.
137
+ #
138
+ # @return [Array<Google::Cloud::Bigtable::Table::ClusterState>]
139
+ #
140
+ def cluster_states
141
+ check_view_and_load :REPLICATION_VIEW
142
+ @grpc.cluster_states.map do |name, state_grpc|
143
+ ClusterState.from_grpc state_grpc, name
144
+ end
145
+ end
146
+
147
+ ##
148
+ # Returns a frozen object containing the column families configured for
149
+ # the table, mapped by column family name. Reloads the table if
150
+ # necessary to retrieve the column families data, since it is only
151
+ # available in a table with view type `SCHEMA_VIEW` or `FULL`.
152
+ #
153
+ # Also accepts a block for making modifications to the table's column
154
+ # families. After the modifications are completed, the table will be
155
+ # updated with the changes, and the updated column families will be
156
+ # returned.
157
+ #
158
+ # @yield [column_families] A block for modifying the table's column
159
+ # families. Applies multiple column modifications. Performs a series
160
+ # of column family modifications on the specified table. Either all or
161
+ # none of the modifications will occur before this method returns, but
162
+ # data requests received prior to that point may see a table where
163
+ # only some modifications have taken effect.
164
+ # @yieldparam [ColumnFamilyMap] column_families
165
+ # A mutable object containing the column families for the table,
166
+ # mapped by column family name. Any changes made to this object will
167
+ # be stored in API.
168
+ #
169
+ # @return [ColumnFamilyMap] A frozen object containing the
170
+ # column families for the table, mapped by column family name.
171
+ #
172
+ # @example
173
+ # require "google/cloud/bigtable"
174
+ #
175
+ # bigtable = Google::Cloud::Bigtable.new
176
+ #
177
+ # table = bigtable.table("my-instance", "my-table", perform_lookup: true)
178
+ #
179
+ # table.column_families.each do |name, cf|
180
+ # puts name
181
+ # puts cf.gc_rule
182
+ # end
183
+ #
184
+ # # Get a column family by name
185
+ # cf1 = table.column_families["cf1"]
186
+ #
187
+ # @example Modify the table's column families
188
+ # require "google/cloud/bigtable"
189
+ #
190
+ # bigtable = Google::Cloud::Bigtable.new
191
+ #
192
+ # table = bigtable.table("my-instance", "my-table", perform_lookup: true)
193
+ #
194
+ # table.column_families do |cfm|
195
+ # cfm.add "cf4", gc_rule: Google::Cloud::Bigtable::GcRule.max_age(600)
196
+ # cfm.add "cf5", gc_rule: Google::Cloud::Bigtable::GcRule.max_versions(5)
197
+ #
198
+ # rule_1 = Google::Cloud::Bigtable::GcRule.max_versions(3)
199
+ # rule_2 = Google::Cloud::Bigtable::GcRule.max_age(600)
200
+ # rule_union = Google::Cloud::Bigtable::GcRule.union(rule_1, rule_2)
201
+ # cfm.update "cf2", gc_rule: rule_union
202
+ #
203
+ # cfm.delete "cf3"
204
+ # end
205
+ #
206
+ # puts table.column_families["cf3"] #=> nil
207
+ #
208
+ def column_families
209
+ check_view_and_load :SCHEMA_VIEW
210
+
211
+ if block_given?
212
+ column_families = ColumnFamilyMap.from_grpc @grpc.column_families
213
+ yield column_families
214
+ modifications = column_families.modifications @grpc.column_families
215
+ @grpc = service.modify_column_families instance_id, table_id, modifications if modifications.any?
216
+ end
217
+
218
+ ColumnFamilyMap.from_grpc(@grpc.column_families).freeze
219
+ end
220
+
221
+ ##
222
+ # The granularity (e.g. `MILLIS`, `MICROS`) at which timestamps are stored in
223
+ # this table. Timestamps not matching the granularity will be rejected.
224
+ # If unspecified at creation time, the value will be set to `MILLIS`.
225
+ # Views: `SCHEMA_VIEW`, `FULL`.
226
+ #
227
+ # @return [Symbol]
228
+ #
229
+ def granularity
230
+ check_view_and_load :SCHEMA_VIEW
231
+ @grpc.granularity
232
+ end
233
+
234
+ ##
235
+ # The table keeps data versioned at a granularity of 1 ms.
236
+ #
237
+ # @return [Boolean]
238
+ #
239
+ def granularity_millis?
240
+ granularity == :MILLIS
241
+ end
242
+
243
+ ##
244
+ # Gets the [Cloud IAM](https://cloud.google.com/iam/) access control
245
+ # policy for the table.
246
+ #
247
+ # @see https://cloud.google.com/bigtable/docs/access-control
248
+ #
249
+ # @yield [policy] A block for updating the policy. The latest policy
250
+ # will be read from the Bigtable service and passed to the block. After
251
+ # the block completes, the modified policy will be written to the
252
+ # service.
253
+ # @yieldparam [Policy] policy the current Cloud IAM Policy for this
254
+ # table.
255
+ #
256
+ # @return [Policy] The current Cloud IAM Policy for the table.
257
+ #
258
+ # @example
259
+ # require "google/cloud/bigtable"
260
+ #
261
+ # bigtable = Google::Cloud::Bigtable.new
262
+ #
263
+ # table = bigtable.table("my-instance", "my-table", perform_lookup: true)
264
+ # policy = table.policy
265
+ #
266
+ # @example Update the policy by passing a block.
267
+ # require "google/cloud/bigtable"
268
+ #
269
+ # bigtable = Google::Cloud::Bigtable.new
270
+ #
271
+ # table = bigtable.table("my-instance", "my-table", perform_lookup: true)
272
+ #
273
+ # table.policy do |p|
274
+ # p.add("roles/owner", "user:owner@example.com")
275
+ # end # 2 API calls
276
+ #
277
+ def policy
278
+ ensure_service!
279
+ grpc = service.get_table_policy instance_id, name
280
+ policy = Policy.from_grpc grpc
281
+ return policy unless block_given?
282
+ yield policy
283
+ update_policy policy
284
+ end
285
+
286
+ ##
287
+ # Updates the [Cloud IAM](https://cloud.google.com/iam/) access control
288
+ # policy for the table. The policy should be read from {#policy}.
289
+ # See {Google::Cloud::Bigtable::Policy} for an explanation of the policy
290
+ # `etag` property and how to modify policies.
291
+ #
292
+ # You can also update the policy by passing a block to {#policy}, which
293
+ # will call this method internally after the block completes.
294
+ #
295
+ # @param new_policy [Policy] a new or modified Cloud IAM Policy for this
296
+ # table
297
+ #
298
+ # @return [Policy] The policy returned by the API update operation.
299
+ #
300
+ # @example
301
+ # require "google/cloud/bigtable"
302
+ #
303
+ # bigtable = Google::Cloud::Bigtable.new
304
+ #
305
+ # table = bigtable.table("my-instance", "my-table", perform_lookup: true)
306
+ #
307
+ # policy = table.policy
308
+ # policy.add("roles/owner", "user:owner@example.com")
309
+ # updated_policy = table.update_policy(policy)
310
+ #
311
+ # puts updated_policy.roles
312
+ #
313
+ def update_policy new_policy
314
+ ensure_service!
315
+ grpc = service.set_table_policy instance_id, name, new_policy.to_grpc
316
+ Policy.from_grpc grpc
317
+ end
318
+ alias policy= update_policy
319
+
320
+ ##
321
+ # Tests the specified permissions against the [Cloud
322
+ # IAM](https://cloud.google.com/iam/) access control policy.
323
+ #
324
+ # @see https://cloud.google.com/iam/docs/managing-policies Managing Policies
325
+ # @see https://cloud.google.com/bigtable/docs/access-control Access Control
326
+ #
327
+ # @param permissions [String, Array<String>] permissions The set of permissions to
328
+ # check access for. Permissions with wildcards (such as `*` or
329
+ # `bigtable.*`) are not allowed.
330
+ # See [Access Control](https://cloud.google.com/bigtable/docs/access-control).
331
+ #
332
+ # @return [Array<String>] The permissions that are configured for the policy.
333
+ #
334
+ # @example
335
+ # require "google/cloud/bigtable"
336
+ #
337
+ # bigtable = Google::Cloud::Bigtable.new
338
+ #
339
+ # table = bigtable.table("my-instance", "my-table", perform_lookup: true)
340
+ #
341
+ # permissions = table.test_iam_permissions(
342
+ # "bigtable.tables.delete",
343
+ # "bigtable.tables.get"
344
+ # )
345
+ # permissions.include? "bigtable.tables.delete" #=> false
346
+ # permissions.include? "bigtable.tables.get" #=> true
347
+ #
348
+ def test_iam_permissions *permissions
349
+ ensure_service!
350
+ grpc = service.test_table_permissions instance_id, name, permissions.flatten
351
+ grpc.permissions.to_a
352
+ end
353
+
354
+ ##
355
+ # Permanently deletes the table from a instance.
356
+ #
357
+ # @return [Boolean] Returns `true` if the table was deleted.
358
+ #
359
+ # @example
360
+ # require "google/cloud/bigtable"
361
+ #
362
+ # bigtable = Google::Cloud::Bigtable.new
363
+ #
364
+ # table = bigtable.table("my-instance", "my-table")
365
+ # table.delete
366
+ #
367
+ def delete
368
+ ensure_service!
369
+ service.delete_table instance_id, name
370
+ true
371
+ end
372
+
373
+ ##
374
+ # Checks to see if the table exists.
375
+ #
376
+ # @return [Boolean]
377
+ #
378
+ # @example
379
+ # require "google/cloud/bigtable"
380
+ #
381
+ # bigtable = Google::Cloud::Bigtable.new
382
+ #
383
+ # table = bigtable.table("my-instance", "my-table")
384
+ #
385
+ # if table.exists?
386
+ # p "Table exists."
387
+ # else
388
+ # p "Table does not exist"
389
+ # end
390
+ #
391
+ # @example Using Cloud Bigtable instance
392
+ # require "google/cloud/bigtable"
393
+ #
394
+ # bigtable = Google::Cloud::Bigtable.new
395
+ #
396
+ # instance = bigtable.instance("my-instance")
397
+ # table = instance.table("my-table")
398
+ #
399
+ # if table.exists?
400
+ # p "Table exists."
401
+ # else
402
+ # p "Table does not exist"
403
+ # end
404
+ #
405
+ def exists?
406
+ !service.get_table(instance_id, name, view: :NAME_ONLY).nil?
407
+ rescue Google::Cloud::NotFoundError
408
+ false
409
+ end
410
+
411
+ # @private
412
+ # Creates a table.
413
+ #
414
+ # @param service [Google::Cloud::Bigtable::Service]
415
+ # @param instance_id [String]
416
+ # @param table_id [String]
417
+ # @param column_families [ColumnFamilyMap]
418
+ # @param granularity [Symbol]
419
+ # @param initial_splits [Array<String>]
420
+ # @yield [column_families] A block for adding column_families.
421
+ # @yieldparam [ColumnFamilyMap]
422
+ #
423
+ # @return [Google::Cloud::Bigtable::Table]
424
+ #
425
+ def self.create service, instance_id, table_id, column_families: nil, granularity: nil, initial_splits: nil
426
+ if column_families
427
+ # create an un-frozen and duplicate object
428
+ column_families = ColumnFamilyMap.from_grpc column_families.to_grpc
429
+ end
430
+ column_families ||= ColumnFamilyMap.new
431
+
432
+ yield column_families if block_given?
433
+
434
+ table = Google::Bigtable::Admin::V2::Table.new({
435
+ column_families: column_families.to_grpc_hash,
436
+ granularity: granularity
437
+ }.delete_if { |_, v| v.nil? })
438
+
439
+ grpc = service.create_table instance_id, table_id, table, initial_splits: initial_splits
440
+ from_grpc grpc, service
441
+ end
442
+
443
+ ##
444
+ # Generates a consistency token for a table. The token can be used in
445
+ # CheckConsistency to check whether mutations to the table that finished
446
+ # before this call started have been replicated. The tokens will be available
447
+ # for 90 days.
448
+ #
449
+ # @return [String] The generated consistency token
450
+ #
451
+ # @example
452
+ # require "google/cloud/bigtable"
453
+ #
454
+ # bigtable = Google::Cloud::Bigtable.new
455
+ #
456
+ # instance = bigtable.instance("my-instance")
457
+ # table = instance.table("my-table")
458
+ #
459
+ # table.generate_consistency_token # "l947XelENinaxJQP0nnrZJjHnAF7YrwW8HCJLotwrF"
460
+ #
461
+ def generate_consistency_token
462
+ ensure_service!
463
+ response = service.generate_consistency_token instance_id, name
464
+ response.consistency_token
465
+ end
466
+
467
+ ##
468
+ # Checks replication consistency based on a consistency token. Replication is
469
+ # considered consistent if replication has caught up based on the conditions
470
+ # specified in the token and the check request.
471
+ # @param token [String] Consistency token
472
+ # @return [Boolean] `true` if replication is consistent
473
+ #
474
+ # @example
475
+ # require "google/cloud/bigtable"
476
+ #
477
+ # bigtable = Google::Cloud::Bigtable.new
478
+ #
479
+ # instance = bigtable.instance("my-instance")
480
+ # table = instance.table("my-table")
481
+ #
482
+ # token = "l947XelENinaxJQP0nnrZJjHnAF7YrwW8HCJLotwrF"
483
+ #
484
+ # if table.check_consistency(token)
485
+ # puts "Replication is consistent"
486
+ # end
487
+ #
488
+ def check_consistency token
489
+ ensure_service!
490
+ response = service.check_consistency instance_id, name, token
491
+ response.consistent
492
+ end
493
+
494
+ ##
495
+ # Wait for replication to check replication consistency.
496
+ # Checks replication consistency by generating a consistency token and
497
+ # making the `check_consistency` API call 5 times (by default).
498
+ # If the response is consistent, returns `true`. Otherwise tries again
499
+ # repeatedly until the timeout. If the check does not succeed by the
500
+ # timeout, returns `false`.
501
+ #
502
+ # @param timeout [Integer]
503
+ # Timeout in seconds. Defaults value is 600 seconds.
504
+ # @param check_interval [Integer]
505
+ # Consistency check interval in seconds. Default is 5 seconds.
506
+ # @return [Boolean] `true` if replication is consistent
507
+ #
508
+ # @example
509
+ # require "google/cloud/bigtable"
510
+ #
511
+ # bigtable = Google::Cloud::Bigtable.new
512
+ #
513
+ # table = bigtable.table("my-instance", "my-table", perform_lookup: true)
514
+ #
515
+ # if table.wait_for_replication
516
+ # puts "Replication done"
517
+ # end
518
+ #
519
+ # # With custom timeout and interval
520
+ # if table.wait_for_replication(timeout: 300, check_interval: 10)
521
+ # puts "Replication done"
522
+ # end
523
+ #
524
+ def wait_for_replication timeout: 600, check_interval: 5
525
+ raise InvalidArgumentError, "'check_interval' cannot be greater than timeout" if check_interval > timeout
526
+ token = generate_consistency_token
527
+ status = false
528
+ start_at = Time.now
529
+
530
+ loop do
531
+ status = check_consistency token
532
+
533
+ break if status || (Time.now - start_at) >= timeout
534
+ sleep check_interval
535
+ end
536
+ status
537
+ end
538
+
539
+ ##
540
+ # Deletes all rows.
541
+ #
542
+ # @param timeout [Integer] Call timeout in seconds.
543
+ # Use in case of insufficient deadline for DropRowRange, then
544
+ # try again with a longer request deadline.
545
+ # @return [Boolean]
546
+ #
547
+ # @example
548
+ # require "google/cloud/bigtable"
549
+ #
550
+ # bigtable = Google::Cloud::Bigtable.new
551
+ #
552
+ # instance = bigtable.instance("my-instance")
553
+ # table = instance.table("my-table")
554
+ # table.delete_all_rows
555
+ #
556
+ # # With timeout
557
+ # table.delete_all_rows(timeout: 120) # 120 seconds.
558
+ #
559
+ def delete_all_rows timeout: nil
560
+ drop_row_range delete_all_data: true, timeout: timeout
561
+ end
562
+
563
+ ##
564
+ # Deletes rows using row key prefix.
565
+ #
566
+ # @param prefix [String] Row key prefix (for example, "user").
567
+ # @param timeout [Integer] Call timeout in seconds.
568
+ # @return [Boolean]
569
+ # @example
570
+ # require "google/cloud/bigtable"
571
+ #
572
+ # bigtable = Google::Cloud::Bigtable.new
573
+ #
574
+ # table = bigtable.table("my-instance", "my-table")
575
+ #
576
+ # table.delete_rows_by_prefix("user-100")
577
+ #
578
+ # # With timeout
579
+ # table.delete_rows_by_prefix("user-1", timeout: 120) # 120 seconds.
580
+ #
581
+ def delete_rows_by_prefix prefix, timeout: nil
582
+ drop_row_range row_key_prefix: prefix, timeout: timeout
583
+ end
584
+
585
+ ##
586
+ # Drops row range by row key prefix or deletes all.
587
+ #
588
+ # @param row_key_prefix [String] Row key prefix (for example, "user").
589
+ # @param delete_all_data [Boolean]
590
+ # @return [Boolean]
591
+ #
592
+ # @example
593
+ # require "google/cloud/bigtable"
594
+ #
595
+ # bigtable = Google::Cloud::Bigtable.new
596
+ #
597
+ # table = bigtable.table("my-instance", "my-table")
598
+ #
599
+ # # Delete rows using row key prefix.
600
+ # table.drop_row_range(row_key_prefix: "user-100")
601
+ #
602
+ # # Delete all data With timeout
603
+ # table.drop_row_range(delete_all_data: true, timeout: 120) # 120 seconds.
604
+ #
605
+ def drop_row_range row_key_prefix: nil, delete_all_data: nil, timeout: nil
606
+ ensure_service!
607
+ service.drop_row_range(
608
+ instance_id,
609
+ name,
610
+ row_key_prefix: row_key_prefix,
611
+ delete_all_data_from_table: delete_all_data,
612
+ timeout: timeout
613
+ )
614
+ true
615
+ end
616
+
617
+ # @private
618
+ # Creates a new Table instance from a Google::Bigtable::Admin::V2::Table.
619
+ #
620
+ # @param grpc [Google::Bigtable::Admin::V2::Table]
621
+ # @param service [Google::Cloud::Bigtable::Service]
622
+ # @param view [Symbol] View type.
623
+ # @return [Google::Cloud::Bigtable::Table]
624
+ #
625
+ def self.from_grpc grpc, service, view: nil
626
+ new grpc, service, view: view
627
+ end
628
+
629
+ # @private
630
+ # Creates a new Table object from table path.
631
+ #
632
+ # @param path [String] Table path.
633
+ # Formatted table path
634
+ # +projects/<project>/instances/<instance>/tables/<table>+
635
+ # @param service [Google::Cloud::Bigtable::Service]
636
+ # @return [Google::Cloud::Bigtable::Table]
637
+ #
638
+ def self.from_path path, service
639
+ grpc = Google::Bigtable::Admin::V2::Table.new name: path
640
+ new grpc, service, view: :NAME_ONLY
641
+ end
642
+
643
+ protected
644
+
645
+ # @private
646
+ # Raises an error unless an active connection to the service is
647
+ # available.
648
+ #
649
+ def ensure_service!
650
+ raise "Must have active connection to service" unless service
651
+ end
652
+
653
+ FIELDS_BY_VIEW = {
654
+ SCHEMA_VIEW: ["granularity", "column_families"],
655
+ REPLICATION_VIEW: ["cluster_states"],
656
+ FULL: ["granularity", "column_families", "cluster_states"]
657
+ }.freeze
658
+
659
+ # @private
660
+ #
661
+ # Checks and reloads table with expected view and sets fields.
662
+ # @param view [Symbol] Expected view type.
663
+ #
664
+ def check_view_and_load view
665
+ ensure_service!
666
+ @loaded_views ||= Set.new [@view]
667
+
668
+ return if @loaded_views.include?(view) || @loaded_views.include?(:FULL)
669
+
670
+ grpc = service.get_table instance_id, table_id, view: view
671
+ @loaded_views << view
672
+
673
+ FIELDS_BY_VIEW[view].each do |field|
674
+ case grpc[field]
675
+ when Google::Protobuf::Map
676
+ @grpc[field].clear
677
+ grpc[field].each { |k, v| @grpc[field][k] = v }
678
+ else
679
+ @grpc[field] = grpc[field]
680
+ end
681
+ end
682
+ end
683
+ end
684
+ end
685
+ end
686
+ end