Skip to content

Commit 544a869

Browse files
committed
预估容量新增对 HAVING 聚合函数的处理;优化代码
1 parent 2433f3b commit 544a869

File tree

4 files changed

+79
-21
lines changed

4 files changed

+79
-21
lines changed

APIJSONORM/src/main/java/apijson/StringUtil.java

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -225,13 +225,27 @@ public static int getLength(String s, boolean trim) {
225225

226226
//判断字符是否为空 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
227227

228+
/**判断字符是否为空 trim = true
229+
* @param obj
230+
* @return
231+
*/
232+
public static boolean isEmpty(Object obj) {
233+
return isEmpty(obj, true);
234+
}
228235
/**判断字符是否为空
229-
* @param object
236+
* @param obj
230237
* @param trim
231238
* @return
232239
*/
233-
public static boolean isEmpty(Object object, boolean trim) {
234-
return isEmpty(getString(object), trim);
240+
public static boolean isEmpty(Object obj, boolean trim) {
241+
return isEmpty(getString(obj), trim);
242+
}
243+
/**判断字符是否为空 trim = true
244+
* @param cs
245+
* @return
246+
*/
247+
public static boolean isEmpty(CharSequence cs) {
248+
return isEmpty(cs, true);
235249
}
236250
/**判断字符是否为空
237251
* @param cs
@@ -241,6 +255,13 @@ public static boolean isEmpty(Object object, boolean trim) {
241255
public static boolean isEmpty(CharSequence cs, boolean trim) {
242256
return isEmpty(getString(cs), trim);
243257
}
258+
/**判断字符是否为空 trim = true
259+
* @param s
260+
* @return
261+
*/
262+
public static boolean isEmpty(String s) {
263+
return isEmpty(s, true);
264+
}
244265
/**判断字符是否为空
245266
* @param s
246267
* @param trim
@@ -267,21 +288,42 @@ public static boolean isEmpty(String s, boolean trim) {
267288

268289
//判断字符是否非空 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
269290

270-
/**判断字符是否非空
291+
/**判断字符是否非空 trim = true
271292
* @param object
293+
* @return
294+
*/
295+
public static boolean isNotEmpty(Object obj) {
296+
return ! isEmpty(obj);
297+
}
298+
/**判断字符是否非空
299+
* @param obj
272300
* @param trim
273301
* @return
274302
*/
275-
public static boolean isNotEmpty(Object object, boolean trim) {
276-
return isNotEmpty(getString(object), trim);
303+
public static boolean isNotEmpty(Object obj, boolean trim) {
304+
return ! isEmpty(obj, trim);
305+
}
306+
/**判断字符是否非空 trim = true
307+
* @param cs
308+
* @return
309+
*/
310+
public static boolean isNotEmpty(CharSequence cs) {
311+
return ! isEmpty(cs);
277312
}
278313
/**判断字符是否非空
279314
* @param cs
280315
* @param trim
281316
* @return
282317
*/
283318
public static boolean isNotEmpty(CharSequence cs, boolean trim) {
284-
return isNotEmpty(getString(cs), trim);
319+
return ! isEmpty(cs, trim);
320+
}
321+
/**判断字符是否非空 trim = true
322+
* @param s
323+
* @return
324+
*/
325+
public static boolean isNotEmpty(String s) {
326+
return ! isEmpty(s);
285327
}
286328
/**判断字符是否非空
287329
* @param s

APIJSONORM/src/main/java/apijson/orm/AbstractParser.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
import java.util.ArrayList;
1919
import java.util.Arrays;
2020
import java.util.HashMap;
21-
import java.util.LinkedHashMap;
22-
import java.util.LinkedHashSet;
2321
import java.util.List;
2422
import java.util.Map;
2523
import java.util.Map.Entry;
@@ -42,7 +40,6 @@
4240
import apijson.Log;
4341
import apijson.NotNull;
4442
import apijson.RequestMethod;
45-
import apijson.SQL;
4643
import apijson.StringUtil;
4744
import apijson.orm.exception.ConditionErrorException;
4845
import apijson.orm.exception.ConflictException;

APIJSONORM/src/main/java/apijson/orm/AbstractSQLExecutor.java

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -264,11 +264,15 @@ public JSONObject execute(@NotNull SQLConfig config, boolean unknowType) throws
264264

265265
//id,id{}至少一个会有,一定会返回,不用抛异常来阻止关联写操作时前面错误导致后面无条件执行!
266266
result.put(JSONResponse.KEY_COUNT, updateCount);//返回修改的记录数
267+
268+
String idKey = config.getIdKey();
267269
if (config.getId() != null) {
268-
result.put(config.getIdKey(), config.getId());
269-
} else {
270-
result.put(config.getIdKey() + "[]", config.getWhere(config.getIdKey() + "{}", true));
270+
result.put(idKey, config.getId());
271+
}
272+
if (config.getIdIn() != null) {
273+
result.put(idKey + "[]", config.getIdIn());
271274
}
275+
272276
return result;
273277

274278
case GET:
@@ -327,14 +331,14 @@ public JSONObject execute(@NotNull SQLConfig config, boolean unknowType) throws
327331
capacity = 1;
328332
}
329333
else {
330-
Object idIn = config.getWhere(config.getIdKey() + "{}", true);
334+
Object idIn = config.getIdIn();
331335
if (idIn instanceof Collection<?>) { // id{}:[] 一定是 AND 条件,最终返回数据最多就这么多
332336
capacity = ((Collection<?>) idIn).size();
333337
}
334338
else { // 预估容量
335339
capacity = config.getCount() <= 0 ? Parser.MAX_QUERY_COUNT : config.getCount();
336340
if (capacity > 100) {
337-
// 有条件,条件越多过滤数据越多
341+
// 有 WHERE 条件,条件越多过滤数据越多,暂时不考虑 @combine:"a | (b & !c)" 里面 | OR 和 ! NOT 条件,太复杂也不是很必要
338342
Map<String, List<String>> combine = config.getCombineMap();
339343

340344
List<String> andList = combine == null ? null : combine.get("&");
@@ -346,23 +350,29 @@ public JSONObject execute(@NotNull SQLConfig config, boolean unknowType) throws
346350
List<String> notList = combine == null ? null : combine.get("|");
347351
int notCondCount = notList == null ? 0 : notList.size();
348352

349-
350-
// 有分组,分组字段越少过滤数据越多
353+
// 有 GROUP BY 分组,字段越少过滤数据越多
351354
String[] group = StringUtil.split(config.getGroup());
352355
int groupCount = group == null ? 0 : group.length;
353356
if (groupCount > 0 && Arrays.asList(group).contains(config.getIdKey())) {
354357
groupCount = 0;
355358
}
356359

357-
capacity /= Math.pow(1.5, Math.log10(capacity) + andCondCount
358-
+ (orCondCount <= 0 ? 0 : 2/orCondCount) // 1: 2.3, 2: 1.5, 3: 1.3, 4: 1.23, 5: 1.18
359-
+ (notCondCount/5) // 1: 1.08, 2: 1.18, 3: 1.28, 4: 1.38, 1.50
360-
+ (groupCount <= 0 ? 0 : 10/groupCount) // 1: 57.7, 7.6, 3: 3.9, 4: 2.8, 5: 2.3
360+
// 有 HAVING 聚合函数,字段越多过滤数据越多,暂时不考虑 @combine:"a | (b & !c)" 里面 | OR 和 ! NOT 条件,太复杂也不是很必要
361+
Map<String, Object> having = config.getHaving();
362+
int havingCount = having == null ? 0 : having.size();
363+
364+
capacity /= Math.pow(1.5, Math.log10(capacity)
365+
+ andCondCount
366+
+ ((orCondCount <= 0 ? 0 : 2.0d/orCondCount) // 1: 2.3, 2: 1.5, 3: 1.3, 4: 1.23, 5: 1.18
367+
+ (notCondCount/5.0d) // 1: 1.08, 2: 1.18, 3: 1.28, 4: 1.38, 1.50
368+
+ (groupCount <= 0 ? 0 : 10.0d/groupCount)) // 1: 57.7, 7.6, 3: 3.9, 4: 2.8, 5: 2.3
369+
+ havingCount
361370
);
362371
capacity += 1; // 避免正好比需要容量少一点点导致多一次扩容,大量数据 System.arrayCopy
363372
}
364373
}
365374
}
375+
366376
resultList = new ArrayList<>(capacity);
367377
}
368378

APIJSONORM/src/main/java/apijson/orm/SQLConfig.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,15 @@ public interface SQLConfig {
116116

117117
Object getId();
118118
SQLConfig setId(Object id);
119+
120+
Object getIdIn();
121+
SQLConfig setIdIn(Object idIn);
122+
123+
Object getUserId();
124+
SQLConfig setUserId(Object userId);
125+
126+
Object getUserIdIn();
127+
SQLConfig setUserIdIn(Object userIdIn);
119128

120129
String getRole();
121130
SQLConfig setRole(String role);

0 commit comments

Comments
 (0)