DATACOUCH-608 - Propagate expiry option and annotation to insert and upsert calls.

This commit is contained in:
mikereiche
2020-09-14 14:47:44 -07:00
parent bfa2c93720
commit 5fff02849c
10 changed files with 258 additions and 26 deletions

View File

@@ -15,6 +15,7 @@
*/
package org.springframework.data.couchbase.core;
import java.time.Duration;
import java.util.Collection;
import com.couchbase.client.core.msg.kv.DurabilityLevel;
@@ -46,6 +47,11 @@ public interface ExecutableInsertByIdOperation {
}
interface ExecutableInsertById<T> extends InsertByIdWithDurability<T> {}
interface InsertByIdWithExpiry<T> extends InsertByIdWithDurability<T> {
InsertByIdWithDurability<T> withExpiry(Duration expiry);
}
interface ExecutableInsertById<T> extends InsertByIdWithExpiry<T> {}
}

View File

@@ -15,6 +15,7 @@
*/
package org.springframework.data.couchbase.core;
import java.time.Duration;
import java.util.Collection;
import org.springframework.util.Assert;
@@ -35,7 +36,7 @@ public class ExecutableInsertByIdOperationSupport implements ExecutableInsertByI
public <T> ExecutableInsertById<T> insertById(final Class<T> domainType) {
Assert.notNull(domainType, "DomainType must not be null!");
return new ExecutableInsertByIdSupport<>(template, domainType, null, PersistTo.NONE, ReplicateTo.NONE,
DurabilityLevel.NONE);
DurabilityLevel.NONE, Duration.ofSeconds(0));
}
static class ExecutableInsertByIdSupport<T> implements ExecutableInsertById<T> {
@@ -46,18 +47,21 @@ public class ExecutableInsertByIdOperationSupport implements ExecutableInsertByI
private final PersistTo persistTo;
private final ReplicateTo replicateTo;
private final DurabilityLevel durabilityLevel;
private final Duration expiry;
private final ReactiveInsertByIdOperationSupport.ReactiveInsertByIdSupport<T> reactiveSupport;
ExecutableInsertByIdSupport(final CouchbaseTemplate template, final Class<T> domainType, final String collection,
final PersistTo persistTo, final ReplicateTo replicateTo, final DurabilityLevel durabilityLevel) {
final PersistTo persistTo, final ReplicateTo replicateTo, final DurabilityLevel durabilityLevel,
final Duration expiry) {
this.template = template;
this.domainType = domainType;
this.collection = collection;
this.persistTo = persistTo;
this.replicateTo = replicateTo;
this.durabilityLevel = durabilityLevel;
this.expiry = expiry;
this.reactiveSupport = new ReactiveInsertByIdOperationSupport.ReactiveInsertByIdSupport<>(template.reactive(),
domainType, collection, persistTo, replicateTo, durabilityLevel);
domainType, collection, persistTo, replicateTo, durabilityLevel, expiry);
}
@Override
@@ -74,14 +78,14 @@ public class ExecutableInsertByIdOperationSupport implements ExecutableInsertByI
public TerminatingInsertById<T> inCollection(final String collection) {
Assert.hasText(collection, "Collection must not be null nor empty.");
return new ExecutableInsertByIdSupport<>(template, domainType, collection, persistTo, replicateTo,
durabilityLevel);
durabilityLevel, expiry);
}
@Override
public InsertByIdWithCollection<T> withDurability(final DurabilityLevel durabilityLevel) {
Assert.notNull(durabilityLevel, "Durability Level must not be null.");
return new ExecutableInsertByIdSupport<>(template, domainType, collection, persistTo, replicateTo,
durabilityLevel);
durabilityLevel, expiry);
}
@Override
@@ -89,7 +93,14 @@ public class ExecutableInsertByIdOperationSupport implements ExecutableInsertByI
Assert.notNull(persistTo, "PersistTo must not be null.");
Assert.notNull(replicateTo, "ReplicateTo must not be null.");
return new ExecutableInsertByIdSupport<>(template, domainType, collection, persistTo, replicateTo,
durabilityLevel);
durabilityLevel, expiry);
}
@Override
public InsertByIdWithDurability<T> withExpiry(final Duration expiry) {
Assert.notNull(expiry, "expiry must not be null.");
return new ExecutableInsertByIdSupport<>(template, domainType, collection, persistTo, replicateTo,
durabilityLevel, expiry);
}
}

