From e2ca94297a07ef92ce382569fa23d412ee92be84 Mon Sep 17 00:00:00 2001 From: godotg Date: Tue, 2 Jul 2024 11:04:16 +0800 Subject: [PATCH] ref[ttl]: delete ttl index --- .../main/java/com/zfoo/orm/anno/Index.java | 2 - .../java/com/zfoo/orm/manager/OrmManager.java | 37 ++++++++---------- .../java/com/zfoo/orm/model/IndexDef.java | 10 +---- .../java/com/zfoo/orm/accessor/TtlTest.java | 38 ------------------- .../java/com/zfoo/orm/entity/MailEntity.java | 3 -- 5 files changed, 17 insertions(+), 73 deletions(-) delete mode 100644 orm/src/test/java/com/zfoo/orm/accessor/TtlTest.java diff --git a/orm/src/main/java/com/zfoo/orm/anno/Index.java b/orm/src/main/java/com/zfoo/orm/anno/Index.java index dafd5ec0a..5e465cefb 100644 --- a/orm/src/main/java/com/zfoo/orm/anno/Index.java +++ b/orm/src/main/java/com/zfoo/orm/anno/Index.java @@ -28,6 +28,4 @@ boolean unique(); - // 默认小于0不开启TTL文档超时索引 - long ttlExpireAfterSeconds() default -1L; } 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 1a21751b8..10e325daf 100644 --- a/orm/src/main/java/com/zfoo/orm/manager/OrmManager.java +++ b/orm/src/main/java/com/zfoo/orm/manager/OrmManager.java @@ -42,6 +42,8 @@ import org.bson.codecs.configuration.CodecRegistries; import org.bson.codecs.pojo.PojoCodecProvider; import org.bson.types.ObjectId; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.aop.framework.AopProxyUtils; import org.springframework.core.io.Resource; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; @@ -63,6 +65,9 @@ * @author godotg */ public class OrmManager implements IOrmManager { + + private static final Logger logger = LoggerFactory.getLogger(OrmManager.class); + private OrmConfig ormConfig; private MongoClient mongoClient; @@ -154,18 +159,15 @@ public void initBefore() { } } if (!hasIndex) { - var indexOptions = new IndexOptions(); - indexOptions.unique(index.isUnique()); - - if (index.getTtlExpireAfterSeconds() > 0) { - indexOptions.expireAfter(index.getTtlExpireAfterSeconds(), TimeUnit.SECONDS); - } + var isUnique = index.isUnique(); + var isAscending = index.isAscending(); - if (index.isAscending()) { - collection.createIndex(Indexes.ascending(fieldName), indexOptions); - } else { - collection.createIndex(Indexes.descending(fieldName), indexOptions); - } + var indexOptions = new IndexOptions(); + indexOptions.unique(isUnique); + String indexName = isAscending + ? collection.createIndex(Indexes.ascending(fieldName), indexOptions) + : collection.createIndex(Indexes.descending(fieldName), indexOptions); + logger.info("orm auto created index:[{}] ascending:[{}] field:[{}] unique:[{}]", indexName, isAscending, fieldName, isUnique); } } } @@ -185,7 +187,8 @@ public void initBefore() { } } if (!hasIndex) { - collection.createIndex(Indexes.text(fieldName)); + String indexName = collection.createIndex(Indexes.text(fieldName)); + logger.info("orm auto created text index:[{}] field:[{}]", indexName, fieldName); } } } @@ -386,15 +389,7 @@ public EntityDef parserEntityDef(Class> clazz) { for (var field : fields) { var indexAnnotation = field.getAnnotation(Index.class); - if (indexAnnotation.ttlExpireAfterSeconds() > 0) { - var fieldType = field.getGenericType(); - if (!(fieldType == Date.class || field.getGenericType().toString().equals("java.util.List"))) { - // MongoDB规定TTL类型必须是Date,List的其中一种类型 - throw new IllegalArgumentException(StringUtils.format("MongoDB TTL type:[{}] must be Date or List", field.getName())); - } - } - - IndexDef indexDef = new IndexDef(field, indexAnnotation.ascending(), indexAnnotation.unique(), indexAnnotation.ttlExpireAfterSeconds()); + IndexDef indexDef = new IndexDef(field, indexAnnotation.ascending(), indexAnnotation.unique()); indexDefMap.put(field.getName(), indexDef); } diff --git a/orm/src/main/java/com/zfoo/orm/model/IndexDef.java b/orm/src/main/java/com/zfoo/orm/model/IndexDef.java index 5aaf6d72d..6a25320a2 100644 --- a/orm/src/main/java/com/zfoo/orm/model/IndexDef.java +++ b/orm/src/main/java/com/zfoo/orm/model/IndexDef.java @@ -22,13 +22,11 @@ public class IndexDef { private Field field; private boolean ascending; private boolean unique; - private long ttlExpireAfterSeconds; - public IndexDef(Field field, boolean ascending, boolean unique, long ttlExpireAfterSeconds) { + public IndexDef(Field field, boolean ascending, boolean unique) { this.field = field; this.ascending = ascending; this.unique = unique; - this.ttlExpireAfterSeconds = ttlExpireAfterSeconds; } public Field getField() { @@ -55,11 +53,5 @@ public void setUnique(boolean unique) { this.unique = unique; } - public long getTtlExpireAfterSeconds() { - return ttlExpireAfterSeconds; - } - public void setTtlExpireAfterSeconds(long ttlExpireAfterSeconds) { - this.ttlExpireAfterSeconds = ttlExpireAfterSeconds; - } } diff --git a/orm/src/test/java/com/zfoo/orm/accessor/TtlTest.java b/orm/src/test/java/com/zfoo/orm/accessor/TtlTest.java deleted file mode 100644 index 16b4bc16c..000000000 --- a/orm/src/test/java/com/zfoo/orm/accessor/TtlTest.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright (C) 2020 The zfoo Authors - * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed - * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and limitations under the License. - */ - -package com.zfoo.orm.accessor; - -import com.zfoo.orm.OrmContext; -import com.zfoo.orm.entity.MailEntity; -import com.zfoo.scheduler.util.TimeUtils; -import org.junit.Ignore; -import org.junit.Test; -import org.springframework.context.support.ClassPathXmlApplicationContext; - -import java.util.Date; - -/** - * @author godotg - */ -@Ignore -public class TtlTest { - - @Test - public void ttlTest() { - var context = new ClassPathXmlApplicationContext("application.xml"); - OrmContext.getOrmManager().getCollection(MailEntity.class).drop(); - var mailEntity = MailEntity.valueOf("d", "godot", "hello ttl", new Date(TimeUtils.now())); - OrmContext.getAccessor().insert(mailEntity); - } - -} 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 613f48189..e5a69e056 100644 --- a/orm/src/test/java/com/zfoo/orm/entity/MailEntity.java +++ b/orm/src/test/java/com/zfoo/orm/entity/MailEntity.java @@ -35,9 +35,6 @@ public class MailEntity implements IEntity { private String content; -// @Index(ascending = true, unique = false, ttlExpireAfterSeconds = 10) -// private Date createDate; - @Index(ascending = true, unique = false, ttlExpireAfterSeconds = 10) private Date createDate; public static MailEntity valueOf(String id, String userName, String content, Date createDate) {