internal IDictionary <string, byte[]> BuildPayload(IGraphStatement statement)
        {
            if (statement.GraphLanguage == null && statement.GraphName == null &&
                statement.GraphSource == null && statement.ReadTimeoutMillis == 0)
            {
                if (!statement.IsSystemQuery || !_defaultPayload.ContainsKey(PayloadKey.Name))
                {
                    //The user has not used the graph settings at statement level
                    //Or is a system query but there isn't a name defined at GraphOptions level
                    return(_defaultPayload);
                }
            }
            var payload = new Dictionary <string, byte[]>();

            Add(payload, PayloadKey.Language, statement.GraphLanguage, null);
            if (!statement.IsSystemQuery)
            {
                Add(payload, PayloadKey.Name, statement.GraphName, null);
            }
            Add(payload, PayloadKey.Source, statement.GraphSource, null);
            Add(payload, PayloadKey.ReadConsistencyLevel,
                statement.GraphReadConsistencyLevel == null ? null : GetConsistencyName(statement.GraphReadConsistencyLevel.Value), null);
            Add(payload, PayloadKey.WriteConsitencyLevel,
                statement.GraphWriteConsistencyLevel == null ? null : GetConsistencyName(statement.GraphWriteConsistencyLevel.Value), null);
            var readTimeout = statement.ReadTimeoutMillis != 0 ? statement.ReadTimeoutMillis : ReadTimeoutMillis;

            if (readTimeout > 0)
            {
                // only non-infinite timeouts needs to be included in the payload
                Add <long>(payload, PayloadKey.RequestTimeout, statement.ReadTimeoutMillis, 0, ToBuffer);
            }
            return(payload);
        }
Example #2
0
        private ConvertedStatementResult GetIStatement(IGraphStatement graphStmt, GraphOptions options)
        {
            var graphProtocol = graphStmt.GraphProtocolVersion ?? options.GraphProtocolVersion;

            if (graphProtocol == null)
            {
                throw new DriverInternalError("Unable to determine graph protocol version. This is a bug, please report.");
            }

            // Existing graph statement implementations of this method are empty
            // but it's part of the public interface definition
            var stmt = graphStmt.ToIStatement(options);

            if (stmt != null)
            {
                return(new ConvertedStatementResult
                {
                    Serializer = _graphTypeSerializerFactory.CreateSerializer(
                        _session, null, null, graphProtocol.Value, true),
                    Statement = stmt
                });
            }

            return(ConvertGraphStatement(graphStmt, options, graphProtocol.Value));
        }
Example #3
0
        /// <inheritdoc />
        public GraphProtocol GetDefaultGraphProtocol(IInternalSession session, IGraphStatement statement, GraphOptions options)
        {
            var protocol = GetByKeyspaceMetadata(session, statement, options);

            if (protocol != null)
            {
                GraphTypeSerializerFactory.Logger.Verbose(
                    "Resolved graph protocol {0} according to the keyspace metadata.", protocol.Value.GetInternalRepresentation());
                return(protocol.Value);
            }

            protocol = GetByLanguage(statement, options);

            if (protocol != null)
            {
                GraphTypeSerializerFactory.Logger.Verbose(
                    "Resolved graph protocol {0} according to the graph language option.", protocol.Value.GetInternalRepresentation());
                return(protocol.Value);
            }

            protocol = GraphProtocol.GraphSON2;

            GraphTypeSerializerFactory.Logger.Verbose(
                "Unable to resolve graph protocol according to keyspace metadata or graph language. " +
                "Resolved graph protocol to {0}.", protocol.Value.GetInternalRepresentation());
            return(protocol.Value);
        }