View File

@@ -15,6 +15,7 @@
*/
package org.springframework.data.couchbase.core;
import java.time.Duration;
import java.util.Collection;
import com.couchbase.client.core.msg.kv.DurabilityLevel;
@@ -46,6 +47,11 @@ public interface ExecutableUpsertByIdOperation {
}
interface ExecutableUpsertById<T> extends UpsertByIdWithDurability<T> {}
interface UpsertByIdWithExpiry<T> extends UpsertByIdWithDurability<T> {
UpsertByIdWithDurability<T> withExpiry(Duration expiry);
}
interface ExecutableUpsertById<T> extends UpsertByIdWithExpiry<T> {}
}

View File

@@ -15,6 +15,7 @@
*/
package org.springframework.data.couchbase.core;
import java.time.Duration;
import java.util.Collection;
import org.springframework.util.Assert;
@@ -35,7 +36,7 @@ public class ExecutableUpsertByIdOperationSupport implements ExecutableUpsertByI
public <T> ExecutableUpsertById<T> upsertById(final Class<T> domainType) {
Assert.notNull(domainType, "DomainType must not be null!");
return new ExecutableUpsertByIdSupport<>(template, domainType, null, PersistTo.NONE, ReplicateTo.NONE,
DurabilityLevel.NONE);
DurabilityLevel.NONE, Duration.ofSeconds(0));
}
static class ExecutableUpsertByIdSupport<T> implements ExecutableUpsertById<T> {
@@ -46,18 +47,21 @@ public class ExecutableUpsertByIdOperationSupport implements ExecutableUpsertByI
private final PersistTo persistTo;
private final ReplicateTo replicateTo;
private final DurabilityLevel durabilityLevel;
private final Duration expiry;
private final ReactiveUpsertByIdOperationSupport.ReactiveUpsertByIdSupport<T> reactiveSupport;
ExecutableUpsertByIdSupport(final CouchbaseTemplate template, final Class<T> domainType, final String collection,
final PersistTo persistTo, final ReplicateTo replicateTo, final DurabilityLevel durabilityLevel) {
final PersistTo persistTo, final ReplicateTo replicateTo, final DurabilityLevel durabilityLevel,
final Duration expiry) {
this.template = template;
this.domainType = domainType;
this.collection = collection;
this.persistTo = persistTo;
this.replicateTo = replicateTo;
this.durabilityLevel = durabilityLevel;
this.expiry = expiry;
this.reactiveSupport = new ReactiveUpsertByIdOperationSupport.ReactiveUpsertByIdSupport<>(template.reactive(),
domainType, collection, persistTo, replicateTo, durabilityLevel);
domainType, collection, persistTo, replicateTo, durabilityLevel, expiry);
}
@Override
@@ -74,14 +78,14 @@ public class ExecutableUpsertByIdOperationSupport implements ExecutableUpsertByI
public TerminatingUpsertById<T> inCollection(final String collection) {
Assert.hasText(collection, "Collection must not be null nor empty.");
return new ExecutableUpsertByIdSupport<>(template, domainType, collection, persistTo, replicateTo,
durabilityLevel);
durabilityLevel, expiry);
}
@Override
public UpsertByIdWithCollection<T> withDurability(final DurabilityLevel durabilityLevel) {
Assert.notNull(durabilityLevel, "Durability Level must not be null.");
return new ExecutableUpsertByIdSupport<>(template, domainType, collection, persistTo, replicateTo,
durabilityLevel);
durabilityLevel, expiry);
}
@Override
@@ -89,7 +93,14 @@ public class ExecutableUpsertByIdOperationSupport implements ExecutableUpsertByI
Assert.notNull(persistTo, "PersistTo must not be null.");
Assert.notNull(replicateTo, "ReplicateTo must not be null.");
return new ExecutableUpsertByIdSupport<>(template, domainType, collection, persistTo, replicateTo,
durabilityLevel);
durabilityLevel, expiry);
}
@Override
public UpsertByIdWithDurability<T> withExpiry(final Duration expiry) {
Assert.notNull(expiry, "expiry must not be null.");
return new ExecutableUpsertByIdSupport<>(template, domainType, collection, persistTo, replicateTo,
durabilityLevel, expiry);
}
}

