1 package com.jofti.tree; 2 3 import java.util.Collection ; 4 import java.util.Iterator ; 5 import java.util.LinkedHashMap ; 6 import java.util.Map ; 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 31 public class NameSpacedTreeMatcherEngine extends AbstractMatchingEngine{ 32 33 private static Log log = LogFactory.getLog(IndexManagerImpl.class); 34 37 38 39 protected Map constructReturnMap(Collection col){ 40 LinkedHashMap map = new LinkedHashMap (col.size()); 41 int size =col.size(); 42 Iterator 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 matchNameSpace(MatchNSQuery query, TreeIndex index) throws JoftiException{ 51 52 int dimension = index.introspector.getKeyDimension(NameSpaceWrapper.class); 54 return BTOperations.match(index.tree, (Comparable )query.getNameSpaceWrapper(), dimension, query.alias); 55 56 57 } 58 59 protected Map matchProperty(MatchNSQuery query, TreeIndex index) throws JoftiException{ 60 61 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 return new OpenHashMap(1); 73 }catch (JoftiException e){ 74 throw e; 75 } 76 77 return BTOperations.match(index.tree, wrapNull(query.getValue()), dimension,query.alias); 79 80 } 81 82 protected Map matchIsProperty(MatchNSQuery query, TreeIndex index) throws JoftiException{ 83 84 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 return new OpenHashMap(1); 97 }catch (JoftiException e){ 98 throw e; 99 } 100 101 return BTOperations.match(index.tree, wrapNull(null), dimension,query.alias); 103 104 105 106 107 } 108 109 110 protected Map matchProperty(MatchNSInQuery query, TreeIndex index) throws JoftiException{ 111 112 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 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 138 139 140 141 } 142 143 144 145 151 152 153 154 155 156 157 protected Map matchPropertyRange(MatchNSRangeQuery query, TreeIndex index) throws JoftiException{ 158 159 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 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 return new OpenHashMap(1); 189 }catch (JoftiException e){ 190 throw e; 191 } 192 193 return BTOperations.range(index.tree, query.getStartValue(),query.getEndValue(), dimension,query.isInclusive() , query.alias); 195 196 197 198 } 199 200 203 protected Map performQuery(int operator, Class className, Object nameSpace,String attribute, Object value, TreeIndex index, Map namedParameters,Object alias) throws JoftiException{ 204 Map temp = null; 205 switch(operator){ 206 207 208 case SQLQueryParserTokenTypes.ASSIGNEQUAL: 209 Comparable comp = attributeValueParser.constructValue((String )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 )value,namedParameters); 214 temp = matchIsProperty(new MatchNSQuery(className, nameSpace,attribute, comp,alias), index); 215 break; 216 case SQLQueryParserTokenTypes.GREATERTHAN:; 217 comp = attributeValueParser.constructValue((String )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 )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 )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 )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 )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 )value,namedParameters); 241 temp = matchNotProperty(new MatchNSNotQuery(className, nameSpace,attribute, comp,alias), index); 242 break; 243 case SQLQueryParserTokenTypes.LIKE: 244 Comparable [] tempVals = attributeValueParser.processVals((String ) 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 matchNotProperty(MatchNSNotQuery query, TreeIndex index) throws JoftiException { 258 259 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 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 |