]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/AssertedStatements.java
Multiple reader thread support for db client
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / AssertedStatements.java
index 41b8e08d59486690543fd79494195c76395a01f9..8649de32eeee907b1e551a4e681b165c1edd1768 100644 (file)
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * Copyright (c) 2007, 2018 Association for Decentralized Information Management
  * in Industry THTH ry.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  *******************************************************************************/
 package org.simantics.db.impl.query;
 
-import java.util.concurrent.Semaphore;
 import java.util.concurrent.atomic.AtomicInteger;
 
 import org.simantics.db.RelationInfo;
+import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.exception.ManyObjectsForFunctionalRelationException;
 import org.simantics.db.impl.graph.ReadGraphImpl;
 import org.simantics.db.impl.procedure.InternalProcedure;
 import org.simantics.db.impl.procedure.TripleIntProcedureAdapter;
-import org.simantics.db.procedure.ListenerBase;
 import org.simantics.db.request.RequestFlags;
 
 
 final public class AssertedStatements extends CollectionBinaryQuery<TripleIntProcedure> {
     
-//    public ArrayList<TripleIntProcedure> procs;
-    
     public AssertedStatements(final int r1, final int r2) {
         super(r1, r2);
     }
@@ -34,82 +31,20 @@ final public class AssertedStatements extends CollectionBinaryQuery<TripleIntPro
     public static AssertedStatements newInstance(final int r1, final int r2) {
         return new AssertedStatements(r1, r2);
     }
-    
-    final static AssertedStatements runner(ReadGraphImpl graph, final int r1, final int r2, final QueryProcessor provider, final AssertedStatements cached, final CacheEntry parent, final ListenerBase listener, final TripleIntProcedure procedure) {
-        
-        AssertedStatements entry = cached != null ? cached : (AssertedStatements)provider.assertedStatementsMap.get(id(r1,r2)); 
-        if(entry == null) {
-               
-               entry = new AssertedStatements(r1, r2);
-               entry.setPending();
-               entry.clearResult(provider.querySupport);
-               entry.putEntry(provider);
-               
-            provider.performForEach(graph, entry, parent, listener, procedure);
-            
-            return entry;
-            
-        } else {
-               
-            if(entry.isPending()) {
-                synchronized(entry) {
-                    if(entry.isPending()) {
-                        throw new IllegalStateException();
-//                     if(entry.procs == null) entry.procs = new ArrayList<TripleIntProcedure>();
-//                        entry.procs.add(procedure);
-//                        provider.registerDependencies(graph, entry, parent, listener, procedure, false);
-//                        return entry;
-                    }
-                }
-            }
-            provider.performForEach(graph, entry, parent, listener, procedure);
-        }
-        
-        return entry;
-        
-    }
-    
-    final public static AssertedStatements queryEach(ReadGraphImpl graph, final int r1, final int r2, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final TripleIntProcedure procedure) {
-        
-        assert(r1 != 0);
-        assert(r2 != 0);
-        
-        final AssertedStatements entry = (AssertedStatements)provider.assertedStatementsMap.get(id(r1,r2));
-        
-        if(parent == null && !(listener != null)) {
-            if(entry != null && entry.isReady()) { 
-                entry.performFromCache(graph, provider, procedure);
-                return entry;
-            }
-        }
-
-        return runner(graph, r1, r2, provider, entry, parent, listener, procedure);
-         
-    }
-
-    @Override
-    public BinaryQuery<TripleIntProcedure> getEntry(QueryProcessor provider) {
-        return provider.assertedStatementsMap.get(id);
-    }
-       
-       @Override
-       public void putEntry(QueryProcessor provider) {
-        provider.assertedStatementsMap.put(id, this);
-       }
 
        @Override
        final public void removeEntry(QueryProcessor provider) {
-           provider.assertedStatementsMap.remove(id);
+           provider.cache.remove(this);
        }
        
-       void computeInheritedAssertions(ReadGraphImpl graph, int type, final int predicate, final RelationInfo ri, final QueryProcessor queryProvider, final TripleIntProcedure proc) {
+       static void computeInheritedAssertions(ReadGraphImpl graph, int type, final int predicate, final RelationInfo ri, final AssertedStatements entry, final TripleIntProcedure proc) throws DatabaseException {
            
-//        final AtomicBoolean found = new AtomicBoolean(0);
+        QueryProcessor processor = graph.processor;
 
-           DirectObjects.queryEach(graph, type, queryProvider.getInherits(), queryProvider, this, null, new SyncIntProcedure() {
+        QueryCache.runnerDirectObjects(graph, type, processor.getInherits(), entry, null, new SyncIntProcedure() {
 
             @Override
-            public void run(ReadGraphImpl graph) {
+            public void run(ReadGraphImpl graph) throws DatabaseException {
 
 //                if(ri.isFunctional && found.get() == 1) {
 //
@@ -120,29 +55,29 @@ final public class AssertedStatements extends CollectionBinaryQuery<TripleIntPro
 //                    
 //                }
 
-                finish(graph, queryProvider);
+                finish(graph, entry);
                 proc.finished(graph);
                 
             }
 
             @Override
-            public void execute(ReadGraphImpl graph, int inh) {
+            public void execute(ReadGraphImpl graph, int inh) throws DatabaseException {
                 
 //                if(ri.isFunctional && found.get() == 1) return;
 
                 inc();
                 
-                AssertedStatements.queryEach(graph, inh, predicate, queryProvider, AssertedStatements.this, null, new TripleIntProcedureAdapter() {
+                QueryCache.runnerAssertedStatements(graph, inh, predicate, entry, null, new TripleIntProcedureAdapter() {
 
                     @Override
-                    public void execute(ReadGraphImpl graph, int s, int p, int o) {
+                    public void execute(ReadGraphImpl graph, int s, int p, int o) throws DatabaseException {
 
 //                        if(ri.isFunctional) {
 //                            
 ////                            if(found.get() == 1) return;
 //
 //                            if(found.compareAndSet(0, o)) {
-                                if(addOrSet(s,p,o))
+                                if(addOrSet(s,p,o, entry))
                                        proc.execute(graph, s, p, o);
 //                            }
 //                            // If this was a duplicate, we can ignore this
@@ -164,12 +99,12 @@ final public class AssertedStatements extends CollectionBinaryQuery<TripleIntPro
                     }
 
                     @Override
-                    public void finished(ReadGraphImpl graph) {
+                    public void finished(ReadGraphImpl graph) throws DatabaseException {
                         dec(graph);
                     }
                                
                                @Override
-                               public void exception(ReadGraphImpl graph, Throwable t) {
+                               public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
                                        proc.exception(graph, t);
                                        dec(graph);
                     }
@@ -179,7 +114,7 @@ final public class AssertedStatements extends CollectionBinaryQuery<TripleIntPro
             }
 
             @Override
-            public void finished(ReadGraphImpl graph) {
+            public void finished(ReadGraphImpl graph) throws DatabaseException {
                 dec(graph);
             }
             
@@ -187,47 +122,62 @@ final public class AssertedStatements extends CollectionBinaryQuery<TripleIntPro
 
        }
 
-    void computeLocalAssertions(ReadGraphImpl graph, final int type, final int predicate, final RelationInfo ri, final QueryProcessor queryProvider, final TripleIntProcedure proc) {
+    static void computeLocalAssertions(ReadGraphImpl graph, final int type, final int predicate, final RelationInfo ri, final AssertedStatements entry, final TripleIntProcedure proc) throws DatabaseException {
+        
         
+    }
+
+       //@Override
+       public Object compute(ReadGraphImpl graph, final TripleIntProcedure procedure) throws DatabaseException {
+               computeForEach(graph, r1(), r2(), this, procedure);
+               return getResult();
+       }
+
+    public static void computeForEach(ReadGraphImpl graph, final int type, final int predicate, final AssertedStatements entry, final TripleIntProcedure procedure) throws DatabaseException {
+
+       RelationInfo ri = QueryCache.resultRelationInfoQuery(graph, predicate, entry, null);
+       
         final AtomicInteger found = new AtomicInteger(0);
 
-        DirectObjects.queryEach(graph, type, queryProvider.getAsserts(), queryProvider, this, null, new SyncIntProcedure() {
+        QueryProcessor processor = graph.processor;
+        
+        QueryCache.runnerDirectObjects(graph, type, processor.getAsserts(), entry, null, new SyncIntProcedure() {
 
             @Override
-            public void run(ReadGraphImpl graph) {
+            public void run(ReadGraphImpl graph) throws DatabaseException {
                 
                 if(ri.isFunctional && found.get() > 1) {
 
                     ManyObjectsForFunctionalRelationException exception = new ManyObjectsForFunctionalRelationException("Functional relation has more than one asserted statement.");
-                    except(exception);
-                    proc.exception(graph, exception);
+                    except(exception, entry);
+                    procedure.exception(graph, exception);
                     return;
                     
                 }
 
                 if(ri.isFunctional && found.get() == 1) {
                                         
-                    finish(graph, queryProvider);
-                    proc.finished(graph);
+                    finish(graph, entry);
+                    procedure.finished(graph);
                     return;
                     
                 }
 
-                computeInheritedAssertions(graph, type, predicate, ri, queryProvider, proc);
+                computeInheritedAssertions(graph, type, predicate, ri, entry, procedure);
                 
             }
 
             @Override
-            public void execute(ReadGraphImpl graph, final int ass) {
+            public void execute(ReadGraphImpl graph, final int ass) throws DatabaseException {
                 
                 if(ri.isFunctional && found.get() > 1) return;
                 
                 inc();
                 
-                DirectObjects.queryEach(graph, ass, queryProvider.getHasPredicate(), queryProvider, AssertedStatements.this, null, new IntProcedure() {
+                QueryCache.runnerDirectObjects(graph, ass, processor.getHasPredicate(), entry, null, new IntProcedure() {
 
                     @Override
-                    public void execute(ReadGraphImpl graph, final int pred) {
+                    public void execute(ReadGraphImpl graph, final int pred) throws DatabaseException {
                         
                         if(ri.isFunctional) {
                             
@@ -235,18 +185,18 @@ final public class AssertedStatements extends CollectionBinaryQuery<TripleIntPro
 
                             inc();
 
-                            DirectObjects.queryEach(graph, ass, queryProvider.getHasObject(), queryProvider, AssertedStatements.this, null, new IntProcedure() {
+                            QueryCache.runnerDirectObjects(graph, ass, processor.getHasObject(), entry, null, new IntProcedure() {
 
                                 @Override
-                                public void execute(ReadGraphImpl graph, final int object) {
+                                public void execute(ReadGraphImpl graph, final int object) throws DatabaseException {
                                     
                                     if(found.get() > 1) return;
 
                                     if(pred == predicate) {
 
                                        if(found.getAndIncrement() == 0) {
-                                               if(addOrSet(type, pred, object))
-                                                       proc.execute(graph, type, pred, object);
+                                               if(addOrSet(type, pred, object, entry))
+                                                       procedure.execute(graph, type, pred, object);
                                        }
                                        
                                         return;
@@ -257,10 +207,10 @@ final public class AssertedStatements extends CollectionBinaryQuery<TripleIntPro
 
                                         inc();
 
-                                        SuperRelations.queryEach(graph, pred, queryProvider, AssertedStatements.this, null, new InternalProcedure<IntSet>() {
+                                        QueryCache.runnerSuperRelations(graph, pred, entry, null, new InternalProcedure<IntSet>() {
                                             
                                             @Override
-                                            public void execute(ReadGraphImpl graph, IntSet result) {
+                                            public void execute(ReadGraphImpl graph, IntSet result) throws DatabaseException {
                                                 
                                                 if(found.get() > 1) {
                                                     dec(graph);
@@ -270,8 +220,8 @@ final public class AssertedStatements extends CollectionBinaryQuery<TripleIntPro
                                                 if(result.contains(predicate)) {
                     
                                                     if(found.getAndIncrement() == 0) {
-                                                               if(addOrSet(type, pred, object))
-                                                                       proc.execute(graph, type, pred, object);
+                                                               if(addOrSet(type, pred, object, entry))
+                                                                       procedure.execute(graph, type, pred, object);
                                                        }
                                                     
                                                 }
@@ -281,9 +231,9 @@ final public class AssertedStatements extends CollectionBinaryQuery<TripleIntPro
                                             }
                                                        
                                                        @Override
-                                                       public void exception(ReadGraphImpl graph, Throwable t) {
+                                                       public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
                                                            
-                                                               proc.exception(graph, t);
+                                                               procedure.exception(graph, t);
                                                 dec(graph);
                                                                
                                             }
@@ -295,13 +245,13 @@ final public class AssertedStatements extends CollectionBinaryQuery<TripleIntPro
                                 }
                                 
                                 @Override
-                                public void finished(ReadGraphImpl graph) {
+                                public void finished(ReadGraphImpl graph) throws DatabaseException {
                                     dec(graph);
                                 }
                                        
                                        @Override
-                                       public void exception(ReadGraphImpl graph, Throwable t) {
-                                               proc.exception(graph, t);
+                                       public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
+                                               procedure.exception(graph, t);
                                     dec(graph);
                                 }
 
@@ -311,15 +261,15 @@ final public class AssertedStatements extends CollectionBinaryQuery<TripleIntPro
 
                             inc();
 
-                            DirectObjects.queryEach(graph, ass, queryProvider.getHasObject(), queryProvider, AssertedStatements.this, null, new IntProcedure() {
+                            QueryCache.runnerDirectObjects(graph, ass, processor.getHasObject(), entry, null, new IntProcedure() {
 
                                 @Override
-                                public void execute(ReadGraphImpl graph, final int object) {
+                                public void execute(ReadGraphImpl graph, final int object) throws DatabaseException {
                                     
                                     if(pred == predicate) {
 
-                                        addOrSet(type, pred, object);
-                                        proc.execute(graph, type, pred, object);
+                                        addOrSet(type, pred, object, entry);
+                                        procedure.execute(graph, type, pred, object);
                                         return;
                                         
                                     }
@@ -328,15 +278,15 @@ final public class AssertedStatements extends CollectionBinaryQuery<TripleIntPro
 
                                         inc();
 
-                                        SuperRelations.queryEach(graph, pred, queryProvider, AssertedStatements.this, null, new InternalProcedure<IntSet>() {
+                                        QueryCache.runnerSuperRelations(graph, pred, entry, null, new InternalProcedure<IntSet>() {
                                             
                                             @Override
-                                            public void execute(ReadGraphImpl graph, IntSet result) {
+                                            public void execute(ReadGraphImpl graph, IntSet result) throws DatabaseException {
                                                 
                                                 if(result.contains(predicate)) {
                     
-                                                    addOrSet(type, pred, object);
-                                                    proc.execute(graph, type, pred, object);
+                                                    addOrSet(type, pred, object, entry);
+                                                    procedure.execute(graph, type, pred, object);
 
                                                 }
                                                 
@@ -345,8 +295,8 @@ final public class AssertedStatements extends CollectionBinaryQuery<TripleIntPro
                                             }
                                                        
                                                        @Override
-                                                       public void exception(ReadGraphImpl graph, Throwable t) {
-                                                               proc.exception(graph, t);
+                                                       public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
+                                                               procedure.exception(graph, t);
                                                 dec(graph);
                                             }
  
@@ -357,13 +307,13 @@ final public class AssertedStatements extends CollectionBinaryQuery<TripleIntPro
                                 }
                                 
                                 @Override
-                                public void finished(ReadGraphImpl graph) {
+                                public void finished(ReadGraphImpl graph) throws DatabaseException {
                                     dec(graph);
                                 }
                                        
                                        @Override
-                                       public void exception(ReadGraphImpl graph, Throwable t) {
-                                               proc.exception(graph, t);
+                                       public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
+                                               procedure.exception(graph, t);
                                     dec(graph);
                                 }
 
@@ -374,13 +324,13 @@ final public class AssertedStatements extends CollectionBinaryQuery<TripleIntPro
                     }
 
                     @Override
-                    public void finished(ReadGraphImpl graph) {
+                    public void finished(ReadGraphImpl graph) throws DatabaseException {
                         dec(graph);
                     }
                                
                                @Override
-                               public void exception(ReadGraphImpl graph, Throwable t) {
-                                       proc.exception(graph, t);
+                               public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
+                                       procedure.exception(graph, t);
                         dec(graph);
                     }
 
@@ -389,42 +339,21 @@ final public class AssertedStatements extends CollectionBinaryQuery<TripleIntPro
             }
 
             @Override
-            public void finished(ReadGraphImpl graph) {
+            public void finished(ReadGraphImpl graph) throws DatabaseException {
                 dec(graph);
             }
             
         });
         
-    }
-
-       @Override
-    public void computeForEach(ReadGraphImpl graph, final QueryProcessor provider, final TripleIntProcedure procedure, boolean store) {
-        
-        RelationInfoQuery.queryEach(graph, r2(), provider, this, null, new InternalProcedure<RelationInfo>() {
-
-            @Override
-            public void execute(ReadGraphImpl graph, RelationInfo ri) {
-                
-                computeLocalAssertions(graph, r1(), r2(), ri, provider, procedure);
-                
-            }
-                       
-                       @Override
-                       public void exception(ReadGraphImpl graph, Throwable t) {
-                               procedure.exception(graph, t);
-            }
-        });
-        
     }
     
     @Override
     public String toString() {
-       return "AssertedStatements2[" + r1() + " - " + r2() + "]";
+       return "AssertedStatements[" + r1() + " - " + r2() + "]";
     }
 
     private boolean addOrSet(int s, int p, int o) {
-        
+       
        assert(isPending());
         
         IntArray value = (IntArray)getResult();
@@ -449,39 +378,39 @@ final public class AssertedStatements extends CollectionBinaryQuery<TripleIntPro
         
     }
     
-    final private void finish(ReadGraphImpl graph, QueryProcessor provider) {
+    static boolean addOrSet(int s, int p, int o, AssertedStatements entry) {
+       if(entry != null) {
+               return entry.addOrSet(s, p, o);
+       } else {
+               return true;
+       }
+    }
+    
+    static void finish(ReadGraphImpl graph, AssertedStatements entry) {
         
-       assert(isPending());
-
-//        ArrayList<TripleIntProcedure> p = null;
-
-        synchronized(this) {
-               
-               setReady();
-//            p = procs;
-//            procs = null; 
-        
-        }
-        
-//        if(p != null) {
-//             final IntArray value = (IntArray)getResult();
-//             for(TripleIntProcedure proc : p) {
-//                     for(int i=0;i<value.size();i+=3) {
-//                             proc.execute(graph, value.data[i], value.data[i+1], value.data[i+2]);
-//                     }
-//             }
-//
-//             for(TripleIntProcedure proc : p) proc.finished(graph);
-//        }
+       assert(entry.isPending());
+       if(entry != null) {
+               synchronized(entry) {
+                       entry.setReady();
+               }
+       }
 
     }
+    
+    static void except(Throwable t, AssertedStatements entry) {
+       if(entry != null) {
+               synchronized(entry) {
+                       entry.except(t);
+               }
+       }
+    }
 
     @Override
-    public void performFromCache(ReadGraphImpl graph, QueryProcessor provider, final TripleIntProcedure procedure) {
+    public Object performFromCache(ReadGraphImpl graph, final TripleIntProcedure procedure) throws DatabaseException {
         
        assert(isReady());
         
-       if(handleException(graph, procedure)) return;
+       if(handleException(graph, procedure)) return getResult();
        
         final IntArray value = (IntArray)getResult();
         for(int i=0;i<value.size();i+=3) {
@@ -490,18 +419,17 @@ final public class AssertedStatements extends CollectionBinaryQuery<TripleIntPro
 
         procedure.finished(graph);
         
+        return value;
+        
     }
     
     @Override
-    public void recompute(ReadGraphImpl graph, QueryProcessor provider) {
+    public void recompute(ReadGraphImpl graph) throws DatabaseException {
         
-        final Semaphore s = new Semaphore(0);
-
-        computeForEach(graph, provider, new TripleIntProcedureAdapter() {
+        compute(graph, new TripleIntProcedureAdapter() {
 
             @Override
             public void finished(ReadGraphImpl graph) {
-                s.release();
             }
                        
                        @Override
@@ -509,13 +437,7 @@ final public class AssertedStatements extends CollectionBinaryQuery<TripleIntPro
                                throw new Error("Error in recompute", t);
             }
     
-        }, true);
-        
-        try {
-            s.acquire();
-        } catch (InterruptedException e) {
-            throw new Error(e);
-        }
+        });
         
     }