KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.jofti.tree;
2
3 import java.util.Collection JavaDoc;
4 import java.util.Iterator JavaDoc;
5 import java.util.LinkedHashMap JavaDoc;
6 import java.util.Map JavaDoc;
7
8 import org.apache.commons.logging.Log;
9 import org.apache.commons.logging.LogFactory;
10
11 import com.jofti.btree.BTOperations;
12 import com.jofti.cache.adapter.NameSpaceWrapper;
13 import com.jofti.exception.JoftiException;
14 import com.jofti.exception.PropertyNotIndexedException;
15 import com.jofti.manager.IndexManagerImpl;
16 import com.jofti.parser.CommonLexerTokenTypes;
17 import com.jofti.parser.sql.SQLQueryParserTokenTypes;
18 import com.jofti.query.namespace.MatchNSInQuery;
19 import com.jofti.query.namespace.MatchNSLargerQuery;
20 import com.jofti.query.namespace.MatchNSNotQuery;
21 import com.jofti.query.namespace.MatchNSQuery;
22 import com.jofti.query.namespace.MatchNSRangeQuery;
23 import com.jofti.query.namespace.MatchNSSmallerQuery;
24 import com.jofti.util.OpenHashMap;
25
26 /**
27  * @author Steve Woodcock (steve@jofti.com)
28  *
29
30  */

