]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.common/src/org/simantics/db/common/utils/CommonDBUtils.java
Merge "Multiple reader thread support for db client"
[simantics/platform.git] / bundles / org.simantics.db.common / src / org / simantics / db / common / utils / CommonDBUtils.java
1 package org.simantics.db.common.utils;
2
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.Collections;
6 import java.util.HashSet;
7 import java.util.Iterator;
8 import java.util.List;
9 import java.util.Set;
10
11 import org.simantics.databoard.Bindings;
12 import org.simantics.db.ReadGraph;
13 import org.simantics.db.Resource;
14 import org.simantics.db.Statement;
15 import org.simantics.db.WriteGraph;
16 import org.simantics.db.common.procedure.adapter.DirectStatementProcedure;
17 import org.simantics.db.common.request.IsParent;
18 import org.simantics.db.common.request.ObjectsWithType;
19 import org.simantics.db.common.request.PossibleObjectWithType;
20 import org.simantics.db.common.request.PossibleOwner;
21 import org.simantics.db.exception.DatabaseException;
22 import org.simantics.db.exception.InvalidResourceReferenceException;
23 import org.simantics.db.service.ClusterUID;
24 import org.simantics.db.service.ClusteringSupport;
25 import org.simantics.db.service.DirectQuerySupport;
26 import org.simantics.db.service.SerialisationSupport;
27 import org.simantics.db.service.XSupport;
28 import org.simantics.layer0.Layer0;
29 import org.simantics.utils.datastructures.collections.CollectionUtils;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 import gnu.trove.list.array.TIntArrayList;
34 import gnu.trove.procedure.TIntProcedure;
35 import gnu.trove.set.hash.TIntHashSet;
36
37 public class CommonDBUtils {
38
39         private static final Logger LOGGER = LoggerFactory.getLogger(CommonDBUtils.class);
40
41         public static boolean isParent(ReadGraph graph, Resource possibleParent, Resource possibleChild) throws DatabaseException {
42                 return graph.sync(new IsParent(possibleParent, possibleChild));
43         }
44         
45         public static Resource parent(ReadGraph graph, Resource child) throws DatabaseException {
46                 return graph.getSingleObject(child, Layer0.getInstance(graph).PartOf);
47         }
48         
49         public static String possibleRelatedString(ReadGraph graph, Resource subject, Resource relation) throws DatabaseException {
50                 return graph.getPossibleRelatedValue(subject, relation, Bindings.STRING);
51         }
52
53         public static Integer possibleRelatedInteger(ReadGraph graph, Resource subject, Resource relation) throws DatabaseException {
54                 return graph.getPossibleRelatedValue(subject, relation, Bindings.INTEGER);
55         }
56
57     public static Resource getPossibleOwner(ReadGraph graph, Resource resource) throws DatabaseException {
58         return graph.syncRequest(new PossibleOwner(resource));
59     }
60     
61     public static Resource commonAncestor(ReadGraph graph, Resource r1, Resource r2) throws DatabaseException {
62         Layer0 L0 = Layer0.getInstance(graph);
63         if(r1.equals(r2)) return r1;
64         HashSet<Resource> visited = new HashSet<Resource>();
65         visited.add(r1);
66         visited.add(r2);
67         while(true) {
68                 if(r1 != null) {
69                         r1 = graph.getPossibleObject(r1, L0.IsOwnedBy);
70                         if(r1 != null)
71                                 if(!visited.add(r1)) return r1;
72                 }
73                 else if(r2 == null) return null;
74                 if(r2 != null) {
75                         r2 = graph.getPossibleObject(r2, L0.IsOwnedBy);
76                         if(r2 != null)
77                                 if(!visited.add(r2)) return r2;
78                 }
79         }
80     }
81         
82     public static Resource getNearestOwner(ReadGraph graph, Collection<Resource> resources) throws DatabaseException {
83
84         Layer0 L0 = Layer0.getInstance(graph);
85         
86         
87         Set<Resource> direct = new HashSet<Resource>();
88         Set<Resource> owners = new HashSet<Resource>();
89         
90         for(Resource r : resources) {
91             
92             Collection<Resource> objects = graph.getObjects(r, L0.IsOwnedBy);
93             // FIXME: 
94             // TODO: getObjects returns duplicate entries (https://www.simantics.org/redmine/issues/4885) and therefore direct is Set<Resource>. Fix getObjects to not return duplicate entries
95             if (objects.size() > 1) objects = new HashSet<Resource>(objects);
96             
97             if (objects.size() == 1)
98                 direct.addAll(objects);
99             else if (objects.isEmpty()) {
100                 for(Statement stm : graph.getStatements(r, L0.IsWeaklyRelatedTo)) {
101                     Resource inverse = graph.getPossibleInverse(stm.getPredicate());
102                     if(inverse != null) {
103                         if(graph.isSubrelationOf(inverse, L0.IsRelatedTo)) {
104                             // Filter away tags
105                             if(!r.equals(stm.getObject()))
106                                 owners.add(stm.getObject());
107                         }
108                     }
109                 }
110             } else {
111                 System.err.println("Multiple owners for " + graph.getPossibleURI(r) + " id : " + r);
112                 for (Resource r2 : objects)
113                     System.err.println("owner : " + graph.getPossibleURI(r2) + " id " + r2);
114                 return null;
115             }
116         }
117         
118         if(!direct.isEmpty()) {
119             Iterator<Resource> iter = direct.iterator();
120             Resource common = iter.next();
121             while (iter.hasNext()) {
122                 Resource other = iter.next();
123                 common = commonAncestor(graph, common, other);
124                 if (common == null) break;
125             }
126                 if(common != null)
127                         owners.add(common);
128         }
129         
130         if(!Collections.disjoint(owners, resources)) {
131             System.err.println("Overlapping owners:");
132             for(Resource r : resources)
133                 System.err.println("-resource " + NameUtils.getSafeName(graph, r, true));
134             for(Resource r : owners)
135                 System.err.println("-owner " + NameUtils.getSafeName(graph, r, true));
136             return null;
137         }
138         
139         if(owners.size() == 1) return owners.iterator().next();
140         if(owners.size() == 0) return null;
141         
142         return getNearestOwner(graph, owners);
143         
144     }
145     
146     public static Resource getClusterSetForNewResource(ReadGraph graph, Resource ... resources) throws DatabaseException {
147
148         if(resources.length == 1) return getClusterSetForNewResource(graph, resources[0]);
149         
150         Resource owner = getNearestOwner(graph, CollectionUtils.toList(resources));
151         if(owner == null) return null;
152         return getClusterSetForNewResource(graph, owner, new HashSet<Resource>());
153         
154     }
155     
156     public static Resource getClusterSetForNewResource(ReadGraph graph, Collection<Resource> resources) throws DatabaseException {
157
158         if(resources.size() == 1) return getClusterSetForNewResource(graph, resources.iterator().next());
159
160         Resource owner = getNearestOwner(graph, resources);
161         return getClusterSetForNewResource(graph, owner, new HashSet<Resource>());
162         
163     }
164     
165     public static Resource getClusterSetForNewResource(ReadGraph graph, Resource resource, Set<Resource> visited) throws DatabaseException {
166         
167         ClusteringSupport cs = graph.getService(ClusteringSupport.class);
168         if(cs.isClusterSet(resource)) return resource;
169         
170         Resource owner = getPossibleOwner(graph, resource);
171         
172         if(owner == null || owner == resource) return null;
173         if(!visited.add(owner)) return null;
174
175         return getClusterSetForNewResource(graph, owner, visited);
176         
177     }
178
179     public static Resource getClusterSetForNewResource(ReadGraph graph, Resource r) throws DatabaseException {
180         return getClusterSetForNewResource(graph, r, new HashSet<Resource>());
181     }
182
183
184     public static void selectClusterSet(WriteGraph graph, Collection<Resource> resources) throws DatabaseException {
185         Resource clusterSet = getClusterSetForNewResource(graph, resources);
186         if(clusterSet == null) clusterSet = graph.getRootLibrary();
187         graph.setClusterSet4NewResource(clusterSet);
188     }
189
190     public static void selectClusterSet(WriteGraph graph, Resource ... resources) throws DatabaseException {
191         Resource clusterSet = getClusterSetForNewResource(graph, resources);
192         if(clusterSet == null) clusterSet = graph.getRootLibrary();
193         graph.setClusterSet4NewResource(clusterSet);
194     }
195     
196     public static void selectClusterSet(WriteGraph graph, Resource resource) throws DatabaseException {
197         Resource clusterSet = getClusterSetForNewResource(graph, resource);
198         if(clusterSet == null) clusterSet = graph.getRootLibrary();
199         graph.setClusterSet4NewResource(clusterSet);
200     }
201     
202     public static List<Resource> objectsWithType(ReadGraph graph, Resource subject, Resource relation, Resource type) throws DatabaseException {
203         return new ArrayList<Resource>(graph.syncRequest(new ObjectsWithType(subject, relation, type)));
204     }
205
206     public static Resource possibleObjectWithType(ReadGraph graph, Resource subject, Resource relation, Resource type) throws DatabaseException {
207         return graph.syncRequest(new PossibleObjectWithType(subject, relation, type));
208     }
209
210         public static List<ClusterUID> listClusters(ReadGraph graph) throws DatabaseException {
211         XSupport xs = graph.getService(XSupport.class);
212         ClusterUID uids[] = xs.listClusters();
213         ArrayList<ClusterUID> result = new ArrayList<>(uids.length);
214         for(ClusterUID uid : uids) result.add(uid);
215         return result;
216     }
217         
218         public static List<Resource> resourcesByCluster(ReadGraph graph, ClusterUID uid) throws DatabaseException {
219         SerialisationSupport ss = graph.getService(SerialisationSupport.class);
220         ArrayList<Resource> result = new ArrayList<Resource>();
221         // Index 0 is illegal
222                 for(int i=1;i<1<<12;i++) {
223                         try {
224                                 result.add(ss.getResource(uid.toRID(i)));
225                         } catch (InvalidResourceReferenceException e) {
226                         }
227                 }
228                 return result;
229         }
230         
231         public static List<Statement> directStatements(ReadGraph graph, Resource resource, boolean ignoreVirtual) throws DatabaseException {
232                 
233                 DirectQuerySupport dqs = graph.getService(DirectQuerySupport.class);
234                 DirectStatementProcedure proc = new DirectStatementProcedure();
235
236                 if (ignoreVirtual) {
237                         return dqs.getDirectPersistentStatements(graph, resource);
238                 } else {
239                         return dqs.getDirectStatements(graph, resource);
240                 }
241
242         }
243         
244     public static List<Resource> garbageResources(ReadGraph graph) throws DatabaseException {
245         
246         SerialisationSupport ss = graph.getService(SerialisationSupport.class);
247         
248         TIntArrayList refs = new TIntArrayList();
249         TIntArrayList res = new TIntArrayList();
250         
251         // Find all statements in the database
252         for(ClusterUID uid : listClusters(graph)) {
253                 for(Resource r : resourcesByCluster(graph, uid)) {
254                         int sid = ss.getTransientId(r);
255                         for(Statement stm : directStatements(graph, r, true)) {
256                                 int oid = ss.getTransientId(stm.getObject());
257                                 refs.add(sid);
258                                 refs.add(oid);
259                         }
260                         res.add(sid);
261                 }
262         }
263
264         TIntHashSet reached = new TIntHashSet();
265         
266         // Initialize root
267         int root = ss.getTransientId(graph.getRootLibrary());
268         reached.add(root);
269         
270         int[] refArray = refs.toArray();
271         
272         boolean changes = true;
273         
274         while(changes) {
275                 changes = false;
276                 for(int i=0;i<refArray.length;i+=2) {
277                         int s = refArray[i];
278                         int o = refArray[i+1];
279                         if(reached.contains(s)) {
280                                 if(reached.add(o)) {
281                                         changes = true;
282                                 }
283                         }
284                 }
285                 
286                 System.err.println("Reachability iteration, changes = " + changes);
287         }
288         
289         ArrayList<Resource> result = new ArrayList<>();
290                 for(int i=0;i<refArray.length;i+=2) {
291                         int s = refArray[i];
292                         if(reached.contains(s)) {
293                                 if(reached.add(refArray[i+1]))
294                                         changes = true;
295                         }
296                 }
297
298                 res.forEach(new TIntProcedure() {
299                         
300                         @Override
301                         public boolean execute(int r) {
302                                 if(!reached.contains(r)) {
303                                         try {
304                                                 result.add(ss.getResource(r));
305                                         } catch (DatabaseException e) {
306                                                 LOGGER.error("Unexpected error while resolving garbage resources.", e);
307                                         }
308                                 }
309                                 return true;
310                         }
311                         
312                 });
313                 
314         return result;
315         
316     }
317
318     public static ClusterUID clusterUIDOfResource(ReadGraph graph, Resource resource) throws DatabaseException {
319         SerialisationSupport ss = graph.getService(SerialisationSupport.class);
320         return ss.getUID(resource).asCID();
321     }
322     
323     public static boolean isClusterLoaded(ReadGraph graph, ClusterUID clusterUID) throws DatabaseException {
324         XSupport xs = graph.getService(XSupport.class);
325         return xs.isClusterLoaded(clusterUID);
326     }
327     
328     
329 }