google-cloud-bigtable 0.6.2 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (49) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +36 -0
  3. data/CONTRIBUTING.md +1 -1
  4. data/lib/google-cloud-bigtable.rb +19 -21
  5. data/lib/google/cloud/bigtable.rb +11 -17
  6. data/lib/google/cloud/bigtable/app_profile.rb +148 -82
  7. data/lib/google/cloud/bigtable/app_profile/job.rb +5 -8
  8. data/lib/google/cloud/bigtable/app_profile/list.rb +11 -5
  9. data/lib/google/cloud/bigtable/chunk_processor.rb +23 -35
  10. data/lib/google/cloud/bigtable/cluster.rb +38 -11
  11. data/lib/google/cloud/bigtable/cluster/job.rb +3 -7
  12. data/lib/google/cloud/bigtable/cluster/list.rb +20 -18
  13. data/lib/google/cloud/bigtable/column_family.rb +22 -229
  14. data/lib/google/cloud/bigtable/column_family_map.rb +426 -0
  15. data/lib/google/cloud/bigtable/column_range.rb +9 -1
  16. data/lib/google/cloud/bigtable/convert.rb +12 -4
  17. data/lib/google/cloud/bigtable/errors.rb +4 -1
  18. data/lib/google/cloud/bigtable/gc_rule.rb +184 -65
  19. data/lib/google/cloud/bigtable/instance.rb +136 -126
  20. data/lib/google/cloud/bigtable/instance/cluster_map.rb +9 -7
  21. data/lib/google/cloud/bigtable/instance/job.rb +4 -3
  22. data/lib/google/cloud/bigtable/instance/list.rb +14 -9
  23. data/lib/google/cloud/bigtable/longrunning_job.rb +6 -0
  24. data/lib/google/cloud/bigtable/mutation_entry.rb +19 -23
  25. data/lib/google/cloud/bigtable/mutation_operations.rb +82 -29
  26. data/lib/google/cloud/bigtable/policy.rb +9 -5
  27. data/lib/google/cloud/bigtable/project.rb +62 -140
  28. data/lib/google/cloud/bigtable/read_modify_write_rule.rb +9 -4
  29. data/lib/google/cloud/bigtable/read_operations.rb +28 -41
  30. data/lib/google/cloud/bigtable/routing_policy.rb +171 -0
  31. data/lib/google/cloud/bigtable/row.rb +18 -7
  32. data/lib/google/cloud/bigtable/row_filter.rb +49 -20
  33. data/lib/google/cloud/bigtable/row_filter/chain_filter.rb +71 -43
  34. data/lib/google/cloud/bigtable/row_filter/condition_filter.rb +4 -1
  35. data/lib/google/cloud/bigtable/row_filter/interleave_filter.rb +74 -43
  36. data/lib/google/cloud/bigtable/row_filter/simple_filter.rb +22 -7
  37. data/lib/google/cloud/bigtable/row_range.rb +5 -0
  38. data/lib/google/cloud/bigtable/rows_mutator.rb +14 -17
  39. data/lib/google/cloud/bigtable/rows_reader.rb +18 -14
  40. data/lib/google/cloud/bigtable/sample_row_key.rb +5 -2
  41. data/lib/google/cloud/bigtable/service.rb +161 -242
  42. data/lib/google/cloud/bigtable/status.rb +76 -0
  43. data/lib/google/cloud/bigtable/table.rb +141 -236
  44. data/lib/google/cloud/bigtable/table/cluster_state.rb +7 -1
  45. data/lib/google/cloud/bigtable/table/list.rb +14 -7
  46. data/lib/google/cloud/bigtable/value_range.rb +5 -0
  47. data/lib/google/cloud/bigtable/version.rb +1 -1
  48. metadata +27 -25
  49. data/lib/google/cloud/bigtable/table/column_family_map.rb +0 -70
@@ -18,6 +18,7 @@
18
18
  module Google
19
19
  module Cloud
20
20
  module Bigtable
21
+ ##
21
22
  # # ReadModifyWriteRule
22
23
  #
23
24
  # Specifies an atomic read/modify/write operation on the latest value of the
@@ -48,6 +49,7 @@ module Google
48
49
  @grpc.column_qualifier = qualifier
49
50
  end
50
51
 
52
+ ##
51
53
  # Create an instance of an append-value rule.
52
54
  #
53
55
  # @param family [String]
@@ -65,11 +67,12 @@ module Google
65
67
  # )
66
68
  #
67
69
  def self.append family, qualifier, value
