google-cloud-bigtable 0.6.2 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (82) hide show
  1. checksums.yaml +4 -4
  2. data/AUTHENTICATION.md +4 -26
  3. data/CHANGELOG.md +85 -0
  4. data/CONTRIBUTING.md +1 -1
  5. data/OVERVIEW.md +388 -19
  6. data/lib/google-cloud-bigtable.rb +19 -22
  7. data/lib/google/bigtable/admin/v2/bigtable_table_admin_services_pb.rb +1 -1
  8. data/lib/google/bigtable/v2/bigtable_pb.rb +3 -0
  9. data/lib/google/bigtable/v2/bigtable_services_pb.rb +1 -1
  10. data/lib/google/cloud/bigtable.rb +11 -17
  11. data/lib/google/cloud/bigtable/admin.rb +2 -2
  12. data/lib/google/cloud/bigtable/admin/v2.rb +2 -2
  13. data/lib/google/cloud/bigtable/admin/v2/bigtable_instance_admin_client.rb +1 -1
  14. data/lib/google/cloud/bigtable/admin/v2/bigtable_table_admin_client.rb +2 -2
  15. data/lib/google/cloud/bigtable/admin/v2/credentials.rb +1 -1
  16. data/lib/google/cloud/bigtable/admin/v2/doc/google/bigtable/admin/v2/bigtable_instance_admin.rb +1 -1
  17. data/lib/google/cloud/bigtable/admin/v2/doc/google/bigtable/admin/v2/bigtable_table_admin.rb +1 -1
  18. data/lib/google/cloud/bigtable/admin/v2/doc/google/bigtable/admin/v2/instance.rb +1 -1
  19. data/lib/google/cloud/bigtable/admin/v2/doc/google/bigtable/admin/v2/table.rb +1 -1
  20. data/lib/google/cloud/bigtable/admin/v2/doc/google/iam/v1/iam_policy.rb +1 -1
  21. data/lib/google/cloud/bigtable/admin/v2/doc/google/iam/v1/options.rb +1 -1
  22. data/lib/google/cloud/bigtable/admin/v2/doc/google/iam/v1/policy.rb +7 -6
  23. data/lib/google/cloud/bigtable/admin/v2/doc/google/longrunning/operations.rb +2 -2
  24. data/lib/google/cloud/bigtable/admin/v2/doc/google/protobuf/any.rb +1 -1
  25. data/lib/google/cloud/bigtable/admin/v2/doc/google/protobuf/duration.rb +1 -1
  26. data/lib/google/cloud/bigtable/admin/v2/doc/google/protobuf/empty.rb +1 -1
  27. data/lib/google/cloud/bigtable/admin/v2/doc/google/protobuf/field_mask.rb +1 -1
  28. data/lib/google/cloud/bigtable/admin/v2/doc/google/protobuf/timestamp.rb +1 -1
  29. data/lib/google/cloud/bigtable/admin/v2/doc/google/rpc/status.rb +7 -55
  30. data/lib/google/cloud/bigtable/admin/v2/doc/google/type/expr.rb +1 -1
  31. data/lib/google/cloud/bigtable/app_profile.rb +162 -96
  32. data/lib/google/cloud/bigtable/app_profile/job.rb +5 -8
  33. data/lib/google/cloud/bigtable/app_profile/list.rb +18 -12
  34. data/lib/google/cloud/bigtable/chunk_processor.rb +24 -36
  35. data/lib/google/cloud/bigtable/cluster.rb +45 -18
  36. data/lib/google/cloud/bigtable/cluster/job.rb +3 -7
  37. data/lib/google/cloud/bigtable/cluster/list.rb +22 -20
  38. data/lib/google/cloud/bigtable/column_family.rb +18 -231
  39. data/lib/google/cloud/bigtable/column_family_map.rb +426 -0
  40. data/lib/google/cloud/bigtable/column_range.rb +15 -7
  41. data/lib/google/cloud/bigtable/convert.rb +12 -4
  42. data/lib/google/cloud/bigtable/errors.rb +4 -1
  43. data/lib/google/cloud/bigtable/gc_rule.rb +188 -69
  44. data/lib/google/cloud/bigtable/instance.rb +209 -189
  45. data/lib/google/cloud/bigtable/instance/cluster_map.rb +17 -13
  46. data/lib/google/cloud/bigtable/instance/job.rb +6 -5
  47. data/lib/google/cloud/bigtable/instance/list.rb +18 -13
  48. data/lib/google/cloud/bigtable/longrunning_job.rb +7 -1
  49. data/lib/google/cloud/bigtable/mutation_entry.rb +36 -39
  50. data/lib/google/cloud/bigtable/mutation_operations.rb +90 -73
  51. data/lib/google/cloud/bigtable/policy.rb +9 -5
  52. data/lib/google/cloud/bigtable/project.rb +87 -196
  53. data/lib/google/cloud/bigtable/read_modify_write_rule.rb +15 -10
  54. data/lib/google/cloud/bigtable/read_operations.rb +42 -59
  55. data/lib/google/cloud/bigtable/routing_policy.rb +172 -0
  56. data/lib/google/cloud/bigtable/row.rb +32 -21
  57. data/lib/google/cloud/bigtable/row_filter.rb +80 -35
  58. data/lib/google/cloud/bigtable/row_filter/chain_filter.rb +119 -68
  59. data/lib/google/cloud/bigtable/row_filter/condition_filter.rb +8 -2
  60. data/lib/google/cloud/bigtable/row_filter/interleave_filter.rb +117 -66
  61. data/lib/google/cloud/bigtable/row_filter/simple_filter.rb +24 -9
  62. data/lib/google/cloud/bigtable/row_range.rb +5 -0
  63. data/lib/google/cloud/bigtable/rows_mutator.rb +14 -21
  64. data/lib/google/cloud/bigtable/rows_reader.rb +23 -18
  65. data/lib/google/cloud/bigtable/sample_row_key.rb +6 -3
  66. data/lib/google/cloud/bigtable/service.rb +200 -253
  67. data/lib/google/cloud/bigtable/status.rb +76 -0
  68. data/lib/google/cloud/bigtable/table.rb +158 -262
  69. data/lib/google/cloud/bigtable/table/cluster_state.rb +17 -6
  70. data/lib/google/cloud/bigtable/table/list.rb +16 -9
  71. data/lib/google/cloud/bigtable/v2.rb +2 -2
  72. data/lib/google/cloud/bigtable/v2/bigtable_client.rb +13 -13
  73. data/lib/google/cloud/bigtable/v2/credentials.rb +1 -1
  74. data/lib/google/cloud/bigtable/v2/doc/google/bigtable/v2/bigtable.rb +17 -14
  75. data/lib/google/cloud/bigtable/v2/doc/google/bigtable/v2/data.rb +1 -1
  76. data/lib/google/cloud/bigtable/v2/doc/google/protobuf/any.rb +1 -1
  77. data/lib/google/cloud/bigtable/v2/doc/google/protobuf/wrappers.rb +1 -1
  78. data/lib/google/cloud/bigtable/v2/doc/google/rpc/status.rb +7 -55
  79. data/lib/google/cloud/bigtable/value_range.rb +19 -13
  80. data/lib/google/cloud/bigtable/version.rb +1 -1
  81. metadata +67 -25
  82. data/lib/google/cloud/bigtable/table/column_family_map.rb +0 -70
