Skip to content

Commit 772c2f9

Browse files
committed
Server:新增全局默认的 @Explain 和 @cache;优化 AbstractSQLExecutor 的缓存
1 parent f59a653 commit 772c2f9

File tree

4 files changed

+50
-27
lines changed

4 files changed

+50
-27
lines changed

APIJSON-Java-Server/APIJSONORM/src/main/java/zuo/biao/apijson/server/AbstractObjectParser.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,12 @@ else if (method == PUT && value instanceof JSONArray
321321
if (sqlRequest.get(JSONRequest.KEY_SCHEMA) == null && parser.getGlobleSchema() != null) {
322322
sqlRequest.put(JSONRequest.KEY_SCHEMA, parser.getGlobleSchema());
323323
}
324+
if (sqlRequest.get(JSONRequest.KEY_EXPLAIN) == null && parser.getGlobleExplain() != null) {
325+
sqlRequest.put(JSONRequest.KEY_EXPLAIN, parser.getGlobleExplain());
326+
}
327+
if (sqlRequest.get(JSONRequest.KEY_CACHE) == null && parser.getGlobleCache() != null) {
328+
sqlRequest.put(JSONRequest.KEY_CACHE, parser.getGlobleCache());
329+
}
324330
}
325331
}
326332

APIJSON-Java-Server/APIJSONORM/src/main/java/zuo/biao/apijson/server/AbstractParser.java

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,15 @@ public AbstractParser<T> setRequest(JSONObject request) {
129129
return this;
130130
}
131131