68
- rule = new(family, qualifier)
69
- rule.append(value)
70
+ rule = new family, qualifier
71
+ rule.append value
70
72
  rule
71
73
  end
72
74
 
75
+ ##
73
76
  # Create an instance of an increment-amount rule.
74
77
  #
75
78
  # @param family [String]
@@ -89,11 +92,12 @@ module Google
89
92
  # )
90
93
  #
91
94
  def self.increment family, qualifier, amount
92
- rule = new(family, qualifier)
93
- rule.increment(amount)
95
+ rule = new family, qualifier
96
+ rule.increment amount
94
97
  rule
95
98
  end
96
99
 
100
+ ##
97
101
  # Set append value.
98
102
  #
99
103
  # @param value [String]
@@ -104,6 +108,7 @@ module Google
104
108
  self
105
109
  end
106
110
 
111
+ ##
107
112
  # Set increment amount.
108
113
  #
109
114
  # @param amount [Integer]
@@ -24,6 +24,7 @@ require "google/cloud/bigtable/sample_row_key"
24
24
  module Google
25
25
  module Cloud
26
26
  module Bigtable
27
+ ##
27
28
  # # ReadOperations
28
29
  #
29
30
  # Collection of read-rows APIs.
@@ -33,6 +34,7 @@ module Google
33
34
  # * Read rows
34
35
  #
35
36
  module ReadOperations
37
+ ##
36
38
  # Reads sample row keys.
37
39
  #
38
40
  # Returns a sample of row keys in the table. The returned row keys will
@@ -45,7 +47,7 @@ module Google
45
47
  # Yield block for each processed SampleRowKey.
46
48
  #
47
49
  # @example
48
- # require "google/cloud"
50
+ # require "google/cloud/bigtable"
49
51
  #
50
52
  # bigtable = Google::Cloud::Bigtable.new
51
53
  # table = bigtable.table("my-instance", "my-table")
@@ -56,17 +58,15 @@ module Google
56
58
  # end
57
59
  #
58
60
  def sample_row_keys
59
- return enum_for(:sample_row_keys) unless block_given?
61
+ return enum_for :sample_row_keys unless block_given?
60
62
 
61
- response = client.sample_row_keys(
62
- path,
63
- app_profile_id: @app_profile_id
64
- )
63
+ response = client.sample_row_keys path, app_profile_id: @app_profile_id
65
64
  response.each do |grpc|
66
- yield SampleRowKey.from_grpc(grpc)
65
+ yield SampleRowKey.from_grpc grpc
67
66
  end
68
67
  end
69
68
 
69
+ ##
70
70
  # Reads rows.
71
71
  #
72
72
  # Streams back the contents of all requested rows in key order, optionally
@@ -112,7 +112,7 @@ module Google
112
112
  # bigtable = Google::Cloud::Bigtable.new
113
113
  # table = bigtable.table("my-instance", "my-table")
114
114
  #
115
- # range = table.row_range.between("user-1", "user-100")
115
+ # range = table.new_row_range.between("user-1", "user-100")
116
116
  #
117
117
  # table.read_rows(ranges: range).each do |row|
118
118
  # puts row
@@ -148,43 +148,25 @@ module Google
148
148
  # puts row
149
149
  # end
150
150
  #
151
- def read_rows \
152
- keys: nil,
153
- ranges: nil,
154
- filter: nil,
155
- limit: nil,
156
- &block
157
- unless block_given?
158
- return enum_for(
159
- :read_rows,
160
- keys: keys,
161
- ranges: ranges,
162
- filter: filter,
163
- limit: limit
164
- )
165
- end
166
- row_set = build_row_set(keys, ranges)
151
+ def read_rows keys: nil, ranges: nil, filter: nil, limit: nil, &block
152
+ return enum_for :read_rows, keys: keys, ranges: ranges, filter: filter, limit: limit unless block_given?
153
+
154
+ row_set = build_row_set keys, ranges
167
155
  rows_limit = limit
168
156
  rows_filter = filter.to_grpc if filter
169
- rows_reader = RowsReader.new(self)
157
+ rows_reader = RowsReader.new self
170
158
 
171
159
  begin
172
- rows_reader.read(
173
- rows: row_set,
174
- filter: rows_filter,
175
- rows_limit: rows_limit,
176
- &block
177
- )
160
+ rows_reader.read rows: row_set, filter: rows_filter, rows_limit: rows_limit, &block
178
161
  rescue *RowsReader::RETRYABLE_ERRORS => e
