]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.impl/src/org/simantics/db/impl/graph/ReadGraphImpl.java
Optimize ReadGraph.hasStatements(s,p) to not use getObjects(s,p)
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / graph / ReadGraphImpl.java
index 9b6d74044ccacc0d27e0039d3646ed0b9d5fabe7..d6a0aa9f45dea69a4cbb87efa7be87e77df59745 100644 (file)
@@ -11,8 +11,6 @@
  *******************************************************************************/
 package org.simantics.db.impl.graph;
 
-import gnu.trove.map.hash.TObjectIntHashMap;
-
 import java.io.BufferedOutputStream;
 import java.io.File;
 import java.io.FileOutputStream;
@@ -29,6 +27,7 @@ import java.util.Iterator;
 import java.util.List;
 import java.util.ListIterator;
 import java.util.Set;
+import java.util.function.Consumer;
 
 import org.eclipse.core.runtime.Platform;
 import org.simantics.databoard.Accessors;
@@ -49,6 +48,7 @@ import org.simantics.db.DevelopmentKeys;
 import org.simantics.db.ExternalValueSupport;
 import org.simantics.db.ReadGraph;
 import org.simantics.db.RelationContext;
+import org.simantics.db.RelationInfo;
 import org.simantics.db.Resource;
 import org.simantics.db.Session;
 import org.simantics.db.Statement;
@@ -181,10 +181,11 @@ import org.simantics.scl.reflection.ValueNotFoundException;
 import org.simantics.scl.runtime.function.Function3;
 import org.simantics.utils.DataContainer;
 import org.simantics.utils.Development;
-import org.simantics.utils.datastructures.Callback;
 import org.simantics.utils.datastructures.Pair;
 import org.simantics.utils.datastructures.collections.CollectionUtils;
 
