Example #1
0
 public BatchRequest(ProtocolVersion protocolVersion, BatchStatement statement, ConsistencyLevel consistency,
                     Policies policies)
 {
     if (!protocolVersion.SupportsBatch())
     {
         throw new NotSupportedException("Batch request is supported in C* >= 2.0.x");
     }
     _type     = statement.BatchType;
     _requests = statement.Queries
                 .Select(q => q.CreateBatchRequest(protocolVersion))
                 .ToArray();
     Consistency = consistency;
     if (statement.IsTracing)
     {
         _headerFlags = FrameHeader.HeaderFlag.Tracing;
     }
     if (statement.SerialConsistencyLevel != ConsistencyLevel.Any)
     {
         if (!protocolVersion.SupportsTimestamp())
         {
             throw new NotSupportedException("Serial consistency level for BATCH request is supported in Cassandra 2.1 or above.");
         }
         if (statement.SerialConsistencyLevel < ConsistencyLevel.Serial)
         {
             throw new RequestInvalidException("Non-serial consistency specified as a serial one.");
         }
         _batchFlags       |= QueryProtocolOptions.QueryFlags.WithSerialConsistency;
         _serialConsistency = statement.SerialConsistencyLevel;
     }
     _timestamp = GetRequestTimestamp(protocolVersion, statement, policies);
     if (_timestamp != null)
     {
         _batchFlags |= QueryProtocolOptions.QueryFlags.WithDefaultTimestamp;
     }
 }
Example #2
0
 public QueryRequest(int protocolVersion, string cqlQuery, bool tracingEnabled, QueryProtocolOptions queryOptions)
 {
     //TODO: Replace constructor parameters with IStatement
     ProtocolVersion = protocolVersion;
     _cqlQuery       = cqlQuery;
     _queryOptions   = queryOptions;
     if (tracingEnabled)
     {
         _headerFlags = FrameHeader.HeaderFlag.Tracing;
     }
     if (queryOptions == null)
     {
         throw new ArgumentNullException("queryOptions");
     }
     if (Consistency.IsSerialConsistencyLevel())
     {
         throw new RequestInvalidException("Serial consistency specified as a non-serial one.");
     }
     if (queryOptions.SerialConsistency != ConsistencyLevel.Any && queryOptions.SerialConsistency.IsSerialConsistencyLevel() == false)
     {
         throw new RequestInvalidException("Non-serial consistency specified as a serial one.");
     }
     if (protocolVersion < 3)
     {
         //Features supported in protocol v3 and above
         if (queryOptions.Timestamp != null)
         {
             throw new NotSupportedException("Timestamp for query is supported in Cassandra 2.1 and above.");
         }
         if (queryOptions.ValueNames != null && queryOptions.ValueNames.Count > 0)
         {
             throw new NotSupportedException("Query parameter names feature is supported in Cassandra 2.1 and above.");
         }
     }
 }
Example #3
0
        public int WriteFrame(short streamId, MemoryStream stream, ISerializer serializer)
        {
            var wb = new FrameWriter(stream, serializer);
            var protocolVersion = serializer.ProtocolVersion;

            if (Payload != null)
            {
                _headerFlags |= FrameHeader.HeaderFlag.CustomPayload;
            }
            wb.WriteFrameHeader((byte)_headerFlags, streamId, OpCode);
            if (Payload != null)
            {
                //A custom payload for this request
                wb.WriteBytesMap(Payload);
            }
            wb.WriteShortBytes(_id);

            if (protocolVersion.SupportsResultMetadataId())
            {
                wb.WriteShortBytes(_resultMetadataId);
            }

            _queryOptions.Write(wb, true);
            return(wb.Close());
        }
Example #4
0
        public ExecuteRequest(int protocolVersion, byte[] id, RowSetMetadata metadata, bool tracingEnabled, QueryProtocolOptions queryOptions)
        {
            ProtocolVersion = protocolVersion;
            if (metadata != null && queryOptions.Values.Length != metadata.Columns.Length)
            {
                throw new ArgumentException("Number of values does not match with number of prepared statement markers(?).", "values");
            }
            _id           = id;
            _metadata     = metadata;
            _queryOptions = queryOptions;
            if (tracingEnabled)
            {
                _headerFlags = FrameHeader.HeaderFlag.Tracing;
            }

            if (Consistency.IsSerialConsistencyLevel())
            {
                throw new RequestInvalidException("Serial consistency specified as a non-serial one.");
            }
            if (queryOptions.SerialConsistency != ConsistencyLevel.Any && queryOptions.SerialConsistency.IsSerialConsistencyLevel() == false)
            {
                throw new RequestInvalidException("Non-serial consistency specified as a serial one.");
            }
            if (queryOptions.Timestamp != null && protocolVersion < 3)
            {
                throw new NotSupportedException("Timestamp for query is supported in Cassandra 2.1 or above.");
            }
        }
