mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-10-18 00:15:39 +00:00
ref[ttl]: delete ttl index
This commit is contained in:
1 parent
14816b193d
commit
e2ca94297a
5 files changed
+17
-73
No files matched your search
@@ -28,6 +28,4 @@ public @interface Index {
|
|||||||
|
|
||||||
boolean unique();
|
boolean unique();
|
||||||
|
|
||||||
// 默认小于0不开启TTL文档超时索引
|
|
||||||
long ttlExpireAfterSeconds() default -1L;
|
|
||||||
}
|
}
|
||||||
@@ -42,6 +42,8 @@ import org.bson.Document;
|
|||||||
import org.bson.codecs.configuration.CodecRegistries;
|
import org.bson.codecs.configuration.CodecRegistries;
|
||||||
import org.bson.codecs.pojo.PojoCodecProvider;
|
import org.bson.codecs.pojo.PojoCodecProvider;
|
||||||
import org.bson.types.ObjectId;
|
import org.bson.types.ObjectId;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.aop.framework.AopProxyUtils;
|
import org.springframework.aop.framework.AopProxyUtils;
|
||||||
import org.springframework.core.io.Resource;
|
import org.springframework.core.io.Resource;
|
||||||
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
|
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
|
||||||
@@ -63,6 +65,9 @@ import java.util.concurrent.TimeUnit;
|
|||||||
* @author godotg
|
* @author godotg
|
||||||
*/
|
*/
|
||||||
public class OrmManager implements IOrmManager {
|
public class OrmManager implements IOrmManager {
|
||||||
|
|
||||||
|
private static final Logger logger = LoggerFactory.getLogger(OrmManager.class);
|
||||||
|
|
||||||
private OrmConfig ormConfig;
|
private OrmConfig ormConfig;
|
||||||
|
|
||||||
private MongoClient mongoClient;
|
private MongoClient mongoClient;
|
||||||
@@ -154,18 +159,15 @@ public class OrmManager implements IOrmManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!hasIndex) {
|
if (!hasIndex) {
|
||||||
|
var isUnique = index.isUnique();
|
||||||
|
var isAscending = index.isAscending();
|
||||||
|
|
||||||
var indexOptions = new IndexOptions();
|
var indexOptions = new IndexOptions();
|
||||||
indexOptions.unique(index.isUnique());
|
indexOptions.unique(isUnique);
|
||||||
|
String indexName = isAscending
|
||||||
if (index.getTtlExpireAfterSeconds() > 0) {
|
? collection.createIndex(Indexes.ascending(fieldName), indexOptions)
|
||||||
indexOptions.expireAfter(index.getTtlExpireAfterSeconds(), TimeUnit.SECONDS);
|
: collection.createIndex(Indexes.descending(fieldName), indexOptions);
|
||||||
}
|
logger.info("orm auto created index:[{}] ascending:[{}] field:[{}] unique:[{}]", indexName, isAscending, fieldName, isUnique);
|
||||||
|
|
||||||
if (index.isAscending()) {
|
|
||||||
collection.createIndex(Indexes.ascending(fieldName), indexOptions);
|
|
||||||
} else {
|
|
||||||
collection.createIndex(Indexes.descending(fieldName), indexOptions);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -185,7 +187,8 @@ public class OrmManager implements IOrmManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!hasIndex) {
|
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 class OrmManager implements IOrmManager {
|
|||||||
for (var field : fields) {
|
for (var field : fields) {
|
||||||
var indexAnnotation = field.getAnnotation(Index.class);
|
var indexAnnotation = field.getAnnotation(Index.class);
|
||||||
|
|
||||||
if (indexAnnotation.ttlExpireAfterSeconds() > 0) {
|
IndexDef indexDef = new IndexDef(field, indexAnnotation.ascending(), indexAnnotation.unique());
|
||||||
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());
|
|
||||||
indexDefMap.put(field.getName(), indexDef);
|
indexDefMap.put(field.getName(), indexDef);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -22,13 +22,11 @@ public class IndexDef {
|
|||||||
private Field field;
|
private Field field;
|
||||||
private boolean ascending;
|
private boolean ascending;
|
||||||
private boolean unique;
|
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.field = field;
|
||||||
this.ascending = ascending;
|
this.ascending = ascending;
|
||||||
this.unique = unique;
|
this.unique = unique;
|
||||||
this.ttlExpireAfterSeconds = ttlExpireAfterSeconds;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Field getField() {
|
public Field getField() {
|
||||||
@@ -55,11 +53,5 @@ public class IndexDef {
|
|||||||
this.unique = unique;
|
this.unique = unique;
|
||||||
}
|
}
|
||||||
|
|
||||||
public long getTtlExpireAfterSeconds() {
|
|
||||||
return ttlExpireAfterSeconds;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setTtlExpireAfterSeconds(long ttlExpireAfterSeconds) {
|
|
||||||
this.ttlExpireAfterSeconds = ttlExpireAfterSeconds;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -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);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -35,9 +35,6 @@ public class MailEntity implements IEntity<String> {
|
|||||||
|
|
||||||
private String content;
|
private String content;
|
||||||
|
|
||||||
// @Index(ascending = true, unique = false, ttlExpireAfterSeconds = 10)
|
|
||||||
// private Date createDate;
|
|
||||||
@Index(ascending = true, unique = false, ttlExpireAfterSeconds = 10)
|
|
||||||
private Date createDate;
|
private Date createDate;
|
||||||
|
|
||||||
public static MailEntity valueOf(String id, String userName, String content, Date createDate) {
|
public static MailEntity valueOf(String id, String userName, String content, Date createDate) {
|
||||||
|
|||||||
Reference in new issue
Block a user