]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/CollectionUnaryQuery.java
Multiple reader thread support for db client
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / CollectionUnaryQuery.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.impl.procedure.IntProcedureAdapter;
17
18 public abstract class CollectionUnaryQuery extends UnaryQuery<IntProcedure> implements IntProcedure {
19     
20     public CollectionUnaryQuery(final int id) {
21         super(id);
22     }
23     
24     public abstract void compute(ReadGraphImpl graph, IntProcedure procedure) throws DatabaseException;
25
26     @Override
27     public final void clearResult(QuerySupport support) {
28         setResult(new IntArray());
29     }
30     
31     @Override
32     public final void setReady() {
33         super.setReady();
34         IntArray v = (IntArray)getResult();
35         int size = v.size();
36         if(size == 0) setResult(IntArray.EMPTY);
37         else v.trim();
38     }
39     
40     @Override
41     public final Object performFromCache(ReadGraphImpl graph, final IntProcedure procedure) throws DatabaseException {
42
43         assert(isReady());
44
45         if(handleException(graph, procedure)) return EXCEPTED;
46         
47         final IntArray value = (IntArray)getResult();
48         if(value.data == null) {
49             if(value.sizeOrData != IntArray.NO_DATA) procedure.execute(graph, value.sizeOrData);
50         } else {
51             for(int i = 0;i < value.sizeOrData ; i++) procedure.execute(graph, value.data[i]);
52         }
53
54         procedure.finished(graph);
55         
56         return getResult();
57
58     }
59
60     @Override
61     public final void recompute(ReadGraphImpl graph) throws DatabaseException {
62
63         compute(graph, new IntProcedureAdapter() {
64
65             @Override
66             public void finished(ReadGraphImpl graph) {
67             }
68
69             @Override
70             public void exception(ReadGraphImpl graph, Throwable t) {
71                 new Error("Error in recompute.", t).printStackTrace();
72             }
73
74         });
75
76     }
77     
78     @Override
79     final boolean isImmutable(ReadGraphImpl graph) {
80         return graph.processor.isImmutable(id);
81     }
82
83     @Override
84     public final void execute(ReadGraphImpl graph, int i) throws DatabaseException {
85         IntArray v = (IntArray)getResult();
86         v.add(i);
87     }
88
89     @Override
90     public final void finished(ReadGraphImpl graph) throws DatabaseException {
91         setReady();
92     }
93
94     @Override
95     public final void exception(ReadGraphImpl graph, Throwable throwable) throws DatabaseException {
96         except(throwable);
97     }
98
99 }