All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.springframework.data.cassandra.repository.support.SimpleCassandraRepository Maven / Gradle / Ivy

There is a newer version: 4.3.4
Show newest version
/*
 * Copyright 2013-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.repository.support;

import static org.springframework.data.cassandra.core.query.Criteria.*;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

import org.springframework.data.cassandra.core.CassandraOperations;
import org.springframework.data.cassandra.core.CassandraTemplate;
import org.springframework.data.cassandra.core.InsertOptions;
import org.springframework.data.cassandra.core.mapping.BasicCassandraPersistentEntity;
import org.springframework.data.cassandra.core.mapping.CassandraPersistentProperty;
import org.springframework.data.cassandra.core.query.Query;
import org.springframework.data.cassandra.repository.CassandraRepository;
import org.springframework.data.cassandra.repository.query.CassandraEntityInformation;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import org.springframework.data.mapping.context.AbstractMappingContext;
import org.springframework.util.Assert;

/**
 * Repository base implementation for Cassandra.
 *
 * @author Alex Shvid
 * @author Matthew T. Adams
 * @author Mark Paluch
 * @author John Blum
 * @author Jens Schauder
 * @see org.springframework.data.cassandra.repository.CassandraRepository
 */
public class SimpleCassandraRepository implements CassandraRepository {

	private static final InsertOptions INSERT_NULLS = InsertOptions.builder().withInsertNulls().build();

	private final AbstractMappingContext, CassandraPersistentProperty> mappingContext;

	private final CassandraEntityInformation entityInformation;

	private final CassandraOperations operations;

	/**
	 * Create a new {@link SimpleCassandraRepository} for the given {@link CassandraEntityInformation} and
	 * {@link CassandraTemplate}.
	 *
	 * @param metadata must not be {@literal null}.
	 * @param operations must not be {@literal null}.
	 */
	public SimpleCassandraRepository(CassandraEntityInformation metadata, CassandraOperations operations) {

		Assert.notNull(metadata, "CassandraEntityInformation must not be null");
		Assert.notNull(operations, "CassandraOperations must not be null");

		this.entityInformation = metadata;
		this.operations = operations;
		this.mappingContext = operations.getConverter().getMappingContext();
	}

	// -------------------------------------------------------------------------
	// Methods from CrudRepository
	// -------------------------------------------------------------------------

	@Override
	public  S save(S entity) {

		Assert.notNull(entity, "Entity must not be null");

		BasicCassandraPersistentEntity persistentEntity = this.mappingContext.getPersistentEntity(entity.getClass());

		if (persistentEntity != null && persistentEntity.hasVersionProperty()) {

			if (!entityInformation.isNew(entity)) {
				return this.operations.update(entity);
			}
		}

		return this.operations.insert(entity, INSERT_NULLS).getEntity();
	}

	@Override
	public  List saveAll(Iterable entities) {

		Assert.notNull(entities, "The given Iterable of entities must not be null");

		List result = new ArrayList<>();

		for (S entity : entities) {
			result.add(save(entity));
		}

		return result;
	}

	@Override
	public Optional findById(ID id) {

		Assert.notNull(id, "The given id must not be null");

		return Optional.ofNullable(doFindOne(id));
	}

	private T doFindOne(ID id) {
		return this.operations.selectOneById(id, this.entityInformation.getJavaType());
	}

	@Override
	public boolean existsById(ID id) {

		Assert.notNull(id, "The given id must not be null");

		return this.operations.exists(id, this.entityInformation.getJavaType());
	}

	@Override
	public List findAll() {
		return this.operations.select(Query.empty(), this.entityInformation.getJavaType());
	}

	@Override
	public List findAllById(Iterable ids) {

		Assert.notNull(ids, "The given Iterable of id's must not be null");

		if (!ids.iterator().hasNext()) {
			return Collections.emptyList();
		}

		return this.operations.select(createIdsInQuery(ids), this.entityInformation.getJavaType());
	}

	@Override
	public long count() {
		return this.operations.count(this.entityInformation.getJavaType());
	}

	@Override
	public void deleteById(ID id) {

		Assert.notNull(id, "The given id must not be null");

		this.operations.deleteById(id, this.entityInformation.getJavaType());
	}

	@Override
	public void delete(T entity) {

		Assert.notNull(entity, "The given entity must not be null");

		deleteById(this.entityInformation.getRequiredId(entity));
	}

	@Override
	public void deleteAllById(Iterable ids) {

		Assert.notNull(ids, "The given Iterable of ids must not be null");

		if (FindByIdQuery.hasCompositeKeys(ids, this.mappingContext)) {

			for (ID id : ids) {
				deleteById(id);
			}
			return;
		}

		this.operations.delete(createIdsInQuery(ids), this.entityInformation.getJavaType());
	}

	@Override
	public void deleteAll(Iterable entities) {

		Assert.notNull(entities, "The given Iterable of entities must not be null");

		entities.forEach(this.operations::delete);
	}

	@Override
	public void deleteAll() {
		this.operations.truncate(this.entityInformation.getJavaType());
	}

	// -------------------------------------------------------------------------
	// Methods from CassandraRepository
	// -------------------------------------------------------------------------

	@Override
	public Slice findAll(Pageable pageable) {

		Assert.notNull(pageable, "Pageable must not be null");

		return this.operations.slice(Query.empty().pageRequest(pageable), this.entityInformation.getJavaType());
	}

	@Override
	public  S insert(S entity) {

		Assert.notNull(entity, "Entity must not be null");

		return this.operations.insert(entity);
	}

	@Override
	public  List insert(Iterable entities) {

		Assert.notNull(entities, "The given Iterable of entities must not be null");

		List result = new ArrayList<>();

		for (S entity : entities) {
			result.add(this.operations.insert(entity));
		}

		return result;
	}

	private Query createIdsInQuery(Iterable ids) {

		FindByIdQuery mapIdQuery = FindByIdQuery.forIds(ids);
		List idCollection = mapIdQuery.getIdCollection();
		String idField = mapIdQuery.getIdProperty();

		if (idField == null) {
			idField = this.entityInformation.getIdAttribute();
		}

		return Query.query(where(idField).in(idCollection));
	}

}