KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jalisto > se > query > execution > ExecutionElement


1 /*
2  * Jalisto - JAva LIght STOrage
3  * Copyright (C) 2000-2005 Xcalia http://www.xcalia.com
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
18  *
19  * Xcalia
20  * 71, rue Desnouettes
21  * 75014 Paris - France
22  * http://www.xcalia.com
23  */

24 package org.objectweb.jalisto.se.query.execution;
25
26 import org.objectweb.jalisto.se.api.MetaRepository;
27 import org.objectweb.jalisto.se.api.Session;
28 import org.objectweb.jalisto.se.api.query.IndexManager;
29 import org.objectweb.jalisto.se.query.result.QueryResultWrapper;
30 import org.objectweb.jalisto.se.query.result.WrapperSet;
31
32 import java.util.Iterator JavaDoc;
33 import java.util.List JavaDoc;
34 import java.util.Set JavaDoc;
35
36 public class ExecutionElement implements Comparable JavaDoc {
37
38     public ExecutionElement(ExecutionTree tree, ExecutionElement father, int depth) {
39         this.tree = tree;
40         this.resolvedValues = new WrapperSet();
41         this.isResolved = false;
42         this.father = (ExecutionNode) father;
43         this.depth = depth;
44         this.toLeaf = -1;
45     }
46
47     public void setToLeaf(int toLeaf) {
48         if (this.toLeaf < toLeaf) {
49             this.toLeaf = toLeaf;
50             if (father != null) {
51                 father.setToLeaf(toLeaf + 1);
52             }
53         }
54     }
55
56     public boolean onlyOneToResolve() {
57         return false;
58     }
59
60     public boolean isAndNode() {
61         return false;
62     }
63
64     public boolean isInOrBranch(IndexManager indexManager) {
65         return true;
66     }
67
68     public ExecutionNode getFather() {
69         return father;
70     }
71
72     public boolean isResolved() {
73         return isResolved;
74     }
75
76     public void setResolved(boolean resolved) {
77         isResolved = resolved;
78     }
79
80     public WrapperSet getResolvedValues() {
81         return resolvedValues;
82     }
83
84     public int getToLeaf() {
85         return toLeaf;
86     }
87
88     public int getDepth() {
89         return depth;
90     }
91
92     // do not clean resolved values in the constraints
93
public void cleanSubExecutionTree() {
94         resolvedValues.clear();
95         isResolved = false;
96     }
97
98     public void cleanAll() {
99         resolvedValues.clear();
100         isResolved = false;
101     }
102
103     public void defineMeta(MetaRepository repository) {
104     }
105
106     public int compareTo(Object JavaDoc o) {
107         ExecutionElement candidate = (ExecutionElement) o;
108         if (this.toLeaf < candidate.toLeaf) {
109             return -1;
110         } else if (candidate.toLeaf == this.toLeaf) {
111             return 0;
112         } else {
113             return 1;
114         }
115     }
116
117     /**
118      * ****************************** RESOLVE *******************************************
119      * used to propagate back results of resolved nodes, without reading objects
120      * return the father of the node if this node can be resolved, else return null
121      */

122
123     // used to propagate back results of resolved nodes, without reading objects
124
// return the father of the node if this node can be resolved, else return null
125
public ExecutionNode resolveBack() {
126         if (isResolved()) {
127             return father;
128         }
129         return null;
130     }
131
132     // used to resolve first nearest from root and nodes
133
public void resolveBeforeAndNodeNearRoot(Session session, WrapperSet candidates, List JavaDoc toResolve) {
134         resolveWithCandidates(session, candidates);
135         toResolve.remove(this);
136     }
137
138     // used to resolve node, with a set of resolved value
139
public WrapperSet resolveWithCandidates(Session session, WrapperSet candidates) {
140         return new WrapperSet();
141     }
142
143     // resolve on a specific readed element
144
public boolean resolveOnElement(Session session, QueryResultWrapper wrapper) {
145         return false;
146     }
147
148     // resolve this leaf if field has an index
149
public void resolveLeafOnIndex(Session session, IndexManager indexManager) {
150     }
151
152     /**
153      * ************************** UTIL **************************************************
154      * s1 most important
155      */

156
157     // s1 most important
158
public WrapperSet doIntersection(WrapperSet in, WrapperSet s1, WrapperSet s2) {
159         if (s1.isEmpty()) {
160             in.addAll(s2);
161             return in;
162         }
163         if (s2.isEmpty()) {
164             in.addAll(s1);
165             return in;
166         }
167         Iterator JavaDoc i = s1.iterator();
168         while (i.hasNext()) {
169             QueryResultWrapper w = (QueryResultWrapper) i.next();
170             QueryResultWrapper w2 = s2.get(w.getLogicalOid());
171             if (w2 != null) {
172                 if (w.isReaded()) {
173                     in.addToRead(w);
174                 } else {
175                     in.add(w2);
176                 }
177             }
178         }
179         return in;
180     }
181
182     // intersection done in s1
183
public WrapperSet doIntersection(WrapperSet s1, WrapperSet s2) {
184         if (s1.isEmpty()) {
185             s1.addAll(s2);
186             return s1;
187         }
188         if (s2.isEmpty()) {
189             return s1;
190         }
191         Iterator JavaDoc i = s1.iterator();
192         while (i.hasNext()) {
193             QueryResultWrapper w = (QueryResultWrapper) i.next();
194             QueryResultWrapper w2 = s2.get(w.getLogicalOid());
195             if (w2 != null) {
196                 if ((!w.isReaded()) && w2.isReaded()) {
197                     s1.remove(w);
198                     s1.addToRead(w2);
199                 }
200             }
201         }
202         return s1;
203     }
204
205     public Set readValues(Session session, Set set) {
206         Iterator JavaDoc wrappers = set.iterator();
207         session.currentTransaction().begin();
208         while (wrappers.hasNext()) {
209             QueryResultWrapper wrapper = (QueryResultWrapper) wrappers.next();
210             if (wrapper.getValue() == null) {
211                 wrapper.setValue(session.readObjectByOid(wrapper.getLogicalOid()));
212             }
213         }
214         session.currentTransaction().commit();
215         return set;
216     }
217
218
219     private ExecutionNode father;
220     private int depth;
221     private int toLeaf;
222     protected WrapperSet resolvedValues;
223     protected boolean isResolved;
224     protected ExecutionTree tree;
225 }
226
Popular Tags