DATACOUCH-158 - Allow more explicit expiry unit
The Document annotation now also support explicitly giving a TimeUnit for the expiry, ensuring the framework will correctly convert that to the expected Couchbase format.
This commit is contained in:
@@ -16,6 +16,10 @@
|
||||
|
||||
package org.springframework.data.couchbase.core.mapping;
|
||||
|
||||
import java.util.Calendar;
|
||||
import java.util.TimeZone;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import com.couchbase.client.java.repository.annotation.Id;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
@@ -95,15 +99,28 @@ public class BasicCouchbasePersistentEntity<T> extends BasicPersistentEntity<T,
|
||||
return super.returnPropertyIfBetterIdPropertyCandidateOrNull(property);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the expiration time of the entity.
|
||||
*
|
||||
* @return the expiration time.
|
||||
*/
|
||||
@Override
|
||||
public int getExpiry() {
|
||||
org.springframework.data.couchbase.core.mapping.Document annotation =
|
||||
getType().getAnnotation(org.springframework.data.couchbase.core.mapping.Document.class);
|
||||
return annotation == null ? 0 : annotation.expiry();
|
||||
if (annotation == null)
|
||||
return 0;
|
||||
|
||||
long secondsShift = annotation.expiryUnit().toSeconds(annotation.expiry());
|
||||
if (secondsShift > TTL_IN_SECONDS_INCLUSIVE_END) {
|
||||
//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, annotation.expiry());
|
||||
} else {
|
||||
//use the shift in seconds since resolution should be smaller
|
||||
cal.add(Calendar.SECOND, (int) secondsShift);
|
||||
}
|
||||
return (int) (cal.getTimeInMillis() / 1000); //note: Unix UTC time representation in int is okay until year 2038
|
||||
} else {
|
||||
return (int) secondsShift;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -225,6 +225,9 @@ public class CouchbaseDocument implements CouchbaseStorable {
|
||||
* <p/>
|
||||
* If the expiration time is 0, then the document will be persisted until
|
||||
* deleted manually ("forever").
|
||||
* <p/>
|
||||
* Expiration should be expressed as seconds if <= 30 days (30 x 24 x 60 x 60 seconds),
|
||||
* or as an expiry date (UTC, UNIX time ie. seconds form the Epoch) if > 30 days.
|
||||
*
|
||||
* @param expiration
|
||||
* @return the {@link CouchbaseDocument} for chaining.
|
||||
|
||||
@@ -27,9 +27,18 @@ public interface CouchbasePersistentEntity<T> extends
|
||||
PersistentEntity<T, CouchbasePersistentProperty> {
|
||||
|
||||
/**
|
||||
* Returns the expiry time for the document.
|
||||
* The threshold (inclusive) after which expiry should be expressed as a Unix UTC time.
|
||||
*/
|
||||
long TTL_IN_SECONDS_INCLUSIVE_END = 30 * 24 * 60 * 60;
|
||||
|
||||
/**
|
||||
* 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.
|
||||
* @return the expiration time in correct Couchbase format.
|
||||
*/
|
||||
int getExpiry();
|
||||
|
||||
|
||||
@@ -21,6 +21,7 @@ import java.lang.annotation.Inherited;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.springframework.data.annotation.Persistent;
|
||||
|
||||
@@ -36,8 +37,13 @@ import org.springframework.data.annotation.Persistent;
|
||||
public @interface Document {
|
||||
|
||||
/**
|
||||
* An optional expiry time for the document.
|
||||
* An optional expiry time for the document. Default is no expiry.
|
||||
*/
|
||||
int expiry() default 0;
|
||||
|
||||
/**
|
||||
* An optional time unit for the document's {@link #expiry()}, if set. Default is {@link TimeUnit#SECONDS}.
|
||||
*/
|
||||
TimeUnit expiryUnit() default TimeUnit.SECONDS;
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user