google-cloud-bigtable 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (88) hide show
  1. checksums.yaml +7 -0
  2. data/.yardopts +10 -0
  3. data/LICENSE +201 -0
  4. data/README.md +65 -0
  5. data/lib/google/bigtable/admin/v2/bigtable_instance_admin_pb.rb +139 -0
  6. data/lib/google/bigtable/admin/v2/bigtable_instance_admin_services_pb.rb +85 -0
  7. data/lib/google/bigtable/admin/v2/bigtable_table_admin_pb.rb +137 -0
  8. data/lib/google/bigtable/admin/v2/bigtable_table_admin_services_pb.rb +117 -0
  9. data/lib/google/bigtable/admin/v2/common_pb.rb +24 -0
  10. data/lib/google/bigtable/admin/v2/instance_pb.rb +72 -0
  11. data/lib/google/bigtable/admin/v2/table_pb.rb +88 -0
  12. data/lib/google/bigtable/v2/bigtable_pb.rb +109 -0
  13. data/lib/google/bigtable/v2/bigtable_services_pb.rb +67 -0
  14. data/lib/google/bigtable/v2/data_pb.rb +155 -0
  15. data/lib/google/cloud/bigtable/admin/credentials.rb +26 -0
  16. data/lib/google/cloud/bigtable/admin/v2/bigtable_instance_admin_client.rb +1417 -0
  17. data/lib/google/cloud/bigtable/admin/v2/bigtable_instance_admin_client_config.json +123 -0
  18. data/lib/google/cloud/bigtable/admin/v2/bigtable_table_admin_client.rb +1079 -0
  19. data/lib/google/cloud/bigtable/admin/v2/bigtable_table_admin_client_config.json +109 -0
  20. data/lib/google/cloud/bigtable/admin/v2/credentials.rb +50 -0
  21. data/lib/google/cloud/bigtable/admin/v2/doc/google/bigtable/admin/v2/bigtable_instance_admin.rb +279 -0
  22. data/lib/google/cloud/bigtable/admin/v2/doc/google/bigtable/admin/v2/bigtable_table_admin.rb +353 -0
  23. data/lib/google/cloud/bigtable/admin/v2/doc/google/bigtable/admin/v2/instance.rb +194 -0
  24. data/lib/google/cloud/bigtable/admin/v2/doc/google/bigtable/admin/v2/table.rb +209 -0
  25. data/lib/google/cloud/bigtable/admin/v2/doc/google/iam/v1/iam_policy.rb +62 -0
  26. data/lib/google/cloud/bigtable/admin/v2/doc/google/iam/v1/policy.rb +127 -0
  27. data/lib/google/cloud/bigtable/admin/v2/doc/google/longrunning/operations.rb +92 -0
  28. data/lib/google/cloud/bigtable/admin/v2/doc/google/protobuf/any.rb +124 -0
  29. data/lib/google/cloud/bigtable/admin/v2/doc/google/protobuf/duration.rb +90 -0
  30. data/lib/google/cloud/bigtable/admin/v2/doc/google/protobuf/empty.rb +28 -0
  31. data/lib/google/cloud/bigtable/admin/v2/doc/google/protobuf/field_mask.rb +223 -0
  32. data/lib/google/cloud/bigtable/admin/v2/doc/google/protobuf/timestamp.rb +106 -0
  33. data/lib/google/cloud/bigtable/admin/v2/doc/google/rpc/status.rb +83 -0
  34. data/lib/google/cloud/bigtable/admin/v2.rb +200 -0
  35. data/lib/google/cloud/bigtable/admin.rb +196 -0
  36. data/lib/google/cloud/bigtable/app_profile/job.rb +102 -0
  37. data/lib/google/cloud/bigtable/app_profile/list.rb +159 -0
  38. data/lib/google/cloud/bigtable/app_profile.rb +373 -0
  39. data/lib/google/cloud/bigtable/chunk_processor.rb +253 -0
  40. data/lib/google/cloud/bigtable/cluster/job.rb +92 -0
  41. data/lib/google/cloud/bigtable/cluster/list.rb +169 -0
  42. data/lib/google/cloud/bigtable/cluster.rb +264 -0
  43. data/lib/google/cloud/bigtable/column_family.rb +280 -0
  44. data/lib/google/cloud/bigtable/column_range.rb +186 -0
  45. data/lib/google/cloud/bigtable/convert.rb +75 -0
  46. data/lib/google/cloud/bigtable/credentials.rb +24 -0
  47. data/lib/google/cloud/bigtable/errors.rb +35 -0
  48. data/lib/google/cloud/bigtable/gc_rule.rb +215 -0
  49. data/lib/google/cloud/bigtable/instance/cluster_map.rb +70 -0
  50. data/lib/google/cloud/bigtable/instance/job.rb +97 -0
  51. data/lib/google/cloud/bigtable/instance/list.rb +159 -0
  52. data/lib/google/cloud/bigtable/instance.rb +921 -0
  53. data/lib/google/cloud/bigtable/longrunning_job.rb +105 -0
  54. data/lib/google/cloud/bigtable/mutation_entry.rb +244 -0
  55. data/lib/google/cloud/bigtable/mutation_operations.rb +338 -0
  56. data/lib/google/cloud/bigtable/policy.rb +163 -0
  57. data/lib/google/cloud/bigtable/project.rb +580 -0
  58. data/lib/google/cloud/bigtable/read_modify_write_rule.rb +129 -0
  59. data/lib/google/cloud/bigtable/read_operations.rb +345 -0
  60. data/lib/google/cloud/bigtable/row.rb +125 -0
  61. data/lib/google/cloud/bigtable/row_filter/chain_filter.rb +539 -0
  62. data/lib/google/cloud/bigtable/row_filter/condition_filter.rb +108 -0
  63. data/lib/google/cloud/bigtable/row_filter/interleave_filter.rb +570 -0
  64. data/lib/google/cloud/bigtable/row_filter/simple_filter.rb +273 -0
  65. data/lib/google/cloud/bigtable/row_filter.rb +593 -0
  66. data/lib/google/cloud/bigtable/row_range.rb +174 -0
  67. data/lib/google/cloud/bigtable/rows_mutator.rb +120 -0
  68. data/lib/google/cloud/bigtable/rows_reader.rb +196 -0
  69. data/lib/google/cloud/bigtable/sample_row_key.rb +82 -0
  70. data/lib/google/cloud/bigtable/service.rb +817 -0
  71. data/lib/google/cloud/bigtable/table/cluster_state.rb +93 -0
  72. data/lib/google/cloud/bigtable/table/column_family_map.rb +68 -0
  73. data/lib/google/cloud/bigtable/table/list.rb +147 -0
  74. data/lib/google/cloud/bigtable/table.rb +676 -0
  75. data/lib/google/cloud/bigtable/v2/bigtable_client.rb +579 -0
  76. data/lib/google/cloud/bigtable/v2/bigtable_client_config.json +65 -0
  77. data/lib/google/cloud/bigtable/v2/credentials.rb +45 -0
  78. data/lib/google/cloud/bigtable/v2/doc/google/bigtable/v2/bigtable.rb +286 -0
  79. data/lib/google/cloud/bigtable/v2/doc/google/bigtable/v2/data.rb +492 -0
  80. data/lib/google/cloud/bigtable/v2/doc/google/protobuf/any.rb +124 -0
  81. data/lib/google/cloud/bigtable/v2/doc/google/protobuf/wrappers.rb +89 -0
  82. data/lib/google/cloud/bigtable/v2/doc/google/rpc/status.rb +83 -0
  83. data/lib/google/cloud/bigtable/v2.rb +132 -0
  84. data/lib/google/cloud/bigtable/value_range.rb +175 -0
  85. data/lib/google/cloud/bigtable/version.rb +22 -0
  86. data/lib/google/cloud/bigtable.rb +223 -0
  87. data/lib/google-cloud-bigtable.rb +167 -0
  88. metadata +283 -0
