1 package com.jofti.tree; 2 3 import java.util.Map ; 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 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 performQuery(int operator, Class className, Object nameSpace,String attribute, Object value, TreeIndex index, Map namedParameters, Object alias) throws JoftiException{ 34 Map temp = null; 35 36 switch(operator){ 37 38 39 case CommonLexerTokenTypes.ASSIGNEQUAL: 40 Comparable comp = attributeValueParser.constructValue((String )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 )value,namedParameters); 46 temp = matchIsProperty(new MatchQuery(className, attribute, comp, alias), index); 47 48 break; 49 case CommonLexerTokenTypes.GREATERTHAN:; 50 comp = attributeValueParser.constructValue((String )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 )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 )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 )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 )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 )value,namedParameters); 74 temp = matchNotProperty(new MatchNotQuery(className, attribute, comp,alias), index); 75 break; 76 case CommonLexerTokenTypes.LIKE: 77 Comparable [] tempVals = attributeValueParser.processVals((String ) 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 matchProperty(MatchQuery query, TreeIndex index) throws JoftiException { 96 return matchProperty(query.className,query.propertyName, query.value,query.alias, index); 97 } 98 99 public Map matchProperty(final Class className, final String attribute, final Comparable comp, final Object alias,TreeIndex index) throws JoftiException { 100 101 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 return new OpenHashMap(1); 118 } catch (JoftiException e) { 119 throw e; 120 } 121 122 124 return BTOperations.match(index.tree,comp, 125 dimension, alias); 126 127 128 129 } 130 131 132 public Map matchIsProperty(MatchQuery query, TreeIndex index) throws JoftiException { 133 134 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 return new OpenHashMap(1); 147 } catch (JoftiException e) { 148 throw e; 149 } 150 151 153 return BTOperations.match(index.tree, wrapNull(null), 154 dimension, query.alias); 155 156 157 158 } 159 160 public Map matchProperty(MatchInQuery query, TreeIndex index) throws JoftiException { 161 162 164 int dimension = 0; 165 Comparable [] 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 return new OpenHashMap(1); 181 } catch (JoftiException e) { 182 throw e; 183 } 184 185 187 return BTOperations.match(index.tree, values, 188 dimension, query.alias); 189 190 191 192 } 193 194 public Map matchNotProperty(MatchNotQuery query, TreeIndex index) throws JoftiException { 195 196 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 return new OpenHashMap(1); 210 } catch (JoftiException e) { 211 throw e; 212 } 213 214 216 return BTOperations.notEqual(index.tree, wrapNull(query.getValue()), 217 dimension, query.alias); 218 219 220 221 } 222 223 229 230 231 public Map 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 matchPropertyRange(Class className, String attribute, Comparable startValue, 238 Comparable endValue,boolean inclusive, Object alias, 239 TreeIndex index) throws JoftiException { 240 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 return new OpenHashMap(1); 275 } catch (JoftiException e) { 276 throw e; 277 } 278 279 281 return BTOperations.range(index.tree, startValue, 282 endValue, dimension, inclusive, alias); 283 284 285 } 286 287 288 } 289 | Popular Tags |