@@ -17,6 +17,7 @@ module Google
17
17
  module Cloud
18
18
  module Bigtable
19
19
  class Cluster
20
+ ##
20
21
  # # Job
21
22
  #
22
23
  # A resource representing the long-running, asynchronous processing of
@@ -55,6 +56,7 @@ module Google
55
56
  # end
56
57
  #
57
58
  class Job < LongrunningJob
59
+ ##
58
60
  # Gets the cluster object from job results
59
61
  #
60
62
  # @return [Google::Cloud::Bigtable::Cluster, nil] The cluster instance, or
@@ -73,17 +75,11 @@ module Google
73
75
  # storage_type: :SSD
74
76
  # )
75
77
  #
76
- # job.done? #=> false
77
- # job.reload!
78
- # job.done? #=> true
79
- #
80
- # # OR wait utill complete
81
- #
82
78
  # job.wait_until_done!
83
79
  # cluster = job.cluster
84
80
  #
85
81
  def cluster
86
- Cluster.from_grpc(results, service) if results
82
+ Cluster.from_grpc results, service if results
87
83
  end
88
84
  end
89
85
  end
@@ -21,6 +21,7 @@ module Google
21
21
  module Cloud
22
22
  module Bigtable
23
23
  class Cluster
24
+ ##
24
25
  # Cluster::List is a special case Array with additional
25
26
  # values.
26
27
  class List < DelegateClass(::Array)
@@ -32,10 +33,12 @@ module Google
32
33
  # Instance ID
33
34
  attr_accessor :instance_id
34
35
 
36
+ ##
35
37
  # If not empty, indicates that there are more records that match
36
38
  # the request and this value should be passed to continue.
37
39
  attr_accessor :token
38
40
 
41
+ ##
39
42
  # Locations from which cluster information could not be retrieved,
40
43
  # due to an outage or some other transient condition.
41
44
  # Clusters from these locations may be missing from `clusters`
@@ -46,10 +49,11 @@ module Google
46
49
  # Create a new Cluster::List with an array of
47
50
  # Cluster instances.
48
51
  def initialize arr = []
49
- super(arr)
52
+ super arr
50
53
  end
51
54
 
52
- # Whether there is a next page of instances.
55
+ ##
56
+ # Whether there is a next page of clusters.
53
57
  #