@@ -0,0 +1,93 @@
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
+ module Google
19
+ module Cloud
20
+ module Bigtable
21
+ class Table
22
+ # Table::ClusterState is a The state of a table's data in a particular cluster.
23
+ class ClusterState
24
+ attr_reader :cluster_name
25
+
26
+ # @private
27
+ # Create a new Table::ClusterState
28
+ #
29
+ def initialize grpc, cluster_name
30
+ @grpc = grpc
31
+ @cluster_name = cluster_name
32
+ end
33
+
34
+ # The state of replication for the table in this cluster.
35
+ # Valid values are:
36
+ # * `:INITIALIZING` - The cluster was recently created.
37
+ # * `:PLANNED_MAINTENANCE` - The table is temporarily unable to serve.
38
+ # * `:UNPLANNED_MAINTENANCE` - The table is temporarily unable to serve.
39
+ # * `:READY` - The table can serve.
40
+ # @return [Symbol]
41
+ #
42
+ def replication_state
43
+ @grpc.replication_state
44
+ end
45
+
46
+ # The cluster was recently created, and the table must finish copying
47
+ # over pre-existing data from other clusters before it can begin
48
+ # receiving live replication updates and serving.
49
+ # @return [Boolean]
50
+ #
51
+ def initializing?
52
+ replication_state == :INITIALIZING
53
+ end
54
+
55
+ # The table is temporarily unable to serve
56
+ # requests from this cluster due to planned internal maintenance.
57
+ # @return [Boolean]
58
+ #
59
+ def planned_maintenance?
60
+ replication_state == :PLANNED_MAINTENANCE
61
+ end
62
+
63
+ # The table is temporarily unable to serve requests from this
64
+ # cluster due to unplanned or emergency maintenance.
65
+ # @return [Boolean]
66
+ #
67
+ def unplanned_maintenance?
68
+ replication_state == :UNPLANNED_MAINTENANCE
69
+ end
70
+
71
+ # The table can serve requests from this cluster.
72
+ # Depending on replication delay, reads may not immediately
73
+ # reflect the state of the table in other clusters.
74
+ # @return [Boolean]
75
+ #
76
+ def ready?
77
+ replication_state == :READY
78
+ end
79
+
80
+ # @private
81
+ # New Table::ClusterState from a Google::Bigtable::Admin::V2::Table::ClusterState object.
82
+ # @param grpc [Google::Bigtable::Admin::V2::Table::ClusterState]
83
+ # @param cluster_name [String]
84
+ # @return [Google::Cloud::Bigtable::Table::ClusterState]
85
+ #
86
+ def self.from_grpc grpc, cluster_name
87
+ new(grpc, cluster_name)
88
+ end
89
+ end
90
+ end
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,68 @@
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
+ module Google
19
+ module Cloud
20
+ module Bigtable
21
+ class Table
22
+ # Table::ColumnFamilyMap is a Hash with column_family name and grpc object.
23
+ # It is used to create instance.
24
+ # @example Add column family with name and GC rule
25
+ #
26
+ # column_families = Google::Cloud::Bigtable::Instance::ColumnFamilyMap.new
27
+ #
28
+ # column_families.add('cf1', Google::Cloud::Bigtable::GcRule.max_versions(3))
29
+ #
30
+ class ColumnFamilyMap < DelegateClass(::Hash)
31
+ # @private
32
+ # Create a new ColumnFamilyMap.
33
+ def initialize value = {}
34
+ super(value)
35
+ end
36
+
37
+ # Add column family.
38
+ #
39
+ # @param name [String] Column family name
40
+ # @param gc_rule [Google::Bigtable::GcRule] GC Rule
41
+ # @example
42
+ # column_families = Google::Cloud::Bigtable::Instance::ColumnFamilyMap.new
43
+ #
44
+ # gc_rule_1 = Google::Cloud::Bigtable::GcRule.max_versions(3)
45
+ # column_families.add('cf1', gc_rule_1)
46
+ #
47
+ # gc_rule = Google::Cloud::Bigtable::GcRule.max_age(1800)
48
+ # column_families.add('cf2', gc_rule)
49
+
50
+ def add name, gc_rule
51
+ self[name] = Google::Bigtable::Admin::V2::ColumnFamily.new(
52
+ gc_rule: gc_rule.to_grpc
53
+ )
54
+ end
55
+
56
+ # Remove column family from map.
57
+ #
58
+ # @param name [String] Column family name
59
+ # @return [Google::Bigtable::Admin::V2::ColumnFamily]
60
+
61
+ def remove name
62
+ delete(name)
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,147 @@
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
+ module Google
19
+ module Cloud
20
+ module Bigtable
21
+ class Table
22
+ # Table::List is a special case Array with additional
23
+ # values.
24
+ class List < DelegateClass(::Array)
25
+ # @private
26
+ # The gRPC Service object.
27
+ attr_accessor :service
28
+
29
+ # @private
30
+ # The gRPC page enumbrable object.
31
+ attr_accessor :grpc
32
+
33
+ # @private
34
+ # Create a new Table::List with an array of table instances.
35
+ def initialize arr = []
36
+ super(arr)
37
+ end
38
+
39
+ # Whether there is a next page of tables.
40
+ #
41
+ # @return [Boolean]
42
+ #
43
+ # @example
44
+ # require "google/cloud/bigtable"
45
+ #
46
+ # bigtable = Google::Cloud::Bigtable.new
47
+ #
48
+ # tables = bigtable.tables("my-instance")
49
+ # if tables.next?
50
+ # next_tables = tables.next
51
+ # end
52
+ def next?
53
+ grpc.next_page?
54
+ end
55
+
56
+ # Retrieve the next page of tables.
57
+ #
58
+ # @return [Table::List] The list of table instances.
59
+ #
60
+ # @example
61
+ # require "google/cloud/bigtable"
62
+ #
63
+ # bigtable = Google::Cloud::Bigtable.new
64
+ #
65
+ # tables = bigtable.tables("my-instance")
66
+ # if tables.next?
67
+ # next_tables = tables.next
68
+ # end
69
+ def next
70
+ ensure_grpc!
71
+
72
+ return nil unless next?
73
+ grpc.next_page
74
+ self.class.from_grpc(grpc, service)
75
+ end
76
+
77
+ # Retrieves remaining results by repeatedly invoking {#next} until
78
+ # {#next?} returns `false`. Calls the given block once for each
79
+ # result, which is passed as the argument to the block.
80
+ #
81
+ # An Enumerator is returned if no block is given.
82
+ #
83
+ # This method will make repeated API calls until all remaining results
84
+ # are retrieved. (Unlike `#each`, for example, which merely iterates
85
+ # over the results returned by a single API call.) Use with caution.
86
+ #
87
+ # @yield [table] The block for accessing each table instance.
88
+ # @yieldparam [Table] instance The table instance object.
89
+ #
90
+ # @return [Enumerator]
91
+ #
92
+ # @example Iterating each instance by passing a block:
93
+ # require "google/cloud/bigtable"
94
+ #
95
+ # bigtable = Google::Cloud::Bigtable.new
96
+ #
97
+ # bigtable.tables("instance-id").all do |table|
98
+ # puts table.table_id
99
+ # end
100
+ #
101
+ # @example Using the enumerator by not passing a block:
102
+ # require "google/cloud/bigtable"
103
+ #
104
+ # bigtable = Google::Cloud::Bigtable.new
105
+ #
106
+ # all_table_ids = bigtable.tables("instance-id").all.map do |table|
107
+ # puts table.table_id
108
+ # end
109
+ #
110
+ def all
111
+ return enum_for(:all) unless block_given?
112
+
113
+ results = self
114
+ loop do
115
+ results.each { |r| yield r }
116
+ break unless next?
117
+ grpc.next_page
118
+ results = self.class.from_grpc(grpc, service)
119
+ end
120
+ end
121
+
122
+ # @private
123
+ # New Table::List from a Google::Gax::PagedEnumerable<Google::Bigtable::Admin::V2::Table> object.
124
+ #
125
+ def self.from_grpc grpc, service
126
+ tables = List.new(Array(grpc.response.tables).map do |table|
127
+ Table.from_grpc(table, service, view: :NAME_ONLY)
128
+ end)
129
+ tables.grpc = grpc
130
+ tables.service = service
131
+ tables
132
+ end
133
+
134
+ protected
135
+
136
+ # @private
137
+ #
138
+ # Raise an error unless an active grpc call is available.
139
+ #
140
+ def ensure_grpc!
141
+ raise "Must have grpc call" unless grpc
142
+ end
143
+ end
144
+ end
145
+ end
146
+ end
147
+ end
@@ -0,0 +1,676 @@
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"
21
+ require "google/cloud/bigtable/table/column_family_map"
22
+ require "google/cloud/bigtable/gc_rule"
23
+ require "google/cloud/bigtable/mutation_operations"
24
+ require "google/cloud/bigtable/read_operations"
25
+
26
+ module Google
27
+ module Cloud
28
+ module Bigtable
29
+ # # Table
30
+ #
31
+ # A collection of user data indexed by row, column, and timestamp.
32
+ # Each table is served using the resources of its parent cluster.
33
+ #
34
+ # @example
35
+ # require "google/cloud/bigtable"
36
+ #
37
+ # bigtable = Google::Cloud::Bigtable.new
38
+ #
39
+ # table = bigtable.table("my-instance", "my-table", perform_lookup: true)
40
+ #
41
+ # table.column_families.each do |cf|
42
+ # p cf.name
43
+ # p cf.gc_rule
44
+ # end
45
+ #
46
+ # # Get column family by name
47
+ # cf1 = table.column_families.find_by_name("cf1")
48
+ #
49
+ # # Create column family
50
+ # gc_rule = Google::Cloud::Bigtable::GcRule.max_versions(3)
51
+ # cf2 = table.column_families.create("cf2", gc_rule)
52
+ #
53
+ # # Delete table
54
+ # table.delete
55
+ #
56
+ class Table
57
+ # @!parse extend MutationOperations
58
+ include MutationOperations
59
+
60
+ # @!parse extend ReadOperations
61
+ include ReadOperations
62
+
63
+ # @private
64
+ # The gRPC Service object.
65
+ attr_accessor :service
66
+
67
+ # @return [String] App profile id for request routing.
68
+ attr_accessor :app_profile_id
69
+
70
+ # @private
71
+ #
72
+ # Creates a new Table instance.
73
+ def initialize grpc, service, view: nil
74
+ @grpc = grpc
75
+ @service = service
76
+ @view = view || :SCHEMA_VIEW
77
+ end
78
+
79
+ # The unique identifier for the project.
80
+ #
81
+ # @return [String]
82
+ def project_id
83
+ @grpc.name.split("/")[1]
84
+ end
85
+
86
+ # The unique identifier for the instance.
87
+ #
88
+ # @return [String]
89
+ def instance_id
90
+ @grpc.name.split("/")[3]
91
+ end
92
+
93
+ # The unique identifier for the table.
94
+ #
95
+ # @return [String]
96
+ def name
97
+ @grpc.name.split("/")[5]
98
+ end
99
+ alias table_id name
100
+
101
+ # The full path for the table resource. Values are of the form
102
+ # `projects/<project_id>/instances/<instance_id>/table/<table_id>`.
103
+ #
104
+ # @return [String]
105
+ def path
106
+ @grpc.name
107
+ end
108
+
109
+ # Reload table information.
110
+ #
111
+ # @param view [Symbol] Table view type.
112
+ # Default view type is `:SCHEMA_VIEW`
113
+ # Valid view types are.
114
+ #
115
+ # * `:NAME_ONLY` - Only populates `name`
116
+ # * `:SCHEMA_VIEW` - Only populates `name` and fields related to the table's schema
117
+ # * `:REPLICATION_VIEW` - Only populates `name` and fields related to the table's replication state.
118
+ # * `:FULL` - Populates all fields
119
+ #
120
+ # @return [Google::Cloud::Bigtable::Table]
121
+
122
+ def reload! view: nil
123
+ @view = view || :SCHEMA_VIEW
124
+ @grpc = service.get_table(instance_id, name, view: view)
125
+ self
126
+ end
127
+
128
+ # Map from cluster ID to per-cluster table state.
129
+ # If it could not be determined whether or not the table has data in a
130
+ # particular cluster (for example, if its zone is unavailable), then
131
+ # there will be an entry for the cluster with UNKNOWN `replication_status`.
132
+ # Views: `FULL`
133
+ #
134
+ # @return [Array<Google::Cloud::Bigtable::Table::ClusterState>]
135
+ def cluster_states
136
+ check_view_and_load(:REPLICATION_VIEW)
137
+ @grpc.cluster_states.map do |name, state_grpc|
138
+ ClusterState.from_grpc(state_grpc, name)
139
+ end
140
+ end
141
+
142
+ # The column families configured for this table, mapped by column family ID.
143
+ # Available column families data only in table view types: `SCHEMA_VIEW`, `FULL`
144
+ #
145
+ #
146
+ # @return [Array<Google::Bigtable::ColumnFamily>]
147
+ #
148
+ def column_families
149
+ check_view_and_load(:SCHEMA_VIEW)
150
+ @grpc.column_families.map do |cf_name, cf_grpc|
151
+ ColumnFamily.from_grpc(
152
+ cf_grpc,
153
+ service,
154
+ name: cf_name,
155
+ instance_id: instance_id,
156
+ table_id: table_id
157
+ )
158
+ end
159
+ end
160
+
161
+ # The granularity (e.g. `MILLIS`, `MICROS`) at which timestamps are stored in
162
+ # this table. Timestamps not matching the granularity will be rejected.
163
+ # If unspecified at creation time, the value will be set to `MILLIS`.
164
+ # Views: `SCHEMA_VIEW`, `FULL`
165
+ #
166
+ # @return [Symbol]
167
+ #
168
+ def granularity
169
+ check_view_and_load(:SCHEMA_VIEW)
170
+ @grpc.granularity
171
+ end
172
+
173
+ # The table keeps data versioned at a granularity of 1ms.
174
+ #
175
+ # @return [Boolean]
176
+ #
177
+ def granularity_millis?
178
+ granularity == :MILLIS
179
+ end
180
+
181
+ # Permanently deletes the table from a instance.
182
+ #
183
+ # @return [Boolean] Returns `true` if the table was deleted.
184
+ #
185
+ # @example
186
+ # require "google/cloud/bigtable"
187
+ #
188
+ # bigtable = Google::Cloud::Bigtable.new
189
+ #
190
+ # instance = bigtable.table("my-instance", "my-table")
191
+ # table.delete
192
+ #
193
+ def delete
194
+ ensure_service!
195
+ service.delete_table(instance_id, name)
196
+ true
197
+ end
198
+
199
+ # Check table existence.
200
+ #
201
+ # @return [Boolean]
202
+ #
203
+ # @example
204
+ # require "google/cloud/bigtable"
205
+ #
206
+ # bigtable = Google::Cloud::Bigtable.new
207
+ #
208
+ # table = bigtable.table("my-instance", "my-table")
209
+ #
210
+ # if table.exists?
211
+ # p "Table is exists."
212
+ # else
213
+ # p "Table is not exists"
214
+ # end
215
+ #
216
+ # @example Using bigtable instance
217
+ # require "google/cloud/bigtable"
218
+ #
219
+ # bigtable = Google::Cloud::Bigtable.new
220
+ #
221
+ # instance = bigtable.instance("my-instance")
222
+ # table = bigtable.table("my-table")
223
+ #
224
+ # if table.exists?
225
+ # p "Table is exists."
226
+ # else
227
+ # p "Table is not exists"
228
+ # end
229
+ #
230
+
231
+ def exists?
232
+ !service.get_table(instance_id, name, view: :NAME_ONLY).nil?
233
+ rescue Google::Cloud::NotFoundError
234
+ false
235
+ end
236
+
237
+ # Create column family object to perform create,update or delete operation.
238
+ #
239
+ # @param name [String] Name of the column family
240
+ # @param gc_rule [Google::Cloud::Bigtable::GcRule] Optional.
241
+ # GC Rule only required for create and update.
242
+ #
243
+ # @example Create column family
244
+ # require "google/cloud/bigtable"
245
+ #
246
+ # bigtable = Google::Cloud::Bigtable.new
247
+ #
248
+ # table = bigtable.table("my-instance", my-table)
249
+ #
250
+ # # OR get table from Instance object.
251
+ # instance = bigtable.instance("my-instance")
252
+ # table = instance.table("my-table")
253
+ #
254
+ # gc_rule = Google::Cloud::Bigtable::GcRule.max_versions(5)
255
+ # column_family = table.column_family("cf1", gc_rule)
256
+ # column_family.create
257
+ #
258
+ # @example Update column family
259
+ # require "google/cloud/bigtable"
260
+ #
261
+ # bigtable = Google::Cloud::Bigtable.new
262
+ #
263
+ # table = bigtable.table("my-instance", "my-table")
264
+ #
265
+ # gc_rule = Google::Cloud::Bigtable::GcRule.max_age(1800)
266
+ # column_family = table.column_family("cf2", gc_rule)
267
+ # column_family.save
268
+ # # OR Using alias method update.
269
+ # column_family.update
270
+ #
271
+ # @example Delete column family
272
+ # require "google/cloud/bigtable"
273
+ #
274
+ # bigtable = Google::Cloud::Bigtable.new
275
+ #
276
+ # table = bigtable.table("my-instance", "my-table")
277
+ #
278
+ # column_family = table.column_family("cf3")
279
+ # column_family.delete
280
+ #
281
+ def column_family name, gc_rule = nil
282
+ cf_grpc = Google::Bigtable::Admin::V2::ColumnFamily.new
283
+ cf_grpc.gc_rule = gc_rule.to_grpc if gc_rule
284
+
285
+ ColumnFamily.from_grpc(
286
+ cf_grpc,
287
+ service,
288
+ name: name,
289
+ instance_id: instance_id,
290
+ table_id: table_id
291
+ )
292
+ end
293
+
294
+ # Apply multitple column modifications
295
+ # Performs a series of column family modifications on the specified table.
296
+ # Either all or none of the modifications will occur before this method
297
+ # returns, but data requests received prior to that point may see a table
298
+ # where only some modifications have taken effect.
299
+ #
300
+ # @param modifications [Array<Google::Cloud::Bigtable::ColumnFamilyModification>]
301
+ # Modifications to be atomically applied to the specified table's families.
302
+ # Entries are applied in order, meaning that earlier modifications can be
303
+ # masked by later ones (in the case of repeated updates to the same family,
304
+ # for example).
305
+ # @return [Google::Cloud::Bigtable::Table] Table with updated column families.
306
+ #
307
+ # @example Apply multiple modificationss
308
+ # require "google/cloud/bigtable"
309
+ #
310
+ # bigtable = Google::Cloud::Bigtable.new
311
+ #
312
+ # instance = bigtable.instance("my-instance")
313
+ # table = instance.table("my-table")
314
+ #
315
+ # modifications = []
316
+ # modifications << Google::Cloud::Bigtable::ColumnFamily.create_modification(
317
+ # "cf1", Google::Cloud::Bigtable::GcRule.max_age(600))
318
+ # )
319
+ #
320
+ # modifications << Google::Cloud::Bigtable::ColumnFamily.update_modification(
321
+ # "cf2", Google::Cloud::Bigtable::GcRule.max_versions(5)
322
+ # )
323
+ #
324
+ # gc_rule_1 = Google::Cloud::Bigtable::GcRule.max_versions(3)
325
+ # gc_rule_2 = Google::Cloud::Bigtable::GcRule.max_age(600)
326
+ # modifications << Google::Cloud::Bigtable::ColumnFamily.update_modification(
327
+ # "cf3", Google::Cloud::Bigtable::GcRule.union(gc_rule_1, gc_rule_2)
328
+ # )
329
+ #
330
+ # max_age_gc_rule = Google::Cloud::Bigtable::GcRule.max_age(300)
331
+ # modifications << Google::Cloud::Bigtable::ColumnFamily.update_modification(
332
+ # "cf4", Google::Cloud::Bigtable::GcRule.union(max_version_gc_rule)
333
+ # )
334
+ #
335
+ # modifications << Google::Cloud::Bigtable::ColumnFamily.drop_modification("cf5")
336
+ #
337
+ # table = bigtable.modify_column_families(modifications)
338
+ #
339
+ # p table.column_families
340
+
341
+ def modify_column_families modifications
342
+ ensure_service!
343
+ self.class.modify_column_families(
344
+ service,
345
+ instance_id,
346
+ table_id,
347
+ modifications
348
+ )
349
+ end
350
+
351
+ # @private
352
+ #
353
+ # Performs a series of column family modifications on the specified table.
354
+ # Either all or none of the modifications will occur before this method
355
+ # returns, but data requests received prior to that point may see a table
356
+ # where only some modifications have taken effect.
357
+ #
358
+ # @param service [Google::Cloud::Bigtable::Service]
359
+ # @param instance_id [String]
360
+ # The unique Id of the instance in which table is exists.
361
+ # @param table_id [String]
362
+ # The unique Id of the table whose families should be modified.
363
+ # @param modifications [Array<Google::Bigtable::Admin::V2::ModifyColumnFamiliesRequest::Modification> | Google::Bigtable::Admin::V2::ModifyColumnFamiliesRequest::Modification]
364
+ # Modifications to be atomically applied to the specified table's families.
365
+ # Entries are applied in order, meaning that earlier modifications can be
366
+ # masked by later ones (in the case of repeated updates to the same family,
367
+ # for example).
368
+ # @return [Google::Cloud::Bigtable::Table] Table with updated column families.
369
+ #
370
+ def self.modify_column_families \
371
+ service,
372
+ instance_id,
373
+ table_id,
374
+ modifications
375
+ modifications = [modifications] unless modifications.is_a?(Array)
376
+ grpc = service.modify_column_families(
377
+ instance_id,
378
+ table_id,
379
+ modifications
380
+ )
381
+ from_grpc(grpc, service)
382
+ end
383
+
384
+ # @private
385
+ # Creates a table.
386
+ #
387
+ # @param service [Google::Cloud::Bigtable::Service]
388
+ # @param instance_id [String]
389
+ # @param table_id [String]
390
+ # @param column_families [Hash{String => Google::Cloud::Bigtable::ColumnFamily}]
391
+ # @param granularity [Symbol]
392
+ # @param initial_splits [Array<String>]
393
+ # @yield [column_families] A block for adding column_families.
394
+ # @yieldparam [Hash{String => Google::Cloud::Bigtable::ColumnFamily}]
395
+ #
396
+ # @return [Google::Cloud::Bigtable::Table]
397
+ def self.create \
398
+ service,
399
+ instance_id,
400
+ table_id,
401
+ column_families: nil,
402
+ granularity: nil,
403
+ initial_splits: nil
404
+ column_families ||= Table::ColumnFamilyMap.new
405
+ yield column_families if block_given?
406
+
407
+ table = Google::Bigtable::Admin::V2::Table.new({
408
+ column_families: column_families.to_h,
409
+ granularity: granularity
410
+ }.delete_if { |_, v| v.nil? })
411
+
412
+ grpc = service.create_table(
413
+ instance_id,
414
+ table_id,
415
+ table,
416
+ initial_splits: initial_splits
417
+ )
418
+ from_grpc(grpc, service)
419
+ end
420
+
421
+ # Generates a consistency token for a Table, which can be used in
422
+ # CheckConsistency to check whether mutations to the table that finished
423
+ # before this call started have been replicated. The tokens will be available
424
+ # for 90 days.
425
+ #
426
+ # @return [String] Generated consistency token
427
+ #
428
+ # @example
429
+ # require "google/cloud/bigtable"
430
+ #
431
+ # bigtable = Google::Cloud::Bigtable.new
432
+ #
433
+ # instance = bigtable.instance("my-instance")
434
+ # table = instance.table("my-table")
435
+ #
436
+ # table.generate_consistency_token # "l947XelENinaxJQP0nnrZJjHnAF7YrwW8HCJLotwrF"
437
+ #
438
+ def generate_consistency_token
439
+ ensure_service!
440
+ response = service.generate_consistency_token(instance_id, name)
441
+ response.consistency_token
442
+ end
443
+
444
+ # Checks replication consistency based on a consistency token, that is, if
445
+ # replication has caught up based on the conditions specified in the token
446
+ # and the check request.
447
+ # @param token [String] Consistency token
448
+ # @return [Boolean] Replication is consistent or not.
449
+ #
450
+ # @example
451
+ # require "google/cloud/bigtable"
452
+ #
453
+ # bigtable = Google::Cloud::Bigtable.new
454
+ #
455
+ # instance = bigtable.instance("my-instance")
456
+ # table = instance.table("my-table")
457
+ #
458
+ # token = "l947XelENinaxJQP0nnrZJjHnAF7YrwW8HCJLotwrF"
459
+ #
460
+ # if table.check_consistency(token)
461
+ # puts "Replication is consistent"
462
+ # end
463
+ #
464
+ def check_consistency token
465
+ ensure_service!
466
+ response = service.check_consistency(instance_id, name, token)
467
+ response.consistent
468
+ end
469
+
470
+ # Wait for replication to check replication consistency of table
471
+ # Checks replication consistency by generating consistency token and
472
+ # calling +check_consistency+ api call 5 times(default).
473
+ # If the response is consistent then return true. Otherwise try again.
474
+ # If consistency checking will run for more than 10 minutes and still
475
+ # not got the +true+ response then return +false+.
476
+ #
477
+ # @param timeout [Integer]
478
+ # Timeout in seconds. Defaults value is 600 seconds.
479
+ # @param check_interval [Integer]
480
+ # Consistency check interval in seconds. Default is 5 seconds.
481
+ # @return [Boolean] Replication is consistent or not.
482
+ #
483
+ # @example
484
+ # require "google/cloud/bigtable"
485
+ #
486
+ # bigtable = Google::Cloud::Bigtable.new
487
+ #
488
+ # table = bigtable.table("instance_id", "my-table", perform_lookup: true)
489
+ #
490
+ # if table.wait_for_replication
491
+ # puts "Replication done"
492
+ # end
493
+ #
494
+ # # With custom timeout and interval
495
+ # if table.wait_for_replication(timeout: 300, check_interval: 10)
496
+ # puts "Replication done"
497
+ # end
498
+ #
499
+ def wait_for_replication timeout: 600, check_interval: 5
500
+ if check_interval > timeout
501
+ raise(
502
+ InvalidArgumentError,
503
+ "'check_interval' can not be greather then timeout"
504
+ )
505
+ end
506
+ token = generate_consistency_token
507
+ status = false
508
+ start_at = Time.now
509
+
510
+ loop do
511
+ status = check_consistency(token)
512
+
513
+ break if status || (Time.now - start_at) >= timeout
514
+ sleep(check_interval)
515
+ end
516
+ status
517
+ end
518
+
519
+ # @private
520
+ # Get Data client instance
521
+ #
522
+ # @return [Google::Cloud::Bigtable::V2::BigtableClient]
523
+ #
524
+ def client
525
+ service.client
526
+ end
527
+
528
+ # Delete all rows
529
+ #
530
+ # @param timeout [Integer] Call timeout in seconds
531
+ # Use in case of : Insufficient deadline for DropRowRange then
532
+ # try again with a longer request deadline.
533
+ # @return [Boolean]
534
+ #
535
+ # @example
536
+ # require "google/cloud/bigtable"
537
+ #
538
+ # bigtable = Google::Cloud::Bigtable.new
539
+ #
540
+ # instance = bigtable.instance("my-instance")
541
+ # table = instance.table("my-table")
542
+ # table.delete_all_rows
543
+ #
544
+ # # With timeout
545
+ # table.delete_all_rows(timeout: 120) # 120 seconds.
546
+ #
547
+ def delete_all_rows timeout: nil
548
+ drop_row_range(delete_all_data: true, timeout: timeout)
549
+ end
550
+
551
+ # Delete rows using row key prefix.
552
+ #
553
+ # @param prefix [String] Row key prefix. i.e "user"
554
+ # @param timeout [Integer] Call timeout in seconds
555
+ # @return [Boolean]
556
+ # @example
557
+ # require "google/cloud/bigtable"
558
+ #
559
+ # bigtable = Google::Cloud::Bigtable.new
560
+ #
561
+ # table = bigtable.table("my-instance", "my-table")
562
+ #
563
+ # table.delete_rows_by_prefix("user-100")
564
+ #
565
+ # # With timeout
566
+ # table.delete_all_rows("user-1", timeout: 120) # 120 seconds.
567
+ #
568
+ def delete_rows_by_prefix prefix, timeout: nil
569
+ drop_row_range(row_key_prefix: prefix, timeout: timeout)
570
+ end
571
+
572
+ # Drop row range by row key prefix or delete all.
573
+ #
574
+ # @param row_key_prefix [String] Row key prefix. i.e "user"
575
+ # @param delete_all_data [Boolean]
576
+ # @return [Boolean]
577
+ #
578
+ # @example
579
+ # require "google/cloud/bigtable"
580
+ #
581
+ # bigtable = Google::Cloud::Bigtable.new
582
+ #
583
+ # table = bigtable.table("my-instance", "my-table")
584
+ #
585
+ # # Delete rows using row key prefix.
586
+ # table.drop_row_range("user-100")
587
+ #
588
+ # # Delete all data With timeout
589
+ # table.drop_row_range(delete_all_data: true, timeout: 120) # 120 seconds.
590
+ #
591
+ def drop_row_range \
592
+ row_key_prefix: nil,
593
+ delete_all_data: nil,
594
+ timeout: nil
595
+ ensure_service!
596
+ service.drop_row_range(
597
+ instance_id,
598
+ name,
599
+ row_key_prefix: row_key_prefix,
600
+ delete_all_data_from_table: delete_all_data,
601
+ timeout: timeout
602
+ )
603
+ true
604
+ end
605
+
606
+ # @private
607
+ # Creates a new Table instance from a Google::Bigtable::Admin::V2::Table.
608
+ #
609
+ # @param grpc [Google::Bigtable::Admin::V2::Table]
610
+ # @param service [Google::Cloud::Bigtable::Service]
611
+ # @param view [Symbol] View type.
612
+ # @return [Google::Cloud::Bigtable::Table]
613
+ #
614
+ def self.from_grpc grpc, service, view: nil
615
+ new(grpc, service, view: view)
616
+ end
617
+
618
+ # @private
619
+ # Creates a new Table object from table path.
620
+ #
621
+ # @param path [String] Table path.
622
+ # Formatted table path
623
+ # +projects/<project>/instances/<instance>/tables/<table>+
624
+ # @param service [Google::Cloud::Bigtable::Service]
625
+ # @return [Google::Cloud::Bigtable::Table]
626
+ #
627
+ def self.from_path path, service
628
+ grpc = Google::Bigtable::Admin::V2::Table.new(name: path)
629
+ new(grpc, service, view: :NAME_ONLY)
630
+ end
631
+
632
+ protected
633
+
634
+ # @private
635
+ # Raise an error unless an active connection to the service is
636
+ # available.
637
+ #
638
+ def ensure_service!
639
+ raise "Must have active connection to service" unless service
640
+ end
641
+
642
+ FIELDS_BY_VIEW = {
643
+ SCHEMA_VIEW: %w[granularity column_families],
644
+ REPLICATION_VIEW: ["cluster_states"],
645
+ FULL: %w[granularity column_families cluster_states]
646
+ }.freeze
647
+
648
+ # @private
649
+ #
650
+ # Check and reload table with expected view and set fields
651
+ # @param view [Symbol] Expected view type.
652
+ #
653
+ def check_view_and_load view
654
+ @loaded_views ||= Set.new([@view])
655
+
656
+ if @loaded_views.include?(view) || @loaded_views.include?(:FULL)
657
+ return
658
+ end
659
+
660
+ grpc = service.get_table(instance_id, table_id, view: view)
661
+ @loaded_views << view
662
+
663
+ FIELDS_BY_VIEW[view].each do |field|
664
+ case grpc[field]
665
+ when Google::Protobuf::Map
666
+ @grpc[field].clear
667
+ grpc[field].each { |k, v| @grpc[field][k] = v }
668
+ else
669
+ @grpc[field] = grpc[field]
670
+ end
671
+ end
672
+ end
673
+ end
674
+ end
675
+ end
676
+ end