Class: Moneta::Adapters::HBase
- Inherits:
-
Object
- Object
- Moneta::Adapters::HBase
- Includes:
- Defaults
- Defined in:
- lib/moneta/adapters/hbase.rb
Overview
HBase thrift backend
Instance Attribute Summary collapse
- #backend ⇒ Object readonly
Instance Method Summary collapse
-
#clear(options = {}) ⇒ void
Clear all keys in this store.
-
#close ⇒ Object
Explicitly close the store.
-
#delete(key, options = {}) ⇒ Object
Delete the key from the store and return the current value.
-
#increment(key, amount = 1, options = {}) ⇒ Object
Atomically increment integer value with key.
-
#initialize(options = {}) ⇒ HBase
constructor
A new instance of HBase.
-
#key?(key, options = {}) ⇒ Boolean
Exists the value with key.
-
#load(key, options = {}) ⇒ Object
Fetch value with key.
-
#store(key, value, options = {}) ⇒ Object
Store value with key.
Methods included from Defaults
#[], #[]=, #create, #decrement, #each_key, #features, #fetch, #fetch_values, included, #merge!, #slice, #supports?, #update, #values_at
Methods included from OptionSupport
#expires, #prefix, #raw, #with
Constructor Details
#initialize(options = {}) ⇒ HBase
Returns a new instance of HBase.
24 25 26 27 28 29 30 31 32 33 |
# File 'lib/moneta/adapters/hbase.rb', line 24 def initialize( = {}) [:column] ||= 'value' [:table] ||= 'moneta' cf = ([:column_family] || 'moneta') @column = "#{cf}:#{[:column]}" @backend = [:backend] || HBaseRb::Client.new([:host] || '127.0.0.1', [:port] || '9090') @backend.create_table([:table], cf) unless @backend.has_table?([:table]) @table = @backend.get_table([:table]) end |
Instance Attribute Details
#backend ⇒ Object (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
69 70 71 72 73 74 |
# File 'lib/moneta/adapters/hbase.rb', line 69 def clear( = {}) @table.create_scanner do |row| @table.delete_row(row.row) end self end |
#close ⇒ Object
Explicitly close the store
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
61 62 63 64 65 66 |
# File 'lib/moneta/adapters/hbase.rb', line 61 def delete(key, = {}) if value = load(key, ) @table.delete_row(key) value end end |
#increment(key, amount = 1, options = {}) ⇒ Object
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.
53 54 55 56 57 58 |
# File 'lib/moneta/adapters/hbase.rb', line 53 def increment(key, amount = 1, = {}) result = @table.atomic_increment(key, @column, amount) # HACK: Throw error if applied to invalid value Integer(load(key)) if result == 0 result end |
#key?(key, options = {}) ⇒ Boolean
Exists the value with key
36 37 38 |
# File 'lib/moneta/adapters/hbase.rb', line 36 def key?(key, = {}) @table.get(key, @column).first != nil end |
#load(key, options = {}) ⇒ Object
Fetch value with key. Return nil if the key doesn’t exist
41 42 43 44 |
# File 'lib/moneta/adapters/hbase.rb', line 41 def load(key, = {}) cell = @table.get(key, @column).first cell && unpack(cell.value) end |
#store(key, value, options = {}) ⇒ Object
Store value with key
47 48 49 50 |
# File 'lib/moneta/adapters/hbase.rb', line 47 def store(key, value, = {}) @table.mutate_row(key, @column => pack(value)) value end |