]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/ChildMap.java
Multiple readers and variable optimization
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / ChildMap.java
similarity index 59%
rename from bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/NamespaceIndex.java
rename to bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/ChildMap.java
index 884f3f6d4e7ad37c0a124486a50f26173cefda37..9f24ba64023a62b439c8c5bdc84345b826fae4ed 100644 (file)
@@ -13,33 +13,37 @@ package org.simantics.db.impl.query;
 
 import org.simantics.databoard.binding.Binding;
 import org.simantics.databoard.serialization.Serializer;
-import org.simantics.databoard.util.URIStringUtils;
+import org.simantics.db.ObjectResourceIdMap;
 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.service.CollectionSupport;
 
-import gnu.trove.map.hash.TObjectIntHashMap;
-
-final public class NamespaceIndex extends StringQuery<InternalProcedure<TObjectIntHashMap<String>>> {
+final public class ChildMap extends UnaryQuery<InternalProcedure<ObjectResourceIdMap<String>>> {
        
-    NamespaceIndex(final String id) {
-        super(id);
+    ChildMap(final int r) {
+        super(r);
     }
     
        @Override
        final public void removeEntry(QueryProcessor provider) {
                provider.cache.remove(this);
        }
-       
-       final static private void index(ReadGraphImpl graph, int root, NamespaceIndex entry, final InternalProcedure<TObjectIntHashMap<String>> procedure) throws DatabaseException {
+       
+    @Override
+       public Object compute(ReadGraphImpl graph, final InternalProcedure<ObjectResourceIdMap<String>> procedure) throws DatabaseException {
+               computeForEach(graph, id, this, procedure);
+               return getResult();
+    }
+
+       public static void computeForEach(ReadGraphImpl graph, final int root, final ChildMap entry, final InternalProcedure<ObjectResourceIdMap<String>> procedure) throws DatabaseException {
                
                if(root == 0) {
                        if(entry != null)
                                entry.add2(graph, null);
             procedure.execute(graph, null);
-//            System.err.println("NamespaceIndex[" + id + "]->null");
             return;
                }
 
@@ -48,7 +52,7 @@ final public class NamespaceIndex extends StringQuery<InternalProcedure<TObjectI
                final int consistsOf = processor.getConsistsOf();
                final int hasName = processor.getHasName();
         
-        final TObjectIntHashMap<String> result = new TObjectIntHashMap<String>();
+               ObjectResourceIdMap<String> result = graph.getService(CollectionSupport.class).createObjectResourceMap(String.class);
         
         QueryCache.runnerObjects(graph, root, consistsOf, entry, null, new SyncIntProcedure() {
                
@@ -67,8 +71,6 @@ final public class NamespaceIndex extends StringQuery<InternalProcedure<TObjectI
 
                @Override
                public void execute(ReadGraphImpl graph, final int obj) throws DatabaseException {
-                       
-                       //System.out.println(id + " => " + obj);
 
                        inc();
                        
@@ -91,12 +93,7 @@ final public class NamespaceIndex extends StringQuery<InternalProcedure<TObjectI
                                                Binding b = WriteBindings.STRING;
                                            Serializer serializer = b.serializer();
                                            final String part = (String)serializer.deserialize(value);
-       
-                                           synchronized(result) {
-                                               Object previous = result.put(URIStringUtils.escape(part), obj);
-                                               // TODO: this is not the most elegant solution
-                                               if(previous != null) previous = "";
-                                           }
+                                           result.putId(part, obj);
                                            
                                                } catch (Throwable e) {
                                            if(DebugException.DEBUG) new DebugException(e).printStackTrace();
@@ -137,67 +134,12 @@ final public class NamespaceIndex extends StringQuery<InternalProcedure<TObjectI
         
     }
 
-       
-    @Override
-       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.getRootLibrary(), entry, procedure);
-        } else {
-            final String[] parts = URIStringUtils.splitURI(id);
-            if(parts != null) {
-                QueryCache.runnerNamespaceIndex(graph, parts[0], entry, null, new InternalProcedure<TObjectIntHashMap<String>>() {
-    
-                    @Override
-                    public void execute(ReadGraphImpl graph, TObjectIntHashMap<String> index) throws DatabaseException {
-    
-                        if(index != null) {
-                            index(graph, index.get(parts[1]), entry, procedure);
-                        } else {
-                            if(entry != null) entry.add2(graph, null);
-                            procedure.execute(graph, null);
-//                            System.err.println("NamespaceIndex[" + id + "]->null");
-                        }
-                        
-                    }
-    
-                    @Override
-                    public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
-                        if(DebugException.DEBUG) new DebugException(t).printStackTrace();
-                        if(entry != null) entry.except(t);
-                        procedure.exception(graph, t);
-                    }
-    
-                });
-            } else {
-                if(entry != null) entry.add2(graph, null);
-                procedure.execute(graph, null);
-//                System.err.println("NamespaceIndex[" + id + "]->null");
-            }
-
-        }
-        
-    }
-
     @Override
     public String toString() {
-       return "NamespaceIndex[" + id + "]";
+       return "ChildMap[" + id + "]";
     }
 
-    private void add2(ReadGraphImpl graph, TObjectIntHashMap<String> result) {
-       
-       if(!isPending()) {
-               new Exception(""+hashCode()).printStackTrace();
-       }
+    private void add2(ReadGraphImpl graph, ObjectResourceIdMap<String> result) {
        
        assert(isPending());
 
@@ -211,13 +153,13 @@ final public class NamespaceIndex extends StringQuery<InternalProcedure<TObjectI
     }
     
     @Override
-    public Object performFromCache(ReadGraphImpl graph, InternalProcedure<TObjectIntHashMap<String>> procedure) throws DatabaseException {
+    public Object performFromCache(ReadGraphImpl graph, InternalProcedure<ObjectResourceIdMap<String>> procedure) throws DatabaseException {
         
         assert(isReady());
         
        if(handleException(graph, procedure)) return (Throwable)statusOrException;
         
-       TObjectIntHashMap<String> result = (TObjectIntHashMap<String>)getResult();
+       ObjectResourceIdMap<String> result = (ObjectResourceIdMap<String>)getResult();
        
         procedure.execute(graph, result);
         
@@ -228,10 +170,10 @@ final public class NamespaceIndex extends StringQuery<InternalProcedure<TObjectI
     @Override
     public void recompute(ReadGraphImpl graph) throws DatabaseException {
         
-        compute(graph, new InternalProcedure<TObjectIntHashMap<String>>() {
+        compute(graph, new InternalProcedure<ObjectResourceIdMap<String>>() {
 
             @Override
-            public void execute(ReadGraphImpl graph, TObjectIntHashMap<String> result) {
+            public void execute(ReadGraphImpl graph, ObjectResourceIdMap<String> result) {
             }
                        
                        @Override