ok_hbase 0.0.7 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -39,6 +39,9 @@ module OkHbase
39
39
  table.delete(row_key)
40
40
  end
41
41
 
42
+ def increment(increment)
43
+ self.table.increment(_new_increment(increment))
44
+ end
42
45
 
43
46
  def method_missing(method, *arguments, &block)
44
47
  if method.to_s[-1, 1] == '='
@@ -80,6 +83,11 @@ module OkHbase
80
83
  encoded
81
84
  end
82
85
 
86
+ def _new_increment(args)
87
+ args[:row] = self.row_key
88
+ args
89
+ end
90
+
83
91
  end
84
92
  end
85
93
  end
@@ -86,7 +86,7 @@ module OkHbase
86
86
  self.connection.client.getRowsWithColumns(self.connection.table_name(table_name), row_keys, columns, {})
87
87
  end
88
88
 
89
- rows.map { |row| [row.row, _make_row(row.columns, include_timestamp) ]}
89
+ rows.map { |row| [row.row, _make_row(row.columns, include_timestamp)] }
90
90
  end
91
91
 
92
92
  def cells(row_key, column, versions = nil, timestamp = nil, include_timestamp = nil)
@@ -204,6 +204,15 @@ module OkHbase
204
204
  counter_inc(row_key, column, -value)
205
205
  end
206
206
 
207
+ def increment(increment)
208
+ self.connection.increment(_new_increment(increment))
209
+ end
210
+
211
+ def increment_rows(increments)
212
+ increments.map! { |i| _new_increment(i) }
213
+ self.connection.increment(_new_increment(increments))
214
+ end
215
+
207
216
  alias_method :find, :scan
208
217
 
209
218
  def _column_family_names()
@@ -236,6 +245,11 @@ module OkHbase
236
245
  end
237
246
  row
238
247
  end
248
+
249
+ def _new_increment(args)
250
+ args[:table] = self.table_name
251
+ args
252
+ end
239
253
  end
240
254
  end
241
255
  end
@@ -143,6 +143,15 @@ module OkHbase
143
143
  table_prefix && !name.start_with?(table_prefix) ? [table_prefix, name].join(table_prefix_separator) : name
144
144
  end
145
145
 
146
+ def increment(increment)
147
+ client.increment(_new_increment(increment))
148
+ end
149
+
150
+ def increment_rows(increments)
151
+ increments.map! { |i| _new_increment(i) }
152
+ client.incrementRows(increments)
153
+ end
154
+
146
155
  private
147
156
 
148
157
  def _refresh_thrift_client
@@ -151,5 +160,13 @@ module OkHbase
151
160
  protocol = Thrift::BinaryProtocolAccelerated.new(@transport)
152
161
  @client = OkHbase::Client.new(protocol, nil, max_tries)
153
162
  end
163
+
164
+ def _new_increment(args)
165
+ if args[:amount]
166
+ args[:ammount] ||= args[:amount]
167
+ args.delete(:amount)
168
+ end
169
+ Apache::Hadoop::Hbase::Thrift::TIncrement.new(args)
170
+ end
154
171
  end
155
172
  end
@@ -1,3 +1,3 @@
1
1
  module OkHbase
2
- VERSION = "0.0.7"
2
+ VERSION = "0.0.8"
3
3
  end
@@ -2,60 +2,70 @@ require 'spec_helper'
2
2
 
3
3
  module OkHbase
4
4
  describe Connection do
5
- describe ".create_table" do
6
- let(:conn) { Connection.new auto_connect: true, timeout: 60 }
7
-
8
- it "should create tables with the right column families" do
9
- name = "ok_hbase_test_table"
10
- column_families = {
11
- 'a' => {
12
- 'max_versions' => 5,
13
- 'compression' => 'GZ',
14
- 'in_memory' => true,
15
- 'bloom_filter_type' => 'ROW',
16
- 'block_cache_enabled' => true,
17
-
18
- #TODO find out if these aren't being set properly or just aren't reported properly. 0 is the default
19
- 'bloom_filter_vector_size' => 0,
20
- 'bloom_filter_nb_hashes' => 0,
21
-
22
- #TODO find out why this doesn't get reported properly. -1 is the default
23
- 'time_to_live' => -1
24
- },
25
- 'b' => {
26
- 'max_versions' => 15,
27
- 'compression' => 'NONE',
28
- 'in_memory' => true,
29
- 'bloom_filter_type' => 'ROWCOL',
30
- 'block_cache_enabled' => true,
31
-
32
- 'bloom_filter_vector_size' => 0,
33
- 'bloom_filter_nb_hashes' => 0,
34
-
35
- 'time_to_live' => -1
36
- }
37
- }
38
-
39
- expected_families = Hash[column_families.map { |cf, data| [cf, { 'name' => "#{cf}:" }.merge(data)] }]
5
+ let(:connect_options) { {} }
6
+ let(:conn) { Connection.new(connect_options) }
7
+
8
+ let(:test_table_name) { 'ok_hbase_test_table' }
9
+ let(:test_table_column_families) {
10
+ {
11
+ 'a' => {
12
+ 'max_versions' => 5,
13
+ 'compression' => 'GZ',
14
+ 'in_memory' => true,
15
+ 'bloom_filter_type' => 'ROW',
16
+ 'block_cache_enabled' => true,
17
+
18
+ #TODO find out if these aren't being set properly or just aren't reported properly. 0 is the default
19
+ 'bloom_filter_vector_size' => 0,
20
+ 'bloom_filter_nb_hashes' => 0,
21
+
22
+ #TODO find out why this doesn't get reported properly. -1 is the default
23
+ 'time_to_live' => -1
24
+ },
25
+ 'b' => {
26
+ 'max_versions' => 15,
27
+ 'compression' => 'NONE',
28
+ 'in_memory' => true,
29
+ 'bloom_filter_type' => 'ROWCOL',
30
+ 'block_cache_enabled' => true,
31
+
32
+ 'bloom_filter_vector_size' => 0,
33
+ 'bloom_filter_nb_hashes' => 0,
34
+
35
+ 'time_to_live' => -1
36
+ }
37
+ }
38
+ }
39
+
40
+ let(:test_table) {
41
+ conn.table(test_table_name) || conn.create_table(test_table_name, test_table_column_families)
42
+ }
43
+
44
+
45
+ describe '#create_table' do
46
+ before { connect_options[:timeout] = 60 }
47
+ after { conn.delete_table(test_table_name, true) }
48
+
49
+ it 'should create tables with the right column families' do
50
+
51
+ expected_families = Hash[test_table_column_families.map { |cf, data| [cf, { 'name' => "#{cf}:" }.merge(data)] }]
40
52
 
