google-cloud-bigtable 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (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,171 @@
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 "delegate"
19
+
20
+ module Google
21
+ module Cloud
22
+ module Bigtable
23
+ class Cluster
24
+ ##
25
+ # Cluster::List is a special case Array with additional
26
+ # values.
27
+ class List < DelegateClass(::Array)
28
+ # @private
29
+ # The gRPC Service object.
30
+ attr_accessor :service
31
+
32
+ # @private
33
+ # Instance ID
34
+ attr_accessor :instance_id
35
+
36
+ ##
37
+ # If not empty, indicates that there are more records that match
38
+ # the request and this value should be passed to continue.
39
+ attr_accessor :token
40
+
41
+ ##
42
+ # Locations from which cluster information could not be retrieved,
43
+ # due to an outage or some other transient condition.
44
+ # Clusters from these locations may be missing from `clusters`
45
+ # or may only have partial information returned.
46
+ attr_accessor :failed_locations
47
+
48
+ # @private
49
+ # Create a new Cluster::List with an array of
50
+ # Cluster instances.
51
+ def initialize arr = []
52
+ super arr
53
+ end
54
+
55
+ ##
56
+ # Whether there is a next page of clusters.
57
+ #
58
+ # @return [Boolean]
59
+ #
60
+ # @example
61
+ # require "google/cloud/bigtable"
62
+ #
63
+ # bigtable = Google::Cloud::Bigtable.new
64
+ #
65
+ # clusters = bigtable.clusters
66
+ # if clusters.next?
67
+ # next_clusters = clusters.next
68
+ # end
69
+ #
70
+ def next?
71
+ !token.nil?
72
+ end
73
+
74
+ ##
75
+ # Retrieve the next page of clusters.
76
+ #
77
+ # @return [Cluster::List] The list of clusters.
78
+ #
79
+ # @example
80
+ # require "google/cloud/bigtable"
81
+ #
82
+ # bigtable = Google::Cloud::Bigtable.new
83
+ #
84
+ # clusters = bigtable.clusters
85
+ # if clusters.next?
86
+ # next_clusters = clusters.next
87
+ # end
88
+ #
89
+ def next
90
+ return nil unless next?
91
+ ensure_service!
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
95
+ next_list
96
+ end
97
+
98
+ ##
99
+ # Retrieves remaining results by repeatedly invoking {#next} until
100
+ # {#next?} returns `false`. Calls the given block once for each
101
+ # result, which is passed as the argument to the block.
102
+ #
103
+ # An enumerator is returned if no block is given.
104
+ #
105
+ # This method will make repeated API calls until all remaining results
106
+ # are retrieved (unlike `#each`, for example, which merely iterates
107
+ # over the results returned by a single API call). Use with caution.
108
+ #
109
+ # @yield [cluster] The block for accessing each cluster.
110
+ # @yieldparam [Cluster] cluster The cluster object.
111
+ #
112
+ # @return [Enumerator,nil] An enumerator is returned if no block is given, otherwise `nil`.
113
+ #
114
+ # @example Iterating each cluster by passing a block:
115
+ # require "google/cloud/bigtable"
116
+ #
117
+ # bigtable = Google::Cloud::Bigtable.new
118
+ #
119
+ # bigtable.clusters.all do |cluster|
120
+ # puts cluster.cluster_id
121
+ # end
122
+ #
123
+ # @example Using the enumerator by not passing a block:
124
+ # require "google/cloud/bigtable"
125
+ #
126
+ # bigtable = Google::Cloud::Bigtable.new
127
+ #
128
+ # all_cluster_ids = bigtable.clusters.all.map do |cluster|
129
+ # puts cluster.cluster_id
130
+ # end
131
+ #
132
+ def all
133
+ return enum_for :all unless block_given?
134
+
135
+ results = self
136
+ loop do
137
+ results.each { |r| yield r }
138
+ break unless results.next?
139
+ results = results.next
140
+ end
141
+ end
142
+
143
+ # @private
144
+ #
145
+ # New Cluster::List from a Google::Bigtable::Admin::V2::ListClustersResponse object.
146
+ def self.from_grpc grpc, service, instance_id: nil
147
+ clusters = List.new(Array(grpc.clusters).map do |cluster|
148
+ Cluster.from_grpc cluster, service
149
+ end)
150
+ token = grpc.next_page_token
151
+ token = nil if token == ""
152
+ clusters.token = token
153
+ clusters.instance_id = instance_id
154
+ clusters.service = service
155
+ clusters.failed_locations = grpc.failed_locations.map(&:to_s)
156
+ clusters
157
+ end
158
+
159
+ protected
160
+
161
+ # @private
162
+ #
163
+ # Raise an error unless an active service is available.
164
+ def ensure_service!
165
+ raise "Must have active connection" unless service
166
+ end
167
+ end
168
+ end
169
+ end
170
+ end
171
+ end
@@ -0,0 +1,73 @@
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/gc_rule"
19
+
20
+ module Google
21
+ module Cloud
22
+ module Bigtable
23
+ ##
24
+ # # ColumnFamily
25
+ #
26
+ # A set of columns within a table that share a common configuration.
27
+ #
28
+ # @example
29
+ # require "google/cloud/bigtable"
30
+ #
31
+ # bigtable = Google::Cloud::Bigtable.new
32
+ #
33
+ # instance = bigtable.instance("my-instance")
34
+ # table = instance.table("my-table")
35
+ #
36
+ # column_family = table.column_families["cf2"]
37
+ # puts column_family.gc_rule
38
+ #
39
+ class ColumnFamily
40
+ ##
41
+ # Name of the column family.
42
+ # @return [String]
43
+ attr_reader :name
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
+
51
+ # @private
52
+ def initialize name, gc_rule: nil
53
+ @name = name
54
+ @gc_rule = gc_rule
55
+ end
56
+
57
+ # @private
58
+ #
59
+ # Create a new ColumnFamily instance from a {Google::Bigtable::Admin::V2::ColumnFamily}.
60
+ #
61
+ # @param grpc [Google::Bigtable::Admin::V2::ColumnFamily]
62
+ # @param name [String] Column family name
63
+ # @return [Google::Cloud::Bigtable::ColumnFamily]
64
+ #
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
68
+ end
69
+ end
70
+ end
71
+ end
72
+ end
73
+ 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,nil] An enumerator is returned if no block is given, otherwise `nil`.
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