Skip to content

Commit

Permalink
added:
Browse files Browse the repository at this point in the history
Connection#increment
Connection#increment_rows
Table#increment
Table#increment_rows
Row#increment
  • Loading branch information
Nathan Keyes committed Aug 26, 2013
1 parent 9001c29 commit 5ab526b
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 56 deletions.
9 changes: 9 additions & 0 deletions lib/ok_hbase/concerns/row.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ def delete
table.delete(row_key)
end

def increment(increment)
self.table.increment(_new_increment(increment))
end

def method_missing(method, *arguments, &block)
if method.to_s[-1, 1] == '='
Expand Down Expand Up @@ -80,6 +83,12 @@ def _encode(value)
encoded
end

def _new_increment(args)
args[:table] = self.table.table_name
args[:row] = self.row_key
args
end

end
end
end
16 changes: 15 additions & 1 deletion lib/ok_hbase/concerns/table.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def rows(row_keys, columns = nil, timestamp = nil, include_timestamp = false)
self.connection.client.getRowsWithColumns(self.connection.table_name(table_name), row_keys, columns, {})
end

rows.map { |row| [row.row, _make_row(row.columns, include_timestamp) ]}
rows.map { |row| [row.row, _make_row(row.columns, include_timestamp)] }
end

def cells(row_key, column, versions = nil, timestamp = nil, include_timestamp = nil)
Expand Down Expand Up @@ -204,6 +204,15 @@ def counter_dec(row_key, column, value = 1)
counter_inc(row_key, column, -value)
end

def increment(increment)
self.connection.increment(_new_increment(increment))
end

def increment_rows(increments)
increments.map! { |i| _new_increment(i) }
self.connection.increment(_new_increment(increments))
end

alias_method :find, :scan

def _column_family_names()
Expand Down Expand Up @@ -236,6 +245,11 @@ def _make_row(cell_map, include_timestamp)
end
row
end

def _new_increment(args)
args[:table] = self.table_name
args
end
end
end
end
17 changes: 17 additions & 0 deletions lib/ok_hbase/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,15 @@ def table_name(name)
table_prefix && !name.start_with?(table_prefix) ? [table_prefix, name].join(table_prefix_separator) : name
end

def increment(increment)
client.increment(_new_increment(increment))
end

def increment_rows(increments)
increments.map! { |i| _new_increment(i) }
client.incrementRows(increments)
end

private

def _refresh_thrift_client
Expand All @@ -151,5 +160,13 @@ def _refresh_thrift_client
protocol = Thrift::BinaryProtocolAccelerated.new(@transport)
@client = OkHbase::Client.new(protocol, nil, max_tries)
end

def _new_increment(args)
if args[:amount]
args[:ammount] ||= args[:amount]
args.delete(:amount)
end
Apache::Hadoop::Hbase::Thrift::TIncrement.new(args)
end
end
end
153 changes: 98 additions & 55 deletions spec/ok_hbase/connection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,60 +2,70 @@

module OkHbase
describe Connection do
describe ".create_table" do
let(:conn) { Connection.new auto_connect: true, timeout: 60 }

it "should create tables with the right column families" do
name = "ok_hbase_test_table"
column_families = {
'a' => {
'max_versions' => 5,
'compression' => 'GZ',
'in_memory' => true,
'bloom_filter_type' => 'ROW',
'block_cache_enabled' => true,

#TODO find out if these aren't being set properly or just aren't reported properly. 0 is the default
'bloom_filter_vector_size' => 0,
'bloom_filter_nb_hashes' => 0,

#TODO find out why this doesn't get reported properly. -1 is the default
'time_to_live' => -1
},
'b' => {
'max_versions' => 15,
'compression' => 'NONE',
'in_memory' => true,
'bloom_filter_type' => 'ROWCOL',
'block_cache_enabled' => true,

'bloom_filter_vector_size' => 0,
'bloom_filter_nb_hashes' => 0,

'time_to_live' => -1
}
}

expected_families = Hash[column_families.map { |cf, data| [cf, { 'name' => "#{cf}:" }.merge(data)] }]
let(:connect_options) { {} }
let(:conn) { Connection.new(connect_options) }

