Class DefaultBridgedReactiveSession

java.lang.Object
org.springframework.data.cassandra.core.cql.session.DefaultBridgedReactiveSession
All Implemented Interfaces:
Closeable, AutoCloseable, ReactiveSession

public class DefaultBridgedReactiveSession extends Object implements ReactiveSession
Default implementation of a ReactiveSession. This implementation bridges asynchronous CqlSession methods to reactive execution patterns.

Calls are deferred until a subscriber subscribes to the resulting Publisher. The calls are executed by subscribing to CompletionStage and returning the result as calls complete.

Elements are emitted on netty EventLoop threads. AsyncResultSet allows AsyncPagingIterable.fetchNextPage() asynchronous requesting} of subsequent pages. The next page is requested after emitting all elements of the previous page. However, this is an intermediate solution until Datastax can provide a fully reactive driver.

All CQL operations performed by this class are logged at debug level, using org.springframework.data.cassandra.core.cql.DefaultBridgedReactiveSession as log category.

Since:
2.0
Author:
Mark Paluch, Mateusz Stefek
See Also:
  • Constructor Summary

    Constructors
    Constructor
    Description
    DefaultBridgedReactiveSession(com.datastax.oss.driver.api.core.CqlSession session)
    Create a new DefaultBridgedReactiveSession for a CqlSession.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Initiates a shutdown of this session instance and blocks until that shutdown completes.
    reactor.core.publisher.Mono<ReactiveResultSet>
    execute(com.datastax.oss.driver.api.core.cql.Statement<?> statement)
    Executes the provided query.
    reactor.core.publisher.Mono<ReactiveResultSet>
    execute(String query)
    Executes the provided query.
    reactor.core.publisher.Mono<ReactiveResultSet>
    execute(String query, Object... values)
    Executes the provided query using the provided values.
    reactor.core.publisher.Mono<ReactiveResultSet>
    execute(String query, Map<String,Object> values)
    Executes the provided query using the provided named values.
    com.datastax.oss.driver.api.core.context.DriverContext
    Returns a context that provides access to all the policies used by this driver instance.
    Optional<com.datastax.oss.driver.api.core.CqlIdentifier>
    The keyspace that this session is currently connected to, or Optional.empty() if this session is not connected to any keyspace.
    com.datastax.oss.driver.api.core.metadata.Metadata
    Returns a snapshot of the Cassandra cluster's topology and schema metadata.
    boolean
    Whether this Session instance has been closed.
    reactor.core.publisher.Mono<com.datastax.oss.driver.api.core.cql.PreparedStatement>
    prepare(com.datastax.oss.driver.api.core.cql.SimpleStatement statement)
    Prepares the provided query.
    reactor.core.publisher.Mono<com.datastax.oss.driver.api.core.cql.PreparedStatement>
    prepare(String query)
    Prepares the provided query string.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • DefaultBridgedReactiveSession

      public DefaultBridgedReactiveSession(com.datastax.oss.driver.api.core.CqlSession session)
      Create a new DefaultBridgedReactiveSession for a CqlSession.
      Parameters:
      session - must not be null.
      Since:
      2.1
  • Method Details

    • getMetadata

      public com.datastax.oss.driver.api.core.metadata.Metadata getMetadata()
      Description copied from interface: ReactiveSession
      Returns a snapshot of the Cassandra cluster's topology and schema metadata.

      In order to provide atomic updates, this method returns an immutable object: the node list, token map, and schema contained in a given instance will always be consistent with each other (but note that Node itself is not immutable: some of its properties will be updated dynamically, in particular Node.getState()).

      As a consequence of the above, you should call this method each time you need a fresh view of the metadata. Do not call it once and store the result, because it is a frozen snapshot that will become stale over time.

      If a metadata refresh triggers events (such as node added/removed, or schema events), then the new version of the metadata is guaranteed to be visible by the time you receive these events.

      Specified by:
      getMetadata in interface ReactiveSession
      Returns:
      never null, but may be empty if metadata has been disabled in the configuration.
    • getKeyspace

      public Optional<com.datastax.oss.driver.api.core.CqlIdentifier> getKeyspace()
      Description copied from interface: ReactiveSession
      The keyspace that this session is currently connected to, or Optional.empty() if this session is not connected to any keyspace.

      There are two ways that this can be set: before initializing the session (either with the session-keyspace option in the configuration, or with SessionBuilder.withKeyspace(CqlIdentifier)); or at runtime, if the client issues a request that changes the keyspace (such as a CQL USE query). Note that this second method is inherently unsafe, since other requests expecting the old keyspace might be executing concurrently. Therefore it is highly discouraged, aside from trivial cases (such as a cqlsh-style program where requests are never concurrent).

      Specified by:
      getKeyspace in interface ReactiveSession
    • isClosed

      public boolean isClosed()
      Description copied from interface: ReactiveSession
      Whether this Session instance has been closed.

      Note that this method returns true as soon as the closing of this Session has started but it does not guarantee that the closing is done. If you want to guarantee that the closing is done, you can call close() and wait until it returns (or call the get method on closeAsync() with a very short timeout and check this doesn't timeout).

      Specified by:
      isClosed in interface ReactiveSession
      Returns:
      true if this Session instance has been closed, false otherwise.
    • getContext

      public com.datastax.oss.driver.api.core.context.DriverContext getContext()
      Description copied from interface: ReactiveSession
      Returns a context that provides access to all the policies used by this driver instance.
      Specified by:
      getContext in interface ReactiveSession
      Returns:
      a context that provides access to all the policies used by this driver instance.
    • execute

      public reactor.core.publisher.Mono<ReactiveResultSet> execute(String query)
      Description copied from interface: ReactiveSession
      Executes the provided query.

      This is a convenience method for execute(new SimpleStatement(query)).

      Specified by:
      execute in interface ReactiveSession
      Parameters:
      query - the CQL query to execute.
      Returns:
      the result of the query. That result will never be null but can be empty (and will be for any non SELECT query).
    • execute

      public reactor.core.publisher.Mono<ReactiveResultSet> execute(String query, Object... values)
      Description copied from interface: ReactiveSession
      Executes the provided query using the provided values.

      This is a convenience method for execute(new SimpleStatement(query, values)).

      Specified by:
      execute in interface ReactiveSession
      Parameters:
      query - the CQL query to execute.
      values - values required for the execution of query. See SimpleStatement.newInstance(String, Object...) for more details.
      Returns:
      the result of the query. That result will never be null but can be empty (and will be for any non SELECT query).
    • execute

      public reactor.core.publisher.Mono<ReactiveResultSet> execute(String query, Map<String,Object> values)
      Description copied from interface: ReactiveSession
      Executes the provided query using the provided named values.

      This is a convenience method for execute(new SimpleStatement(query, values)).

      Specified by:
      execute in interface ReactiveSession
      Parameters:
      query - the CQL query to execute.
      values - values required for the execution of query. See SimpleStatement.newInstance(String, Map) for more details.
      Returns:
      the result of the query. That result will never be null but can be empty (and will be for any non SELECT query).
    • execute

      public reactor.core.publisher.Mono<ReactiveResultSet> execute(com.datastax.oss.driver.api.core.cql.Statement<?> statement)
      Description copied from interface: ReactiveSession
      Executes the provided query.

      This method blocks until at least some result has been received from the database. However, for SELECT queries, it does not guarantee that the result has been received in full. But it does guarantee that some response has been received from the database, and in particular guarantees that if the request is invalid, an exception will be thrown by this method.

      Specified by:
      execute in interface ReactiveSession
      Parameters:
      statement - the CQL query to execute (that can be any Statement).
      Returns:
      the result of the query. That result will never be null but can be empty (and will be for any non SELECT query).
    • prepare

      public reactor.core.publisher.Mono<com.datastax.oss.driver.api.core.cql.PreparedStatement> prepare(String query)
      Description copied from interface: ReactiveSession
      Prepares the provided query string.
      Specified by:
      prepare in interface ReactiveSession
      Parameters:
      query - the CQL query string to prepare
      Returns:
      the prepared statement corresponding to query.
    • prepare

      public reactor.core.publisher.Mono<com.datastax.oss.driver.api.core.cql.PreparedStatement> prepare(com.datastax.oss.driver.api.core.cql.SimpleStatement statement)
      Description copied from interface: ReactiveSession
      Prepares the provided query.

      This method behaves like ReactiveSession.prepare(String), but note that the resulting PreparedStatement will inherit the query properties set on statement. Concretely, this means that in the following code:

       Statement toPrepare = SimpleStatement.newInstance("SELECT * FROM test WHERE k=?")
                      .setConsistencyLevel(ConsistencyLevel.QUORUM);
       PreparedStatement prepared = session.prepare(toPrepare);
       session.execute(prepared.bind("someValue"));
       
      the final execution will be performed with Quorum consistency.

      Please note that if the same CQL statement is prepared more than once, all calls to this method will return the same PreparedStatement object but the method will still apply the properties of the prepared Statement to this object.

      Specified by:
      prepare in interface ReactiveSession
      Parameters:
      statement - the statement to prepare
      Returns:
      the prepared statement corresponding to statement.
    • close

      public void close()
      Description copied from interface: ReactiveSession
      Initiates a shutdown of this session instance and blocks until that shutdown completes.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Specified by:
      close in interface ReactiveSession