]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/URIToResource.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / URIToResource.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2018 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.exception.ResourceNotFoundException;
19 import org.simantics.db.impl.graph.ReadGraphImpl;
20 import org.simantics.db.impl.procedure.InternalProcedure;
21
22 public class URIToResource extends StringQuery<InternalProcedure<Integer>> implements InternalProcedure<Integer> {
23
24     URIToResource(String id) {
25         super(id);
26     }
27
28     @Override
29     public final void removeEntry(QueryProcessor provider) {
30         provider.cache.remove(this);
31     }
32
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         InternalProcedure<Integer> procedure = entry != null ? entry : procedure_;
41
42         if("http://".equals(id) || "http:/".equals(id)) {
43
44             QueryProcessor processor = graph.processor;
45             procedure.execute(graph, processor.getRootLibrary());
46
47         } else {
48
49             final String[] parts = URIStringUtils.splitURI(id);
50             if (parts != null) {
51
52                 QueryCache.runnerURIToResource(graph, parts[0], entry, null, new InternalProcedure<Integer>() {
53
54                     @Override
55                     public void execute(ReadGraphImpl graph, Integer parentId) throws DatabaseException {
56
57                         ObjectResourceIdMap<String> map = QueryCache.resultChildMap(graph, parentId, entry, null);
58                         assert(map != null);
59                         int result = map.getId(URIStringUtils.unescape(parts[1]));
60                         if (result == 0) {
61                             ResourceNotFoundException e = new ResourceNotFoundException(id);
62                             procedure.exception(graph, e);
63                         } else {
64                             procedure.execute(graph, result);
65                         }
66                     }
67
68                     @Override
69                     public void exception(ReadGraphImpl graph, Throwable throwable) throws DatabaseException {
70                         procedure.exception(graph, throwable);
71                     }
72
73                 });
74
75             } else {
76                 ResourceNotFoundException e = new ResourceNotFoundException(id);
77                 procedure.exception(graph, e);
78             }
79
80         }
81
82         if(entry != null) entry.performFromCache(graph, procedure_);
83
84     }
85
86     public void addOrSet(Integer result) {
87
88         assert(isPending());
89
90         synchronized(this) {
91             setResult(result);
92             setReady();
93         }
94
95     }
96
97     @Override
98     public String toString() {
99         return "URIToResource[" + id + "]";
100     }
101
102     @Override
103     public Object performFromCache(ReadGraphImpl graph, InternalProcedure<Integer> procedure) throws DatabaseException {
104
105         assert(isReady());
106
107         if(handleException(graph, procedure)) return (Throwable)getResult();
108
109         Integer result = (Integer)getResult();
110         procedure.execute(graph, result);
111         return result;
112
113     }
114
115     @Override
116     public void recompute(ReadGraphImpl graph) throws DatabaseException {
117
118         compute(graph, new InternalProcedure<Integer>() {
119
120             @Override
121             public void execute(ReadGraphImpl graph, Integer result) {
122             }
123
124             @Override
125             public void exception(ReadGraphImpl graph, Throwable t) {
126                 if(DebugException.DEBUG) new DebugException(t).printStackTrace();
127                 throw new Error("Error in recompute.", t);
128             }
129
130         });
131
132     }
133
134     @Override
135     public void execute(ReadGraphImpl graph, Integer result) throws DatabaseException {
136         synchronized(this) {
137             setResult(result);
138             setReady();
139         }
140     }
141
142     @Override
143     public void exception(ReadGraphImpl graph, Throwable throwable) throws DatabaseException {
144         except(throwable);
145     }
146
147     @Override
148     public long cluster(QueryProcessor processor) {
149         return 0;
150     }
151     
152     @Override
153     public void serializeKey(QuerySerializer serializer) {
154         serializer.addString(id);
155     }
156
157     @Override
158     public void serializeValue(QuerySerializer serializer) {
159         Integer value = getResult();
160         serializer.addResource(value);
161     }
162
163 }