Changes for PR 1223 that I had missed. (#1230)

Closes #1229.
This commit is contained in:
Michael Reiche
2021-09-14 16:38:11 -07:00
committed by GitHub
parent b9fa1c76ab
commit 66cc1b5491
4 changed files with 15 additions and 51 deletions

View File

@@ -167,10 +167,10 @@ public class ReactiveFindByIdOperationSupport implements ReactiveFindByIdOperati
if (expiryToUse == null) { // GetAndTouchOptions without specifying expiry -> get expiry from annoation
final CouchbasePersistentEntity<?> entity = template.getConverter().getMappingContext()
.getRequiredPersistentEntity(domainType);
expiryToUse = Duration.ofSeconds(entity.getExpiry());
expiryToUse = entity.getExpiryDuration();
}
}
return expiry;
return expiryToUse;
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors
* Copyright 2012-2021 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.
@@ -17,7 +17,6 @@
package org.springframework.data.couchbase.core.mapping;
import java.time.Duration;
import java.time.Instant;
import java.util.Calendar;
import java.util.TimeZone;
import java.util.concurrent.TimeUnit;
@@ -98,10 +97,12 @@ public class BasicCouchbasePersistentEntity<T> extends BasicPersistentEntity<T,
}
@Override
@Deprecated
public int getExpiry() {
return getExpiry(AnnotatedElementUtils.findMergedAnnotation(getType(), Expiry.class), environment);
}
@Deprecated
public static int getExpiry(Expiry annotation, Environment environment) {
if (annotation == null) {
return 0;
@@ -133,40 +134,13 @@ public class BasicCouchbasePersistentEntity<T> extends BasicPersistentEntity<T,
private static Duration getExpiryDuration(Expiry annotation, Environment environment) {
if (annotation == null) {
return Duration.ofSeconds(0);
return Duration.ZERO;
}
int expiryValue = getExpiryValue(annotation, environment);
long secondsShift = annotation.expiryUnit().toSeconds(expiryValue);
return Duration.ofSeconds(secondsShift);
}
@Override
public Instant getExpiryInstant() {
return getExpiryInstant(AnnotatedElementUtils.findMergedAnnotation(getType(), Expiry.class), environment);
}
private static Instant getExpiryInstant(Expiry annotation, Environment environment) {
if (annotation == null) {
return Instant.ofEpochSecond(0);
}
int expiryValue = getExpiryValue(annotation, environment);
long secondsShift = annotation.expiryUnit().toSeconds(expiryValue);
if(secondsShift == 0 ){
return Instant.ofEpochSecond(0);
}
// we want it to be represented as a UNIX timestamp style, seconds since Epoch in UTC
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
if (annotation.expiryUnit() == TimeUnit.DAYS) {
// makes sure we won't lose resolution
cal.add(Calendar.DAY_OF_MONTH, expiryValue);
} else {
// use the shift in seconds since resolution should be smaller
cal.add(Calendar.SECOND, (int) secondsShift);
}
return Instant.ofEpochSecond(cal.getTimeInMillis() / 1000); // note: Unix UTC time representation in int is okay
// until year 2038
}
private static int getExpiryValue(Expiry annotation, Environment environment) {
int expiryValue = annotation.expiry();
String expiryExpressionString = annotation.expiryExpression();

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors
* Copyright 2012-2021 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.
@@ -16,10 +16,9 @@
package org.springframework.data.couchbase.core.mapping;
import org.springframework.data.mapping.PersistentEntity;
import java.time.Duration;
import java.time.Instant;
import org.springframework.data.mapping.PersistentEntity;
/**
* Represents an entity that can be persisted which contains 0 or more properties.
@@ -41,6 +40,7 @@ public interface CouchbasePersistentEntity<T> extends PersistentEntity<T, Couchb
*
* @return the expiration time in correct Couchbase format.
*/
@Deprecated
int getExpiry();
/**
@@ -53,16 +53,6 @@ public interface CouchbasePersistentEntity<T> extends PersistentEntity<T, Couchb
*/
Duration getExpiryDuration();
/**
* Returns the expiration time of the entity.
* <p/>
* The Couchbase format for expiration time is: - for TTL < 31 days (<= 30 * 24 * 60 * 60): expressed as a TTL in
* seconds - for TTL > 30 days: expressed as Unix UTC time of expiry (number of SECONDS since the Epoch)
*
* @return the expiration time Instant
*/
Instant getExpiryInstant();
/**
* Flag for using getAndTouch operations for reads, resetting the expiration (if one was set) when the entity is
* directly read (eg. findOne, findById).

View File

@@ -16,8 +16,8 @@
package org.springframework.data.couchbase.core.mapping;
import static org.assertj.core.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.*;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.util.Calendar;
import java.util.TimeZone;
@@ -66,7 +66,7 @@ public class BasicCouchbasePersistentEntityTests {
CouchbasePersistentEntity<OverLimitDaysExpiry> entityOver = new BasicCouchbasePersistentEntity<>(
ClassTypeInformation.from(OverLimitDaysExpiry.class));
int expiryOver = (int)entityOver.getExpiryInstant().getEpochSecond();
int expiryOver = (int) entityOver.getExpiry();
Calendar expected = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
expected.add(Calendar.DAY_OF_YEAR, 31);
@@ -87,7 +87,7 @@ public class BasicCouchbasePersistentEntityTests {
ClassTypeInformation.from(OverLimitDaysExpiryExpression.class));
entityOver.setEnvironment(environment);
int expiryOver = (int)entityOver.getExpiryInstant().getEpochSecond();
int expiryOver = (int) entityOver.getExpiry();
Calendar expected = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
expected.add(Calendar.DAY_OF_YEAR, 31);
@@ -107,7 +107,7 @@ public class BasicCouchbasePersistentEntityTests {
CouchbasePersistentEntity<OverLimitSecondsExpiry> entityOver = new BasicCouchbasePersistentEntity<>(
ClassTypeInformation.from(OverLimitSecondsExpiry.class));
int expiryOver = (int)entityOver.getExpiryInstant().getEpochSecond();
int expiryOver = (int) entityOver.getExpiry();
Calendar expected = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
expected.add(Calendar.DAY_OF_YEAR, 31);