forked from keycloak/keycloak
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
KEYCLOAK-18854 Introduce storage-independent ModelCriteriaBuilder
- Loading branch information
Showing
4 changed files
with
389 additions
and
11 deletions.
There are no files selected for viewing
99 changes: 99 additions & 0 deletions
99
model/map/src/main/java/org/keycloak/models/map/storage/criteria/DefaultModelCriteria.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/* | ||
* Copyright 2021 Red Hat, Inc. and/or its affiliates | ||
* and other contributors as indicated by the @author tags. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.keycloak.models.map.storage.criteria; | ||
|
||
import org.keycloak.models.map.storage.ModelCriteriaBuilder; | ||
import org.keycloak.models.map.storage.criteria.ModelCriteriaNode.ExtOperator; | ||
import org.keycloak.storage.SearchableModelField; | ||
|
||
/** | ||
* Descriptive model criteria implementation which in other words represents a Boolean formula on searchable fields. | ||
* @author hmlnarik | ||
*/ | ||
public class DefaultModelCriteria<M> implements ModelCriteriaBuilder<M> { | ||
|
||
private final ModelCriteriaNode<M> node; | ||
|
||
public DefaultModelCriteria() { | ||
this.node = null; | ||
} | ||
|
||
private DefaultModelCriteria(ModelCriteriaNode<M> node) { | ||
this.node = node; | ||
} | ||
|
||
@Override | ||
public DefaultModelCriteria<M> compare(SearchableModelField<M> modelField, Operator op, Object... value) { | ||
final ModelCriteriaNode<M> targetNode; | ||
if (isEmpty()) { | ||
targetNode = new ModelCriteriaNode<>(modelField, op, value); | ||
} else if (node.getNodeOperator() == ExtOperator.AND) { | ||
targetNode = node.cloneTree(); | ||
targetNode.addChild(new ModelCriteriaNode<>(modelField, op, value)); | ||
} else { | ||
targetNode = new ModelCriteriaNode<>(ExtOperator.AND); | ||
targetNode.addChild(node.cloneTree()); | ||
targetNode.addChild(new ModelCriteriaNode<>(modelField, op, value)); | ||
} | ||
return new DefaultModelCriteria<>(targetNode); | ||
} | ||
|
||
@Override | ||
public DefaultModelCriteria<M> and(ModelCriteriaBuilder<M>... mcbs) { | ||
final ModelCriteriaNode<M> targetNode = new ModelCriteriaNode<>(ExtOperator.AND); | ||
for (ModelCriteriaBuilder<M> mcb : mcbs) { | ||
targetNode.addChild(((DefaultModelCriteria<M>) mcb.unwrap(DefaultModelCriteria.class)).node); | ||
} | ||
return new DefaultModelCriteria<>(targetNode); | ||
} | ||
|
||
@Override | ||
public DefaultModelCriteria<M> or(ModelCriteriaBuilder<M>... mcbs) { | ||
final ModelCriteriaNode<M> targetNode = new ModelCriteriaNode<>(ExtOperator.OR); | ||
for (ModelCriteriaBuilder<M> mcb : mcbs) { | ||
targetNode.addChild(((DefaultModelCriteria<M>) mcb.unwrap(DefaultModelCriteria.class)).node); | ||
} | ||
return new DefaultModelCriteria<>(targetNode); | ||
} | ||
|
||
@Override | ||
public DefaultModelCriteria<M> not(ModelCriteriaBuilder<M> mcb) { | ||
final ModelCriteriaNode<M> targetNode = new ModelCriteriaNode<>(ExtOperator.NOT); | ||
targetNode.addChild(((DefaultModelCriteria<M>) mcb.unwrap(DefaultModelCriteria.class)).node); | ||
return new DefaultModelCriteria<>(targetNode); | ||
} | ||
|
||
/** | ||
* Copies contents of this {@code ModelCriteriaBuilder} into | ||
* another {@code ModelCriteriaBuilder}. | ||
* @param mcb {@code ModelCriteriaBuilder} to copy the contents onto | ||
* @return Updated {@code ModelCriteriaBuilder} | ||
*/ | ||
public ModelCriteriaBuilder<M> flashToModelCriteriaBuilder(ModelCriteriaBuilder<M> mcb) { | ||
return mcb == null ? null : node.flashToModelCriteriaBuilder(mcb); | ||
} | ||
|
||
public boolean isEmpty() { | ||
return node == null; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return isEmpty() ? "" : node.toString(); | ||
} | ||
|
||
} |
141 changes: 141 additions & 0 deletions
141
model/map/src/main/java/org/keycloak/models/map/storage/criteria/ModelCriteriaNode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
/* | ||
* Copyright 2021 Red Hat, Inc. and/or its affiliates | ||
* and other contributors as indicated by the @author tags. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.keycloak.models.map.storage.criteria; | ||
|
||
import org.keycloak.models.map.storage.ModelCriteriaBuilder; | ||
import org.keycloak.models.map.storage.ModelCriteriaBuilder.Operator; | ||
import org.keycloak.models.map.storage.tree.DefaultTreeNode; | ||
import org.keycloak.storage.SearchableModelField; | ||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.Objects; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* | ||
* @author hmlnarik | ||
*/ | ||
public class ModelCriteriaNode<M> extends DefaultTreeNode<ModelCriteriaNode<M>> { | ||
|
||
public static enum ExtOperator { | ||
AND { | ||
@Override public <M> ModelCriteriaBuilder<M> apply(ModelCriteriaBuilder<M> mcb, ModelCriteriaNode<M> node) { | ||
if (node.getChildren().isEmpty()) { | ||
return null; | ||
} | ||
final ModelCriteriaBuilder[] operands = node.getChildren().stream() | ||
.map(n -> n.flashToModelCriteriaBuilder(mcb)) | ||
.filter(Objects::nonNull) | ||
.toArray(ModelCriteriaBuilder[]::new); | ||
return operands.length == 0 ? null : mcb.and(operands); | ||
} | ||
@Override public String toString(ModelCriteriaNode<?> node) { | ||
return "(" + node.getChildren().stream().map(ModelCriteriaNode::toString).collect(Collectors.joining(" && ")) + ")"; | ||
} | ||
}, | ||
OR { | ||
@Override public <M> ModelCriteriaBuilder<M> apply(ModelCriteriaBuilder<M> mcb, ModelCriteriaNode<M> node) { | ||
if (node.getChildren().isEmpty()) { | ||
return null; | ||
} | ||
final ModelCriteriaBuilder[] operands = node.getChildren().stream() | ||
.map(n -> n.flashToModelCriteriaBuilder(mcb)) | ||
.filter(Objects::nonNull) | ||
.toArray(ModelCriteriaBuilder[]::new); | ||
return operands.length == 0 ? null : mcb.or(operands); | ||
} | ||
@Override public String toString(ModelCriteriaNode<?> node) { | ||
return "(" + node.getChildren().stream().map(ModelCriteriaNode::toString).collect(Collectors.joining(" || ")) + ")"; | ||
} | ||
}, | ||
NOT { | ||
@Override public <M> ModelCriteriaBuilder<M> apply(ModelCriteriaBuilder<M> mcb, ModelCriteriaNode<M> node) { | ||
return mcb.not(node.getChildren().iterator().next().flashToModelCriteriaBuilder(mcb)); | ||
} | ||
@Override public String toString(ModelCriteriaNode<?> node) { | ||
return "! " + node.getChildren().iterator().next().toString(); | ||
} | ||
}, | ||
SIMPLE_OPERATOR { | ||
@Override public <M> ModelCriteriaBuilder<M> apply(ModelCriteriaBuilder<M> mcb, ModelCriteriaNode<M> node) { | ||
return mcb.compare( | ||
node.field, | ||
node.simpleOperator, | ||
node.simpleOperatorArguments | ||
); | ||
} | ||
@Override public String toString(ModelCriteriaNode<?> node) { | ||
return node.field.getName() + " " + node.simpleOperator + " " + Arrays.deepToString(node.simpleOperatorArguments); | ||
} | ||
}, | ||
; | ||
|
||
public abstract <M> ModelCriteriaBuilder<M> apply(ModelCriteriaBuilder<M> mcbCreator, ModelCriteriaNode<M> node); | ||
public abstract String toString(ModelCriteriaNode<?> node); | ||
} | ||
|
||
private final ExtOperator nodeOperator; | ||
|
||
private final Operator simpleOperator; | ||
|
||
private final SearchableModelField<M> field; | ||
|
||
private final Object[] simpleOperatorArguments; | ||
|
||
public ModelCriteriaNode(SearchableModelField<M> field, Operator simpleOperator, Object... simpleOperatorArguments) { | ||
super(Collections.emptyMap()); | ||
this.simpleOperator = simpleOperator; | ||
this.field = field; | ||
this.simpleOperatorArguments = simpleOperatorArguments; | ||
this.nodeOperator = ExtOperator.SIMPLE_OPERATOR; | ||
} | ||
|
||
public ModelCriteriaNode(ExtOperator nodeOperator) { | ||
super(Collections.emptyMap()); | ||
this.nodeOperator = nodeOperator; | ||
this.simpleOperator = null; | ||
this.field = null; | ||
this.simpleOperatorArguments = null; | ||
} | ||
|
||
private ModelCriteriaNode(ExtOperator nodeOperator, Operator simpleOperator, SearchableModelField<M> field, Object[] simpleOperatorArguments) { | ||
super(Collections.emptyMap()); | ||
this.nodeOperator = nodeOperator; | ||
this.simpleOperator = simpleOperator; | ||
this.field = field; | ||
this.simpleOperatorArguments = simpleOperatorArguments; | ||
} | ||
|
||
public ExtOperator getNodeOperator() { | ||
return nodeOperator; | ||
} | ||
|
||
public ModelCriteriaNode<M> cloneTree() { | ||
return cloneTree(n -> new ModelCriteriaNode<>(n.nodeOperator, n.simpleOperator, n.field, n.simpleOperatorArguments)); | ||
} | ||
|
||
public ModelCriteriaBuilder<M> flashToModelCriteriaBuilder(ModelCriteriaBuilder<M> mcb) { | ||
final ModelCriteriaBuilder<M> res = nodeOperator.apply(mcb, this); | ||
return res == null ? mcb : res; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return nodeOperator.toString(this); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.