]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.procore/src/fi/vtt/simantics/procore/internal/QuerySupportImpl.java
Multiple reader thread support for db client
[simantics/platform.git] / bundles / org.simantics.db.procore / src / fi / vtt / simantics / procore / internal / QuerySupportImpl.java
index 190db364cbdf8bda86c56cc2c4f427990200c352..eea97e2e0a673c0050cf0f14f6aa1d2c99494719 100644 (file)
@@ -1,7 +1,5 @@
 package fi.vtt.simantics.procore.internal;
 
-import gnu.trove.set.hash.TIntHashSet;
-
 import java.io.ByteArrayInputStream;
 import java.io.InputStream;
 import java.util.Collection;
@@ -35,9 +33,14 @@ import org.simantics.db.procore.cluster.ClusterImpl;
 import org.simantics.db.procore.cluster.ClusterSmall;
 import org.simantics.db.service.SerialisationSupport;
 import org.simantics.utils.DataContainer;
+import org.slf4j.LoggerFactory;
+
+import gnu.trove.set.hash.TIntHashSet;
 
 public class QuerySupportImpl implements QuerySupport {
-       
+
+    private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(QuerySupportImpl.class);
+
        final SessionImplSocket session;
        final State state;
        final ClusterTable clusterTable;
@@ -129,7 +132,8 @@ public class QuerySupportImpl implements QuerySupport {
                try {
                        return serializationSupport.getResource(id);
                } catch (DatabaseException e) {
-                       e.printStackTrace();
+                       // TODO: consider changing this method to throw something
+                       LOGGER.error("getResource(" + id + ") failed", e);
                }
                return null;
        }
@@ -212,7 +216,11 @@ public class QuerySupportImpl implements QuerySupport {
 
 //                    int suggestSchedule = graph.processor.processor.resourceThread(id);
 //                    if(graph.callerThread == suggestSchedule) {
-                       procedure.execute(graph, new ResourceImpl(resourceSupport, id));
+                       try {
+                                                       procedure.execute(graph, new ResourceImpl(resourceSupport, id));
+                                               } catch (DatabaseException e) {
+                                                       LOGGER.error("Unexpected exception while handling object", e);
+                                               }
 //                    } else {
 //                     graph.processor.processor.schedule(graph.callerThread, new SessionTask(suggestSchedule) {
 //             
@@ -226,7 +234,11 @@ public class QuerySupportImpl implements QuerySupport {
                        
                 }
                        }
-                       procedure.finished(graph);
+               try {
+                       procedure.finished(graph);
+                       } catch (DatabaseException e) {
+                               LOGGER.error("Unexpected exception while handling objects", e);
+                       }
 //             graph.dec();
                return;
                
@@ -234,14 +246,12 @@ public class QuerySupportImpl implements QuerySupport {
                
         final ClusterImpl cluster = (ClusterImpl)clusterTable.getClusterByResourceKey(subject);
         if(!cluster.isLoaded()) {
-               cluster.load(session.clusterTranslator, new Runnable() {
-
-                               @Override
-                               public void run() {
-                                       getObjects4(graph, subject, procedure);
-                               }
-                       
-               });
+               try {
+                               cluster.load();
+                       } catch (DatabaseException e) {
+                               LOGGER.error("Unexpected exception while handling objects", e);
+                       }
+               getObjects4(graph, subject, procedure);
                return;
         }
                
@@ -249,9 +259,15 @@ public class QuerySupportImpl implements QuerySupport {
                        
                        for(TransientGraph g : virtualGraphServerSupport.providers) {
                 for (final int id : g.getObjects(subject, procedure.predicateKey)) {
+                       try {
+                       procedure.execute(graph, new ResourceImpl(resourceSupport, id));
+                               } catch (DatabaseException e) {
+                                       LOGGER.error("Unexpected exception while handling objects", e);
+                               }
+                       
+                       
 //                    int suggestSchedule = graph.processor.processor.resourceThread(id);
 //                    if(graph.callerThread == suggestSchedule) {
-                       procedure.execute(graph, new ResourceImpl(resourceSupport, id));
 //                    } else {
 //                     graph.processor.processor.schedule(graph.callerThread, new SessionTask(suggestSchedule) {
 //             
@@ -292,7 +308,15 @@ public class QuerySupportImpl implements QuerySupport {
 
 //                    int suggestSchedule = graph.processor.processor.resourceThread(id);
 //                    if(graph.callerThread == suggestSchedule) {
-                       procedure.execute(graph, context, new ResourceImpl(resourceSupport, id));
+                       
+                       try {
+                               procedure.execute(graph, context, new ResourceImpl(resourceSupport, id));
+                               } catch (DatabaseException e) {
+                                       LOGGER.error("Unexpected exception while handling objects", e);
+                               }
+                       
+                       
+                       
 //                    } else {
 //                     graph.processor.processor.schedule(graph.callerThread, new SessionTask(suggestSchedule) {
 //             
@@ -306,7 +330,14 @@ public class QuerySupportImpl implements QuerySupport {
                        
                 }
                        }
-                       procedure.finished(graph, context);
+                       
+               try {
+                       procedure.finished(graph, context);
+                       } catch (DatabaseException e) {
+                               LOGGER.error("Unexpected exception while handling objects", e);
+                       }
+
+                       
 //             graph.dec();
                return;
                
@@ -331,7 +362,11 @@ public class QuerySupportImpl implements QuerySupport {
                 for (final int id : g.getObjects(subject, procedure.predicateKey)) {
 //                    int suggestSchedule = graph.processor.processor.resourceThread(id);
 //                    if(graph.callerThread == suggestSchedule) {
-                       procedure.execute(graph, context, new ResourceImpl(resourceSupport, id));
+                    try {
+                        procedure.execute(graph, context, new ResourceImpl(resourceSupport, id));
+                    } catch (DatabaseException e) {
+                        LOGGER.error("Unexpected exception while handling objects", e);
+                    }
 //                    } else {
 //                     graph.processor.processor.schedule(graph.callerThread, new SessionTask(suggestSchedule) {
 //             
@@ -435,178 +470,8 @@ public class QuerySupportImpl implements QuerySupport {
        
     }
 
-//     @Override
-//    public void getSingleSuperrelation(ReadGraphImpl graph, final int subject, final AsyncProcedure<Resource> procedure) {
-//     
-//     // Do not process this information for virtual resources
-//     if(subject < 0) {
-//             procedure.execute(graph, null);
-//                     graph.state.barrier.dec();
-//             return;
-//     }
-//
-//     final ClusterI cluster = clusterTable.getClusterByResourceKey(subject);
-//        if (cluster == null)
-//            System.out.println("null cluster: " + Integer.toString(subject, 16));
-//        
-//        assert (cluster != null);
-//
-//        if(!cluster.isLoaded()) {
-//            
-//             procedure.execute(graph, null);
-//                     graph.state.barrier.dec();
-//             return;
-//
-////           queryProvider2.requestCluster(callerThread, cluster.getClusterId(), new Callback<Integer>() {
-////   
-////                           @Override
-////                           public void run(Integer i) {
-////   
-////                                   queryProvider2.schedule(i, callerThread, new Runnable() {
-////   
-////                                           @Override
-////                                           public void run() {
-////                                                   
-////                                                   try  {
-////                                                   
-////                                               ClusterI.CompleteTypeEnum type = cluster.getCompleteType(callerThread, subject, SessionImplSocket.this);
-////                                               if(ClusterI.CompleteTypeEnum.SubrelationOf == type) {
-////                                                   int result = cluster.getCompleteObjectKey(callerThread, subject, SessionImplSocket.this);
-////                                                   assert(result > 0);
-////                                                   procedure.execute(graph, getResourceByKey(result));
-////                                               } else {
-////                                                   procedure.execute(graph, null);
-////                                               }
-////                                                           graph.state.barrier.dec();
-////                                                           
-////                                                   } catch (DatabaseException e) {
-////                                                           e.printStackTrace();
-////                                                   }
-////                                                   
-////                                           }
-////   
-////                                   });
-////   
-////                           }
-////   
-////                   });
-//                     
-//        } else {
-//
-//             try {
-//
-//                     ClusterI.CompleteTypeEnum type = cluster.getCompleteType(graph.callerThread, subject, clusterSupport);
-//                     if(ClusterI.CompleteTypeEnum.SubrelationOf == type) {
-//                             int result = cluster.getCompleteObjectKey(graph.callerThread, subject, clusterSupport);
-//                             assert(result > 0);
-//                             procedure.execute(graph, new ResourceImpl(resourceSupport, result));
-//                     } else {
-//                             procedure.execute(graph, null);
-//                     }
-//                     graph.state.barrier.dec();
-//
-//             } catch (DatabaseException e) {
-//                     e.printStackTrace();
-//             }
-//                     
-//        }
-//        
-//        
-//    }
-
-//     @Override
-//    public void getObjects2(final int callerThread, final int subject, final int predicate, final IntProcedure procedure) {
-//             ensureLoaded(callerThread, subject, predicate, new Runnable() {
-//
-//                     @Override
-//                     public void run() {
-//                             safeGetObjects2(callerThread, subject, predicate, procedure);
-//                     }
-//                     
-//             });
-//     }
-
-//    public void safeGetObjects2(final ReadGraphImpl graph, final int subject, final int predicate, final IntProcedure procedure) {
-//
-//        assert (subject != 0);
-//        assert (predicate != 0);
-////        System.out.println("getObjects2: s=" + subject + "p=" + predicate);
-//        Collection<TransientGraph> providers = virtualGraphServerSupport.getVirtualGraphs(subject);
-//        if (providers != null) {
-//
-//            final TIntHashSet result = new TIntHashSet(16);
-//
-//            for (VirtualGraph provider : providers) {
-//
-//                for (int id : ((VirtualGraphImpl)provider).getObjects(subject, predicate)) {
-//
-//                    if (result.add(id)) {
-//                     procedure.execute(graph, id);
-//                    }
-//
-//                }
-//
-//            }
-//
-//            if (subject < 0)
-//                return;
-//
-//            final ClusterI cluster = clusterTable.getClusterByResourceKey(subject);
-//
-//            assert (testCluster(subject, cluster));
-//            
-//            // wheels within wheels
-//            final ClusterI.ObjectProcedure<Object> proc = new ClusterI.ObjectProcedure<Object>() {
-//
-//                @Override
-//                public boolean execute(int callerThread, Object context, int object) {
-//
-//                    if (result.add(object)) {
-//                     procedure.execute(graph.newAsync(callerThread), object);
-//                    }
-//
-//                    return false; // continue looping
-//
-//                }
-//                
-//                             @Override
-//                             public boolean found() {
-//                                     throw new UnsupportedOperationException();
-//                             }
-//                
-//            };
-//
-//            try {
-//                cluster.forObjects(graph.callerThread, subject, predicate, proc, null, clusterSupport);
-//            } catch (DatabaseException e) {
-//                Logger.defaultLogError(e);
-//            } catch (Throwable t) {
-//                Logger.defaultLogError(t);
-//             t.printStackTrace();
-//            }
-//            return;
-//            
-//        }
-//        
-//        assert(subject > 0);
-//
-//        final ClusterI cluster = clusterTable.getClusterByResourceKey(subject);
-//
-//        assert (testCluster(subject, cluster));
-//        
-//        try {
-//            cluster.forObjects(graph.callerThread, subject, predicate, new Wheels(procedure), null, clusterSupport);
-//        } catch (DatabaseException e) {
-//            Logger.defaultLogError(e);
-//        } catch (Throwable t) {
-//             t.printStackTrace();
-//            Logger.defaultLogError(t);
-//        }
-//        
-//    }
-
        @Override
-    public boolean getObjects(final ReadGraphImpl graph, final int subject, final int predicate, final IntProcedure procedure) {
+    public boolean getObjects(final ReadGraphImpl graph, final int subject, final int predicate, final IntProcedure procedure) throws DatabaseException {
                
                assert (subject != 0);
                assert (predicate != 0);
@@ -656,7 +521,11 @@ public class QuerySupportImpl implements QuerySupport {
 
                                found.set(true);
                                if (result.add(object)) {
-                                       procedure.execute(graph, object);
+                                       try {
+                                                       procedure.execute(graph, object);
+                                               } catch (DatabaseException e) {
+                                                       Logger.defaultLogError(e);
+                                               }
                                }
 
                                return false; // continue looping
@@ -685,12 +554,15 @@ public class QuerySupportImpl implements QuerySupport {
                        @Override
                        public boolean execute(Object context, int object) {
 
-                               found = true;
-                               procedure.execute(graph, object);
-
-                               return false; // continue looping
+                                       found = true;
+                                       try {
+                                               procedure.execute(graph, object);
+                                       } catch (DatabaseException e) {
+                                               Logger.defaultLogError(e);
+                                       }
+                                       return false; // continue looping
 
-                       }
+                               }
 
                                public boolean found() {
                                        return found;
@@ -704,7 +576,7 @@ public class QuerySupportImpl implements QuerySupport {
                        try {
                 cluster.forObjects(subject, predicate, proc, null, clusterSupport);
                        } catch (DatabaseException e) {
-                               e.printStackTrace();
+                               Logger.defaultLogError(e);
                        } 
                        // This happens if resource is bogus
                        catch (Throwable t) {
@@ -1041,15 +913,8 @@ public class QuerySupportImpl implements QuerySupport {
 
     }
 
-//    @Override
-//    public void getStatements(ReadGraphImpl graph, final int subject, final Procedure<DirectStatements> procedure) {
-//     
-//             procedure.exception(new DatabaseException("Not supported"));
-//             
-//    }
-
     @Override
-    public void getPredicates(final ReadGraphImpl graph, final int subject, final IntProcedure procedure) {
+    public void getPredicates(final ReadGraphImpl graph, final int subject, final IntProcedure procedure) throws DatabaseException {
 
         final TIntHashSet result = new TIntHashSet(16);
 
@@ -1077,19 +942,6 @@ public class QuerySupportImpl implements QuerySupport {
         }
 
         ClusterI proxy = clusterTable.getClusterByResourceKey(subject);
-//        if(!proxy.isLoaded()) {
-//             
-//             proxy.load(callerThread, session, new Runnable() {
-//
-//                             @Override
-//                             public void run() {
-//                                     getPredicates(callerThread, subject, procedure);
-//                             }
-//                     
-//             });
-//             return;
-//             
-//        }
         assert (proxy != null);
 
         final DataContainer<Integer> got = new DataContainer<Integer>(0);
@@ -1097,7 +949,11 @@ public class QuerySupportImpl implements QuerySupport {
             @Override
             public boolean execute(Object context, int predicate, int oi) {
                 if (result.add(predicate)) {
-                    procedure.execute(graph, predicate);
+                    try {
+                        procedure.execute(graph, predicate);
+                    } catch (DatabaseException e) {
+                        Logger.defaultLogError(e);
+                    }
                 }
                 got.set(got.get() + 1);
                 return false; // continue looping
@@ -1533,7 +1389,7 @@ public class QuerySupportImpl implements QuerySupport {
                        
                } else {
                        
-                               new Exception().printStackTrace();
+                               //new Exception().printStackTrace();
                        
                        cluster.load(session.clusterTranslator, new Runnable() {
 
@@ -1583,7 +1439,7 @@ public class QuerySupportImpl implements QuerySupport {
                } else {
                        
 //                     System.err.println("cluster not loaded " + subject);
-                               new Exception().printStackTrace();
+                               //new Exception().printStackTrace();
                        
                        cluster.load(session.clusterTranslator, new Runnable() {
 
@@ -1701,7 +1557,7 @@ public class QuerySupportImpl implements QuerySupport {
                        try {
                                ((ClusterSmall)cluster).check();
                        } catch (DatabaseException e) {
-                               e.printStackTrace();
+                               LOGGER.error("ClusterSmall.check failed", e);
                        }
        
     }