179
162
  rows_reader.retry_count += 1
180
- unless rows_reader.retryable?
181
- raise Google::Cloud::Error.from_error(e)
182
- end
183
- rows_limit, row_set = rows_reader.retry_options(limit, row_set)
163
+ raise Google::Cloud::Error.from_error(e) unless rows_reader.retryable?
164
+ rows_limit, row_set = rows_reader.retry_options limit, row_set
184
165
  retry
185
166
  end
186
167
  end
187
168
 
169
+ ##
188
170
  # Reads a single row by row key.
189
171
  #
190
172
  # @param key [String] Row key. Required
@@ -216,6 +198,7 @@ module Google
216
198
  read_rows(keys: [key], filter: filter).first
217
199
  end
218
200
 
201
+ ##
219
202
  # Creates a new instance of ValueRange.
220
203
  #
221
204
  # @return [Google::Cloud::Bigtable::ValueRange]
@@ -226,7 +209,7 @@ module Google
226
209
  # bigtable = Google::Cloud::Bigtable.new
227
210
  # table = bigtable.table("my-instance", "my-table")
228
211
  #
229
- # range = table.value_range
212
+ # range = table.new_value_range
230
213
  # range.from("abc")
231
214
  # range.to("xyz")
232
215
  #
@@ -245,6 +228,7 @@ module Google
245
228
  Google::Cloud::Bigtable::ValueRange.new
246
229
  end
247
230
 
231
+ ##
248
232
  # Get a new instance of ColumnRange.
249
233
  #
250
234
  # @param family [String] Column family name
@@ -256,7 +240,7 @@ module Google
256
240
  # bigtable = Google::Cloud::Bigtable.new
257
241
  # table = bigtable.table("my-instance", "my-table")
258
242
  #
259
- # range = table.column_range("test-family")
243
+ # range = table.new_column_range("test-family")
260
244
  # range.from("abc")
261
245
  # range.to("xyz")
262
246
  #
@@ -272,9 +256,10 @@ module Google
272
256
  # range = table.new_column_range("test-family").from("key-1", inclusive: false).to("key-5")
273
257
  #
274
258
  def new_column_range family
275
- Google::Cloud::Bigtable::ColumnRange.new(family)
259
+ Google::Cloud::Bigtable::ColumnRange.new family
276
260
  end
277
261
 
262
+ ##
278
263
  # Get a new instance of RowRange.
279
264
  #
280
265
  # @return [Google::Cloud::Bigtable::RowRange]
@@ -285,7 +270,7 @@ module Google
285
270
  # bigtable = Google::Cloud::Bigtable.new
286
271
  # table = bigtable.table("my-instance", "my-table")
287
272
  #
288
- # range = table.row_range
273
+ # range = table.new_row_range
289
274
  # range.from("abc")
290
275
  # range.to("xyz")
291
276
  #
@@ -304,6 +289,7 @@ module Google
304
289
  Google::Cloud::Bigtable::RowRange.new
305
290
  end
306
291
 
292
+ ##
307
293
  # Gets a row filter.
308
294
  #
309
295
  # @return [Google::Cloud::Bigtable::RowRange]
@@ -322,6 +308,7 @@ module Google
322
308
 
323
309
  private
324
310
 
311
+ ##
325
312
  # Builds a RowSet object from row keys and row ranges.
326
313
  #
327
314
  # @param row_keys [Array<String>]
@@ -333,11 +320,11 @@ module Google
333
320
  row_set[:row_keys] = row_keys if row_keys
334
321
 
335
322
  if row_ranges
336
- row_ranges = [row_ranges] unless row_ranges.instance_of?(Array)
323
+ row_ranges = [row_ranges] unless row_ranges.instance_of? Array
337
324
  row_set[:row_ranges] = row_ranges.map(&:to_grpc)
338
325
  end
339
326
 
340
- Google::Bigtable::V2::RowSet.new(row_set)
327
+ Google::Bigtable::V2::RowSet.new row_set
341
328
  end
342
329
  end
343
330
  end
