KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  *
3  * Created on 01 June 2003, 08:32
4  */

5
6 package com.jofti.tree;
7
8 import java.util.Collection JavaDoc;
9 import java.util.HashMap JavaDoc;
10 import java.util.Iterator JavaDoc;
11 import java.util.Map JavaDoc;
12 import java.util.Properties JavaDoc;
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 /**
38  *
39  *
40  * The wrapper for the IndexCache that integrates the BTree implementation, the introspector
41  * and the query engine.
42  *
43  * @author xenephon (xenephon@jofti.com)
44  */

45 public class TreeIndex implements InternalIndex
46 {
47     public final String JavaDoc 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     /** Creates a new instance
58      of BTreeWrapper */

59     
60     BTree tree = null;
61     ClassIntrospector introspector = null;
62     
63     Map JavaDoc 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 JavaDoc indexedDimensions)
90     {
91         
92         this.tree = tree;
93         this.introspector = parser;
94         this.indexedDimensions = indexedDimensions;
95
96        
97         
98     }
99     
100     /**
101      * @param props
102      * @throws JoftiException
103      */

104     public void init(Properties JavaDoc 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 JavaDoc query(IndexQuery query) throws JoftiException{
114         //return engine.query(query, this);
115
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      /* (non-Javadoc)
141      * @see com.jofti.core.InternalIndex#contains(java.lang.Object)
142      */

143     public boolean contains(Object JavaDoc key)throws JoftiException{
144          try {
145              return treeAdapter.contains( (Comparable JavaDoc)key, tree,introspector);
146          } catch (Throwable JavaDoc 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      /* (non-Javadoc)
165      * @see com.jofti.core.InternalIndex#insert(java.lang.Object, java.lang.Object)
166      */

167     public void insert (Object JavaDoc key, Object JavaDoc value) throws IllegalArgumentException JavaDoc, JoftiException{
168         try{
169          
170              treeAdapter.insert((Comparable JavaDoc)key, value,tree, introspector);
171             keyNumber.incrementAndGet();
172          } catch (Throwable JavaDoc 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 JavaDoc key, Object JavaDoc value) throws IllegalArgumentException JavaDoc, JoftiException{
192         try{
193          
194              treeAdapter.insertEntry((Comparable JavaDoc)key, value,tree, introspector);
195             keyNumber.incrementAndGet();
196          } catch (Throwable JavaDoc e) {
197              if (!(e instanceof JoftiException)) {
198                  throw new JoftiException(e);
199              }else{
200                  throw (JoftiException)e;
201              }
202          }
203      }
204     
205     
206     /* (non-Javadoc)
207      * @see com.jofti.core.InternalIndex#remove(java.lang.Object, java.lang.Object)
208      */

209     public void remove(Object JavaDoc key, Object JavaDoc value) throws IllegalArgumentException JavaDoc, JoftiException{
210         try {
211                 treeAdapter.remove((Comparable JavaDoc)key, value,tree, introspector);
212             keyNumber.decrementAndGet();
213         } catch (Throwable JavaDoc e) {
214             if (!(e instanceof JoftiException)) {
215                 throw new JoftiException(e);
216             }else{
217                 throw (JoftiException)e;
218             }
219             
220         }
221     }
222     
223  /* (non-Javadoc)
224  * @see com.jofti.core.InternalIndex#removeByKey(java.lang.Object)
225  */

226 public void removeByKey(Object JavaDoc key) throws IllegalArgumentException JavaDoc, JoftiException{
227         try {
228                 treeAdapter.removeByKey((Comparable JavaDoc)key, tree, introspector);
229                 keyNumber.decrementAndGet();
230          } catch (Throwable JavaDoc 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 JavaDoc toString() {
244         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
245         buf.append(" tree:" + tree);
246         return buf.toString();
247     }
248     
249       
250     /* (non-Javadoc)
251      * @see com.jofti.core.InternalIndex#getEntries(java.lang.Comparable)
252      */

253     public Map JavaDoc getEntries(Object JavaDoc key) throws JoftiException {
254         try{
255             return treeAdapter.getAllValuesForKey((Comparable JavaDoc)key,tree,introspector);
256          } catch (Throwable JavaDoc 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 JavaDoc getAllEntries() throws JoftiException {
268         try{
269             return treeAdapter.getAllValuesForTree(tree, introspector);
270             
271          } catch (Throwable JavaDoc e) {
272              if (!(e instanceof JoftiException)) {
273                  throw new JoftiException(e);
274              }else{
275                  throw (JoftiException)e;
276              }
277              
278          }
279     }
280     
281     /* (non-Javadoc)
282      * @see com.jofti.core.InternalIndex#getAttributesByKey(java.lang.Object)
283      */

284     public Map JavaDoc getAttributesByKey(Object JavaDoc key) throws JoftiException {
285         try{
286         Collection JavaDoc col = treeAdapter.getAttribsByKey((Comparable JavaDoc)key,tree,introspector);
287         if(col ==null || col.size()==0){
288             return new HashMap JavaDoc(2);
289         }
290         Map JavaDoc temp = new HashMap JavaDoc(col.size());
291         int size =col.size();
292         Iterator JavaDoc it = col.iterator();
293         for (int i=0;i<size;i++){
294             ValueObject obj = (ValueObject)it.next();
295             temp.put(new Integer JavaDoc(obj.getDimension()),obj.getRealValue());
296         }
297         return temp;
298     } catch (Throwable JavaDoc 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     /* (non-Javadoc)
312      * @see com.jofti.core.InternalIndex#removeAll()
313      */

314     public void removeAll()
315     {
316         tree.removeAll();
317         keyNumber =new AtomicLong(0);
318     }
319  
320     /**
321      * @return Returns the tree.
322      */

323     BTree getTree() {
324         return tree;
325     }
326     /**
327      * @param tree The tree to set.
328      */

329     public void setTree(BTree tree) {
330         this.tree = tree;
331     }
332     
333     /**
334      * @return Returns the parser.
335      */

336     public ClassIntrospector getIntrospector() {
337         return introspector;
338     }
339     /**
340      * @param parser The parser to set.
341      */

342     public void setParser(ClassIntrospector parser) {
343         this.introspector = parser;
344     }
345
346
347     
348     
349     
350
351     /**
352      * @return Returns the keyNumber.
353      */

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