Exemplo n.º 1
0
        public QueryRequest(ProtocolVersion protocolVersion, string cqlQuery, bool tracingEnabled, QueryProtocolOptions queryOptions)
        {
            this.RequestId = Guid.NewGuid();

            _cqlQuery     = cqlQuery;
            _queryOptions = queryOptions;
            if (tracingEnabled)
            {
                _headerFlags = FrameHeader.HeaderFlag.Tracing;
            }
            if (queryOptions == null)
            {
                throw new ArgumentNullException("queryOptions");
            }
            if (queryOptions.SerialConsistency != ConsistencyLevel.Any && queryOptions.SerialConsistency.IsSerialConsistencyLevel() == false)
            {
                throw new RequestInvalidException("Non-serial consistency specified as a serial one.");
            }
            if (!protocolVersion.SupportsTimestamp())
            {
                //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.");
                }
            }
        }
Exemplo n.º 2
0
        internal static QueryProtocolOptions CreateFromQuery(
            ProtocolVersion protocolVersion, Statement query, IRequestOptions requestOptions)
        {
            if (query == null)
            {
                return(Default);
            }
            var  consistency = query.ConsistencyLevel ?? requestOptions.ConsistencyLevel;
            var  pageSize    = query.PageSize != 0 ? query.PageSize : requestOptions.PageSize;
            long?timestamp   = null;

            if (query.Timestamp != null)
            {
                timestamp = TypeSerializer.SinceUnixEpoch(query.Timestamp.Value).Ticks / 10;
            }
            else if (protocolVersion.SupportsTimestamp())
            {
                timestamp = requestOptions.TimestampGenerator.Next();
                if (timestamp == long.MinValue)
                {
                    timestamp = null;
                }
            }

            return(new QueryProtocolOptions(
                       consistency,
                       query.QueryValues,
                       query.SkipMetadata,
                       pageSize,
                       query.PagingState,
                       requestOptions.GetSerialConsistencyLevelOrDefault(query),
                       timestamp,
                       query.Keyspace));
        }
Exemplo n.º 3
0
        private QueryFlags GetFlags(ProtocolVersion protocolVersion)
        {
            QueryFlags flags = 0;

            if (Values != null && Values.Length > 0)
            {
                flags |= QueryFlags.Values;
            }
            if (_skipMetadata)
            {
                flags |= QueryFlags.SkipMetadata;
            }
            if (PageSize != int.MaxValue && PageSize >= 0)
            {
                flags |= QueryFlags.PageSize;
            }
            if (PagingState != null)
            {
                flags |= QueryFlags.WithPagingState;
            }
            if (SerialConsistency != ConsistencyLevel.Any)
            {
                flags |= QueryFlags.WithSerialConsistency;
            }
            if (protocolVersion.SupportsTimestamp() && RawTimestamp != null)
            {
                flags |= QueryFlags.WithDefaultTimestamp;
            }
            if (ValueNames != null && ValueNames.Count > 0)
            {
                flags |= QueryFlags.WithNameForValues;
            }
            return(flags);
        }
Exemplo n.º 4
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;
     }
 }
        internal static QueryProtocolOptions CreateFromQuery(ProtocolVersion protocolVersion, Statement query,
                                                             QueryOptions queryOptions, Policies policies)
        {
            if (query == null)
            {
                return(Default);
            }
            var  consistency = query.ConsistencyLevel ?? queryOptions.GetConsistencyLevel();
            var  pageSize    = query.PageSize != 0 ? query.PageSize : queryOptions.GetPageSize();
            long?timestamp   = null;

            if (query.Timestamp != null)
            {
                timestamp = TypeSerializer.SinceUnixEpoch(query.Timestamp.Value).Ticks / 10;
            }
            else if (protocolVersion.SupportsTimestamp())
            {
                timestamp = policies.TimestampGenerator.Next();
                if (timestamp == long.MinValue)
                {
                    timestamp = null;
                }
            }
            return(new QueryProtocolOptions(
                       consistency,
                       query.QueryValues,
                       query.SkipMetadata,
                       pageSize,
                       query.PagingState,
                       query.SerialConsistencyLevel,
                       timestamp));
        }
Exemplo n.º 6
0
        /// <summary>
        /// Gets the timestamp of the request or null if not defined.
        /// </summary>
        /// <exception cref="NotSupportedException" />
        private static long?GetRequestTimestamp(ProtocolVersion protocolVersion, BatchStatement statement, ITimestampGenerator timestampGenerator)
        {
            if (!protocolVersion.SupportsTimestamp())
            {
                if (statement.Timestamp != null)
                {
                    throw new NotSupportedException(
                              "Timestamp for BATCH request is supported in Cassandra 2.1 or above.");
                }
                return(null);
            }
            if (statement.Timestamp != null)
            {
                return(TypeSerializer.SinceUnixEpoch(statement.Timestamp.Value).Ticks / 10);
            }
            var timestamp = timestampGenerator.Next();

            return(timestamp != long.MinValue ? (long?)timestamp : null);
        }
Exemplo n.º 7
0
        private QueryFlags GetFlags(ProtocolVersion protocolVersion, bool isPrepared)
        {
            QueryFlags flags = 0;

            if (Values != null && Values.Length > 0)
            {
                flags |= QueryFlags.Values;
            }
            if (_skipMetadata)
            {
                flags |= QueryFlags.SkipMetadata;
            }
            if (PageSize != int.MaxValue && PageSize >= 0)
            {
                flags |= QueryFlags.PageSize;
            }
            if (PagingState != null)
            {
                flags |= QueryFlags.WithPagingState;
            }
            if (SerialConsistency != ConsistencyLevel.Any)
            {
                flags |= QueryFlags.WithSerialConsistency;
            }
            if (protocolVersion.SupportsTimestamp() && RawTimestamp != null)
            {
                flags |= QueryFlags.WithDefaultTimestamp;
            }
            if (ValueNames != null && ValueNames.Count > 0)
            {
                flags |= QueryFlags.WithNameForValues;
            }

            if (!isPrepared && protocolVersion.SupportsKeyspaceInRequest() && _keyspace != null)
            {
                // Providing keyspace is only useful for QUERY requests.
                // For EXECUTE requests, the keyspace will be the one from the prepared statement.
                flags |= QueryFlags.WithKeyspace;
            }

            return(flags);
        }
Exemplo n.º 8
0
        public ExecuteRequest(ProtocolVersion protocolVersion, byte[] id, RowSetMetadata metadata, bool tracingEnabled, QueryProtocolOptions queryOptions)
        {
            if (metadata != null && queryOptions.Values.Length != metadata.Columns.Length)
            {
                throw new ArgumentException("Number of values does not match with number of prepared statement markers(?).");
            }
            _id           = id;
            _queryOptions = queryOptions;
            if (tracingEnabled)
            {
                _headerFlags = FrameHeader.HeaderFlag.Tracing;
            }

            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.SupportsTimestamp())
            {
                throw new NotSupportedException("Timestamp for query is supported in Cassandra 2.1 or above.");
            }
        }