perf[orm]: 简化代码

This commit is contained in:
jaysunxiao
2021-07-05 22:32:43 +08:00
parent c1481c9479
commit 079617f25c
10 changed files with 45 additions and 134 deletions
+15 -1
View File
@@ -56,4 +56,18 @@ cron表达式的用法
spring自带的Scheduled和zfoo的Scheduler
java的线程池,SingleThreadScheduledExecutorSingleThreadExecutor
通过spring自带的CronExpression计算下一次cron表达式要执行的时间
```
```
### [第六讲](https://www.bilibili.com/video/BV1to4y1C77Y)
- scheduler原理讲解
- 知识点
```
Mysql中的数据库,表,一条数据,在Mongodb分别叫做,数据库,集合,文档
Elastic Search的全文搜索引擎
Java的缓存框架guava cache,还有它的升级版本caffeine
mongodb的map reduce操作
分布式唯一Id,Java自带的UUID,雪花算法(SnowFlake),Redis分布式唯一Id实现,Mongodb分布式唯一Id实现,Zookeeper分布式唯一Id实现
```
@@ -1,53 +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.UserEntity;
import com.zfoo.orm.model.cache.IEntityCaches;
import com.zfoo.util.ThreadUtils;
import org.junit.Ignore;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author jaysunxiao
* @version 3.0
*/
@Ignore
public class DeleteTest {
private static final Logger logger = LoggerFactory.getLogger(InsertTest.class);
@Test
public void test() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
IEntityCaches<Long, UserEntity> userEntityCaches = (IEntityCaches<Long, UserEntity>) OrmContext.getOrmManager().getEntityCaches(UserEntity.class);
for (int i = 0; i < 10; i++) {
UserEntity userEntity = new UserEntity(i, (byte) 1, (short) i, i, true, "helloOrm" + i, "helloOrm" + i);
OrmContext.getAccessor().delete(userEntity);
logger.debug("thread2执行移除[{}]", i);
}
ThreadUtils.sleep(Long.MAX_VALUE);
}
}
@@ -22,7 +22,6 @@ import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
/**
@@ -34,8 +33,8 @@ public class IndexTextTest {
@Test
public void insertTest() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
List<UserEntity> listUser = new ArrayList<>();
var context = new ClassPathXmlApplicationContext("application.xml");
var listUser = new ArrayList<UserEntity>();
var userEntity = new UserEntity(1, (byte) 1, (short) 1, 1, true, "两个黄鹂鸣翠柳,一行白鹭上青天。窗含西岭千秋雪,门泊东吴万里船。", null);
listUser.add(userEntity);
userEntity = new UserEntity(2, (byte) 2, (short) 2, 2, true, "床前明月光,疑是地上霜。 举头望明月,低头思故乡。", null);
@@ -49,7 +48,7 @@ public class IndexTextTest {
@Test
public void queryTest() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
var context = new ClassPathXmlApplicationContext("application.xml");
var collection = OrmContext.getOrmManager().getCollection(UserEntity.class);
collection.find(Filters.text("窗含西岭千秋雪")).forEach(new Consumer<UserEntity>() {
@Override
@@ -1,46 +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.UserEntity;
import com.zfoo.orm.model.cache.IEntityCaches;
import com.zfoo.util.ThreadUtils;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author jaysunxiao
* @version 3.0
*/
@Ignore
public class InsertTest {
@SuppressWarnings("unchecked")
@Test
public void test() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
IEntityCaches<Long, UserEntity> userEntityCaches = (IEntityCaches<Long, UserEntity>) OrmContext.getOrmManager().getEntityCaches(UserEntity.class);
for (int i = 0; i < 10; i++) {
UserEntity userEntity = new UserEntity(i, (byte) 1, (short) i, i, true, "helloOrm" + i, "helloOrm" + i);
OrmContext.getAccessor().insert(userEntity);
}
ThreadUtils.sleep(Long.MAX_VALUE);
}
}
@@ -35,7 +35,7 @@ public class TransactionTest {
@Test
public void transactionTest() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
var context = new ClassPathXmlApplicationContext("application.xml");
/* Step 1: Start a client session. */
var clientSession = OrmContext.getOrmManager().getClientSession();
@@ -71,6 +71,7 @@ public class TransactionTest {
clientSession.withTransaction(txnBody, txnOptions);
} catch (RuntimeException e) {
// some error handling
e.printStackTrace();
} finally {
clientSession.close();
}
@@ -1,6 +1,5 @@
/*
* 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
*
@@ -9,9 +8,10 @@
* 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;
package com.zfoo.orm.cache;
import com.mongodb.client.model.Filters;
import com.zfoo.orm.OrmContext;
@@ -29,20 +29,21 @@ import org.springframework.context.support.ClassPathXmlApplicationContext;
* @version 3.0
*/
@Ignore
public class UpdateTest {
public class EntityCachesTest {
@Test
public void test() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
var context = new ClassPathXmlApplicationContext("application.xml");
IEntityCaches<Long, UserEntity> userEntityCaches = (IEntityCaches<Long, UserEntity>) OrmContext.getOrmManager().getEntityCaches(UserEntity.class);
// 动态去拿到UserEntity的EntityCaches
var userEntityCaches = (IEntityCaches<Long, UserEntity>) OrmContext.getOrmManager().getEntityCaches(UserEntity.class);
for (int i = 0; i < 10; i++) {
var userEnt = userEntityCaches.load((long) i);
userEnt.setE("update" + i);
userEnt.setC(i);
userEntityCaches.update(userEnt);
for (var i = 1; i <= 10; i++) {
var entity = userEntityCaches.load((long) i);
entity.setE("update" + i);
entity.setC(i);
userEntityCaches.update(entity);
}
ThreadUtils.sleep(Long.MAX_VALUE);
@@ -51,7 +52,7 @@ public class UpdateTest {
@Test
public void collectionTest() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
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)));
+6 -8
View File
@@ -28,18 +28,16 @@ public class OrmTest {
@Test
public void test() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
var context = new ClassPathXmlApplicationContext("application.xml");
UserManager mainOrm = context.getBean(UserManager.class);
// 通过注解自动注入的方式去拿到UserEntity的EntityCaches
var userEntityCaches = context.getBean(UserManager.class).userEntityCaches;
for (int i = 0; i <= 10; i++) {
var entity = mainOrm.getEntityCaches().load((long) i);
entity.setA((byte) i);
entity.setB((short) i);
var entity = userEntityCaches.load((long) i);
entity.setE("update" + i);
entity.setC(i);
entity.setD(true);
entity.setE("helloOrm" + i);
mainOrm.getEntityCaches().update(entity);
userEntityCaches.update(entity);
}
OrmContext.getOrmManager().getAllEntityCaches().forEach(it -> System.out.println(it.recordStatus()));
+1 -5
View File
@@ -26,10 +26,6 @@ import org.springframework.stereotype.Component;
public class UserManager {
@EntityCachesInjection
private IEntityCaches<Long, UserEntity> entityCaches;
public IEntityCaches<Long, UserEntity> getEntityCaches() {
return entityCaches;
}
public IEntityCaches<Long, UserEntity> userEntityCaches;
}
@@ -19,7 +19,6 @@ import org.junit.Ignore;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.List;
/**
* @author jaysunxiao
@@ -30,15 +29,15 @@ public class QueryTest {
@Test
public void queryAllTest() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
List<UserEntity> list = OrmContext.getQuery().queryAll(UserEntity.class);
var context = new ClassPathXmlApplicationContext("application.xml");
var list = OrmContext.getQuery().queryAll(UserEntity.class);
System.out.println(list);
}
@Test
public void queryByFieldTest() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
List<UserEntity> list = OrmContext.getQuery().queryFieldEqual("a", 1, UserEntity.class);
var context = new ClassPathXmlApplicationContext("application.xml");
var list = OrmContext.getQuery().queryFieldEqual("a", 1, UserEntity.class);
System.out.println(list);
}
@@ -19,6 +19,8 @@ import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* MongoDB分布式唯一Id测试
*
* @author jaysunxiao
* @version 3.0
*/