KickJava   Java API By Example, From Geeks To Geeks.

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


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.ClassDescription;
27 import org.objectweb.jalisto.se.impl.LogicalOid;
28 import org.objectweb.jalisto.se.api.Session;
29 import org.objectweb.jalisto.se.api.MetaRepository;
30 import org.objectweb.jalisto.se.api.query.Index;
31 import org.objectweb.jalisto.se.api.query.IndexManager;
32 import org.objectweb.jalisto.se.query.exception.QueryEngineException;
33 import org.objectweb.jalisto.se.query.NavigationInfo;
34 import org.objectweb.jalisto.se.query.constraint.ValueConstraintImpl;
35 import org.objectweb.jalisto.se.query.result.QueryResultWrapper;
36 import org.objectweb.jalisto.se.query.result.WrapperSet;
37
38 import java.util.Iterator JavaDoc;
39
40 public class ValueExecutionLeaf extends ExecutionElement {
41
42     public ValueExecutionLeaf(ExecutionTree tree, ExecutionElement father,
43                               ValueConstraintImpl valueConstraint, int depth) {
44         super(tree, father, depth);
45         this.valueConstraint = valueConstraint;
46         setToLeaf(0);
47     }
48
49     // not an index => "Or branch" i.e. not resolvable branch
50
public boolean isInOrBranch(IndexManager indexManager) {
51         return (navigationInfos.getIndexIfa() == null);
52     }
53
54     public boolean isResolved() {
55         return (super.isResolved() || valueConstraint.isResolved());
56     }
57
58     public WrapperSet getResolvedValues() {
59         if (resolvedValues.isEmpty() && valueConstraint.isResolved()) {
60             return valueConstraint.getResolvedValues();
61         }
62         return resolvedValues;
63     }
64
65     public void cleanAll() {
66         super.cleanAll();
67         valueConstraint.clean();
68     }
69
70     public void defineMeta(MetaRepository repository) {
71         ClassDescription meta = repository.getMetaData(valueConstraint.getClassName());
72         navigationInfos = new NavigationInfo(meta, valueConstraint.getFieldNames());
73     }
74
75     /**
76      * ***************************** RESOLVE ******************************************
77      * resolve this leaf if field has an index
78      */

79
80     // resolve this leaf if field has an index
81
public void resolveLeafOnIndex(Session session, IndexManager indexManager) {
82         if (!navigationInfos.isIndexDefined()) {
83             return;
84         }
85         if (!valueConstraint.isResolved()) {
86             WrapperSet result = valueConstraint.getResolvedValues(); // get empty collection to fill it
87
Index indexOnValues = navigationInfos.getIndex(indexManager);
88             Iterator JavaDoc keys = indexOnValues.keySet().iterator();
89             while (keys.hasNext()) {
90                 Object JavaDoc value = keys.next();
91                 if (valueConstraint.execute(session, navigationInfos.getComparator(), value)) {
92                     Iterator JavaDoc oids = indexOnValues.get(value).iterator();
93                     while (oids.hasNext()) {
94                         LogicalOid oid = (LogicalOid) oids.next();
95                         result.addToUnread(new QueryResultWrapper(oid, null));
96                     }
97                 }
98             }
99             valueConstraint.setResolved(true);
100         }
101     }
102
103     public boolean resolveOnElement(Session session, QueryResultWrapper wrapper) {
104         Object JavaDoc value = wrapper.getValue()[navigationInfos.getIndex()];
105         return valueConstraint.execute(session, navigationInfos.getComparator(), value);
106     }
107
108     // not on a resolved leaf
109
public WrapperSet resolveWithCandidates(Session session, WrapperSet candidates) {
110         if (valueConstraint.isResolved()) {
111             throw new QueryEngineException("resolveWithCandidates on a resolved leaf");
112         }
113         candidates.readAll(session, tree.getReadedValues());
114         Iterator JavaDoc wrappers = candidates.iterator();
115         while (wrappers.hasNext()) {
116             QueryResultWrapper wrapper = (QueryResultWrapper) wrappers.next();
117             if (resolveOnElement(session, wrapper)) {
118                 resolvedValues.addToRead(wrapper);
119             }
120         }
121         return resolvedValues;
122     }
123
124     public String JavaDoc toString() {
125         return "Eleaf:" + valueConstraint.toString();
126     }
127
128     private ValueConstraintImpl valueConstraint;
129     private NavigationInfo navigationInfos;
130 }
131
Popular Tags