Added Delete by Id and partition key API (#437)

* Added Delete by Id and partition key API

* Added request units to collection create implementation.
Updated composite token to be used when checking last page
This commit is contained in:
Kushagra Thapar 2019-10-09 11:35:21 -05:00 коммит произвёл GitHub
Родитель e72ce3ba88
Коммит f7e06b79d4
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
8 изменённых файлов: 59 добавлений и 5 удалений

Просмотреть файл

@ -254,7 +254,7 @@ public class CosmosTemplate implements CosmosOperations, ApplicationContextAware
.flatMap(cosmosDatabaseResponse -> cosmosDatabaseResponse .flatMap(cosmosDatabaseResponse -> cosmosDatabaseResponse
.database() .database()
.createContainerIfNotExists(information.getCollectionName(), .createContainerIfNotExists(information.getCollectionName(),
"/" + information.getPartitionKeyFieldName()) "/" + information.getPartitionKeyFieldName(), information.getRequestUnit())
.map(cosmosContainerResponse -> cosmosContainerResponse)) .map(cosmosContainerResponse -> cosmosContainerResponse))
.block(); .block();
if (response == null) { if (response == null) {

Просмотреть файл

@ -90,7 +90,7 @@ public class ReactiveCosmosTemplate implements ReactiveCosmosOperations, Applica
.flatMap(cosmosDatabaseResponse -> cosmosDatabaseResponse .flatMap(cosmosDatabaseResponse -> cosmosDatabaseResponse
.database() .database()
.createContainerIfNotExists(information.getCollectionName(), .createContainerIfNotExists(information.getCollectionName(),
"/" + information.getPartitionKeyFieldName()) "/" + information.getPartitionKeyFieldName(), information.getRequestUnit())
.map(cosmosContainerResponse -> { .map(cosmosContainerResponse -> {
this.collectionCache.add(information.getCollectionName()); this.collectionCache.add(information.getCollectionName());
return cosmosContainerResponse; return cosmosContainerResponse;

Просмотреть файл

@ -20,11 +20,19 @@ public interface CosmosRepository<T, ID extends Serializable> extends PagingAndS
* Retrieves an entity by its id. * Retrieves an entity by its id.
* *
* @param id must not be {@literal null}. * @param id must not be {@literal null}.
* @param partitionKey partition key value of entity * @param partitionKey partition key value of entity, must not be null.
* @return the entity with the given id or {@literal Optional#empty()} if none found * @return the entity with the given id or {@literal Optional#empty()} if none found
* @throws IllegalArgumentException if {@code id} is {@literal null}. * @throws IllegalArgumentException if {@code id} is {@literal null}.
*/ */
Optional<T> findById(ID id, PartitionKey partitionKey); Optional<T> findById(ID id, PartitionKey partitionKey);
/**
* Deletes an entity by its id and partition key.
* @param id must not be {@literal null}.
* @param partitionKey partition key value of the entity, must not be null.
* @throws IllegalArgumentException in case the given {@code id} is {@literal null}.
*/
void deleteById(ID id, PartitionKey partitionKey);
} }

Просмотреть файл

@ -16,9 +16,18 @@ public interface ReactiveCosmosRepository<T, K> extends ReactiveSortingRepositor
/** /**
* Retrieves an entity by its id and partition key. * Retrieves an entity by its id and partition key.
* @param id must not be {@literal null}. * @param id must not be {@literal null}.
* @param partitionKey partition key value of the entity. * @param partitionKey partition key value of the entity, must not be null.
* @return {@link Mono} emitting the entity with the given id or {@link Mono#empty()} if none found. * @return {@link Mono} emitting the entity with the given id or {@link Mono#empty()} if none found.
* @throws IllegalArgumentException in case the given {@code id} is {@literal null}. * @throws IllegalArgumentException in case the given {@code id} is {@literal null}.
*/ */
Mono<T> findById(K id, PartitionKey partitionKey); Mono<T> findById(K id, PartitionKey partitionKey);
/**
* Deletes an entity by its id and partition key.
* @param id must not be {@literal null}.
* @param partitionKey partition key value of the entity, must not be null.
* @return {@link Mono} emitting the void Mono.
* @throws IllegalArgumentException in case the given {@code id} is {@literal null}.
*/
Mono<Void> deleteById(K id, PartitionKey partitionKey);
} }

Просмотреть файл

@ -172,6 +172,14 @@ public class SimpleCosmosRepository<T, ID extends Serializable> implements Cosmo
operation.deleteById(information.getCollectionName(), id, null); operation.deleteById(information.getCollectionName(), id, null);
} }
@Override
public void deleteById(ID id, PartitionKey partitionKey) {
Assert.notNull(id, "id to be deleted should not be null");
Assert.notNull(partitionKey, "partitionKey to be deleted should not be null");
operation.deleteById(information.getCollectionName(), id, partitionKey);
}
/** /**
* delete one document per entity * delete one document per entity
* *

Просмотреть файл

@ -168,6 +168,15 @@ public class SimpleReactiveCosmosRepository<T, K extends Serializable> implement
id, null)).then(); id, null)).then();
} }
@Override
public Mono<Void> deleteById(K id, PartitionKey partitionKey) {
Assert.notNull(id, "Id must not be null!");
Assert.notNull(partitionKey, "PartitionKey must not be null!");
return cosmosOperations.deleteById(entityInformation.getCollectionName(), id, partitionKey);
}
@Override @Override
public Mono<Void> delete(@NonNull T entity) { public Mono<Void> delete(@NonNull T entity) {
Assert.notNull(entity, "entity to be deleted must not be null!"); Assert.notNull(entity, "entity to be deleted must not be null!");

Просмотреть файл

@ -39,6 +39,6 @@ public class PageTestUtils {
final JSONObject jsonObject = new JSONObject(tokenJson); final JSONObject jsonObject = new JSONObject(tokenJson);
return jsonObject.isNull("token"); return jsonObject.isNull("compositeToken");
} }
} }

Просмотреть файл

@ -10,6 +10,7 @@ import com.microsoft.azure.spring.data.cosmosdb.common.TestConstants;
import com.microsoft.azure.spring.data.cosmosdb.common.TestUtils; import com.microsoft.azure.spring.data.cosmosdb.common.TestUtils;
import com.microsoft.azure.spring.data.cosmosdb.core.CosmosTemplate; import com.microsoft.azure.spring.data.cosmosdb.core.CosmosTemplate;
import com.microsoft.azure.spring.data.cosmosdb.domain.Address; import com.microsoft.azure.spring.data.cosmosdb.domain.Address;
import com.microsoft.azure.spring.data.cosmosdb.exception.CosmosDBAccessException;
import com.microsoft.azure.spring.data.cosmosdb.repository.TestRepositoryConfig; import com.microsoft.azure.spring.data.cosmosdb.repository.TestRepositoryConfig;
import com.microsoft.azure.spring.data.cosmosdb.repository.repository.AddressRepository; import com.microsoft.azure.spring.data.cosmosdb.repository.repository.AddressRepository;
import com.microsoft.azure.spring.data.cosmosdb.repository.support.CosmosEntityInformation; import com.microsoft.azure.spring.data.cosmosdb.repository.support.CosmosEntityInformation;
@ -165,6 +166,25 @@ public class AddressRepositoryIT {
assertThat(result.get(0).getCity()).isNotEqualTo(TEST_ADDRESS1_PARTITION1.getCity()); assertThat(result.get(0).getCity()).isNotEqualTo(TEST_ADDRESS1_PARTITION1.getCity());
} }
@Test(expected = CosmosDBAccessException.class)
public void testDeleteByIdAndPartitionKey() {
final long count = repository.count();
assertThat(count).isEqualTo(4);
final Optional<Address> addressById = repository.findById(TEST_ADDRESS1_PARTITION1.getPostalCode(),
new PartitionKey(TEST_ADDRESS1_PARTITION1.getCity()));
assertThat(addressById.isPresent()).isTrue();
repository.deleteById(TEST_ADDRESS1_PARTITION1.getPostalCode(),
new PartitionKey(TEST_ADDRESS1_PARTITION1.getCity()));
final List<Address> result = TestUtils.toList(repository.findAll());
assertThat(result.size()).isEqualTo(3);
repository.findById(TEST_ADDRESS1_PARTITION1.getPostalCode(),
new PartitionKey(TEST_ADDRESS1_PARTITION1.getCity()));
}
@Test @Test
public void testUpdateEntity() { public void testUpdateEntity() {
final Address updatedAddress = new Address(TEST_ADDRESS1_PARTITION1.getPostalCode(), TestConstants.NEW_STREET, final Address updatedAddress = new Address(TEST_ADDRESS1_PARTITION1.getPostalCode(), TestConstants.NEW_STREET,