Permalink
Browse files

a fix for issue #44

  • Loading branch information...
nberardi committed Aug 21, 2012
1 parent 7f21cab commit 27b16f3663f8501660853ab0297398ab02356a26
Showing with 50 additions and 39 deletions.
  1. +1 −0 src/FluentCassandra.csproj
  2. +5 −38 src/Operations/Helper.cs
  3. +1 −1 src/Operations/Remove.cs
  4. +43 −0 src/TimestampHelper.cs
@@ -251,6 +251,7 @@
<Compile Include="Thrift\Transport\TTransport.cs" />
<Compile Include="Thrift\Transport\TTransportException.cs" />
<Compile Include="Thrift\Transport\TTransportFactory.cs" />
+ <Compile Include="TimestampHelper.cs" />
<Compile Include="Types\AsciiType.cs" />
<Compile Include="Types\AsciiTypeConverter.cs" />
<Compile Include="System\Numerics\BigDecimal.cs" />
View
@@ -11,19 +11,6 @@ namespace FluentCassandra.Operations
{
internal static class Helper
{
- private static readonly DateTimeOffset UnixStart;
- private static readonly long MaxUnixSeconds;
- private static readonly long MaxUnixMilliseconds;
- private static readonly long MaxUnixMicroseconds;
-
- static Helper()
- {
- UnixStart = new DateTimeOffset(1970, 1, 1, 0, 0, 0, TimeSpan.Zero);
- MaxUnixSeconds = Convert.ToInt64((DateTimeOffset.MaxValue - UnixStart).TotalSeconds);
- MaxUnixMilliseconds = Convert.ToInt64((DateTimeOffset.MaxValue - UnixStart).TotalMilliseconds);
- MaxUnixMicroseconds = Convert.ToInt64((DateTimeOffset.MaxValue - UnixStart).Ticks / 10L);
- }
-
public static List<byte[]> ToByteArrayList(List<CassandraObject> list)
{
return list.Select(x => x.TryToBigEndian()).ToList();
@@ -125,7 +112,7 @@ public static Column CreateColumn(CassandraColumn column)
var ccol = new Column {
Name = column.Name.TryToBigEndian(),
Value = column.Value.TryToBigEndian(),
- Timestamp = column.Timestamp.ToTimestamp()
+ Timestamp = column.Timestamp.ToCassandraTimestamp()
};
if (column.Ttl.HasValue && column.Ttl.Value > 0)
@@ -150,26 +137,6 @@ public static byte[] TryToBigEndian(this CassandraObject value)
return value.ToBigEndian();
}
- public static long ToTimestamp(this DateTimeOffset dt)
- {
- // we are using the microsecond format from 1/1/1970 00:00:00 UTC same as the Cassandra server
- return (dt - UnixStart).Ticks / 10L;
- }
-
- public static DateTimeOffset FromTimestamp(long ts)
- {
- if (ts <= MaxUnixSeconds)
- ts *= 1000L;
-
- if (ts <= MaxUnixMilliseconds)
- ts *= 1000L;
-
- if (ts <= MaxUnixMicroseconds)
- ts *= 10L;
-
- return UnixStart.AddTicks(ts);
- }
-
public static IFluentBaseColumn ConvertToFluentBaseColumn(ColumnOrSuperColumn col, CassandraColumnFamilySchema schema = null)
{
if (col.Super_column != null)
@@ -238,7 +205,7 @@ public static FluentColumn ConvertColumnToFluentColumn(Column col, CassandraColu
var fcol = new FluentColumn(colSchema) {
ColumnName = CassandraObject.GetCassandraObjectFromDatabaseByteArray(col.Name, colSchema.NameType),
ColumnValue = CassandraObject.GetCassandraObjectFromDatabaseByteArray(col.Value, colSchema.ValueType),
- ColumnTimestamp = FromTimestamp(col.Timestamp),
+ ColumnTimestamp = TimestampHelper.FromCassandraTimestamp(col.Timestamp),
};
if (col.__isset.ttl)
@@ -298,7 +265,7 @@ public static IEnumerable<Mutation> CreateDeletedColumnMutation(IEnumerable<Flue
foreach (var col in mutation)
{
var deletion = new Deletion {
- Timestamp = col.ColumnTimestamp.ToTimestamp(),
+ Timestamp = col.ColumnTimestamp.ToCassandraTimestamp(),
Predicate = CreateSlicePredicate(new[] { col.Column.ColumnName })
};
@@ -315,7 +282,7 @@ public static IEnumerable<Mutation> CreateDeletedSuperColumnMutation(IEnumerable
var superColumn = col.Column.GetPath().SuperColumn.ColumnName.TryToBigEndian();
var deletion = new Deletion {
- Timestamp = col.ColumnTimestamp.ToTimestamp(),
+ Timestamp = col.ColumnTimestamp.ToCassandraTimestamp(),
Super_column = superColumn,
Predicate = CreateSlicePredicate(new[] { col.Column.ColumnName })
};
@@ -346,7 +313,7 @@ public static Column CreateColumn(FluentColumn column)
var col = new Column {
Name = column.ColumnName.TryToBigEndian(),
Value = column.ColumnValue.TryToBigEndian(),
- Timestamp = column.ColumnTimestamp.ToTimestamp()
+ Timestamp = column.ColumnTimestamp.ToCassandraTimestamp()
};
if (column.ColumnSecondsUntilDeleted.HasValue)
View
@@ -31,7 +31,7 @@ public override Void Execute()
Session.GetClient().remove(
Key,
path,
- DateTimePrecise.UtcNowOffset.ToTimestamp(),
+ DateTimePrecise.UtcNowOffset.ToCassandraTimestamp(),
Session.WriteConsistency
);
View
@@ -0,0 +1,43 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace FluentCassandra
+{
+ public static class TimestampHelper
+ {
+ public static readonly DateTimeOffset UnixStart;
+ public static readonly long MaxUnixSeconds;
+ public static readonly long MaxUnixMilliseconds;
+ public static readonly long MaxUnixMicroseconds;
+
+ static TimestampHelper()
+ {
+ UnixStart = new DateTimeOffset(1970, 1, 1, 0, 0, 0, TimeSpan.Zero);
+ MaxUnixSeconds = Convert.ToInt64((DateTimeOffset.MaxValue - UnixStart).TotalSeconds);
+ MaxUnixMilliseconds = Convert.ToInt64((DateTimeOffset.MaxValue - UnixStart).TotalMilliseconds);
+ MaxUnixMicroseconds = Convert.ToInt64((DateTimeOffset.MaxValue - UnixStart).Ticks / 10L);
+ }
+
+ public static long ToCassandraTimestamp(this DateTimeOffset dt)
+ {
+ // we are using the microsecond format from 1/1/1970 00:00:00 UTC same as the Cassandra server
+ return (dt - UnixStart).Ticks / 10L;
+ }
+
+ public static DateTimeOffset FromCassandraTimestamp(long ts)
+ {
+ if (ts <= MaxUnixSeconds)
+ ts *= 1000L;
+
+ if (ts <= MaxUnixMilliseconds)
+ ts *= 1000L;
+
+ if (ts <= MaxUnixMicroseconds)
+ ts *= 10L;
+
+ return UnixStart.AddTicks(ts);
+ }
+ }
+}

0 comments on commit 27b16f3

Please sign in to comment.