Skip to content

Commit c2b0e09

Browse files
committed
通过缓存 Request 校验规则来大幅提升增删改等非开放请求的性能;不再默认加载 Oracle, SQLServer, DB2 的驱动,用户自己按需加载
1 parent dd37eab commit c2b0e09

2 files changed

Lines changed: 27 additions & 44 deletions

File tree

src/main/java/apijson/framework/APIJSONSQLExecutor.java

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -68,37 +68,6 @@ public class APIJSONSQLExecutor extends AbstractSQLExecutor {
6868
Log.e(TAG, "加载 PostgresSQL 驱动失败,请检查 libs 目录中 postgresql.jar 版本是否存在以及可用 !!!");
6969
}
7070

71-
72-
try { //加载驱动程序
73-
Log.d(TAG, "尝试加载 SQLServer 驱动 <<<<<<<<<<<<<<<<<<<<< ");
74-
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
75-
Log.d(TAG, "成功加载 SQLServer 驱动!>>>>>>>>>>>>>>>>>>>>> ");
76-
}
77-
catch (ClassNotFoundException e) {
78-
e.printStackTrace();
79-
Log.e(TAG, "加载 SQLServer 驱动失败,请检查 pom.xml 中 net.sourceforge.jtds 版本是否存在以及可用 !!!");
80-
}
81-
82-
try { //加载驱动程序
83-
Log.d(TAG, "尝试加载 Oracle 驱动 <<<<<<<<<<<<<<<<<<<<< ");
84-
Class.forName("oracle.jdbc.driver.OracleDriver");
85-
Log.d(TAG, "成功加载 Oracle 驱动!>>>>>>>>>>>>>>>>>>>>> ");
86-
}
87-
catch (ClassNotFoundException e) {
88-
e.printStackTrace();
89-
Log.e(TAG, "加载 Oracle 驱动失败,请检查 pom.xml 中 com.oracle.jdbc 版本是否存在以及可用 !!!");
90-
}
91-
92-
try { //加载驱动程序
93-
Log.d(TAG, "尝试加载 DB2 驱动 <<<<<<<<<<<<<<<<<<<<< ");
94-
Class.forName("com.ibm.db2.jcc.DB2Driver");
95-
Log.d(TAG, "成功加载 DB2 驱动!>>>>>>>>>>>>>>>>>>>>> ");
96-
}
97-
catch (ClassNotFoundException e) {
98-
e.printStackTrace();
99-
Log.e(TAG, "加载 DB2 驱动失败,请检查 pom.xml 中 com.ibm.db2 版本是否存在以及可用 !!!");
100-
}
101-
10271
}
10372

10473

src/main/java/apijson/framework/APIJSONVerifier.java

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,11 @@
2020
import static apijson.framework.APIJSONConstant.VISITOR_ID;
2121

2222
import java.rmi.ServerException;
23+
import java.util.Comparator;
2324
import java.util.HashMap;
2425
import java.util.Map;
26+
import java.util.SortedMap;
27+
import java.util.TreeMap;
2528

2629
import javax.servlet.http.HttpSession;
2730

@@ -141,6 +144,9 @@ public static JSONObject initAccess(boolean shutdownWhenServerError, APIJSONCrea
141144

142145
{ //Access<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
143146
JSONRequest access = new JSONRequest();
147+
if (Log.DEBUG == false) {
148+
access.put("debug", 0);
149+
}
144150
accessItem.put(ACCESS_, access);
145151
} //Access>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
146152

@@ -196,18 +202,14 @@ public static JSONObject initAccess(boolean shutdownWhenServerError, APIJSONCrea
196202
onServerError("name: " + name + "不合法!字段 alias 的值为空时,name 必须为合法表名!", shutdownWhenServerError);
197203
}
198204

199-
if (Log.DEBUG || item.getBooleanValue("debug") == false) {
200-
ACCESS_MAP.put(name, map);
201-
}
205+
ACCESS_MAP.put(name, map);
202206
}
203207
else {
204208
if (JSONRequest.isTableKey(alias) == false) {
205209
onServerError("alias: " + alias + "不合法!字段 alias 的值只能为 空 或者 合法表名!", shutdownWhenServerError);
206210
}
207211

208-
if (Log.DEBUG || item.getBooleanValue("debug") == false) {
209-
ACCESS_MAP.put(alias, map);
210-
}
212+
ACCESS_MAP.put(alias, map);
211213
}
212214

