Improving code

This commit is contained in:
Luca Frosini 2021-02-19 14:34:48 +01:00
parent de12ce5a6d
commit 799aa5a770
13 changed files with 214 additions and 85 deletions

View File

@ -184,7 +184,7 @@ public abstract class ElementManagement<El extends OElement> {
oClass = ElementManagementUtility.getOClass(element);
} else {
TypesCache typesCache = TypesCache.getInstance();
CachedType cachedType = typesCache.getType(typeName);
CachedType cachedType = typesCache.getCachedType(typeName);
oClass = cachedType.getOClass();
AccessType gotAccessType = cachedType.getAccessType();
if(accessType!=gotAccessType) {

View File

@ -40,7 +40,7 @@ public class ElementManagementUtility {
@SuppressWarnings("rawtypes")
public static ElementManagement getERManagement(String type) throws ResourceRegistryException {
AccessType accessType = TypesCache.getInstance().getType(type).getAccessType();
AccessType accessType = TypesCache.getInstance().getCachedType(type).getAccessType();
ElementManagement erManagement = null;

View File

@ -56,7 +56,7 @@ public class PropertyElementManagement {
try {
TypesCache typesCache = TypesCache.getInstance();
CachedType cachedType = typesCache.getType(type);
CachedType cachedType = typesCache.getCachedType(type);
oClass = cachedType.getOClass();
AccessType gotAccessType = cachedType.getAccessType();
if(!AccessType.PROPERTY_ELEMENT.getClass().isAssignableFrom(gotAccessType.getClass())) {
@ -131,7 +131,7 @@ public class PropertyElementManagement {
}
TypesCache typesCache = TypesCache.getInstance();
CachedType cachedType = typesCache.getType(type);
CachedType cachedType = typesCache.getCachedType(type);
OClass oClass = cachedType.getOClass();
AccessType gotAccessType = cachedType.getAccessType();
if(!AccessType.PROPERTY_ELEMENT.getClass().isAssignableFrom(gotAccessType.getClass())) {

View File

@ -781,13 +781,13 @@ public abstract class EntityManagement<E extends Entity> extends EntityElementMa
oDatabaseDocument = getWorkingContext().getDatabaseDocument(PermissionMode.READER);
TypesCache typesCache = TypesCache.getInstance();
AccessType relationAccessType = typesCache.getType(relationType).getAccessType();
AccessType relationAccessType = typesCache.getCachedType(relationType).getAccessType();
if(relationAccessType != AccessType.IS_RELATED_TO && relationAccessType != AccessType.CONSISTS_OF) {
String error = String.format("%s must be a relation type", relationType);
throw new ResourceRegistryException(error);
}
AccessType referenceAccessType = typesCache.getType(referenceType).getAccessType();
AccessType referenceAccessType = typesCache.getCachedType(referenceType).getAccessType();
if(referenceAccessType != AccessType.RESOURCE && referenceAccessType != AccessType.FACET) {
String error = String.format("%s must be a en entity type", referenceType);
throw new ResourceRegistryException(error);

View File

@ -334,7 +334,7 @@ public class ResourceManagement extends EntityManagement<Resource> {
Set<LinkedEntity> constraints = new HashSet<>();
TypesCache typesCache = TypesCache.getInstance();
CachedType cachedType = typesCache.getType(typeName);
CachedType cachedType = typesCache.getCachedType(typeName);
ResourceType resourceType = (ResourceType) cachedType.getType();
List<LinkedEntity> linkedEntities = resourceType.getFacets();
@ -347,7 +347,7 @@ public class ResourceManagement extends EntityManagement<Resource> {
Set<CachedType> cachedSuperTypes = new HashSet<>();
List<String> superTypes = cachedType.getSuperTypes();
for(String superType : superTypes) {
CachedType cachedSuperType = typesCache.getType(superType);
CachedType cachedSuperType = typesCache.getCachedType(superType);
cachedSuperTypes.add(cachedSuperType);
ResourceType resourceSuperType = (ResourceType) cachedSuperType.getType();
@ -380,20 +380,20 @@ public class ResourceManagement extends EntityManagement<Resource> {
return stringBuffer.toString();
}
protected boolean typeSatified(TypesCache typesCache, String requiredType, String effectiveType) {
protected boolean typeSatified(TypesCache typesCache, String requiredType, String effectiveType) throws SchemaException, ResourceRegistryException {
if(requiredType.compareTo(effectiveType)==0) {
return true;
}
CachedType cachedType = typesCache.getType(requiredType);
if(cachedType.getSpecilisationTypes().contains(effectiveType)) {
CachedType cachedType = typesCache.getCachedType(requiredType);
if(cachedType.getSubTypes().contains(effectiveType)) {
return true;
}
return false;
}
protected boolean constraintSatisfied(TypesCache typesCache, LinkedEntity constraint, String consistsOfType, String facetType) {
protected boolean constraintSatisfied(TypesCache typesCache, LinkedEntity constraint, String consistsOfType, String facetType) throws SchemaException, ResourceRegistryException {
String requiredSourceResourceType = constraint.getSource();
if(!typeSatified(typesCache, requiredSourceResourceType, typeName)) {
return false;

View File

@ -1,6 +1,7 @@
package org.gcube.informationsystem.resourceregistry.rest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
@ -35,6 +36,8 @@ import org.gcube.informationsystem.resourceregistry.instances.model.entities.Res
import org.gcube.informationsystem.resourceregistry.query.Query;
import org.gcube.informationsystem.resourceregistry.query.QueryImpl;
import org.gcube.informationsystem.resourceregistry.types.SchemaManagement;
import org.gcube.informationsystem.types.TypeMapper;
import org.gcube.informationsystem.types.reference.Type;
import com.orientechnologies.orient.core.record.ODirection;
import com.tinkerpop.blueprints.Direction;
@ -158,7 +161,12 @@ public class Access extends BaseRest {
SchemaManagement schemaManagement = new SchemaManagement();
schemaManagement.setTypeName(type);
return schemaManagement.read(polymorphic);
List<Type> types = schemaManagement.read(polymorphic);
try {
return TypeMapper.serializeTypeDefinitions(types);
}catch (Exception e) {
throw new ResourceRegistryException(e);
}
}
/*

View File

@ -1,5 +1,7 @@
package org.gcube.informationsystem.resourceregistry.rest;
import java.util.List;
import javax.ws.rs.Consumes;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
@ -20,6 +22,8 @@ import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.Schema
import org.gcube.informationsystem.resourceregistry.api.rest.AccessPath;
import org.gcube.informationsystem.resourceregistry.api.rest.TypePath;
import org.gcube.informationsystem.resourceregistry.types.SchemaManagement;
import org.gcube.informationsystem.types.TypeMapper;
import org.gcube.informationsystem.types.reference.Type;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -92,7 +96,12 @@ public class SchemaManager {
CalledMethodProvider.instance.set("readType");
SchemaManagement schemaManagement = new SchemaManagement();
schemaManagement.setTypeName(type);
return schemaManagement.read(polymorphic);
List<Type> types = schemaManagement.read(polymorphic);
try {
return TypeMapper.serializeTypeDefinitions(types);
}catch (Exception e) {
throw new ResourceRegistryException(e);
}
}
}

View File

@ -14,6 +14,8 @@ import org.gcube.informationsystem.resourceregistry.contexts.ContextUtility;
import org.gcube.informationsystem.resourceregistry.contexts.security.AdminSecurityContext;
import org.gcube.informationsystem.resourceregistry.contexts.security.SecurityContext.PermissionMode;
import org.gcube.informationsystem.resourceregistry.dbinitialization.DatabaseEnvironment;
import org.gcube.informationsystem.resourceregistry.instances.base.ElementManagement;
import org.gcube.informationsystem.types.TypeMapper;
import org.gcube.informationsystem.types.reference.Type;
import org.gcube.informationsystem.types.reference.properties.LinkedEntity;
import org.slf4j.Logger;
@ -23,6 +25,7 @@ import com.orientechnologies.orient.core.db.document.ODatabaseDocument;
import com.orientechnologies.orient.core.metadata.OMetadata;
import com.orientechnologies.orient.core.metadata.schema.OClass;
import com.orientechnologies.orient.core.metadata.schema.OSchema;
import com.orientechnologies.orient.core.record.OElement;
import com.tinkerpop.blueprints.util.StringFactory;
/**
@ -50,7 +53,7 @@ public class CachedType {
protected Type type;
protected List<String> superTypes;
protected Set<String> specilisationTypes;
protected List<String> subTypes;
/* Valid only for resource types */
protected Set<LinkedEntity> constraints;
@ -64,7 +67,7 @@ public class CachedType {
ODatabaseDocument current = ContextUtility.getCurrentODatabaseDocumentFromThreadLocal();
ODatabaseDocument oDatabaseDocument = null;
try {
logger.debug("GettingType {} schema", type);
logger.debug("GettingType {} schema", typeName);
AdminSecurityContext adminSecurityContext = ContextUtility.getAdminSecurityContext();
oDatabaseDocument = adminSecurityContext.getDatabaseDocument(PermissionMode.READER);
OMetadata oMetadata = oDatabaseDocument.getMetadata();
@ -97,23 +100,12 @@ public class CachedType {
}
private OClass internalGetOClass() throws SchemaNotFoundException, SchemaException, ResourceRegistryException {
if(oClass==null) {
oClass = retrieveOClass();
}
return oClass;
}
private synchronized void setOClass(OClass oClass) throws SchemaNotFoundException, SchemaException, ResourceRegistryException {
if(this.oClass==null) {
this.oClass = oClass;
}
}
public synchronized OClass getOClass() throws SchemaNotFoundException, SchemaException, ResourceRegistryException {
return internalGetOClass();
}
private AccessType getAccessTypeFromOClass(OClass oClass) throws ResourceRegistryException {
AccessType[] accessTypes = AccessType.values();
for(int i=accessTypes.length-1; i>=0; i--) {
@ -125,7 +117,76 @@ public class CachedType {
throw new ResourceRegistryException(typeName + " is not a base type");
}
public AccessType internalGetAccessType() throws SchemaNotFoundException, SchemaException, ResourceRegistryException {
private List<String> getAllSuperclasses(Collection<String> superClassesToBeExcluded) throws SchemaException, ResourceRegistryException {
TypesCache typesCache = TypesCache.getInstance();
List<String> allSuperClasses = new ArrayList<>();
// Instead of using getAllSuperClasses() we get just the first level superclasses and so on.
// This allow to have an order list where the first superclass is a a first level superclass.
List<OClass> superClasses = internalGetOClass().getSuperClasses();
while(superClasses.size()>0) {
List<OClass> toBeAnalysed = new ArrayList<>(superClasses);
superClasses = new ArrayList<>();
for(OClass oSuperClass : toBeAnalysed) {
String name = oSuperClass.getName();
CachedType cachedType = typesCache.getCachedType(name);
cachedType.setOClass(oSuperClass);
if(name.compareTo(StringFactory.V.toUpperCase()) == 0 || name.compareTo(StringFactory.E.toUpperCase()) == 0
|| name.compareTo(DatabaseEnvironment.O_RESTRICTED_CLASS) == 0) {
continue;
}
if(superClassesToBeExcluded.contains(name)) {
continue;
}
allSuperClasses.add(allSuperClasses.size(), name);
superClasses.addAll(oSuperClass.getSuperClasses());
}
}
return allSuperClasses;
}
protected List<String> getAllSubclasses() throws SchemaException, ResourceRegistryException {
TypesCache typesCache = TypesCache.getInstance();
List<String> allSubClasses = new ArrayList<>();
Collection<OClass> subclasses = internalGetOClass().getSubclasses();
while(subclasses.size()>0) {
List<OClass> toBeAnalysed = new ArrayList<>(subclasses);
subclasses = new ArrayList<>();
for(OClass oSubClass : toBeAnalysed) {
String name = oSubClass.getName();
CachedType cachedType = typesCache.getCachedType(name);
cachedType.setOClass(oSubClass);
allSubClasses.add(allSubClasses.size(), name);
subclasses.addAll(oSubClass.getSubclasses());
}
}
return allSubClasses;
}
private OClass internalGetOClass() throws SchemaNotFoundException, SchemaException, ResourceRegistryException {
if(oClass==null) {
oClass = retrieveOClass();
}
return oClass;
}
private Type internalGetType() throws SchemaNotFoundException, SchemaException, ResourceRegistryException {
if(type==null) {
ElementManagement<? extends OElement> erManagement = SchemaManagement.getTypeManagement(internalGetOClass());
String typeString = erManagement.read();
try {
type = TypeMapper.deserializeTypeDefinition(typeString);
} catch (Exception e) {
throw new ResourceRegistryException(e);
}
}
return type;
}
private AccessType internalGetAccessType() throws SchemaNotFoundException, SchemaException, ResourceRegistryException {
if(accessType==null) {
if(type!=null) {
accessType = type.getAccessType();
@ -136,56 +197,39 @@ public class CachedType {
return accessType;
}
private List<String> internaGetSuperTypes() throws SchemaException, ResourceRegistryException {
if(superTypes==null) {
superTypes = getAllSuperclasses(superClassesToBeExcluded);
}
return superTypes;
}
private List<String> internalGetSubTypes() throws SchemaException, ResourceRegistryException {
if(subTypes==null) {
subTypes = getAllSubclasses();
}
return subTypes;
}
public synchronized OClass getOClass() throws SchemaNotFoundException, SchemaException, ResourceRegistryException {
return internalGetOClass();
}
public synchronized Type getType() throws SchemaNotFoundException, SchemaException, ResourceRegistryException {
return internalGetType();
}
public synchronized AccessType getAccessType() throws SchemaNotFoundException, SchemaException, ResourceRegistryException {
return internalGetAccessType();
}
protected List<String> getSuperclasses(Collection<String> superClassesToBeExcluded) throws SchemaException, ResourceRegistryException {
TypesCache typesCache = TypesCache.getInstance();
List<String> superClasses = new ArrayList<>();
List<OClass> allSuperClasses = internalGetOClass().getSuperClasses();
while(allSuperClasses.size()>0) {
List<OClass> toBeAnalysed = new ArrayList<>(allSuperClasses);
allSuperClasses = new ArrayList<>();
for(OClass oSuperClass : toBeAnalysed) {
String name = oSuperClass.getName();
CachedType cachedType = typesCache.getType(name);
cachedType.setOClass(oSuperClass);
if(name.compareTo(StringFactory.V.toUpperCase()) == 0 || name.compareTo(StringFactory.E.toUpperCase()) == 0
|| name.compareTo(DatabaseEnvironment.O_RESTRICTED_CLASS) == 0) {
continue;
}
if(superClassesToBeExcluded.contains(name)) {
continue;
}
superClasses.add(superClasses.size(), name);
allSuperClasses.addAll(oSuperClass.getSuperClasses());
}
}
return superClasses;
}
public synchronized Type getType() {
if(type==null) {
}
return type;
}
public synchronized List<String> getSuperTypes() throws SchemaException, ResourceRegistryException {
if(superTypes==null) {
superTypes = getSuperclasses(superClassesToBeExcluded);
}
return superTypes;
return internaGetSuperTypes();
}
public synchronized Set<String> getSpecilisationTypes() {
if(specilisationTypes==null) {
}
return specilisationTypes;
public synchronized List<String> getSubTypes() throws SchemaException, ResourceRegistryException {
return internalGetSubTypes();
}
@Override

View File

@ -219,8 +219,7 @@ public class SchemaManagement {
private String getTypeAsString(ElementManagement<? extends OElement> erManagement) throws SchemaException {
try {
if(erManagement!=null) {
String ret = erManagement.read().toString();
return ret;
return erManagement.read();
}else {
throw new SchemaException("You can only request schema of IS Model types and their specilization");
}
@ -700,7 +699,7 @@ public class SchemaManagement {
}
}
protected String getSchema(boolean includeSubtypes) throws SchemaNotFoundException, SchemaException {
protected List<Type> getSchema(boolean includeSubtypes) throws SchemaNotFoundException, SchemaException {
ODatabaseDocument oDatabaseDocument = null;
try {
AdminSecurityContext adminSecurityContext = ContextUtility.getAdminSecurityContext();
@ -723,7 +722,7 @@ public class SchemaManagement {
}
}
return TypeMapper.serializeTypeDefinitions(typeDefinitions);
return typeDefinitions;
} catch(SchemaException e) {
throw e;
} catch(SchemaNotFoundException e) {
@ -782,7 +781,7 @@ public class SchemaManagement {
}
}
public String read(boolean includeSubtypes) throws SchemaNotFoundException, SchemaException {
public List<Type> read(boolean includeSubtypes) throws SchemaNotFoundException, SchemaException {
return getSchema(includeSubtypes);
}

View File

@ -28,7 +28,7 @@ public class TypesCache {
cachedTypes = new HashMap<>();
}
public CachedType getType(String typeName) {
public CachedType getCachedType(String typeName) {
CachedType cachedType = cachedTypes.get(typeName);
if(cachedType == null ) {
logger.trace("{} not in cache. Going to create {} instance", typeName, CachedType.class.getSimpleName());

View File

@ -62,7 +62,8 @@ public class SchemaManagementImplTest extends ContextTest {
public void getPropertyTypeSchema() throws Exception {
SchemaManagement schemaManagement = new SchemaManagement();
schemaManagement.setTypeName(Property.NAME);
String json = schemaManagement.read(false);
List<Type> types = schemaManagement.read(false);
String json = TypeMapper.serializeTypeDefinitions(types);
logger.debug(json);
}
@ -87,7 +88,8 @@ public class SchemaManagementImplTest extends ContextTest {
public void getFacetSchema() throws Exception {
SchemaManagement schemaManagement = new SchemaManagement();
schemaManagement.setTypeName(ContactFacet.NAME);
String json = schemaManagement.read(false);
List<Type> types = schemaManagement.read(false);
String json = TypeMapper.serializeTypeDefinitions(types);
logger.info(json);
List<Type> typeDefinitions = TypeMapper.deserializeTypeDefinitions(json);
logger.info("{}", typeDefinitions);
@ -114,7 +116,8 @@ public class SchemaManagementImplTest extends ContextTest {
public void getResourceSchema() throws Exception {
SchemaManagement schemaManagement = new SchemaManagement();
schemaManagement.setTypeName(Actor.NAME);
String json = schemaManagement.read(false);
List<Type> types = schemaManagement.read(false);
String json = TypeMapper.serializeTypeDefinitions(types);
logger.trace(json);
}
@ -134,7 +137,8 @@ public class SchemaManagementImplTest extends ContextTest {
SchemaManagement schemaManagement = new SchemaManagement();
schemaManagement.setTypeName(Resource.NAME);
String list = schemaManagement.read(includeSubTypes);
List<Type> types = schemaManagement.read(includeSubTypes);
String list = TypeMapper.serializeTypeDefinitions(types);
logger.info("{} list : {}", Resource.NAME, list);
}
@ -146,7 +150,8 @@ public class SchemaManagementImplTest extends ContextTest {
SchemaManagement schemaManagement = new SchemaManagement();
schemaManagement.setTypeName(Facet.NAME);
String list = schemaManagement.read(includeSubTypes);
List<Type> types = schemaManagement.read(includeSubTypes);
String list = TypeMapper.serializeTypeDefinitions(types);
logger.info("{} list : {}", Facet.NAME, list);
}
@ -158,7 +163,8 @@ public class SchemaManagementImplTest extends ContextTest {
SchemaManagement schemaManagement = new SchemaManagement();
schemaManagement.setTypeName(ConsistsOf.NAME);
String list = schemaManagement.read(includeSubTypes);
List<Type> types = schemaManagement.read(includeSubTypes);
String list = TypeMapper.serializeTypeDefinitions(types);
logger.info("{} list : {}", ConsistsOf.NAME, list);
}
@ -170,7 +176,8 @@ public class SchemaManagementImplTest extends ContextTest {
SchemaManagement schemaManagement = new SchemaManagement();
schemaManagement.setTypeName(IsRelatedTo.NAME);
String list = schemaManagement.read(includeSubTypes);
List<Type> types = schemaManagement.read(includeSubTypes);
String list = TypeMapper.serializeTypeDefinitions(types);
logger.info("{} list : {}", IsRelatedTo.NAME, list);
}

View File

@ -0,0 +1,62 @@
package org.gcube.informationsystem.resourceregistry.types;
import java.util.List;
import org.gcube.informationsystem.base.reference.AccessType;
import org.gcube.informationsystem.types.TypeMapper;
import org.gcube.informationsystem.types.reference.Type;
import org.gcube.resourcemanagement.model.reference.entities.resources.RunningPlugin;
import org.gcube.resourcemanagement.model.reference.entities.resources.Service;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.orientechnologies.orient.core.metadata.schema.OClass;
public class TypesCacheTest {
private static Logger logger = LoggerFactory.getLogger(TypesCacheTest.class);
protected CachedType getCachedType(TypesCache typesCache, String typeName) throws Exception {
CachedType cachedType = typesCache.getCachedType(typeName);
OClass oClass = cachedType.getOClass();
AccessType accessType = cachedType.getAccessType();
Type type = cachedType.getType();
logger.debug("{} ({}) : {}", oClass.toString(), accessType.toString(), TypeMapper.serializeTypeDefinition(type));
return cachedType;
}
public void testCacheType(String typeName) throws Exception {
TypesCache typesCache = TypesCache.getInstance();
CachedType cachedType = getCachedType(typesCache, typeName);
List<String> superTypes = cachedType.getSuperTypes();
logger.debug("{} superTypes are {}", typeName, superTypes);
for(String superType : superTypes) {
getCachedType(typesCache, superType);
}
List<String> subTypes = cachedType.getSubTypes();
logger.debug("{} subTypes are {}", typeName, subTypes);
for(String subType : subTypes) {
getCachedType(typesCache, subType);
}
}
@Test
public void test() throws Exception {
testCacheType(Service.NAME);
testCacheType(RunningPlugin.NAME);
}
@Test
public void test2() throws Exception {
logger.debug("------------------------------------------");
testCacheType(Service.NAME);
testCacheType(RunningPlugin.NAME);
}
}

View File

@ -13,7 +13,7 @@
<logger name="org.gcube.informationsystem" level="TRACE" />
<logger name="org.gcube.informationsystem.types" level="INFO" />
<logger name="org.gcube.informationsystem.resourceregistry.dbinitialization" level="INFO" />
<logger name=" org.gcube.informationsystem.impl.utils.discovery" level="INFO" />
<logger name="org.gcube.informationsystem.utils.discovery" level="ERROR" />
<root level="WARN">
<appender-ref ref="STDOUT" />