@@ -0,0 +1,171 @@
1
+ # Copyright 2019 Google LLC
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # https://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+
16
+ module Google
17
+ module Cloud
18
+ module Bigtable
19
+ ##
20
+ # # RoutingPolicy
21
+ #
22
+ # An abstract routing policy.
23
+ #
24
+ # See subclasses for concrete implementations:
25
+ # * {Google::Cloud::Bigtable::MultiClusterRoutingUseAny} - Read/write
26
+ # requests may be routed to any cluster in the instance and will
27
+ # fail over to another cluster in the event of transient errors or
28
+ # delays. Choosing this option sacrifices read-your-writes
29
+ # consistency to improve availability.
30
+ # * {Google::Cloud::Bigtable::SingleClusterRouting} - Unconditionally
31
+ # routes all read/write requests to a specific cluster. This option
32
+ # preserves read-your-writes consistency but does not improve
33
+ # availability. Value contains `cluster_id` and optional field
34
+ # `allow_transactional_writes`.
35
+ #
36
+ # @example Create an app profile with a single cluster routing policy
37
+ # require "google/cloud/bigtable"
38
+ #
39
+ # bigtable = Google::Cloud::Bigtable.new
40
+ #
41
+ # instance = bigtable.instance("my-instance")
42
+ #
43
+ # routing_policy = Google::Cloud::Bigtable::AppProfile.single_cluster_routing(
44
+ # "my-instance-cluster-1",
45
+ # allow_transactional_writes: true
46
+ # )
47
+ #
48
+ # app_profile = instance.create_app_profile(
49
+ # "my-app-profile",
50
+ # routing_policy,
51
+ # description: "App profile for user data instance"
52
+ # )
53
+ # puts app_profile.routing_policy
54
+ #
55
+ # @example Create an app profile with multi-cluster routing policy
56
+ # require "google/cloud/bigtable"
57
+ #
58
+ # bigtable = Google::Cloud::Bigtable.new
59
+ #
60
+ # instance = bigtable.instance("my-instance")
61
+ #
62
+ # routing_policy = Google::Cloud::Bigtable::AppProfile.multi_cluster_routing
63
+ #
64
+ # app_profile = instance.create_app_profile(
65
+ # "my-app-profile",
66
+ # routing_policy,
67
+ # description: "App profile for user data instance"
68
+ # )
69
+ # puts app_profile.routing_policy
70
+ #
71
+ class RoutingPolicy
72
+ end
73
+
74
+ ##
75
+ # A multi-cluster routing policy for all read/write requests that use the
76
+ # associated app profile.
77
+ #
78
+ # Read/write requests may be routed to any cluster in the instance, and will
79
+ # fail over to another cluster in the event of transient errors or delays.
80
+ # Choosing this option sacrifices read-your-writes consistency to improve
81
+ # availability.
82
+ #
83
+ # @example
84
+ # require "google/cloud/bigtable"
85
+ #
86
+ # bigtable = Google::Cloud::Bigtable.new
87
+ #
88
+ # instance = bigtable.instance("my-instance")
89
+ #
90
+ # routing_policy = Google::Cloud::Bigtable::AppProfile.multi_cluster_routing
91
+ #
92
+ # app_profile = instance.create_app_profile(
93
+ # "my-app-profile",
94
+ # routing_policy,
95
+ # description: "App profile for user data instance"
96
+ # )
97
+ # puts app_profile.routing_policy
98
+ #
99
+ class MultiClusterRoutingUseAny < RoutingPolicy
100
+ # @private
101
+ def to_grpc
102
+ Google::Bigtable::Admin::V2::AppProfile::MultiClusterRoutingUseAny.new
103
+ end
104
+ end
105
+
106
+ ##
107
+ # A single-cluster routing policy for all read/write requests that use the
108
+ # associated app profile.
109
+ #
110
+ # Unconditionally routes all read/write requests to a specific cluster.
111
+ # This option preserves read-your-writes consistency, but does not improve
112
+ # availability.
113
+ #
114
+ # @example
115
+ # require "google/cloud/bigtable"
116
+ #
117
+ # bigtable = Google::Cloud::Bigtable.new
118
+ #
119
+ # instance = bigtable.instance("my-instance")
120
+ #
121
+ # routing_policy = Google::Cloud::Bigtable::AppProfile.single_cluster_routing(
122
+ # "my-instance-cluster-1",
123
+ # allow_transactional_writes: true
124
+ # )
125
+ #
126
+ # app_profile = instance.create_app_profile(
127
+ # "my-app-profile",
128
+ # routing_policy,
129
+ # description: "App profile for user data instance"
130
+ # )
131
+ # puts app_profile.routing_policy
132
+ #
133
+ # @!attribute [rw] cluster_id
134
+ # @return [String]
135
+ # The cluster to which read/write requests should be routed.
136
+ # @!attribute [rw] allow_transactional_writes
137
+ # @return [true, false]
138
+ # If true, `CheckAndMutateRow` and `ReadModifyWriteRow` requests are
139
+ # allowed by this app profile. It is unsafe to send these requests to
140
+ # the same table/row/column in multiple clusters.
141
+ # Default value is false.
142
+ class SingleClusterRouting < RoutingPolicy
143
+ attr_reader :cluster_id, :allow_transactional_writes
144
+
145
+ ##
146
+ # Creates a new single-cluster routing policy.
147
+ #
148
+ # @param cluster_id [String] The cluster to which read/write requests
149
+ # should be routed.
150
+ # @param allow_transactional_writes [Boolean]
151
+ # If true, `CheckAndMutateRow` and `ReadModifyWriteRow` requests are
152
+ # allowed by this app profile. It is unsafe to send these requests to
153
+ # the same table/row/column in multiple clusters.
154
+ # Default value is false.
155
+ #
156
+ def initialize cluster_id, allow_transactional_writes
157
+ @cluster_id = cluster_id
158
+ @allow_transactional_writes = allow_transactional_writes
159
+ end
160
+
161
+ # @private
162
+ def to_grpc
163
+ Google::Bigtable::Admin::V2::AppProfile::SingleClusterRouting.new(
164
+ cluster_id: cluster_id,
165
+ allow_transactional_writes: allow_transactional_writes
166
+ )
167
+ end
168
+ end
169
+ end
170
+ end
171
+ end
@@ -18,16 +18,21 @@
18
18
  module Google
