Skip to content

Commit

Permalink
Faster PrismSchema.findItemDefinitionsByElementName.
Browse files Browse the repository at this point in the history
  • Loading branch information
mederly committed Jul 10, 2017
1 parent bb780fd commit 47f9095
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 24 deletions.
Expand Up @@ -708,7 +708,7 @@ private void createDefinitionsFromElements(XSSchemaSet set) throws SchemaExcepti
}
if (definition != null) {
definition.setSubstitutionHead(getSubstitutionHead(xsElementDecl));
schema.getDefinitions().add(definition);
schema.add(definition);
}

} else { //if (xsElementDecl.getTargetNamespace().equals(XMLConstants.W3C_XML_SCHEMA_NS_URI)) {
Expand Down
Expand Up @@ -19,6 +19,7 @@
import com.evolveum.midpoint.prism.*;
import com.evolveum.midpoint.util.QNameUtil;
import com.evolveum.midpoint.util.exception.SchemaException;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand All @@ -27,10 +28,7 @@
import org.xml.sax.EntityResolver;

import javax.xml.namespace.QName;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.*;
import java.util.stream.Collectors;

/**
Expand All @@ -45,6 +43,7 @@ public class PrismSchemaImpl implements PrismSchema {
//private static final Trace LOGGER = TraceManager.getTrace(PrismSchema.class);

@NotNull protected final Collection<Definition> definitions = new ArrayList<>();
@NotNull private final Map<QName, ItemDefinition<?>> itemDefinitionMap = new HashMap<>(); // key is the item name (qualified or unqualified)
protected String namespace; // may be null if not properly initialized
protected PrismContext prismContext;

Expand Down Expand Up @@ -78,7 +77,7 @@ public void setNamespace(@NotNull String namespace) {
@NotNull
@Override
public Collection<Definition> getDefinitions() {
return definitions;
return Collections.unmodifiableCollection(definitions);
}

@SuppressWarnings("unchecked")
Expand Down Expand Up @@ -107,6 +106,10 @@ public boolean isEmpty() {

public void add(@NotNull Definition def) {
definitions.add(def);
if (def instanceof ItemDefinition) {
ItemDefinition<?> itemDef = (ItemDefinition<?>) def;
itemDefinitionMap.put(itemDef.getName(), itemDef);
}
}

@Override
Expand Down Expand Up @@ -171,8 +174,8 @@ public PrismContainerDefinitionImpl createPropertyContainerDefinition(String loc
QName name = new QName(getNamespace(), toElementName(localTypeName));
ComplexTypeDefinition cTypeDef = new ComplexTypeDefinitionImpl(typeName, prismContext);
PrismContainerDefinitionImpl def = new PrismContainerDefinitionImpl(name, cTypeDef, prismContext);
definitions.add(cTypeDef);
definitions.add(def);
add(cTypeDef);
add(def);
return def;
}

Expand All @@ -182,16 +185,16 @@ public PrismContainerDefinitionImpl createPropertyContainerDefinition(String loc
ComplexTypeDefinition cTypeDef = findComplexTypeDefinitionByType(typeName);
if (cTypeDef == null) {
cTypeDef = new ComplexTypeDefinitionImpl(typeName, prismContext);
definitions.add(cTypeDef);
add(cTypeDef);
}
PrismContainerDefinitionImpl def = new PrismContainerDefinitionImpl(name, cTypeDef, prismContext);
definitions.add(def);
add(def);
return def;
}

public ComplexTypeDefinition createComplexTypeDefinition(QName typeName) {
ComplexTypeDefinition cTypeDef = new ComplexTypeDefinitionImpl(typeName, prismContext);
definitions.add(cTypeDef);
add(cTypeDef);
return cTypeDef;
}

Expand Down Expand Up @@ -241,7 +244,7 @@ public PrismPropertyDefinition createPropertyDefinition(String localName, QName
*/
public PrismPropertyDefinition createPropertyDefinition(QName name, QName typeName) {
PrismPropertyDefinition def = new PrismPropertyDefinitionImpl(name, typeName, prismContext);
definitions.add(def);
add(def);
return def;
}

Expand Down Expand Up @@ -368,22 +371,22 @@ public <ID extends ItemDefinition> ID findItemDefinitionByType(@NotNull QName ty

@NotNull
@Override
@SuppressWarnings("unchecked")
public <ID extends ItemDefinition> List<ID> findItemDefinitionsByElementName(@NotNull QName elementName,
@NotNull Class<ID> definitionClass) {
List<ID> rv = new ArrayList<ID>();
for (Definition definition : definitions) {
if (definitionClass.isAssignableFrom(definition.getClass())) {
@SuppressWarnings("unchecked")
ID itemDef = (ID) definition;
if (QNameUtil.match(elementName, itemDef.getName())) {
rv.add(itemDef);
}
}
List<Definition> matching = new ArrayList<>();
CollectionUtils.addIgnoreNull(matching, itemDefinitionMap.get(elementName));
if (QNameUtil.hasNamespace(elementName)) {
CollectionUtils.addIgnoreNull(matching, itemDefinitionMap.get(QNameUtil.unqualify(elementName)));
} else if (namespace != null) {
CollectionUtils.addIgnoreNull(matching, itemDefinitionMap.get(new QName(namespace, elementName.getLocalPart())));
}
return rv;
return matching.stream()
.filter(d -> definitionClass.isAssignableFrom(d.getClass()))
.map(d -> (ID) d)
.collect(Collectors.toList());
}


// private Map<Class<? extends Objectable>, PrismObjectDefinition> classToDefCache = Collections.synchronizedMap(new HashMap<>());
// @Override
// public <O extends Objectable> PrismObjectDefinition<O> findObjectDefinitionByCompileTimeClass(@NotNull Class<O> type) {
Expand Down
Expand Up @@ -1002,7 +1002,7 @@ private void adjustSchemaForSimulatedCapabilities(PrismObject<ResourceType> reso

private void checkSchema(PrismSchema schema) throws SchemaException {
// This is resource schema, it should contain only
// ResourceObjectDefintions
// ResourceObjectDefinitions
for (Definition def : schema.getDefinitions()) {
if (def instanceof ComplexTypeDefinition) {
// This is OK
Expand Down

0 comments on commit 47f9095

Please sign in to comment.