41
53
  #sanity check
42
- conn.tables.should_not include(name)
54
+ conn.tables.should_not include(test_table_name)
43
55
 
44
- conn.create_table(name, column_families)
56
+ conn.create_table(test_table_name, test_table_column_families)
45
57
 
46
- conn.tables.should include(name)
58
+ conn.tables.should include(test_table_name)
47
59
 
48
- table = conn.table(name)
60
+ table = conn.table(test_table_name)
49
61
 
50
62
  table.families.should == expected_families
51
63
 
52
- #cleanup
53
- conn.delete_table(name, true)
54
64
  end
55
65
 
56
66
 
57
- it "should create tables with the right name" do
58
- name = "ok_hbase_test_table"
67
+ it 'should create tables with the right name' do
68
+ name = 'ok_hbase_test_table'
59
69
  column_families = {
60
70
  'd' => {}
61
71
  }
@@ -66,34 +76,67 @@ module OkHbase
66
76
  conn.create_table(name, column_families)
67
77
 
68
78
  conn.tables.should include(name)
69
-
70
- #cleanup
71
- conn.delete_table(name, true)
72
79
  end
73
80
  end
74
81
 
75
- describe ".open" do
76
- let(:conn) { Connection.new }
82
+ describe '#open' do
83
+ before { connect_options[:auto_connect] = false }
77
84
 
78
- it "should open a connection" do
85
+ it 'should open a connection' do
79
86
  expect { conn.open }.to change { conn.open? }.to(true)
80
87
  end
81
88
  end
82
89
 
83
- describe ".close" do
84
- let(:conn) { Connection.new auto_connect: true }
90
+ describe '#close' do
91
+ before { connect_options[:auto_connect] = true }
85
92
 
86
- it "should close a connection" do
93
+ it 'should close a connection' do
87
94
  expect { conn.close }.to change { conn.open? }.to(false)
88
95
  end
89
96
  end
90
97
 
91
- describe ".tables" do
92
- let(:conn) { Connection.new auto_connect: true }
98
+ describe '#tables' do
99
+ before { connect_options[:auto_connect] = true }
93
100
 
94
- it "should return an array of table names" do
101
+ it 'should return an array of table names' do
95
102
  conn.tables.should be_an Array
96
103
  end
97
104
  end
105
+
106
+ describe '#increment' do
107
+ before do
108
+ connect_options[:auto_connect] = true
109
+ conn.create_table(test_table_name, test_table_column_families)
110
+
111
+ test_table.put('test_row', { 'a:test_column' => [0].pack('Q>*') })
112
+ end
113
+
114
+ after { conn.delete_table(test_table_name, true) }
115
+
116
+ it 'should increment the right cell by the expected amount' do
117
+ expect {
118
+ conn.increment(table: test_table_name, row: 'test_row', column: 'a:test_column', amount: 2)
119
+ }.to change { test_table.row('test_row')['a:test_column'] }.to([2].pack('Q>*'))
120
+ end
121
+ end
122
+
123
+ describe '#increment_rows' do
124
+ before do
125
+ connect_options[:auto_connect] = true
126
+ conn.create_table(test_table_name, test_table_column_families)
127
+
128
+ test_table.put('test_row1', { 'a:test_column' => [0].pack('Q>*') })
129
+ test_table.put('test_row2', { 'a:test_column' => [1].pack('Q>*') })
130
+ end
131
+
132
+ after { conn.delete_table(test_table_name, true) }
133
+
134
+ it 'should increment the right cells by the expected amounts' do
135
+ expect {
136
+ conn.increment_rows([{ table: test_table_name, row: 'test_row1', column: 'a:test_column', amount: 2 },
137
+ { table: test_table_name, row: 'test_row2', column: 'a:test_column', amount: 3 }])
138
+ }.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>*')])
139
+ end
140
+ end
98
141
  end
99
142
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ok_hbase
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.8
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-08-25 00:00:00.000000000 Z
12
+ date: 2013-08-26 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: thrift