Class: Moneta::Adapters::HBase

Inherits:
Object
  • Object
show all
Includes:
Defaults
Defined in:
lib/moneta/adapters/hbase.rb

Overview

HBase thrift backend

Instance Method Summary collapse

Methods included from Defaults

#[], #[]=, #decrement, #fetch

Methods included from OptionSupport

#expires, #prefix, #raw, #with

Constructor Details

#initialize(options = {}) ⇒ HBase

Returns a new instance of HBase.

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :host (String) — default: '127.0.0.1'

    Server host name

  • :port (Integer) — default: 9090

    Server port

  • :table (String) — default: 'moneta'

    Table name

  • :column_family (String) — default: 'moneta'

    Column family

  • :column (String) — default: 'value'

    Column



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/moneta/adapters/hbase.rb', line 16

def initialize(options = {})
  options[:host] ||= '127.0.0.1'
  options[:port] ||= '9090'
  options[:table] ||= 'moneta'
  options[:column] ||= 'value'
  cf = (options[:column_family] || 'moneta')
  @db = HBaseRb::Client.new(options[:host], options[:port])
  @db.create_table(options[:table], cf) unless @db.has_table?(options[:table])
  @table = @db.get_table(options[:table])
  @column = "#{cf}:#{options[:column]}"
end

Instance Method Details

#clear(options = {}) ⇒ void

This method returns an undefined value.

Clear all keys in this store

Parameters:

  • options (Hash) (defaults to: {})


65
66
67
68
69
70
# File 'lib/moneta/adapters/hbase.rb', line 65

def clear(options = {})
  @table.create_scanner do |row|
    @table.delete_row(row.row)
  end
  self
end

#closeObject

Explicitly close the store

Returns:

  • nil



73
74
75
76
# File 'lib/moneta/adapters/hbase.rb', line 73

def close
  @db.close
  nil
end

#delete(key, options = {}) ⇒ Object

Delete the key from the store and return the current value

Parameters:

  • key (Object)
  • options (Hash) (defaults to: {})

Returns:

  • (Object)

    current value



57
58
59
60
61
62
# File 'lib/moneta/adapters/hbase.rb', line 57

def delete(key, options = {})
  if value = load(key, options)
    @table.delete_row(key)
    unpack(value)
  end
end

#increment(key, amount = 1, options = {}) ⇒ Object

Note:

Not every Moneta store implements this method, a NotImplementedError is raised if it is not supported.

Atomically increment integer value with key

This method also accepts negative amounts.

Parameters:

  • key (Object)
  • amount (Integer) (defaults to: 1)
  • options (Hash) (defaults to: {})

Returns:

  • (Object)

    value from store



46
47
48
49
50
51
52
53
54
# File 'lib/moneta/adapters/hbase.rb', line 46

def increment(key, amount = 1, options = {})
  result = @table.atomic_increment(key, @column, amount)
  # HACK: Throw error if applied to invalid value
  if result == 0
    value = load(key)
    raise 'Tried to increment non integer value' unless value.to_s == value.to_i.to_s
  end
  result
end

#key?(key, options = {}) ⇒ Boolean

Exists the value with key

Parameters:

  • key (Object)
  • options (Hash) (defaults to: {})

Returns:

  • (Boolean)


29
30
31
# File 'lib/moneta/adapters/hbase.rb', line 29

def key?(key, options = {})
  @table.get(key, @column).first != nil
end

#load(key, options = {}) ⇒ Object

Fetch value with key. Return nil if the key doesn’t exist

Parameters:

  • key (Object)
  • options (Hash) (defaults to: {})

Returns:

  • (Object)

    value



34
35
36
37
# File 'lib/moneta/adapters/hbase.rb', line 34

def load(key, options = {})
  cell = @table.get(key, @column).first
  cell && unpack(cell.value)
end

#store(key, value, options = {}) ⇒ Object

Store value with key

Parameters:

  • key (Object)
  • value (Object)
  • options (Hash) (defaults to: {})

Returns:

  • value



40
41
42
43
# File 'lib/moneta/adapters/hbase.rb', line 40

def store(key, value, options = {})
  @table.mutate_row(key, @column => pack(value))
  value
end