Skip to content

Commit

Permalink
[SYNCOPE-1855] Rewriting JPAAnySearchDAO to reduce subqueries (#957)
Browse files Browse the repository at this point in the history
  • Loading branch information
ilgrosso authored Jan 21, 2025
1 parent 8881db4 commit db24b63
Show file tree
Hide file tree
Showing 16 changed files with 1,505 additions and 1,999 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import org.apache.syncope.core.persistence.api.dao.PlainSchemaDAO;
import org.apache.syncope.core.persistence.api.dao.RealmDAO;
import org.apache.syncope.core.persistence.api.dao.UserDAO;
import org.apache.syncope.core.persistence.api.dao.search.AnyCond;
import org.apache.syncope.core.persistence.api.dao.search.AttrCond;
import org.apache.syncope.core.persistence.api.entity.AnyUtilsFactory;
import org.apache.syncope.core.persistence.api.entity.EntityFactory;
Expand Down Expand Up @@ -66,14 +65,13 @@ public MaJPAJSONAnySearchDAO(
}

@Override
protected String getQuery(
protected AnySearchNode getQuery(
final AttrCond cond,
final boolean not,
final Pair<PlainSchema, PlainAttrValue> checked,
final List<Object> parameters,
final SearchSupport svs) {

Pair<PlainSchema, PlainAttrValue> checked = check(cond, svs.anyTypeKind);

// normalize NULL / NOT NULL checks
if (not) {
if (cond.getType() == AttrCond.Type.ISNULL) {
Expand All @@ -83,20 +81,20 @@ protected String getQuery(
}
}

StringBuilder query =
new StringBuilder("SELECT DISTINCT any_id FROM ").append(svs.field().name).append(" WHERE ");
switch (cond.getType()) {
case ISNOTNULL:
query.append("JSON_SEARCH(plainAttrs, 'one', '").
append(checked.getLeft().getKey()).
append("', NULL, '$[*].schema') IS NOT NULL");
break;
return new AnySearchNode.Leaf(
svs.field(),
"JSON_SEARCH("
+ "plainAttrs, 'one', '" + checked.getLeft().getKey() + "', NULL, '$[*].schema'"
+ ") IS NOT NULL");

case ISNULL:
query.append("JSON_SEARCH(plainAttrs, 'one', '").
append(checked.getLeft().getKey()).
append("', NULL, '$[*].schema') IS NULL");
break;
return new AnySearchNode.Leaf(
svs.field(),
"JSON_SEARCH("
+ "plainAttrs, 'one', '" + checked.getLeft().getKey() + "', NULL, '$[*].schema'"
+ ") IS NULL");

default:
if (!not && cond.getType() == AttrCond.Type.EQ) {
Expand All @@ -108,31 +106,21 @@ protected String getQuery(
((JSONPlainAttr) container).add(checked.getRight());
}

query.append("JSON_CONTAINS(plainAttrs, '").
append(POJOHelper.serialize(List.of(container)).replace("'", "''")).
append("')");
return new AnySearchNode.Leaf(
svs.field(),
"JSON_CONTAINS("
+ "plainAttrs, '" + POJOHelper.serialize(List.of(container)).replace("'", "''")
+ "')");
} else {
query = new StringBuilder("SELECT DISTINCT any_id FROM ");
if (not && !(cond instanceof AnyCond) && checked.getLeft().isMultivalue()) {
query.append(svs.field().name).append(" WHERE ");
} else {
query.append((checked.getLeft().isUniqueConstraint()
? svs.asSearchViewSupport().uniqueAttr().name
: svs.asSearchViewSupport().attr().name)).
append(" WHERE schema_id='").append(checked.getLeft().getKey());
}

Optional.ofNullable(checked.getRight().getDateValue()).
map(DateTimeFormatter.ISO_OFFSET_DATE_TIME::format).
ifPresent(formatted -> {
checked.getRight().setDateValue(null);
checked.getRight().setStringValue(formatted);
});

fillAttrQuery(query, checked.getRight(), checked.getLeft(), cond, not, parameters, svs);
return super.getQuery(cond, not, checked, parameters, svs);
}
}

return query.toString();
}
}
Loading

0 comments on commit db24b63

Please sign in to comment.