]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/UnaryQuery.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / UnaryQuery.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.db.exception.DatabaseException;
15 import org.simantics.db.impl.graph.ReadGraphImpl;
16 import org.simantics.db.request.RequestFlags;
17
18 import gnu.trove.list.array.TByteArrayList;
19
20 public abstract class UnaryQuery<Procedure> extends CacheEntryBase<Procedure> implements Query {
21
22     final public int id;
23
24     public UnaryQuery(int r) {
25         assert(r != 0);
26         this.id = r;
27     }
28     
29     @Override
30     int makeHash() {
31         return id;
32     }
33     
34     @Override
35     public int type() {
36         return RequestFlags.INVALIDATE;
37     }
38     
39     @Override
40     final public boolean equals(Object object) {
41         if (this == object)
42             return true;
43         else if (object == null)
44             return false;
45         else if (getClass() != object.getClass())
46             return false;
47         UnaryQuery<?> other = (UnaryQuery<?>)object;
48         return id == other.id;
49     }
50     
51     @Override
52     final public Query getQuery() {
53         assert(!isDiscarded());
54         return this;
55     }
56     
57     abstract public void removeEntry(QueryProcessor provider);
58         
59     public Object get(ReadGraphImpl graph, Procedure procedure) throws Throwable {
60         if(procedure != null) {
61             performFromCache(graph, procedure);
62         } else {
63             checkAndThrow();
64         }
65         return getResult();
66     }
67     
68     @Override
69     boolean isImmutable(ReadGraphImpl graph) throws DatabaseException {
70         return graph.processor.isImmutable(id);
71     }
72     
73     @Override
74     public long cluster(QueryProcessor processor) {
75         return processor.cluster(id);
76     }
77     
78     @Override
79     public void serializeKey(QuerySerializer serializer) {
80         serializer.addResource(id);
81     }
82     
83 }