Skip to content

Commit eb32bfd

Browse files
committed
Server: 优化Array解析和查询逻辑;提高estimateMaxCount精度;Array新增query可选获取对象
1 parent 6906797 commit eb32bfd

2 files changed

Lines changed: 110 additions & 57 deletions

File tree

APIJSON(Server)/APIJSON(Eclipse_JEE)/src/main/java/zuo/biao/apijson/JSONRequest.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,30 @@ public String getTag() {
8282

8383
//array object <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
8484

85+
public static final int QUERY_TABLE = 0;
86+
public static final int QUERY_TOTAL = 1;
87+
public static final int QUERY_ALL = 2;
88+
89+
public static final String KEY_QUERY = "query";
8590
public static final String KEY_COUNT = "count";
8691
public static final String KEY_PAGE = "page";
8792

93+
/**
94+
* @param query what need to query, Table,total,ALL?
95+
* @return
96+
*/
97+
public JSONRequest setQuery(int query) {
98+
put(KEY_QUERY, query);
99+
return this;
100+
}
101+
public int getQuery() {
102+
return getIntValue(KEY_QUERY);
103+
}
104+
105+
/**
106+
* @param count
107+
* @return
108+
*/
88109
public JSONRequest setCount(int count) {
89110
put(KEY_COUNT, count);
90111
return this;
@@ -93,6 +114,10 @@ public int getCount() {
93114
return getIntValue(KEY_COUNT);
94115
}
95116

117+
/**
118+
* @param page
119+
* @return
120+
*/
96121
public JSONRequest setPage(int page) {
97122
put(KEY_PAGE, page);
98123
return this;

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

Lines changed: 85 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,6 @@ private JSONObject getObject(String parentPath, final QueryConfig parentConfig,
478478
Log.d(TAG, "getObject table = " + table + "; isTableKey = " + isTableKey);
479479

480480

481-
boolean containRelation = false;
482481

483482
JSONObject transferredRequest = new JSONObject(true);//must init
484483
Map<String, String> functionMap = null;
@@ -578,10 +577,9 @@ private JSONObject getObject(String parentPath, final QueryConfig parentConfig,
578577
throw new IllegalArgumentException("\"key@\": 后面必须为依赖路径String!");
579578
}
580579
// System.out.println("getObject key.endsWith(@) >> parseRelation = " + parseRelation);
581-
// System.out.println("getObject key.endsWith(@) >> parseRelation = " + parseRelation);
582580
String replaceKey = key.substring(0, key.length() - 1);//key{}@ getRealKey
583581
String targetPath = getValuePath(parentPath, new String((String) value));
584-
582+
585583
//先尝试获取,尽量保留缺省依赖路径,这样就不需要担心路径改变
586584
Object target = getValueByPath(targetPath);
587585
Log.i(TAG, "getObject targetPath = " + targetPath + "; target = " + target);
@@ -592,7 +590,7 @@ private JSONObject getObject(String parentPath, final QueryConfig parentConfig,
592590
}
593591
if (targetPath.equals(target)) {//必须valuePath和保证getValueByPath传进去的一致!
594592
Log.d(TAG, "getObject targetPath.equals(target) >>");
595-
593+
596594
//非查询关键词 @key 不影响查询,直接跳过
597595
if (isTableKey && (key.startsWith("@") == false || QueryConfig.TABLE_KEY_LIST.contains(key))) {
598596
Log.e(TAG, "getObject isTableKey && (key.startsWith(@) == false"
@@ -603,8 +601,8 @@ private JSONObject getObject(String parentPath, final QueryConfig parentConfig,
603601
continue;//舍去,对Table无影响
604602
}
605603
}
606-
607-
604+
605+
608606
//直接替换原来的key@:path为key:target
609607
Log.i(TAG, "getObject >> key = replaceKey; value = target;");
610608
key = replaceKey;
@@ -629,7 +627,7 @@ private JSONObject getObject(String parentPath, final QueryConfig parentConfig,
629627

630628
boolean query = false;
631629
//执行SQL操作数据库
632-
if (containRelation == false && isTableKey) {//提高性能
630+
if (isTableKey) {//提高性能
633631
query = true;
634632

635633
QueryConfig config = newQueryConfig(table, transferredRequest);
@@ -697,7 +695,7 @@ private JSONObject getObject(String parentPath, final QueryConfig parentConfig,
697695
* @return 转为JSONArray不可行,因为会和被当成条件的key:JSONArray冲突。好像一般也就key{}:JSONArray用到??
698696
* @throws Exception
699697
*/
700-
private JSONObject getArray(String parentPath, String name, JSONObject request) throws Exception {
698+
private JSONObject getArray(String parentPath, String name, final JSONObject request) throws Exception {
701699
Log.i(TAG, "\n\n\n getArray parentPath = " + parentPath
702700
+ "; name = " + name + "; request = " + JSON.toJSONString(request));
703701
if (isHeadMethod(requestMethod, true)) {
@@ -708,66 +706,89 @@ private JSONObject getArray(String parentPath, String name, JSONObject request)
708706
}
709707
String path = getAbsPath(parentPath, name);
710708

711-
int count = 0, page = 0, total = 0;
712-
713-
count = request.getIntValue(JSONRequest.KEY_COUNT);
714-
page = request.getIntValue(JSONRequest.KEY_PAGE);
709+
//不能改变,因为后面可能继续用到,导致1以上都改变 []:{0:{Comment[]:{0:{Comment:{}},1:{...},...}},1:{...},...}
710+
final int query = request.getIntValue(JSONRequest.KEY_QUERY);
711+
final int count = request.getIntValue(JSONRequest.KEY_COUNT);
712+
final int page = request.getIntValue(JSONRequest.KEY_PAGE);
713+
Log.d(TAG, "getArray query = " + query + "; count = " + count + "; page = " + page);
715714

715+
//后面还要put回来,所以中间不要return;除非不需要保留page,count!
716+
request.remove(JSONRequest.KEY_QUERY);
716717
request.remove(JSONRequest.KEY_COUNT);
717718
request.remove(JSONRequest.KEY_PAGE);
718719

719-
if (count <= 0 || count > 100) {//count最大为100
720-
count = 100;
721-
}
722-
723720
//最好先获取第一个table的所有项(where条件),填充一个列表?
724721
Set<String> set = new LinkedHashSet<>(request.keySet());
725-
if(set != null) {
726-
if (count <= 0 || count > 5) {//5以下不优化长度
727-
String table;
728-
Object value;
729-
for (String key : set) {
730-
table = Pair.parseEntry(key, true).getKey();
731-
value = isTableKey(table) ? request.get(key) : null;
732-
if (value != null && value instanceof JSONObject) {// && value.isEmpty() == false) {
733-
total = estimateMaxCount(path, table, (JSONObject) value);
734-
break;
735-
}
736-
}
737-
Log.d(TAG, "total = " + total);
738-
if (total <= 0) {//request内部没有JSONObject或者不存在适合条件的table内容
739-
return null;
740-
}
741-
putQueryResult(path + "/" + JSONResponse.KEY_TOTAL, total);
742-
if (count > total) {
743-
count = total;
744-
}
745-
}
722+
if (set.isEmpty()) {//如果条件成立,说明所有的 parentPath/name:request 中request都无效!!!
723+
Log.e(TAG, "getArray set.isEmpty() >> return null;");
724+
return null;
746725
}
747-
Log.i(TAG, "getArray page = " + page + "; count = " + count);
748-
749-
QueryConfig config = new QueryConfig(requestMethod, count, page);
750-
751726

752-
JSONObject transferredRequest = new JSONObject(true);
727+
//不用total限制数量了,只用中断机制,total只在query = 1,2的时候才获取
728+
int size = 0;//在QUERY_TOTAL下不查询Table
729+
if (query != JSONRequest.QUERY_TOTAL) {
730+
size = count;//count为每页数量,size为第page页实际数量,max(size) = count
731+
if (size <= 0 || size > 100) {//size最大为100
732+
Log.d(TAG, "getArray size <= 0 || size > 100 >> size = 100;");
733+
size = 100;
734+
}
735+
}
753736

754-
JSONObject parent = null;
755-
//生成count个
756-
for (int i = 0; i < count; i++) {
757-
parent = getObject(path, config.setPosition(i), "" + i, request);
758-
if (parent == null || parent.isEmpty()) {
759-
break;//数据库返回数量不够count,后面没有了。有依赖不为空,无依赖直接查询数据库。
737+
738+
739+
//total<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
740+
if (query != JSONRequest.QUERY_TABLE) {
741+
int total = 0;//满足条件的总数,忽略page,count
742+
Log.d(TAG, "getArray query != JSONRequest.QUERY_TABLE >> ");
743+
String table;
744+
Object value;
745+
for (String key : set) {
746+
table = Pair.parseEntry(key, true).getKey();
747+
value = isTableKey(table) ? request.get(key) : null;
748+
if (value != null && value instanceof JSONObject) {// && value.isEmpty() == false) {
749+
total = estimateMaxCount(path, table, (JSONObject) value);
750+
break;
751+
}
752+
}
753+
if (total <= size*page) {
754+
Log.d(TAG, "getArray total <= size*page >> size = 0;");
755+
size = 0;//不再查询,但不能return null;因为还需要put条件
756+
}
757+
if (total > 0) {
758+
Log.d(TAG, "getArray total = " + total + " >> putQueryResult(...)");
759+
putQueryResult(path + "/" + JSONResponse.KEY_TOTAL, total);//GET下替代HEAD的方式
760760
}
761-
transferredRequest.put("" + i, parent);
762761
}
762+
//total>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
763+
764+
Log.d(TAG, "getArray size = " + size + "; page = " + page);
765+
766+
//Table<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
767+
JSONObject response = new JSONObject(true);
768+
if (size > 0) {//request内部没有JSONObject或者不存在适合条件的table内容
769+
QueryConfig config = new QueryConfig(requestMethod, size, page);//count不能变,变了后start就不对了
770+
JSONObject parent;
771+
//生成size个
772+
for (int i = 0; i < size; i++) {
773+
parent = getObject(path, config.setPosition(i), "" + i, request);
774+
if (parent == null || parent.isEmpty()) {
775+
break;//数据库返回数量不够size,后面没有了。有依赖不为空,无依赖直接查询数据库。
776+
}
777+
response.put("" + i, parent);
778+
}
779+
}
780+
//Table>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
763781

764-
//解决[]:{Comment[]:{...}}内层count,page丢失
782+
783+
784+
//解决[]:{Comment[]:{...}}内层0之后的count,page丢失,导致性能下降
785+
request.put(JSONRequest.KEY_QUERY, query);
765786
request.put(JSONRequest.KEY_COUNT, count);
766787
request.put(JSONRequest.KEY_PAGE, page);
767788

768-
Log.i(TAG, "getArray return " + JSON.toJSONString(transferredRequest) + "\n>>>>>>>>>>>>>>>\n\n\n");
789+
Log.i(TAG, "getArray return response = \n" + JSON.toJSONString(response) + "\n>>>>>>>>>>>>>>>\n\n\n");
769790

770-
return transferredRequest;
791+
return response;
771792
}
772793

773794
//TODO 获取status和message,如果发生异常就throw new Exception(message!),不行,不知道Exception类型,还是传boolean catchException到parse好
@@ -800,11 +821,16 @@ public int estimateMaxCount(String path, String table, JSONObject value) throws
800821
if (v == null) {
801822
continue;
802823
}
803-
target = getValueByPath(getAbsPath(((String) v).startsWith(SEPARATOR) ? path : "", (String) v));
804-
if (target != null && ((String) v).equals(target) == false) {
805-
k = k.substring(0, k.length() - 1);
806-
v = target;
824+
825+
String targetPath = getValuePath(path, new String((String) v));
826+
target = getValueByPath(targetPath);
827+
Log.i(TAG, "estimateMaxCount k = "+ k + "; targetPath = " + targetPath + "; target = " + target);
828+
829+
if (target == null || targetPath.equals(target)) {
830+
continue;
807831
}
832+
k = k.substring(0, k.length() - 1);
833+
v = target;
808834
}
809835

810836
valid = new String(k);
@@ -834,7 +860,9 @@ public int estimateMaxCount(String path, String table, JSONObject value) throws
834860
return response == null ? 0 : response.getIntValue(JSONResponse.KEY_COUNT);
835861
}
836862

837-
863+
864+
865+
838866
/**获取被依赖引用的key的路径, 实时替换[] -> []/i
839867
* @param parentPath
840868
* @param valuePath

0 commit comments

Comments
 (0)