View File

@@ -18,6 +18,7 @@ package org.springframework.data.couchbase.core;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import java.time.Duration;
import java.util.Collection;
import com.couchbase.client.core.msg.kv.DurabilityLevel;
@@ -49,6 +50,11 @@ public interface ReactiveInsertByIdOperation {
}
interface ReactiveInsertById<T> extends InsertByIdWithDurability<T> {}
interface InsertByIdWithExpiry<T> extends InsertByIdWithDurability<T> {
InsertByIdWithDurability<T> withExpiry(Duration expiry);
}
interface ReactiveInsertById<T> extends InsertByIdWithExpiry<T> {}
}

View File

@@ -15,9 +15,12 @@
*/
package org.springframework.data.couchbase.core;
import com.couchbase.client.java.kv.UpsertOptions;
import org.springframework.data.couchbase.core.mapping.Document;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import java.time.Duration;
import java.util.Collection;
import org.springframework.data.couchbase.core.mapping.CouchbaseDocument;
@@ -40,7 +43,7 @@ public class ReactiveInsertByIdOperationSupport implements ReactiveInsertByIdOpe
public <T> ReactiveInsertById<T> insertById(final Class<T> domainType) {
Assert.notNull(domainType, "DomainType must not be null!");
return new ReactiveInsertByIdSupport<>(template, domainType, null, PersistTo.NONE, ReplicateTo.NONE,
DurabilityLevel.NONE);
DurabilityLevel.NONE, Duration.ofSeconds(0));
}
static class ReactiveInsertByIdSupport<T> implements ReactiveInsertById<T> {
@@ -51,16 +54,18 @@ public class ReactiveInsertByIdOperationSupport implements ReactiveInsertByIdOpe
private final PersistTo persistTo;
private final ReplicateTo replicateTo;
private final DurabilityLevel durabilityLevel;
private final Duration expiry;
ReactiveInsertByIdSupport(final ReactiveCouchbaseTemplate template, final Class<T> domainType,
final String collection, final PersistTo persistTo, final ReplicateTo replicateTo,
final DurabilityLevel durabilityLevel) {
final DurabilityLevel durabilityLevel, Duration expiry) {
this.template = template;
this.domainType = domainType;
this.collection = collection;
this.persistTo = persistTo;
this.replicateTo = replicateTo;
this.durabilityLevel = durabilityLevel;
this.expiry = expiry;
}
@Override
@@ -93,28 +98,40 @@ public class ReactiveInsertByIdOperationSupport implements ReactiveInsertByIdOpe
} else if (durabilityLevel != DurabilityLevel.NONE) {
options.durability(durabilityLevel);
}
if (expiry != null) {
options.expiry(expiry);
} else if (domainType.isAnnotationPresent(Document.class)) {
Document documentAnn = domainType.getAnnotation(Document.class);
long durationSeconds = documentAnn.expiryUnit().toSeconds(documentAnn.expiry());
options.expiry(Duration.ofSeconds(durationSeconds));
}
return options;
}
@Override
public TerminatingInsertById<T> inCollection(final String collection) {
Assert.hasText(collection, "Collection must not be null nor empty.");
return new ReactiveInsertByIdSupport<>(template, domainType, collection, persistTo, replicateTo, durabilityLevel);
return new ReactiveInsertByIdSupport<>(template, domainType, collection, persistTo, replicateTo, durabilityLevel, expiry);
}
@Override
public InsertByIdWithCollection<T> withDurability(final DurabilityLevel durabilityLevel) {
Assert.notNull(durabilityLevel, "Durability Level must not be null.");
return new ReactiveInsertByIdSupport<>(template, domainType, collection, persistTo, replicateTo, durabilityLevel);
return new ReactiveInsertByIdSupport<>(template, domainType, collection, persistTo, replicateTo, durabilityLevel, expiry);
}
@Override
public InsertByIdWithCollection<T> withDurability(final PersistTo persistTo, final ReplicateTo replicateTo) {
Assert.notNull(persistTo, "PersistTo must not be null.");
Assert.notNull(replicateTo, "ReplicateTo must not be null.");
return new ReactiveInsertByIdSupport<>(template, domainType, collection, persistTo, replicateTo, durabilityLevel);
return new ReactiveInsertByIdSupport<>(template, domainType, collection, persistTo, replicateTo, durabilityLevel, expiry);
}
@Override
public InsertByIdWithDurability<T> withExpiry(final Duration expiry) {
Assert.notNull(expiry, "expiry must not be null.");
return new ReactiveInsertByIdSupport<>(template, domainType, collection, persistTo, replicateTo, durabilityLevel, expiry);
}
}
}

