perf[orm]: 将超时索引ttl和expiredAfter合二为一,默认小于0不开启TTL文档超时索引

This commit is contained in:
godotg committed 2022-10-15 16:30:43 +08:00
1 parent 5a24ec239d
commit 606e45838c
4 files changed
+18 -40

No files matched your search

@@ -20,9 +20,9 @@ import com.mongodb.client.*;
import com.mongodb.client.model.IndexOptions;
import com.mongodb.client.model.Indexes;
import com.zfoo.orm.OrmContext;
import com.zfoo.orm.model.anno.*;
import com.zfoo.orm.cache.EntityCaches;
import com.zfoo.orm.cache.IEntityCaches;
import com.zfoo.orm.model.anno.*;
import com.zfoo.orm.model.config.OrmConfig;
import com.zfoo.orm.model.entity.IEntity;
import com.zfoo.orm.model.vo.EntityDef;
@@ -145,8 +145,8 @@ public class OrmManager implements IOrmManager {
var indexOptions = new IndexOptions();
indexOptions.unique(index.isUnique());
if(index.isTtl()){
indexOptions.expireAfter(index.getExpireAfterSeconds(), TimeUnit.SECONDS) ;
if (index.getTtlExpireAfterSeconds() > 0) {
indexOptions.expireAfter(index.getTtlExpireAfterSeconds(), TimeUnit.SECONDS);
}
if (index.isAscending()) {
@@ -333,22 +333,14 @@ public class OrmManager implements IOrmManager {
for (var field : fields) {
var indexAnnotation = field.getAnnotation(Index.class);
if(indexAnnotation.ttl()) {
if (!field.getGenericType().toString().equals(
"class java.util.Date")){
throw new IllegalArgumentException(StringUtils.format("[{}]不是Date类型", field.getName()));
}
if (!field.getGenericType().toString().equals(
"java.util.List<java.util.Date>")){
throw new IllegalArgumentException(StringUtils.format("[{}]不是List<Date>类型", field.getName()));
}
if (!field.getGenericType().toString().equals(
"class [Ljava.util.Date;")){
throw new IllegalArgumentException(StringUtils.format("[{}]不是Date[]类型", field.getName()));
if (indexAnnotation.ttlExpireAfterSeconds() > 0) {
var fieldType = field.getGenericType();
if (!(fieldType == Date.class || fieldType == Date[].class || field.getGenericType().toString().equals("java.util.List<java.util.Date>"))) {
throw new IllegalArgumentException(StringUtils.format("MongoDB规定TTL类型[{}]必须是Date,Date[],List<Date>的其中一种类型", field.getName()));
}
}
IndexDef indexDef = new IndexDef(field, indexAnnotation.ascending(), indexAnnotation.unique(),indexAnnotation.ttl(),indexAnnotation.expireAfterSeconds());
IndexDef indexDef = new IndexDef(field, indexAnnotation.ascending(), indexAnnotation.unique(), indexAnnotation.ttlExpireAfterSeconds());
indexDefMap.put(field.getName(), indexDef);
}
@@ -27,7 +27,6 @@ public @interface Index {
boolean unique();
boolean ttl() default false;;
long expireAfterSeconds() default 0L;
// 默认小于0不开启TTL文档超时索引
long ttlExpireAfterSeconds() default -1L;
}
@@ -24,18 +24,13 @@ public class IndexDef {
private Field field;
private boolean ascending;
private boolean unique;
private long ttlExpireAfterSeconds;
private boolean ttl;
private long expireAfterSeconds;
public IndexDef(Field field, boolean ascending, boolean unique, boolean ttl, long expireAfterSeconds) {
public IndexDef(Field field, boolean ascending, boolean unique, long ttlExpireAfterSeconds) {
this.field = field;
this.ascending = ascending;
this.unique = unique;
this.ttl = ttl;
this.expireAfterSeconds = expireAfterSeconds;
this.ttlExpireAfterSeconds = ttlExpireAfterSeconds;
}
public Field getField() {
@@ -62,19 +57,11 @@ public class IndexDef {
this.unique = unique;
}
public boolean isTtl() {
return ttl;
public long getTtlExpireAfterSeconds() {
return ttlExpireAfterSeconds;
}
public void setTtl(boolean ttl) {
this.ttl = ttl;
}
public long getExpireAfterSeconds() {
return expireAfterSeconds;
}
public void setExpireAfterSeconds(long expireAfterSeconds) {
this.expireAfterSeconds = expireAfterSeconds;
public void setTtlExpireAfterSeconds(long ttlExpireAfterSeconds) {
this.ttlExpireAfterSeconds = ttlExpireAfterSeconds;
}
}
@@ -36,7 +36,7 @@ public class MailEntity implements IEntity<String> {
private String content;
@Index(ascending = true, unique = false, ttl = true, expireAfterSeconds = 10)
@Index(ascending = true, unique = false, ttlExpireAfterSeconds = 10)
private Date createDate;
public static MailEntity valueOf(String id, String userName, String content, Date createDate) {