]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/UnaryQueryP.java
Merge "Multiple reader thread support for db client"
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / UnaryQueryP.java
1 /*******************************************************************************
2  * Copyright (c) 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  *     Semantum Oy - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.db.impl.query;
13
14 import org.simantics.db.common.exception.DebugException;
15 import org.simantics.db.exception.DatabaseException;
16 import org.simantics.db.impl.graph.ReadGraphImpl;
17 import org.simantics.db.impl.procedure.InternalProcedure;
18
19 public abstract class UnaryQueryP<R> extends UnaryQuery<InternalProcedure<R>> implements InternalProcedure<R> {
20
21     public UnaryQueryP(int r) {
22         super(r);
23     }
24
25     public abstract void compute(ReadGraphImpl graph, InternalProcedure<R> procedure) throws DatabaseException;
26
27     @Override
28     public final void recompute(ReadGraphImpl graph) throws DatabaseException {
29
30         compute(graph, new InternalProcedure<R>() {
31
32             @Override
33             public void execute(ReadGraphImpl graph, R result) {
34             }
35
36             @Override
37             public void exception(ReadGraphImpl graph, Throwable t) {
38                 if (DebugException.DEBUG)
39                     new DebugException(t).printStackTrace();
40                 throw new Error("Error in recompute.", t);
41             }
42
43         });
44
45     }
46
47     @Override
48     public final Object performFromCache(ReadGraphImpl graph, InternalProcedure<R> procedure)
49             throws DatabaseException {
50
51         if (handleException(graph, procedure))
52             return (Throwable) statusOrException;
53
54         procedure.execute(graph, (R)getResult());
55
56         return result;
57
58     }
59
60     @Override
61     public final void execute(ReadGraphImpl graph, R result) throws DatabaseException {
62         setResult(result);
63         setReady();
64     }
65
66     @Override
67     public final void exception(ReadGraphImpl graph, Throwable throwable) throws DatabaseException {
68         except(throwable);
69     }
70
71     @Override
72     final boolean isImmutable(ReadGraphImpl graph) {
73         return graph.processor.isImmutable(id);
74     }
75
76 }