public void GetRequest_With_Timestamp_Generator() { // Timestamp generator should be enabled by default var statement = new SimpleStatement("QUERY"); var config = new Configuration(); var request = RequestHandler.GetRequest(statement, Serializer, config); var bodyBuffer = GetBodyBuffer(request); // The query request is composed by: // <query><consistency><flags><result_page_size><paging_state><serial_consistency><timestamp> var queryBuffer = BeConverter.GetBytes(statement.QueryString.Length) .Concat(Encoding.UTF8.GetBytes(statement.QueryString)) .ToArray(); CollectionAssert.AreEqual(queryBuffer, bodyBuffer.Take(queryBuffer.Length)); // Skip the query and consistency (2) var offset = queryBuffer.Length + 2; var flags = GetQueryFlags(bodyBuffer, ref offset); Assert.True(flags.HasFlag(QueryFlags.WithDefaultTimestamp)); Assert.True(flags.HasFlag(QueryFlags.PageSize)); Assert.False(flags.HasFlag(QueryFlags.Values)); Assert.False(flags.HasFlag(QueryFlags.WithPagingState)); Assert.False(flags.HasFlag(QueryFlags.SkipMetadata)); Assert.True(flags.HasFlag(QueryFlags.WithSerialConsistency)); // Skip result_page_size (4) + serial_consistency (2) offset += 6; var timestamp = BeConverter.ToInt64(bodyBuffer, offset); var expectedTimestamp = TypeSerializer.SinceUnixEpoch(DateTimeOffset.Now.Subtract(TimeSpan.FromMilliseconds(100))).Ticks / 10; Assert.Greater(timestamp, expectedTimestamp); }
public void GetRequest_Batch_With_Provided_Timestamp() { var batch = new BatchStatement(); batch.Add(new SimpleStatement("QUERY")); var providedTimestamp = DateTimeOffset.Now; // To microsecond precision providedTimestamp = providedTimestamp.Subtract(TimeSpan.FromTicks(providedTimestamp.Ticks % 10)); batch.SetTimestamp(providedTimestamp); var config = new Configuration( Policies.DefaultPolicies, new ProtocolOptions(), PoolingOptions.Create(), new SocketOptions(), new ClientOptions(), NoneAuthProvider.Instance, null, new QueryOptions(), new DefaultAddressTranslator()); var request = RequestHandler.GetRequest(batch, Serializer, config); var bodyBuffer = GetBodyBuffer(request); // The batch request is composed by: // <type><n><query_1>...<query_n><consistency><flags>[<serial_consistency>][<timestamp>] var offset = 1 + 2 + 1; var queryLength = BeConverter.ToInt32(bodyBuffer, offset); Assert.AreEqual(5, queryLength); // skip query, n_params and consistency offset += 4 + queryLength + 2 + 2; var flags = GetQueryFlags(bodyBuffer, ref offset); Assert.True(flags.HasFlag(QueryFlags.WithDefaultTimestamp)); // Skip serial consistency offset += 2; var timestamp = TypeSerializer.UnixStart.AddTicks(BeConverter.ToInt64(bodyBuffer, offset) * 10); Assert.AreEqual(providedTimestamp, timestamp); }
public void GetRequest_With_Timestamp_Generator_Empty_Value_With_Statement_Timestamp() { var statement = new SimpleStatement("STATEMENT WITH TIMESTAMP"); var expectedTimestamp = new DateTimeOffset(2010, 04, 29, 1, 2, 3, 4, TimeSpan.Zero).AddTicks(20); statement.SetTimestamp(expectedTimestamp); var policies = new Cassandra.Policies( Cassandra.Policies.DefaultLoadBalancingPolicy, Cassandra.Policies.DefaultReconnectionPolicy, Cassandra.Policies.DefaultRetryPolicy, Cassandra.Policies.DefaultSpeculativeExecutionPolicy, new NoTimestampGenerator()); var config = RequestHandlerTests.GetConfig(new QueryOptions(), policies, PoolingOptions.Create()); var request = RequestHandler.GetRequest(statement, Serializer, config.DefaultRequestOptions); var bodyBuffer = GetBodyBuffer(request); // The query request is composed by: // <query><consistency><flags><result_page_size><paging_state><serial_consistency><timestamp> var queryBuffer = BeConverter.GetBytes(statement.QueryString.Length) .Concat(Encoding.UTF8.GetBytes(statement.QueryString)) .ToArray(); CollectionAssert.AreEqual(queryBuffer, bodyBuffer.Take(queryBuffer.Length)); // Skip the query and consistency (2) var offset = queryBuffer.Length + 2; var flags = GetQueryFlags(bodyBuffer, ref offset); Assert.True(flags.HasFlag(QueryFlags.WithDefaultTimestamp)); Assert.True(flags.HasFlag(QueryFlags.PageSize)); // Skip result_page_size (4) + serial_consistency (2) offset += 6; var timestamp = BeConverter.ToInt64(bodyBuffer, offset); Assert.AreEqual(TypeSerializer.SinceUnixEpoch(expectedTimestamp).Ticks / 10, timestamp); }
public void GetRequest_Batch_With_Timestamp_Generator() { var batch = new BatchStatement(); batch.Add(new SimpleStatement("QUERY")); var startDate = DateTimeOffset.Now; // To microsecond precision startDate = startDate.Subtract(TimeSpan.FromTicks(startDate.Ticks % 10)); var config = new Configuration( Policies.DefaultPolicies, new ProtocolOptions(), PoolingOptions.Create(), new SocketOptions(), new ClientOptions(), NoneAuthProvider.Instance, null, new QueryOptions(), new DefaultAddressTranslator(), Mock.Of <IStartupOptionsFactory>(), new SessionFactoryBuilder()); var request = RequestHandler.GetRequest(batch, Serializer, config); var bodyBuffer = GetBodyBuffer(request); // The batch request is composed by: // <type><n><query_1>...<query_n><consistency><flags>[<serial_consistency>][<timestamp>] var offset = 1; // n = 1 Assert.AreEqual(1, BeConverter.ToInt16(bodyBuffer, offset)); // Query_1 <kind><string><n_params> offset += 2; // kind = 0, not prepared Assert.AreEqual(0, bodyBuffer[offset++]); var queryLength = BeConverter.ToInt32(bodyBuffer, offset); Assert.AreEqual(5, queryLength); // skip query, n_params and consistency offset += 4 + queryLength + 2 + 2; var flags = GetQueryFlags(bodyBuffer, ref offset); Assert.True(flags.HasFlag(QueryFlags.WithDefaultTimestamp)); // Skip serial consistency offset += 2; var timestamp = TypeSerializer.UnixStart.AddTicks(BeConverter.ToInt64(bodyBuffer, offset) * 10); Assert.GreaterOrEqual(timestamp, startDate); Assert.LessOrEqual(timestamp, DateTimeOffset.Now.Add(TimeSpan.FromMilliseconds(100))); }
public void GetRequest_With_Timestamp_Generator_Empty_Value_With_Statement_Timestamp() { var statement = new SimpleStatement("STATEMENT WITH TIMESTAMP"); var expectedTimestamp = new DateTimeOffset(2010, 04, 29, 1, 2, 3, 4, TimeSpan.Zero).AddTicks(20); statement.SetTimestamp(expectedTimestamp); var policies = new Policies( Policies.DefaultLoadBalancingPolicy, Policies.DefaultReconnectionPolicy, Policies.DefaultRetryPolicy, Policies.DefaultSpeculativeExecutionPolicy, new NoTimestampGenerator()); var config = new Configuration( policies, new ProtocolOptions(), PoolingOptions.Create(), new SocketOptions(), new ClientOptions(), NoneAuthProvider.Instance, null, new QueryOptions(), new DefaultAddressTranslator(), Mock.Of <IStartupOptionsFactory>(), new SessionFactoryBuilder()); var request = RequestHandler.GetRequest(statement, Serializer, config); var bodyBuffer = GetBodyBuffer(request); // The query request is composed by: // <query><consistency><flags><result_page_size><paging_state><serial_consistency><timestamp> var queryBuffer = BeConverter.GetBytes(statement.QueryString.Length) .Concat(Encoding.UTF8.GetBytes(statement.QueryString)) .ToArray(); CollectionAssert.AreEqual(queryBuffer, bodyBuffer.Take(queryBuffer.Length)); // Skip the query and consistency (2) var offset = queryBuffer.Length + 2; // The remaining length should be = flags (1) + result_page_size (4) + serial_consistency (2) + timestamp (8) Assert.AreEqual(15, bodyBuffer.Length - offset); var flags = GetQueryFlags(bodyBuffer, ref offset); Assert.True(flags.HasFlag(QueryFlags.WithDefaultTimestamp)); Assert.True(flags.HasFlag(QueryFlags.PageSize)); // Skip result_page_size (4) + serial_consistency (2) offset += 6; var timestamp = BeConverter.ToInt64(bodyBuffer, offset); Assert.AreEqual(TypeSerializer.SinceUnixEpoch(expectedTimestamp).Ticks / 10, timestamp); }
public void GetRequest_With_Timestamp_Generator_Empty_Value_With_Statement_Timestamp() { var statement = new SimpleStatement("STATEMENT WITH TIMESTAMP"); var expectedTimestamp = new DateTimeOffset(2010, 04, 29, 1, 2, 3, 4, TimeSpan.Zero).AddTicks(20); statement.SetTimestamp(expectedTimestamp); var policies = new Dse.Policies( Dse.Policies.DefaultLoadBalancingPolicy, Dse.Policies.DefaultReconnectionPolicy, Dse.Policies.DefaultRetryPolicy, Dse.Policies.DefaultSpeculativeExecutionPolicy, new NoTimestampGenerator()); var config = new Configuration( policies, new ProtocolOptions(), PoolingOptions.GetDefault(ProtocolVersion.MaxSupported), new SocketOptions(), new ClientOptions(), NoneAuthProvider.Instance, null, new QueryOptions(), new DefaultAddressTranslator()); var request = RequestHandler <RowSet> .GetRequest(statement, Serializer, config); var stream = new MemoryStream(); request.WriteFrame(1, stream, Serializer); var headerSize = FrameHeader.GetSize(ProtocolVersion.MaxSupported); var bodyBuffer = new byte[stream.Length - headerSize]; stream.Position = headerSize; stream.Read(bodyBuffer, 0, bodyBuffer.Length); // The query request is composed by: // <query><consistency><flags><result_page_size><timestamp> var queryBuffer = BeConverter.GetBytes(statement.QueryString.Length) .Concat(Encoding.UTF8.GetBytes(statement.QueryString)) .ToArray(); CollectionAssert.AreEqual(queryBuffer, bodyBuffer.Take(queryBuffer.Length)); // Skip the query and consistency (2) var offset = queryBuffer.Length + 2; // The remaining length should be 13 = flags (1) + result_page_size (4) + timestamp (8) Assert.AreEqual(13, bodyBuffer.Length - offset); var flags = (QueryFlags)bodyBuffer[offset]; Assert.True(flags.HasFlag(QueryFlags.WithDefaultTimestamp)); Assert.True(flags.HasFlag(QueryFlags.PageSize)); // Skip flags (1) + result_page_size (4) offset += 5; var timestamp = BeConverter.ToInt64(bodyBuffer, offset); Assert.AreEqual(TypeSerializer.SinceUnixEpoch(expectedTimestamp).Ticks / 10, timestamp); }
public void GetRequest_With_Timestamp_Generator() { // Timestamp generator should be enabled by default var statement = new SimpleStatement("QUERY"); var config = new Configuration(); var request = RequestHandler <RowSet> .GetRequest(statement, Serializer, config); var stream = new MemoryStream(); request.WriteFrame(1, stream, Serializer); var headerSize = FrameHeader.GetSize(ProtocolVersion.MaxSupported); var bodyBuffer = new byte[stream.Length - headerSize]; stream.Position = headerSize; stream.Read(bodyBuffer, 0, bodyBuffer.Length); // The query request is composed by: // <query><consistency><flags><result_page_size><timestamp> var queryBuffer = BeConverter.GetBytes(statement.QueryString.Length) .Concat(Encoding.UTF8.GetBytes(statement.QueryString)) .ToArray(); CollectionAssert.AreEqual(queryBuffer, bodyBuffer.Take(queryBuffer.Length)); // Skip the query and consistency (2) var offset = queryBuffer.Length + 2; // The remaining length should be 13 = flags (1) + result_page_size (4) + timestamp (8) Assert.AreEqual(13, bodyBuffer.Length - offset); var flags = (QueryFlags)bodyBuffer[offset]; Assert.True(flags.HasFlag(QueryFlags.WithDefaultTimestamp)); Assert.True(flags.HasFlag(QueryFlags.PageSize)); Assert.False(flags.HasFlag(QueryFlags.Values)); Assert.False(flags.HasFlag(QueryFlags.WithPagingState)); Assert.False(flags.HasFlag(QueryFlags.SkipMetadata)); Assert.False(flags.HasFlag(QueryFlags.WithSerialConsistency)); // Skip flags (1) + result_page_size (4) offset += 5; var timestamp = BeConverter.ToInt64(bodyBuffer, offset); var expectedTimestamp = TypeSerializer.SinceUnixEpoch(DateTimeOffset.Now.Subtract(TimeSpan.FromMilliseconds(100))).Ticks / 10; Assert.Greater(timestamp, expectedTimestamp); }
public void GetRequest_Batch_With_Provided_Timestamp() { var batch = new BatchStatement(); batch.Add(new SimpleStatement("QUERY")); var providedTimestamp = DateTimeOffset.Now; // To microsecond precision providedTimestamp = providedTimestamp.Subtract(TimeSpan.FromTicks(providedTimestamp.Ticks % 10)); batch.SetTimestamp(providedTimestamp); var config = new Configuration( Policies.DefaultPolicies, new ProtocolOptions(), PoolingOptions.Create(), new SocketOptions(), new ClientOptions(), NoneAuthProvider.Instance, null, new QueryOptions(), new DefaultAddressTranslator()); var request = RequestHandler.GetRequest(batch, Serializer, config); var stream = new MemoryStream(); request.WriteFrame(1, stream, Serializer); var headerSize = FrameHeader.GetSize(ProtocolVersion.MaxSupported); var bodyBuffer = new byte[stream.Length - headerSize]; stream.Position = headerSize; stream.Read(bodyBuffer, 0, bodyBuffer.Length); // The batch request is composed by: // <type><n><query_1>...<query_n><consistency><flags>[<serial_consistency>][<timestamp>] var offset = 1 + 2 + 1; var queryLength = BeConverter.ToInt32(bodyBuffer, offset); Assert.AreEqual(5, queryLength); // skip query, n_params and consistency offset += 4 + queryLength + 2 + 2; var flags = (QueryFlags)bodyBuffer[offset++]; Assert.True(flags.HasFlag(QueryFlags.WithDefaultTimestamp)); var timestamp = TypeSerializer.UnixStart.AddTicks(BeConverter.ToInt64(bodyBuffer, offset) * 10); Assert.AreEqual(providedTimestamp, timestamp); }
public override LocalTime Deserialize(ushort protocolVersion, byte[] buffer, int offset, int length, IColumnInfo typeInfo) { return(new LocalTime(BeConverter.ToInt64(buffer, offset))); }
internal static DateTimeOffset Deserialize(byte[] buffer, int offset) { var milliseconds = BeConverter.ToInt64(buffer, offset); return(UnixStart.AddTicks(TimeSpan.TicksPerMillisecond * milliseconds)); }
internal static DateTimeOffset Deserialize(byte[] buffer, int offset) { return(UnixStart.AddMilliseconds(BeConverter.ToInt64(buffer, offset))); }