1 5 6 package com.jofti.tree; 7 8 import java.util.Collection ; 9 import java.util.HashMap ; 10 import java.util.Iterator ; 11 import java.util.Map ; 12 import java.util.Properties ; 13 14 import org.apache.commons.logging.Log; 15 import org.apache.commons.logging.LogFactory; 16 17 import com.jofti.api.IndexQuery; 18 import com.jofti.btree.BTOperations; 19 import com.jofti.btree.BTree; 20 import com.jofti.btree.ValueObject; 21 import com.jofti.core.IParsedQuery; 22 import com.jofti.core.InternalIndex; 23 import com.jofti.core.QueryId; 24 import com.jofti.exception.JoftiException; 25 import com.jofti.introspect.ClassIntrospector; 26 import com.jofti.manager.IndexManagerImpl; 27 import com.jofti.parser.ConvenienceQueryWrapper; 28 import com.jofti.parser.ParserManager; 29 import com.jofti.query.MatchInQuery; 30 import com.jofti.query.MatchNotQuery; 31 import com.jofti.query.MatchQuery; 32 import com.jofti.query.MatchRangeQuery; 33 34 import edu.emory.mathcs.backport.java.util.concurrent.ConcurrentHashMap; 35 import edu.emory.mathcs.backport.java.util.concurrent.LinkedBlockingQueue; 36 import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicLong; 37 45 public class TreeIndex implements InternalIndex 46 { 47 public final String KEY_DIMENSION = "com.activescript.KEY_DIMENSION"; 48 49 protected static int DEFAULT_OFFERTIME =1000; 50 51 protected long REMOVAL_WATCHER_SLEEP_TIME =3000; 52 53 private static Log log = LogFactory.getLog(IndexManagerImpl.class); 54 55 56 protected TreeOperationAdapter treeAdapter = null; 57 59 60 BTree tree = null; 61 ClassIntrospector introspector = null; 62 63 Map indexedDimensions = new ConcurrentHashMap(); 64 65 protected AtomicLong keyNumber =new AtomicLong(0); 66 67 LinkedBlockingQueue removalCandidateQueue = new LinkedBlockingQueue(20000); 68 69 70 MatchingEngine engine=null; 71 72 ParserManager parserManager = null; 73 74 public TreeIndex(){ 75 76 } 77 78 79 public TreeIndex(BTree tree, ClassIntrospector parser) 80 { 81 82 this.tree = tree; 83 this.introspector = parser; 84 85 86 87 } 88 89 public TreeIndex(BTree tree, ClassIntrospector parser, Map indexedDimensions) 90 { 91 92 this.tree = tree; 93 this.introspector = parser; 94 this.indexedDimensions = indexedDimensions; 95 96 97 98 } 99 100 104 public void init(Properties props) throws JoftiException{ 105 tree.init(props); 106 engine = new TreeMatcherEngine(); 107 parserManager = new ParserManager(introspector); 108 109 this.treeAdapter = new TreeOperationAdapter(); 110 111 } 112 113 public Map query(IndexQuery query) throws JoftiException{ 114 TreeMatcherEngine tempEngine = (TreeMatcherEngine)engine; 116 if (query == null) { 117 throw new JoftiException("Query object cannot be null"); 118 } 119 120 QueryId queryId = (QueryId)query; 121 122 switch(queryId.getQueryType().type){ 123 case 100 : 124 return tempEngine.matchProperty((MatchQuery) ((ConvenienceQueryWrapper)query).getQuery(), this); 125 case 101: 126 return tempEngine.matchPropertyRange((MatchRangeQuery) ((ConvenienceQueryWrapper)query).getQuery(), this); 127 case 102: 128 return tempEngine.matchNotProperty((MatchNotQuery) ((ConvenienceQueryWrapper)query).getQuery(), this); 129 case 0: 130 return tempEngine.processQuery((IParsedQuery)query, this); 131 case 103: 132 return tempEngine.matchProperty((MatchInQuery)((ConvenienceQueryWrapper)query).getQuery(), this); 133 default: 134 throw new JoftiException("Query type " + ((ConvenienceQueryWrapper)query).getQuery().getClass() 135 + " cannot be used for Non nameSpaced IndexCache"); 136 } 137 138 } 139 140 143 public boolean contains(Object key)throws JoftiException{ 144 try { 145 return treeAdapter.contains( (Comparable )key, tree,introspector); 146 } catch (Throwable e) { 147 if (!(e instanceof JoftiException)) { 148 while(e.getCause() != null){ 149 System.err.println(e); 150 e = e.getCause(); 151 } 152 throw new JoftiException(e); 153 }else{ 154 while(e.getCause() != null){ 155 System.err.println(e); 156 e = e.getCause(); 157 } 158 throw (JoftiException)e; 159 } 160 } 161 } 162 163 164 167 public void insert (Object key, Object value) throws IllegalArgumentException , JoftiException{ 168 try{ 169 170 treeAdapter.insert((Comparable )key, value,tree, introspector); 171 keyNumber.incrementAndGet(); 172 } catch (Throwable e) { 173 if (!(e instanceof JoftiException)) { 174 System.err.println("error at " + keyNumber); 175 while(e.getCause() != null){ 176 System.err.println(e); 177 e = e.getCause(); 178 } 179 throw new JoftiException(e); 180 }else{ 181 while(e.getCause() != null){ 182 System.err.println(e); 183 e = e.getCause(); 184 } 185 throw (JoftiException)e; 186 } 187 } 188 } 189 190 191 public void insertEntry (Object key, Object value) throws IllegalArgumentException , JoftiException{ 192 try{ 193 194 treeAdapter.insertEntry((Comparable )key, value,tree, introspector); 195 keyNumber.incrementAndGet(); 196 } catch (Throwable e) { 197 if (!(e instanceof JoftiException)) { 198 throw new JoftiException(e); 199 }else{ 200 throw (JoftiException)e; 201 } 202 } 203 } 204 205 206 209 public void remove(Object key, Object value) throws IllegalArgumentException , JoftiException{ 210 try { 211 treeAdapter.remove((Comparable )key, value,tree, introspector); 212 keyNumber.decrementAndGet(); 213 } catch (Throwable e) { 214 if (!(e instanceof JoftiException)) { 215 throw new JoftiException(e); 216 }else{ 217 throw (JoftiException)e; 218 } 219 220 } 221 } 222 223 226 public void removeByKey(Object key) throws IllegalArgumentException , JoftiException{ 227 try { 228 treeAdapter.removeByKey((Comparable )key, tree, introspector); 229 keyNumber.decrementAndGet(); 230 } catch (Throwable e) { 231 if (!(e instanceof JoftiException)) { 232 throw new JoftiException(e); 233 }else{ 234 throw (JoftiException)e; 235 } 236 237 } 238 } 239 240 241 242 243 public String toString() { 244 StringBuffer buf = new StringBuffer (); 245 buf.append(" tree:" + tree); 246 return buf.toString(); 247 } 248 249 250 253 public Map getEntries(Object key) throws JoftiException { 254 try{ 255 return treeAdapter.getAllValuesForKey((Comparable )key,tree,introspector); 256 } catch (Throwable e) { 257 if (!(e instanceof JoftiException)) { 258 throw new JoftiException(e); 259 }else{ 260 throw (JoftiException)e; 261 } 262 263 } 264 } 265 266 267 public Map getAllEntries() throws JoftiException { 268 try{ 269 return treeAdapter.getAllValuesForTree(tree, introspector); 270 271 } catch (Throwable e) { 272 if (!(e instanceof JoftiException)) { 273 throw new JoftiException(e); 274 }else{ 275 throw (JoftiException)e; 276 } 277 278 } 279 } 280 281 284 public Map getAttributesByKey(Object key) throws JoftiException { 285 try{ 286 Collection col = treeAdapter.getAttribsByKey((Comparable )key,tree,introspector); 287 if(col ==null || col.size()==0){ 288 return new HashMap (2); 289 } 290 Map temp = new HashMap (col.size()); 291 int size =col.size(); 292 Iterator it = col.iterator(); 293 for (int i=0;i<size;i++){ 294 ValueObject obj = (ValueObject)it.next(); 295 temp.put(new Integer (obj.getDimension()),obj.getRealValue()); 296 } 297 return temp; 298 } catch (Throwable e) { 299 if (!(e instanceof JoftiException)) { 300 throw new JoftiException(e); 301 }else{ 302 throw (JoftiException)e; 303 } 304 305 } 306 } 307 308 309 310 311 314 public void removeAll() 315 { 316 tree.removeAll(); 317 keyNumber =new AtomicLong(0); 318 } 319 320 323 BTree getTree() { 324 return tree; 325 } 326 329 public void setTree(BTree tree) { 330 this.tree = tree; 331 } 332 333 336 public ClassIntrospector getIntrospector() { 337 return introspector; 338 } 339 342 public void setParser(ClassIntrospector parser) { 343 this.introspector = parser; 344 } 345 346 347 348 349 350 351 354 public long getKeyNumber() { 355 return keyNumber.get(); 356 } 357 358 359 public synchronized ParserManager getParserManager() { 360 return parserManager; 361 } 362 363 364 365 } 366 | Popular Tags |