Example #4
0
        private async Task <IStatement> GetAnalyticsPrimary(
            IStatement statement, IGraphStatement graphStatement, IRequestOptions requestOptions)
        {
            if (!(statement is TargettedSimpleStatement) || !requestOptions.GraphOptions.IsAnalyticsQuery(graphStatement))
            {
                return(statement);
            }

            var targetedSimpleStatement = (TargettedSimpleStatement)statement;

            RowSet rs;

            try
            {
                rs = await _session.ExecuteAsync(
                    new SimpleStatement("CALL DseClientTool.getAnalyticsGraphServer()"), requestOptions).ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                GraphRequestHandler.Logger.Verbose("Error querying graph analytics server, query will not be routed optimally: {0}", ex);
                return(statement);
            }

            return(AdaptRpcPrimaryResult(rs, targetedSimpleStatement));
        }
Example #5
0
        private GraphProtocol?GetByKeyspaceMetadata(
            ISession session, IGraphStatement statement, GraphOptions options)
        {
            var graphName = statement.GraphName ?? options.Name;

            if (graphName == null)
            {
                return(null);
            }

            var ksMetadata = session.Cluster.Metadata.GetKeyspaceFromCache(graphName);

            if (ksMetadata?.GraphEngine == null)
            {
                return(null);
            }

            if (ksMetadata.GraphEngine.Equals(
                    GraphTypeSerializerFactory.CoreEngine, StringComparison.InvariantCultureIgnoreCase))
            {
                return(GraphProtocol.GraphSON3);
            }

            return(null);
        }
 private static void CompareGraphOptionsOnStatement(GraphOptions options, IGraphStatement statement)
 {
     Assert.AreEqual(options.Name, statement.GraphName);
     Assert.AreEqual(options.Source, statement.GraphSource);
     Assert.AreEqual(options.ReadTimeoutMillis, statement.ReadTimeoutMillis);
     Assert.AreEqual(options.ReadConsistencyLevel, statement.GraphReadConsistencyLevel);
     Assert.AreEqual(options.WriteConsistencyLevel, statement.GraphWriteConsistencyLevel);
 }
Example #7
0
 public Task<GraphResultSet> ExecuteGraphAsync(IGraphStatement graphStatement)
 {
     var stmt = graphStatement.ToIStatement(_config.GraphOptions);
     return GetAnalyticsMaster(stmt, graphStatement)
         .Then(s =>
             _coreSession
                 .ExecuteAsync(s)
                 .ContinueSync(rs => new GraphResultSet(rs)));
 }
Example #8
0
        public async Task <GraphResultSet> ExecuteGraphAsync(IGraphStatement graphStatement)
        {
            var stmt = graphStatement.ToIStatement(_config.GraphOptions);

            await GetAnalyticsMaster(stmt, graphStatement).ConfigureAwait(false);

            var rs = await _coreSession.ExecuteAsync(stmt).ConfigureAwait(false);

            return(GraphResultSet.CreateNew(rs, graphStatement, _config.GraphOptions));
        }
Example #9
0
        public Task <GraphResultSet> ExecuteGraphAsync(IGraphStatement graphStatement)
        {
            var stmt = graphStatement.ToIStatement(_config.GraphOptions);

            return(GetAnalyticsMaster(stmt, graphStatement)
                   .Then(s =>
                         _coreSession
                         .ExecuteAsync(s)
                         .ContinueSync(rs => new GraphResultSet(rs))));
        }
Example #10
0
        private GraphProtocol?GetByLanguage(IGraphStatement statement, GraphOptions options)
        {
            var language = statement.GraphLanguage ?? options.Language;

            if (language == GraphOptions.GremlinGroovy)
            {
                return(GraphProtocol.GraphSON1);
            }

            return(null);
        }
Example #11
0
 public Task<IStatement> GetAnalyticsMaster(IStatement statement, IGraphStatement graphStatement)
 {
     if (!(statement is TargettedSimpleStatement) || !_config.GraphOptions.IsAnalyticsQuery(graphStatement))
     {
         return TaskHelper.ToTask(statement);
     }
     var targettedSimpleStatement = (TargettedSimpleStatement) statement;
     return _coreSession
         .ExecuteAsync(new SimpleStatement("CALL DseClientTool.getAnalyticsGraphServer()"))
         .ContinueWith(t => AdaptRpcMasterResult(t, targettedSimpleStatement), TaskContinuationOptions.ExecuteSynchronously);
 }