let(:test_table_name) { 'ok_hbase_test_table' }
let(:test_table_column_families) {
{
'a' => {
'max_versions' => 5,
'compression' => 'GZ',
'in_memory' => true,
'bloom_filter_type' => 'ROW',
'block_cache_enabled' => true,

#TODO find out if these aren't being set properly or just aren't reported properly. 0 is the default
'bloom_filter_vector_size' => 0,
'bloom_filter_nb_hashes' => 0,

#TODO find out why this doesn't get reported properly. -1 is the default
'time_to_live' => -1
},
'b' => {
'max_versions' => 15,
'compression' => 'NONE',
'in_memory' => true,
'bloom_filter_type' => 'ROWCOL',
'block_cache_enabled' => true,

'bloom_filter_vector_size' => 0,
'bloom_filter_nb_hashes' => 0,

'time_to_live' => -1
}
}
}

let(:test_table) {
conn.table(test_table_name) || conn.create_table(test_table_name, test_table_column_families)
}


describe '#create_table' do
before { connect_options[:timeout] = 60 }
after { conn.delete_table(test_table_name, true) }

it 'should create tables with the right column families' do

expected_families = Hash[test_table_column_families.map { |cf, data| [cf, { 'name' => "#{cf}:" }.merge(data)] }]

#sanity check
conn.tables.should_not include(name)
conn.tables.should_not include(test_table_name)

conn.create_table(name, column_families)
conn.create_table(test_table_name, test_table_column_families)

conn.tables.should include(name)
conn.tables.should include(test_table_name)

table = conn.table(name)
table = conn.table(test_table_name)

table.families.should == expected_families

#cleanup
conn.delete_table(name, true)
end


it "should create tables with the right name" do
name = "ok_hbase_test_table"
it 'should create tables with the right name' do
name = 'ok_hbase_test_table'
column_families = {
'd' => {}
}
Expand All @@ -66,34 +76,67 @@ module OkHbase
conn.create_table(name, column_families)

conn.tables.should include(name)

#cleanup
conn.delete_table(name, true)
end
end

describe ".open" do
let(:conn) { Connection.new }
describe '#open' do
before { connect_options[:auto_connect] = false }

it "should open a connection" do
it 'should open a connection' do
expect { conn.open }.to change { conn.open? }.to(true)
end
end

describe ".close" do
let(:conn) { Connection.new auto_connect: true }
describe '#close' do
before { connect_options[:auto_connect] = true }

it "should close a connection" do
it 'should close a connection' do
expect { conn.close }.to change { conn.open? }.to(false)
end
end

describe ".tables" do
let(:conn) { Connection.new auto_connect: true }
describe '#tables' do
before { connect_options[:auto_connect] = true }

it "should return an array of table names" do
it 'should return an array of table names' do
conn.tables.should be_an Array
end
end

describe '#increment' do
before do
connect_options[:auto_connect] = true
conn.create_table(test_table_name, test_table_column_families)

test_table.put('test_row', { 'a:test_column' => [0].pack('Q>*') })
end

after { conn.delete_table(test_table_name, true) }

it 'should increment the right cell by the expected amount' do
expect {
conn.increment(table: test_table_name, row: 'test_row', column: 'a:test_column', amount: 2)
}.to change { test_table.row('test_row')['a:test_column'] }.to([2].pack('Q>*'))
end
end

describe '#increment_rows' do
before do
connect_options[:auto_connect] = true
conn.create_table(test_table_name, test_table_column_families)

test_table.put('test_row1', { 'a:test_column' => [0].pack('Q>*') })
test_table.put('test_row2', { 'a:test_column' => [1].pack('Q>*') })
end

after { conn.delete_table(test_table_name, true) }

it 'should increment the right cells by the expected amounts' do
expect {
conn.increment_rows([{ table: test_table_name, row: 'test_row1', column: 'a:test_column', amount: 2 },
{ table: test_table_name, row: 'test_row2', column: 'a:test_column', amount: 3 }])
}.to change { [test_table.row('test_row1')['a:test_column'], test_table.row('test_row2')['a:test_column']] }.to([[2].pack('Q>*'), [4].pack('Q>*')])
end
end
end
end

0 comments on commit 5ab526b

Please sign in to comment.