KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jofti > tree > TreeMatcherEngine


1 package com.jofti.tree;
2
3 import java.util.Map JavaDoc;
4
5 import org.apache.commons.logging.Log;
6 import org.apache.commons.logging.LogFactory;
7
8 import com.jofti.btree.BTOperations;
9 import com.jofti.exception.JoftiException;
10 import com.jofti.exception.PropertyNotIndexedException;
11 import com.jofti.manager.IndexManagerImpl;
12 import com.jofti.parser.CommonLexerTokenTypes;
13 import com.jofti.query.MatchInQuery;
14 import com.jofti.query.MatchNotQuery;
15 import com.jofti.query.MatchQuery;
16 import com.jofti.query.MatchRangeQuery;
17 import com.jofti.util.OpenHashMap;
18
19
20 /**
21  * @author Steve Woodcock (steve@jofti.com)
22  *
23  */

24 public class TreeMatcherEngine extends AbstractMatchingEngine {
25
26
27     
28     private static Log log = LogFactory.getLog(IndexManagerImpl.class);
29     
30     
31
32     
33     protected Map JavaDoc performQuery(int operator, Class JavaDoc className, Object JavaDoc nameSpace,String JavaDoc attribute, Object JavaDoc value, TreeIndex index, Map JavaDoc namedParameters, Object JavaDoc alias) throws JoftiException{
34         Map JavaDoc temp = null;
35         
36         switch(operator){
37         
38             
39             case CommonLexerTokenTypes.ASSIGNEQUAL:
40                  Comparable JavaDoc comp = attributeValueParser.constructValue((String JavaDoc)value, index.introspector,className,attribute,namedParameters);
41                 temp = matchProperty(className, attribute, comp, alias, index);
42
43                 break;
44              case CommonLexerTokenTypes.IS:
45                 comp = attributeValueParser.constructForIsNot((String JavaDoc)value,namedParameters);
46                 temp = matchIsProperty(new MatchQuery(className, attribute, comp, alias), index);
47                 
48                 break;
49             case CommonLexerTokenTypes.GREATERTHAN:;
50                 comp = attributeValueParser.constructValue((String JavaDoc)value,index.introspector,className,attribute,namedParameters);
51                 temp = matchPropertyRange(className, attribute, comp, null,false,alias, index);
52                 break;
53             case CommonLexerTokenTypes.GREATERTHANOREQUALTO1:;
54             case CommonLexerTokenTypes.GREATERTHANOREQUALTO2:
55                 comp = attributeValueParser.constructValue((String JavaDoc)value, index.introspector,className,attribute,namedParameters);
56                 temp = matchPropertyRange(className, attribute, comp,null,true,alias, index);
57                 break;
58             case CommonLexerTokenTypes.LESSTHANOREQUALTO1:;
59             case CommonLexerTokenTypes.LESSTHANOREQUALTO2:;
60                 comp = attributeValueParser.constructValue((String JavaDoc)value, index.introspector,className,attribute,namedParameters);
61                 temp = matchPropertyRange(className, attribute, null,comp,true,alias, index);
62                 break;
63             case CommonLexerTokenTypes.LESSTHAN:
64                  comp = attributeValueParser.constructValue((String JavaDoc)value, index.introspector,className,attribute,namedParameters);
65                 temp = matchPropertyRange(className, attribute, null,comp,false,alias, index);
66                 break;
67             case CommonLexerTokenTypes.NOTEQUAL1:;
68             case CommonLexerTokenTypes.NOTEQUAL2:
69                 comp = attributeValueParser.constructValue((String JavaDoc)value, index.introspector,className,attribute,namedParameters);
70                 temp = matchNotProperty(new MatchNotQuery(className, attribute, comp,alias), index);
71                 break;
72             case CommonLexerTokenTypes.NOT:
73                 comp = attributeValueParser.constructForIsNot((String JavaDoc)value,namedParameters);
74                 temp = matchNotProperty(new MatchNotQuery(className, attribute, comp,alias), index);
75                 break;
76             case CommonLexerTokenTypes.LIKE:
77                     Comparable JavaDoc[] tempVals = attributeValueParser.processVals((String JavaDoc) value,namedParameters);
78                 temp = matchPropertyRange(className, attribute, tempVals[0],tempVals[1],true,alias, index);
79                 break;
80             case CommonLexerTokenTypes.IN:
81                 tempVals = attributeValueParser.constructArrayValue(value,index.introspector, className, attribute,namedParameters );
82                 temp = matchProperty(new MatchInQuery(className, attribute, tempVals,alias), index);
83                 break;
84            
85             default:
86                     break;
87         }
88         return temp;
89     }
90
91     
92     
93         
94     
95     public Map JavaDoc matchProperty(MatchQuery query, TreeIndex index) throws JoftiException {
96         return matchProperty(query.className,query.propertyName, query.value,query.alias, index);
97     }
98
99         public Map JavaDoc matchProperty(final Class JavaDoc className, final String JavaDoc attribute, final Comparable JavaDoc comp, final Object JavaDoc alias,TreeIndex index) throws JoftiException {
100             
101         // we need to find the dimension from the name here
102

103         int dimension = 0;
104         try {
105             if (comp ==null){
106                 throw new JoftiException("null is not allowed in equals query use IS query");
107             }
108             if (attribute == null) {
109                 
110                 dimension = index.introspector.getDimension(comp.getClass(), null);
111             } else {
112                 dimension = index.introspector.getDimension(className,
113                         attribute);
114             }
115         } catch (PropertyNotIndexedException e) {
116             //no property match found
117
return new OpenHashMap(1);
118         } catch (JoftiException e) {
119             throw e;
120         }
121
122         //log.info("Looking for property " + propertyName + " with index " + dimension);
123

124         return BTOperations.match(index.tree,comp,
125                 dimension, alias);
126         
127         
128
129     }
130     
131     
132     public Map JavaDoc matchIsProperty(MatchQuery query, TreeIndex index) throws JoftiException {
133
134         // we need to find the dimension from the name here
135

136         int dimension = 0;
137         try {
138             if (query.getPropertyName() == null) {
139                 dimension = index.introspector.getDimension(query.getClassName(), null);
140             } else {
141                 dimension = index.introspector.getDimension(query.getClassName(),
142                         query.getPropertyName());
143             }
144         } catch (PropertyNotIndexedException e) {
145             //no property match found
146
return new OpenHashMap(1);
147         } catch (JoftiException e) {
148             throw e;
149         }
150
151         //log.info("Looking for property " + propertyName + " with index " + dimension);
152

153         return BTOperations.match(index.tree, wrapNull(null),
154                 dimension, query.alias);
155         
156         
157
158     }
159     
160     public Map JavaDoc matchProperty(MatchInQuery query, TreeIndex index) throws JoftiException {
161
162         // we need to find the dimension from the name here
163

164         int dimension = 0;
165         Comparable JavaDoc[] values =query.getValues();
166         try {
167             if (values == null || values[0] == null){
168                 return new OpenHashMap(1);
169             }
170             if (query.getPropertyName() == null) {
171                 
172                 dimension = index.introspector.getDimension(values[0]
173                         .getClass(), null);
174             } else {
175                 dimension = index.introspector.getDimension(query.getClassName(),
176                         query.getPropertyName());
177             }
178         } catch (PropertyNotIndexedException e) {
179             //no property match found
180
return new OpenHashMap(1);
181         } catch (JoftiException e) {
182             throw e;
183         }
184
185         //log.info("Looking for property " + propertyName + " with index " + dimension);
186

187         return BTOperations.match(index.tree, values,
188                 dimension, query.alias);
189         
190         
191
192     }
193     
194     public Map JavaDoc matchNotProperty(MatchNotQuery query, TreeIndex index) throws JoftiException {
195
196         // we need to find the dimension from the name here
197

198         int dimension = 0;
199         try {
200             if (query.getPropertyName() == null) {
201                 dimension = index.introspector.getDimension(query.getValue()
202                         .getClass(), null);
203             } else {
204                 dimension = index.introspector.getDimension(query.getClassName(),
205                         query.getPropertyName());
206             }
207         } catch (PropertyNotIndexedException e) {
208             //no property match found
209
return new OpenHashMap(1);
210         } catch (JoftiException e) {
211             throw e;
212         }
213
214         //log.info("Looking for property " + propertyName + " with index " + dimension);
215

216         return BTOperations.notEqual(index.tree, wrapNull(query.getValue()),
217                 dimension, query.alias);
218         
219         
220
221     }
222     
223     /**
224      * @param cache
225      * @param col
226      * @return
227      * @throws JoftiException
228      */

229     
230
231     public Map JavaDoc matchPropertyRange(MatchRangeQuery query,
232             TreeIndex index) throws JoftiException {
233         return matchPropertyRange(query.getClassName(),query.getPropertyName(),query.getStartValue(), query.getEndValue(),
234         query.isInclusive(),query.alias,index);
235         
236     }
237     public Map JavaDoc matchPropertyRange(Class JavaDoc className, String JavaDoc attribute, Comparable JavaDoc startValue,
238             Comparable JavaDoc endValue,boolean inclusive, Object JavaDoc alias,
239                 TreeIndex index) throws JoftiException {
240         // we need to find the dimension from the name here
241
// we need to find the dimension from the name here
242
if (startValue != null && endValue != null) {
243             if (!((startValue.getClass().equals(endValue
244                     .getClass())))) {
245                 throw new JoftiException(
246                         "Classes for property range query must be the same. start:"
247                                 + startValue.getClass() + " end:"
248                                 + endValue.getClass());
249             }
250             if (startValue.compareTo(endValue) > 0) {
251                 if (log.isDebugEnabled()) {
252                     log.info("Range search: start range "
253                             + startValue
254                             + " is greater then end range "
255                             + endValue + " returning empty results");
256                 }
257                 return new OpenHashMap(1);
258             }
259         }
260         int dimension = 0;
261         try {
262
263             if (className == null) {
264                 if (startValue != null) {
265                     className = startValue.getClass();
266                 } else if (endValue != null) {
267                     className = endValue.getClass();
268                 }
269             }
270
271             dimension = index.introspector.getDimension(className,attribute);
272         } catch (PropertyNotIndexedException e) {
273             //no property match found
274
return new OpenHashMap(1);
275         } catch (JoftiException e) {
276             throw e;
277         }
278
279         //log.info("Looking for property " + propertyName + " with index " + dimension);
280

281         return BTOperations.range(index.tree, startValue,
282                 endValue, dimension, inclusive, alias);
283         
284
285     }
286
287     
288 }
289
Popular Tags