Skip to content

Commit

Permalink
ref[ttl]: delete ttl index
Browse files Browse the repository at this point in the history
  • Loading branch information
jaysunxiao committed Jul 2, 2024
1 parent 14816b1 commit e2ca942
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 73 deletions.
2 changes: 0 additions & 2 deletions orm/src/main/java/com/zfoo/orm/anno/Index.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,4 @@

boolean unique();

// 默认小于0不开启TTL文档超时索引
long ttlExpireAfterSeconds() default -1L;
}
37 changes: 16 additions & 21 deletions orm/src/main/java/com/zfoo/orm/manager/OrmManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}
}
}
Expand All @@ -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);
}
}
}
Expand Down Expand Up @@ -386,15 +389,7 @@ public EntityDef parserEntityDef(Class<? extends IEntity<?>> 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<java.util.Date>"))) {
// MongoDB规定TTL类型必须是Date,List<Date>的其中一种类型
throw new IllegalArgumentException(StringUtils.format("MongoDB TTL type:[{}] must be Date or List<Date>", 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);
}

Expand Down
10 changes: 1 addition & 9 deletions orm/src/main/java/com/zfoo/orm/model/IndexDef.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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;
}
}
38 changes: 0 additions & 38 deletions orm/src/test/java/com/zfoo/orm/accessor/TtlTest.java

This file was deleted.

3 changes: 0 additions & 3 deletions orm/src/test/java/com/zfoo/orm/entity/MailEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@ public class MailEntity implements IEntity<String> {

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) {
Expand Down

0 comments on commit e2ca942

Please sign in to comment.