Example #5
0
 public QueryRequest(int protocolVersion, string cqlQuery, bool tracingEnabled, QueryProtocolOptions queryOptions)
 {
     //TODO: Replace constructor parameters with IStatement
     ProtocolVersion = protocolVersion;
     _cqlQuery = cqlQuery;
     _queryOptions = queryOptions;
     if (tracingEnabled)
     {
         _headerFlags = FrameHeader.HeaderFlag.Tracing;
     }
     if (queryOptions == null)
     {
         throw new ArgumentNullException("queryOptions");
     }
     if (Consistency.IsSerialConsistencyLevel())
     {
         throw new RequestInvalidException("Serial consistency specified as a non-serial one.");
     }
     if (queryOptions.SerialConsistency != ConsistencyLevel.Any && queryOptions.SerialConsistency.IsSerialConsistencyLevel() == false)
     {
         throw new RequestInvalidException("Non-serial consistency specified as a serial one.");
     }
     if (protocolVersion < 3)
     {
         //Features supported in protocol v3 and above
         if (queryOptions.Timestamp != null)
         {
             throw new NotSupportedException("Timestamp for query is supported in Cassandra 2.1 and above.");
         }
         if (queryOptions.ValueNames != null && queryOptions.ValueNames.Count > 0)
         {
             throw new NotSupportedException("Query parameter names feature is supported in Cassandra 2.1 and above.");
         }
     }
 }
Example #6
0
        public BatchRequest(ProtocolVersion protocolVersion, BatchStatement statement, ConsistencyLevel consistency, Configuration config)
        {
            if (!protocolVersion.SupportsBatch())
            {
                throw new NotSupportedException("Batch request is supported in C* >= 2.0.x");
            }
            _type     = statement.BatchType;
            _requests = statement.Queries
                        .Select(q => q.CreateBatchRequest(protocolVersion))
                        .ToArray();
            Consistency = consistency;
            if (statement.IsTracing)
            {
                _headerFlags = FrameHeader.HeaderFlag.Tracing;
            }

            _serialConsistency = config.QueryOptions.GetSerialConsistencyLevelOrDefault(statement);
            _batchFlags       |= QueryProtocolOptions.QueryFlags.WithSerialConsistency;

            _timestamp = GetRequestTimestamp(protocolVersion, statement, config.Policies);
            if (_timestamp != null)
            {
                _batchFlags |= QueryProtocolOptions.QueryFlags.WithDefaultTimestamp;
            }
        }
Example #7
0
        public BatchRequest(ProtocolVersion protocolVersion, BatchStatement statement, ConsistencyLevel consistency, IRequestOptions requestOptions)
        {
            if (!protocolVersion.SupportsBatch())
            {
                throw new NotSupportedException("Batch request is supported in C* >= 2.0.x");
            }
            _type     = statement.BatchType;
            _requests = statement.Queries
                        .Select(q => q.CreateBatchRequest(protocolVersion))
                        .ToArray();
            Consistency = consistency;
            if (statement.IsTracing)
            {
                _headerFlags = FrameHeader.HeaderFlag.Tracing;
            }

            SerialConsistency = requestOptions.GetSerialConsistencyLevelOrDefault(statement);
            _batchFlags      |= QueryProtocolOptions.QueryFlags.WithSerialConsistency;

            _timestamp = BatchRequest.GetRequestTimestamp(protocolVersion, statement, requestOptions.TimestampGenerator);
            if (_timestamp != null)
            {
                _batchFlags |= QueryProtocolOptions.QueryFlags.WithDefaultTimestamp;
            }

            if (protocolVersion.SupportsKeyspaceInRequest() && statement.Keyspace != null)
            {
                _batchFlags |= QueryProtocolOptions.QueryFlags.WithKeyspace;
                _keyspace    = statement.Keyspace;
            }
        }
