Skip to content

Commit

Permalink
be:优化分组聚合回填执行效率
Browse files Browse the repository at this point in the history
  • Loading branch information
getrebuild committed Dec 1, 2023
1 parent f29922c commit 9fd0459
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import com.rebuild.core.metadata.easymeta.EasyMetaFactory;
import com.rebuild.core.privileges.PrivilegesGuardContextHolder;
import com.rebuild.core.privileges.UserService;
import com.rebuild.core.service.RecordHelper;
import com.rebuild.core.service.general.GeneralEntityServiceContextHolder;
import com.rebuild.core.service.general.OperatingContext;
import com.rebuild.core.service.general.RecordDifference;
Expand Down Expand Up @@ -86,7 +85,7 @@ public class FieldAggregation extends TriggerAction {
protected String followSourceWhere;

public FieldAggregation(ActionContext context) {
this(context, Boolean.FALSE);
this(context, Boolean.TRUE);
}

protected FieldAggregation(ActionContext context, boolean ignoreSame) {
Expand Down Expand Up @@ -278,16 +277,19 @@ public Object execute(OperatingContext operatingContext) throws TriggerException

// 回填 (v3.1)
// 仅分组聚合有此配置

String fillbackField = ((JSONObject) actionContext.getActionContent()).getString("fillbackField");
if (fillbackField != null && MetadataHelper.checkAndWarnField(sourceEntity, fillbackField)) {
String sql = String.format("select %s from %s where %s",
sourceEntity.getPrimaryField().getName(), sourceEntity.getName(), filterSql);
String sql = String.format("select %s,%s from %s where %s",
sourceEntity.getPrimaryField().getName(), fillbackField, sourceEntity.getName(), filterSql);
Object[][] fillbacks = Application.createQueryNoFilter(sql).array();

for (Object[] to : fillbacks) {
for (Object[] o : fillbacks) {
if (CommonsUtils.isSame(o[1], targetRecordId)) continue;

// FIXME 回填仅更新,无业务规则
RecordHelper.setValue((ID) to[0], fillbackField, targetRecordId);
Record r = EntityHelper.forUpdate((ID) o[0], UserService.SYSTEM_USER, false);
r.setID(fillbackField, targetRecordId);
Application.getCommonsService().update(r, false);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public GroupAggregation(ActionContext context) {
this(context, Boolean.FALSE);
}

// ignoreSame 本触发器有回填,忽略同值会导致无法回填
public GroupAggregation(ActionContext context, boolean ignoreSame) {
super(context, ignoreSame);
}
Expand Down
38 changes: 30 additions & 8 deletions src/main/java/com/rebuild/utils/CommonsUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.rebuild.core.Application;
import com.rebuild.core.RebuildException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.lang.StringUtils;
Expand All @@ -26,6 +27,7 @@
import java.math.RoundingMode;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Collection;
import java.util.Objects;
import java.util.UUID;
Expand Down Expand Up @@ -258,17 +260,37 @@ public static boolean hasLength(Object any) {
* @param b
* @return
*/
@SuppressWarnings("unchecked")
public static boolean isSame(Object a, Object b) {
boolean e = Objects.equals(a, b);
if (!e) {
if (a instanceof Number && b instanceof Number) {
// FIXME 有精度问题
e = ObjectUtils.toDouble(a) == ObjectUtils.toDouble(b);
}
if (a == null && b != null) return false;
if (a != null && b == null) return false;
if (Objects.equals(a, b)) return true;

// 数字
if (a instanceof Number && b instanceof Number) {
// FIXME 有精度问题
return ObjectUtils.toDouble(a) == ObjectUtils.toDouble(b);
}
return e;
}

// 集合/数组
if ((a instanceof Collection || a instanceof Object[]) && (b instanceof Collection || b instanceof Object[])) {
Collection<Object> aColl;
if (a instanceof Object[]) aColl = Arrays.asList((Object[]) a);
else aColl = (Collection<Object>) a;
Collection<Object> bColl;
if (b instanceof Object[]) bColl = Arrays.asList((Object[]) b);
else bColl = (Collection<Object>) b;

if (aColl.size() != bColl.size()) return false;
if (aColl.isEmpty()) return true;
return CollectionUtils.containsAll(aColl, bColl) && CollectionUtils.containsAll(bColl, aColl);
}

// 其他
// FIXME 完善不同值类型的比较
return StringUtils.equals(a.toString(), b.toString());
}

/**
* 指定范围的随机数
*
Expand Down

0 comments on commit 9fd0459

Please sign in to comment.