Class: Dse::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/dse/session.rb

Overview

A session is used to execute queries. In addition to executing standard CQL queries via the #execute and #execute_async methods, it executes graph queries via the #execute_graph_async and #execute_graph methods.

See Also:

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object (protected)



106
107
108
109
110
111
112
# File 'lib/dse/session.rb', line 106

def method_missing(method_name, *args, &block)
  # If we get here, we don't have a method of our own. Forward the request to @cassandra_session.
  # If it returns itself, we will coerce the result to return our *self* instead.

  result = @cassandra_session.send(method_name, *args, &block)
  (result == @cassandra_session) ? self : result
end

Instance Method Details

#execute_graph(statement, options = {}) ⇒ Cassandra::Result

Execute a graph statement synchronously.

Returns:

  • (Cassandra::Result)

    a Cassandra result containing individual JSON results.

See Also:



98
99
100
# File 'lib/dse/session.rb', line 98

def execute_graph(statement, options = {})
  execute_graph_async(statement, options).get
end

#execute_graph_async(graph_statement, options = {}) ⇒ Cassandra::Future<Cassandra::Result>

Execute a graph statement asynchronously.

Parameters:

  • graph_statement (String, Dse::Graph::Statement)

    a graph statement

  • options (Hash) (defaults to: {})

    a customizable set of options. All of the options supported by Cassandra::Session#execute_async are valid here. However, there are some extras, noted below.

Options Hash (options):

  • :arguments (Hash)

    Parameters for the graph statement. NOTE: Unlike #execute and #execute_async, this must be a hash of <parameter-name,value>.

  • :graph_name (String)

    name of graph to use in graph statements

  • :graph_source (String)

    graph traversal source

  • :graph_language (String)

    language used in graph queries

  • :graph_read_consistency (Cassandra::CONSISTENCIES)

    read consistency level for graph statements. Overrides the standard statement consistency level

  • :graph_write_consistency (Cassandra::CONSISTENCIES)

    write consistency level for graph statements. Overrides the standard statement consistency level

  • :execution_profile (String, Symbol) — default: :default_graph

    name of Graph::ExecutionProfile from which to obtain certain query options.

Returns:

  • (Cassandra::Future<Cassandra::Result>)

See Also:



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/dse/session.rb', line 43

def execute_graph_async(graph_statement, options = {})
  # Make our own copy of the options. The caller might want to re-use the options they provided, and we're
  # about to do some destructive mutations.

  options = options.dup

  Cassandra::Util.assert_instance_of_one_of([String, Dse::Graph::Statement], graph_statement)
  arguments = nil
  if options
    Cassandra::Util.assert_instance_of(::Hash, options)
    arguments = options.delete(:arguments)
  end

  if graph_statement.is_a?(String)
    graph_statement = Dse::Graph::Statement.new(graph_statement, arguments, options, options[:idempotent])
  end

  options[:execution_profile] ||= :default_graph
  profile = @profile_manager.profiles[options[:execution_profile]]
  Cassandra::Util.assert(!profile.nil?, "Profile '#{options[:execution_profile]}' does not exist")
  Cassandra::Util.assert_instance_of(Dse::Graph::ExecutionProfile, profile)

  graph_options = profile.graph_options.merge(graph_statement.options)

  # The custom payload should have a 'request-timeout' that is the value of the :timeout option, if specified.
  # Otherwise, fall back to the timeout in the execution profile.
  options[:payload] = graph_options.as_payload(options[:timeout] || profile.timeout)

  if graph_options.analytics?
    @cassandra_session.execute_async('CALL DseClientTool.getAnalyticsGraphServer()').then do |rows|
      row = rows.first
      if row.nil? || row['result'].nil?
        @cassandra_session.execute_async(graph_statement, options).then do |raw_result|
          Dse::Graph::ResultSet.new(raw_result)
        end
      else
        ip = row['result']['ip']
        targeted_statement = Dse::Statements::HostTargeting.new(graph_statement, ip)
        @cassandra_session.execute_async(targeted_statement, options).then do |raw_result|
          Dse::Graph::ResultSet.new(raw_result)
        end
      end
    end
  else
    @cassandra_session.execute_async(graph_statement, options).then do |raw_result|
      Dse::Graph::ResultSet.new(raw_result)
    end
  end
rescue => e
  @futures.error(e)
end