+import gnu.trove.map.hash.TObjectIntHashMap;
+
 public class ReadGraphImpl implements ReadGraph {
 
     final static boolean EMPTY_RESOURCE_CHECK = false;
@@ -283,7 +284,7 @@ public class ReadGraphImpl implements ReadGraph {
 
                } catch (ResourceNotFoundException e) {
 
-                       throw new ResourceNotFoundException(e);
+                       throw new ResourceNotFoundException(id, e);
 
                } catch (ValidationException e) {
 
@@ -347,7 +348,7 @@ public class ReadGraphImpl implements ReadGraph {
 
                } catch (ResourceNotFoundException e) {
 
-                       throw new ResourceNotFoundException(e);
+                       throw new ResourceNotFoundException(id, e);
 
                } catch (ServiceException e) {
 
@@ -900,83 +901,57 @@ public class ReadGraphImpl implements ReadGraph {
                if( relation == null) throw new IllegalArgumentException("relation can not be null");
 
                try {
-
                        int single = processor.getSingleObject(this, subject, relation);
-                       if(single == 0) throw new NoSingleResultException("subject=" + subject + " relation="+relation);
+                       if (single == 0) {
+                               if (EMPTY_RESOURCE_CHECK) {
+                                       if (!hasStatement(subject)) {
+                                               throw new EmptyResourceException("Resource " + debugString(subject));
+                                       }
+                               }
+                               throw new NoSingleResultException("No single object for subject " + debugString(subject)
+                                               + " and relation " + debugString(relation), single);
+                       }
                        return processor.querySupport.getResource(single);
-
                } catch (NoSingleResultException e) {
-                   
-                   if(EMPTY_RESOURCE_CHECK) {
-                       if(!hasStatement(subject)) throw new EmptyResourceException("Resource " + debugString(subject));
-                   }
-                   
-                       throw new NoSingleResultException("No single object for subject "
-                                       + debugString(subject) + " and relation "
-                                       + debugString(relation), e);
-            
+                       throw e;
                } catch (DatabaseException e) {
-                   
                        throw new ServiceException(e);
-                       
                } 
-               
        }
 
        @Override
-       final public Statement getSingleStatement(final Resource subject,
-                       final Resource relation) throws NoSingleResultException,
-                       ManyObjectsForFunctionalRelationException, ServiceException {
-
+       final public Statement getSingleStatement(final Resource subject, final Resource relation) throws NoSingleResultException, ManyObjectsForFunctionalRelationException, ServiceException {
                assert (subject != null);
                assert (relation != null);
-
                try {
-
                        Collection<Statement> statements = getStatements(subject, relation);
-                       if(statements.size() == 1) return statements.iterator().next();
-                       else throw new NoSingleResultException("");
-
-        } catch (NoSingleResultException e) {
-            
-            if(EMPTY_RESOURCE_CHECK) {
-                if(!hasStatement(subject)) throw new EmptyResourceException("Resource " + debugString(subject));
-            }
-            
-            throw new NoSingleResultException("No single statement for subject "
-                    + debugString(subject) + " and relation "
-                    + debugString(relation), e);
-            
-        } catch (DatabaseException e) {
-            
-            throw new ServiceException(e);
-            
-        } 
-
+                       if (statements.size() == 1) {
+                               return statements.iterator().next();
+                       } else {
+                               if (EMPTY_RESOURCE_CHECK)
+                                       if (!hasStatement(subject))
+                                               throw new EmptyResourceException("Resource " + debugString(subject));
+                               throw new NoSingleResultException("No single statement for subject " + debugString(subject)
+                                               + " and relation " + debugString(relation), statements.size());
+                       }
+               } catch (ServiceException e) {
+                       throw new ServiceException(e);
+               } 
        }
 
        @Override
-       final public Resource getSingleType(final Resource subject) throws NoSingleResultException,
-                       ServiceException {
-
+       final public Resource getSingleType(final Resource subject) throws NoSingleResultException, ServiceException {
                assert (subject != null);
-
                try {
-
                        ArrayList<Resource> principalTypes = (ArrayList<Resource>)getPrincipalTypes(subject);
-                       if(principalTypes.size() == 1) return principalTypes.get(0);
-                       else throw new NoSingleResultException("");
-
-               } catch (NoSingleResultException e) {
-
-                       throw new NoSingleResultException(e);
-
+                       if (principalTypes.size() == 1) {
+                           return principalTypes.get(0);
+                       } else {
+                           throw new NoSingleResultException("No single type for subject " + debugString(subject), principalTypes.size());
+                       }
                } catch (ServiceException e) {
-
                        throw new ServiceException(e);
-
                } 
-
        }
 
        @Override
@@ -988,23 +963,10 @@ public class ReadGraphImpl implements ReadGraph {
                assert (baseType != null);
 
                try {
-
                        return syncRequest(new SingleType(subject, baseType));
-
-               } catch (NoSingleResultException e) {
-
-                       throw new NoSingleResultException(new NoSingleResultException("subject=" + subject + ", baseType=" + baseType, e));
-
-               } catch (ServiceException e) {
-
-                       throw new ServiceException(e);
-
                } catch (DatabaseException e) {
-
-                       throw new ServiceException(INTERNAL_ERROR_STRING, e);
-
+                   throw new NoSingleResultException("subject=" + subject + ", baseType=" + baseType, 0, e);
                }
-
        }
 
        @Override
@@ -1142,16 +1104,11 @@ public class ReadGraphImpl implements ReadGraph {
                assert (relation != null);
 
                try {
-
                        Resource object = getSingleObject(subject, relation);
                        return getValue(object);
-                       
                } catch (NoSingleResultException e) {
-
-                       throw new NoSingleResultException(e);
-
+                       throw new NoSingleResultException("No single value found for subject " + debugString(subject) + " and relation " + debugString(relation), e.getResultCount(), e);
                } catch (DoesNotContainValueException e) {
-
                        try {
                                Layer0 L0 = processor.getL0(this);
                                Resource object = getPossibleObject(subject, relation);
@@ -1159,23 +1116,19 @@ public class ReadGraphImpl implements ReadGraph {
                                        if(isInstanceOf(object, L0.Literal)) {
                                                throw new DoesNotContainValueException(e);
                                        } else {
-                                               throw new InvalidLiteralException("The object " + object + " is not an instance of L0.Literal (use getRelatedValue2 instead)");
+                                               throw new InvalidLiteralException("The object " + object + " is not an instance of L0.Literal (use getRelatedValue2 instead)", e);
                                        }
                                } else {
-                                       throw new DoesNotContainValueException("The object " + object + " is not an instance of L0.Value");
+                                       throw new DoesNotContainValueException("The object " + object + " is not an instance of L0.Value", e);
                                }
                        } catch (DoesNotContainValueException e2) {
                                throw e2;
                        } catch (DatabaseException e2) {
                                throw new InternalException("The client failed to analyse the cause of the following exception", e);
                        }
-
                } catch (ServiceException e) {
-
                        throw new ServiceException(e);
-
-               } 
-               
+               }
        }
 
     @Override
@@ -1186,16 +1139,11 @@ public class ReadGraphImpl implements ReadGraph {
         assert (relation != null);
 
         try {
-
             Resource object = getSingleObject(subject, relation);
             return getVariantValue(object);
-            
         } catch (NoSingleResultException e) {
-
-            throw new NoSingleResultException(e);
-
+            throw new NoSingleResultException("No single object for subject " + debugString(subject) + " and relation " + debugString(relation), e.getResultCount(), e);
         } catch (DoesNotContainValueException e) {
-
             try {
                 Layer0 L0 = processor.getL0(this);
                 Resource object = getPossibleObject(subject, relation);
@@ -1203,23 +1151,19 @@ public class ReadGraphImpl implements ReadGraph {
                     if(isInstanceOf(object, L0.Literal)) {
                         throw new DoesNotContainValueException(e);
                     } else {
-                        throw new InvalidLiteralException("The object " + object + " is not an instance of L0.Literal (use getRelatedValue2 instead)");
+                        throw new InvalidLiteralException("The object " + object + " is not an instance of L0.Literal (use getRelatedValue2 instead)", e);
                     }
                 } else {
-                    throw new DoesNotContainValueException("The object " + object + " is not an instance of L0.Value");
+                    throw new DoesNotContainValueException("The object " + object + " is not an instance of L0.Value", e);
                 }
             } catch (DoesNotContainValueException e2) {
                 throw e2;
             } catch (DatabaseException e2) {
                 throw new InternalException("The client failed to analyse the cause of the following exception", e);
             }
-
         } catch (ServiceException e) {
-
             throw new ServiceException(e);
-
         } 
-        
     }
     
        @Override
@@ -1230,40 +1174,23 @@ public class ReadGraphImpl implements ReadGraph {
                assert (relation != null);
 
                try {
-
                        Resource object = getSingleObject(subject, relation);
                        return getValue(object, binding);
-
                } catch (NoSingleResultException e) {
-
                    String message = "";
-                   
                    try {
-                   
                    String subjectName = NameUtils.getSafeName(this, subject, true);
                    String relationName = NameUtils.getSafeName(this, relation, true);
                    message = "Subject: " + subjectName + ", Relation: " + relationName;
-
                    } catch (DatabaseException e2) {
                        
                    }
-
-            throw new NoSingleResultException(message);
-                   
+            throw new NoSingleResultException(message, e.getResultCount(), e);
                } catch (DoesNotContainValueException e) {
-
                        throw new DoesNotContainValueException(e);
-
                } catch (ServiceException e) {
-
                        throw new ServiceException(e);
-
-               } catch (DatabaseException e) {
-
-                       throw new ServiceException(INTERNAL_ERROR_STRING, e);
-
                }
-
        }
 
        @Override
@@ -1891,8 +1818,35 @@ public class ReadGraphImpl implements ReadGraph {
 
                try {
 
-                       Collection<Resource> objects = getObjects(subject, relation);
-                       return !objects.isEmpty();
+                       SyncReadProcedure<RelationInfo> procedure = new SyncReadProcedure<RelationInfo>();
+                       processor.forRelationInfo(this, relation, procedure);
+                       Collection<Resource> predicates = getPredicates(subject);
+                       
+                       if(procedure.result.isFinal) {
+                               
+                               return predicates.contains(relation);
+                               
+                       } else if (procedure.result.isFunctional) {
+
+                               try {
+                                       int result = processor.getSingleObject(this, subject, relation);
+                                       return result != 0;
+                               } catch (ManyObjectsForFunctionalRelationException e) {
+                                       return true;
+                               } catch (DatabaseException e) {
+                                       throw new ServiceException(e);
+                               }
+                               
+                       } else {
+
+                               for(Resource predicate : getPredicates(subject)) {
+                                       if(isSubrelationOf(predicate, relation))
+                                               return true;
+                               }
+                               
+                       }
+                       
+                       return false;
 
                } catch (ServiceException e) {
 
@@ -1949,7 +1903,7 @@ public class ReadGraphImpl implements ReadGraph {
 
        }
 
-       final AsyncProcedure NONE = new AsyncProcedure() {
+       final AsyncProcedure<?> NONE = new AsyncProcedure<Object>() {
 
                @Override
                public void execute(AsyncReadGraph graph, Object result) {
@@ -2152,7 +2106,7 @@ public class ReadGraphImpl implements ReadGraph {
 
                if (parent != null || listener != null || ((request.getFlags() & RequestFlags.SCHEDULE) > 0)) {
 
-                       Object syncParent = request;
+//                     Object syncParent = request;
 
 //                     final ReadGraphImpl newGraph = newSync();
 
@@ -2180,7 +2134,7 @@ public class ReadGraphImpl implements ReadGraph {
                        // System.out.println("direct call " + request );
 
                        // Do not set the sync state.parent for external threads
-                       Object syncParent = request;
+//                     Object syncParent = request;
 
 //                     final ReadGraphImpl newGraph = newSync();
 
@@ -2342,7 +2296,7 @@ public class ReadGraphImpl implements ReadGraph {
 
                if (parent != null || listener != null) {
 
-                       Object syncParent = request;
+//                     Object syncParent = request;
 
 //                     final ReadGraphImpl newGraph = newSync();
 
@@ -2352,7 +2306,7 @@ public class ReadGraphImpl implements ReadGraph {
 
                } else {
 
-                       Object syncParent = request;
+//                     Object syncParent = request;
 
 //                     final ReadGraphImpl newGraph = newSync();
 
@@ -2471,7 +2425,7 @@ public class ReadGraphImpl implements ReadGraph {
 
                if (parent != null || listener != null) {
 
-                       Object syncParent = request;
+//                     Object syncParent = request;
 
 //                     final ReadGraphImpl newGraph = newSync();
 
@@ -2519,7 +2473,7 @@ public class ReadGraphImpl implements ReadGraph {
 
                if (parent != null || listener != null) {
 
-                       Object syncParent = request;
+//                     Object syncParent = request;
 
 //                     final ReadGraphImpl newGraph = newSync();
 
@@ -2529,7 +2483,7 @@ public class ReadGraphImpl implements ReadGraph {
 
                } else {
 
-                       Object syncParent = request;
+//                     Object syncParent = request;
 
 //                     final ReadGraphImpl newGraph = newSync();
 
@@ -5912,7 +5866,7 @@ public class ReadGraphImpl implements ReadGraph {
        }
        
        @Override
-       public void asyncRequest(Write request, Callback<DatabaseException> callback) {
+       public void asyncRequest(Write request, Consumer<DatabaseException> callback) {
                
                assert (request != null);
                
@@ -5936,7 +5890,7 @@ public class ReadGraphImpl implements ReadGraph {
        
        @Override
        public void asyncRequest(DelayedWrite r,
-                       Callback<DatabaseException> callback) {
+                       Consumer<DatabaseException> callback) {
                throw new Error("Not implemented.");
        }
 
@@ -5955,7 +5909,7 @@ public class ReadGraphImpl implements ReadGraph {
        }
        
        @Override
-       public void asyncRequest(WriteOnly r, Callback<DatabaseException> callback) {
+       public void asyncRequest(WriteOnly r, Consumer<DatabaseException> callback) {
                throw new Error("Not implemented.");
        }
 
@@ -6470,7 +6424,6 @@ public class ReadGraphImpl implements ReadGraph {
        }
     }
 
-    @SuppressWarnings("unchecked")
     @Override
     public Variant getVariantValue2(Resource r, Object context) throws DatabaseException {
         Layer0 L0 = processor.getL0(this);