public void GetRequest_Query_Should_Use_Serial_Consistency_From_QueryOptions() { const ConsistencyLevel expectedSerialConsistencyLevel = ConsistencyLevel.LocalSerial; var statement = new SimpleStatement("QUERY"); var config = new Configuration(); config.QueryOptions.SetSerialConsistencyLevel(expectedSerialConsistencyLevel); 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> // Skip the query and consistency (2) var offset = 4 + statement.QueryString.Length + 2; var flags = GetQueryFlags(bodyBuffer, ref offset); Assert.True(flags.HasFlag(QueryFlags.WithDefaultTimestamp)); Assert.True(flags.HasFlag(QueryFlags.PageSize)); Assert.True(flags.HasFlag(QueryFlags.WithSerialConsistency)); // Skip result_page_size (4) offset += 4; Assert.That((ConsistencyLevel)BeConverter.ToInt16(bodyBuffer, offset), Is.EqualTo(expectedSerialConsistencyLevel)); }
public void GetRequest_Batch_With_Provided_Keyspace() { const string keyspace = "test_keyspace"; var batch = new BatchStatement(); batch.Add(new SimpleStatement("QUERY")).SetKeyspace(keyspace); var request = RequestHandler.GetRequest(batch, Serializer, new Configuration().DefaultRequestOptions); var bodyBuffer = GetBodyBuffer(request); // The batch request is composed by: // <type><n><query_1>...<query_n><consistency><flags>[<serial_consistency>][<timestamp>][<keyspace>] 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.WithKeyspace)); // Skip serial consistency and timestamp offset += (flags.HasFlag(QueryFlags.WithSerialConsistency) ? 2 : 0) + (flags.HasFlag(QueryFlags.WithDefaultTimestamp) ? 8 : 0); var keyspaceLength = BeConverter.ToInt16(bodyBuffer, offset); offset += 2; Assert.AreEqual(keyspace, Encoding.UTF8.GetString(bodyBuffer, offset, keyspaceLength)); }
public void GetRequest_Execute_Should_Use_Serial_Consistency_From_Statement() { const ConsistencyLevel expectedSerialConsistencyLevel = ConsistencyLevel.Serial; var ps = GetPrepared(new byte[16]); var statement = ps.Bind().SetSerialConsistencyLevel(expectedSerialConsistencyLevel); var config = new Configuration(); config.QueryOptions.SetSerialConsistencyLevel(ConsistencyLevel.LocalSerial); var request = RequestHandler.GetRequest(statement, Serializer, config); var bodyBuffer = GetBodyBuffer(request); // The execute request is composed by: // <query_id><consistency><flags><result_page_size><paging_state><serial_consistency><timestamp> // Skip the queryid and consistency (2) var offset = 2 + ps.Id.Length + 2; var flags = GetQueryFlags(bodyBuffer, ref offset); Assert.True(flags.HasFlag(QueryFlags.WithDefaultTimestamp)); Assert.True(flags.HasFlag(QueryFlags.PageSize)); Assert.True(flags.HasFlag(QueryFlags.WithSerialConsistency)); // Skip result_page_size (4) offset += 4; Assert.That((ConsistencyLevel)BeConverter.ToInt16(bodyBuffer, offset), Is.EqualTo(expectedSerialConsistencyLevel)); }
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))); }
private static void AssertBatchSerialConsistencyLevel(BatchStatement batch, Configuration config, string query, ConsistencyLevel expectedSerialConsistencyLevel) { 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>] // Skip query, n_params and consistency var offset = 1 + 2 + 1 + 4 + query.Length + 2 + 2; var flags = GetQueryFlags(bodyBuffer, ref offset); Assert.True(flags.HasFlag(QueryFlags.WithSerialConsistency)); Assert.That((ConsistencyLevel)BeConverter.ToInt16(bodyBuffer, offset), Is.EqualTo(expectedSerialConsistencyLevel)); }
/// <summary> /// Decodes length for collection types depending on the protocol version /// </summary> internal static int DecodeCollectionLength(ushort protocolVersion, byte[] buffer, ref int index) { int result; if (protocolVersion < 3) { //length is a short result = BeConverter.ToInt16(buffer, index); index += 2; } else { //length is expressed in int result = BeConverter.ToInt32(buffer, index); index += 4; } return(result); }
public void Prepare_With_Keyspace_Should_Send_Keyspace_And_Flag() { const string query = "QUERY1"; const string keyspace = "ks1"; var request = new PrepareRequest(RequestHandlerTests.Serializer, query, keyspace, null); // The request is composed by: <query><flags>[<keyspace>] var buffer = GetBodyBuffer(request); var queryLength = BeConverter.ToInt32(buffer); Assert.AreEqual(query.Length, queryLength); var offset = 4 + queryLength; var flags = (PrepareFlags)BeConverter.ToInt32(buffer, offset); offset += 4; Assert.True(flags.HasFlag(PrepareFlags.WithKeyspace)); var keyspaceLength = BeConverter.ToInt16(buffer, offset); offset += 2; Assert.AreEqual(keyspace.Length, keyspaceLength); Assert.AreEqual(keyspace, Encoding.UTF8.GetString(buffer.Skip(offset).Take(keyspaceLength).ToArray())); }
public void GetRequest_Query_Should_Use_Provided_Keyspace() { const string keyspace = "my_keyspace"; var statement = new SimpleStatement("QUERY").SetKeyspace(keyspace); var request = RequestHandler.GetRequest(statement, Serializer, new Configuration().DefaultRequestOptions); var bodyBuffer = GetBodyBuffer(request); // The query request is composed by: // <consistency><flags>[<n>[name_1]<value_1>...[name_n]<value_n>][<result_page_size>][<paging_state>] // [<serial_consistency>][<timestamp>][<keyspace>][continuous_paging_options] // Skip the query and consistency (2) var offset = 4 + statement.QueryString.Length + 2; var flags = GetQueryFlags(bodyBuffer, ref offset); offset += (flags.HasFlag(QueryFlags.WithDefaultTimestamp) ? 8 : 0) + (flags.HasFlag(QueryFlags.PageSize) ? 4 : 0) + (flags.HasFlag(QueryFlags.WithSerialConsistency) ? 2 : 0); var keyspaceLength = BeConverter.ToInt16(bodyBuffer, offset); offset += 2; Assert.AreEqual(keyspace, Encoding.UTF8.GetString(bodyBuffer, offset, keyspaceLength)); }
public override short Deserialize(ushort protocolVersion, byte[] buffer, int offset, int length, IColumnInfo typeInfo) { return(BeConverter.ToInt16(buffer, offset)); }