Example #12
0
        public Task <IStatement> GetAnalyticsMaster(IStatement statement, IGraphStatement graphStatement)
        {
            if (!(statement is TargettedSimpleStatement) || !_config.GraphOptions.IsAnalyticsQuery(graphStatement))
            {
                return(TaskHelper.ToTask(statement));
            }
            var targettedSimpleStatement = (TargettedSimpleStatement)statement;

            return(_coreSession
                   .ExecuteAsync(new SimpleStatement("CALL DseClientTool.getAnalyticsGraphServer()"))
                   .ContinueWith(t => AdaptRpcMasterResult(t, targettedSimpleStatement), TaskContinuationOptions.ExecuteSynchronously));
        }
        private static void WithMock(WithMockDelegate handler)
        {
            IGraphStatement statement   = null;
            var             sessionMock = new Mock <ISession>();

            sessionMock.Setup(s => s.ExecuteGraphAsync(It.IsAny <IGraphStatement>()))
            .ReturnsAsync(new GraphResultSet(new RowSet()))
            .Callback <IGraphStatement>(stmt =>
            {
                statement = stmt;
            });
            sessionMock.Setup(s => s.ExecuteGraph(It.IsAny <IGraphStatement>()))
            .Returns(new GraphResultSet(new RowSet()))
            .Callback <IGraphStatement>(stmt =>
            {
                statement = stmt;
            });
            handler(sessionMock.Object, ref statement);
        }
Example #14
0
        internal IDictionary <string, byte[]> BuildPayload(IGraphStatement statement)
        {
            if (statement.GraphProtocolVersion == null && GraphProtocolVersion == null)
            {
                throw new DriverInternalError(
                          "Could not resolve graph protocol version. This is a bug, please report.");
            }

            if (statement.GraphLanguage == null && statement.GraphName == null &&
                statement.GraphSource == null && statement.ReadTimeoutMillis == 0 &&
                statement.GraphProtocolVersion == null)
            {
                if (!statement.IsSystemQuery || !_defaultPayload.ContainsKey(PayloadKey.Name))
                {
                    //The user has not used the graph settings at statement level
                    //Or is a system query but there isn't a name defined at GraphOptions level
                    return(_defaultPayload);
                }
            }
            var payload = new Dictionary <string, byte[]>();

            Add(payload, PayloadKey.Results, statement.GraphProtocolVersion?.GetInternalRepresentation(), null);
            Add(payload, PayloadKey.Language, statement.GraphLanguage, null);
            if (!statement.IsSystemQuery)
            {
                Add(payload, PayloadKey.Name, statement.GraphName, null);
            }
            Add(payload, PayloadKey.Source, statement.GraphSource, null);
            Add(payload, PayloadKey.ReadConsistencyLevel,
                statement.GraphReadConsistencyLevel == null ? null : GraphOptions.GetConsistencyName(statement.GraphReadConsistencyLevel.Value), null);
            Add(payload, PayloadKey.WriteConsitencyLevel,
                statement.GraphWriteConsistencyLevel == null ? null : GraphOptions.GetConsistencyName(statement.GraphWriteConsistencyLevel.Value), null);
            var readTimeout = statement.ReadTimeoutMillis != 0 ? statement.ReadTimeoutMillis : ReadTimeoutMillis;

            if (readTimeout > 0)
            {
                // only non-infinite timeouts needs to be included in the payload
                Add <long>(payload, PayloadKey.RequestTimeout, statement.ReadTimeoutMillis, 0, GraphOptions.ToBuffer);
            }
            return(payload);
        }
