mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-10-14 04:16:53 +00:00
test[orm]: cache test
This commit is contained in:
1 parent
a2fbc08b1a
commit
82f54c7786
10 files changed
+547
-23
No files matched your search
@@ -54,7 +54,7 @@ public abstract class EnhanceUtils {
|
||||
} else if (idType == float.class || idType == Float.class) {
|
||||
return "((Float)$1).floatValue()";
|
||||
} else if (idType == double.class || idType == Double.class) {
|
||||
return "((Double)$1).floatValue()";
|
||||
return "((Double)$1).doubleValue()";
|
||||
} else if (idType == String.class) {
|
||||
return "(String)$1";
|
||||
} else {
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* 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.mongodb.client.model.Filters;
|
||||
import com.zfoo.orm.OrmContext;
|
||||
import com.zfoo.orm.entity.UserEntity;
|
||||
import com.zfoo.protocol.util.ThreadUtils;
|
||||
import org.bson.Document;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
|
||||
/**
|
||||
* @author godotg
|
||||
*/
|
||||
@Ignore
|
||||
public class DocumentTest {
|
||||
|
||||
@Test
|
||||
public void collectionTest() {
|
||||
var context = new ClassPathXmlApplicationContext("application.xml");
|
||||
|
||||
var collection = OrmContext.getOrmManager().getCollection(UserEntity.class);
|
||||
var result = collection.updateOne(Filters.eq("_id", 1), new Document("$inc", new Document("c", 1L)));
|
||||
System.out.println(result);
|
||||
ThreadUtils.sleep(Long.MAX_VALUE);
|
||||
}
|
||||
|
||||
}
|
||||
+17
-21
@@ -36,7 +36,7 @@ public class EntityCachesTest {
|
||||
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
public void loadTest() {
|
||||
var context = new ClassPathXmlApplicationContext("application.xml");
|
||||
|
||||
// 每次运行前先删除数据库
|
||||
@@ -65,33 +65,29 @@ public class EntityCachesTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLoadOrInsert() {
|
||||
public void loadOrCreateTest() {
|
||||
var context = new ClassPathXmlApplicationContext("application.xml");
|
||||
|
||||
// 每次运行前先删除数据库
|
||||
var collection = OrmContext.getOrmManager().getCollection(UserEntity.class);
|
||||
collection.drop();
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
var userEntityCaches = (IEntityCache<Long, UserEntity>) OrmContext.getOrmManager().getEntityCaches(UserEntity.class);
|
||||
|
||||
// 动态去拿到UserEntity的EntityCaches
|
||||
@SuppressWarnings("unchecked")
|
||||
var mailEntityCaches = (IEntityCache<String, MailEntity>) OrmContext.getOrmManager().getEntityCaches(MailEntity.class);
|
||||
|
||||
for (var i = 1; i <= 10; i++) {
|
||||
var entity = mailEntityCaches.loadOrCreate(String.valueOf(i));
|
||||
entity.setContent("msg:" + i);
|
||||
mailEntityCaches.update(entity);
|
||||
for (var j = 1; j <= 10; j++) {
|
||||
var entity = userEntityCaches.loadOrCreate((long) j);
|
||||
entity.setE(StringUtils.format("update-{}-{}", i, j));
|
||||
entity.setC(j);
|
||||
userEntityCaches.update(entity);
|
||||
}
|
||||
|
||||
ThreadUtils.sleep(60 * TimeUtils.MILLIS_PER_SECOND);
|
||||
}
|
||||
|
||||
ThreadUtils.sleep(60 * TimeUtils.MILLIS_PER_SECOND);
|
||||
|
||||
mailEntityCaches.load("1");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void collectionTest() {
|
||||
var context = new ClassPathXmlApplicationContext("application.xml");
|
||||
|
||||
var collection = OrmContext.getOrmManager().getCollection(UserEntity.class);
|
||||
var result = collection.updateOne(Filters.eq("_id", 1), new Document("$inc", new Document("c", 1L)));
|
||||
System.out.println(result);
|
||||
ThreadUtils.sleep(Long.MAX_VALUE);
|
||||
}
|
||||
|
||||
public void batchInsert() {
|
||||
var listUser = new ArrayList<UserEntity>();
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
/*
|
||||
* 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.entity.wrapper;
|
||||
|
||||
import com.zfoo.orm.anno.EntityCache;
|
||||
import com.zfoo.orm.anno.Id;
|
||||
import com.zfoo.orm.anno.Version;
|
||||
import com.zfoo.orm.model.IEntity;
|
||||
|
||||
/**
|
||||
* @author godotg
|
||||
*/
|
||||
@EntityCache
|
||||
public class DoubleEntity implements IEntity<Double> {
|
||||
|
||||
@Id
|
||||
private double id;
|
||||
|
||||
private String message;
|
||||
|
||||
@Version
|
||||
private long version;
|
||||
|
||||
public DoubleEntity() {
|
||||
}
|
||||
|
||||
public DoubleEntity(double id, String message, long version) {
|
||||
this.id = id;
|
||||
this.message = message;
|
||||
this.version = version;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Double id() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public double getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(double id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
public void setMessage(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
public long getVersion() {
|
||||
return version;
|
||||
}
|
||||
|
||||
public void setVersion(long version) {
|
||||
this.version = version;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "DoubleEntity{" +
|
||||
"id=" + id +
|
||||
", message='" + message + '\'' +
|
||||
", version=" + version +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
/*
|
||||
* 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.entity.wrapper;
|
||||
|
||||
import com.zfoo.orm.anno.EntityCache;
|
||||
import com.zfoo.orm.anno.Id;
|
||||
import com.zfoo.orm.anno.Version;
|
||||
import com.zfoo.orm.model.IEntity;
|
||||
|
||||
/**
|
||||
* @author godotg
|
||||
*/
|
||||
@EntityCache
|
||||
public class FloatEntity implements IEntity<Float> {
|
||||
|
||||
@Id
|
||||
private float id;
|
||||
|
||||
private String message;
|
||||
|
||||
@Version
|
||||
private long version;
|
||||
|
||||
public FloatEntity() {
|
||||
}
|
||||
|
||||
public FloatEntity(float id, String message, long version) {
|
||||
this.id = id;
|
||||
this.message = message;
|
||||
this.version = version;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Float id() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public float getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(float id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
public void setMessage(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
public long getVersion() {
|
||||
return version;
|
||||
}
|
||||
|
||||
public void setVersion(long version) {
|
||||
this.version = version;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "FloatEntity{" +
|
||||
"id=" + id +
|
||||
", message='" + message + '\'' +
|
||||
", version=" + version +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
/*
|
||||
* 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.entity.wrapper;
|
||||
|
||||
import com.zfoo.orm.anno.*;
|
||||
import com.zfoo.orm.model.IEntity;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author godotg
|
||||
*/
|
||||
@EntityCache
|
||||
public class IntEntity implements IEntity<Integer> {
|
||||
|
||||
@Id
|
||||
private int id;
|
||||
|
||||
private String message;
|
||||
|
||||
@Version
|
||||
private long version;
|
||||
|
||||
public IntEntity() {
|
||||
}
|
||||
|
||||
public IntEntity(int id, String message, long version) {
|
||||
this.id = id;
|
||||
this.message = message;
|
||||
this.version = version;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer id() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
public void setMessage(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
public long getVersion() {
|
||||
return version;
|
||||
}
|
||||
|
||||
public void setVersion(long version) {
|
||||
this.version = version;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "IntEntity{" +
|
||||
"id=" + id +
|
||||
", message='" + message + '\'' +
|
||||
", version=" + version +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
/*
|
||||
* 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.entity.wrapper;
|
||||
|
||||
import com.zfoo.orm.anno.EntityCache;
|
||||
import com.zfoo.orm.anno.Id;
|
||||
import com.zfoo.orm.anno.Version;
|
||||
import com.zfoo.orm.model.IEntity;
|
||||
|
||||
/**
|
||||
* @author godotg
|
||||
*/
|
||||
@EntityCache
|
||||
public class LongEntity implements IEntity<Long> {
|
||||
|
||||
@Id
|
||||
private long id;
|
||||
|
||||
private String message;
|
||||
|
||||
@Version
|
||||
private long version;
|
||||
|
||||
public LongEntity() {
|
||||
}
|
||||
|
||||
public LongEntity(long id, String message, long version) {
|
||||
this.id = id;
|
||||
this.message = message;
|
||||
this.version = version;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long id() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
public void setMessage(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
public long getVersion() {
|
||||
return version;
|
||||
}
|
||||
|
||||
public void setVersion(long version) {
|
||||
this.version = version;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "LongEntity{" +
|
||||
"id=" + id +
|
||||
", message='" + message + '\'' +
|
||||
", version=" + version +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
/*
|
||||
* 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.entity.wrapper;
|
||||
|
||||
import com.zfoo.orm.anno.EntityCache;
|
||||
import com.zfoo.orm.anno.Id;
|
||||
import com.zfoo.orm.anno.Version;
|
||||
import com.zfoo.orm.model.IEntity;
|
||||
import org.bson.types.ObjectId;
|
||||
|
||||
/**
|
||||
* @author godotg
|
||||
*/
|
||||
@EntityCache
|
||||
public class ObjectIdEntity implements IEntity<ObjectId> {
|
||||
|
||||
@Id
|
||||
private ObjectId id;
|
||||
|
||||
private String message;
|
||||
|
||||
@Version
|
||||
private long version;
|
||||
|
||||
public ObjectIdEntity() {
|
||||
}
|
||||
|
||||
public ObjectIdEntity(ObjectId id, String message, long version) {
|
||||
this.id = id;
|
||||
this.message = message;
|
||||
this.version = version;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ObjectId id() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public ObjectId getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(ObjectId id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
public void setMessage(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
public long getVersion() {
|
||||
return version;
|
||||
}
|
||||
|
||||
public void setVersion(long version) {
|
||||
this.version = version;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "ObjectIdEntity{" +
|
||||
"id=" + id +
|
||||
", message='" + message + '\'' +
|
||||
", version=" + version +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
/*
|
||||
* 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.entity.wrapper;
|
||||
|
||||
import com.zfoo.orm.anno.EntityCache;
|
||||
import com.zfoo.orm.anno.Id;
|
||||
import com.zfoo.orm.anno.Version;
|
||||
import com.zfoo.orm.model.IEntity;
|
||||
|
||||
/**
|
||||
* @author godotg
|
||||
*/
|
||||
@EntityCache
|
||||
public class StringEntity implements IEntity<String> {
|
||||
|
||||
@Id
|
||||
private String id;
|
||||
|
||||
private String message;
|
||||
|
||||
@Version
|
||||
private long version;
|
||||
|
||||
public StringEntity() {
|
||||
}
|
||||
|
||||
public StringEntity(String id, String message, long version) {
|
||||
this.id = id;
|
||||
this.message = message;
|
||||
this.version = version;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String id() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
public void setMessage(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
public long getVersion() {
|
||||
return version;
|
||||
}
|
||||
|
||||
public void setVersion(long version) {
|
||||
this.version = version;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "StringEntity{" +
|
||||
"id=" + id +
|
||||
", message='" + message + '\'' +
|
||||
", version=" + version +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
@@ -44,7 +44,7 @@ public abstract class FieldUtils {
|
||||
clazz.getDeclaredMethod(methodName);
|
||||
return methodName;
|
||||
} catch (NoSuchMethodException e) {
|
||||
throw new RunException("field:[{}] has no getMethod or isMethod in class:[{}]", field.getName(), clazz.getCanonicalName());
|
||||
throw new RunException("field:[{}] has no get or set method in class:[{}]", field.getName(), clazz.getCanonicalName());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user