Class: Google::Cloud::Bigtable::ColumnFamilyMap

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/google/cloud/bigtable/column_family_map.rb

Overview

Represents a table's column families.

See Project#create_table, Instance#create_table and Table#column_families.

Examples:

require "google/cloud/bigtable"

bigtable = Google::Cloud::Bigtable.new

column_families = Google::Cloud::Bigtable::ColumnFamilyMap.new

column_families.add "cf1", gc_rule: Google::Cloud::Bigtable::GcRule.max_age(600)
column_families.add "cf2", gc_rule: Google::Cloud::Bigtable::GcRule.max_versions(5)

Create a table with a block yielding a ColumnFamilyMap.

require "google/cloud/bigtable"

bigtable = Google::Cloud::Bigtable.new

table = bigtable.create_table("my-instance", "my-table") do |cfm|
  cfm.add('cf1', gc_rule: Google::Cloud::Bigtable::GcRule.max_versions(5))
  cfm.add('cf2', gc_rule: Google::Cloud::Bigtable::GcRule.max_age(600))

  gc_rule = Google::Cloud::Bigtable::GcRule.union(
    Google::Cloud::Bigtable::GcRule.max_age(1800),
    Google::Cloud::Bigtable::GcRule.max_versions(3)
  )
  cfm.add('cf3', gc_rule: gc_rule)
end

puts table.column_families

Update column families with a block yielding a ColumnFamilyMap.

require "google/cloud/bigtable"

bigtable = Google::Cloud::Bigtable.new

table = bigtable.table("my-instance", "my-table", perform_lookup: true)

table.column_families do |cfm|
  cfm.add "cf4", gc_rule: Google::Cloud::Bigtable::GcRule.max_age(600)
  cfm.add "cf5", gc_rule: Google::Cloud::Bigtable::GcRule.max_versions(5)

  rule_1 = Google::Cloud::Bigtable::GcRule.max_versions(3)
  rule_2 = Google::Cloud::Bigtable::GcRule.max_age(600)
  rule_union = Google::Cloud::Bigtable::GcRule.union(rule_1, rule_2)
  cfm.update "cf2", gc_rule: rule_union

  cfm.delete "cf3"
end

puts table.column_families["cf3"] #=> nil

Instance Method Summary collapse

Constructor Details

#initialize {|_self| ... } ⇒ ColumnFamilyMap

Creates a new ColumnFamilyMap object.

Examples:

require "google/cloud/bigtable"

bigtable = Google::Cloud::Bigtable.new

column_families = Google::Cloud::Bigtable::ColumnFamilyMap.new

column_families.add "cf1", gc_rule: Google::Cloud::Bigtable::GcRule.max_age(600)
column_families.add "cf2", gc_rule: Google::Cloud::Bigtable::GcRule.max_versions(5)

Create new column families using block syntax:

require "google/cloud/bigtable"

bigtable = Google::Cloud::Bigtable.new

column_families = Google::Cloud::Bigtable::ColumnFamilyMap.new do |cfm|
  cfm.add "cf1", gc_rule: Google::Cloud::Bigtable::GcRule.max_age(600)
  cfm.add "cf2", gc_rule: Google::Cloud::Bigtable::GcRule.max_versions(5)
end

Yields:

  • (_self)

Yield Parameters:



104
105
106
107
108
# File 'lib/google/cloud/bigtable/column_family_map.rb', line 104

def initialize
  @column_families = empty_map

  yield self if block_given?
end

Instance Method Details

#[](name) ⇒ ColumnFamily

Retrieves the ColumnFamily object corresponding to the name. If not found, returns nil.

Returns:



137
138
139
140
141
# File 'lib/google/cloud/bigtable/column_family_map.rb', line 137

def [] name
  return nil unless name? name

  ColumnFamily.from_grpc @column_families[name], name
end

#add(name, gc_rule: nil) ⇒ Object Also known as: create

Adds a new column family to the table.

Examples:

require "google/cloud/bigtable"

bigtable = Google::Cloud::Bigtable.new

table = bigtable.table("my-instance", "my-table", perform_lookup: true)

table.column_families do |column_families|
  column_families.add "cf4", gc_rule: Google::Cloud::Bigtable::GcRule.max_age(600)
  column_families.add "cf5", gc_rule: Google::Cloud::Bigtable::GcRule.max_versions(5)
end

Parameters:

  • name (String)

    Column family name.

  • gc_rule (Google::Cloud::Bigtable::GcRule) (defaults to: nil)

    The garbage collection rule to be used for the column family. Optional. The service default value will be used when not specified.

