Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright 2016-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.cassandra.core.cql;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import java.util.Map;
import java.util.function.Function;
import java.util.function.Supplier;
import org.reactivestreams.Publisher;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.support.DataAccessUtils;
import org.springframework.data.cassandra.ReactiveResultSet;
import org.springframework.data.cassandra.ReactiveSession;
import org.springframework.data.cassandra.ReactiveSessionFactory;
import org.springframework.data.cassandra.core.cql.session.DefaultReactiveSessionFactory;
import org.springframework.data.util.Lazy;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import com.datastax.oss.driver.api.core.ConsistencyLevel;
import com.datastax.oss.driver.api.core.CqlIdentifier;
import com.datastax.oss.driver.api.core.CqlSession;
import com.datastax.oss.driver.api.core.DriverException;
import com.datastax.oss.driver.api.core.cql.BatchStatement;
import com.datastax.oss.driver.api.core.cql.BoundStatement;
import com.datastax.oss.driver.api.core.cql.PreparedStatement;
import com.datastax.oss.driver.api.core.cql.Row;
import com.datastax.oss.driver.api.core.cql.SimpleStatement;
import com.datastax.oss.driver.api.core.cql.Statement;
import com.datastax.oss.driver.api.core.retry.RetryPolicy;
/**
* This is the central class in the CQL core package for reactive Cassandra data access. It simplifies the use of
* CQL and helps to avoid common errors. It executes core CQL workflow, leaving application code to provide CQL and
* extract results. This class executes CQL queries or updates, initiating iteration over {@link ReactiveResultSet}s and
* catching {@link DriverException} exceptions and translating them to the generic, more informative exception hierarchy
* defined in the {@code org.springframework.dao} package.
*
* Code using this class need only implement callback interfaces, giving them a clearly defined contract. The
* {@link PreparedStatementCreator} callback interface creates a prepared statement given a Connection, providing CQL
* and any necessary parameters. The {@link ResultSetExtractor} interface extracts values from a
* {@link ReactiveResultSet}. See also {@link PreparedStatementBinder} and {@link RowMapper} for two popular alternative
* callback interfaces.
*
* Can be used within a service implementation via direct instantiation with a {@link ReactiveSessionFactory} reference,
* or get prepared in an application context and given to services as bean reference. Note: The
* {@link ReactiveSessionFactory} should always be configured as a bean in the application context, in the first case
* given to the service directly, in the second case to the prepared template.
*
* Because this class is parameterizable by the callback interfaces and the
* {@link org.springframework.dao.support.PersistenceExceptionTranslator} interface, there should be no need to subclass
* it.
*
* All CQL operations performed by this class are logged at debug level, using
* "org.springframework.data.cassandra.core.cql.ReactiveCqlTemplate" as log category.
*
* NOTE: An instance of this class is thread-safe once configured.
*
* @author Mark Paluch
* @author Tomasz Lelek
* @since 2.0
* @see PreparedStatementCreator
* @see PreparedStatementBinder
* @see PreparedStatementCallback
* @see ResultSetExtractor
* @see RowCallbackHandler
* @see RowMapper
* @see org.springframework.dao.support.PersistenceExceptionTranslator
*/
public class ReactiveCqlTemplate extends ReactiveCassandraAccessor implements ReactiveCqlOperations {
/**
* If this variable is set to a value, it will be used for setting the {@code consistencyLevel} property on statements
* used for query processing.
*/
private @Nullable ConsistencyLevel consistencyLevel;
/**
* If this variable is set to a value, it will be used for setting the {@code executionProfile} property on statements
* used for query processing.
*/
private ExecutionProfileResolver executionProfileResolver = ExecutionProfileResolver.none();
/**
* If this variable is set to a value, it will be used for setting the {@code keyspace} property on statements used
* for query processing.
*/
private @Nullable CqlIdentifier keyspace;
/**
* If this variable is set to a non-negative value, it will be used for setting the {@code pageSize} property on
* statements used for query processing.
*/
private int pageSize = -1;
/**
* If this variable is set to a value, it will be used for setting the serial {@code consistencyLevel} property on
* statements used for query processing.
*/
private @Nullable ConsistencyLevel serialConsistencyLevel;
/**
* Construct a new {@link ReactiveCqlTemplate}. Note: The {@link ReactiveSessionFactory} has to be set before using
* the instance.
*
* @see #setSessionFactory
*/
public ReactiveCqlTemplate() {}
/**
* Construct a new {@link ReactiveCqlTemplate}, given a {@link ReactiveSession}.
*
* @param reactiveSession the {@link ReactiveSession}, must not be {@literal null}.
*/
public ReactiveCqlTemplate(ReactiveSession reactiveSession) {
Assert.notNull(reactiveSession, "ReactiveSession must not be null");
setSessionFactory(new DefaultReactiveSessionFactory(reactiveSession));
afterPropertiesSet();
}
/**
* Construct a new {@link ReactiveCqlTemplate}, given a {@link ReactiveSessionFactory} to obtain
* {@link ReactiveSession}s from.
*
* @param reactiveSessionFactory the {@link ReactiveSessionFactory} to obtain {@link ReactiveSession}s from, must not
* be {@literal null}.
*/
public ReactiveCqlTemplate(ReactiveSessionFactory reactiveSessionFactory) {
setSessionFactory(reactiveSessionFactory);
afterPropertiesSet();
}
/**
* Set the consistency level for this {@link ReactiveCqlTemplate}. Consistency level defines the number of nodes
* involved into query processing. Relaxed consistency level settings use fewer nodes but eventual consistency is more
* likely to occur while a higher consistency level involves more nodes to obtain results with a higher consistency
* guarantee.
*
* @see Statement#setConsistencyLevel(ConsistencyLevel)
* @see RetryPolicy
*/
public void setConsistencyLevel(@Nullable ConsistencyLevel consistencyLevel) {
this.consistencyLevel = consistencyLevel;
}
/**
* @return the {@link ConsistencyLevel} specified for this {@link ReactiveCqlTemplate}.
*/
@Nullable
public ConsistencyLevel getConsistencyLevel() {
return consistencyLevel;
}
/**
* Set the driver execution profile for this template.
*
* @see Statement#setExecutionProfileName(String)
* @see ExecutionProfileResolver
* @since 3.0
*/
public void setExecutionProfile(String profileName) {
setExecutionProfileResolver(ExecutionProfileResolver.from(profileName));
}
/**
* Set the {@link ExecutionProfileResolver} for this template.
*
* @see com.datastax.oss.driver.api.core.config.DriverExecutionProfile
* @see ExecutionProfileResolver
* @since 3.0
*/
public void setExecutionProfileResolver(ExecutionProfileResolver executionProfileResolver) {
Assert.notNull(executionProfileResolver, "ExecutionProfileResolver must not be null");
this.executionProfileResolver = executionProfileResolver;
}
/**
* @return the {@link ExecutionProfileResolver} specified for this {@link ReactiveCqlTemplate}.
* @since 3.0
*/
public ExecutionProfileResolver getExecutionProfileResolver() {
return executionProfileResolver;
}
/**
* Set the fetch size for this template. This is important for processing large result sets: Setting this higher than
* the default value will increase processing speed at the cost of memory consumption; setting this lower can avoid
* transferring row data that will never be read by the application. Default is -1, indicating to use the CQL driver's
* default configuration (i.e. to not pass a specific fetch size setting on to the driver).
*
* @see com.datastax.oss.driver.api.core.cql.SimpleStatementBuilder#setPageSize(int)
* @deprecated since 3.0, use {@link #setPageSize(int)}
*/
@Deprecated
public void setFetchSize(int fetchSize) {
setPageSize(fetchSize);
}
/**
* @return the fetch size specified for this template.
* @deprecated since 3.0, use {@link #getPageSize()}.
*/
@Deprecated
public int getFetchSize() {
return getPageSize();
}
/**
* Set the {@link CqlIdentifier keyspace} to be applied on statement-level for this template. If not set, the default
* {@link CqlSession} keyspace will be used.
*
* @param keyspace the keyspace to apply, must not be {@literal null}.
* @see SimpleStatement#setKeyspace(CqlIdentifier)
* @see BatchStatement#setKeyspace(CqlIdentifier)
* @since 3.1
*/
public void setKeyspace(CqlIdentifier keyspace) {
Assert.notNull(keyspace, "Keyspace must not be null");
this.keyspace = keyspace;
}
/**
* @return the {@link CqlIdentifier keyspace} to be applied on statement-level for this template.
* @since 3.1
*/
@Nullable
public CqlIdentifier getKeyspace() {
return this.keyspace;
}
/**
* Set the page size for this template. This is important for processing large result sets: Setting this higher than
* the default value will increase processing speed at the cost of memory consumption; setting this lower can avoid
* transferring row data that will never be read by the application. Default is -1, indicating to use the CQL driver's
* default configuration (i.e. to not pass a specific page size setting on to the driver).
*
* @see com.datastax.oss.driver.api.core.cql.SimpleStatementBuilder#setPageSize(int)
*/
public void setPageSize(int fetchSize) {
this.pageSize = fetchSize;
}
/**
* @return the page size specified for this template.
*/
public int getPageSize() {
return this.pageSize;
}
/**
* Set the serial consistency level for this template.
*
* @since 3.0
* @see Statement#setSerialConsistencyLevel(ConsistencyLevel)
* @see ConsistencyLevel
*/
public void setSerialConsistencyLevel(@Nullable ConsistencyLevel consistencyLevel) {
this.serialConsistencyLevel = consistencyLevel;
}
/**
* @return the serial {@link ConsistencyLevel} specified for this template.
* @since 3.0
*/
@Nullable
public ConsistencyLevel getSerialConsistencyLevel() {
return this.serialConsistencyLevel;
}
// -------------------------------------------------------------------------
// Methods dealing with a plain org.springframework.data.cassandra.core.cql.ReactiveSession
// -------------------------------------------------------------------------
@Override
public Flux execute(ReactiveSessionCallback action) throws DataAccessException {
Assert.notNull(action, "Callback object must not be null");
return createFlux(action).onErrorMap(translateException("ReactiveSessionCallback", () -> toCql(action)));
}
// -------------------------------------------------------------------------
// Methods dealing with static CQL
// -------------------------------------------------------------------------
@Override
public Mono execute(String cql) throws DataAccessException {
Assert.hasText(cql, "CQL must not be empty");
return queryForResultSet(cql).map(ReactiveResultSet::wasApplied);
}
@Override
public Flux query(String cql, ReactiveResultSetExtractor resultSetExtractor) throws DataAccessException {
Assert.hasText(cql, "CQL must not be empty");
Assert.notNull(resultSetExtractor, "ReactiveResultSetExtractor must not be null");
return query(SimpleStatement.newInstance(cql), resultSetExtractor);
}
@Override
public Flux query(String cql, RowMapper rowMapper) throws DataAccessException {
return query(cql, new ReactiveRowMapperResultSetExtractor<>(rowMapper));
}
@Override
public Mono queryForObject(String cql, RowMapper rowMapper) throws DataAccessException {
return query(cql, rowMapper).buffer(2).flatMap(list -> Mono.just(DataAccessUtils.requiredSingleResult(list)))
.next();
}
@Override
public Mono queryForObject(String cql, Class requiredType) throws DataAccessException {
return queryForObject(cql, getSingleColumnRowMapper(requiredType));
}
@Override
public Mono