Skip to content

Commit 23619c3

Browse files
砍掉一个中间变量,缓存优化实现,反射解析放入了xml解析中,编译进一步得到提升
1 parent de97b93 commit 23619c3

File tree

17 files changed

+178
-274
lines changed

17 files changed

+178
-274
lines changed

src/main/java/com/midream/sheep/swcj/core/build/builds/javanative/BuildTool.java

Lines changed: 17 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,16 @@
33
import com.midream.sheep.swcj.annotation.Param;
44
import com.midream.sheep.swcj.annotation.RequestType;
55
import com.midream.sheep.swcj.annotation.WebSpider;
6-
import com.midream.sheep.swcj.Exception.ConfigException;
7-
import com.midream.sheep.swcj.Exception.EmptyMatchMethodException;
86
import com.midream.sheep.swcj.Exception.InterfaceIllegal;
97
import com.midream.sheep.swcj.cache.CacheCorn;
108
import com.midream.sheep.swcj.data.Constant;
119
import com.midream.sheep.swcj.data.ReptileConfig;
1210
import com.midream.sheep.swcj.pojo.buildup.MethodHandler;
11+
import com.midream.sheep.swcj.pojo.buildup.MethodMeta;
1312
import com.midream.sheep.swcj.pojo.enums.ChooseStrategy;
1413
import com.midream.sheep.swcj.pojo.swc.ReptileUrl;
15-
import com.midream.sheep.swcj.pojo.swc.RootReptile;
1614
import com.midream.sheep.swcj.pojo.buildup.SWCJClass;
1715
import com.midream.sheep.swcj.pojo.buildup.SWCJMethod;
18-
import com.midream.sheep.swcj.pojo.swc.passvalue.ReptlileMiddle;
1916
import com.midream.sheep.swcj.util.function.StringUtil;
2017

2118
import java.lang.reflect.Method;
@@ -24,15 +21,11 @@
2421
import java.util.concurrent.atomic.AtomicInteger;
2522
import java.util.logging.Logger;
2623

