Skip to content

Commit 66cc1b5

Browse files
authored
Changes for PR 1223 that I had missed. (#1230)
Closes #1229.
1 parent b9fa1c7 commit 66cc1b5

File tree

4 files changed

+15
-51
lines changed

4 files changed

+15
-51
lines changed

src/main/java/org/springframework/data/couchbase/core/ReactiveFindByIdOperationSupport.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,10 +167,10 @@ private Duration expiryToUse() {
167167
if (expiryToUse == null) { // GetAndTouchOptions without specifying expiry -> get expiry from annoation
168168
final CouchbasePersistentEntity<?> entity = template.getConverter().getMappingContext()
169169
.getRequiredPersistentEntity(domainType);
170-
expiryToUse = Duration.ofSeconds(entity.getExpiry());
170+
expiryToUse = entity.getExpiryDuration();
171171
}
172172
}
173-
return expiry;
173+
return expiryToUse;
174174
}
175175
}
176176

src/main/java/org/springframework/data/couchbase/core/mapping/BasicCouchbasePersistentEntity.java

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors
2+
* Copyright 2012-2021 the original author or authors
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,7 +17,6 @@
1717
package org.springframework.data.couchbase.core.mapping;
1818

1919
import java.time.Duration;
20-
import java.time.Instant;
2120
import java.util.Calendar;
2221
import java.util.TimeZone;
2322
import java.util.concurrent.TimeUnit;
@@ -98,10 +97,12 @@ protected CouchbasePersistentProperty returnPropertyIfBetterIdPropertyCandidateO
9897
}
9998

10099
@Override
100+
@Deprecated
101101
public int getExpiry() {
102102
return getExpiry(AnnotatedElementUtils.findMergedAnnotation(getType(), Expiry.class), environment);
103103
}
104104

105+
@Deprecated
105106
public static int getExpiry(Expiry annotation, Environment environment) {
106107
if (annotation == null) {
107108
return 0;
@@ -133,40 +134,13 @@ public Duration getExpiryDuration() {
133134

134135
private static Duration getExpiryDuration(Expiry annotation, Environment environment) {
135136
if (annotation == null) {
136-
return Duration.ofSeconds(0);
137+
return Duration.ZERO;
137138
}
138139
int expiryValue = getExpiryValue(annotation, environment);
139140
long secondsShift = annotation.expiryUnit().toSeconds(expiryValue);
140141
return Duration.ofSeconds(secondsShift);
141142
}
142143

143-
@Override
144-
public Instant getExpiryInstant() {
145-
return getExpiryInstant(AnnotatedElementUtils.findMergedAnnotation(getType(), Expiry.class), environment);
146-
}
147-
148-
private static Instant getExpiryInstant(Expiry annotation, Environment environment) {
149-
if (annotation == null) {
150-
return Instant.ofEpochSecond(0);
151-
}
152-
int expiryValue = getExpiryValue(annotation, environment);
153-
long secondsShift = annotation.expiryUnit().toSeconds(expiryValue);
154-
if(secondsShift == 0 ){
155-
return Instant.ofEpochSecond(0);
156-
}
157-
// we want it to be represented as a UNIX timestamp style, seconds since Epoch in UTC
158-
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
159-
if (annotation.expiryUnit() == TimeUnit.DAYS) {
160-
// makes sure we won't lose resolution
161-
cal.add(Calendar.DAY_OF_MONTH, expiryValue);
162-
} else {
163-
// use the shift in seconds since resolution should be smaller
164-
cal.add(Calendar.SECOND, (int) secondsShift);
165-
}
166-
return Instant.ofEpochSecond(cal.getTimeInMillis() / 1000); // note: Unix UTC time representation in int is okay
167-
// until year 2038
168-
}
169-
170144
private static int getExpiryValue(Expiry annotation, Environment environment) {
171145
int expiryValue = annotation.expiry();
172146
String expiryExpressionString = annotation.expiryExpression();

src/main/java/org/springframework/data/couchbase/core/mapping/CouchbasePersistentEntity.java

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors
2+
* Copyright 2012-2021 the original author or authors
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,10 +16,9 @@
1616

1717
package org.springframework.data.couchbase.core.mapping;
1818

19-
import org.springframework.data.mapping.PersistentEntity;
20-
2119
import java.time.Duration;
22-
import java.time.Instant;
20+
21+
import org.springframework.data.mapping.PersistentEntity;
2322

2423
/**
2524
* 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
4140
*
4241
* @return the expiration time in correct Couchbase format.
4342
*/
43+
@Deprecated
4444
int getExpiry();
4545

4646
/**
@@ -53,16 +53,6 @@ public interface CouchbasePersistentEntity<T> extends PersistentEntity<T, Couchb
5353
*/
5454
Duration getExpiryDuration();
5555

56-
/**
57-
* Returns the expiration time of the entity.
58-
* <p/>
59-
* The Couchbase format for expiration time is: - for TTL < 31 days (<= 30 * 24 * 60 * 60): expressed as a TTL in
60-
* seconds - for TTL > 30 days: expressed as Unix UTC time of expiry (number of SECONDS since the Epoch)
61-
*
62-
* @return the expiration time Instant
63-
*/
64-
Instant getExpiryInstant();
65-
6656
/**
6757
* Flag for using getAndTouch operations for reads, resetting the expiration (if one was set) when the entity is
6858
* directly read (eg. findOne, findById).

src/test/java/org/springframework/data/couchbase/core/mapping/BasicCouchbasePersistentEntityTests.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616

1717
package org.springframework.data.couchbase.core.mapping;
1818

19-
import static org.assertj.core.api.Assertions.*;
20-
import static org.junit.jupiter.api.Assertions.*;
19+
import static org.assertj.core.api.Assertions.assertThat;
20+
import static org.junit.jupiter.api.Assertions.assertThrows;
2121

2222
import java.util.Calendar;
2323
import java.util.TimeZone;
@@ -66,7 +66,7 @@ void testLargeExpiry31DaysIsConvertedToUnixUtcTime() {
6666
CouchbasePersistentEntity<OverLimitDaysExpiry> entityOver = new BasicCouchbasePersistentEntity<>(
6767
ClassTypeInformation.from(OverLimitDaysExpiry.class));
6868

69-
int expiryOver = (int)entityOver.getExpiryInstant().getEpochSecond();
69+
int expiryOver = (int) entityOver.getExpiry();
7070
Calendar expected = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
7171
expected.add(Calendar.DAY_OF_YEAR, 31);
7272

@@ -87,7 +87,7 @@ void testLargeExpiryExpression31DaysIsConvertedToUnixUtcTime() {
8787
ClassTypeInformation.from(OverLimitDaysExpiryExpression.class));
8888
entityOver.setEnvironment(environment);
8989

90-
int expiryOver = (int)entityOver.getExpiryInstant().getEpochSecond();
90+
int expiryOver = (int) entityOver.getExpiry();
9191
Calendar expected = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
9292
expected.add(Calendar.DAY_OF_YEAR, 31);
9393

@@ -107,7 +107,7 @@ void testLargeExpiry31DaysInSecondsIsConvertedToUnixUtcTime() {
107107
CouchbasePersistentEntity<OverLimitSecondsExpiry> entityOver = new BasicCouchbasePersistentEntity<>(
108108
ClassTypeInformation.from(OverLimitSecondsExpiry.class));
109109

110-
int expiryOver = (int)entityOver.getExpiryInstant().getEpochSecond();
110+
int expiryOver = (int) entityOver.getExpiry();
111111
Calendar expected = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
112112
expected.add(Calendar.DAY_OF_YEAR, 31);
113113

0 commit comments

Comments
 (0)