Example #15
0
        private async Task <GraphResultSet> ExecuteGraphAsync(IGraphStatement graphStatement, IRequestOptions requestOptions)
        {
            var graphOptions = requestOptions.GraphOptions;

            if (graphStatement.GraphProtocolVersion == null && requestOptions.GraphOptions.GraphProtocolVersion == null)
            {
                var version = _graphTypeSerializerFactory.GetDefaultGraphProtocol(
                    _session, graphStatement, requestOptions.GraphOptions);
                graphOptions = new GraphOptions(graphOptions, version);
            }

            var conversionResult = GetIStatement(graphStatement, graphOptions);

            var stmt       = conversionResult.Statement;
            var serializer = conversionResult.Serializer;

            await GetAnalyticsPrimary(stmt, graphStatement, requestOptions).ConfigureAwait(false);

            var rs = await _session.ExecuteAsync(stmt, requestOptions).ConfigureAwait(false);

            return(CreateGraphResultSet(rs, serializer));
        }
Example #16
0
 public GraphResultSet ExecuteGraph(IGraphStatement statement)
 {
     return(TaskHelper.WaitToComplete(ExecuteGraphAsync(statement)));
 }
Example #17
0
        private ConvertedStatementResult ConvertGraphStatement(
            IGraphStatement graphStmt, GraphOptions options, GraphProtocol graphProtocol)
        {
            string jsonParams;
            string query;
            IGraphTypeSerializer serializer;

            if (graphStmt is SimpleGraphStatement simpleGraphStatement)
            {
                serializer = _graphTypeSerializerFactory.CreateSerializer(_session, null, null, graphProtocol, true);
                query      = simpleGraphStatement.Query;
                if (simpleGraphStatement.ValuesDictionary != null)
                {
                    jsonParams = serializer.ToDb(simpleGraphStatement.ValuesDictionary);
                }
                else if (simpleGraphStatement.Values != null)
                {
                    jsonParams = serializer.ToDb(Utils.GetValues(simpleGraphStatement.Values));
                }
                else
                {
                    jsonParams = null;
                }
            }
            else if (graphStmt is FluentGraphStatement fluentGraphStatement)
            {
                serializer = _graphTypeSerializerFactory.CreateSerializer(
                    _session,
                    fluentGraphStatement.CustomDeserializers,
                    fluentGraphStatement.CustomSerializers,
                    graphProtocol,
                    fluentGraphStatement.DeserializeGraphNodes);
                query      = serializer.ToDb(fluentGraphStatement.QueryBytecode);
                jsonParams = null;
            }
            else
            {
                throw new NotSupportedException("Statement of type " + graphStmt.GetType().FullName + " not supported");
            }

            IStatement stmt = jsonParams != null
                ? new TargettedSimpleStatement(query, jsonParams)
                : new TargettedSimpleStatement(query);

            //Set Cassandra.Statement properties
            if (graphStmt.Timestamp != null)
            {
                stmt.SetTimestamp(graphStmt.Timestamp.Value);
            }
            var readTimeout = graphStmt.ReadTimeoutMillis != 0 ? graphStmt.ReadTimeoutMillis : options.ReadTimeoutMillis;

            if (readTimeout <= 0)
            {
                // Infinite (-1) is not supported in the core driver, set an arbitrarily large int
                readTimeout = int.MaxValue;
            }

            stmt = stmt
                   .SetIdempotence(false)
                   .SetConsistencyLevel(graphStmt.ConsistencyLevel)
                   .SetReadTimeoutMillis(readTimeout)
                   .SetOutgoingPayload(options.BuildPayload(graphStmt));

            return(new ConvertedStatementResult
            {
                Serializer = serializer,
                Statement = stmt
            });
        }
Example #18
0
 public GraphResultSet ExecuteGraph(IGraphStatement statement)
 {
     return TaskHelper.WaitToComplete(ExecuteGraphAsync(statement));
 }
Example #19
0
 public Task <GraphResultSet> SendAsync(IGraphStatement graphStatement, IRequestOptions requestOptions)
 {
     return(ExecuteGraphAsync(graphStatement, requestOptions));
 }
 internal bool IsAnalyticsQuery(IGraphStatement statement)
 {
     return((statement.GraphSource ?? Source) == "a");
 }
Example #21
0
 internal static GraphResultSet CreateNew(RowSet rs, IGraphStatement statement, GraphOptions options)
 {
     return(new GraphResultSet(rs, statement.GraphLanguage ?? options.Language));
 }