mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-10-15 14:16:45 +00:00
Merge pull request #23 from yujuncai/main
为index注解添加添加MongoDB 的ttl属性,并设置过期时间,单位秒,当设置ttl=true时检验索引数据类型只能是Data类型
This commit is contained in:
3 files changed
+44
-2
No files matched your search
@@ -54,6 +54,7 @@ import java.lang.reflect.ParameterizedType;
|
|||||||
import java.lang.reflect.Type;
|
import java.lang.reflect.Type;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
|
||||||
@@ -143,6 +144,11 @@ public class OrmManager implements IOrmManager {
|
|||||||
if (!hasIndex) {
|
if (!hasIndex) {
|
||||||
var indexOptions = new IndexOptions();
|
var indexOptions = new IndexOptions();
|
||||||
indexOptions.unique(index.isUnique());
|
indexOptions.unique(index.isUnique());
|
||||||
|
|
||||||
|
if(index.isTtl()){
|
||||||
|
indexOptions.expireAfter(index.getExpireAfterSeconds(), TimeUnit.SECONDS) ;
|
||||||
|
}
|
||||||
|
|
||||||
if (index.isAscending()) {
|
if (index.isAscending()) {
|
||||||
collection.createIndex(Indexes.ascending(fieldName), indexOptions);
|
collection.createIndex(Indexes.ascending(fieldName), indexOptions);
|
||||||
} else {
|
} else {
|
||||||
@@ -326,7 +332,16 @@ public class OrmManager implements IOrmManager {
|
|||||||
var fields = ReflectionUtils.getFieldsByAnnoInPOJOClass(clazz, Index.class);
|
var fields = ReflectionUtils.getFieldsByAnnoInPOJOClass(clazz, Index.class);
|
||||||
for (var field : fields) {
|
for (var field : fields) {
|
||||||
var indexAnnotation = field.getAnnotation(Index.class);
|
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);
|
indexDefMap.put(field.getName(), indexDef);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -26,4 +26,8 @@ public @interface Index {
|
|||||||
boolean ascending();
|
boolean ascending();
|
||||||
|
|
||||||
boolean unique();
|
boolean unique();
|
||||||
|
|
||||||
|
boolean ttl() default false;;
|
||||||
|
|
||||||
|
long expireAfterSeconds() default 0L;
|
||||||
}
|
}
|
||||||
@@ -25,10 +25,17 @@ public class IndexDef {
|
|||||||
private boolean ascending;
|
private boolean ascending;
|
||||||
private boolean unique;
|
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.field = field;
|
||||||
this.ascending = ascending;
|
this.ascending = ascending;
|
||||||
this.unique = unique;
|
this.unique = unique;
|
||||||
|
this.ttl = ttl;
|
||||||
|
this.expireAfterSeconds = expireAfterSeconds;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Field getField() {
|
public Field getField() {
|
||||||
@@ -54,4 +61,20 @@ public class IndexDef {
|
|||||||
public void setUnique(boolean unique) {
|
public void setUnique(boolean unique) {
|
||||||
this.unique = 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