From 606e45838c6bec49a9147d297e105e8c38c9a8ff Mon Sep 17 00:00:00 2001 From: godotg Date: Sat, 15 Oct 2022 16:30:43 +0800 Subject: [PATCH] =?UTF-8?q?perf[orm]:=20=E5=B0=86=E8=B6=85=E6=97=B6?= =?UTF-8?q?=E7=B4=A2=E5=BC=95ttl=E5=92=8CexpiredAfter=E5=90=88=E4=BA=8C?= =?UTF-8?q?=E4=B8=BA=E4=B8=80=EF=BC=8C=E9=BB=98=E8=AE=A4=E5=B0=8F=E4=BA=8E?= =?UTF-8?q?0=E4=B8=8D=E5=BC=80=E5=90=AFTTL=E6=96=87=E6=A1=A3=E8=B6=85?= =?UTF-8?q?=E6=97=B6=E7=B4=A2=E5=BC=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/zfoo/orm/manager/OrmManager.java | 24 ++++++----------- .../java/com/zfoo/orm/model/anno/Index.java | 5 ++-- .../java/com/zfoo/orm/model/vo/IndexDef.java | 27 +++++-------------- .../java/com/zfoo/orm/entity/MailEntity.java | 2 +- 4 files changed, 18 insertions(+), 40 deletions(-) diff --git a/orm/src/main/java/com/zfoo/orm/manager/OrmManager.java b/orm/src/main/java/com/zfoo/orm/manager/OrmManager.java index 5b2aa361..d51ddc31 100644 --- a/orm/src/main/java/com/zfoo/orm/manager/OrmManager.java +++ b/orm/src/main/java/com/zfoo/orm/manager/OrmManager.java @@ -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")){ - throw new IllegalArgumentException(StringUtils.format("[{}]不是List类型", 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"))) { + throw new IllegalArgumentException(StringUtils.format("MongoDB规定TTL类型[{}]必须是Date,Date[],List的其中一种类型", 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); } diff --git a/orm/src/main/java/com/zfoo/orm/model/anno/Index.java b/orm/src/main/java/com/zfoo/orm/model/anno/Index.java index 4ef1b456..3731ed1a 100644 --- a/orm/src/main/java/com/zfoo/orm/model/anno/Index.java +++ b/orm/src/main/java/com/zfoo/orm/model/anno/Index.java @@ -27,7 +27,6 @@ public @interface Index { boolean unique(); - boolean ttl() default false;; - - long expireAfterSeconds() default 0L; + // 默认小于0不开启TTL文档超时索引 + long ttlExpireAfterSeconds() default -1L; } diff --git a/orm/src/main/java/com/zfoo/orm/model/vo/IndexDef.java b/orm/src/main/java/com/zfoo/orm/model/vo/IndexDef.java index 86133fb4..a62a5303 100644 --- a/orm/src/main/java/com/zfoo/orm/model/vo/IndexDef.java +++ b/orm/src/main/java/com/zfoo/orm/model/vo/IndexDef.java @@ -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; } } diff --git a/orm/src/test/java/com/zfoo/orm/entity/MailEntity.java b/orm/src/test/java/com/zfoo/orm/entity/MailEntity.java index 771d55c3..daa3badb 100644 --- a/orm/src/test/java/com/zfoo/orm/entity/MailEntity.java +++ b/orm/src/test/java/com/zfoo/orm/entity/MailEntity.java @@ -36,7 +36,7 @@ public class MailEntity implements IEntity { 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) {