Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exposed default time to live API on CosmosContainerProperties #6651

Merged
merged 1 commit into from
Dec 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,46 @@ public CosmosContainerProperties conflictResolutionPolicy(ConflictResolutionPoli
return this;
}

/**
* Gets the collection's default time-to-live value.
*
* @return the default time-to-live value in seconds.
*/
public Integer defaultTimeToLive() {
if (super.has(Constants.Properties.DEFAULT_TTL)) {
return super.getInt(Constants.Properties.DEFAULT_TTL);
}

return null;
}

/**
* Sets the collection's default time-to-live value.
* <p>
* The default time-to-live value on a collection is an optional property. If set, the documents within the collection
* expires after the specified number of seconds since their last write time. The value of this property should be one of the following:
* <p>
* null - indicates evaluation of time-to-live is disabled and documents within the collection will never expire, regardless whether
* individual documents have their time-to-live set.
* <p>
* nonzero positive integer - indicates the default time-to-live value for all documents within the collection. This value can be overridden
* by individual documents' time-to-live value.
* <p>
* -1 - indicates by default all documents within the collection never expire. This value can be overridden by individual documents'
* time-to-live value.
*
* @param timeToLive the default time-to-live value in seconds.
*/
public void defaultTimeToLive(Integer timeToLive) {
// a "null" value is represented as a missing element on the wire.
// setting timeToLive to null should remove the property from the property bag.
if (timeToLive != null) {
super.set(Constants.Properties.DEFAULT_TTL, timeToLive);
} else if (super.has(Constants.Properties.DEFAULT_TTL)) {
super.remove(Constants.Properties.DEFAULT_TTL);
}
}

DocumentCollection getV2Collection(){
DocumentCollection collection = new DocumentCollection(this.toJson());
collection.setPartitionKey(this.partitionKeyDefinition());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,19 @@ public void validate(CosmosContainerResponse resourceResponse) {
return this;
}

public Builder<T> withDefaultTimeToLive(Integer timeToLive) {
validators.add(new CosmosResponseValidator<CosmosContainerResponse>() {

@Override
public void validate(CosmosContainerResponse resourceResponse) {
assertThat(resourceResponse.properties()).isNotNull();
assertThat(resourceResponse.properties().defaultTimeToLive()).isNotNull();
assertThat(resourceResponse.properties().defaultTimeToLive()).isEqualTo(timeToLive);
}
});
return this;
}

public Builder<T> withProperty(String propertyName, String value) {
validators.add(new CosmosResponseValidator<T>() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,23 @@ public void createCollection(String collectionName) throws InterruptedException
safeDeleteAllCollections(database);
}

@Test(groups = { "emulator" }, timeOut = TIMEOUT, dataProvider = "collectionCrudArgProvider")
public void createCollectionWithTTL(String collectionName) throws InterruptedException {
CosmosContainerProperties collectionDefinition = getCollectionDefinition(collectionName);

Integer defaultTimeToLive = 300;
collectionDefinition.defaultTimeToLive(defaultTimeToLive);

Mono<CosmosContainerResponse> createObservable = database
.createContainer(collectionDefinition);

CosmosResponseValidator<CosmosContainerResponse> validator = new CosmosResponseValidator.Builder<CosmosContainerResponse>()
.withId(collectionDefinition.id()).withDefaultTimeToLive(defaultTimeToLive).build();

validateSuccess(createObservable, validator);
safeDeleteAllCollections(database);
}

@Test(groups = { "emulator" }, timeOut = TIMEOUT)
public void createCollectionWithCompositeIndexAndSpatialSpec() throws InterruptedException {
PartitionKeyDefinition partitionKeyDef = new PartitionKeyDefinition();
Expand Down Expand Up @@ -189,6 +206,24 @@ public void readCollection(String collectionName) throws InterruptedException {
safeDeleteAllCollections(database);
}

@Test(groups = { "emulator" }, timeOut = TIMEOUT, dataProvider = "collectionCrudArgProvider")
public void readCollectionWithTTL(String collectionName) throws InterruptedException {
CosmosContainerProperties collectionDefinition = getCollectionDefinition(collectionName);

Integer defaultTimeToLive = 200;
collectionDefinition.defaultTimeToLive(defaultTimeToLive);

Mono<CosmosContainerResponse> createObservable = database.createContainer(collectionDefinition);
CosmosContainer collection = createObservable.block().container();

Mono<CosmosContainerResponse> readObservable = collection.read();

CosmosResponseValidator<CosmosContainerResponse> validator = new CosmosResponseValidator.Builder<CosmosContainerResponse>()
.withId(collection.id()).withDefaultTimeToLive(defaultTimeToLive).build();
validateSuccess(readObservable, validator);
safeDeleteAllCollections(database);
}

@Test(groups = { "emulator" }, timeOut = TIMEOUT, dataProvider = "collectionCrudArgProvider")
public void readCollection_DoesntExist(String collectionName) throws Exception {

Expand Down Expand Up @@ -236,6 +271,33 @@ public void replaceCollection(String collectionName) throws InterruptedException
safeDeleteAllCollections(database);
}

@Test(groups = { "emulator" }, timeOut = TIMEOUT, dataProvider = "collectionCrudArgProvider")
public void replaceCollectionWithTTL(String collectionName) throws InterruptedException {
// create a collection
CosmosContainerProperties collectionDefinition = getCollectionDefinition(collectionName);
Integer defaultTimeToLive = 120;
collectionDefinition.defaultTimeToLive(defaultTimeToLive);
Mono<CosmosContainerResponse> createObservable = database.createContainer(collectionDefinition);
CosmosContainer collection = createObservable.block().container();
CosmosContainerProperties collectionSettings = collection.read().block().properties();
// sanity check
assertThat(collectionSettings.indexingPolicy().indexingMode()).isEqualTo(IndexingMode.CONSISTENT);
assertThat(collectionSettings.defaultTimeToLive()).isEqualTo(defaultTimeToLive);

// replace indexing mode
IndexingPolicy indexingMode = new IndexingPolicy();
indexingMode.indexingMode(IndexingMode.LAZY);
collectionSettings.indexingPolicy(indexingMode);
collectionSettings.defaultTimeToLive(defaultTimeToLive * 2);
Mono<CosmosContainerResponse> readObservable = collection.replace(collectionSettings, new CosmosContainerRequestOptions());

// validate
CosmosResponseValidator<CosmosContainerResponse> validator = new CosmosResponseValidator.Builder<CosmosContainerResponse>()
.indexingMode(IndexingMode.LAZY).withDefaultTimeToLive(defaultTimeToLive * 2).build();
validateSuccess(readObservable, validator);
safeDeleteAllCollections(database);
}

@Test(groups = { "emulator" }, timeOut = 10 * TIMEOUT, retryAnalyzer = RetryAnalyzer.class)
public void sessionTokenConsistencyCollectionDeleteCreateSameName() {
CosmosClient client1 = clientBuilder().build();
Expand Down