Implementing Referential integrity

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/information-system/resource-registry@135078 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Luca Frosini 2016-11-29 17:49:43 +00:00
parent bcad10fe05
commit 78213468eb
3 changed files with 100 additions and 71 deletions

View File

@ -61,7 +61,6 @@ import com.tinkerpop.blueprints.Element;
import com.tinkerpop.blueprints.Vertex;
import com.tinkerpop.blueprints.impls.orient.OrientBaseGraph;
import com.tinkerpop.blueprints.impls.orient.OrientEdge;
import com.tinkerpop.blueprints.impls.orient.OrientElement;
import com.tinkerpop.blueprints.impls.orient.OrientGraph;
import com.tinkerpop.blueprints.impls.orient.OrientVertex;
@ -956,7 +955,7 @@ public class EntityManagementImpl implements EntityManagement {
}
}
}
@Override
public boolean deleteFacet(UUID uuid) throws FacetNotFoundException,
ResourceRegistryException {
@ -1364,6 +1363,86 @@ public class EntityManagementImpl implements EntityManagement {
}
}
private String getTraverseQueryToSelectEntityToDelete(String relationName, UUID uuid){
/*
* SELECT FROM (
* TRAVERSE outE('Relation') FROM
* //( SELECT FROM Resource WHERE header.uuid = 'c2001636-9227-425e-a3f5-67902c6fe301' )
* #<cluster>:<position>
* )
*
* WHERE (
* relationProperty.referentialIntegrity = 'onDeleteCascade' OR
* (
* relationProperty.referentialIntegrity = 'onDeleteCascadeWhenOrphan' AND
* inV().inE().size()=1
* )
*)
*
*/
String query =
"SELECT FROM ("
+ "TRAVERSE outE('" + relationName +"') FROM "
+ "(SELECT FROM " + Resource.NAME + " WHERE " + Resource.HEADER_PROPERTY + "." + Header.UUID_PROPERTY + " = '" + uuid.toString() + "'" + ")"
//+ rid
+ ") "
+ "WHERE ("
+ Relation.RELATION_PROPERTY + "." + RelationProperty.REFERENTIAL_INTEGRITY + " = '" + ReferentiaIntegrity.onDeleteCascade.toString() + "' OR ("
+ Relation.RELATION_PROPERTY + "." + RelationProperty.REFERENTIAL_INTEGRITY + " = '" + ReferentiaIntegrity.onDeleteCascadeWhenOrphan.toString() + "' AND "
+ "inV().inE().size()=1"
+ ")"
+ ")";
return query;
}
private void internalDeleteResource(OrientGraph orientGraph, UUID uuid, Set<UUID> excludes) throws ResourceNotFoundException, ResourceRegistryException{
if(excludes == null){
excludes = new HashSet<>();
}
excludes.add(uuid);
Vertex resource = getEntity(orientGraph, uuid, null, Resource.class);
String query = getTraverseQueryToSelectEntityToDelete(IsRelatedTo.NAME, uuid);
OSQLSynchQuery<OrientEdge> osqlSynchQuery = new OSQLSynchQuery<>(query);
Iterable<OrientEdge> iterable = orientGraph.command(osqlSynchQuery).execute();
if (iterable != null && iterable.iterator() != null) {
Iterator<OrientEdge> iterator = iterable.iterator();
while (iterator.hasNext()) {
OrientEdge relation = iterator.next();
ODocument oDocument = relation.getInVertex().getRecord();
ODocument header = oDocument.field(Entity.HEADER_PROPERTY);
String uuidString = header.field(Header.UUID_PROPERTY);
UUID resourceUUID = UUID.fromString(uuidString);
if (uuid.compareTo(resourceUUID)!=0 && !excludes.contains(resourceUUID)){
internalDeleteResource(orientGraph, resourceUUID, excludes);
}
}
}
query = getTraverseQueryToSelectEntityToDelete(ConsistsOf.NAME, uuid);
osqlSynchQuery = new OSQLSynchQuery<>(query);
iterable = orientGraph.command(osqlSynchQuery).execute();
if (iterable != null && iterable.iterator() !=null) {
Iterator<OrientEdge> iterator = iterable.iterator();
while(iterator.hasNext()){
OrientEdge relation = iterator.next();
ODocument oDocument = relation.getInVertex().getRecord();
oDocument.delete();
}
}
resource.remove();
}
@Override
public boolean deleteResource(UUID uuid) throws ResourceNotFoundException,
ResourceRegistryException {
@ -1376,62 +1455,10 @@ public class EntityManagementImpl implements EntityManagement {
orientGraph = ContextUtility
.getActualSecurityContextGraph(PermissionMode.WRITER);
Vertex resource = getEntity(orientGraph, uuid, null, Resource.class);
/*
* SELECT FROM (
* TRAVERSE outE('ConsistsOf') FROM
* #<cluster>:<position>
* // Use this instead of the previous if you want to skip getEntity
* // ( SELECT FROM Resource WHERE header.uuid = 'c2001636-9227-425e-a3f5-67902c6fe301' )
* )
*
* WHERE (
* relationProperty IS null OR
* relationProperty.referentialIntegrity IS null OR
* relationProperty.referentialIntegrity = 'onDeleteCascade' OR (
* relationProperty.referentialIntegrity = 'onDeleteCascadeWhenOrphan' AND
* inV().inE().size()=1
* )
*)
*
*/
String query =
"SELECT FROM ("
+ "TRAVERSE outE('" + ConsistsOf.NAME +"') FROM "
+ resource.getId()
//+ "SELECT FROM " + Resource.NAME + " WHERE " + Resource.HEADER_PROPERTY + "." + Header.UUID_PROPERTY + " = '" + uuid.toString() + "'" + ")"
+ ") "
+ "WHERE ("
+ Relation.RELATION_PROPERTY + " IS null OR "
+ Relation.RELATION_PROPERTY + "." + RelationProperty.REFERENTIAL_INTEGRITY + " IS null OR "
+ Relation.RELATION_PROPERTY + "." + RelationProperty.REFERENTIAL_INTEGRITY + " = '" + ReferentiaIntegrity.onDeleteCascade.toString() + "' OR ("
+ Relation.RELATION_PROPERTY + "." + RelationProperty.REFERENTIAL_INTEGRITY + " = '" + ReferentiaIntegrity.onDeleteCascadeWhenOrphan.toString() + "' AND "
+ "inV().inE().size()=1"
+ ")"
+ ")";
internalDeleteResource(orientGraph, uuid, null);
OSQLSynchQuery<OrientElement> osqlSynchQuery = new OSQLSynchQuery<>(query);
Iterable<OrientElement> iterable = orientGraph.command(osqlSynchQuery).execute();
if (iterable != null && iterable.iterator() !=null) {
Iterator<OrientElement> iterator = iterable.iterator();
while(iterator.hasNext()){
OrientElement element = iterator.next();
if(element instanceof OrientVertex){
continue;
}else{
OrientEdge consistsOf = (OrientEdge) element;
consistsOf.getInVertex().getRecord().delete();
}
}
}
resource.remove();
orientGraph.commit();
logger.info("{} with UUID {} was successfully deleted.",
Resource.NAME, uuid);

View File

@ -213,7 +213,7 @@ public class ContextManagementImplTest {
logger.debug("The DB should be now clean");
}
//@Test
@Test
public void createDevContext() throws ContextNotFoundException,
ContextException, InternalException, JsonParseException,
JsonMappingException, IOException {

View File

@ -24,6 +24,7 @@ import java.util.regex.Pattern;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.informationsystem.impl.embedded.HeaderImpl;
import org.gcube.informationsystem.impl.embedded.RelationPropertyImpl;
import org.gcube.informationsystem.impl.entity.facet.CPUFacetImpl;
import org.gcube.informationsystem.impl.entity.facet.ContainerStateFacetImpl;
import org.gcube.informationsystem.impl.entity.facet.MemoryFacetImpl;
@ -31,11 +32,13 @@ import org.gcube.informationsystem.impl.entity.facet.NetworkingFacetImpl;
import org.gcube.informationsystem.impl.entity.facet.SimplePropertyFacetImpl;
import org.gcube.informationsystem.impl.entity.facet.SoftwareFacetImpl;
import org.gcube.informationsystem.impl.entity.resource.HostingNodeImpl;
import org.gcube.informationsystem.impl.relation.ConsistsOfImpl;
import org.gcube.informationsystem.impl.relation.IsIdentifiedByImpl;
import org.gcube.informationsystem.impl.relation.consistsof.HasPersistentMemoryImpl;
import org.gcube.informationsystem.impl.relation.consistsof.HasVolatileMemoryImpl;
import org.gcube.informationsystem.impl.utils.Entities;
import org.gcube.informationsystem.model.embedded.Header;
import org.gcube.informationsystem.model.embedded.RelationProperty;
import org.gcube.informationsystem.model.entity.Facet;
import org.gcube.informationsystem.model.entity.Resource;
import org.gcube.informationsystem.model.entity.facet.CPUFacet;
@ -52,7 +55,6 @@ import org.gcube.informationsystem.model.relation.IsIdentifiedBy;
import org.gcube.informationsystem.model.relation.consistsof.HasPersistentMemory;
import org.gcube.informationsystem.model.relation.consistsof.HasVolatileMemory;
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.resource.ResourceNotFoundException;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -127,15 +129,19 @@ public class SmartgearResourcesTest {
}
networkingFacet.setHostName("pc-frosini.isti.cnr.it");
networkingFacet.setDomainName(getDomain(networkingFacet.getHostName()));
networkingFacet.setAdditionalProperty("Port", 8080);
RelationProperty relationProperty = new RelationPropertyImpl();
relationProperty.setReferentialIntegrity(RelationProperty.ReferentiaIntegrity.onDeleteCascadeWhenOrphan);
IsIdentifiedBy<HostingNode, NetworkingFacet> isIdentifiedBy =
new IsIdentifiedByImpl<>(hostingNode, networkingFacet, null);
new IsIdentifiedByImpl<>(hostingNode, networkingFacet, relationProperty);
hostingNode.addFacet(isIdentifiedBy);
List<CPUFacet> cpuFacets = getCPUFacets();
for (CPUFacet cpuFacet: cpuFacets) {
hostingNode.addFacet(cpuFacet);
ConsistsOf<HostingNode, CPUFacet> consistsOf = new ConsistsOfImpl<HostingNode, CPUFacet>(hostingNode, cpuFacet, relationProperty);
hostingNode.addFacet(consistsOf);
}
@ -144,11 +150,13 @@ public class SmartgearResourcesTest {
softwareFacet.setGroup(mxbean.getName()); // softwareFacet.setGroup(System.getProperty("os.name"));
softwareFacet.setName(mxbean.getArch()); // softwareFacet.setName(System.getProperty("os.arch"));
softwareFacet.setVersion(mxbean.getVersion()); // softwareFacet.setName(System.getProperty("os.version"));
hostingNode.addFacet(softwareFacet);
ConsistsOf<HostingNode, SoftwareFacet> sfR = new ConsistsOfImpl<HostingNode, SoftwareFacet>(hostingNode, softwareFacet, relationProperty);
hostingNode.addFacet(sfR);
SimplePropertyFacet simplePropertyFacet = addEnvironmentVariables();
hostingNode.addFacet(simplePropertyFacet);
ConsistsOf<HostingNode, SimplePropertyFacet> spfR = new ConsistsOfImpl<HostingNode, SimplePropertyFacet>(hostingNode, simplePropertyFacet, relationProperty);
hostingNode.addFacet(spfR);
ContainerStateFacet containerStateFacet = getContainerStateFacet(null);
hostingNode.addFacet(containerStateFacet);
@ -157,20 +165,20 @@ public class SmartgearResourcesTest {
MemoryFacet ramFacet = getRamInfo(null);
HasVolatileMemory<HostingNode, MemoryFacet> hasVolatileRAMMemory =
new HasVolatileMemoryImpl<HostingNode, MemoryFacet>(
hostingNode, ramFacet, null);
hostingNode, ramFacet, relationProperty);
hasVolatileRAMMemory.setAdditionalProperty(MEMORY_TYPE, MEMORY_TYPE_RAM);
hostingNode.addFacet(hasVolatileRAMMemory);
MemoryFacet jvmMemoryFacet = getJVMMemoryInfo(null);
HasVolatileMemory<HostingNode, MemoryFacet> hasVolatileJVMMemory =
new HasVolatileMemoryImpl<HostingNode, MemoryFacet>(
hostingNode, jvmMemoryFacet, null);
hostingNode, jvmMemoryFacet, relationProperty);
hasVolatileJVMMemory.setAdditionalProperty(MEMORY_TYPE, MEMORY_TYPE_JVM);
hostingNode.addFacet(hasVolatileJVMMemory);
MemoryFacet disk = getDiskSpace(null);
HasPersistentMemory<HostingNode, MemoryFacet> hasPersistentMemory =
new HasPersistentMemoryImpl<HostingNode, MemoryFacet>(hostingNode, disk, null);
new HasPersistentMemoryImpl<HostingNode, MemoryFacet>(hostingNode, disk, relationProperty);
hostingNode.addFacet(hasPersistentMemory);
String json = entityManagementImpl.createResource(HostingNode.NAME, Entities.marshal(hostingNode));
@ -228,12 +236,6 @@ public class SmartgearResourcesTest {
}
@Test
public void testDelete() throws ResourceNotFoundException, ResourceRegistryException{
ScopeProvider.instance.set("/gcube/devNext");
entityManagementImpl.deleteResource(UUID.fromString("9a2cedcb-2df2-49d7-93b9-f2b34d70fc06"));
}
private ContainerStateFacet getContainerStateFacet(ContainerStateFacet containerStateFacet){
if(containerStateFacet == null){
containerStateFacet = new ContainerStateFacetImpl();