]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/QueryDeserializer.java
DB query swapping to file system
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / QueryDeserializer.java
1 package org.simantics.db.impl.query;
2
3 import java.lang.reflect.InvocationTargetException;
4 import java.util.Arrays;
5 import java.util.HashMap;
6 import java.util.Map;
7
8 import org.simantics.db.ObjectResourceIdMap;
9 import org.simantics.db.exception.DatabaseException;
10 import org.simantics.db.impl.ClusterBase;
11 import org.simantics.db.impl.ClusterSupport;
12 import org.simantics.db.impl.ClusterTraitsBase;
13 import org.simantics.db.service.Bytes;
14 import org.slf4j.LoggerFactory;
15
16 import gnu.trove.map.hash.TIntLongHashMap;
17
18 public class QueryDeserializer {
19
20     private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(QueryDeserializer.class);
21
22     QueryCache qc;
23     QuerySupport qs;
24     ClusterSupport cs;
25     
26     private byte[] bytes;
27     private int byteIndex;
28     
29     private TIntLongHashMap clusterKeys = new TIntLongHashMap();
30     private Map<Integer,QueryFactory> ids = new HashMap<Integer,QueryFactory>();
31
32     public QueryDeserializer(QueryProcessor qp, byte[] bytes) {
33         this.qc = qp.cache;
34         this.qs = qp.querySupport;
35         this.cs = qs.getClusterSupport();;
36         this.bytes = bytes;
37     }
38     
39     public byte readByte() {
40         return bytes[byteIndex++];
41     }
42     
43     public int readLE4() {
44         int result = Bytes.readLE4(bytes, byteIndex);
45         byteIndex += 4;
46         return result;
47     }
48     
49     public long readLE8() {
50         long result = Bytes.readLE8(bytes, byteIndex);
51         byteIndex += 8;
52         return result;
53     }
54
55     public byte[] readBytes(int len) {
56         byte[] result = Arrays.copyOfRange(bytes, byteIndex, byteIndex+len);
57         byteIndex += len;
58         return result;
59     }
60     
61     public void readHeaders() {
62         int idsSize = readLE4();
63         for(int i=0;i<idsSize;i++) {
64             int size = readLE4();
65             byte[] data = readBytes(size);
66             String id = new String(data);
67             int key = readLE4();
68             try {
69                 Class<QueryFactory> clazz = (Class<QueryFactory>)getClass().getClassLoader().loadClass(id + "Factory");
70                 QueryFactory qf = clazz.getDeclaredConstructor().newInstance(); 
71                 ids.put(key, qf);
72             } catch (ClassNotFoundException e) {
73                 LOGGER.error("Error while resolving QueryFactory", e);
74             } catch (InstantiationException e) {
75                 LOGGER.error("Error while resolving QueryFactory", e);
76             } catch (IllegalAccessException e) {
77                 LOGGER.error("Error while resolving QueryFactory", e);
78             } catch (IllegalArgumentException e) {
79                 LOGGER.error("Error while resolving QueryFactory", e);
80             } catch (InvocationTargetException e) {
81                 LOGGER.error("Error while resolving QueryFactory", e);
82             } catch (NoSuchMethodException e) {
83                 LOGGER.error("Error while resolving QueryFactory", e);
84             } catch (SecurityException e) {
85                 LOGGER.error("Error while resolving QueryFactory", e);
86             }
87         }
88         int clusterKeysSize = readLE4();
89         for(int i=0;i<clusterKeysSize;i++) {
90             long cluster = readLE8();
91             int key = readLE4();
92             clusterKeys.put(key, cluster);
93         }
94     }
95     
96     public QueryFactory readFactory() {
97         int key = readLE4();
98         return ids.get(key);
99     }
100
101     public void readQueries() {
102         int count = readLE4();
103         for(int i=0;i<count;i++) {
104             QueryFactory qf = readFactory();
105             try {
106                 qf.read(this);
107             } catch (DatabaseException e) {
108                 e.printStackTrace();
109             }
110         }
111     }
112
113     public int readResource() throws DatabaseException {
114         int key = readLE4();
115         if(key < 0)
116             return key;
117         int clusterKey = ClusterTraitsBase.getClusterKeyFromResourceKey(key);
118         long cluster = clusterKeys.get(clusterKey);
119         ClusterBase cb = cs.getClusterByClusterId(cluster);
120         return ClusterTraitsBase.createResourceKey(cb.getClusterKey(), ClusterTraitsBase.getResourceIndexFromResourceKey(key));
121     }
122     
123     public byte[] readByteArray() {
124         int len = readLE4();
125         if(len == -1)
126             return null;
127         return readBytes(len);
128     }
129     
130     public String readString() {
131         return new String(readByteArray());
132     }
133     
134     public ObjectResourceIdMap<String> createChildMap() {
135         return qs.createChildMap();
136     }
137
138     AssertedPredicates readAssertedPredicates() throws DatabaseException {
139         int r = readResource();
140         return qc.getOrCreateAssertedPredicates(r);
141     }
142
143     AssertedStatements readAssertedStatements() throws DatabaseException {
144         int r1 = readResource();
145         int r2 = readResource();
146         return qc.getOrCreateAssertedStatements(r1, r2);
147     }
148
149     ChildMap readChildMap() throws DatabaseException {
150         int r = readResource();
151         return qc.getOrCreateChildMap(r);
152     }
153
154     DirectObjects readDirectObjects() throws DatabaseException {
155         int r1 = readResource();
156         int r2 = readResource();
157         return qc.getOrCreateDirectObjects(r1, r2);
158     }
159     
160     DirectPredicates readDirectPredicates() throws DatabaseException {
161         int r = readResource();
162         return qc.getOrCreateDirectPredicates(r);
163     }
164
165     Objects readObjects() throws DatabaseException {
166         int r1 = readResource();
167         int r2 = readResource();
168         return qc.getOrCreateObjects(r1, r2);
169     }
170
171     OrderedSet readOrderedSet() throws DatabaseException {
172         int r = readResource();
173         return qc.getOrCreateOrderedSet(r);
174     }
175
176     Predicates readPredicates() throws DatabaseException {
177         int r = readResource();
178         return qc.getOrCreatePredicates(r);
179     }
180
181     PrincipalTypes readPrincipalTypes() throws DatabaseException {
182         int r = readResource();
183         return qc.getOrCreatePrincipalTypes(r);
184     }
185
186     RelationInfoQuery readRelationInfoQuery() throws DatabaseException {
187         int r = readResource();
188         return qc.getOrCreateRelationInfoQuery(r);
189     }
190
191     Statements readStatements() throws DatabaseException {
192         int r1 = readResource();
193         int r2 = readResource();
194         return qc.getOrCreateStatements(r1, r2);
195     }
196
197     SuperRelations readSuperRelations() throws DatabaseException {
198         int r = readResource();
199         return qc.getOrCreateSuperRelations(r);
200     }
201
202     SuperTypes readSuperTypes() throws DatabaseException {
203         int r = readResource();
204         return qc.getOrCreateSuperTypes(r);
205     }
206
207     TypeHierarchy readTypeHierarchy() throws DatabaseException {
208         int r = readResource();
209         return qc.getOrCreateTypeHierarchy(r);
210     }
211
212     Types readTypes() throws DatabaseException {
213         int r = readResource();
214         return qc.getOrCreateTypes(r);
215     }
216
217     URIToResource readURIToResource() throws DatabaseException {
218         String s = readString();
219         return qc.getOrCreateURIToResource(s);
220     }
221
222     ValueQuery readValueQuery() throws DatabaseException {
223         int r = readResource();
224         return qc.getOrCreateValueQuery(r);
225     }
226
227 }