Class: Moneta::Adapters::HBase

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

Overview

HBase thrift backend

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Defaults

#[], #[]=, #create, #decrement, #each_key, #features, #fetch, #fetch_values, included, #merge!, #slice, #supports?, #values_at

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

  • :backend (::HBaseRb::Client)

    Use existing backend instance


24
25
26
27
28
29
30
31
32
33
# File 'lib/moneta/adapters/hbase.rb', line 24

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

Instance Attribute Details

#backendObject (readonly)


10
11
12
# File 'lib/moneta/adapters/hbase.rb', line 10

def backend
  @backend
end

Instance Method Details

#clear(options = {}) ⇒ void

This method returns an undefined value.

Clear all keys in this store

Parameters:

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

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

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

#closeObject

Explicitly close the store

Returns:

  • nil


77
78
79
80
# File 'lib/moneta/adapters/hbase.rb', line 77

def close
  @backend.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 Transformer)

  • :prefix (String)

    Prefix key (See Transformer)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • (Object)

    current value


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

def delete(key, options = {})
  if value = load(key, options)
    @table.delete_row(key)
    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: {})

Options Hash (options):

  • :prefix (String)

    Prefix key (See Transformer)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • (Object)

    value from store


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

def increment(key, amount = 1, options = {})
  result = @table.atomic_increment(key, @column, amount)
  # HACK: Throw error if applied to invalid value
  Utils.to_int(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 Expires)

  • :prefix (String)

    Prefix key (See Transformer)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • (Boolean)

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

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 Expires)

  • :raw (Boolean)

    Raw access without value transformation (See Transformer)

  • :prefix (String)

    Prefix key (See Transformer)

  • :sync (Boolean)

    Synchronized load (Cache reloads from adapter, Daybreak syncs with file)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • (Object)

    value


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

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 Expires)

  • :raw (Boolean)

    Raw access without value transformation (See Transformer)

  • :prefix (String)

    Prefix key (See Transformer)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • value


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

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