54
58
  # @return [Boolean]
55
59
  #
@@ -58,14 +62,16 @@ module Google
58
62
  #
59
63
  # bigtable = Google::Cloud::Bigtable.new
60
64
  #
61
- # clusters = bigtable.clusters("instance-id")
65
+ # clusters = bigtable.clusters
62
66
  # if clusters.next?
63
67
  # next_clusters = clusters.next
64
68
  # end
69
+ #
65
70
  def next?
66
71
  !token.nil?
67
72
  end
68
73
 
74
+ ##
69
75
  # Retrieve the next page of clusters.
70
76
  #
71
77
  # @return [Cluster::List] The list of clusters.
@@ -75,25 +81,21 @@ module Google
75
81
  #
76
82
  # bigtable = Google::Cloud::Bigtable.new
77
83
  #
78
- # clusters = bigtable.clusters("instance-id")
79
- # if instances.next?
84
+ # clusters = bigtable.clusters
85
+ # if clusters.next?
80
86
  # next_clusters = clusters.next
81
87
  # end
88
+ #
82
89
  def next
83
90
  return nil unless next?
84
91
  ensure_service!
85
- grpc = service.list_clusters(instance_id, token: token)
86
- next_list = self.class.from_grpc(
87
- grpc,
88
- service,
89
- instance_id: instance_id
90
- )
91
- if failed_locations
92
- next_list.failed_locations.concat(failed_locations.map(&:to_s))
93
- end
92
+ grpc = service.list_clusters instance_id, token: token
93
+ next_list = self.class.from_grpc grpc, service, instance_id: instance_id
94
+ next_list.failed_locations.concat(failed_locations.map(&:to_s)) if failed_locations
94
95
  next_list
95
96
  end
96
97
 
98
+ ##
97
99
  # Retrieves remaining results by repeatedly invoking {#next} until
98
100
  # {#next?} returns `false`. Calls the given block once for each
99
101
  # result, which is passed as the argument to the block.
@@ -114,7 +116,7 @@ module Google
114
116
  #
115
117
  # bigtable = Google::Cloud::Bigtable.new
116
118
  #
117
- # bigtable.clusters("instance-id").all do |cluster|
119
+ # bigtable.clusters.all do |cluster|
118
120
  # puts cluster.cluster_id
119
121
  # end
120
122
  #
@@ -123,12 +125,12 @@ module Google
123
125
  #
124
126
  # bigtable = Google::Cloud::Bigtable.new
125
127
  #
126
- # all_cluster_ids = bigtable.clusters("instance-id").all.map do |cluster|
127
- # puts cluster.instance_id
128
+ # all_cluster_ids = bigtable.clusters.all.map do |cluster|
129
+ # puts cluster.cluster_id
128
130
  # end
129
131
  #
130
132
  def all
131
- return enum_for(:all) unless block_given?
133
+ return enum_for :all unless block_given?
132
134
 
133
135
  results = self
134
136
  loop do
@@ -143,10 +145,10 @@ module Google
143
145
  # New Cluster::List from a Google::Bigtable::Admin::V2::ListClustersResponse object.
144
146
  def self.from_grpc grpc, service, instance_id: nil
145
147
  clusters = List.new(Array(grpc.clusters).map do |cluster|
146
- Cluster.from_grpc(cluster, service)
148
+ Cluster.from_grpc cluster, service
147
149
  end)
148
150
  token = grpc.next_page_token
149
- token = nil if token == "".freeze
151
+ token = nil if token == ""
150
152
  clusters.token = token
151
153
  clusters.instance_id = instance_id
152
154
  clusters.service = service
@@ -20,6 +20,7 @@ require "google/cloud/bigtable/gc_rule"
20
20
  module Google
21
21
  module Cloud
22
22
  module Bigtable
23
+ ##
23
24
  # # ColumnFamily
24
25
  #
25
26
  # A set of columns within a table that share a common configuration.
@@ -32,254 +33,40 @@ module Google
32
33
  # instance = bigtable.instance("my-instance")
33
34
  # table = instance.table("my-table")
34
35
  #
35
- # # Create
36
- # gc_rule = Google::Cloud::Bigtable::GcRule.max_versions(5)
37
- # column_family = table.column_families.add("cf1", gc_rule)
38
- #
39
- # # Update
40
- # column_family = table.column_families.find_by_name("cf2")
41
- # column_family.gc_rule = Google::Cloud::Bigtable::GcRule.max_age(600)
42
- # column_family.update
43
- #
44
- # # Delete
45
- # column_family = table.column_families.find_by_name("cf3")
46
- # column_family.delete
36
+ # column_family = table.column_families["cf2"]
37
+ # puts column_family.gc_rule
47
38
  #
48
39
  class ColumnFamily
