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:
Родитель
e72ce3ba88
Коммит
f7e06b79d4
|
@ -254,7 +254,7 @@ public class CosmosTemplate implements CosmosOperations, ApplicationContextAware
|
|||
.flatMap(cosmosDatabaseResponse -> cosmosDatabaseResponse
|
||||
.database()
|
||||
.createContainerIfNotExists(information.getCollectionName(),
|
||||
"/" + information.getPartitionKeyFieldName())
|
||||
"/" + information.getPartitionKeyFieldName(), information.getRequestUnit())
|
||||
.map(cosmosContainerResponse -> cosmosContainerResponse))
|
||||
.block();
|
||||
if (response == null) {
|
||||
|
|
|
@ -90,7 +90,7 @@ public class ReactiveCosmosTemplate implements ReactiveCosmosOperations, Applica
|
|||
.flatMap(cosmosDatabaseResponse -> cosmosDatabaseResponse
|
||||
.database()
|
||||
.createContainerIfNotExists(information.getCollectionName(),
|
||||
"/" + information.getPartitionKeyFieldName())
|
||||
"/" + information.getPartitionKeyFieldName(), information.getRequestUnit())
|
||||
.map(cosmosContainerResponse -> {
|
||||
this.collectionCache.add(information.getCollectionName());
|
||||
return cosmosContainerResponse;
|
||||
|
|
|
@ -20,11 +20,19 @@ public interface CosmosRepository<T, ID extends Serializable> extends PagingAndS
|
|||
* Retrieves an entity by its id.
|
||||
*
|
||||
* @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
|
||||
* @throws IllegalArgumentException if {@code id} is {@literal null}.
|
||||
*/
|
||||
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.
|
||||
* @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.
|
||||
* @throws IllegalArgumentException in case the given {@code id} is {@literal null}.
|
||||
*/
|
||||
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);
|
||||
}
|
||||
|
||||
@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
|
||||
*
|
||||
|
|
|
@ -168,6 +168,15 @@ public class SimpleReactiveCosmosRepository<T, K extends Serializable> implement
|
|||
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
|
||||
public Mono<Void> delete(@NonNull T entity) {
|
||||
Assert.notNull(entity, "entity to be deleted must not be null!");
|
||||
|
|
|
@ -39,6 +39,6 @@ public class PageTestUtils {
|
|||
|
||||
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.core.CosmosTemplate;
|
||||
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.repository.AddressRepository;
|
||||
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());
|
||||
}
|
||||
|
||||
@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
|
||||
public void testUpdateEntity() {
|
||||
final Address updatedAddress = new Address(TEST_ADDRESS1_PARTITION1.getPostalCode(), TestConstants.NEW_STREET,
|
||||
|
|
Загрузка…
Ссылка в новой задаче