View File

@@ -18,6 +18,7 @@ package org.springframework.data.couchbase.core;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import java.time.Duration;
import java.util.Collection;
import com.couchbase.client.core.msg.kv.DurabilityLevel;
@@ -49,6 +50,11 @@ public interface ReactiveUpsertByIdOperation {
}
interface ReactiveUpsertById<T> extends UpsertByIdWithDurability<T> {}
interface UpsertByIdWithExpiry<T> extends UpsertByIdWithDurability<T> {
UpsertByIdWithDurability<T> withExpiry(Duration expiry);
}
interface ReactiveUpsertById<T> extends UpsertByIdWithExpiry<T> {}
}

View File

@@ -15,9 +15,12 @@
*/
package org.springframework.data.couchbase.core;
import com.couchbase.client.java.kv.Expiry;
import org.springframework.data.couchbase.core.mapping.Document;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import java.time.Duration;
import java.util.Collection;
import org.springframework.data.couchbase.core.mapping.CouchbaseDocument;
@@ -40,7 +43,7 @@ public class ReactiveUpsertByIdOperationSupport implements ReactiveUpsertByIdOpe
public <T> ReactiveUpsertById<T> upsertById(final Class<T> domainType) {
Assert.notNull(domainType, "DomainType must not be null!");
return new ReactiveUpsertByIdSupport<>(template, domainType, null, PersistTo.NONE, ReplicateTo.NONE,
DurabilityLevel.NONE);
DurabilityLevel.NONE, Duration.ofSeconds(0));
}
static class ReactiveUpsertByIdSupport<T> implements ReactiveUpsertById<T> {
@@ -51,16 +54,18 @@ public class ReactiveUpsertByIdOperationSupport implements ReactiveUpsertByIdOpe
private final PersistTo persistTo;
private final ReplicateTo replicateTo;
private final DurabilityLevel durabilityLevel;
private final Duration expiry;
ReactiveUpsertByIdSupport(final ReactiveCouchbaseTemplate template, final Class<T> domainType,
final String collection, final PersistTo persistTo, final ReplicateTo replicateTo,
final DurabilityLevel durabilityLevel) {
final DurabilityLevel durabilityLevel, final Duration expiry) {
this.template = template;
this.domainType = domainType;
this.collection = collection;
this.persistTo = persistTo;
this.replicateTo = replicateTo;
this.durabilityLevel = durabilityLevel;
this.expiry = expiry;
}
@Override
@@ -93,28 +98,44 @@ public class ReactiveUpsertByIdOperationSupport implements ReactiveUpsertByIdOpe
} else if (durabilityLevel != DurabilityLevel.NONE) {
options.durability(durabilityLevel);
}
if (expiry != null) {
options.expiry(expiry);
} else if (domainType.isAnnotationPresent(Document.class)) {
Document documentAnn = domainType.getAnnotation(Document.class);
long durationSeconds = documentAnn.expiryUnit().toSeconds(documentAnn.expiry());
options.expiry(Duration.ofSeconds(durationSeconds));
}
return options;
}
@Override
public TerminatingUpsertById<T> inCollection(final String collection) {
Assert.hasText(collection, "Collection must not be null nor empty.");
return new ReactiveUpsertByIdSupport<>(template, domainType, collection, persistTo, replicateTo, durabilityLevel);
return new ReactiveUpsertByIdSupport<>(template, domainType, collection, persistTo, replicateTo, durabilityLevel,
expiry);
}
@Override
public UpsertByIdWithCollection<T> withDurability(final DurabilityLevel durabilityLevel) {
Assert.notNull(durabilityLevel, "Durability Level must not be null.");
return new ReactiveUpsertByIdSupport<>(template, domainType, collection, persistTo, replicateTo, durabilityLevel);
return new ReactiveUpsertByIdSupport<>(template, domainType, collection, persistTo, replicateTo, durabilityLevel,
expiry);
}
@Override
public UpsertByIdWithCollection<T> withDurability(final PersistTo persistTo, final ReplicateTo replicateTo) {
Assert.notNull(persistTo, "PersistTo must not be null.");
Assert.notNull(replicateTo, "ReplicateTo must not be null.");
return new ReactiveUpsertByIdSupport<>(template, domainType, collection, persistTo, replicateTo, durabilityLevel);
return new ReactiveUpsertByIdSupport<>(template, domainType, collection, persistTo, replicateTo, durabilityLevel,
expiry);
}
@Override
public UpsertByIdWithDurability<T> withExpiry(final Duration expiry) {
Assert.notNull(expiry, "expiry must not be null.");
return new ReactiveUpsertByIdSupport<>(template, domainType, collection, persistTo, replicateTo, durabilityLevel,
expiry);
}
}
}

