From 075467501afbfd2c7a2fcae6579dbce0c317f3e0 Mon Sep 17 00:00:00 2001 From: Greg Lu Date: Sat, 26 Nov 2011 15:23:02 -0500 Subject: [PATCH] Decided to make the timestamp definitions explicit, instead of converting 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. --- lib/stargate/operation/row_operation.rb | 13 +++---------- lib/stargate/operation/scanner_operation.rb | 2 -- spec/hbase-stargate/operation/row_operation_spec.rb | 10 +++++----- .../operation/scanner_operation_spec.rb | 9 +++++---- .../operation/table_operation_spec.rb | 2 +- 5 files changed, 14 insertions(+), 22 deletions(-) diff --git a/lib/stargate/operation/row_operation.rb b/lib/stargate/operation/row_operation.rb index 604458f..185c62a 100644 --- a/lib/stargate/operation/row_operation.rb +++ b/lib/stargate/operation/row_operation.rb @@ -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] } @@ -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] :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 @@ -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] 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 = [] @@ -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] 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)) diff --git a/lib/stargate/operation/scanner_operation.rb b/lib/stargate/operation/scanner_operation.rb index 0de0c04..de2f170 100644 --- a/lib/stargate/operation/scanner_operation.rb +++ b/lib/stargate/operation/scanner_operation.rb @@ -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) diff --git a/spec/hbase-stargate/operation/row_operation_spec.rb b/spec/hbase-stargate/operation/row_operation_spec.rb index bd02ff2..fc465e9 100644 --- a/spec/hbase-stargate/operation/row_operation_spec.rb +++ b/spec/hbase-stargate/operation/row_operation_spec.rb @@ -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 @@ -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 @@ -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 @@ -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) diff --git a/spec/hbase-stargate/operation/scanner_operation_spec.rb b/spec/hbase-stargate/operation/scanner_operation_spec.rb index 82cce6a..fdd6c26 100644 --- a/spec/hbase-stargate/operation/scanner_operation_spec.rb +++ b/spec/hbase-stargate/operation/scanner_operation_spec.rb @@ -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 @@ -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 diff --git a/spec/hbase-stargate/operation/table_operation_spec.rb b/spec/hbase-stargate/operation/table_operation_spec.rb index 1cd86f1..2e239cf 100644 --- a/spec/hbase-stargate/operation/table_operation_spec.rb +++ b/spec/hbase-stargate/operation/table_operation_spec.rb @@ -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