Class: Moneta::Adapters::HBase
- Inherits:
-
Object
- Object
- Moneta::Adapters::HBase
- Includes:
- Defaults, IncrementSupport
- Defined in:
- lib/moneta/adapters/hbase.rb
Overview
HBase thrift backend
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
#[], #[]=, #decrement, #fetch
Methods included from OptionSupport
#expires, #prefix, #raw, #with
Constructor Details
#initialize(options = {}) ⇒ HBase
Returns a new instance of HBase.
17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/moneta/adapters/hbase.rb', line 17 def initialize( = {}) [:host] ||= '127.0.0.1' [:port] ||= '9090' [:table] ||= 'moneta' [:column] ||= 'value' cf = ([:column_family] || 'moneta') @db = HBaseRb::Client.new([:host], [:port]) @db.create_table([:table], cf) unless @db.has_table?([:table]) @table = @db.get_table([:table]) @column = "#{cf}:#{[:column]}" end |
Instance Method Details
#clear(options = {}) ⇒ void
This method returns an undefined value.
Clear all keys in this store
63 64 65 66 67 68 |
# File 'lib/moneta/adapters/hbase.rb', line 63 def clear( = {}) @table.create_scanner do |row| @table.delete_row(row.row) end self end |
#close ⇒ Object
Explicitly close the store
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
55 56 57 58 59 60 |
# File 'lib/moneta/adapters/hbase.rb', line 55 def delete(key, = {}) if value = load(key, ) @table.delete_row(key) unpack(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.
47 48 49 50 51 52 |
# File 'lib/moneta/adapters/hbase.rb', line 47 def increment(key, amount = 1, = {}) 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
30 31 32 |
# File 'lib/moneta/adapters/hbase.rb', line 30 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
35 36 37 38 |
# File 'lib/moneta/adapters/hbase.rb', line 35 def load(key, = {}) cell = @table.get(key, @column).first cell && unpack(cell.value) end |
#store(key, value, options = {}) ⇒ Object
Store value with key
41 42 43 44 |
# File 'lib/moneta/adapters/hbase.rb', line 41 def store(key, value, = {}) @table.mutate_row(key, @column => pack(value)) value end |