Example #8
0
        public ExecuteRequest(int protocolVersion, byte[] id, RowSetMetadata metadata, bool tracingEnabled, QueryProtocolOptions queryOptions)
        {
            ProtocolVersion = protocolVersion;
            if (metadata != null && queryOptions.Values.Length != metadata.Columns.Length)
            {
                throw new ArgumentException("Number of values does not match with number of prepared statement markers(?).", "values");
            }
            _id = id;
            _metadata = metadata;
            _queryOptions = queryOptions;
            if (tracingEnabled)
            {
                _headerFlags = FrameHeader.HeaderFlag.Tracing;
            }

            if (Consistency.IsSerialConsistencyLevel())
            {
                throw new RequestInvalidException("Serial consistency specified as a non-serial one.");
            }
            if (queryOptions.SerialConsistency != ConsistencyLevel.Any && queryOptions.SerialConsistency.IsSerialConsistencyLevel() == false)
            {
                throw new RequestInvalidException("Non-serial consistency specified as a serial one.");
            }
            if (queryOptions.Timestamp != null && protocolVersion < 3)
            {
                throw new NotSupportedException("Timestamp for query is supported in Cassandra 2.1 or above.");
            }
        }
Example #9
0
        public int WriteFrame(short streamId, MemoryStream stream, ISerializer serializer)
        {
            //protocol v2: <type><n><query_1>...<query_n><consistency>
            //protocol v3: <type><n><query_1>...<query_n><consistency><flags>[<serial_consistency>][<timestamp>]
            var protocolVersion = serializer.ProtocolVersion;
            var wb = new FrameWriter(stream, serializer);

            if (Payload != null)
            {
                _headerFlags |= FrameHeader.HeaderFlag.CustomPayload;
            }
            wb.WriteFrameHeader((byte)_headerFlags, streamId, OpCode);
            if (Payload != null)
            {
                //A custom payload for this request
                wb.WriteBytesMap(Payload);
            }
            wb.WriteByte((byte)_type);
            wb.WriteUInt16((ushort)_requests.Count);
            foreach (var br in _requests)
            {
                br.WriteToBatch(wb);
            }
            wb.WriteUInt16((ushort)Consistency);
            if (protocolVersion.SupportsTimestamp())
            {
                if (protocolVersion.Uses4BytesQueryFlags())
                {
                    wb.WriteInt32((int)_batchFlags);
                }
                else
                {
                    wb.WriteByte((byte)_batchFlags);
                }

                wb.WriteUInt16((ushort)SerialConsistency);

                if (_timestamp != null)
                {
                    wb.WriteLong(_timestamp.Value);
                }

                if (_keyspace != null)
                {
                    wb.WriteString(_keyspace);
                }
            }
            return(wb.Close());
        }
Example #10
0
        public InternalPrepareRequest(string cqlQuery, string keyspace = null, IDictionary <string, byte[]> payload = null)
        {
            Query    = cqlQuery;
            Keyspace = keyspace;
            _payload = payload;
            if (payload != null)
            {
                _headerFlags |= FrameHeader.HeaderFlag.CustomPayload;
            }

            if (keyspace != null)
            {
                _prepareFlags |= PrepareFlags.WithKeyspace;
            }
        }
Example #11
0
 public int WriteFrame(short streamId, MemoryStream stream, Serializer serializer)
 {
     var wb = new FrameWriter(stream, serializer);
     if (Payload != null)
     {
         _headerFlags |= FrameHeader.HeaderFlag.CustomPayload;
     }
     wb.WriteFrameHeader((byte)_headerFlags, streamId, OpCode);
     if (Payload != null)
     {
         //A custom payload for this request
         wb.WriteBytesMap(Payload);
     }
     wb.WriteShortBytes(_id);
     _queryOptions.Write(wb, true);
     return wb.Close();
 }
Example #12
0
        public RequestFrame GetFrame(short streamId)
        {
            var wb = new BEBinaryWriter();

            if (Payload != null)
            {
                _headerFlags |= FrameHeader.HeaderFlag.CustomPayload;
            }
            wb.WriteFrameHeader((byte)ProtocolVersion, (byte)_headerFlags, streamId, OpCode);
            if (Payload != null)
            {
                //A custom payload for this request
                wb.WriteBytesMap(Payload);
            }
            wb.WriteLongString(_cqlQuery);
            _queryOptions.Write(wb, (byte)ProtocolVersion, false);
            return(wb.GetFrame());
        }
