Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Cassandra uses Java's BigDecimal object to store and create these val…

…ues,

this isn't compatible with .NET's Decimal object, so I was able to put a
hack in place to solve the issue when the decimal values are positive,
negative values are a different story right now, fixed issue #25
  • Loading branch information...
commit ab8b19fb828c1d3b8b1e5cb09b0e87bc94d04bbf 1 parent c1a5a57
@nberardi nberardi authored
View
8 src/CassandraColumnFamilySchema.cs
@@ -10,8 +10,16 @@ public class CassandraColumnFamilySchema
{
public static readonly AsciiType DefaultKeyName = "KEY";
+#if DEBUG
+ private CfDef _def;
+#endif
+
public CassandraColumnFamilySchema(CfDef def)
{
+#if DEBUG
+ _def = def;
+#endif
+
var familyType = ColumnType.Standard;
Enum.TryParse<ColumnType>(def.Column_type, out familyType);
View
33 src/Types/BytesTypeConverter.cs
@@ -284,6 +284,18 @@ private static byte[] FromDecimal(decimal d)
private static decimal ToDecimal(byte[] bytes)
{
+ try
+ {
+ return FromDotNetDecimalToDecimal(bytes);
+ }
+ catch
+ {
+ return FromJavaBigDecimalToDecimal(bytes);
+ }
+ }
+
+ private static decimal FromDotNetDecimalToDecimal(byte[] bytes)
+ {
int[] bits = new int[4];
bits[0] = ((bytes[0] | (bytes[1] << 8)) | (bytes[2] << 0x10)) | (bytes[3] << 0x18); //lo
bits[1] = ((bytes[4] | (bytes[5] << 8)) | (bytes[6] << 0x10)) | (bytes[7] << 0x18); //mid
@@ -292,5 +304,24 @@ private static decimal ToDecimal(byte[] bytes)
return new decimal(bits);
}
+
+ private static decimal FromJavaBigDecimalToDecimal(byte[] bytes)
+ {
+ byte[] number = new byte[12];
+ byte[] flags = new byte[4];
+ Array.Copy(bytes, bytes.Length - 4, flags, 0, 4);
+ Array.Copy(bytes, 0, number, 0, bytes.Length - 4);
+
+ Console.WriteLine(bytes);
+ Console.WriteLine(number);
+ Console.WriteLine(flags);
+
+ int[] bits = new int[4];
+ bits[0] = ((number[0] | (number[1] << 8)) | (number[2] << 0x10)) | (number[3] << 0x18); //lo
+ bits[1] = ((number[4] | (number[5] << 8)) | (number[6] << 0x10)) | (number[7] << 0x18); //mid
+ bits[2] = ((number[8] | (number[9] << 8)) | (number[10] << 0x10)) | (number[11] << 0x18); //hi
+
+ return new decimal(bits[0], bits[1], bits[2], false, flags[0]);
+ }
}
-}
+}
View
36 test/FluentCassandra.Tests/Bugs/Issue25JavaBigDecimalBinaryConversion.cs
@@ -0,0 +1,36 @@
+using System;
+using System.Linq;
+using NUnit.Framework;
+
+namespace FluentCassandra.Bugs
+{
+ [TestFixture]
+ public class Issue25JavaBigDecimalBinaryConversion
+ {
+ private CassandraContext _db;
+
+ [TestFixtureSetUp]
+ public void TestInit()
+ {
+ var setup = new CassandraDatabaseSetup();
+ _db = setup.DB;
+ }
+
+ [Test]
+ public void Test()
+ {
+ // arrange
+ _db.ExecuteNonQuery("CREATE TABLE OfferReservation (KEY text PRIMARY KEY) WITH comparator = text AND default_validation = decimal");
+ _db.ExecuteNonQuery("INSERT INTO OfferReservation (KEY, 'MyColumn') VALUES ('Key1', 0.25)");
+ _db.ExecuteNonQuery("INSERT INTO OfferReservation (KEY, 'MyColumn') VALUES ('Key2', 2000000000000.1234)");
+
+ // act
+ var actual = _db.ExecuteQuery("SELECT * FROM OfferReservation");
+
+ // assert
+ var results = actual.ToList();
+ Assert.AreEqual(.25M, (decimal)results.First(x => x.Key == "Key1")["MyColumn"]);
+ Assert.AreEqual(2000000000000.1234M, (decimal)results.First(x => x.Key == "Key2")["MyColumn"]);
+ }
+ }
+}
View
2  test/FluentCassandra.Tests/FluentCassandra.Tests.csproj
@@ -55,6 +55,7 @@
</Reference>
</ItemGroup>
<ItemGroup>
+ <Compile Include="Bugs\Issue25JavaBigDecimalBinaryConversion.cs" />
<Compile Include="CassandraQueryTest.cs" />
<Compile Include="Connections\ConnectionProviderTests.cs" />
<Compile Include="Connections\NormalConnectionProviderTests.cs" />
@@ -111,6 +112,7 @@
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
+ <ItemGroup />
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<Import Project="$(SolutionDir)\.nuget\nuget.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Please sign in to comment.
Something went wrong with that request. Please try again.