]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/URIToResource.java
Generate parts of db client query code
[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.common.exception.DebugException;
16 import org.simantics.db.exception.DatabaseException;
17 import org.simantics.db.impl.graph.ReadGraphImpl;
18 import org.simantics.db.impl.procedure.InternalProcedure;
19 import org.simantics.db.procedure.ListenerBase;
20
21 import gnu.trove.map.hash.TObjectIntHashMap;
22
23 public class URIToResource extends StringQuery<InternalProcedure<Integer>> {
24
25     URIToResource(final String id) {
26         super(id);
27     }
28
29     @Override
30     final public void removeEntry(QueryProcessor provider) {
31         provider.cache.remove(this);
32     }
33
34     private static void lookup(ReadGraphImpl graph, final URIToResource entry, final InternalProcedure<Integer> procedure, final String namespace, final String name) throws DatabaseException {
35         
36         QueryCache.runnerNamespaceIndex(graph, namespace, entry, null, new InternalProcedure<TObjectIntHashMap<String>>() {
37
38             @Override
39             public void execute(ReadGraphImpl graph, TObjectIntHashMap<String> index) throws DatabaseException {
40
41                 if(index != null) {
42                     int result = index.get(name);
43                     if(result != 0) {
44                         if(entry != null) entry.addOrSet(graph, graph.processor, result);
45                         procedure.execute(graph, result);
46                         return;
47                     }
48                 }
49                 
50                 Integer zero = 0;
51                 if(entry != null) entry.addOrSet(graph, graph.processor, zero);
52                 procedure.execute(graph, zero);
53
54             }
55
56             @Override
57             public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
58                 if(entry != null) entry.except(t);
59                 procedure.exception(graph, t);
60             }
61
62         });
63
64     }
65     
66     @Override
67     public Object compute(ReadGraphImpl graph, final InternalProcedure<Integer> procedure) throws DatabaseException {
68         computeForEach(graph, id, this, procedure);
69         return getResult();
70     }
71
72     static void computeForEach(ReadGraphImpl graph, String id, final URIToResource entry, final InternalProcedure<Integer> procedure) throws DatabaseException {
73         
74         if("http://".equals(id) || "http:/".equals(id)) {
75             
76                 QueryProcessor processor = graph.processor;
77             if(entry != null) entry.addOrSet(graph, processor, processor.getRootLibrary());
78             procedure.execute(graph, processor.getRootLibrary());
79
80         } else {
81             
82             final String[] parts = URIStringUtils.splitURI(id);
83             if (parts != null) {
84                 lookup(graph, entry, procedure, parts[0], parts[1]);
85             } else {
86                 lookup(graph, entry, procedure, "http://", id.replaceFirst("http://", ""));
87             }
88
89         }
90         
91     }
92     
93     public void addOrSet(ReadGraphImpl graph, QueryProcessor provider, Integer result) {
94
95         assert(isPending());
96
97         synchronized(this) {
98             setResult(result);
99             setReady();
100         }
101         
102     }
103     
104     @Override
105     public String toString() {
106         return "URIToResource[" + id + "]";
107     }
108
109     @Override
110     public Object performFromCache(ReadGraphImpl graph, InternalProcedure<Integer> procedure) throws DatabaseException {
111         
112         assert(isReady());
113         
114         if(handleException(graph, procedure)) return (Throwable)statusOrException;
115
116         Integer result = (Integer)getResult();
117         procedure.execute(graph, result);
118         return result;
119         
120     }
121     
122     @Override
123     public void recompute(ReadGraphImpl graph) throws DatabaseException {
124         
125         compute(graph, new InternalProcedure<Integer>() {
126
127             @Override
128             public void execute(ReadGraphImpl graph, Integer result) {
129             }
130             
131             @Override
132             public void exception(ReadGraphImpl graph, Throwable t) {
133                 if(DebugException.DEBUG) new DebugException(t).printStackTrace();
134                 throw new Error("Error in recompute.", t);
135             }
136
137         });
138         
139     }
140         
141 }