49
- # @private
50
- # The gRPC Service object
51
- attr_accessor :service
52
-
53
- # @private
54
- attr_accessor :instance_id
55
-
56
- # @private
57
- attr_accessor :table_id
58
-
59
- # Column family name
40
+ ##
41
+ # Name of the column family.
42
+ # @return [String]
60
43
  attr_reader :name
61
44
 
45
+ ##
46
+ # The garbage collection rule to be used for the column family.
47
+ # Optional. The service default value will be used when not specified.
48
+ # @return [Google::Cloud::Bigtable::GcRule, nil]
49
+ attr_accessor :gc_rule
50
+
62
51
  # @private
63
- #
64
- # Create instance of ColumnFamily
65
- # @param service [Google::Cloud::Bigtable::Service]
66
- # @param grpc [Google::Bigtable::Admin::V2::ColumnFamily]
67
- # @param name [String] Name of the column family
68
- #
69
- def initialize service, grpc: nil, name: nil
70
- @service = service
71
- @grpc = grpc || Google::Bigtable::Admin::V2::ColumnFamily.new
52
+ def initialize name, gc_rule: nil
72
53
  @name = name
73
- end
74
-
75
- # Set Column Family garbage collection rules
76
- #
77
- # @param rule [Google::Cloud::Bigtable::GcRule, nil] the new rule, or
78
- # `nil` for the service default value.
79
- #
80
- def gc_rule= rule
81
- @grpc.gc_rule = rule.nil? ? nil : rule.to_grpc
82
- end
83
-
84
- # Get garbage collection rule
85
- #
86
- # @return [Google::Cloud::Bigtable::GcRule]
87
- #
88
- def gc_rule
89
- GcRule.from_grpc(@grpc.gc_rule) if @grpc.gc_rule
90
- end
91
-
92
- # Create column family
93
- #
94
- # @return [Google::Cloud::Bigtable::ColumnFamily]
95
- #
96
- # @example
97
- # require "google/cloud/bigtable"
98
- #
99
- # bigtable = Google::Cloud::Bigtable.new
100
- #
101
- # instance = bigtable.instance("my-instance")
102
- # table = instance.table("my-table")
103
- #
104
- # gc_rule = Google::Cloud::Bigtable::GcRule.max_versions(3)
105
- # cf = table.column_family("cf", gc_rule)
106
- # cf.create
107
- #
108
- def create
109
- modify_column_family(self.class.create_modification(name, gc_rule))
110
- end
111
-
112
- # Update column family
113
- #
114
- # @return [Google::Cloud::Bigtable::ColumnFamily]
115
- #
116
- # @example
117
- # require "google/cloud/bigtable"
118
- #
119
- # bigtable = Google::Cloud::Bigtable.new
120
- #
121
- # instance = bigtable.instance("my-instance")
122
- # table = instance.table("my-table")
123
- #
124
- # column_family = table.column_families.find {|cf| cf.name == "cf" }
125
- # column_family.gc_rule = Google::Cloud::Bigtable::GcRule.max_age(600)
126
- # column_family.save
127
- #
128
- def save
129
- modify_column_family(self.class.update_modification(name, gc_rule))
130
- end
131
- alias update save
132
-
133
- # Permanently delete column family from table
134
- #
135
- # @return [Boolean] Returns true if the column family was deleted.
136
- #
137
- # @example
138
- # require "google/cloud/bigtable"
139
- #
140
- # bigtable = Google::Cloud::Bigtable.new
141
- #
142
- # instance = bigtable.instance("my-instance")
143
- # table = instance.table("my-table")
144
- #
145
- # column_family = table.column_families.find {|cf| cf.name == "cf" }
146
- # column_family.delete
147
- #
148
- def delete
149
- modify_column_family(self.class.drop_modification(name)).nil?
150
- end
151
-
152
- # Create gPRC instance to create column family modification
153
- #
154
- # @param name [String] Column family name
155
- # @param gc_rule [Google::Cloud::Bigtable::GcRule] The garbage
156
- # collection rule to be used for the column family. Optional. The
157
- # service default value will be used when not specified.
158
- # @return [Google::Bigtable::Admin::V2::ModifyColumnFamiliesRequest::Modification]
159
- #
160
- # @example
161
- # require "google/cloud/bigtable"
162
- #
163
- # Google::Cloud::Bigtable::ColumnFamily.create_modification(
164
- # "cf1", Google::Cloud::Bigtable::GcRule.max_age(600))
165
- # )
166
- #
167
- def self.create_modification name, gc_rule
168
- column_modification_grpc(:create, name, gc_rule)
169
- end
170
-
171
- # Create update column family modification gPRC instance
172
- #
173
- # @param name [String] Column family name
174
- # @param gc_rule [Google::Cloud::Bigtable::GcRule] The garbage
175
- # collection rule to be used for the column family. Optional. The
176
- # service default value will be used when not specified.
177
- # @return [Google::Bigtable::Admin::V2::ModifyColumnFamiliesRequest::Modification]
178
- #
179
- # @example
180
- # require "google/cloud/bigtable"
181
- #
182
- # Google::Cloud::Bigtable::ColumnFamily.update_modification(
183
- # "cf1", Google::Cloud::Bigtable::GcRule.max_age(600))
184
- # )
185
- #
186
- def self.update_modification name, gc_rule
187
- column_modification_grpc(:update, name, gc_rule)
188
- end
189
-
190
- # Create drop column family modification gRPC instance
191
- #
192
- # @param name [String] Column family name
193
- # @return [Google::Bigtable::Admin::V2::ModifyColumnFamiliesRequest::Modification]
194
- #
195
- # @example
196
- # require "google/cloud/bigtable"
197
- #
198
- # Google::Cloud::Bigtable::ColumnFamily.drop_modification("cf1")
199
- #
200
- def self.drop_modification name
201
- column_modification_grpc(:drop, name)
54
+ @gc_rule = gc_rule
202
55
  end