Raises:

  • (ArgumentError)

    if the column family name already exists.

  • (FrozenError)

    if the column family map is frozen.



206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/google/cloud/bigtable/column_family_map.rb', line 206

def add name, positional_gc_rule = nil, gc_rule: nil
  raise ArgumentError, "column family #{name.inspect} already exists" if @column_families.has_key? name

  gc_rule ||= positional_gc_rule
  if positional_gc_rule
    warn "The positional gc_rule argument is deprecated. Use the named gc_rule argument instead."
  end

  column_family = Google::Bigtable::Admin::V2::ColumnFamily.new
  column_family.gc_rule = gc_rule.to_grpc if gc_rule
  @column_families[name] = column_family

  nil
end

#delete(name) ⇒ Object Also known as: drop

Deletes the named column family from the table.

Examples:

require "google/cloud/bigtable"

bigtable = Google::Cloud::Bigtable.new

table = bigtable.table("my-instance", "my-table", perform_lookup: true)

table.column_families do |column_families|
  column_families.delete "cf3"
end

Parameters:

  • name (String)

    Column family name.

Raises:

  • (ArgumentError)

    if the column family name does not exist.

  • (FrozenError)

    if the column family map is frozen.



277
278
279
280
281
282
283
# File 'lib/google/cloud/bigtable/column_family_map.rb', line 277

def delete name
  raise ArgumentError, "column family #{name.inspect} does not exist" unless @column_families.has_key? name

  @column_families.delete name

  nil
end

#each {|name, column_family| ... } ⇒ Enumerator?

Calls the block once for each column family in the map, passing the name and column family pair as parameters.

If no block is given, an enumerator is returned instead.

Yields:

  • (name, column_family)

    The name and column family pair.

Yield Parameters:

  • name (String)

    the column family name.

  • column_family (ColumnFamily)

    the column family object.

Returns:

  • (Enumerator, nil)

    An enumerator is returned if no block is given, otherwise nil.



122
123
124
125
126
127
128
129
# File 'lib/google/cloud/bigtable/column_family_map.rb', line 122

def each
  return enum_for :each unless block_given?

  @column_families.each do |name, column_family_grpc|
    column_family = ColumnFamily.from_grpc column_family_grpc, name
    yield name, column_family
  end
end

#empty?Boolean

Returns true if the map contains no name and column family pairs.

Returns:

  • (Boolean)


178
179
180
# File 'lib/google/cloud/bigtable/column_family_map.rb', line 178

def empty?
  length.zero?
end

#lengthInteger Also known as: size

Returns the number of name and column family pairs in the map.

Returns:

  • (Integer)


168
169
170
# File 'lib/google/cloud/bigtable/column_family_map.rb', line 168

def length
  @column_families.length
end

#name?(name) ⇒ Boolean Also known as: key?

Returns true if the given name is present in the map.

Returns:

  • (Boolean)


148
149
150
# File 'lib/google/cloud/bigtable/column_family_map.rb', line 148

def name? name
  @column_families.has_key? name
end

#namesArray<String> Also known as: keys

Returns a new array populated with the names from the map.

Returns:

  • (Array<String>)


158
159
160
# File 'lib/google/cloud/bigtable/column_family_map.rb', line 158

def names
  @column_families.keys
end

#update(name, gc_rule: nil) ⇒ Object

Updates an existing column family in the table.

Examples:

require "google/cloud/bigtable"

bigtable = Google::Cloud::Bigtable.new

table = bigtable.table("my-instance", "my-table", perform_lookup: true)

table.column_families do |column_families|
  rule_1 = Google::Cloud::Bigtable::GcRule.max_versions(3)
  rule_2 = Google::Cloud::Bigtable::GcRule.max_age(600)
  rule_union = Google::Cloud::Bigtable::GcRule.union(rule_1, rule_2)

  column_families.update "cf2", gc_rule: rule_union
end

Parameters:

  • name (String)

    Column family name.

  • gc_rule (Google::Cloud::Bigtable::GcRule) (defaults to: nil)

    The new garbage collection rule to be used for the column family. Optional. The service default value will be used when not specified.

Raises:

  • (ArgumentError)

    if the column family name does not exist.

  • (FrozenError)

    if the column family map is frozen.



248
249
250
251
252
253
254
255
256
# File 'lib/google/cloud/bigtable/column_family_map.rb', line 248

def update name, gc_rule: nil
  raise ArgumentError, "column family #{name.inspect} does not exist" unless @column_families.has_key? name

  column_family = Google::Bigtable::Admin::V2::ColumnFamily.new
  column_family.gc_rule = gc_rule.to_grpc if gc_rule
  @column_families[name] = column_family

  nil
end