27-
import static com.midream.sheep.swcj.util.function.StringUtil.add;
28-
2924
/**
3025
* 构建工具类
3126
* @author midreamsheep
3227
*/
3328
public class BuildTool {
34-
/**原子性自增数*/
35-
private static final AtomicInteger t = new AtomicInteger(0);
3629
/**
3730
* 获取爬虫的具体类
3831
* @param className 类名
@@ -41,40 +34,13 @@ public class BuildTool {
4134
public static Object getObjectFromTool(String className) {
4235
return CacheCorn.SPIDER_CACHE.getCacheSpider(className);
4336
}
44-
/**
45-
* 获取爬虫实体类
46-
* @param middle 传递配置包
47-
* @return 爬虫实体类
48-
* */
49-
public static SWCJClass getSWCJClass(ReptlileMiddle middle) throws ConfigException, EmptyMatchMethodException {
50-
RootReptile rootReptile = middle.getRootReptile();
51-
//实例化类
52-
SWCJClass swcjClass = SWCJClass.buildClass();
53-
//设置接口,类名
54-
swcjClass.setClassName("swcj" + (t.addAndGet(1)));
55-
swcjClass.setItIterface(rootReptile.getParentInter());
56-
//效验接口是否有方法,并注入方法
57-
try {
58-
getFunction(swcjClass, rootReptile, middle.getConfig());
59-
} catch (ClassNotFoundException e) {
60-
Logger.getLogger(BuildTool.class.getName()).severe(e.getMessage());
61-
throw new ConfigException("你的接口不存在:" + rootReptile.getParentInter());
62-
} catch (InterfaceIllegal e) {
63-
Logger.getLogger(BuildTool.class.getName()).severe(e.getMessage());
64-
throw new RuntimeException(e);
65-
}
66-
if (swcjClass.getMethods() == null || swcjClass.getMethods().size() == 0) {
67-
throw new EmptyMatchMethodException("EmptyMatchMethodException(空匹配方法异常)");
68-
}
69-
return swcjClass;
70-
}
7137
/**
7238
* 为爬虫实体类注入方法数据
7339
* @param swcjClass 爬虫实体类
74-
* @param rootReptile 爬虫实体数据
40+
* @param rus 爬虫配置
7541
* @param config 爬虫配置数据
7642
* */
77-
private static void getFunction(SWCJClass swcjClass, RootReptile rootReptile, ReptileConfig config) throws ClassNotFoundException, InterfaceIllegal {
43+
public static void getFunction(SWCJClass swcjClass, MethodMeta meta, ReptileConfig config,List<ReptileUrl> rus) throws ClassNotFoundException, InterfaceIllegal {
7844
Method[] methods = Class.forName(swcjClass.getItIterface()).getMethods();
7945
for (Method method : methods) {
8046
//实例化方法类
@@ -100,29 +66,29 @@ private static void getFunction(SWCJClass swcjClass, RootReptile rootReptile, Re
10066
swcjMethod.setRequestType(method.getAnnotation(RequestType.class) == null ? "GET" : method.getAnnotation(RequestType.class).value().getValue());
10167
swcjMethod.setReturnType(Constant.getClassName(method.getReturnType().toString()));
10268
if (config.getChoice() == ChooseStrategy.ANNOTATION) {
103-
analysisMethodByAnnotation(swcjMethod, method, rootReptile,config, swcjClass);
69+
analysisMethodByAnnotation(swcjMethod, method, rus,config, swcjClass,meta);
10470
} else if (config.getChoice() == ChooseStrategy.METHOD_NAME) {
105-
analysisMethodByMethodName(swcjMethod, method, rootReptile,config, swcjClass);
71+
analysisMethodByMethodName(swcjMethod, method, rus,config, swcjClass,meta);
10672
}
10773
}
10874
}
10975
/**
11076
* 通过注解解析方法
11177
* @param swcjMethod SWCJ方法实体类
11278
* @param method java反射方法对象
113-
* @param rootReptile 爬虫实体数据
79+
* @param rus 爬虫配置数据
11480
* @param swcjClass 爬虫实体类
11581
* */
116-
private static void analysisMethodByAnnotation(SWCJMethod swcjMethod, Method method, RootReptile rootReptile,ReptileConfig reptileConfig, SWCJClass swcjClass) throws InterfaceIllegal {
82+
private static void analysisMethodByAnnotation(SWCJMethod swcjMethod, Method method, List<ReptileUrl> rus,ReptileConfig reptileConfig, SWCJClass swcjClass,MethodMeta meta) throws InterfaceIllegal {
11783
//获取方法上的注解
11884
WebSpider spider = method.getAnnotation(WebSpider.class);
11985
//放入所有有注解的方法
12086
if (spider == null || spider.value().equals(Constant.nullString)) {
12187
throw new InterfaceIllegal("InterfaceMethodIllegal(接口方法不合法,请定义注解)");
12288
}
123-
for (ReptileUrl url : rootReptile.getRu()) {
89+
for (ReptileUrl url : rus) {
12490
if(url.getName().equals(spider.value())){
125-
parsePublicArea(swcjMethod, url, rootReptile,reptileConfig);
91+
parsePublicArea(swcjMethod, url, meta,reptileConfig);
12692
swcjClass.addMethod(spider.value(), swcjMethod);
12793
}
12894
}
@@ -131,34 +97,33 @@ private static void analysisMethodByAnnotation(SWCJMethod swcjMethod, Method met
13197
* 通过方法名解析方法
13298
* @param swcjMethod SWCJ方法实体类
13399
* @param method java反射方法对象
134-
* @param rootReptile 爬虫实体数据
100+
* @param meta 方法源数据
135101
* @param swcjClass 爬虫实体类
136102
* */
137-
private static void analysisMethodByMethodName(SWCJMethod swcjMethod, Method method, RootReptile rootReptile,ReptileConfig reptileConfig ,SWCJClass swcjClass) {
138-
for (ReptileUrl url : rootReptile.getRu()) {
103+
private static void analysisMethodByMethodName(SWCJMethod swcjMethod, Method method,List<ReptileUrl> rus,ReptileConfig reptileConfig ,SWCJClass swcjClass,MethodMeta meta) {
104+
for (ReptileUrl url : rus) {
139105
if(url.getName().equals(method.getName())){
140-
parsePublicArea(swcjMethod, url, rootReptile,reptileConfig);
106+
parsePublicArea(swcjMethod, url, meta,reptileConfig);
141107
swcjClass.addMethod(method.getName(), swcjMethod);
142108
return;
143109
}
144110
}
145111
}
146112

147-
private static void parsePublicArea(SWCJMethod swcjMethod,ReptileUrl url, RootReptile rootReptile,ReptileConfig reptileConfig) {
113+
private static void parsePublicArea(SWCJMethod swcjMethod,ReptileUrl url, MethodMeta meta,ReptileConfig reptileConfig) {
148114
swcjMethod.setName(url.getName());
149115
List<String> vars = swcjMethod.getExecuteVars();
150-
swcjMethod.setParamIn(getMethodParametric(url, swcjMethod,vars).replace("class",Constant.nullString));
151-
swcjMethod.setExecuteStr(StringUtil.getExecuteCharacter(url,vars,reptileConfig,rootReptile,swcjMethod));
116+
swcjMethod.setParamIn(getMethodParametric(swcjMethod,vars).replace("class",Constant.nullString));
117+
swcjMethod.setExecuteStr(StringUtil.getExecuteCharacter(url,vars,reptileConfig,meta,swcjMethod));
152118

153119
}
154120
/**
155121
* 获取方法参数列表
156-
* @param ru 爬虫方法实体数据
157122
* @param method 方法实体类
158123
* @param injection 参数列表
159124
* @return 参数列表字符串
160125
* */
161-
public static String getMethodParametric(ReptileUrl ru, SWCJMethod method, List<String> injection) {
126+
public static String getMethodParametric(SWCJMethod method, List<String> injection) {
162127
//获取拼接对象
163128
StringBuilder sb = new StringBuilder();
164129
//获取方法参数和输入参数

src/main/java/com/midream/sheep/swcj/core/build/builds/javanative/ReptilesBuilder.java

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,31 +13,22 @@
1313

1414
public class ReptilesBuilder extends SWCJBuilderAbstract {
1515

16-
public Object loadClass(SWCJClass sclass,ReptlileMiddle middle) {
16+
public Object loadClass(ReptlileMiddle middle) {
1717
try {
18-
DataInComplier data = swcjCompiler.compileAndLoad(sclass,middle);
18+
DataInComplier data = swcjCompiler.compileAndLoad(middle.getSwcjClass(),middle);
1919
if(data.isIsload()){
2020
return data.getaClass().getDeclaredConstructor().newInstance();
2121
}
22-
return swcjcl.loadData(Constant.DEFAULT_PACKAGE_NAME + "." + sclass.getClassName(), data.getDatas()).getDeclaredConstructor().newInstance();
22+
return swcjcl.loadData(Constant.DEFAULT_PACKAGE_NAME + "." + middle.getSwcjClass().getClassName(), data.getDatas()).getDeclaredConstructor().newInstance();
2323
} catch (ClassNotFoundException | NoSuchMethodException | InstantiationException | IllegalAccessException |
2424
InvocationTargetException e) {
2525
Logger.getLogger(ReptilesBuilder.class.getName()).info(e.getMessage());
2626
throw new RuntimeException(e);
2727
}
2828
}
2929

30-
public SWCJClass getSWCJClass(ReptlileMiddle middle) throws ClassNotFoundException, EmptyMatchMethodException, ConfigException {
31-
return BuildTool.getSWCJClass(middle);
32-
}
33-
3430
@Override
3531
protected Object buildObject(ReptlileMiddle middle) {
36-
try {
37-
return loadClass(getSWCJClass(middle),middle);
38-
} catch (ClassNotFoundException | EmptyMatchMethodException | ConfigException e) {
39-
Logger.getLogger(ReptilesBuilder.class.getName()).info(e.getMessage());
40-
throw new RuntimeException(e);
41-
}
32+
return loadClass(middle);
4233
}
4334
}

src/main/java/com/midream/sheep/swcj/core/build/inter/SWCJBuilderAbstract.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public Object Builder(ReptlileMiddle middle) {
2525
//非空判断
2626
notNull();
2727
//开始拼接类信息
28-
return CacheCorn.SPIDER_CACHE.addCacheSpider(middle.getRootReptile().getId(),buildObject(middle));
28+
return CacheCorn.SPIDER_CACHE.addCacheSpider(middle.getSwcjClass().getId(),buildObject(middle));
2929
}
3030
private void notNull(){
3131
if(swcjcl==null){

src/main/java/com/midream/sheep/swcj/core/classtool/compiler/javanative/DynamicCompiler.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,8 @@ private String getWholeClassString(SWCJClass swcjClass){
7777
}
7878
private SWCJClass getAllMethod(SWCJClass swcjClass, ReptlileMiddle middle) {
7979
Map<String, SWCJMethod> function = swcjClass.getMethods();
80-
for (ReptileUrl reptileUrl : middle.getRootReptile().getRu()) {
81-
SWCJMethod method = function.get(reptileUrl.getName());
82-
if (method == null) {
83-
continue;
84-
}
85-
method.setExecuteStr(BuildTool.spliceMethod(method));
80+
for (Map.Entry<String, SWCJMethod> entry : function.entrySet()) {
81+
entry.getValue().setExecuteStr(BuildTool.spliceMethod(entry.getValue()));
8682
}
8783
return swcjClass;
8884
}

src/main/java/com/midream/sheep/swcj/core/factory/SWCJAbstractFactory.java

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@
1010
import com.midream.sheep.swcj.core.classtool.classloader.SWCJClassLoader;
1111
import com.midream.sheep.swcj.core.classtool.classloader.SWCJClassLoaderInter;
1212
import com.midream.sheep.swcj.core.classtool.compiler.SWCJCompiler;
13+
import com.midream.sheep.swcj.core.factory.parse.bystr.BetterXmlParseTool;
14+
import com.midream.sheep.swcj.core.factory.parse.bystr.SWCJParseI;
1315
import com.midream.sheep.swcj.data.Constant;
1416
import com.midream.sheep.swcj.data.ReptileConfig;
15-
import com.midream.sheep.swcj.pojo.swc.RootReptile;
17+
import com.midream.sheep.swcj.pojo.buildup.SWCJClass;
1618
import com.midream.sheep.swcj.pojo.swc.passvalue.ReptlileMiddle;
1719
import com.midream.sheep.swcj.util.function.StringUtil;
1820

@@ -30,10 +32,8 @@
3032
public abstract class SWCJAbstractFactory implements SWCJXmlFactory{
3133
//核心配置文件
3234
public static volatile ReptileConfig config = new ReptileConfig();
33-
//是否已经已经读取过缓存
34-
private static volatile boolean isCache = false;
3535
//爬虫文件
36-
protected Map<String, RootReptile> rootReptiles = new HashMap<>();
36+
protected Map<String, SWCJClass> swcjClasses = new HashMap<>();
3737
//构造器
3838
protected SWCJBuilder swcjBuilder = null;
3939
//解析器
@@ -54,7 +54,7 @@ public Object getWebSpiderById(String id) throws ConfigException, EmptyMatchMeth
5454
if (object != null) {
5555
return object;
5656
}
57-
RootReptile rootReptile = rootReptiles.get(id);
57+
SWCJClass rootReptile = swcjClasses.get(id);
5858
if(rootReptile==null){
5959
try {
6060
TimeUnit.MILLISECONDS.sleep(200);
@@ -74,9 +74,9 @@ public Object getWebSpiderById(String id) throws ConfigException, EmptyMatchMeth
7474
Logger.getLogger(SWCJAbstractFactory.class.getName()).warning("线程暂停失败");
7575
}
7676
}else {
77-
rootReptiles.get(id).setLoad(true);
78-
rootReptiles.remove(rootReptiles.get(id));
79-
return swcjBuilder.Builder(new ReptlileMiddle(rootReptiles.get(id), config));
77+
swcjClasses.get(id).setLoad(true);
78+
swcjClasses.remove(swcjClasses.get(id));
79+
return swcjBuilder.Builder(new ReptlileMiddle(swcjClasses.get(id), config));
8080
}
8181
}
8282
}
@@ -115,13 +115,9 @@ public SWCJXmlFactory setClassLoader(SWCJClassLoaderInter classLoader) {
115115
return null;
116116
}
117117
//读取配置文件中的对象
118-
protected void cache() throws ConfigException {
119-
if(isCache){
120-
return;
121-
}
122-
String workplace = config.getWorkplace();
118+
protected void cache(String workplace) throws ConfigException {
123119
//通过io流读取文件
124-
File file = new File(config.getWorkplace()+"/ClassCatch.swcj");
120+
File file = new File(workplace+"/ClassCatch.swcj");
125121
if(!file.exists()) {
126122
return;
127123
}
@@ -146,14 +142,14 @@ protected void cache() throws ConfigException {
146142
//将类放入池中
147143
CacheCorn.SPIDER_CACHE.addCacheSpider(id, classLoader.loadData(className, bytesByStream).getDeclaredConstructor().newInstance());
148144
//删除id为id的的rootReptile
149-
rootReptiles.remove(id);
145+
swcjClasses.remove(id);
150146
} catch (FileNotFoundException e) {
151147
throw new ConfigException("找不到缓存文件");
152148
} catch (InvocationTargetException | InstantiationException | IllegalAccessException |
153149
NoSuchMethodException e) {
154150
throw new RuntimeException("加载类异常");
155151
}
156152
}
157-
isCache=true;
153+
SWCJParseI.count.addAndGet(key_value.split("\n").length);
158154
}
159155
}

src/main/java/com/midream/sheep/swcj/core/factory/SWCJParseI.java

Lines changed: 0 additions & 23 deletions
This file was deleted.

src/main/java/com/midream/sheep/swcj/core/factory/SWCJXmlFactory.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.midream.sheep.swcj.core.build.inter.SWCJBuilder;
77
import com.midream.sheep.swcj.core.classtool.classloader.SWCJClassLoaderInter;
88
import com.midream.sheep.swcj.core.classtool.compiler.SWCJCompiler;
9+
import com.midream.sheep.swcj.core.factory.parse.bystr.SWCJParseI;
910
import org.xml.sax.SAXException;
1011

1112
import javax.xml.parsers.ParserConfigurationException;

0 commit comments

Comments
 (0)