203
56
 
204
57
  # @private
205
58
  #
206
- # Create a new ColumnFamily instance from a
207
- # Google::Bigtable::Admin::V2::ColumnFamily.
59
+ # Create a new ColumnFamily instance from a {Google::Bigtable::Admin::V2::ColumnFamily}.
208
60
  #
209
61
  # @param grpc [Google::Bigtable::Admin::V2::ColumnFamily]
210
- # @param service [Google::Cloud::Bigtable::Service]
211
62
  # @param name [String] Column family name
212
- # @param instance_id [String]
213
- # @param table_id [String]
214
63
  # @return [Google::Cloud::Bigtable::ColumnFamily]
215
64
  #
216
- def self.from_grpc \
217
- grpc,
218
- service,
219
- name: nil,
220
- instance_id: nil,
221
- table_id: nil
222
- new(service, grpc: grpc, name: name).tap do |cf|
223
- cf.table_id = table_id
224
- cf.instance_id = instance_id
65
+ def self.from_grpc grpc, name
66
+ new(name).tap do |cf|
67
+ cf.gc_rule = GcRule.from_grpc grpc.gc_rule if grpc.gc_rule
225
68
  end
226
69
  end
227
-
228
- # @private
229
- #
230
- # Create column family modification gRPC instance
231
- #
232
- # @param type [Symbol] Type of modification.
233
- # Valid values are `:create`, `:update`, `drop`
234
- # @param family_name [String] Column family name
235
- # @param gc_rule [Google::Cloud::Bigtable::GcRule] The garbage
236
- # collection rule to be used for the column family. Optional. The
237
- # service default value will be used when not specified.
238
- # @return [Google::Bigtable::Admin::V2::ModifyColumnFamiliesRequest::Modification]
239
- #
240
- def self.column_modification_grpc type, family_name, gc_rule = nil
241
- attrs = { id: family_name }
242
-
243
- attrs[type] = if type == :drop
244
- true
245
- else
246
- cf = Google::Bigtable::Admin::V2::ColumnFamily.new
247
- cf.gc_rule = gc_rule.to_grpc if gc_rule
248
- cf
249
- end
250
-
251
- Google::Bigtable::Admin::V2::ModifyColumnFamiliesRequest:: \
252
- Modification.new(attrs)
253
- end
254
-
255
- protected
256
-
257
- # @private
258
- #
259
- # Create/Update/Delete column_family
260
- #
261
- # @param modification [Google::Bigtable::Admin::V2::ModifyColumnFamiliesRequest::Modification]
262
- # @return [Google::Cloud::Bigtable::ColumnFamily]
263
- #
264
- def modify_column_family modification
265
- ensure_service!
266
- table = Table.modify_column_families(
267
- service,
268
- instance_id,
269
- table_id,
270
- modification
271
- )
272
- table.column_families.find { |cf| cf.name == name }
273
- end
274
-
275
- # @private
276
- #
277
- # Raise an error unless an active connection to the service is
278
- # available
279
- #
280
- def ensure_service!
281
- raise "Must have active connection to service" unless service
282
- end
283
70
  end
284
71
  end
285
72
  end