Example #13
0
        public int WriteFrame(short streamId, MemoryStream stream, Serializer serializer)
        {
            var wb = new FrameWriter(stream, serializer);

            if (Payload != null)
            {
                _headerFlags |= FrameHeader.HeaderFlag.CustomPayload;
            }
            wb.WriteFrameHeader((byte)_headerFlags, streamId, OpCode);
            if (Payload != null)
            {
                //A custom payload for this request
                wb.WriteBytesMap(Payload);
            }
            wb.WriteLongString(_cqlQuery);
            _queryOptions.Write(wb, false);
            return(wb.Close());
        }
Example #14
0
        public int WriteFrame(short streamId, MemoryStream stream)
        {
            var wb = new FrameWriter(stream);

            if (Payload != null)
            {
                _headerFlags |= FrameHeader.HeaderFlag.CustomPayload;
            }
            wb.WriteFrameHeader((byte)ProtocolVersion, (byte)_headerFlags, streamId, OpCode);
            if (Payload != null)
            {
                //A custom payload for this request
                wb.WriteBytesMap(Payload);
            }
            wb.WriteShortBytes(_id);
            _queryOptions.Write(wb, (byte)ProtocolVersion, true);
            return(wb.Close());
        }
Example #15
0
        public BatchRequest(int protocolVersion, BatchStatement statement, ConsistencyLevel consistency)
        {
            ProtocolVersion = protocolVersion;
            if (ProtocolVersion < 2)
            {
                throw new NotSupportedException("Batch request is supported in C* >= 2.0.x");
            }

            _type = statement.BatchType;
            _requests = statement.Queries
                .Select(q => q.CreateBatchRequest(ProtocolVersion))
                .ToArray();
            Consistency = consistency;
            _timestamp = statement.Timestamp;
            if (statement.IsTracing)
            {
                _headerFlags = FrameHeader.HeaderFlag.Tracing;
            }
            if (statement.SerialConsistencyLevel != ConsistencyLevel.Any)
            {
                if (protocolVersion < 3)
                {
                    throw new NotSupportedException("Serial consistency level for BATCH request is supported in Cassandra 2.1 or above.");
                }
                if (statement.SerialConsistencyLevel < ConsistencyLevel.Serial)
                {
                    throw new RequestInvalidException("Non-serial consistency specified as a serial one.");
                }
                _batchFlags |= QueryProtocolOptions.QueryFlags.WithSerialConsistency;
                _serialConsistency = statement.SerialConsistencyLevel;
            }
            if (_timestamp != null)
            {
                if (protocolVersion < 3)
                {
                    throw new NotSupportedException("Timestamp for BATCH request is supported in Cassandra 2.1 or above.");
                }
                _batchFlags |= QueryProtocolOptions.QueryFlags.WithDefaultTimestamp;
            }
        }
        public int WriteFrame(short streamId, MemoryStream stream, Serializer serializer)
        {
            //protocol v2: <type><n><query_1>...<query_n><consistency>
            //protocol v3: <type><n><query_1>...<query_n><consistency><flags>[<serial_consistency>][<timestamp>]
            var protocolVersion = serializer.ProtocolVersion;
            var wb = new FrameWriter(stream, serializer);

            if (Payload != null)
            {
                _headerFlags |= FrameHeader.HeaderFlag.CustomPayload;
            }
            wb.WriteFrameHeader((byte)_headerFlags, streamId, OpCode);
            if (Payload != null)
            {
                //A custom payload for this request
                wb.WriteBytesMap(Payload);
            }
            wb.WriteByte((byte)_type);
            wb.WriteInt16((short)_requests.Count);
            foreach (var br in _requests)
            {
                br.WriteToBatch(wb);
            }
            wb.WriteInt16((short)Consistency);
            if (protocolVersion >= 3)
            {
                wb.WriteByte((byte)_batchFlags);
            }
            if (_serialConsistency != null)
            {
                wb.WriteInt16((short)_serialConsistency.Value);
            }
            if (_timestamp != null)
            {
                //Expressed in microseconds
                wb.WriteLong(TypeSerializer.SinceUnixEpoch(_timestamp.Value).Ticks / 10);
            }
            return(wb.Close());
        }