31 public class NameSpacedTreeMatcherEngine extends AbstractMatchingEngine{
32
33     private static Log log = LogFactory.getLog(IndexManagerImpl.class);
34     /* (non-Javadoc)
35      * @see com.jofti.engine.IQueryEngine#query(com.jofti.api.IndexQuery)
36      */

37     
38
39     protected Map JavaDoc constructReturnMap(Collection JavaDoc col){
40         LinkedHashMap JavaDoc map = new LinkedHashMap JavaDoc(col.size());
41         int size =col.size();
42         Iterator JavaDoc it = col.iterator();
43         for(int i=0;i<size;i++){
44
45             map.put(it.next(),null);
46         }
47         return map;
48     }
49     
50     protected Map JavaDoc matchNameSpace(MatchNSQuery query, TreeIndex index) throws JoftiException{
51         
52         // we need to find the dimension from the name here
53
int dimension = index.introspector.getKeyDimension(NameSpaceWrapper.class);
54        return BTOperations.match(index.tree, (Comparable JavaDoc)query.getNameSpaceWrapper(), dimension, query.alias);
55                 
56      
57         }
58     
59     protected Map JavaDoc matchProperty(MatchNSQuery query, TreeIndex index) throws JoftiException{
60         
61                     // we need to find the dimension from the name here
62

63                     int dimension =0;
64                         try {
65                             if (query.getPropertyName() == null) {
66                                 dimension = index.introspector.getDimension(query.getValue().getClass().getName(), null);
67                             } else {
68                                 dimension = index.introspector.getDimension(query.getClassName(),query.getPropertyName());
69                             }
70                         } catch (PropertyNotIndexedException e){
71                             //no property match found
72
return new OpenHashMap(1);
73                         }catch (JoftiException e){
74                             throw e;
75                         }
76
77                     //log.info("Looking for property " + propertyName + " with index " + dimension);
78
return BTOperations.match(index.tree, wrapNull(query.getValue()), dimension,query.alias);
79                   
80         }
81     
82     protected Map JavaDoc matchIsProperty(MatchNSQuery query, TreeIndex index) throws JoftiException{
83     
84                 // we need to find the dimension from the name here
85

86                 int dimension =0;
87                     try {
88                         if (query.getPropertyName() == null) {
89                             
90                                 dimension = index.introspector.getDimension(query.getClassName(), null);
91                         } else {
92                             dimension = index.introspector.getDimension(query.getClassName(),query.getPropertyName());
93                         }
94                     } catch (PropertyNotIndexedException e){
95                         //no property match found
96
return new OpenHashMap(1);
97                     }catch (JoftiException e){
98                         throw e;
99                     }
100
101                 //log.info("Looking for property " + propertyName + " with index " + dimension);
102
return BTOperations.match(index.tree, wrapNull(null), dimension,query.alias);
103                
104
105             
106  
107     }
108     
109     
110     protected Map JavaDoc matchProperty(MatchNSInQuery query, TreeIndex index) throws JoftiException{
111         
112                     // we need to find the dimension from the name here
113

114          int dimension = 0;
115             try {
116                 if (query.getValues() == null || query.getValues()[0] == null){
117                     return new OpenHashMap(1);
118                 }
119                 if (query.getPropertyName() == null) {
120                     
121                     dimension = index.introspector.getDimension(query.getValues()[0]
122                             .getClass(), null);
123                 } else {
124                     dimension = index.introspector.getDimension(query.getClassName(),
125                             query.getPropertyName());
126                 }
127             } catch (PropertyNotIndexedException e) {
128                 //no property match found
129
return new OpenHashMap(1);
130             } catch (JoftiException e) {
131                 throw e;
132             }
133             
134             return BTOperations.match(index.tree, query.getValues(),
135                     dimension, query.alias);
136                     //log.info("Looking for property " + propertyName + " with index " + dimension);
137

138
139                 
140      
141         }
142         
143         
144     
145     /**
146      * @param cache
147      * @param col
148      * @return
149      * @throws JoftiException
150      */

151
152     
153     
154     
155     
156      
157      protected Map JavaDoc matchPropertyRange(MatchNSRangeQuery query, TreeIndex index) throws JoftiException{
158         
159                     // we need to find the dimension from the name here
160
// we need to find the dimension from the name here
161
if (query.getStartValue() != null && query.getEndValue() != null){
162             if (!((query.getStartValue().getClass().equals(query.getEndValue().getClass()))) ){
163                 throw new JoftiException("Classes for property range query must be the same. start:"+query.getStartValue().getClass() + " end:"+query.getEndValue().getClass());
164             }
165             if (query.getStartValue().compareTo(query.getEndValue()) >0){
166                 if(log.isDebugEnabled()){
167                     log.info("Range search: start range " +query.getStartValue() + " is greater then end range "+ query.getEndValue() + " returning empty results" );
168                 }
169                 return new OpenHashMap(1);
170             }
171         }
172                     int dimension =0;
173                     Class JavaDoc className = null;
174                     try {
175
176                         if (query.getClassName() == null) {
177                             if (query.getStartValue() != null) {
178                                 className = query.getStartValue().getClass();
179                             } else if (query.getEndValue() != null) {
180                                 className = query.getEndValue().getClass();
181                             }
182                         } else {
183                             className = query.getClassName();
184                         }
185                             dimension = index.introspector.getDimension(className, query.getPropertyName());
186                         } catch (PropertyNotIndexedException e){
187                             //no property match found
188
return new OpenHashMap(1);
189                         }catch (JoftiException e){
190                             throw e;
191                         }
192                     
193                         //we need to change this to do the checks on nameSpace
194
return BTOperations.range(index.tree, query.getStartValue(),query.getEndValue(), dimension,query.isInclusive() , query.alias);
195                  
196                 
197      
198         }
199
200     /* (non-Javadoc)
201      * @see com.jofti.tree.AbstractMatchingEngine#performQuery(int, java.lang.String, java.lang.String, java.lang.Comparable, com.jofti.tree.TreeIndex)
202      */

203         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{
204             Map JavaDoc temp = null;
205             switch(operator){
206             
207             
208                 case SQLQueryParserTokenTypes.ASSIGNEQUAL:
209                      Comparable JavaDoc comp = attributeValueParser.constructValue((String JavaDoc)value, index.introspector,className,attribute, namedParameters);
210                     temp = matchProperty(new MatchNSQuery(className, nameSpace,attribute, comp,alias), index);
211                     break;
212                  case SQLQueryParserTokenTypes.IS:
213                     comp = attributeValueParser.constructForIsNot((String JavaDoc)value,namedParameters);
214                     temp = matchIsProperty(new MatchNSQuery(className, nameSpace,attribute, comp,alias), index);
215                     break;
216                 case SQLQueryParserTokenTypes.GREATERTHAN:;
217                      comp = attributeValueParser.constructValue((String JavaDoc)value, index.introspector,className,attribute,namedParameters);
218                     temp = matchPropertyRange(new MatchNSLargerQuery(className,nameSpace, attribute, comp, false,alias), index);
219                     break;
220                 case SQLQueryParserTokenTypes.GREATERTHANOREQUALTO1:;
221                 case SQLQueryParserTokenTypes.GREATERTHANOREQUALTO2:
222                     comp = attributeValueParser.constructValue((String JavaDoc)value, index.introspector,className,attribute,namedParameters);
223                     temp = matchPropertyRange(new MatchNSLargerQuery(className, nameSpace,attribute, comp,alias), index);
224                     break;
225                 case SQLQueryParserTokenTypes.LESSTHANOREQUALTO1:;
226                 case SQLQueryParserTokenTypes.LESSTHANOREQUALTO2:;
227                 comp = attributeValueParser.constructValue((String JavaDoc)value, index.introspector,className,attribute,namedParameters);
228                     temp = matchPropertyRange(new MatchNSSmallerQuery(className,nameSpace, attribute, comp,alias), index);
229                     break;
230                 case SQLQueryParserTokenTypes.LESSTHAN:
231                     comp = attributeValueParser.constructValue((String JavaDoc)value, index.introspector,className,attribute,namedParameters);
232                     temp = matchPropertyRange(new MatchNSSmallerQuery(className, nameSpace,attribute, comp,false,alias), index);
233                     break;
234                 case SQLQueryParserTokenTypes.NOTEQUAL1:;
235                 case SQLQueryParserTokenTypes.NOTEQUAL2:
236                     comp = attributeValueParser.constructValue((String JavaDoc)value, index.introspector,className,attribute,namedParameters);
237                     temp = matchNotProperty(new MatchNSNotQuery(className, nameSpace,attribute, comp,alias), index);
238                     break;
239                 case SQLQueryParserTokenTypes.NOT:
240                     comp = attributeValueParser.constructForIsNot((String JavaDoc)value,namedParameters);
241                     temp = matchNotProperty(new MatchNSNotQuery(className, nameSpace,attribute, comp,alias), index);
242                     break;
243                 case SQLQueryParserTokenTypes.LIKE:
244                     Comparable JavaDoc[] tempVals = attributeValueParser.processVals((String JavaDoc) value,namedParameters);
245                     temp = matchPropertyRange(new MatchNSRangeQuery(className, nameSpace, attribute, tempVals[0],tempVals[1],alias), index);
246                     break;
247                 case CommonLexerTokenTypes.IN:
248                     tempVals = attributeValueParser.constructArrayValue(value,index.introspector, className, attribute,namedParameters );
249                     temp = matchProperty(new MatchNSInQuery(className, nameSpace,attribute, tempVals,alias), index);
250                     break;
251                 default:
252                         break;
253             }
254             return temp;
255         }
256
257         protected Map JavaDoc matchNotProperty(MatchNSNotQuery query, TreeIndex index) throws JoftiException {
258
259             // we need to find the dimension from the name here
260

261             int dimension = 0;
262             try {
263                 if (query.getPropertyName() == null) {
264                     dimension = index.introspector.getDimension(query.getClassName(), null);
265                 } else {
266                     dimension = index.introspector.getDimension(query.getClassName(),
267                             query.getPropertyName());
268                 }
269             } catch (PropertyNotIndexedException e) {
270                 //no property match found
271
return new OpenHashMap(1);
272             } catch (JoftiException e) {
273                 throw e;
274             }
275
276             return BTOperations.notEqual(index.tree, wrapNull(query.getValue()),
277                     dimension, query.alias);
278             
279             
280
281         }
282
283 }
284
Popular Tags