]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/URIToResource.java
Multiple readers and variable optimization
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / URIToResource.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.db.impl.query;
13
14 import org.simantics.databoard.util.URIStringUtils;
15 import org.simantics.db.ObjectResourceIdMap;
16 import org.simantics.db.common.exception.DebugException;
17 import org.simantics.db.exception.DatabaseException;
18 import org.simantics.db.impl.graph.ReadGraphImpl;
19 import org.simantics.db.impl.procedure.InternalProcedure;
20
21 public class URIToResource extends StringQuery<InternalProcedure<Integer>> {
22
23     URIToResource(final String id) {
24         super(id);
25     }
26
27     @Override
28     final public void removeEntry(QueryProcessor provider) {
29         provider.cache.remove(this);
30     }
31     
32     @Override
33     public Object compute(ReadGraphImpl graph, final InternalProcedure<Integer> procedure) throws DatabaseException {
34         computeForEach(graph, id, this, procedure);
35         return getResult();
36     }
37
38     static void computeForEach(ReadGraphImpl graph, String id, final URIToResource entry, final InternalProcedure<Integer> procedure) throws DatabaseException {
39         
40         if("http://".equals(id) || "http:/".equals(id)) {
41             
42                 QueryProcessor processor = graph.processor;
43             if(entry != null) entry.addOrSet(graph, processor, processor.getRootLibrary());
44             procedure.execute(graph, processor.getRootLibrary());
45
46         } else {
47             
48             final String[] parts = URIStringUtils.splitURI(id);
49             if (parts != null) {
50
51                 Integer parentId = QueryCache.resultURIToResource(graph, parts[0], entry, null);
52                 ObjectResourceIdMap<String> map = QueryCache.resultChildMap(graph, parentId, entry, null);
53                 if(map == null) {
54                         procedure.execute(graph, 0);
55                         if(entry != null) entry.addOrSet(graph, graph.processor, 0);
56                 } else {
57                         int result = map.getId(URIStringUtils.unescape(parts[1]));
58                         if(entry != null) entry.addOrSet(graph, graph.processor, result);
59                         procedure.execute(graph, result);
60                 }
61                 
62             } else {
63                 
64                 DatabaseException e = new DatabaseException("No URI for " + id);
65                 if(entry != null) entry.except(e);
66                 procedure.exception(graph, e);
67                 
68             }
69
70         }
71         
72     }
73     
74     public void addOrSet(ReadGraphImpl graph, QueryProcessor provider, Integer result) {
75
76         assert(isPending());
77
78         synchronized(this) {
79             setResult(result);
80             setReady();
81         }
82         
83     }
84     
85     @Override
86     public String toString() {
87         return "URIToResource[" + id + "]";
88     }
89
90     @Override
91     public Object performFromCache(ReadGraphImpl graph, InternalProcedure<Integer> procedure) throws DatabaseException {
92         
93         assert(isReady());
94         
95         if(handleException(graph, procedure)) return (Throwable)statusOrException;
96
97         Integer result = (Integer)getResult();
98         procedure.execute(graph, result);
99         return result;
100         
101     }
102     
103     @Override
104     public void recompute(ReadGraphImpl graph) throws DatabaseException {
105         
106         compute(graph, new InternalProcedure<Integer>() {
107
108             @Override
109             public void execute(ReadGraphImpl graph, Integer result) {
110             }
111             
112             @Override
113             public void exception(ReadGraphImpl graph, Throwable t) {
114                 if(DebugException.DEBUG) new DebugException(t).printStackTrace();
115                 throw new Error("Error in recompute.", t);
116             }
117
118         });
119         
120     }
121         
122 }