Skip to content

Commit 7b50848

Browse files
committed
Server:去除第二次依赖解析,性能大幅提升
1 parent 5e8e06c commit 7b50848

1 file changed

Lines changed: 120 additions & 106 deletions

File tree

  • APIJSON(Server)/APIJSON(Eclipse_JEE)/src/main/java/zuo/biao/apijson/server

‎APIJSON(Server)/APIJSON(Eclipse_JEE)/src/main/java/zuo/biao/apijson/server/Parser.java‎

Lines changed: 120 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public Parser(RequestMethod requestMethod) {
8383

8484
private boolean parseRelation;
8585
//不用keyPath-valuePath-value是因为很可能很多valuePath对应同一个value
86-
private Map<String, String> keyValuePathMap;//keyPath-valuePath
86+
// private Map<String, String> keyValuePathMap;//keyPath-valuePath
8787

8888

8989
/**解析请求json并获取对应结果
@@ -127,7 +127,7 @@ public JSONObject parseResponse(String request) {
127127
public JSONObject parseResponse(JSONObject request) {
128128
final String requestString = JSON.toJSONString(request);//request传进去解析后已经变了
129129

130-
keyValuePathMap = new HashMap<String, String>();
130+
// keyValuePathMap = new HashMap<String, String>();
131131
queryResultMap = new HashMap<String, Object>();
132132
parseRelation = false;
133133

@@ -136,15 +136,15 @@ public JSONObject parseResponse(JSONObject request) {
136136
try {
137137
requestObject = getObject(null, null, null, request);
138138

139-
if (keyValuePathMap.isEmpty() == false) {//优化性能,没有依赖引用的就不用再遍历了
140-
Log.d(TAG, "\n\n\n parseResponse keyValuePathMap.isEmpty() == false"
141-
+ "\n parseRelation = true; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< \n\n");
142-
143-
parseRelation = true;
144-
requestObject = getObject(null, null, null, requestObject);
145-
146-
Log.d(TAG, "\n\n parseRelation = true; >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n\n\n");
147-
}
139+
// if (keyValuePathMap.isEmpty() == false) {//优化性能,没有依赖引用的就不用再遍历了
140+
// Log.d(TAG, "\n\n\n parseResponse keyValuePathMap.isEmpty() == false"
141+
// + "\n parseRelation = true; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< \n\n");
142+
//
143+
// parseRelation = true;
144+
// requestObject = getObject(null, null, null, requestObject);
145+
//
146+
// Log.d(TAG, "\n\n parseRelation = true; >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n\n\n");
147+
// }
148148
} catch (Exception e) {
149149
e.printStackTrace();
150150
error = e;
@@ -160,7 +160,7 @@ public JSONObject parseResponse(JSONObject request) {
160160
: extendResult(requestObject, 206, "未完成全部请求:\n" + error.getMessage());
161161
// }
162162

163-
keyValuePathMap.clear();;
163+
// keyValuePathMap.clear();;
164164
queryResultMap.clear();
165165

166166
Log.d(TAG, "\n\n\n\n<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n "
@@ -491,7 +491,8 @@ private JSONObject getObject(String parentPath, final QueryConfig parentConfig,
491491
final String path = getAbsPath(parentPath, name);
492492
if (parseRelation //优化性能:第二遍遍历时如果内部没有依赖引用其它就跳过
493493
&& StringUtil.isNotEmpty(path, true)//避免最外层返回true而不能parseRelation
494-
&& isInRelationMap(path) == false) {
494+
// && isInRelationMap(path) == false
495+
) {
495496
return request;
496497
}
497498
//为第二遍parseRelation = true服务,优化查询性能并避免"[]":{"0":{"1":{}}}这种导致第3层当成[]的直接子Object
@@ -609,29 +610,8 @@ && isInRelationMap(path) == false) {
609610
if (valuePath.startsWith(SEPARATOR)) {
610611
valuePath = getAbsPath(parentPath, valuePath);
611612
} else {//处理[] -> []/i
612-
String[] ps = StringUtil.split(parentPath, "]/");//"[]/");
613-
614-
if (ps != null && ps.length > 1) {
615-
String[] vs = StringUtil.split(valuePath, "]/");
616-
617-
if (vs != null && vs.length > 0) {
618-
String pos;
619-
for (int i = 0; i < ps.length - 1; i++) {
620-
if (ps[i] == null || ps[i].equals(vs[i]) == false) {//允许""?
621-
break;
622-
}
623-
624-
pos = ps[i+1].contains("/") == false ? ps[i+1]
625-
: ps[i+1].substring(0, ps[i+1].indexOf("/"));
626-
if (
627-
//StringUtil.isNumer(pos) &&
628-
vs[i+1].startsWith(pos + "/") == false) {
629-
vs[i+1] = pos + "/" + vs[i+1];
630-
}
631-
}
632-
valuePath = StringUtil.getString(vs, "]/");
633-
}
634-
}
613+
valuePath = replaceArrayChildPath(parentPath, valuePath);
614+
value = new String(valuePath);//避免后续重新转换
635615
}
636616
//先尝试获取,尽量保留缺省依赖路径,这样就不需要担心路径改变
637617
Object target = getValueByPath(valuePath);
@@ -655,7 +635,7 @@ && isInRelationMap(path) == false) {
655635
+ " >> containRelation = true; putRelation(keyPath, valuePath);");
656636
containRelation = true;
657637
//TODO 上面用实时替换[] -> []/i
658-
putRelation(keyPath, valuePath);
638+
// putRelation(keyPath, valuePath);
659639
}
660640
} else {//直接替换原来的key@:path为key:target
661641
Log.i(TAG, "getObject >> key = replaceKey; value = target;");
@@ -683,7 +663,9 @@ && isInRelationMap(path) == false) {
683663
boolean query = false;
684664
//执行SQL操作数据库
685665
if (containRelation == false && isTableKey) {//提高性能
686-
if (parseRelation == false || isInRelationMap(path)) {//避免覆盖原来已经获取的
666+
if (parseRelation == false
667+
// || isInRelationMap(path)
668+
) {//避免覆盖原来已经获取的
687669
query = true;
688670
// keyValuePathMap.remove(path);
689671
//移除所有startswith path的keyPath?
@@ -701,7 +683,7 @@ && isInRelationMap(path) == false) {
701683
} catch (Exception e) {
702684
Log.e(TAG, "getObject try { transferredRequest = getSQLObject(config2); } catch (Exception e) {");
703685
if (e instanceof NotExistException) {//非严重异常,有时候只是数据不存在
704-
e.printStackTrace();
686+
// e.printStackTrace();
705687
transferredRequest = null;//内部吃掉异常,put到最外层
706688
// requestObject.put(JSONResponse.KEY_MESSAGE
707689
// , StringUtil.getString(requestObject.get(JSONResponse.KEY_MESSAGE)
@@ -822,10 +804,10 @@ private JSONObject getArray(String parentPath, String name, JSONObject request)
822804
// updateRelation(path, getAbsPath(path, "" + i));//request结构已改变,需要更新依赖关系
823805
}
824806

825-
if (isInRelationMap(path)) {
826-
transferredRequest.put(JSONRequest.KEY_COUNT, count);
827-
transferredRequest.put(JSONRequest.KEY_PAGE, page);
828-
}
807+
// if (isInRelationMap(path)) {
808+
// transferredRequest.put(JSONRequest.KEY_COUNT, count);
809+
// transferredRequest.put(JSONRequest.KEY_PAGE, page);
810+
// }
829811
} else {
830812
boolean isArrayKey;
831813
for (String key : set) {//0:{},1:{}...
@@ -943,78 +925,110 @@ private String getAbsPath(String path, String name) {
943925
Log.i(TAG, "getPath return " + path + " >>>>>>>>>>>>>>>>");
944926
return path;
945927
}
946-
947-
//依赖引用关系 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
948-
949-
/**有关联代码的object的关联key在relationMap里
950-
* @param keyPath
951-
* @return
952-
*/
953-
private boolean isInRelationMap(String keyPath) {
954-
if (keyPath == null) {
955-
return false;
956-
}
957-
// return keyValuePathMap == null ? false : keyValuePathMap.containsKey(path);
958-
Set<String> set = keyValuePathMap == null ? null : keyValuePathMap.keySet();
959-
if (set != null) {
960-
for (String key : set) {
961-
if (keyPath.equals(key) || key.startsWith(keyPath + "/")) {//解决相同字符导致的错误) {//key.contains(path)) {//
962-
return true;
963-
}
964-
}
965-
}
966-
return false;
967-
}
968-
969-
970-
/**新增keyPath-valuePath关联
971-
* @param keyPath
928+
929+
/**替换[] -> []/i
930+
* 不能写在getAbsPath里,因为name不一定是依赖路径
931+
* @param parentPath
972932
* @param valuePath
973-
*/
974-
private void putRelation(String keyPath, String valuePath) {
975-
keyValuePathMap.put(keyPath, valuePath);
976-
}
977-
/**根据keyPath获取valuePath
978-
* @param keyPath
979933
* @return
980934
*/
981-
private String getRelationValuePath(String keyPath) {
982-
return keyValuePathMap.get(keyPath);
983-
}
984-
/**移除keyPath-valuePath关联
985-
* @param keyPath
986-
*/
987-
private void removeRelation(String keyPath) {
988-
keyValuePathMap.remove(keyPath);
989-
}
990-
935+
private String replaceArrayChildPath(String parentPath, String valuePath) {
936+
String[] ps = StringUtil.split(parentPath, "]/");//"[]/");
937+
if (ps != null && ps.length > 1) {
938+
String[] vs = StringUtil.split(valuePath, "]/");
939+
940+
if (vs != null && vs.length > 0) {
941+
String pos;
942+
for (int i = 0; i < ps.length - 1; i++) {
943+
if (ps[i] == null || ps[i].equals(vs[i]) == false) {//允许""?
944+
break;
945+
}
991946

992-
/**
993-
* @param path
994-
* @param replacePath
995-
*/
996-
private void updateRelation(String path, String replacePath) {
997-
//更新关系path中对应改变字段
998-
Set<String> relationSet = replacePath == null || keyValuePathMap == null ? null : keyValuePathMap.keySet();
999-
if (relationSet != null) {
1000-
path = StringUtil.getString(path);
1001-
1002-
String relationValue;
1003-
for (String relationKey : relationSet) {
1004-
if (relationKey == null || relationKey.startsWith(replacePath) == false) {
1005-
continue;
1006-
}
1007-
relationValue = keyValuePathMap.get(relationKey);
1008-
if (relationValue != null && relationValue.startsWith(path)
1009-
&& relationValue.startsWith(replacePath) == false) {
1010-
//用replace会将所有符合字符替换! []/Comment[] -> []/0/Comment[]/0, replaceFirst因[]特殊字符崩溃
1011-
relationValue = replacePath + relationValue.substring(path.length());
1012-
keyValuePathMap.put(relationKey, relationValue);
947+
pos = ps[i+1].contains("/") == false ? ps[i+1]
948+
: ps[i+1].substring(0, ps[i+1].indexOf("/"));
949+
if (
950+
//StringUtil.isNumer(pos) &&
951+
vs[i+1].startsWith(pos + "/") == false) {
952+
vs[i+1] = pos + "/" + vs[i+1];
953+
}
1013954
}
955+
return StringUtil.getString(vs, "]/");
1014956
}
1015957
}
958+
return valuePath;
1016959
}
1017960

961+
//依赖引用关系 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
962+
963+
// /**有关联代码的object的关联key在relationMap里
964+
// * @param keyPath
965+
// * @return
966+
// */
967+
// private boolean isInRelationMap(String keyPath) {
968+
// if (keyPath == null) {
969+
// return false;
970+
// }
971+
// // return keyValuePathMap == null ? false : keyValuePathMap.containsKey(path);
972+
// Set<String> set = keyValuePathMap == null ? null : keyValuePathMap.keySet();
973+
// if (set != null) {
974+
// for (String key : set) {
975+
// if (keyPath.equals(key) || key.startsWith(keyPath + "/")) {//解决相同字符导致的错误) {//key.contains(path)) {//
976+
// return true;
977+
// }
978+
// }
979+
// }
980+
// return false;
981+
// }
982+
//
983+
//
984+
// /**新增keyPath-valuePath关联
985+
// * @param keyPath
986+
// * @param valuePath
987+
// */
988+
// private void putRelation(String keyPath, String valuePath) {
989+
// keyValuePathMap.put(keyPath, valuePath);
990+
// }
991+
// /**根据keyPath获取valuePath
992+
// * @param keyPath
993+
// * @return
994+
// */
995+
// private String getRelationValuePath(String keyPath) {
996+
// return keyValuePathMap.get(keyPath);
997+
// }
998+
// /**移除keyPath-valuePath关联
999+
// * @param keyPath
1000+
// */
1001+
// private void removeRelation(String keyPath) {
1002+
// keyValuePathMap.remove(keyPath);
1003+
// }
1004+
//
1005+
//
1006+
// /**
1007+
// * @param path
1008+
// * @param replacePath
1009+
// */
1010+
// private void updateRelation(String path, String replacePath) {
1011+
// //更新关系path中对应改变字段
1012+
// Set<String> relationSet = replacePath == null || keyValuePathMap == null ? null : keyValuePathMap.keySet();
1013+
// if (relationSet != null) {
1014+
// path = StringUtil.getString(path);
1015+
//
1016+
// String relationValue;
1017+
// for (String relationKey : relationSet) {
1018+
// if (relationKey == null || relationKey.startsWith(replacePath) == false) {
1019+
// continue;
1020+
// }
1021+
// relationValue = keyValuePathMap.get(relationKey);
1022+
// if (relationValue != null && relationValue.startsWith(path)
1023+
// && relationValue.startsWith(replacePath) == false) {
1024+
// //用replace会将所有符合字符替换! []/Comment[] -> []/0/Comment[]/0, replaceFirst因[]特殊字符崩溃
1025+
// relationValue = replacePath + relationValue.substring(path.length());
1026+
// keyValuePathMap.put(relationKey, relationValue);
1027+
// }
1028+
// }
1029+
// }
1030+
// }
1031+
10181032
/**将已获取完成的object的内容替换requestObject里对应的值
10191033
* @param path object的路径
10201034
* @param result 需要被关联的object

0 commit comments

Comments
 (0)