]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/genericrelation/DependencyResources.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / genericrelation / DependencyResources.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.layer0.genericrelation;
13
14 import org.simantics.db.ReadGraph;
15 import org.simantics.db.Resource;
16 import org.simantics.db.common.request.UniqueRead;
17 import org.simantics.db.exception.DatabaseException;
18 import org.simantics.db.layer0.adapter.GenericRelationIndex;
19 import org.simantics.db.service.QueryControl;
20 import org.simantics.operation.Layer0X;
21 import org.simantics.scl.runtime.function.FunctionImpl4;
22 import org.simantics.scl.runtime.function.UnsaturatedFunction2;
23 import org.slf4j.LoggerFactory;
24
25 /**
26  * dependencyResources:
27  *      (ReadGraph, Resource model, String query) -> List<Resource>
28  *      (ReadGraph, Resource model, String query, Integer maxResults) -> List<Resource>
29  * 
30  * @author Antti Villberg
31  */
32 public class DependencyResources extends FunctionImpl4<ReadGraph, Resource, String, Integer, Object> {
33
34     private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(DependencyResources.class);
35
36     protected Resource getIndexRelation(ReadGraph graph) {
37         return Layer0X.getInstance(graph).DependenciesRelation;
38     }
39
40     protected String getBindingPattern() {
41         return Dependencies.getBindingPattern();
42     }
43
44     @Override
45     public Object apply(ReadGraph p0, Resource p1) {
46         return new UnsaturatedFunction2(this, p0, p1);
47     }
48
49     @Override
50     public Object apply(ReadGraph p0, Resource p1, String p2) {
51         return apply(p0, p1, p2, Integer.MAX_VALUE);
52     }
53
54     @Override
55     public Object apply(ReadGraph graph, final Resource model, final String query, Integer _maxResults) {
56         
57         try {
58
59             final GenericRelationIndex index = graph.adapt(getIndexRelation(graph), GenericRelationIndex.class);
60
61             // Listen
62             graph.syncRequest(new ExternalRequest(index, model));
63             
64             final int maxResults = _maxResults != null ? _maxResults : Integer.MAX_VALUE;
65             
66             QueryControl qc = graph.getService(QueryControl.class);
67             return qc.syncRequestIndependent(graph, new UniqueRead<Object>() {
68                 @Override
69                 public Object perform(ReadGraph graph) throws DatabaseException {
70                     return index.queryResources(graph, query, getBindingPattern(), new Object[] { model }, maxResults);
71                 }
72             });
73             
74         } catch (DatabaseException e) {
75             LOGGER.error("Error while performing index query", e);
76             return null;
77         }
78     }
79
80 }