]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/NamespaceIndex.java
Merge remote-tracking branch 'origin/master' into private/db-threads
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / NamespaceIndex.java
index 5267280032fa67f51f188f624d7224061c076ad1..884f3f6d4e7ad37c0a124486a50f26173cefda37 100644 (file)
  *******************************************************************************/
 package org.simantics.db.impl.query;
 
-import gnu.trove.map.hash.TObjectIntHashMap;
-
-import java.util.concurrent.Semaphore;
-
 import org.simantics.databoard.binding.Binding;
 import org.simantics.databoard.serialization.Serializer;
 import org.simantics.databoard.util.URIStringUtils;
 import org.simantics.db.common.WriteBindings;
 import org.simantics.db.common.exception.DebugException;
+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;
+
+import gnu.trove.map.hash.TObjectIntHashMap;
 
 final public class NamespaceIndex extends StringQuery<InternalProcedure<TObjectIntHashMap<String>>> {
-    
-//     public ArrayList<InternalProcedure<TObjectIntHashMap<String>>> procs = null;
        
-    private NamespaceIndex(final String id) {
+    NamespaceIndex(final String id) {
         super(id);
     }
     
-    final static void runner(ReadGraphImpl graph, final String id, final QueryProcessor provider, NamespaceIndex cached, final CacheEntry parent, final ListenerBase listener, final InternalProcedure<TObjectIntHashMap<String>> procedure) {
-
-       NamespaceIndex entry = cached != null ? cached : (NamespaceIndex)provider.namespaceIndexMap22.get(id); 
-        if(entry == null) {
-               
-               entry = new NamespaceIndex(id);
-               entry.setPending();
-               entry.clearResult(provider.querySupport);
-               entry.putEntry(provider);
-
-            provider.performForEach(graph, entry, parent, listener, procedure);
-            
-        } else {
-
-            if(entry.isPending()) {
-               synchronized(entry) {
-                    if(entry.isPending()) {
-                        throw new IllegalStateException();
-//                     if(entry.procs == null) entry.procs = new ArrayList<InternalProcedure<TObjectIntHashMap<String>>>(); 
-//                     entry.procs.add(procedure);
-//                        provider.registerDependencies(graph, entry, parent, listener, procedure, false);
-//                     return;
-                    }
-               }
-            }
-            provider.performForEach(graph, entry, parent, listener, procedure);
-        }
-        
-    }
-    
-    final public static void queryEach(ReadGraphImpl graph, final String id, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final InternalProcedure<TObjectIntHashMap<String>> procedure) {
-
-       final NamespaceIndex entry = (NamespaceIndex)provider.namespaceIndexMap22.get(id);
-        
-       if(parent == null && listener == null && entry != null && entry.isReady()) {
-               entry.performFromCache(graph, provider, procedure);
-               return;
-       }
-       
-        runner(graph, id, provider, entry, parent, listener, procedure);
-        
-    }
-     
-       @Override
-       public NamespaceIndex getEntry(QueryProcessor provider) {
-        return provider.namespaceIndexMap22.get(id);
-       }
-       
-       @Override
-       public void putEntry(QueryProcessor provider) {
-        provider.namespaceIndexMap22.put(id, this);
-       }
-
        @Override
        final public void removeEntry(QueryProcessor provider) {
-               provider.namespaceIndexMap22.remove(id);
+               provider.cache.remove(this);
        }
        
-       final private void index(ReadGraphImpl graph, final QueryProcessor provider, int root, final InternalProcedure<TObjectIntHashMap<String>> procedure) {
+       final static private void index(ReadGraphImpl graph, int root, NamespaceIndex entry, final InternalProcedure<TObjectIntHashMap<String>> procedure) throws DatabaseException {
                
                if(root == 0) {
-            add2(graph, null);
+                       if(entry != null)
+                               entry.add2(graph, null);
             procedure.execute(graph, null);
 //            System.err.println("NamespaceIndex[" + id + "]->null");
             return;
                }
 
-               final int consistsOf = provider.getConsistsOf();
-               final int hasName = provider.getHasName();
+               QueryProcessor processor = graph.processor;
+               
+               final int consistsOf = processor.getConsistsOf();
+               final int hasName = processor.getHasName();
         
         final TObjectIntHashMap<String> result = new TObjectIntHashMap<String>();
         
-        Objects.runner(graph, root, consistsOf, graph.parent, null, new SyncIntProcedure() {
+        QueryCache.runnerObjects(graph, root, consistsOf, entry, null, new SyncIntProcedure() {
                
                @Override
-                       public void run(ReadGraphImpl graph) {
+                       public void run(ReadGraphImpl graph) throws DatabaseException {
                        
-                       if(isPending()) { 
-                    add2(graph, result);
-                    procedure.execute(graph, result);
-//                    System.err.println("NamespaceIndex[" + id + "]->" + result.size());
-                       } else {
-                               procedure.exception(graph, (Throwable)statusOrException);
-                       }
+                       if(entry != null) entry.add2(graph, result);
+                       procedure.execute(graph, result);
                                
                        }
 
                        @Override
-                       public void finished(ReadGraphImpl graph) {
-                           
+                       public void finished(ReadGraphImpl graph) throws DatabaseException {
                                dec(graph);
-                               
                        }
 
                @Override
-               public void execute(ReadGraphImpl graph, final int obj) {
+               public void execute(ReadGraphImpl graph, final int obj) throws DatabaseException {
                        
                        //System.out.println(id + " => " + obj);
 
                        inc();
                        
-                       Objects.runner(graph, obj, hasName, graph.parent, null, new IntProcedure() {
+                       QueryCache.runnerObjects(graph, obj, hasName, entry, null, new IntProcedure() {
                        
                        @Override
-                       public void execute(ReadGraphImpl graph, int i) {
+                       public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
 
                                inc();
 
-                               ValueQuery.queryEach(graph, i, NamespaceIndex.this, null, new InternalProcedure<byte[]>() {
+                               QueryCache.runnerValueQuery(graph, i, entry, null, new InternalProcedure<byte[]>() {
                                        
                                        @Override
-                                       public void execute(ReadGraphImpl graph, byte[] value) {
+                                       public void execute(ReadGraphImpl graph, byte[] value) throws DatabaseException {
                                                
                                                if(value != null) {
 
@@ -170,8 +109,8 @@ final public class NamespaceIndex extends StringQuery<InternalProcedure<TObjectI
                                        }
                                        
                                        @Override
-                                       public void exception(ReadGraphImpl graph, Throwable t) {
-                                                       except(t);
+                                       public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
+                                                       if(entry != null) entry.except(t);
                                                dec(graph);
                             }
 
@@ -180,13 +119,13 @@ final public class NamespaceIndex extends StringQuery<InternalProcedure<TObjectI
                        }
                        
                        @Override
-                       public void finished(ReadGraphImpl graph) {
+                       public void finished(ReadGraphImpl graph) throws DatabaseException {
                                dec(graph);
                        }
                                
                                @Override
-                               public void exception(ReadGraphImpl graph, Throwable t) {
-                                               except(t);
+                               public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
+                                       if(entry != null) entry.except(t);
                         dec(graph);
                     }
 
@@ -198,25 +137,33 @@ final public class NamespaceIndex extends StringQuery<InternalProcedure<TObjectI
         
     }
 
+       
     @Override
-       public void computeForEach(ReadGraphImpl graph, final QueryProcessor processor, final InternalProcedure<TObjectIntHashMap<String>> procedure) {
+       public Object compute(ReadGraphImpl graph, final InternalProcedure<TObjectIntHashMap<String>> procedure) throws DatabaseException {
+               computeForEach(graph, id, this, procedure);
+               return getResult();
+    }
+
+       public static void computeForEach(ReadGraphImpl graph, final String id, final NamespaceIndex entry, final InternalProcedure<TObjectIntHashMap<String>> procedure) throws DatabaseException {
+       
+       QueryProcessor processor = graph.processor;
        
 //     System.err.println("NamespaceIndex " + id);
        
         if("http://".equals(id) || "http:/".equals(id)) {
-            index(graph, processor, processor.getRootLibrary(), procedure);
+            index(graph, processor.getRootLibrary(), entry, procedure);
         } else {
             final String[] parts = URIStringUtils.splitURI(id);
             if(parts != null) {
-                NamespaceIndex.queryEach(graph, parts[0], processor, this, null, new InternalProcedure<TObjectIntHashMap<String>>() {
+                QueryCache.runnerNamespaceIndex(graph, parts[0], entry, null, new InternalProcedure<TObjectIntHashMap<String>>() {
     
                     @Override
-                    public void execute(ReadGraphImpl graph, TObjectIntHashMap<String> index) {
+                    public void execute(ReadGraphImpl graph, TObjectIntHashMap<String> index) throws DatabaseException {
     
                         if(index != null) {
-                            index(graph, processor, index.get(parts[1]), procedure);
+                            index(graph, index.get(parts[1]), entry, procedure);
                         } else {
-                            add2(graph, null);
+                            if(entry != null) entry.add2(graph, null);
                             procedure.execute(graph, null);
 //                            System.err.println("NamespaceIndex[" + id + "]->null");
                         }
@@ -224,15 +171,15 @@ final public class NamespaceIndex extends StringQuery<InternalProcedure<TObjectI
                     }
     
                     @Override
-                    public void exception(ReadGraphImpl graph, Throwable t) {
+                    public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
                         if(DebugException.DEBUG) new DebugException(t).printStackTrace();
-                        except(t);
+                        if(entry != null) entry.except(t);
                         procedure.exception(graph, t);
                     }
     
                 });
             } else {
-                add2(graph, null);
+                if(entry != null) entry.add2(graph, null);
                 procedure.execute(graph, null);
 //                System.err.println("NamespaceIndex[" + id + "]->null");
             }
@@ -246,12 +193,6 @@ final public class NamespaceIndex extends StringQuery<InternalProcedure<TObjectI
        return "NamespaceIndex[" + id + "]";
     }
 
-    synchronized private void add(TObjectIntHashMap<String> result) {
-       
-       throw new Error("Not possible!");
-       
-    }
-
     private void add2(ReadGraphImpl graph, TObjectIntHashMap<String> result) {
        
        if(!isPending()) {
@@ -260,46 +201,37 @@ final public class NamespaceIndex extends StringQuery<InternalProcedure<TObjectI
        
        assert(isPending());
 
-//        ArrayList<InternalProcedure<TObjectIntHashMap<String>>> p = null;
-
         synchronized(this) {
 
             setResult(result);
                setReady();
-//            p = procs;
-//            procs = null; 
         
         }
-        
-//        if(p != null) {
-//        
-//             for(InternalProcedure<TObjectIntHashMap<String>> proc : p) proc.execute(graph, result);
-//             
-//        }
        
     }
     
     @Override
-    public void performFromCache(ReadGraphImpl graph, QueryProcessor provider, InternalProcedure<TObjectIntHashMap<String>> procedure) {
+    public Object performFromCache(ReadGraphImpl graph, InternalProcedure<TObjectIntHashMap<String>> procedure) throws DatabaseException {
         
         assert(isReady());
         
-       if(handleException(graph, procedure)) return;
+       if(handleException(graph, procedure)) return (Throwable)statusOrException;
         
-        procedure.execute(graph, (TObjectIntHashMap<String>)getResult());
+       TObjectIntHashMap<String> result = (TObjectIntHashMap<String>)getResult();
+       
+        procedure.execute(graph, result);
+        
+        return result;
         
     }
     
     @Override
-    public synchronized void recompute(ReadGraphImpl graph, QueryProcessor provider) {
-        
-        final Semaphore s = new Semaphore(0);
+    public void recompute(ReadGraphImpl graph) throws DatabaseException {
         
-        computeForEach(graph, provider, new InternalProcedure<TObjectIntHashMap<String>>() {
+        compute(graph, new InternalProcedure<TObjectIntHashMap<String>>() {
 
             @Override
             public void execute(ReadGraphImpl graph, TObjectIntHashMap<String> result) {
-                s.release();
             }
                        
                        @Override
@@ -310,10 +242,6 @@ final public class NamespaceIndex extends StringQuery<InternalProcedure<TObjectI
 
         });
         
-        while(!s.tryAcquire()) {
-               provider.resume(graph);
-        }
-        
     }
     
 }