Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Fixed casting negative value longs. #13

Merged
merged 1 commit into from

2 participants

@psanford

No description provided.

@kreynolds kreynolds merged commit f11b934 into kreynolds:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Aug 30, 2011
  1. @psanford
This page is out of date. Refresh to see the latest.
Showing with 29 additions and 8 deletions.
  1. +8 −3 lib/cassandra-cql/schema.rb
  2. +21 −5 spec/column_family_spec.rb
View
11 lib/cassandra-cql/schema.rb
@@ -74,18 +74,23 @@ def self.cast(value, type)
end
when "org.apache.cassandra.db.marshal.LongType", "org.apache.cassandra.db.marshal.CounterColumnType"
ints = value.unpack("NN")
- (ints[0] << 32) + ints[1]
+ val = (ints[0] << 32) + ints[1]
+ if val & 2**63 == 2**63
+ val - 2**64
+ else
+ val
+ end
when "org.apache.cassandra.db.marshal.AsciiType", "org.apache.cassandra.db.marshal.UTF8Type"
value.to_s
else
value
end
end
-
+
def name
@cf_def.name
end
-
+
def type
@cf_def.column_type
end
View
26 spec/column_family_spec.rb
@@ -43,26 +43,42 @@
super_column_family.super?.should be_true
end
end
-
+
context "when calling self.cast" do
- it "should turn turn UUID bytes into a Time object" do
+ it "should turn UUID bytes into a Time object" do
ts = Time.new
ColumnFamily.cast(UUID.new(ts).bytes, "org.apache.cassandra.db.marshal.TimeUUIDType").should eq(ts)
end
- it "should turn turn a UUID bytes into a UUID object" do
+ it "should turn a UUID bytes into a UUID object" do
uuid = UUID.new
ColumnFamily.cast(uuid.bytes, "org.apache.cassandra.db.marshal.UUIDType").should eq(uuid)
end
- it "should turn turn a packed integer into a Fixnum" do
+ it "should turn a packed integer into a Fixnum" do
ColumnFamily.cast([0x7FFFFFFF].pack("N"), "org.apache.cassandra.db.marshal.IntegerType").should eq(0x7FFFFFFF)
end
- it "should turn turn a packed negative integer into a negative Fixnum" do
+ it "should turn a packed negative integer into a negative Fixnum" do
ColumnFamily.cast([-68047].pack("N"), "org.apache.cassandra.db.marshal.IntegerType").should eq(-68047)
end
+ it "should turn a packed long into a number" do
+ number = 2**33
+ packed = [number >> 32, number].pack("N*")
+
+ ColumnFamily.cast(packed, "org.apache.cassandra.db.marshal.LongType").should eq(number)
+ ColumnFamily.cast(packed, "org.apache.cassandra.db.marshal.CounterColumnType").should eq(number)
+ end
+
+ it "should turn a packed negative long into a negative number" do
+ number = -2**33
+ packed = [number >> 32, number].pack("N*")
+
+ ColumnFamily.cast(packed, "org.apache.cassandra.db.marshal.LongType").should eq(number)
+ ColumnFamily.cast(packed, "org.apache.cassandra.db.marshal.CounterColumnType").should eq(number)
+ end
+
it "should call to_s with AsciiType" do
obj = double("String")
obj.should_receive(:to_s)
Something went wrong with that request. Please try again.