Example #17
0
        public RequestFrame GetFrame(short streamId)
        {
            //protocol v2: <type><n><query_1>...<query_n><consistency>
            //protocol v3: <type><n><query_1>...<query_n><consistency><flags>[<serial_consistency>][<timestamp>]
            var wb = new BEBinaryWriter();

            if (Payload != null)
            {
                _headerFlags |= FrameHeader.HeaderFlag.CustomPayload;
            }
            wb.WriteFrameHeader((byte)ProtocolVersion, (byte)_headerFlags, streamId, OpCode);
            if (Payload != null)
            {
                //A custom payload for this request
                wb.WriteBytesMap(Payload);
            }
            wb.WriteByte((byte)_type);
            wb.WriteInt16((short)_requests.Count);
            foreach (var br in _requests)
            {
                br.WriteToBatch((byte)ProtocolVersion, wb);
            }
            wb.WriteInt16((short)Consistency);
            if (ProtocolVersion >= 3)
            {
                wb.WriteByte((byte)_batchFlags);
            }
            if (_serialConsistency != null)
            {
                wb.WriteInt16((short)_serialConsistency.Value);
            }
            if (_timestamp != null)
            {
                //Expressed in microseconds
                wb.WriteLong(TypeCodec.ToUnixTime(_timestamp.Value).Ticks / 10);
            }
            return(wb.GetFrame());
        }
Example #18
0
 public int WriteFrame(short streamId, MemoryStream stream, Serializer serializer)
 {
     //protocol v2: <type><n><query_1>...<query_n><consistency>
     //protocol v3: <type><n><query_1>...<query_n><consistency><flags>[<serial_consistency>][<timestamp>]
     var protocolVersion = serializer.ProtocolVersion;
     var wb = new FrameWriter(stream, serializer);
     if (Payload != null)
     {
         _headerFlags |= FrameHeader.HeaderFlag.CustomPayload;
     }
     wb.WriteFrameHeader((byte)_headerFlags, streamId, OpCode);
     if (Payload != null)
     {
         //A custom payload for this request
         wb.WriteBytesMap(Payload);
     }
     wb.WriteByte((byte) _type);
     wb.WriteInt16((short) _requests.Count);
     foreach (var br in _requests)
     {
         br.WriteToBatch(wb);
     }
     wb.WriteInt16((short) Consistency);
     if (protocolVersion >= 3)
     {
         wb.WriteByte((byte)_batchFlags);
     }
     if (_serialConsistency != null)
     {
         wb.WriteInt16((short)_serialConsistency.Value);
     }
     if (_timestamp != null)
     {
         //Expressed in microseconds
         wb.WriteLong(TypeSerializer.SinceUnixEpoch(_timestamp.Value).Ticks / 10);
     }
     return wb.Close();
 }
 public RequestFrame GetFrame(short streamId)
 {
     //protocol v2: <type><n><query_1>...<query_n><consistency>
     //protocol v3: <type><n><query_1>...<query_n><consistency><flags>[<serial_consistency>][<timestamp>]
     var wb = new BEBinaryWriter();
     if (Payload != null)
     {
         _headerFlags |= FrameHeader.HeaderFlag.CustomPayload;
     }
     wb.WriteFrameHeader((byte)ProtocolVersion, (byte)_headerFlags, streamId, OpCode);
     if (Payload != null)
     {
         //A custom payload for this request
         wb.WriteBytesMap(Payload);
     }
     wb.WriteByte((byte) _type);
     wb.WriteInt16((short) _requests.Count);
     foreach (var br in _requests)
     {
         br.WriteToBatch((byte)ProtocolVersion, wb);
     }
     wb.WriteInt16((short) Consistency);
     if (ProtocolVersion >= 3)
     {
         wb.WriteByte((byte)_batchFlags);
     }
     if (_serialConsistency != null)
     {
         wb.WriteInt16((short)_serialConsistency.Value);
     }
     if (_timestamp != null)
     {
         //Expressed in microseconds
         wb.WriteLong(TypeCodec.ToUnixTime(_timestamp.Value).Ticks / 10);
     }
     return wb.GetFrame();
 }
Example #20
0
 public int WriteFrame(short streamId, MemoryStream stream)
 {
     var wb = new FrameWriter(stream);
     if (Payload != null)
     {
         _headerFlags |= FrameHeader.HeaderFlag.CustomPayload;
     }
     wb.WriteFrameHeader((byte)ProtocolVersion, (byte)_headerFlags, streamId, OpCode);
     if (Payload != null)
     {
         //A custom payload for this request
         wb.WriteBytesMap(Payload);
     }
     wb.WriteLongString(_cqlQuery);
     _queryOptions.Write(wb, (byte)ProtocolVersion, false);
     return wb.Close();
 }