213215
APIJSONSQLConfig.TABLE_KEY_MAP.put(alias, name);
@@ -260,7 +262,7 @@ public static JSONObject initRequest(boolean shutdownWhenServerError, APIJSONCre
260262
JSONRequest requestItem = new JSONRequest();
261263

262264
{ //Request<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
263-
requestItem.put(REQUEST_, new JSONRequest());
265+
requestItem.put(REQUEST_, new JSONRequest().setOrder("version-,id+")); // 方便查找
264266
} //Request>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
265267

266268
request.putAll(requestItem.toArray(0, 0, REQUEST_));
@@ -280,9 +282,9 @@ public static JSONObject initRequest(boolean shutdownWhenServerError, APIJSONCre
280282
throw new NullPointerException("没有可用的权限配置");
281283
}
282284

283-
// Log.d(TAG, "init < for REQUEST_MAP.size() = " + REQUEST_MAP.size() + " <<<<<<<<<<<<<<<<<<<<<<<<");
285+
Log.d(TAG, "init < for REQUEST_MAP.size() = " + REQUEST_MAP.size() + " <<<<<<<<<<<<<<<<<<<<<<<<");
284286

285-
// REQUEST_MAP.clear();
287+
REQUEST_MAP.clear();
286288

287289
JSONObject item;
288290
for (int i = 0; i < list.size(); i++) {
@@ -311,7 +313,6 @@ public static JSONObject initRequest(boolean shutdownWhenServerError, APIJSONCre
311313

312314
JSONObject structure = JSON.parseObject(item.getString("structure"));
313315

314-
315316
JSONObject target = null;
316317

317318
if (structure != null) {
@@ -338,18 +339,31 @@ public static JSONObject initRequest(boolean shutdownWhenServerError, APIJSONCre
338339
onServerError("服务器内部错误,Request 表中的 version = " + version + ", method = " + method + ", tag = " + tag + " 对应的 structure 不能为空!", shutdownWhenServerError);
339340
}
340341

341-
// REQUEST_MAP.put(tag, target);
342+
String cacheKey = getCacheKeyForRequest(method, tag);
343+
SortedMap<Integer, JSONObject> versionedMap = REQUEST_MAP.get(cacheKey);
344+
if (versionedMap == null) {
345+
versionedMap = new TreeMap<>(new Comparator<Integer>() {
346+
347+
@Override
348+
public int compare(Integer o1, Integer o2) {
349+
return o2 == null ? -1 : o2.compareTo(o1); // 降序
350+
}
351+
});
352+
}
353+
versionedMap.put(Integer.valueOf(version), item);
354+
REQUEST_MAP.put(cacheKey, versionedMap);
342355
}
343356

344-
// Log.d(TAG, "init for /> REQUEST_MAP.size() = " + REQUEST_MAP.size() + " >>>>>>>>>>>>>>>>>>>>>>>");
357+
Log.d(TAG, "init for /> REQUEST_MAP.size() = " + REQUEST_MAP.size() + " >>>>>>>>>>>>>>>>>>>>>>>");
345358

346359
return response;
347360
}
348361

362+
349363
public static void test() {
350364
testStructure();
351365
}
352-
366+
353367
static final String requestString = "{\"Comment\":{\"REFUSE\": \"id\", \"MUST\": \"userId,momentId,content\"}, \"INSERT\":{\"Comment:to\":{}}}";
354368
static final String responseString = "{\"User\":{\"REMOVE\": \"phone\", \"REPLACE\":{\"sex\":2}, \"INSERT\":{\"name\":\"api\"}}, \"UPDATE\":{\"Comment:to\":{}}}";
355369
/**

0 commit comments

Comments
 (0)