]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/Types.java
Multiple reader thread support for db client
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / Types.java
index 49cb5c4c61ac0fabb59d01cf326d96c7ba1ad0b1..a9fe36bc6bfcb60608e2758f3a28721c165e09dc 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 gnu.trove.procedure.TIntProcedure;
-
-import java.util.concurrent.Semaphore;
 import java.util.concurrent.atomic.AtomicInteger;
 
+import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.impl.graph.ReadGraphImpl;
 import org.simantics.db.impl.procedure.InternalProcedure;
-import org.simantics.db.procedure.ListenerBase;
 
-final public class Types extends UnaryQuery<InternalProcedure<IntSet>> {
-    
-//     public ArrayList<InternalProcedure<IntSet>> procs;
-       
-    private Types(final int resource) {
+import gnu.trove.procedure.TIntProcedure;
+
+public final class Types extends UnaryQueryP<IntSet> {
+
+    Types(int resource) {
         super(resource);
     }
-    
-    final static Types entry(final QueryProcessor provider, final int r) {
-        return (Types)provider.typesMap.get(r);
-    }
-    
-   final static void runner(ReadGraphImpl graph, final int r, final QueryProcessor provider, Types cached, final CacheEntry parent, final ListenerBase listener, final InternalProcedure<IntSet> procedure) {
-
-       Types entry = cached != null ? cached : (Types)provider.typesMap.get(r); 
-        if(entry == null) {
-               
-               entry = new Types(r);
-               entry.setPending();
-               entry.clearResult(provider.querySupport);
-               entry.putEntry(provider);
-               
-            provider.performForEach(graph, entry, parent, listener, procedure);
-            
-        } else {
-
-               if(!entry.isReady()) {
-                   throw new IllegalStateException();
-            }
-            provider.performForEach(graph, entry, parent, listener, procedure);
-        }
-       
-    }
-    
-    final static IntSet runner2(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent) throws Throwable {
-
-       Types entry = (Types)provider.typesMap.get(r); 
-        if(entry == null) {
-               
-               entry = new Types(r);
-               entry.setPending();
-               entry.clearResult(provider.querySupport);
-               entry.putEntry(provider);
-               
-            return (IntSet)provider.performForEach2(graph, entry, parent, null, null);
-            
-        } else {
-
-               if(!entry.isReady()) {
-                   throw new IllegalStateException();
-            }
-            return (IntSet)provider.performForEach2(graph, entry, parent, null, null);
-            
-        }
-       
+
+    @Override
+    public final void removeEntry(QueryProcessor provider) {
+        provider.cache.remove(this);
     }
-    
-    final public static void queryEach(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final InternalProcedure<IntSet> procedure) {
-        
-       final Types entry = (Types)provider.typesMap.get(r);
-       
-       if(parent == null && listener == null) {
-               if(entry != null && entry.isReady()) { 
-                       entry.performFromCache(graph, provider, procedure);
-                       return;
-               }
-        }
-       
-        runner(graph, r, provider, entry, parent, listener, procedure);
-         
+
+    @Override
+    public void compute(final ReadGraphImpl graph, final InternalProcedure<IntSet> procedure) throws DatabaseException {
+        computeForEach(graph, id, this, procedure);
     }
 
-    final public static IntSet queryEach2(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent) throws Throwable {
-        
-        if(parent == null) {
-            Types entry = (Types)provider.typesMap.get(r);
-            if(entry != null && entry.isReady()) {
-                return (IntSet)entry.get(graph, provider, null);
-            }
-        }
-        
-        return runner2(graph, r, provider, parent);
-         
+    public static void computeForEach(final ReadGraphImpl graph, int id, Types entry,
+            final InternalProcedure<IntSet> procedure_) throws DatabaseException {
+
+        InternalProcedure<IntSet> procedure = entry != null ? entry : procedure_;
+
+        computeForEach2(graph, id, entry, procedure);
+
+        if (entry != null)
+            entry.performFromCache(graph, procedure_);
+
     }
-    
-       @Override
-       public UnaryQuery<InternalProcedure<IntSet>> getEntry(QueryProcessor provider) {
-        return provider.typesMap.get(id);
-       }
-       
-       @Override
-       public void putEntry(QueryProcessor provider) {
-        provider.typesMap.put(id, this);
-       }
 
-       @Override
-       final public void removeEntry(QueryProcessor provider) {
-               provider.typesMap.remove(id);
-       }
+    public static void computeForEach2(final ReadGraphImpl graph, int id, Types parent,
+            final InternalProcedure<IntSet> procedure) throws DatabaseException {
 
-       @Override
-       public Object computeForEach(final ReadGraphImpl graph, final QueryProcessor queryProvider, final InternalProcedure<IntSet> procedure, final boolean store) {
+        QueryProcessor processor = graph.processor;
 
-               queryProvider.querySupport.ensureLoaded(graph, id);
-               int ret = queryProvider.querySupport.getSingleInstance(id);
-               if(ret > 0) {
+        processor.querySupport.ensureLoaded(graph, id);
 
-                       TypeHierarchy.queryEach(graph, ret, queryProvider, store ? Types.this : null, null, new InternalProcedure<IntSet>() {
+        int ret = processor.querySupport.getSingleInstance(id);
+        if (ret > 0) {
 
-                               @Override
-                               public void execute(ReadGraphImpl graph, IntSet types) {
+            TypeHierarchy.queryEach(graph, ret, processor, parent, null, new InternalProcedure<IntSet>() {
 
-                                       addOrSet(graph, types, queryProvider);
-                                       procedure.execute(graph, types);
+                @Override
+                public void execute(ReadGraphImpl graph, IntSet types) throws DatabaseException {
+                    procedure.execute(graph, types);
+                }
 
-                               }
+                @Override
+                public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
+                    procedure.exception(graph, t);
+                }
 
-                               @Override
-                               public void exception(ReadGraphImpl graph, Throwable t) {
-                                       procedure.exception(graph, t);
-                               }
+            });
 
-                       });
+            return;
 
-                       return getResult();
+        }
 
-               }
-               
-               final int instanceOf = queryProvider.getInstanceOf();
-        final int inherits = queryProvider.getInherits();
-        final int subrelationOf = queryProvider.getSubrelationOf();
+        final int instanceOf = processor.getInstanceOf();
+        final int inherits = processor.getInherits();
+        final int subrelationOf = processor.getSubrelationOf();
+
+        final IntSet result = new IntSet(processor.querySupport);
 
-        final IntSet result = new IntSet(queryProvider.querySupport);
-        
         final TIntProcedure addToResult = new TIntProcedure() {
             @Override
             public boolean execute(int r) {
-               synchronized(result) {
-                       result.add(r);
-               }
+                synchronized (result) {
+                    result.add(r);
+                }
                 return true;
             }
         };
-        
+
         final AtomicInteger finishes = new AtomicInteger(0);
-        
+
         SyncIntProcedure instanceOfProcedure = new SyncIntProcedure() {
-            
+
             @Override
-            public void run(ReadGraphImpl graph) {
-               
-                if(finishes.addAndGet(1) == 3) {
-                    if(store) addOrSet(graph, result, queryProvider);
+            public void run(ReadGraphImpl graph) throws DatabaseException {
+
+                if (finishes.addAndGet(1) == 3) {
                     procedure.execute(graph, result);
-               }
-                
+                }
+
             }
-            
+
             @Override
-            public void execute(ReadGraphImpl graph, int i) {
-                
-               synchronized(result) {
-                       result.add(i);
-               }
-                
+            public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
+
+                result.add(i);
+
                 inc();
 
-                SuperTypes.queryEach(graph, i, queryProvider, store ? Types.this : null, null, new InternalProcedure<IntSet>() {
+                QueryCache.runnerSuperTypes(graph, i, parent, null, new InternalProcedure<IntSet>() {
 
                     @Override
-                    public void execute(ReadGraphImpl graph, IntSet types) {
+                    public void execute(ReadGraphImpl graph, IntSet types) throws DatabaseException {
                         types.forEach(addToResult);
                         dec(graph);
                     }
-                               
-                               @Override
-                               public void exception(ReadGraphImpl graph, Throwable t) {
-                                       procedure.exception(graph, t);
+
+                    @Override
+                    public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
+                        procedure.exception(graph, t);
                         dec(graph);
-                   }
+                    }
 
                 });
-                
+
             }
 
             @Override
-            public void finished(ReadGraphImpl graph) {
+            public void finished(ReadGraphImpl graph) throws DatabaseException {
                 dec(graph);
             }
-            
-        }; 
-        
+
+        };
+
         SyncIntProcedure inheritsProcedure = new SyncIntProcedure() {
-            
+
             @Override
-            public void run(ReadGraphImpl graph) {
+            public void run(ReadGraphImpl graph) throws DatabaseException {
 
-               int current = finishes.addAndGet(1);
-               if(current == 3) {
-                    if(store) addOrSet(graph, result, queryProvider);
+                int current = finishes.addAndGet(1);
+                if (current == 3) {
                     procedure.execute(graph, result);
-               }
-                
+                }
+
             }
-            
+
             @Override
-            public void execute(ReadGraphImpl graph, int i) {
-                
+            public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
+
                 inc();
 
-                Types.queryEach(graph, i, queryProvider, store ? Types.this : null, null, new InternalProcedure<IntSet>() {
+                QueryCache.runnerTypes(graph, i, parent, null, new InternalProcedure<IntSet>() {
 
                     @Override
-                    public void execute(ReadGraphImpl graph, IntSet types) {
+                    public void execute(ReadGraphImpl graph, IntSet types) throws DatabaseException {
                         types.forEach(addToResult);
                         dec(graph);
                     }
-                               
-                               @Override
-                               public void exception(ReadGraphImpl graph, Throwable t) {
-                                       procedure.exception(graph, t);
+
+                    @Override
+                    public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
+                        procedure.exception(graph, t);
                         dec(graph);
-                   }
+                    }
 
                 });
-                
+
             }
 
             @Override
-            public void finished(ReadGraphImpl graph) {
+            public void finished(ReadGraphImpl graph) throws DatabaseException {
 
                 dec(graph);
 
             }
-            
-        }; 
+
+        };
 
         SyncIntProcedure subrelationOfProcedure = new SyncIntProcedure() {
 
             @Override
-            public void run(ReadGraphImpl graph) {
+            public void run(ReadGraphImpl graph) throws DatabaseException {
 
-               int current = finishes.addAndGet(1);
-               if(current == 3) {
-                    if(store) addOrSet(graph, result, queryProvider);
+                int current = finishes.addAndGet(1);
+                if (current == 3) {
                     procedure.execute(graph, result);
-               }
-                
+                }
+
             }
-            
+
             @Override
-            public void execute(ReadGraphImpl graph, int i) {
-                
+            public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
+
                 inc();
-                
-                Types.queryEach(graph, i, queryProvider, store ? Types.this : null, null, new InternalProcedure<IntSet>() {
+
+                QueryCache.runnerTypes(graph, i, parent, null, new InternalProcedure<IntSet>() {
 
                     @Override
-                    public void execute(ReadGraphImpl graph, IntSet types) {
+                    public void execute(ReadGraphImpl graph, IntSet types) throws DatabaseException {
 
                         types.forEach(addToResult);
                         dec(graph);
-                        
+
                     }
-                               
-                               @Override
-                               public void exception(ReadGraphImpl graph, Throwable t) {
-                                       procedure.exception(graph, t);
+
+                    @Override
+                    public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
+                        procedure.exception(graph, t);
                         dec(graph);
-                   }
+                    }
 
                 });
-                
+
             }
 
             @Override
-            public void finished(ReadGraphImpl graph) {
-                
+            public void finished(ReadGraphImpl graph) throws DatabaseException {
+
                 dec(graph);
 
             }
-            
-        }; 
-        
-        queryProvider.querySupport.getObjects(graph, id, instanceOf, instanceOfProcedure);
+
+        };
+
+        processor.querySupport.getObjects(graph, id, instanceOf, instanceOfProcedure);
         instanceOfProcedure.finished(graph);
-        queryProvider.querySupport.getObjects(graph, id, inherits, inheritsProcedure);
+        processor.querySupport.getObjects(graph, id, inherits, inheritsProcedure);
         inheritsProcedure.finished(graph);
-        queryProvider.querySupport.getObjects(graph, id, subrelationOf, subrelationOfProcedure);
+        processor.querySupport.getObjects(graph, id, subrelationOf, subrelationOfProcedure);
         subrelationOfProcedure.finished(graph);
-        
-        return result;
-        
-    }
-    
-    @Override
-    public String toString() {
-       return "Types[" + id + "]";
-    }
-    
-    private void addOrSet(ReadGraphImpl graph, final IntSet value, QueryProcessor provider) {
-       
-        assert(!isReady());
-
-        synchronized(this) {
-        
-            value.trim();
-            setResult(value);
-            setReady();
-        
-        }
-        
-    }
-    
-    @Override
-    final public Object performFromCache(ReadGraphImpl graph, QueryProcessor provider, InternalProcedure<IntSet> procedure) {
-        
-       assert(isReady());
-       
-       if(handleException(graph, procedure)) return EXCEPTED;
-       
-       IntSet result = getResult();
-       
-        procedure.execute(graph, result);
-        
-        return result;
-        
-    }
-    
-    @Override
-    public void recompute(ReadGraphImpl graph, QueryProcessor provider) {
-        
-        final Semaphore s = new Semaphore(0);
-
-        computeForEach(graph, provider, new InternalProcedure<IntSet>() {
-
-               @Override
-               public void execute(ReadGraphImpl graph, IntSet result) {
-                s.release();
-               }
-
-            @Override
-            public void exception(ReadGraphImpl graph, Throwable t) {
-               s.release();
-                new Error("Error in recompute.", t).printStackTrace();
-            }
-
-        }, true);
 
-        while(!s.tryAcquire()) {
-               provider.resume(graph);
-        }
-        
     }
 
     @Override
-    boolean isImmutable(ReadGraphImpl graph) {
-       return graph.processor.isImmutable(id);
+    public String toString() {
+        return "Types[" + id + "]";
     }
-    
+
 }