19
19
  module Cloud
20
20
  module Bigtable
21
+ ##
21
22
  # # Row
22
23
  #
23
24
  # Row structure based on merged cells using read row state.
25
+ #
24
26
  class Row
27
+ ##
25
28
  # Cell
26
29
  #
27
30
  # Row cell built from data chunks.
31
+ #
28
32
  class Cell
29
33
  attr_reader :family, :qualifier, :value, :labels, :timestamp
30
34
 
35
+ ##
31
36
  # Creates a row cell instance.
32
37
  #
33
38
  # @param family [String] Column family name
@@ -35,7 +40,7 @@ module Google
35
40
  # @param timestamp [Integer] Timestamp in microseconds
36
41
  # @param value [String] Cell value
37
42
  # @param labels [Array<String>] List of label array
38
-
43
+ #
39
44
  def initialize family, qualifier, timestamp, value, labels = []
40
45
  @family = family
41
46
  @qualifier = qualifier
@@ -44,6 +49,7 @@ module Google
44
49
  @labels = labels
45
50
  end
46
51
 
52
+ ##
47
53
  # Converts timestamp to Time instance.
48
54
  #
49
55
  # @param granularity [Symbol] Optional
@@ -53,16 +59,17 @@ module Google
53
59
  #
54
60
  def to_time granularity = nil
55
61
  return nil if @timestamp.zero?
56
- return Time.at(@timestamp / 100_0000.0) if granularity == :micros
57
- Time.at(@timestamp / 1000.0)
62
+ return Time.at @timestamp / 1_000_000.0 if granularity == :micros
63
+ Time.at @timestamp / 1000.0
58
64
  end
59
65
 
66
+ ##
60
67
  # Converts a value to an integer.
61
68
  #
62
69
  # @return [Integer]
63
70
  #
64
71
  def to_i
65
- @value.unpack("q>").first
72
+ @value.unpack1 "q>"
66
73
  end
67
74
 
68
75
  # @private
@@ -80,12 +87,17 @@ module Google
80
87
  end
81
88
  end
82
89
 
90
+ ##
83
91
  # @return [String] Row key
92
+ #
84
93
  attr_accessor :key
85
94
 
95
+ ##
86
96
  # @return [Hash{String => Array<Google::Cloud::Bigtable::Row::Cell>}] Row cells
97
+ #
87
98
  attr_accessor :cells
88
99
 
100
+ ##
89
101
  # Creates a flat row object.
90
102
  #
91
103
  # @param key [String] Row key name
@@ -95,6 +107,7 @@ module Google
95
107
  @cells = Hash.new { |h, k| h[k] = [] }
96
108
  end
97
109
 
110
+ ##
98
111
  # List of column families names
99
112
  #
100
113
  # @return [Array<String>]
@@ -111,9 +124,7 @@ module Google
111
124
  #
112
125
  def == other
113
126
  return false unless self.class == other.class
114
- if key != other.key || column_families != other.column_families
115
- return false
116
- end
127
+ return false if key != other.key || column_families != other.column_families
117
128
 
118
129
  cells.all? do |family, list|
119
130
  list == other.cells[family]