KickJava   Java API By Example, From Geeks To Geeks.

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


1
2 /*
3  * Created on 13-May-2005
4  *
5  */

6 package com.jofti.tree;
7
8 import java.lang.reflect.Array JavaDoc;
9 import java.util.ArrayList JavaDoc;
10 import java.util.Collection JavaDoc;
11 import java.util.Iterator JavaDoc;
12 import java.util.List JavaDoc;
13 import java.util.Map JavaDoc;
14
15 import org.apache.commons.logging.Log;
16 import org.apache.commons.logging.LogFactory;
17
18 import com.jofti.btree.BTOperations;
19 import com.jofti.btree.BTree;
20 import com.jofti.btree.KeyValueObject;
21 import com.jofti.btree.ValueObject;
22 import com.jofti.exception.JoftiException;
23 import com.jofti.introspect.ClassIntrospector;
24
25 /**
26  * @author Steve Woodcock (steve@jofti.com)
27  *
28  */

29 public class TreeOperationAdapter {
30
31     
32     
33     private static Log log = LogFactory.getLog(TreeOperationAdapter.class);
34     
35     
36     public void insertEntry(Comparable JavaDoc key, Object JavaDoc value, BTree tree,ClassIntrospector parser) throws IllegalArgumentException JavaDoc, JoftiException{
37         List JavaDoc addedObjects = new ArrayList JavaDoc();
38         
39         ValueObject val = (ValueObject)value;
40         
41          try {
42              
43             
44               if(log.isDebugEnabled()){
45                 log.debug("Attempting to insert key '" + key +"'");
46               }
47             
48               if (val.getDimension() <0){
49                   // must be a key value
50
BTOperations.insertKeyValue(tree, key, ((KeyValueObject)val).getAttributes(),val.getDimension());
51                  addedObjects.add(key);
52               }else{
53                   BTOperations.insertValue(tree, key, (Comparable JavaDoc)val.getRealValue(),val.getDimension());
54               }
55          } catch (JoftiException e){
56                 log.error("Error encountered - removing indexed objects " + addedObjects,e);
57                 removeIndexedValuesByKey(addedObjects,tree ,parser);
58             }
59         
60     }
61     
62     
63     public void insert(Comparable JavaDoc key, Object JavaDoc value, BTree tree,ClassIntrospector parser) throws IllegalArgumentException JavaDoc, JoftiException{
64         // first is it the right type
65
List JavaDoc addedObjects = new ArrayList JavaDoc();
66          Map JavaDoc attributes = parser.getAttributeValues(value);
67          if (attributes == null || attributes.isEmpty()){
68              if(log.isDebugEnabled()){
69                     log.debug("No attributes found for '" + key +"' ignoring in index");
70               }
71              return;
72          }
73          try {
74              
75             
76               if(log.isDebugEnabled()){
77                 log.debug("Attempting to insert key '" + key +"'");
78              }
79              
80               // we index the key first
81

82              BTOperations.insertKeyValue(tree, key, attributes,parser.getKeyDimension(key.getClass()));
83              addedObjects.add(key);
84              if(log.isDebugEnabled()){
85                 log.debug("inserted key '" + key +"'");
86              }
87              
88             
89              
90              treeInsert(key,tree,attributes,addedObjects);
91             
92          } catch (JoftiException e){
93             log.error("Error encountered - removing indexed objects " + addedObjects,e);
94             removeIndexedValues(key,addedObjects,tree );
95         }
96     }
97     
98     protected void treeInsert(Comparable JavaDoc key, BTree tree,Map JavaDoc attributes, List JavaDoc addedObjects) throws IllegalArgumentException JavaDoc, JoftiException{
99           
100         int size =attributes.entrySet().size();
101         Iterator JavaDoc it = attributes.entrySet().iterator();
102         for(int i=0;i<size;i++){
103                     Map.Entry JavaDoc entry = (Map.Entry JavaDoc)it.next();
104                     
105                         if(log.isDebugEnabled()){
106                             log.debug("Attempting to insert key '" + key +"' value '" + entry.getValue());
107                          }
108                         
109                         if (entry.getValue().getClass().isArray()){
110                             Object JavaDoc temp = entry.getValue();
111                             // we need to loop through the values here
112
int arraySize = Array.getLength(temp);
113                             for (int j=0;j<arraySize;j++){
114                                 Object JavaDoc val = Array.get(temp,j);
115                                 BTOperations.insertValue(tree, key, (Comparable JavaDoc)val,((Integer JavaDoc)entry.getKey()).intValue());
116                                 
117                             }
118                         }else{
119                             BTOperations.insertValue(tree, key, (Comparable JavaDoc)entry.getValue(),((Integer JavaDoc)entry.getKey()).intValue());
120                         }
121                         addedObjects.add(entry.getValue());
122                         if(log.isDebugEnabled()){
123                             log.debug("Insert key '" + key +"' value '" + entry.getValue());
124                          }
125                             
126                 }
127             
128     }
129     
130     public void remove(Comparable JavaDoc key, Object JavaDoc value, BTree tree,ClassIntrospector parser) throws IllegalArgumentException JavaDoc, JoftiException{
131         // first is it the right type
132

133         treeRemove(key,value,tree,parser);
134                   
135         if(log.isDebugEnabled()){
136             log.debug("Attempting to remove key '" + key +"'");
137          }
138
139          BTOperations.removeValue(tree, key, key,parser.getKeyDimension(key.getClass()));
140          if(log.isDebugEnabled()){
141             log.debug("Removed key '" + key +"'");
142          }
143     }
144     
145     protected void treeRemove(Comparable JavaDoc key, Object JavaDoc value, BTree tree,ClassIntrospector parser) throws IllegalArgumentException JavaDoc, JoftiException{
146         
147          try {
148             Map JavaDoc attributes = parser.getAttributeValues(value);
149             
150             for (Iterator JavaDoc it = attributes.entrySet().iterator();it.hasNext();){
151                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc)it.next();
152                 if(log.isDebugEnabled()){
153                     log.debug("Attempting to remove key '" + key +"' value '" + entry.getValue());
154                  }
155                 BTOperations.removeValue(tree, key, (Comparable JavaDoc)entry.getValue(),((Integer JavaDoc)entry.getKey()).intValue());
156                 if(log.isDebugEnabled()){
157                     log.debug("Removed key '" + key +"' value '" + entry.getValue());
158                  }
159             }
160          } catch (JoftiException e){
161             throw e;
162          }
163     }
164     
165      public void removeIndexedValuesByKey(List JavaDoc added,BTree tree,ClassIntrospector parser ){
166             for (Iterator JavaDoc it = added.iterator();it.hasNext();){
167                 Comparable JavaDoc obj =null;
168                 try {
169                     obj = (Comparable JavaDoc)it.next();
170                     removeByKey(obj,tree, parser);
171                 } catch (JoftiException e){
172                     log.error("unable to remove value " + obj +" under key " + obj,e);
173                 }
174             }
175         }
176     
177     
178      public void removeIndexedValues(Object JavaDoc key, List JavaDoc added,BTree tree ){
179         for (Iterator JavaDoc it = added.iterator();it.hasNext();){
180             Comparable JavaDoc obj =null;
181             try {
182                 obj = (Comparable JavaDoc)it.next();
183                 BTOperations.removeValueObject(tree, key, obj);
184             } catch (JoftiException e){
185                 log.error("unable to remove value " + obj +" under key " + key,e);
186             }
187         }
188     }
189     
190       public void removeEntries(Comparable JavaDoc value, BTree tree, ClassIntrospector parser) throws JoftiException
191         {
192             Collection JavaDoc matchingValues = BTOperations.getAllValuesForKey(tree,value,parser.getKeyDimension(value.getClass()));
193
194             for (Iterator JavaDoc it = matchingValues.iterator();it.hasNext();){
195                 BTOperations.removeValueObject(tree,value,(Comparable JavaDoc)it.next());
196                 
197             }
198         }
199       
200       public void removeByKey(Comparable JavaDoc value, BTree tree, ClassIntrospector parser) throws JoftiException
201         {
202           
203
204             Collection JavaDoc matchingValues = BTOperations.getKeyAttributes(tree,value,parser.getKeyDimension(value.getClass()));
205             if (matchingValues != null){
206                 if (matchingValues.size() >0){
207                 for (Iterator JavaDoc it = matchingValues.iterator();it.hasNext();){
208                     BTOperations.removeValueObject(tree,value,(Comparable JavaDoc)it.next());
209                     
210                 }
211                 }
212                 if(log.isDebugEnabled()){
213                     log.debug("Attempting to remove key '" + value +"'");
214                  }
215     
216                  BTOperations.removeValue(tree, value, value,parser.getKeyDimension(value.getClass()));
217                  if(log.isDebugEnabled()){
218                     log.debug("Removed key '" + value +"'");
219                  }
220             
221             }
222             
223         }
224       
225       public Collection JavaDoc getAttribsByKey(Comparable JavaDoc value, BTree tree, ClassIntrospector parser) throws JoftiException
226         {
227           
228
229             return BTOperations.getKeyAttributes(tree,value,parser.getKeyDimension(value.getClass()));
230
231             
232         }
233       
234       public boolean contains(Comparable JavaDoc key,BTree tree, ClassIntrospector parser)throws JoftiException{
235          return BTOperations.contains(tree, key, parser.getKeyDimension(key.getClass()));
236     }
237         
238      
239      public Map JavaDoc getAllValuesForKey(Comparable JavaDoc key, BTree tree, ClassIntrospector parser)throws JoftiException{
240         
241          return BTOperations.match(tree,key, parser.getKeyDimension(key.getClass()));
242      
243    }
244      
245      public Map JavaDoc getAllValuesForTree(BTree tree, ClassIntrospector parser)throws JoftiException{
246          
247
248          return BTOperations.getSubTreeKeyValues(tree,null,null,Integer.MIN_VALUE,Integer.MAX_VALUE-1,true);
249      
250    }
251       
252 }
253
Popular Tags