Class: Google::Cloud::Bigtable::MutationEntry

Inherits:
Object
  • Object
show all
Defined in:
lib/google/cloud/bigtable/mutation_entry.rb

Overview

MutationEntry

MutationEntry is a chainable structure, which holds data for diffrent type of mutations. MutationEntry is used in following data operations

  • Mutate row. See Table#mutate_row
  • Mutate rows. See Table#mutate_rows
  • Check and mutate row using a predicate. see Table#check_and_mutate_row

Examples:

entry = Google::Cloud::Bigtable::MutationEntry.new("user-1")
entry.set_cell(
  "cf1", "fiel01", "XYZ", timestamp: Time.now.to_i * 1000
).delete_cells(
  "cf2",
  "field02",
  timestamp_from: (Time.now.to_i - 1800) * 1000,
  timestamp_to: (Time.now.to_i * 1000)
).delete_from_family("cf3").delete_from_row

Using table

require "google/cloud/bigtable"

bigtable = Google::Cloud::Bigtable.new
table = bigtable.table("my-instance", "my-table")

entry = table.new_mutation_entry("user-1")
entry.set_cell(
  "cf1", "fiel01", "XYZ", timestamp: Time.now.to_i * 1000
)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(row_key = nil) ⇒ MutationEntry

Creates a mutation entry instance.

Parameters:

  • row_key (String) (defaults to: nil)


64
65
66
67
68
# File 'lib/google/cloud/bigtable/mutation_entry.rb', line 64

def initialize row_key = nil
  @row_key = row_key
  @mutations = []
  @retryable = true
end

Instance Attribute Details

#mutationsArray<Google::Bigtable::V2::Mutation>

mutations gRPC list

Returns:



59
60
61
# File 'lib/google/cloud/bigtable/mutation_entry.rb', line 59

def mutations
  @mutations
end

#row_keyObject

Returns the value of attribute row_key



55
56
57
# File 'lib/google/cloud/bigtable/mutation_entry.rb', line 55

def row_key
  @row_key
end

Instance Method Details

#delete_cells(family, qualifier, timestamp_from: nil, timestamp_to: nil) ⇒ MutationEntry

Add DeleteFromColumn entry to list of mutations.

A Mutation which deletes cells from the specified column, optionally restricting the deletions to a given timestamp range.

Examples:

Without timestamp range

entry = Google::Cloud::Bigtable::MutationEntry.new("user-1")
entry.delete_cells("cf-1", "field-1")

With timestamp range

entry = Google::Cloud::Bigtable::MutationEntry.new("user-1")
entry.delete_cells(
  "cf1",
  "field-1",
  timestamp_from: (Time.now.to_i - 1800) * 1000,
  timestamp_to: (Time.now.to_i * 1000)
)

With lower bound timestamp range

entry = Google::Cloud::Bigtable::MutationEntry.new("user-1")
entry.delete_cells(
  "cf1",
  "field-1",
  timestamp_from: (Time.now.to_i - 1800) * 1000
)

Parameters:

  • family (String)

    Table column family name. The name of the family from which cells should be deleted. Must match [-_.a-zA-Z0-9]+

  • qualifier (String)

    Column qualifier name. The qualifier of the column from which cells should be deleted. Can be any byte string, including the empty string.

  • timestamp_from (Integer)

    Timestamp lower bound. Optional. The range of timestamps within which cells should be deleted.

  • timestamp_to (Integer)

    Timestamp upper bound. Optional. The range of timestamps within which cells should be deleted.

Returns:



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/google/cloud/bigtable/mutation_entry.rb', line 157

def delete_cells \
    family,
    qualifier,
    timestamp_from: nil,
    timestamp_to: nil
  grpc = Google::Bigtable::V2::Mutation::DeleteFromColumn.new(
    family_name: family,
    column_qualifier: qualifier
  )
  if timestamp_from || timestamp_to
    time_range = Google::Bigtable::V2::TimestampRange.new
    time_range.start_timestamp_micros = timestamp_from if timestamp_from
    time_range.end_timestamp_micros = timestamp_to if timestamp_to
    grpc.time_range = time_range
  end
  @mutations << Google::Bigtable::V2::Mutation.new(
    delete_from_column: grpc
  )
  self
end

#delete_from_family(family) ⇒ MutationEntry

Add DeleteFromFamily to list of mutations.

A Mutation which deletes all cells from the specified column family.

Examples:

entry = Google::Cloud::Bigtable::MutationEntry.new("user-1")
entry.delete_from_family("cf-1")

Parameters:

  • family (String)

    Table column family name. The name of the family from which cells should be deleted. Must match [-_.a-zA-Z0-9]+

Returns:



191
192
193
194
195
196
# File 'lib/google/cloud/bigtable/mutation_entry.rb', line 191

def delete_from_family family
  @mutations << Google::Bigtable::V2::Mutation.new(
    delete_from_family: { family_name: family }
  )
  self
end

#delete_from_rowMutationEntry

Add DeleteFromRow entry to list of mutations

A Mutation which deletes all cells from the containing row.

Examples:

entry = Google::Cloud::Bigtable::MutationEntry.new("user-1")
entry.delete_from_row

Returns:



208
209
210
211
# File 'lib/google/cloud/bigtable/mutation_entry.rb', line 208

def delete_from_row
  @mutations << Google::Bigtable::V2::Mutation.new(delete_from_row: {})
  self
end

#lengthInteger

No of mutations

Returns:

  • (Integer)


225
226
227
# File 'lib/google/cloud/bigtable/mutation_entry.rb', line 225

def length
  @mutations.length
end

#retryable?Boolean

Mutation entry is retryable or not based on set_cell value.

Returns:

  • (Boolean)


217
218
219
# File 'lib/google/cloud/bigtable/mutation_entry.rb', line 217

def retryable?
  @retryable
end

#set_cell(family, qualifier, value, timestamp: nil) ⇒ MutationEntry

Add SetCell mutation to list of mutations.

A Mutation which sets the value of the specified cell.

Examples:

entry = Google::Cloud::Bigtable::MutationEntry.new("user-1")
entry.set_cell("cf1", "field01", "XYZ")

With timestamp

entry = Google::Cloud::Bigtable::MutationEntry.new("user-1")
entry.set_cell(
  "cf-1",
  "field-1",
  "XYZ"
  timestamp: Time.now.to_i * 1000 # Time stamp in millis seconds.
)

Parameters:

  • family (String)

    Table column family name. The name of the family into which new data should be written. Must match [-_.a-zA-Z0-9]+

  • qualifier (String)

    Column qualifier name. The qualifier of the column into which new data should be written. Can be any byte string, including the empty string.

  • value (String, Integer)

    Cell value data. The value to be written into the specified cell.

  • timestamp (Time, Integer)

    Timestamp value. The timestamp of the cell into which new data should be written. Use -1 for current Bigtable server time. Otherwise, the client should set this value itself, noting that the default value is a timestamp of zero if the field is left unspecified. Values must match the granularity of the table (e.g. micros, millis).

Returns:



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/google/cloud/bigtable/mutation_entry.rb', line 103

def set_cell family, qualifier, value, timestamp: nil
  # If value is integer then covert it to sign 64 bit int big-endian.
  value = [value].pack("q>") if value.is_a?(Integer)
  options = {
    family_name: family,
    column_qualifier: qualifier,
    value: value
  }

  if timestamp
    options[:timestamp_micros] = timestamp
    @retryable = timestamp != -1
  end
  @mutations << Google::Bigtable::V2::Mutation.new(set_cell: options)
  self
end