View File

@@ -23,6 +23,9 @@ import java.io.IOException;
import java.time.Duration;
import java.util.UUID;
import com.couchbase.client.core.error.DocumentNotFoundException;
import com.couchbase.client.java.kv.PersistTo;
import com.couchbase.client.java.kv.ReplicateTo;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
@@ -35,6 +38,7 @@ import org.springframework.data.couchbase.CouchbaseClientFactory;
import org.springframework.data.couchbase.SimpleCouchbaseClientFactory;
import org.springframework.data.couchbase.domain.Config;
import org.springframework.data.couchbase.domain.User;
import org.springframework.data.couchbase.domain.UserAnnotated;
import org.springframework.data.couchbase.util.ClusterAwareIntegrationTests;
import org.springframework.data.couchbase.util.ClusterType;
import org.springframework.data.couchbase.util.IgnoreWhen;
@@ -80,6 +84,53 @@ class CouchbaseTemplateKeyValueIntegrationTests extends ClusterAwareIntegrationT
couchbaseTemplate.removeById().one(user.getId());
}
@Test
void upsertWithDurability() {
User user = new User(UUID.randomUUID().toString(), "firstname", "lastname");
User modified = couchbaseTemplate.upsertById(User.class).withDurability(PersistTo.ACTIVE, ReplicateTo.NONE)
.one(user);
assertEquals(user, modified);
User found = couchbaseTemplate.findById(User.class).one(user.getId());
assertEquals(user, found);
couchbaseTemplate.removeById().one(user.getId());
}
@Test
void upsertWithExpiry() {
User user = new User(UUID.randomUUID().toString(), "firstname", "lastname");
try {
User modified = couchbaseTemplate.upsertById(User.class).withExpiry(Duration.ofSeconds(1)).one(user);
assertEquals(user, modified);
sleepSecs(2);
User found = couchbaseTemplate.findById(User.class).one(user.getId());
assertNull(found, "found should have been null as document should be expired");
} finally {
try {
couchbaseTemplate.removeById().one(user.getId());
} catch (DataRetrievalFailureException e) {
//
}
}
}
@Test
void upsertWithExpiryAnnotation() {
UserAnnotated user = new UserAnnotated(UUID.randomUUID().toString(), "firstname", "lastname");
try {
UserAnnotated modified = couchbaseTemplate.upsertById(UserAnnotated.class).one(user);
assertEquals(user, modified);
sleepSecs(6);
User found = couchbaseTemplate.findById(User.class).one(user.getId());
assertNull(found, "found should have been null as document should be expired");
} finally {
try {
couchbaseTemplate.removeById().one(user.getId());
} catch (DataRetrievalFailureException e) {
//
}
}
}
@Test
void findDocWhichDoesNotExist() {
assertNull(couchbaseTemplate.findById(User.class).one(UUID.randomUUID().toString()));
@@ -126,6 +177,55 @@ class CouchbaseTemplateKeyValueIntegrationTests extends ClusterAwareIntegrationT
}
@Test
void insertByIdwithDurability() {
User user = new User(UUID.randomUUID().toString(), "firstname", "lastname");
User inserted = couchbaseTemplate.insertById(User.class).withDurability(PersistTo.ACTIVE, ReplicateTo.NONE)
.one(user);
assertEquals(user, inserted);
assertThrows(DuplicateKeyException.class, () -> couchbaseTemplate.insertById(User.class).one(user));
couchbaseTemplate.removeById().one(user.getId());
}
@Test
void insertByIdwithExpiry() {
User user = new User(UUID.randomUUID().toString(), "firstname", "lastname");
try {
User inserted = couchbaseTemplate.insertById(User.class).withExpiry(Duration.ofSeconds(1)).one(user);
assertEquals(user, inserted);
sleepSecs(2);
User found = couchbaseTemplate.findById(User.class).one(user.getId());
assertNull(found, "found should have been null as document should be expired");
} finally {
try {
couchbaseTemplate.removeById().one(user.getId());
} catch (DataRetrievalFailureException e) {
// ignore
}
}
}
@Test
void insertWithExpiryAnnotation() {
UserAnnotated user = new UserAnnotated(UUID.randomUUID().toString(), "firstname", "lastname");
try {
UserAnnotated inserted = couchbaseTemplate.insertById(UserAnnotated.class).one(user);
assertEquals(user, inserted);
sleepSecs(6);
User found = couchbaseTemplate.findById(User.class).one(user.getId());
assertNull(found, "found should have been null as document should be expired");
} finally {
try {
couchbaseTemplate.removeById().one(user.getId());
} catch (DataRetrievalFailureException e) {
// ignore
}
}
}
@Test
void existsById() {
String id = UUID.randomUUID().toString();
@@ -140,4 +240,10 @@ class CouchbaseTemplateKeyValueIntegrationTests extends ClusterAwareIntegrationT
}
private void sleepSecs(int i) {
try {
Thread.sleep(i * 1000);
} catch (InterruptedException ie) {}
}
}

View File

@@ -0,0 +1,42 @@
/*
* Copyright 2020 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.couchbase.domain;
import java.util.Objects;
import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.LastModifiedBy;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.annotation.PersistenceConstructor;
import org.springframework.data.annotation.Version;
import org.springframework.data.couchbase.core.mapping.Document;
/**
* Annoted User entity for tests
*
* @author Michael Reiche
*/
@Document(expiry = 5)
public class UserAnnotated extends User {
public UserAnnotated(String id, String firstname, String lastname) {
super(id, firstname, lastname);
}
}