Skip to content
This repository has been archived by the owner on Nov 30, 2018. It is now read-only.

Commit

Permalink
Decided to make the timestamp definitions explicit, instead of conver…
Browse files Browse the repository at this point in the history
…ting from seconds to milliseconds underneath. This is because reading back a row whose timestamp you defined will give back a different value, and that's strange behaviour.
  • Loading branch information
greglu committed Nov 26, 2011
1 parent 50b7837 commit 0754675
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 22 deletions.
13 changes: 3 additions & 10 deletions lib/stargate/operation/row_operation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,9 @@ module RowOperation
# @param [String] table_name The HBase table name
# @param [String] row The row id
# @param [Hash] columns hash consisting of the keys as column names and their corresponding values
# @param [Integer] timestamp optional timestamp argument. Can be any custom value (in seconds), but HBase uses the time since epoch.
# @param [Integer] timestamp optional timestamp argument. Can be any custom value, but HBase uses the millisecond time since epoch.
# @return [true,false] true or false depending on whether the row was added successfully
def set(table_name, row, columns, timestamp = nil)
# Changing from Ruby epoch time (seconds) to Java epoch time (milliseconds)
timestamp = timestamp*1000 unless timestamp.nil?

cells = []
columns.each do |name, value|
escaped_name = name.gsub(/[&<>'"]/) { |match| CONVERTER[match] }
Expand Down Expand Up @@ -65,7 +62,7 @@ def multi_get(table_name, rows, options = {})
# @param [String] table_name The HBase table name
# @param [String] row The row id
# @param [Hash] options the options to retrieve the row with
# @option options [Integer] :timestamp A specific timestamp the rows should have
# @option options [Integer] :timestamp A specific timestamp the rows should have (in seconds)
# @option options [Array<String>] :columns List of columns to get
# @option options [Integer] :versions The number of versions to get back
# @return [Model::Row] object corresponding to the requested row, or nil if it could not be found
Expand Down Expand Up @@ -115,13 +112,10 @@ def show_row(table_name, name, timestamp = nil, columns = nil, options = {})
# @deprecated Use the {#set} method instead
# @param [String] table_name The HBase table name
# @param [String] name The row id
# @param [Integer] timestamp optional timestamp argument. Can be any custom value (in seconds), but HBase uses the time since epoch.
# @param [Integer] timestamp optional timestamp argument. Can be any custom value (in seconds), but HBase uses the millisecond time since epoch.
# @param [Hash, Array<Hash>] columns
# @return [true,false] true or false depending on whether the row was added successfully
def create_row(table_name, name, timestamp = nil, columns = nil)
# Changing from Ruby epoch time (seconds) to Java epoch time (milliseconds)
timestamp = timestamp*1000 unless timestamp.nil?

handle_exception(table_name, name) do
request = Request::RowRequest.new(table_name, name, timestamp)
data = []
Expand Down Expand Up @@ -158,7 +152,6 @@ def create_row(table_name, name, timestamp = nil, columns = nil)
# @param [Integer] timestamp optional timestamp value (deletes all timestamp if not specified)
# @param [Array<String>] optional list of specific columns to delete (deletes all columns if not specified)
def delete_row(table_name, name, timestamp = nil, columns = nil)
timestamp *= 1000 unless timestamp.nil?
handle_exception(table_name, name) do
request = Request::RowRequest.new(table_name, name, timestamp)
response = rest_delete_response(request.delete(columns))
Expand Down
2 changes: 0 additions & 2 deletions lib/stargate/operation/scanner_operation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ def open_scanner(table_name, options = {})
columns = options.delete(:columns)
batch = options.delete(:batch) || "100"
start_time = options.delete(:start_time)
start_time = start_time*1000 unless start_time.nil?
end_time = options.delete(:end_time)
end_time = end_time*1000 unless end_time.nil?

begin
request = Request::ScannerRequest.new(table_name)
Expand Down
10 changes: 5 additions & 5 deletions spec/hbase-stargate/operation/row_operation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
end

it "should be able to store multiple columns and use timestamps" do
timestamp = (Time.now.to_i - 5)
timestamp = (Time.now.to_i - 5)*1000

@client.set(@table_name, "new-set-row1", {"col1:cell1" => "col1-cell1-value", "col1:cell2" => "col1-cell2-value" }).should be_true
@client.set(@table_name, "new-set-row2", {"col1:cell1" => "col1-cell1-value", "col1:cell2" => "col1-cell2-value" }, timestamp).should be_true
Expand All @@ -90,8 +90,8 @@
row["col1:cell2"].value.should == "col1-cell2-value"
end

row2["col1:cell1"].timestamp.should == timestamp*1000
row2["col1:cell2"].timestamp.should == timestamp*1000
row2["col1:cell1"].timestamp.should == timestamp
row2["col1:cell2"].timestamp.should == timestamp
end

it "should be able to get versions" do
Expand Down Expand Up @@ -124,7 +124,7 @@
end

it "should create a rows with multiple columns and timestamps" do
timestamp = Time.now.to_i
timestamp = Time.now.to_i*1000

@client.set(@table_name, "row2-newapi", {"col1:cell1" => "row2-col1-cell1", "col1:cell2" => "row2-col1-cell2" }, timestamp).should be_true
@client.create_row(@table_name, "row2-oldapi", timestamp, [{ :name => "col1:cell1", :value => "row2-col1-cell1" }, { :name => "col1:cell2", :value => "row2-col1-cell2" }]).should be_true
Expand All @@ -134,7 +134,7 @@
rows << @client.get(@table_name, "row2-oldapi")
rows.size.should == 2

expected_timestamp = timestamp*1000
expected_timestamp = timestamp

rows.each do |row|
row.should be_a_kind_of(Stargate::Model::Row)
Expand Down
9 changes: 5 additions & 4 deletions spec/hbase-stargate/operation/scanner_operation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@

scan_table = @client.create_table("test-hbase-stargate-scan", "col1")

@ts1 = (Time.now - (5*60)).to_i
@ts1 = (Time.now - (5*60)).to_i*1000
@client.set("test-hbase-stargate-scan", "rowts11", { "col1:cell1" => "rowts11-col1-cell1" }, @ts1).should be_true
@client.set("test-hbase-stargate-scan", "rowts12", { "col1:cell1" => "rowts12-col1-cell1" }, @ts1).should be_true

@ts2 = @ts1 + 10
@ts2 = @ts1 + 10000
@client.set("test-hbase-stargate-scan", "rowts21", { "col1:cell1" => "rowts21-col1-cell1" }, @ts2).should be_true
@client.set("test-hbase-stargate-scan", "rowts22", { "col1:cell1" => "rowts22-col1-cell1" }, @ts2).should be_true

@ts3 = @ts1 + 20
@ts3 = @ts1 + 20000
@client.set("test-hbase-stargate-scan", "rowts31", { "col1:cell1" => "rowts31-col1-cell1" }, @ts3).should be_true
@client.set("test-hbase-stargate-scan", "rowts32", { "col1:cell1" => "rowts32-col1-cell1" }, @ts3).should be_true

@ts4 = @ts1 + 30
@ts4 = @ts1 + 30000
end

after :all do
Expand Down Expand Up @@ -121,6 +121,7 @@
it "should scan all 6 rows when given first timestamp" do
scanner = @client.open_scanner("test-hbase-stargate-scan", {:start_time => @ts1})
rows = @client.get_rows(scanner)

rows.size.should == 6
rows.each do |row|
row.should be_an_instance_of Stargate::Model::Row
Expand Down
2 changes: 1 addition & 1 deletion spec/hbase-stargate/operation/table_operation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
describe Stargate::Operation::TableOperation do
before :all do
url = ENV["STARGATE_URL"].nil? ? "http://localhost:8080" : ENV["STARGATE_URL"]
@client = Stargate::Client.new(url, :timeout => 10000)
@client = Stargate::Client.new(url)
@table_name = "test-hbase-stargate"
end

Expand Down

0 comments on commit 0754675

Please sign in to comment.