]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/Statements.java
QueryListening sync is slow
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / Statements.java
index fd4f03988a2fa824da5323f587f437371fb836fc..218777413fe496a3afbba9d8dfb142a1fd0f1194 100644 (file)
-/*******************************************************************************\r
- * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
- * in Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- *     VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.db.impl.query;\r
-\r
-import java.util.ArrayList;\r
-import java.util.Collection;\r
-import java.util.concurrent.Semaphore;\r
-import java.util.concurrent.atomic.AtomicBoolean;\r
-\r
-import org.simantics.db.RelationInfo;\r
-import org.simantics.db.common.exception.DebugException;\r
-import org.simantics.db.exception.ManyObjectsForFunctionalRelationException;\r
-import org.simantics.db.impl.graph.ReadGraphImpl;\r
-import org.simantics.db.impl.procedure.InternalProcedure;\r
-import org.simantics.db.impl.procedure.TripleIntProcedureAdapter;\r
-import org.simantics.db.procedure.ListenerBase;\r
-import org.simantics.db.request.RequestFlags;\r
-\r
-final public class Statements extends CollectionBinaryQuery<TripleIntProcedure> {\r
-       \r
-//     public ArrayList<TripleIntProcedure> procs = null;\r
-       \r
-    public Statements(final int r1, final int r2) {\r
-        super(r1, r2);\r
-    }\r
-\r
-    final static Statements entry(final QueryProcessor processor, final int r1, final int r2) {\r
-        \r
-        return (Statements)processor.statementsMap.get(id(r1,r2));\r
-\r
-    }\r
-    \r
-       final static Collection<Statements> entries(final QueryProcessor processor, final int r1) {\r
-               return processor.statementsMap.values(r1);\r
-       }\r
-    \r
-    final static void runner(ReadGraphImpl graph, final int r1, final int r2, CacheEntry parent, final ListenerBase listener, final TripleIntProcedure procedure) {\r
-        \r
-       QueryProcessor processor = graph.processor;\r
-\r
-        Statements entry = (Statements)processor.statementsMap.get(id(r1,r2));\r
-        if(entry == null) {\r
-               \r
-               entry = new Statements(r1, r2);\r
-               entry.setPending();\r
-               entry.clearResult(processor.querySupport);\r
-               entry.putEntry(processor);\r
-               \r
-               processor.performForEach(graph, entry, parent, listener, procedure);\r
-            \r
-        } else {\r
-               \r
-            if(entry.isPending()) {\r
-               synchronized(entry) {\r
-                    if(entry.isPending()) {\r
-                        throw new IllegalStateException();\r
-//                        if(entry.procs == null) entry.procs = new ArrayList<TripleIntProcedure>();\r
-//                     entry.procs.add(procedure);\r
-//                     processor.registerDependencies(graph, entry, parent, listener, procedure, false);\r
-//                     return;\r
-                    }\r
-               }\r
-            }\r
-            \r
-            processor.performForEach(graph, entry, parent, listener, procedure);\r
-            \r
-        }\r
-\r
-    }\r
-    \r
-    final public static void queryEach(ReadGraphImpl graph, final int r1, final int r2, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final TripleIntProcedure procedure) {\r
-        \r
-       assert(r1 != 0);\r
-       assert(r2 != 0);\r
-       \r
-        if(parent == null && listener == null) {\r
-               Statements.computeForEach(graph, r1, r2, null, procedure);\r
-               return;\r
-        }\r
-        \r
-        runner(graph, r1, r2, parent, listener, procedure);\r
-         \r
-    }\r
-\r
-    @Override\r
-    public BinaryQuery<TripleIntProcedure> getEntry(QueryProcessor provider) {\r
-        return provider.statementsMap.get(id);\r
-    }\r
-       \r
-       @Override\r
-       public void putEntry(QueryProcessor provider) {\r
-           provider.statementsMap.put(id, this);\r
-       }\r
-\r
-       @Override\r
-       final public void removeEntry(QueryProcessor provider) {\r
-        provider.statementsMap.remove(id);\r
-       }\r
-       \r
-    final static TripleIntProcedure NOPT = new TripleIntProcedure() {\r
-\r
-\r
-               @Override\r
-               public void exception(ReadGraphImpl graph, Throwable throwable) {\r
-               }\r
-\r
-               @Override\r
-               public void execute(ReadGraphImpl graph, int s, int p, int o) {\r
-               }\r
-\r
-               @Override\r
-               public void finished(ReadGraphImpl graph) {\r
-               }\r
-       \r
-    };\r
-       \r
-    final static private IntArray getAssertionMap(ReadGraphImpl graph, final int r1, final int r2, final Statements entry) {\r
-       \r
-        class AssertionMapProc implements IntProcedure {\r
-               \r
-               boolean first = true;\r
-\r
-               private IntArray result;\r
-\r
-               public void addStatement(int s, int p, int o) {\r
-                       \r
-                       if(result.size() == 0) {\r
-                               result.add(s);\r
-                               result.add(p);\r
-                               result.add(o);\r
-                       } else {\r
-                               for(int i = 0;i < result.sizeOrData ; i+=3) {\r
-                                       int existingP = result.data[i+1];\r
-                                       if(p == existingP) {\r
-                                               int existingO = result.data[i+2];\r
-                                               if(existingO == o) return;\r
-                                       }\r
-                               }\r
-                               result.add(s);\r
-                               result.add(p);\r
-                               result.add(o);\r
-                       }\r
-                       \r
-               }\r
-               \r
-            @Override\r
-            public void execute(ReadGraphImpl graph, int type) {\r
-                AssertedStatements stms = AssertedStatements.queryEach(graph, type, r2, graph.processor, entry, null, NOPT);\r
-                if(result == null) {\r
-                       result = stms.getResult();\r
-                } else {\r
-                       if (first) {\r
-                               IntArray ia = result;\r
-                               result = new IntArray();\r
-                               if(ia.data != null) {\r
-                                       for(int i = 0;i < ia.sizeOrData ; i+=3) addStatement(ia.data[i],ia.data[i+1],ia.data[i+2]);\r
-                               }\r
-                               first = false;\r
-                       }\r
-                       IntArray ia = stms.getResult();\r
-                       if(ia.data != null) {\r
-                               for(int i = 0;i < ia.sizeOrData ; i+=3) addStatement(ia.data[i],ia.data[i+1],ia.data[i+2]);\r
-                       }\r
-                }\r
-            }\r
-\r
-            @Override\r
-            public void finished(ReadGraphImpl graph) {\r
-            }\r
-\r
-               @Override\r
-               public void exception(ReadGraphImpl graph, Throwable throwable) {\r
-               }\r
-\r
-        }\r
-        \r
-        AssertionMapProc amp = new AssertionMapProc();\r
-\r
-        // This dependency could be cut\r
-       PrincipalTypes.queryEach(graph, r1, graph.processor, entry, null, amp);\r
-       \r
-        return amp.result;\r
-       \r
-    }\r
-       \r
-    final static private void forSingleAssertion(ReadGraphImpl graph, final int r1, final int r2, final Statements entry, final TripleIntProcedure procedure) {\r
-        \r
-       IntArray map = getAssertionMap(graph, r1, r2, entry);\r
-       if(map == null) {\r
-            if(entry != null) entry.finish(graph, procedure);\r
-               else procedure.finished(graph);\r
-            return;\r
-       }\r
-        \r
-        int size = map.size();\r
-        if(size == 3) {\r
-\r
-               int s = map.data[0];\r
-               int p = map.data[1];\r
-               int o = map.data[2];\r
-               \r
-            if(entry != null) {\r
-               entry.addOrSetFunctional(s,p,o);\r
-               entry.finish(graph, procedure);\r
-            } else {\r
-                       procedure.execute(graph, s,p,o);\r
-                       procedure.finished(graph);\r
-               }\r
-\r
-        } else if(size == 0) {\r
-\r
-            if(entry != null) entry.finish(graph, procedure);\r
-               else procedure.finished(graph);\r
-               \r
-        } else {\r
-\r
-               int candidateS = map.data[0];\r
-               int candidateP = map.data[1];\r
-               int candidateO = map.data[2];\r
-\r
-                       SuperTypes candidate = SuperTypes.queryEach(graph, candidateS, graph.processor, entry, null, NOP);\r
-                       if(candidate.isExcepted()) {\r
-                               if(entry != null) entry.except((Throwable)candidate.getResult());\r
-                               procedure.exception(graph, (Throwable)candidate.getResult());\r
-                               return;\r
-                       }\r
-                       IntSet candidateIs = candidate.getResult();\r
-               \r
-                       for(int i=3;i<map.size();i+=3) {\r
-\r
-                               int nextS = map.data[i];\r
-                               int nextP = map.data[i+1];\r
-                               int nextO = map.data[i+2];\r
-                       \r
-                               if(nextS != candidateS) {\r
-\r
-                               if(candidateIs.contains(nextS)) {\r
-                                       \r
-                                       // Next is a super type of candidate => ignore next\r
-                                       \r
-                               } else {\r
-                               \r
-                                       SuperTypes next = SuperTypes.queryEach(graph, nextS, graph.processor, entry, null, NOP);\r
-                                       if(next.isExcepted()) {\r
-                                               if(entry != null) entry.except((Throwable)next.getResult());\r
-                                               procedure.exception(graph, (Throwable)next.getResult());\r
-                                               return;\r
-                                       }\r
-                                       IntSet nextIs = next.getResult();\r
-                                       \r
-                                       if(nextIs.contains(candidateS)) {\r
-\r
-                                               // Candidate is a super type of next => next is the new candidate\r
-                                               \r
-                                               candidateS = nextS;\r
-                                               candidateP = nextP;\r
-                                               candidateO = nextO;\r
-                                               candidateIs = nextIs;\r
-                                               \r
-                                       } else {\r
-\r
-                                               // candidate and next are unrelated => error\r
-                                               ManyObjectsForFunctionalRelationException exception = new ManyObjectsForFunctionalRelationException("Functional relation has conflicting assertions.", r1);\r
-                                               \r
-                                               if(entry != null) entry.except(exception);\r
-                                               procedure.exception(graph, exception);\r
-                                               return;                                         \r
-                                               \r
-                                       }\r
-                               \r
-                               }\r
-                               \r
-                       }\r
-                       \r
-               }\r
-               \r
-               if(entry != null) {\r
-               entry.addOrSetFunctional(candidateS, candidateP, candidateO);\r
-               entry.finish(graph, procedure);\r
-            } else {\r
-                       procedure.execute(graph, candidateS, candidateP, candidateO);\r
-                       procedure.finished(graph);\r
-               }\r
-               \r
-        }\r
-        \r
-    }\r
-    \r
-    final static InternalProcedure<IntSet> NOP = new InternalProcedure<IntSet>() {\r
-\r
-               @Override\r
-               public void execute(ReadGraphImpl graph, IntSet result) {\r
-               }\r
-\r
-               @Override\r
-               public void exception(ReadGraphImpl graph, Throwable throwable) {\r
-               }\r
-       \r
-    };\r
-       \r
-       // Search for one statement\r
-       final static public void computeFunctionalIndex(ReadGraphImpl graph, final int r1, final int r2, final Statements entry, final RelationInfo ri, final TripleIntProcedure procedure) {\r
-        \r
-        if(ri.isFinal) {\r
-            \r
-               int result = graph.processor.querySupport.getFunctionalObject(r1, r2);\r
-\r
-               if(result == 0) {\r
-\r
-               // Check for assertions\r
-               forSingleAssertion(graph, r1, r2, entry, procedure);\r
-\r
-               } else if(result == -1) {\r
-\r
-               graph.processor.querySupport.getObjects(graph, r1, r2, new IntProcedure() {\r
-\r
-                       @Override\r
-                       public void execute(ReadGraphImpl graph, int i) {\r
-                               if(entry != null) entry.addOrSetFunctional(r1, r2, i);\r
-                               else procedure.execute(graph, r1, r2, i);\r
-                       }\r
-\r
-                       @Override\r
-                       public void exception(ReadGraphImpl graph, Throwable t) {\r
-                               if(DebugException.DEBUG) new DebugException(t).printStackTrace();\r
-                       }\r
-\r
-                       @Override\r
-                       public void finished(ReadGraphImpl graph) {\r
-                       }\r
-\r
-               });\r
-\r
-               // Check for assertions\r
-               forSingleAssertion(graph, r1, r2, entry, procedure);\r
-                       \r
-               } else {\r
-\r
-               // If functional relation was found there is no need to check assertions\r
-                       if(entry != null) {\r
-                               entry.addOrSetFunctional(r1, r2, result);\r
-                               entry.finish(graph, procedure);\r
-                       } else {\r
-                               procedure.execute(graph, r1, r2, result);\r
-                               procedure.finished(graph);\r
-                       }\r
-                       \r
-               }\r
-\r
-            \r
-        } else {\r
-            \r
-            final AtomicBoolean found = new AtomicBoolean(false);\r
-            \r
-            // Note! The dependency is intentionally cut!\r
-            DirectPredicates.queryEach(graph, r1, graph.processor, null, null, new SyncIntProcedure() {\r
-                \r
-                @Override\r
-                public void run(ReadGraphImpl graph) {\r
-                    \r
-                    if(found.get()) {\r
-                       if(entry != null) entry.finish(graph, procedure);\r
-                       else procedure.finished(graph);\r
-                    } else {\r
-                    \r
-                           // Check for assertions\r
-                           forSingleAssertion(graph, r1, r2, entry, procedure);\r
-                           \r
-                    }\r
-                    \r
-                }\r
-\r
-                @Override\r
-                public void execute(ReadGraphImpl graph, final int pred) {\r
-                    \r
-                    if(found.get()) return;\r
-\r
-                    if(pred == r2) {\r
-                        \r
-                        inc();\r
-                        \r
-                        // Note! The dependency is intentionally cut!\r
-                        DirectObjects.queryEach(graph, r1, pred, graph.processor, null, null, new IntProcedure() {\r
-\r
-                            @Override\r
-                            public void execute(ReadGraphImpl graph, int i) {\r
-                                \r
-                               if(found.compareAndSet(false, true)) {\r
-                                    \r
-                                    if(entry != null) entry.addOrSetFunctional(r1, pred, i);\r
-                                    else procedure.execute(graph, r1, pred, i);\r
-                                       \r
-                               } else {\r
-\r
-                                       ManyObjectsForFunctionalRelationException exception = new ManyObjectsForFunctionalRelationException("Functional relation has more than one statement.", r1);\r
-                                       if(entry != null) entry.except(exception);\r
-                                       procedure.exception(graph, exception);\r
-                                       \r
-                               }\r
-\r
-                            }\r
-\r
-                            @Override\r
-                            public void finished(ReadGraphImpl graph) {\r
-                                dec(graph);\r
-                            }\r
-                                               \r
-                                               @Override\r
-                                               public void exception(ReadGraphImpl graph, Throwable t) {\r
-                                                       procedure.exception(graph, t);\r
-                                dec(graph);\r
-                                   }\r
-\r
-                        });\r
-\r
-                    } else {\r
-                        \r
-                        inc();\r
-                    \r
-                        SuperRelations.queryEach(graph, pred, graph.processor, entry, null, new InternalProcedure<IntSet>() {\r
-    \r
-                            @Override\r
-                            public void execute(ReadGraphImpl graph, IntSet result) {\r
-                                \r
-                                if(found.get()) {\r
-                                    dec(graph);\r
-                                    return;\r
-                                }\r
-\r
-                                if(result.contains(r2)) {\r
-                                    \r
-                                    inc();\r
-                                    \r
-                                    // Note! The dependency is intentionally cut!\r
-                                    DirectObjects.queryEach(graph, r1, pred, graph.processor, null, null, new IntProcedure() {\r
-    \r
-                                        @Override\r
-                                        public void execute(ReadGraphImpl graph, int i) {\r
-                                            \r
-                                               if(found.compareAndSet(false, true)) {\r
-                                                \r
-                                               if(entry != null) entry.addOrSetFunctional(r1, pred, i);\r
-                                               else procedure.execute(graph, r1, pred, i);\r
-                                                       \r
-                                               } else {\r
-\r
-                                               ManyObjectsForFunctionalRelationException exception = new ManyObjectsForFunctionalRelationException("Functional relation has more than one statement.", r1);\r
-                                               if(entry != null) entry.except(exception);\r
-                                               procedure.exception(graph, exception);\r
-                                                       \r
-                                               }\r
-                                            \r
-                                        }\r
-    \r
-                                        @Override\r
-                                        public void finished(ReadGraphImpl graph) {\r
-                                            dec(graph);\r
-                                        }\r
-                                                       \r
-                                                       @Override\r
-                                                       public void exception(ReadGraphImpl graph, Throwable t) {\r
-                                                               procedure.exception(graph, t);\r
-                                            dec(graph);\r
-                                           }\r
-\r
-                                    });\r
-    \r
-                                }\r
-                                \r
-                                dec(graph);\r
-                                \r
-                            }\r
-                                               \r
-                                               @Override\r
-                                               public void exception(ReadGraphImpl graph, Throwable t) {\r
-                                                       procedure.exception(graph, t);\r
-                                dec(graph);\r
-                                   }\r
-\r
-                        });\r
-                        \r
-                    }\r
-                    \r
-                }\r
-\r
-                @Override\r
-                public void finished(ReadGraphImpl graph) {\r
-                    \r
-                    dec(graph);\r
-                    \r
-                }\r
-                \r
-            });\r
-            \r
-        }\r
-\r
-       }\r
-    \r
-    final static private void forAssertions(ReadGraphImpl graph, final int r1, final int r2, final Statements entry, final TripleIntProcedure procedure) {\r
-\r
-        PrincipalTypes.queryEach(graph, r1, graph.processor, entry, null, new SyncIntProcedure() {\r
-            \r
-            @Override\r
-            public void run(ReadGraphImpl graph) {\r
-                \r
-                if(entry != null) entry.finish(graph, procedure);\r
-                else procedure.finished(graph);\r
-                \r
-            }\r
-            \r
-            TripleIntProcedure proc = new TripleIntProcedureAdapter() {\r
-\r
-                @Override\r
-                public void execute(ReadGraphImpl graph, int s, int p, int o) {\r
-                       if(entry != null) entry.addOrSet(s, p, o);\r
-                       else procedure.execute(graph, s, p, o);\r
-                }\r
-\r
-                @Override\r
-                public void finished(ReadGraphImpl graph) {\r
-                    dec(graph);\r
-                }\r
-                               \r
-                               @Override\r
-                               public void exception(ReadGraphImpl graph, Throwable t) {\r
-                    dec(graph);\r
-                                       procedure.exception(graph, t);\r
-                   }\r
-\r
-            }; \r
-\r
-            @Override\r
-            public void execute(ReadGraphImpl graph, int type) {\r
-\r
-                inc();\r
-                \r
-                AssertedStatements.queryEach(graph, type, r2, graph.processor, entry, null, proc);\r
-                \r
-            }\r
-            \r
-            @Override\r
-            public void finished(ReadGraphImpl graph) {\r
-                dec(graph);       \r
-            }\r
-            \r
-            @Override\r
-            public void exception(ReadGraphImpl graph, Throwable t) {\r
-                dec(graph);\r
-            }\r
-            \r
-        });\r
-        \r
-\r
-    }\r
-\r
-       final static public void computeNotFunctionalIndex(ReadGraphImpl graph, final int r1, final int r2, final Statements entry,  final RelationInfo ri, final TripleIntProcedure procedure) {\r
-\r
-               if(ri.isFinal) {\r
-\r
-                       graph.processor.querySupport.getObjects(graph, r1, r2, new IntProcedure() {\r
-\r
-                               @Override\r
-                               public void execute(ReadGraphImpl graph, int i) {\r
-                                       if(entry != null) entry.addOrSet(r1, r2, i);\r
-                                       else procedure.execute(graph, r1, r2, i);\r
-                               }\r
-\r
-                               @Override\r
-                               public void exception(ReadGraphImpl graph, Throwable t) {\r
-                                       if(DebugException.DEBUG) new DebugException(t).printStackTrace();\r
-                                       procedure.exception(graph, t);\r
-                               }\r
-\r
-                               @Override\r
-                               public void finished(ReadGraphImpl graph) {\r
-                               }\r
-\r
-                       });\r
-\r
-                       if(ri.isAsserted) {\r
-                               forAssertions(graph, r1, r2, entry, procedure);\r
-                       } else {\r
-                               if(entry != null) entry.finish(graph, procedure);\r
-                               else procedure.finished(graph);\r
-                       }\r
-\r
-        } else {\r
-\r
-            // Note! The dependency is intentionally cut!\r
-            DirectPredicates.queryEach(graph, r1, graph.processor, null, null, new SyncIntProcedure() {\r
-                \r
-                @Override\r
-                public void run(ReadGraphImpl graph) {\r
-                    \r
-                    forAssertions(graph, r1, r2, entry, procedure);\r
-                    \r
-                }\r
-\r
-                @Override\r
-                public void execute(ReadGraphImpl graph, final int pred2) {\r
-\r
-                    if(pred2 == r2) {\r
-                        \r
-                        inc();\r
-                        \r
-                        // Note! The dependency is intentionally cut!\r
-                        DirectObjects.queryEach(graph, r1, pred2, graph.processor, null, null, new IntProcedure() {\r
-\r
-                            @Override\r
-                            public void execute(ReadGraphImpl graph, int i) {\r
-                               if(entry != null) entry.addOrSet(r1, pred2, i);\r
-                               else procedure.execute(graph, r1, pred2, i);\r
-                            }\r
-\r
-                            @Override\r
-                            public void finished(ReadGraphImpl graph) {\r
-                                dec(graph);\r
-                            }\r
-                                               \r
-                                               @Override\r
-                                               public void exception(ReadGraphImpl graph, Throwable t) {\r
-                                                       procedure.exception(graph, t);\r
-                                dec(graph);\r
-                                   }\r
-\r
-                        });\r
-\r
-                    } else {\r
-                    \r
-//                        inc();\r
-\r
-                        try {\r
-                            \r
-                            IntSet result = SuperRelations.queryEach2(graph, pred2, graph.processor, entry, null, null);\r
-                            if(result.contains(r2)) {\r
-\r
-                                inc();\r
-\r
-                                // Note! The dependency is intentionally cut!\r
-                                DirectObjects.queryEach(graph, r1, pred2, graph.processor, null, null, new IntProcedure() {\r
-\r
-                                    @Override\r
-                                    public void execute(ReadGraphImpl graph, int i) {\r
-                                        if(entry != null) entry.addOrSet(r1, pred2, i);\r
-                                        else procedure.execute(graph, r1, pred2, i);\r
-\r
-                                    }\r
-\r
-                                    @Override\r
-                                    public void finished(ReadGraphImpl graph) {\r
-                                        dec(graph);\r
-                                    }\r
-\r
-                                    @Override\r
-                                    public void exception(ReadGraphImpl graph, Throwable t) {\r
-                                        procedure.exception(graph, t);\r
-                                        dec(graph);\r
-                                    }\r
-\r
-                                });\r
-\r
-                            }\r
-                            \r
-                        } catch (Throwable e) {\r
-                            procedure.exception(graph, e);\r
-                        }\r
-\r
-                    }\r
-                    \r
-                }\r
-\r
-                @Override\r
-                public void finished(ReadGraphImpl graph) {\r
-                    dec(graph);\r
-                }\r
-                \r
-            });\r
-            \r
-        }\r
-        \r
-    }\r
-    \r
-    @Override\r
-    public void computeForEach(ReadGraphImpl graph, final QueryProcessor provider, final TripleIntProcedure procedure, final boolean store) {\r
-       computeForEach(graph, r1(), r2(), this, procedure);\r
-    }\r
-       \r
-    public static void computeForEach(ReadGraphImpl graph, final int r1, final int r2, final Statements entry, final TripleIntProcedure procedure) {\r
-\r
-       RelationInfoQuery riEntry = RelationInfoQuery.probe(graph, r2);\r
-       if(riEntry != null) {\r
-               RelationInfo ri = riEntry.getResult();\r
-               graph.ensureLoaded(r1, r2);       \r
-               if(ri.isFunctional) {\r
-                       computeFunctionalIndex(graph, r1, r2, entry, ri, procedure);\r
-               } else {\r
-                       computeNotFunctionalIndex(graph, r1, r2, entry, ri, procedure);\r
-               }\r
-               return;\r
-       }\r
-       \r
-        RelationInfoQuery.queryEach(graph, r2, graph.processor, entry, null, new InternalProcedure<RelationInfo>() {\r
-\r
-            @Override\r
-            public void execute(ReadGraphImpl graph, final RelationInfo ri) {\r
-               \r
-               graph.ensureLoaded(r1, r2);\r
-               if(ri.isFunctional) {\r
-                       computeFunctionalIndex(graph, r1, r2, entry, ri, procedure);\r
-               } else {\r
-                       computeNotFunctionalIndex(graph, r1, r2, entry, ri, procedure);\r
-               }\r
-                \r
-            }\r
-                       \r
-                       @Override\r
-                       public void exception(ReadGraphImpl graph, Throwable t) {\r
-                               procedure.exception(graph, t);\r
-            }\r
-\r
-        });\r
-        \r
-    }\r
-    \r
-    @Override\r
-    public String toString() {\r
-       return "Statements[" + r1() + " - " + r2() + "]";\r
-    }\r
-\r
-    final private void finish(ReadGraphImpl graph, TripleIntProcedure procedure) {\r
-       \r
-        assert(assertPending());\r
-\r
-//        ArrayList<TripleIntProcedure> p = null;\r
-\r
-        synchronized(this) {\r
-\r
-            setReady();\r
-//            p = procs;\r
-//            procs = null;\r
-\r
-        }\r
-\r
-        IntArray v = (IntArray)getResult();\r
-        final IntArray value = (IntArray)getResult();\r
-\r
-//        if(p != null) {\r
-//\r
-//             for(TripleIntProcedure proc : p) {\r
-//                     for(int i=0;i<value.size();i+=3) {\r
-//                             proc.execute(graph, value.data[i], value.data[i+1], value.data[i+2]);\r
-//                     }\r
-//             }\r
-//             for(int i=0;i<value.size();i+=3) {\r
-//                     procedure.execute(graph, value.data[i], value.data[i+1], value.data[i+2]);\r
-//             }\r
-//\r
-//            for(TripleIntProcedure proc : p) proc.finished(graph);\r
-//\r
-//        }\r
-\r
-        for(int i=0;i<value.size();i+=3) {\r
-               procedure.execute(graph, value.data[i], value.data[i+1], value.data[i+2]);\r
-        }\r
-        \r
-        procedure.finished(graph);\r
-       \r
-    }\r
-\r
-//    final private void finish(ReadGraphImpl graph, QueryProcessor provider) {\r
-//        \r
-//     assert(isPending());\r
-//\r
-//        ArrayList<TripleIntProcedure> p = null;\r
-//\r
-//        synchronized(this) {\r
-//             \r
-//             setReady();\r
-//            p = procs;\r
-//            procs = null; \r
-//        \r
-//        }\r
-//        \r
-//        if(p != null) {\r
-//             \r
-//             final IntArray value = (IntArray)getResult();\r
-//             for(TripleIntProcedure proc : p) {\r
-//                     for(int i=0;i<value.size();i+=3) {\r
-//                             proc.execute(graph, value.data[i], value.data[i+1], value.data[i+2]);\r
-//                     }\r
-//             }\r
-//\r
-//             for(TripleIntProcedure proc : p) proc.finished(graph);\r
-//\r
-//        }\r
-//\r
-//    }\r
-    \r
-    synchronized public void addOrSet(int s, int p, int o) {\r
-        \r
-       assert(assertPending());\r
-        \r
-       IntArray value = (IntArray)getResult();\r
-        value.add(s);\r
-        value.add(p);\r
-        value.add(o);\r
-        \r
-    }\r
-\r
-    final static public int r1(long id) {\r
-        return (int)(id>>>32);\r
-    }\r
-    \r
-    final static public int r2(long id) {\r
-        return (int)id;\r
-    }\r
-    \r
-    final public void addOrSetFunctional(int s, long po) {\r
-       \r
-       addOrSetFunctional(s, r1(po), r2(po));\r
-       \r
-    }\r
-\r
-    final public void addOrSetFunctional(int s, int p, int o) {\r
-        \r
-       assert(assertPending());\r
-        \r
-       IntArray value = (IntArray)getResult();\r
-        value.add(s);\r
-        value.add(p);\r
-        value.add(o);\r
-        \r
-    }\r
-    \r
-    @Override\r
-    public void performFromCache(ReadGraphImpl graph, QueryProcessor provider, final TripleIntProcedure procedure) {\r
-\r
-       assert(isReady());\r
-\r
-       if(handleException(graph, procedure)) return;\r
-       \r
-        final IntArray value = (IntArray)getResult();\r
-        for(int i=0;i<value.size();i+=3) {\r
-               procedure.execute(graph, value.data[i], value.data[i+1], value.data[i+2]);\r
-        }\r
-\r
-        procedure.finished(graph);\r
-        \r
-    }\r
-    \r
-    @Override\r
-    public void recompute(ReadGraphImpl graph, QueryProcessor provider) {\r
-        \r
-        final Semaphore s = new Semaphore(0);\r
-       \r
-        computeForEach(graph, provider, new TripleIntProcedureAdapter() {\r
-\r
-            @Override\r
-            public void finished(ReadGraphImpl graph) {\r
-                s.release();\r
-            }\r
-                       \r
-                       @Override\r
-                       public void exception(ReadGraphImpl graph, Throwable t) {\r
-                               new Error("Error in recompute.", t).printStackTrace();\r
-                               s.release();\r
-            }\r
-\r
-        }, true);\r
-        \r
-       while(!s.tryAcquire()) {\r
-               provider.resume(graph);\r
-       }\r
-        \r
-//        try {\r
-//            s.acquire();\r
-//        } catch (InterruptedException e) {\r
-//            throw new Error(e);\r
-//        }\r
-        \r
-    }\r
-\r
-    @Override\r
-    public int type() {\r
-        return RequestFlags.IMMEDIATE_UPDATE;\r
-    }\r
-    \r
-\r
-    @Override\r
-    boolean isImmutable(ReadGraphImpl graph) {\r
-       return graph.processor.isImmutable(r1());\r
-    }\r
-    \r
-}\r
+/*******************************************************************************
+ * 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
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.db.impl.query;
+
+import org.simantics.db.RelationInfo;
+import org.simantics.db.common.exception.DebugException;
+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;
+
+public final class Statements extends CollectionBinaryQuery<TripleIntProcedure> implements TripleIntProcedure {
+
+    public Statements(final int r1, final int r2) {
+        super(r1, r2);
+    }
+
+    final public static void queryEach(ReadGraphImpl graph, final int r1, final int r2, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final TripleIntProcedure procedure) throws DatabaseException {
+
+        assert(r1 != 0);
+        assert(r2 != 0);
+
+        if(parent == null && listener == null) {
+            Statements.computeForEach(graph, r1, r2, null, procedure);
+            return;
+        }
+
+        QueryCache.runnerStatements(graph, r1, r2, parent, listener, procedure);
+
+    }
+
+    @Override
+    final public void removeEntry(QueryProcessor provider) {
+        provider.cache.remove(this);
+    }
+
+    final static TripleIntProcedure NOPT = new TripleIntProcedure() {
+
+
+        @Override
+        public void exception(ReadGraphImpl graph, Throwable throwable) {
+        }
+
+        @Override
+        public void execute(ReadGraphImpl graph, int s, int p, int o) {
+        }
+
+        @Override
+        public void finished(ReadGraphImpl graph) {
+        }
+
+    };
+
+    final static private IntArray getAssertionMap(ReadGraphImpl graph, final int r1, final int r2, final Statements entry) throws DatabaseException {
+
+        class AssertionMapProc implements IntProcedure {
+
+            boolean first = true;
+
+            private IntArray result;
+
+            public void addStatement(int s, int p, int o) {
+
+                if(result.size() == 0) {
+                    result.add(s);
+                    result.add(p);
+                    result.add(o);
+                } else {
+                    for(int i = 0;i < result.sizeOrData ; i+=3) {
+                        int existingP = result.data[i+1];
+                        if(p == existingP) {
+                            int existingO = result.data[i+2];
+                            if(existingO == o) return;
+                        }
+                    }
+                    result.add(s);
+                    result.add(p);
+                    result.add(o);
+                }
+
+            }
+
+            @Override
+            public void execute(ReadGraphImpl graph, int type) throws DatabaseException {
+                if(result == null) {
+                    result = QueryCache.resultAssertedStatements(graph, type, r2, entry, null);
+                } else {
+                    if (first) {
+                        IntArray ia = result;
+                        result = new IntArray();
+                        if(ia.data != null) {
+                            for(int i = 0;i < ia.sizeOrData ; i+=3) addStatement(ia.data[i],ia.data[i+1],ia.data[i+2]);
+                        }
+                        first = false;
+                    }
+                    IntArray ia = QueryCache.resultAssertedStatements(graph, type, r2, entry, null);
+                    if(ia.data != null) {
+                        for(int i = 0;i < ia.sizeOrData ; i+=3) addStatement(ia.data[i],ia.data[i+1],ia.data[i+2]);
+                    }
+                }
+            }
+
+            @Override
+            public void finished(ReadGraphImpl graph) {
+            }
+
+            @Override
+            public void exception(ReadGraphImpl graph, Throwable throwable) {
+            }
+
+        }
+
+        AssertionMapProc amp = new AssertionMapProc();
+
+        // This dependency could be cut
+        QueryCache.runnerPrincipalTypes(graph, r1, entry, null, amp);
+
+        return amp.result;
+
+    }
+
+    final static private void forSingleAssertion(ReadGraphImpl graph, final int r1, final int r2, final Statements parent, final TripleIntProcedure procedure) throws DatabaseException {
+
+        IntArray map = getAssertionMap(graph, r1, r2, parent);
+        if(map == null) {
+            procedure.finished(graph);
+            return;
+        }
+
+        int size = map.size();
+        if(size == 3) {
+
+            int s = map.data[0];
+            int p = map.data[1];
+            int o = map.data[2];
+
+            procedure.execute(graph, s,p,o);
+            procedure.finished(graph);
+
+        } else if(size == 0) {
+
+            procedure.finished(graph);
+
+        } else {
+
+            int candidateS = map.data[0];
+            int candidateP = map.data[1];
+            int candidateO = map.data[2];
+
+            IntSet candidateIs = null;
+            try {
+                candidateIs = QueryCache.resultSuperTypes(graph, candidateS, parent, null);
+            } catch (DatabaseException e) {
+                procedure.exception(graph, e);
+                return;
+            }
+
+            for(int i=3;i<map.size();i+=3) {
+
+                int nextS = map.data[i];
+                int nextP = map.data[i+1];
+                int nextO = map.data[i+2];
+
+                if(nextS != candidateS) {
+
+                    if(candidateIs.contains(nextS)) {
+
+                        // Next is a super type of candidate => ignore next
+
+                    } else {
+
+                        IntSet nextIs = null;
+                        try {
+                            nextIs = QueryCache.resultSuperTypes(graph, nextS, parent, null);
+                        } catch (DatabaseException e) {
+                            procedure.exception(graph, e);
+                            return;
+                        }
+
+                        if(nextIs.contains(candidateS)) {
+
+                            // Candidate is a super type of next => next is the new candidate
+
+                            candidateS = nextS;
+                            candidateP = nextP;
+                            candidateO = nextO;
+                            candidateIs = nextIs;
+
+                        } else {
+                            // candidate and next are unrelated => error
+                            ManyObjectsForFunctionalRelationException exception = new ManyObjectsForFunctionalRelationException("Functional relation has conflicting assertions.", r1);
+                            procedure.exception(graph, exception);
+                            return;                                            
+                        }
+
+                    }
+
+                }
+
+            }
+
+            procedure.execute(graph, candidateS, candidateP, candidateO);
+            procedure.finished(graph);
+
+        }
+
+    }
+
+    final static InternalProcedure<IntSet> NOP = new InternalProcedure<IntSet>() {
+
+        @Override
+        public void execute(ReadGraphImpl graph, IntSet result) {
+        }
+
+        @Override
+        public void exception(ReadGraphImpl graph, Throwable throwable) {
+        }
+
+    };
+
+    // Search for one statement
+    final static public void computeFunctionalIndex(ReadGraphImpl graph, final int r1, final int r2, final Statements parent, final RelationInfo ri, final TripleIntProcedure procedure) throws DatabaseException {
+
+        if(ri.isFinal) {
+
+            int result = graph.processor.querySupport.getFunctionalObject(r1, r2);
+
+            if(result == 0) {
+
+                // Check for assertions
+                forSingleAssertion(graph, r1, r2, parent, procedure);
+
+            } else if(result == -1) {
+
+                graph.processor.querySupport.getObjects(graph, r1, r2, new IntProcedure() {
+
+                    @Override
+                    public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
+                        procedure.execute(graph, r1, r2, i);
+                    }
+
+                    @Override
+                    public void exception(ReadGraphImpl graph, Throwable t) {
+                        if(DebugException.DEBUG) new DebugException(t).printStackTrace();
+                    }
+
+                    @Override
+                    public void finished(ReadGraphImpl graph) {
+                    }
+
+                });
+
+                // Check for assertions
+                forSingleAssertion(graph, r1, r2, parent, procedure);
+
+            } else {
+
+                // If functional relation was found there is no need to check assertions
+                procedure.execute(graph, r1, r2, result);
+                procedure.finished(graph);
+
+            }
+
+
+        } else {
+
+            // Note! The dependency is intentionally cut!
+            IntSet direct = QueryCache.resultDirectPredicates(graph, r1, null, null);
+            direct.forEach(graph, new SyncIntProcedure() {
+
+                /*
+                 * 0 = not found
+                 * 1 = found
+                 * 2 = exception
+                 */
+                int found = 0;
+
+                @Override
+                public void run(ReadGraphImpl graph) throws DatabaseException {
+
+                    if(found == 1) {
+
+                        procedure.finished(graph);
+
+                    } else if(found == 0) {
+
+                        // Check for assertions
+                        forSingleAssertion(graph, r1, r2, parent, procedure);
+
+                    }
+
+                }
+
+                @Override
+                public void execute(ReadGraphImpl graph, final int pred) throws DatabaseException {
+
+                    if(found > 0)
+                        return;
+
+                    if(pred == r2) {
+
+                        // Note! The dependency is intentionally cut!
+                        QueryCache.runnerDirectObjects(graph, r1, pred, null, null, new IntProcedure() {
+
+                            @Override
+                            public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
+
+                                if(found == 0) {
+
+                                    procedure.execute(graph, r1, pred, i);
+                                    found = 1;
+
+                                } else {
+
+                                    ManyObjectsForFunctionalRelationException exception = new ManyObjectsForFunctionalRelationException("Functional relation has more than one statement.", r1);
+                                    procedure.exception(graph, exception);
+                                    found = 2;
+
+                                }
+
+                            }
+
+                            @Override
+                            public void finished(ReadGraphImpl graph) throws DatabaseException {
+                            }
+
+                            @Override
+                            public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
+
+                                procedure.exception(graph, t);
+                                found = 2;
+
+                            }
+
+                        });
+
+                    } else {
+
+                        QueryCache.runnerSuperRelations(graph, pred, parent, null, new InternalProcedure<IntSet>() {
+
+                            @Override
+                            public void execute(ReadGraphImpl graph, IntSet result) throws DatabaseException {
+
+                                if(found > 0)
+                                    return;
+
+                                if(result.contains(r2)) {
+
+                                    inc();
+
+                                    // Note! The dependency is intentionally cut!
+                                    QueryCache.runnerDirectObjects(graph, r1, pred, null, null, new IntProcedure() {
+
+                                        @Override
+                                        public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
+
+                                            if(found == 0) {
+
+                                                procedure.execute(graph, r1, pred, i);
+                                                found = 1;
+
+                                            } else {
+
+                                                ManyObjectsForFunctionalRelationException exception = new ManyObjectsForFunctionalRelationException("Functional relation has more than one statement (r1=" + r1 + ", r2=" + r2 + ").", r1);
+                                                procedure.exception(graph, exception);
+                                                found = 2;
+
+                                            }
+
+                                        }
+
+                                        @Override
+                                        public void finished(ReadGraphImpl graph) throws DatabaseException {
+                                        }
+
+                                        @Override
+                                        public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
+                                            procedure.exception(graph, t);
+                                            found = 2;
+                                        }
+
+                                    });
+
+                                }
+
+                            }
+
+                            @Override
+                            public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
+                                procedure.exception(graph, t);
+                                found = 2;
+                            }
+
+                        });
+
+                    }
+
+                }
+
+                @Override
+                public void finished(ReadGraphImpl graph) throws DatabaseException {
+                    dec(graph);
+                }
+
+            });
+
+        }
+
+    }
+
+    final static private void forAssertions(ReadGraphImpl graph, final int r1, final int r2, final Statements parent, final TripleIntProcedure procedure) throws DatabaseException {
+
+        QueryCache.runnerPrincipalTypes(graph, r1, parent, null, new SyncIntProcedure() {
+
+            @Override
+            public void run(ReadGraphImpl graph) throws DatabaseException {
+                procedure.finished(graph);
+            }
+
+            TripleIntProcedure proc = new TripleIntProcedureAdapter() {
+
+                @Override
+                public void execute(ReadGraphImpl graph, int s, int p, int o) throws DatabaseException {
+                    procedure.execute(graph, s, p, o);
+                }
+
+                @Override
+                public void finished(ReadGraphImpl graph) throws DatabaseException {
+                    dec(graph);
+                }
+
+                @Override
+                public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
+                    dec(graph);
+                    procedure.exception(graph, t);
+                }
+
+            }; 
+
+            @Override
+            public void execute(ReadGraphImpl graph, int type) throws DatabaseException {
+                inc();
+                QueryCache.runnerAssertedStatements(graph, type, r2, parent, null, proc);
+            }
+
+            @Override
+            public void finished(ReadGraphImpl graph) throws DatabaseException {
+                dec(graph);       
+            }
+
+            @Override
+            public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
+                dec(graph);
+            }
+
+        });
+
+
+    }
+
+    final static public void computeNotFunctionalIndex(ReadGraphImpl graph, final int r1, final int r2, final Statements parent, final RelationInfo ri, final TripleIntProcedure procedure) throws DatabaseException {
+
+        if(ri.isFinal) {
+
+            graph.processor.querySupport.getObjects(graph, r1, r2, new IntProcedure() {
+
+                @Override
+                public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
+                    procedure.execute(graph, r1, r2, i);
+                }
+
+                @Override
+                public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
+                    if(DebugException.DEBUG) new DebugException(t).printStackTrace();
+                    procedure.exception(graph, t);
+                }
+
+                @Override
+                public void finished(ReadGraphImpl graph) {
+                }
+
+            });
+
+            if(ri.isAsserted) {
+                forAssertions(graph, r1, r2, parent, procedure);
+            } else {
+                procedure.finished(graph);
+            }
+
+        } else {
+
+            // Note! The dependency is intentionally cut!
+            IntSet direct = QueryCache.resultDirectPredicates(graph, r1, null, null);
+            direct.forEach(graph, new SyncIntProcedure() {
+
+                @Override
+                public void run(ReadGraphImpl graph) throws DatabaseException {
+                    forAssertions(graph, r1, r2, parent, procedure);
+                }
+
+                @Override
+                public void execute(ReadGraphImpl graph, final int pred2) throws DatabaseException {
+
+                    if(pred2 == r2) {
+
+                        // Note! The dependency is intentionally cut!
+                        QueryCache.runnerDirectObjects(graph, r1, pred2, null, null, new IntProcedure() {
+
+                            @Override
+                            public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
+                                procedure.execute(graph, r1, pred2, i);
+                            }
+
+                            @Override
+                            public void finished(ReadGraphImpl graph) throws DatabaseException {
+                            }
+
+                            @Override
+                            public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
+                                procedure.exception(graph, t);
+                            }
+
+                        });
+
+                    } else {
+
+                        try {
+
+                            IntSet result = QueryCache.resultSuperRelations(graph, pred2, parent, null);
+                            if(result.contains(r2)) {
+
+                                inc();
+
+                                // Note! The dependency is intentionally cut!
+                                QueryCache.runnerDirectObjects(graph, r1, pred2, null, null, new IntProcedure() {
+
+                                    @Override
+                                    public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
+                                        procedure.execute(graph, r1, pred2, i);
+
+                                    }
+
+                                    @Override
+                                    public void finished(ReadGraphImpl graph) throws DatabaseException {
+                                        dec(graph);
+                                    }
+
+                                    @Override
+                                    public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
+                                        procedure.exception(graph, t);
+                                        dec(graph);
+                                    }
+
+                                });
+
+                            }
+
+                        } catch (Throwable e) {
+                            procedure.exception(graph, e);
+                        }
+
+                    }
+
+                }
+
+                @Override
+                public void finished(ReadGraphImpl graph) throws DatabaseException {
+                    dec(graph);
+                }
+
+            });
+
+        }
+
+    }
+
+    public static void computeForEach(ReadGraphImpl graph, final int r1, final int r2, final Statements entry, final TripleIntProcedure procedure_) throws DatabaseException {
+
+        TripleIntProcedure procedure = entry != null ? entry : procedure_;
+
+        RelationInfo ri = QueryCache.resultRelationInfoQuery(graph, r2, entry, null);
+        graph.ensureLoaded(r1, r2);
+        if(ri.isFunctional) {
+            computeFunctionalIndex(graph, r1, r2, entry, ri, procedure);
+        } else {
+            computeNotFunctionalIndex(graph, r1, r2, entry, ri, procedure);
+        }
+
+        if(entry != null) entry.performFromCache(graph, procedure_);
+
+    }
+
+    @Override
+    public String toString() {
+        return "Statements[" + r1() + " - " + r2() + "]";
+    }
+
+    final private void finish(ReadGraphImpl graph, TripleIntProcedure procedure) throws DatabaseException {
+
+        assert(assertPending());
+
+        synchronized(this) {
+            setReady();
+            //new Exception(toString() + " is READY").printStackTrace();
+        }
+
+        IntArray v = (IntArray)getResult();
+        final IntArray value = (IntArray)getResult();
+
+        for(int i=0;i<value.size();i+=3) {
+            procedure.execute(graph, value.data[i], value.data[i+1], value.data[i+2]);
+        }
+
+        procedure.finished(graph);
+
+    }
+
+    synchronized public void addOrSet(int s, int p, int o) {
+
+        assert(assertPending());
+
+        IntArray value = (IntArray)getResult();
+        value.add(s);
+        value.add(p);
+        value.add(o);
+
+    }
+
+    final static public int r1(long id) {
+        return (int)(id>>>32);
+    }
+
+    final static public int r2(long id) {
+        return (int)id;
+    }
+
+    final public void addOrSetFunctional(int s, long po) {
+
+        addOrSetFunctional(s, r1(po), r2(po));
+
+    }
+
+    final public void addOrSetFunctional(int s, int p, int o) {
+
+        assert(assertPending());
+
+        IntArray value = (IntArray)getResult();
+        value.add(s);
+        value.add(p);
+        value.add(o);
+
+    }
+
+    @Override
+    public Object performFromCache(ReadGraphImpl graph, final TripleIntProcedure procedure) throws DatabaseException {
+
+        assert(isReady());
+
+        final IntArray value = (IntArray)getResult();
+
+        if(handleException(graph, procedure)) return value;
+
+        for(int i=0;i<value.size();i+=3) {
+            procedure.execute(graph, value.data[i], value.data[i+1], value.data[i+2]);
+        }
+
+        procedure.finished(graph);
+
+        return value;
+
+    }
+
+    @Override
+    public void recompute(ReadGraphImpl graph) throws DatabaseException {
+
+        computeForEach(graph, r1(), r2(), this, new TripleIntProcedureAdapter() {
+
+            @Override
+            public void finished(ReadGraphImpl graph) {
+            }
+
+            @Override
+            public void exception(ReadGraphImpl graph, Throwable t) {
+                new Error("Error in recompute.", t).printStackTrace();
+            }
+
+        });
+
+    }
+
+    @Override
+    public int type() {
+        return RequestFlags.IMMEDIATE_UPDATE;
+    }
+
+
+    @Override
+    boolean isImmutable(ReadGraphImpl graph) {
+        return graph.processor.isImmutable(r1());
+    }
+
+    @Override
+    public void execute(ReadGraphImpl graph, int s, int p, int o) throws DatabaseException {
+        addOrSet(s, p, o);
+    }
+
+    @Override
+    public void finished(ReadGraphImpl graph) throws DatabaseException {
+        setReady();
+    }
+
+    @Override
+    public void exception(ReadGraphImpl graph, Throwable throwable) throws DatabaseException {
+        except(throwable);
+    }
+
+}