@@ -0,0 +1,426 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright 2019 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/column_family"
19
+
20
+ module Google
21
+ module Cloud
22
+ module Bigtable
23
+ ##
24
+ # Represents a table's column families.
25
+ #
26
+ # See {Project#create_table}, {Instance#create_table} and
27
+ # {Table#column_families}.
28
+ #
29
+ # @example
30
+ # require "google/cloud/bigtable"
31
+ #
32
+ # bigtable = Google::Cloud::Bigtable.new
33
+ #
34
+ # column_families = Google::Cloud::Bigtable::ColumnFamilyMap.new
35
+ #
36
+ # column_families.add "cf1", gc_rule: Google::Cloud::Bigtable::GcRule.max_age(600)
37
+ # column_families.add "cf2", gc_rule: Google::Cloud::Bigtable::GcRule.max_versions(5)
38
+ #
39
+ # @example Create a table with a block yielding a ColumnFamilyMap.
40
+ # require "google/cloud/bigtable"
41
+ #
42
+ # bigtable = Google::Cloud::Bigtable.new
43
+ #
44
+ # table = bigtable.create_table("my-instance", "my-table") do |cfm|
45
+ # cfm.add('cf1', gc_rule: Google::Cloud::Bigtable::GcRule.max_versions(5))
46
+ # cfm.add('cf2', gc_rule: Google::Cloud::Bigtable::GcRule.max_age(600))
47
+ #
48
+ # gc_rule = Google::Cloud::Bigtable::GcRule.union(
49
+ # Google::Cloud::Bigtable::GcRule.max_age(1800),
50
+ # Google::Cloud::Bigtable::GcRule.max_versions(3)
51
+ # )
52
+ # cfm.add('cf3', gc_rule: gc_rule)
53
+ # end
54
+ #
55
+ # puts table.column_families
56
+ #
57
+ # @example Update column families with a block yielding a ColumnFamilyMap.
58
+ # require "google/cloud/bigtable"
59
+ #
60
+ # bigtable = Google::Cloud::Bigtable.new
61
+ #
62
+ # table = bigtable.table("my-instance", "my-table", perform_lookup: true)
63
+ #
64
+ # table.column_families do |cfm|
65
+ # cfm.add "cf4", gc_rule: Google::Cloud::Bigtable::GcRule.max_age(600)
66
+ # cfm.add "cf5", gc_rule: Google::Cloud::Bigtable::GcRule.max_versions(5)
67
+ #
68
+ # rule_1 = Google::Cloud::Bigtable::GcRule.max_versions(3)
69
+ # rule_2 = Google::Cloud::Bigtable::GcRule.max_age(600)
70
+ # rule_union = Google::Cloud::Bigtable::GcRule.union(rule_1, rule_2)
71
+ # cfm.update "cf2", gc_rule: rule_union
72
+ #
73
+ # cfm.delete "cf3"
74
+ # end
75
+ #
76
+ # puts table.column_families["cf3"] #=> nil
77
+ #
78
+ class ColumnFamilyMap
79
+ include Enumerable
80
+
81
+ ##
82
+ # Creates a new ColumnFamilyMap object.
83
+ #
84
+ # @example
85
+ # require "google/cloud/bigtable"
86
+ #
87
+ # bigtable = Google::Cloud::Bigtable.new
88
+ #
89
+ # column_families = Google::Cloud::Bigtable::ColumnFamilyMap.new
90
+ #
91
+ # column_families.add "cf1", gc_rule: Google::Cloud::Bigtable::GcRule.max_age(600)
92
+ # column_families.add "cf2", gc_rule: Google::Cloud::Bigtable::GcRule.max_versions(5)
93
+ #
94
+ # @example Create new column families using block syntax:
95
+ # require "google/cloud/bigtable"
96
+ #
97
+ # bigtable = Google::Cloud::Bigtable.new
98
+ #
99
+ # column_families = Google::Cloud::Bigtable::ColumnFamilyMap.new do |cfm|
100
+ # cfm.add "cf1", gc_rule: Google::Cloud::Bigtable::GcRule.max_age(600)
101
+ # cfm.add "cf2", gc_rule: Google::Cloud::Bigtable::GcRule.max_versions(5)
102
+ # end
103
+ #
104
+ def initialize
105
+ @column_families = empty_map
106
+
107
+ yield self if block_given?
108
+ end
109
+
110
+ ##
111
+ # Calls the block once for each column family in the map, passing
112
+ # the name and column family pair as parameters.
113
+ #
114
+ # If no block is given, an enumerator is returned instead.
115
+ #
116
+ # @yield [name, column_family] The name and column family pair.
117
+ # @yieldparam [String] name the column family name.
118
+ # @yieldparam [ColumnFamily] column_family the column family object.
119
+ #
120
+ # @return [Enumerator]
121
+ #
122
+ def each
123
+ return enum_for :each unless block_given?
124
+
125
+ @column_families.each do |name, column_family_grpc|
126
+ column_family = ColumnFamily.from_grpc column_family_grpc, name
127
+ yield name, column_family
128
+ end
129
+ end
130
+
131
+ ##
132
+ # Retrieves the ColumnFamily object corresponding to the `name`. If not
133
+ # found, returns `nil`.
134
+ #
135
+ # @return [ColumnFamily]
136
+ #
137
+ def [] name
138
+ return nil unless name? name
139
+
140
+ ColumnFamily.from_grpc @column_families[name], name
141
+ end
142
+
143
+ ##
144
+ # Returns true if the given name is present in the map.
145
+ #
146
+ # @return [Boolean]
147
+ #
148
+ def name? name
149
+ @column_families.has_key? name
150
+ end
151
+ alias key? name?
152
+
153
+ ##
154
+ # Returns a new array populated with the names from the map.
155
+ #
156
+ # @return [Array<String>]
157
+ #
158
+ def names
159
+ @column_families.keys
160
+ end
161
+ alias keys names
162
+
163
+ ##
164
+ # Returns the number of name and column family pairs in the map.
165
+ #
166
+ # @return [Integer]
167
+ #
168
+ def length
169
+ @column_families.length
170
+ end
171
+ alias size length
172
+
173
+ ##
174
+ # Returns true if the map contains no name and column family pairs.
175
+ #
176
+ # @return [Boolean]
177
+ #
178
+ def empty?
179
+ length.zero?
180
+ end
181
+
182
+ ##
183
+ # Adds a new column family to the table.
184
+ #
185
+ # @overload add(name, gc_rule: nil)
186
+ # @param name [String] Column family name.
187
+ # @param gc_rule [Google::Cloud::Bigtable::GcRule] The garbage
188
+ # collection rule to be used for the column family. Optional. The
189
+ # service default value will be used when not specified.
190
+ #
191
+ # @raise [ArgumentError] if the column family name already exists.
192
+ # @raise [FrozenError] if the column family map is frozen.
193
+ #
194
+ # @example
195
+ # require "google/cloud/bigtable"
196
+ #
197
+ # bigtable = Google::Cloud::Bigtable.new
198
+ #
199
+ # table = bigtable.table("my-instance", "my-table", perform_lookup: true)
200
+ #
201
+ # table.column_families do |column_families|
202
+ # column_families.add "cf4", gc_rule: Google::Cloud::Bigtable::GcRule.max_age(600)
203
+ # column_families.add "cf5", gc_rule: Google::Cloud::Bigtable::GcRule.max_versions(5)
204
+ # end
205
+ #
206
+ def add name, positional_gc_rule = nil, gc_rule: nil
207
+ raise ArgumentError, "column family #{name.inspect} already exists" if @column_families.has_key? name
208
+
209
+ gc_rule ||= positional_gc_rule
210
+ if positional_gc_rule
211
+ warn "The positional gc_rule argument is deprecated. Use the named gc_rule argument instead."
212
+ end
213
+
214
+ column_family = Google::Bigtable::Admin::V2::ColumnFamily.new
215
+ column_family.gc_rule = gc_rule.to_grpc if gc_rule
216
+ @column_families[name] = column_family
217
+
218
+ nil
219
+ end
220
+ alias create add
221
+
222
+ ##
223
+ # Updates an existing column family in the table.
224
+ #
225
+ # @param name [String] Column family name.
226
+ # @param gc_rule [Google::Cloud::Bigtable::GcRule] The new garbage
227
+ # collection rule to be used for the column family. Optional. The
228
+ # service default value will be used when not specified.
229
+ #
230
+ # @raise [ArgumentError] if the column family name does not exist.
231
+ # @raise [FrozenError] if the column family map is frozen.
232
+ #
233
+ # @example
234
+ # require "google/cloud/bigtable"
235
+ #
236
+ # bigtable = Google::Cloud::Bigtable.new
237
+ #
238
+ # table = bigtable.table("my-instance", "my-table", perform_lookup: true)
239
+ #
240
+ # table.column_families do |column_families|
241
+ # rule_1 = Google::Cloud::Bigtable::GcRule.max_versions(3)
242
+ # rule_2 = Google::Cloud::Bigtable::GcRule.max_age(600)
243
+ # rule_union = Google::Cloud::Bigtable::GcRule.union(rule_1, rule_2)
244
+ #
245
+ # column_families.update "cf2", gc_rule: rule_union
246
+ # end
247
+ #
248
+ def update name, gc_rule: nil
249
+ raise ArgumentError, "column family #{name.inspect} does not exist" unless @column_families.has_key? name
250
+
251
+ column_family = Google::Bigtable::Admin::V2::ColumnFamily.new
252
+ column_family.gc_rule = gc_rule.to_grpc if gc_rule
253
+ @column_families[name] = column_family
254
+
255
+ nil
256
+ end
257
+
258
+ ##
259
+ # Deletes the named column family from the table.
260
+ #
261
+ # @param name [String] Column family name.
262
+ #
263
+ # @raise [ArgumentError] if the column family name does not exist.
264
+ # @raise [FrozenError] if the column family map is frozen.
265
+ #
266
+ # @example
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.column_families do |column_families|
274
+ # column_families.delete "cf3"
275
+ # end
276
+ #
277
+ def delete name
278
+ raise ArgumentError, "column family #{name.inspect} does not exist" unless @column_families.has_key? name
279
+
280
+ @column_families.delete name
281
+
282
+ nil
283
+ end
284
+ alias drop delete
285
+
286
+ ##
287
+ # @private
288
+ # We don't need to document this method.
289
+ #
290
+ def freeze
291
+ @column_families.freeze
292
+ super
293
+ end
294
+
295
+ ##
296
+ # @private
297
+ #
298
+ # Build column family modifications for the map.
299
+ #
300
+ # @param comparison_map [Google::Protobuf::Map] The map to compare the
301
+ # current map against for building the modification entries.
302
+ # @return [Array<Google::Bigtable::Admin::V2::ModifyColumnFamiliesRequest::Modification>]
303
+ #
304
+ def modifications comparison_map = nil
305
+ comparison_map ||= empty_map
306
+
307
+ created_modifications(comparison_map) +
308
+ updated_modifications(comparison_map) +
309
+ dropped_modifications(comparison_map)
310
+ end
311
+
312
+ ##
313
+ # @private
314
+ #
315
+ # Return the Google::Protobuf::Map for the ColumnFamilyMap.
316
+ #
317
+ # @return [Google::Protobuf::Map]
318
+ #
319
+ def to_grpc
320
+ # Always return a dup in case it was frozen.
321
+ @column_families.dup
322
+ end
323
+
324
+ ##
325
+ # @private
326
+ #
327
+ # Return the Hash for the ColumnFamilyMap.
328
+ #
329
+ # @return [Hash]
330
+ #
331
+ def to_grpc_hash
332
+ Hash[to_grpc.to_a]
333
+ end
334
+
335
+ ##
336
+ # @private
337
+ #
338
+ # Create new ColumnFamilyMap from Google::Protobuf::Map.
339
+ #
340
+ # @param grpc_map [Google::Protobuf::Map]
341
+ # @return [ColumnFamilyMap]
342
+ #
343
+ def self.from_grpc grpc_map
344
+ new.tap do |cfm|
345
+ # Always dup so we don't modify the original map object.
346
+ cfm.instance_variable_set :@column_families, grpc_map.dup
347
+ end
348
+ end
349
+
350
+ protected
351
+
352
+ ##
353
+ # Create an empty Google::Protobuf::Map suitable for column_families.
354
+ def empty_map
355
+ Google::Bigtable::Admin::V2::Table.new.column_families
356
+ end
357
+
358
+ ##
359
+ # @private
360
+ #
361
+ # Build column family modifications for created column families.
362
+ #
363
+ # @param comparison_map [Google::Protobuf::Map] The map to compare the
364
+ # current map against for building the modification entries.
365
+ # @return [Array<Google::Bigtable::Admin::V2::ModifyColumnFamiliesRequest::Modification>]
366
+ #
367
+ def created_modifications comparison_map
368
+ added_keys = @column_families.keys - comparison_map.keys
369
+
370
+ added_keys.map do |name|
371
+ Google::Bigtable::Admin::V2::ModifyColumnFamiliesRequest:: \
372
+ Modification.new(
373
+ id: name,
374
+ create: @column_families[name]
375
+ )
376
+ end
377
+ end
378
+
379
+ ##
380
+ # @private
381
+ #
382
+ # Build column family modifications for updated column families.
383
+ #
384
+ # @param comparison_map [Google::Protobuf::Map] The map to compare the
385
+ # current map against for building the modification entries.
386
+ # @return [Array<Google::Bigtable::Admin::V2::ModifyColumnFamiliesRequest::Modification>]
387
+ #
388
+ def updated_modifications comparison_map
389
+ possibly_updated_keys = @column_families.keys & comparison_map.keys
390
+ updated_keys = possibly_updated_keys.reject do |name|
391
+ @column_families[name] == comparison_map[name]
392
+ end
393
+
394
+ updated_keys.map do |name|
395
+ Google::Bigtable::Admin::V2::ModifyColumnFamiliesRequest:: \
396
+ Modification.new(
397
+ id: name,
398
+ update: @column_families[name]
399
+ )
400
+ end
401
+ end
402
+
403
+ ##
404
+ # @private
405
+ #
406
+ # Build column family modifications for dropped column families.
407
+ #
408
+ # @param comparison_map [Google::Protobuf::Map] The map to compare the
409
+ # current map against for building the modification entries.
410
+ # @return [Array<Google::Bigtable::Admin::V2::ModifyColumnFamiliesRequest::Modification>]
411
+ #
412
+ def dropped_modifications comparison_map
413
+ dropped_keys = comparison_map.keys - @column_families.keys
414
+
415
+ dropped_keys.map do |name|
416
+ Google::Bigtable::Admin::V2::ModifyColumnFamiliesRequest:: \
417
+ Modification.new(
418
+ id: name,
419
+ drop: true
420
+ )
421
+ end
422
+ end
423
+ end
424
+ end
425
+ end
426
+ end