Class: Moneta::Adapters::HBase

Inherits:
Object
  • Object
show all
Includes:
Defaults, IncrementSupport
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



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

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: {})


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

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

#closeObject

Explicitly close the store

Returns:

  • nil



71
72
73
74
# File 'lib/moneta/adapters/hbase.rb', line 71

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: {})

Options Hash (options):

  • :raw (Boolean)

    Raw access without value transformation (See ‘Moneta::Transformer`)

  • :prefix (String)

    Prefix key (See ‘Moneta::Transformer`)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • (Object)

    current value



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

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



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

def increment(key, amount = 1, options = {})
  result = @table.atomic_increment(key, @column, amount)
  # HACK: Throw error if applied to invalid value
  convert_for_increment(load(key)) if result == 0
  result
end

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

Exists the value with key

Parameters:

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

Options Hash (options):

  • :expires (Integer)

    Update expiration time (See ‘Moneta::Expires`)

  • :prefix (String)

    Prefix key (See ‘Moneta::Transformer`)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • (Boolean)


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

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: {})

Options Hash (options):

  • :expires (Integer)

    Update expiration time (See ‘Moneta::Expires`)

  • :raw (Boolean)

    Raw access without value transformation (See ‘Moneta::Transformer`)

  • :prefix (String)

    Prefix key (See ‘Moneta::Transformer`)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • (Object)

    value



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

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: {})

Options Hash (options):

  • :expires (Integer)

    Set expiration time (See ‘Moneta::Expires`)

  • :raw (Boolean)

    Raw access without value transformation (See ‘Moneta::Transformer`)

  • :prefix (String)

    Prefix key (See ‘Moneta::Transformer`)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • value



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

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