132-
132+
protected Boolean globleFormat;
133+
public AbstractParser<T> setGlobleFormat(Boolean globleFormat) {
134+
this.globleFormat = globleFormat;
135+
return this;
136+
}
137+
@Override
138+
public Boolean getGlobleFormat() {
139+
return globleFormat;
140+
}
133141
protected RequestRole globleRole;
134142
public AbstractParser<T> setGlobleRole(RequestRole globleRole) {
135143
this.globleRole = globleRole;
@@ -157,14 +165,23 @@ public AbstractParser<T> setGlobleSchema(String globleSchema) {
157165
public String getGlobleSchema() {
158166
return globleSchema;
159167
}
160-
protected Boolean globleFormat;
161-
public AbstractParser<T> setGlobleFormat(Boolean globleFormat) {
162-
this.globleFormat = globleFormat;
168+
protected Boolean globleExplain;
169+
public AbstractParser<T> setGlobleExplain(Boolean globleExplain) {
170+
this.globleExplain = globleExplain;
163171
return this;
164172
}
165173
@Override
166-
public Boolean getGlobleFormat() {
167-
return globleFormat;
174+
public Boolean getGlobleExplain() {
175+
return globleExplain;
176+
}
177+
protected String globleCache;
178+
public AbstractParser<T> setGlobleCache(String globleCache) {
179+
this.globleCache = globleCache;
180+
return this;
181+
}
182+
@Override
183+
public String getGlobleCache() {
184+
return globleCache;
168185
}
169186

170187
@Override
@@ -305,13 +322,17 @@ public JSONObject parseResponse(JSONObject request) {
305322
}
306323

307324
try {
325+
setGlobleFormat(requestObject.getBoolean(JSONRequest.KEY_FORMAT));
308326
setGlobleDatabase(requestObject.getString(JSONRequest.KEY_DATABASE));
309327
setGlobleSchema(requestObject.getString(JSONRequest.KEY_SCHEMA));
310-
setGlobleFormat(requestObject.getBoolean(JSONRequest.KEY_FORMAT));
328+
setGlobleExplain(requestObject.getBoolean(JSONRequest.KEY_EXPLAIN));
329+
setGlobleCache(requestObject.getString(JSONRequest.KEY_CACHE));
311330

331+
requestObject.remove(JSONRequest.KEY_FORMAT);
312332
requestObject.remove(JSONRequest.KEY_DATABASE);
313333
requestObject.remove(JSONRequest.KEY_SCHEMA);
314-
requestObject.remove(JSONRequest.KEY_FORMAT);
334+
requestObject.remove(JSONRequest.KEY_EXPLAIN);
335+
requestObject.remove(JSONRequest.KEY_CACHE);
315336
} catch (Exception e) {
316337
return extendErrorResult(requestObject, e);
317338
}

APIJSON-Java-Server/APIJSONORM/src/main/java/zuo/biao/apijson/server/AbstractSQLExecutor.java

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,6 @@ public JSONObject execute(@NotNull SQLConfig config, boolean unknowType) throws
181181

182182
ResultSet rs = null;
183183

184-
boolean noCache = true;
185-
186184
if (unknowType) {
187185
Statement statement = getStatement(config);
188186
rs = execute(statement, sql);
@@ -229,9 +227,7 @@ public JSONObject execute(@NotNull SQLConfig config, boolean unknowType) throws
229227

230228
case GET:
231229
case GETS:
232-
noCache = config.isTest() || config.isExplain(); // TODO explain 照样 cache,但下面的 noCache 涉及的地方要改改,尤其是 JOIN
233-
234-
result = noCache ? null : getCacheItem(sql, position, config.getCache());
230+
result = getCacheItem(sql, position, config.getCache());
235231
Log.i(TAG, ">>> select result = getCache('" + sql + "', " + position + ") = " + result);
236232
if (result != null) {
237233
cachedSQLCount ++;
@@ -282,7 +278,7 @@ public JSONObject execute(@NotNull SQLConfig config, boolean unknowType) throws
282278
// }
283279

284280
// bugfix-修复非常规数据库字段,获取表名失败导致输出异常
285-
if (noCache == false && hasJoin && viceColumnStart > length) {
281+
if (config.isExplain() == false && hasJoin && viceColumnStart > length) {
286282
List<String> column = config.getColumn();
287283

288284
if (column != null && column.isEmpty() == false) {
@@ -293,7 +289,7 @@ else if (config.getSQLTable().equalsIgnoreCase(rsmd.getTableName(i)) == false) {
293289
}
294290
}
295291

296-
item = onPutColumn(config, rs, rsmd, index, item, i, noCache == false && hasJoin && i >= viceColumnStart ? childMap : null);
292+
item = onPutColumn(config, rs, rsmd, index, item, i, config.isExplain() == false && hasJoin && i >= viceColumnStart ? childMap : null);
297293
}
298294

299295
resultList = onPutTable(config, rs, rsmd, resultList, index, item);
@@ -321,19 +317,17 @@ else if (config.getSQLTable().equalsIgnoreCase(rsmd.getTableName(i)) == false) {
321317

322318
// @ APP JOIN 查询副表并缓存到 childMap >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
323319

324-
if (noCache == false) {
325-
//子查询 SELECT Moment.*, Comment.id 中的 Comment 内字段
326-
Set<Entry<String, JSONObject>> set = childMap.entrySet();
327-
328-
//<sql, Table>
329-
for (Entry<String, JSONObject> entry : set) {
330-
List<JSONObject> l = new ArrayList<>();
331-
l.add(entry.getValue());
332-
putCache(entry.getKey(), l, JSONRequest.CACHE_ROM);
333-
}
320+
//子查询 SELECT Moment.*, Comment.id 中的 Comment 内字段
321+
Set<Entry<String, JSONObject>> set = childMap.entrySet();
334322

335-
putCache(sql, resultList, config.getCache());
323+
//<sql, Table>
324+
for (Entry<String, JSONObject> entry : set) {
325+
List<JSONObject> l = new ArrayList<>();
326+
l.add(entry.getValue());
327+
putCache(entry.getKey(), l, JSONRequest.CACHE_ROM);
336328
}
329+
330+
putCache(sql, resultList, config.getCache());
337331
Log.i(TAG, ">>> select putCache('" + sql + "', resultList); resultList.size() = " + resultList.size());
338332

339333
long endTime = System.currentTimeMillis();

APIJSON-Java-Server/APIJSONORM/src/main/java/zuo/biao/apijson/server/Parser.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,11 @@ public interface Parser<T> {
128128
Verifier<T> getVerifier();
129129

130130

131+
Boolean getGlobleFormat();
131132
RequestRole getGlobleRole();
132133
String getGlobleDatabase();
133134
String getGlobleSchema();
134-
Boolean getGlobleFormat();
135+
Boolean getGlobleExplain();
136+
String getGlobleCache();
135137

136138
}

0 commit comments

Comments
 (0)