]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
Fixing URIToResource 09/1909/2
authorjsimomaa <jani.simomaa@gmail.com>
Wed, 11 Jul 2018 05:19:08 +0000 (08:19 +0300)
committerJani Simomaa <jani.simomaa@semantum.fi>
Wed, 11 Jul 2018 06:41:05 +0000 (06:41 +0000)
Change-Id: I67d0145fe48235eeef79212269f5d561f4899180

bundles/org.simantics.db.common/src/org/simantics/db/common/GraphSemaphore.java
bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/URIToResource.java

index 2924f0275a394cb57e160a752c8323cb3c84f03d..d5d7735aad5d3188ce75253f20eb90fdebbcce2e 100644 (file)
@@ -8,41 +8,35 @@ import org.simantics.db.exception.DatabaseException;
 
 public class GraphSemaphore extends Semaphore {
 
-       private static final long serialVersionUID = 2114861433176831938L;
-       
-       private final AsyncReadGraph graph;
-       
-       public GraphSemaphore(AsyncReadGraph graph, int permits) {
-               super(permits);
-               this.graph = graph;
-       }
-
-       public void waitFor(int permits) throws DatabaseException, InterruptedException {
-               
-       boolean success = false;
-       success = tryAcquire(permits);
-       if(success) return;
-       
-       while(!success) {
-               
-               if(graph.performPending()) {
-                       // Some task was done
-                       success = tryAcquire(permits);          
-               } else {
-                       // Nothing to do - just wait
-               try {
-                               success = tryAcquire(permits, 10, TimeUnit.SECONDS);
-                               if(!success) throw new DatabaseException("Timeout while waiting for async request to complete.");
-                       } catch (InterruptedException e) {
-                               throw new DatabaseException(e);
-                       }
-               }
-                               
-       }
-
-               
-       }
-       
-       
+    private static final long serialVersionUID = 2114861433176831938L;
 
+    private final AsyncReadGraph graph;
+
+    public GraphSemaphore(AsyncReadGraph graph, int permits) {
+        super(permits);
+        this.graph = graph;
+    }
+
+    public void waitFor(int permits) throws DatabaseException, InterruptedException {
+        boolean success = false;
+        success = tryAcquire(permits);
+        if (success)
+            return;
+
+        while (!success) {
+            if (graph.performPending()) {
+                // Some task was done
+                success = tryAcquire(permits);
+            } else {
+                // Nothing to do - just wait
+                try {
+                    success = tryAcquire(permits, 10, TimeUnit.SECONDS);
+                    if (!success)
+                        throw new DatabaseException("Timeout while waiting for async request to complete.");
+                } catch (InterruptedException e) {
+                    throw new DatabaseException(e);
+                }
+            }
+        }
+    }
 }
index 794bcc8edc3258fdee965278321c41f8e3789156..918f882f179c2acd4ba8b79677f473abacf67deb 100644 (file)
@@ -49,17 +49,46 @@ public class URIToResource extends StringQuery<InternalProcedure<Integer>> {
             final String[] parts = URIStringUtils.splitURI(id);
             if (parts != null) {
 
-               Integer parentId = QueryCache.resultURIToResource(graph, parts[0], entry, null);
-               ObjectResourceIdMap<String> map = QueryCache.resultChildMap(graph, parentId, entry, null);
-               if(map == null) {
-                       procedure.execute(graph, 0);
-                       if(entry != null) entry.addOrSet(graph, graph.processor, 0);
-               } else {
-                       int result = map.getId(URIStringUtils.unescape(parts[1]));
-                       if(entry != null) entry.addOrSet(graph, graph.processor, result);
-                       procedure.execute(graph, result);
-               }
+               //Integer parentId = QueryCache.resultURIToResource(graph, parts[0], entry, null);
                
+               QueryCache.runnerURIToResource(graph, parts[0], entry, null, new InternalProcedure<Integer>() {
+
+                    @Override
+                    public void execute(ReadGraphImpl graph, Integer parentId) throws DatabaseException {
+                        
+                        ObjectResourceIdMap<String> map = QueryCache.resultChildMap(graph, parentId, entry, null);
+                        assert(map != null);
+//                      if(map == null) {
+//                          throw new DatabaseException("Internal Error, contact application support.");
+//                            if(entry != null) entry.except(e);
+//                            procedure.exception(graph, e);
+////                            procedure.execute(graph, 0);
+////                            if(entry != null) entry.addOrSet(graph, graph.processor, 0);
+//                      } else {
+                            int result = map.getId(URIStringUtils.unescape(parts[1]));
+                            if(result == 0) {
+                                ResourceNotFoundException e = new ResourceNotFoundException("No resource for URI: " + id);
+                                if(entry != null) entry.except(e);
+                                procedure.exception(graph, e);
+                            } else {
+                                if(entry != null) entry.addOrSet(graph, graph.processor, result);
+                                procedure.execute(graph, result);
+                            }
+//                      }
+                        
+                        // TODO Auto-generated method stub
+                        
+                    }
+
+                    @Override
+                    public void exception(ReadGraphImpl graph, Throwable throwable) throws DatabaseException {
+                        if(entry != null) entry.except(throwable);
+                        procedure.exception(graph, throwable);
+                    }
+                    
+                });
+               
+                       
             } else {
                
                 ResourceNotFoundException e = new ResourceNotFoundException("No resource for URI: " + id);
@@ -93,7 +122,7 @@ public class URIToResource extends StringQuery<InternalProcedure<Integer>> {
         
         assert(isReady());
         
-       if(handleException(graph, procedure)) return (Throwable)statusOrException;
+       if(handleException(graph, procedure)) return (Throwable)getResult();
 
        Integer result = (Integer)getResult();
        procedure.execute(graph, result);