mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-05-19 13:27:10 +00:00
Merge pull request #23 from yujuncai/main
为index注解添加添加MongoDB 的ttl属性,并设置过期时间,单位秒,当设置ttl=true时检验索引数据类型只能是Data类型
This commit is contained in:
@@ -54,6 +54,7 @@ import java.lang.reflect.ParameterizedType;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
|
||||
@@ -143,6 +144,11 @@ public class OrmManager implements IOrmManager {
|
||||
if (!hasIndex) {
|
||||
var indexOptions = new IndexOptions();
|
||||
indexOptions.unique(index.isUnique());
|
||||
|
||||
if(index.isTtl()){
|
||||
indexOptions.expireAfter(index.getExpireAfterSeconds(), TimeUnit.SECONDS) ;
|
||||
}
|
||||
|
||||
if (index.isAscending()) {
|
||||
collection.createIndex(Indexes.ascending(fieldName), indexOptions);
|
||||
} else {
|
||||
@@ -326,7 +332,16 @@ public class OrmManager implements IOrmManager {
|
||||
var fields = ReflectionUtils.getFieldsByAnnoInPOJOClass(clazz, Index.class);
|
||||
for (var field : fields) {
|
||||
var indexAnnotation = field.getAnnotation(Index.class);
|
||||
IndexDef indexDef = new IndexDef(field, indexAnnotation.ascending(), indexAnnotation.unique());
|
||||
|
||||
if(indexAnnotation.ttl()) {
|
||||
if (!field.getGenericType().toString().equals(
|
||||
"class java.util.Date")){
|
||||
throw new IllegalArgumentException(StringUtils.format("[{}]不是Date类型", field.getName()));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
IndexDef indexDef = new IndexDef(field, indexAnnotation.ascending(), indexAnnotation.unique(),indexAnnotation.ttl(),indexAnnotation.expireAfterSeconds());
|
||||
indexDefMap.put(field.getName(), indexDef);
|
||||
}
|
||||
|
||||
|
||||
@@ -26,4 +26,8 @@ public @interface Index {
|
||||
boolean ascending();
|
||||
|
||||
boolean unique();
|
||||
|
||||
boolean ttl() default false;;
|
||||
|
||||
long expireAfterSeconds() default 0L;
|
||||
}
|
||||
|
||||
@@ -25,10 +25,17 @@ public class IndexDef {
|
||||
private boolean ascending;
|
||||
private boolean unique;
|
||||
|
||||
public IndexDef(Field field, boolean ascending, boolean unique) {
|
||||
private boolean ttl;
|
||||
|
||||
private long expireAfterSeconds;
|
||||
|
||||
|
||||
public IndexDef(Field field, boolean ascending, boolean unique, boolean ttl, long expireAfterSeconds) {
|
||||
this.field = field;
|
||||
this.ascending = ascending;
|
||||
this.unique = unique;
|
||||
this.ttl = ttl;
|
||||
this.expireAfterSeconds = expireAfterSeconds;
|
||||
}
|
||||
|
||||
public Field getField() {
|
||||
@@ -54,4 +61,20 @@ public class IndexDef {
|
||||
public void setUnique(boolean unique) {
|
||||
this.unique = unique;
|
||||
}
|
||||
|
||||
public boolean isTtl() {
|
||||
return ttl;
|
||||
}
|
||||
|
||||
public void setTtl(boolean ttl) {
|
||||
this.ttl = ttl;
|
||||
}
|
||||
|
||||
public long getExpireAfterSeconds() {
|
||||
return expireAfterSeconds;
|
||||
}
|
||||
|
||||
public void setExpireAfterSeconds(long expireAfterSeconds) {
|
||||
this.expireAfterSeconds = expireAfterSeconds;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user