]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/OrderedSet.java
Merge "Multiple reader thread support for db client"
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / OrderedSet.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.impl.query;
13
14 import java.util.concurrent.atomic.AtomicInteger;
15
16 import org.simantics.db.common.exception.DebugException;
17 import org.simantics.db.exception.DatabaseException;
18 import org.simantics.db.impl.graph.ReadGraphImpl;
19
20 final public class OrderedSet extends CollectionUnaryQuery {
21         
22         public OrderedSet(final int r) {
23         super(r);
24     }
25
26         @Override
27         final public void removeEntry(QueryProcessor provider) {
28         provider.cache.remove(this);
29         }
30
31         private static int nextElement(ReadGraphImpl graph, int current, int orderedSet, OrderedSet parent, final IntProcedure procedure) throws DatabaseException {
32             
33                 QueryProcessor processor = graph.processor;
34                 
35                 processor.querySupport.ensureLoaded(graph, current);
36                 
37                 AtomicInteger res = new AtomicInteger(0);
38                 
39                 processor.querySupport.getObjects(graph, current, orderedSet, new IntProcedure() {
40
41                         @Override
42                         public void execute(ReadGraphImpl graph, int i) throws DatabaseException {
43                                 if(i != orderedSet) {
44                                         procedure.execute(graph, i);
45                                 }
46                                 res.set(i);
47                         }
48
49                         @Override
50                         public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
51                                 if(DebugException.DEBUG) new DebugException(t).printStackTrace();
52                                 procedure.exception(graph, t);
53                         }
54
55                         @Override
56                         public void finished(ReadGraphImpl graph) {
57                         }
58
59                 });
60
61                 if(res.get() == orderedSet) {
62                         procedure.finished(graph);
63                 }
64                 
65                 return res.get();
66
67         }
68         
69     @Override
70     public void compute(ReadGraphImpl graph, final IntProcedure procedure) throws DatabaseException {
71         computeForEach(graph, id, this, procedure);
72     }
73
74     static void computeForEach(ReadGraphImpl graph, int orderedSet, final OrderedSet entry, final IntProcedure procedure_) throws DatabaseException {
75         
76         IntProcedure procedure = entry != null ? entry : procedure_;
77
78         int current = nextElement(graph, orderedSet, orderedSet, entry, procedure);
79         while(current != orderedSet) {
80                 current = nextElement(graph, current, orderedSet, entry, procedure);
81         }
82
83         if(entry != null) entry.performFromCache(graph, procedure_);
84         
85     }
86     
87     @Override
88     public String toString() {
89         return "OrderedSet[" + id + "]";
90     }
91     
92 }