1 2 6 package com.jofti.tree; 7 8 import java.lang.reflect.Array ; 9 import java.util.ArrayList ; 10 import java.util.Collection ; 11 import java.util.Iterator ; 12 import java.util.List ; 13 import java.util.Map ; 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 29 public class TreeOperationAdapter { 30 31 32 33 private static Log log = LogFactory.getLog(TreeOperationAdapter.class); 34 35 36 public void insertEntry(Comparable key, Object value, BTree tree,ClassIntrospector parser) throws IllegalArgumentException , JoftiException{ 37 List addedObjects = new ArrayList (); 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 BTOperations.insertKeyValue(tree, key, ((KeyValueObject)val).getAttributes(),val.getDimension()); 51 addedObjects.add(key); 52 }else{ 53 BTOperations.insertValue(tree, key, (Comparable )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 key, Object value, BTree tree,ClassIntrospector parser) throws IllegalArgumentException , JoftiException{ 64 List addedObjects = new ArrayList (); 66 Map 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 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 key, BTree tree,Map attributes, List addedObjects) throws IllegalArgumentException , JoftiException{ 99 100 int size =attributes.entrySet().size(); 101 Iterator it = attributes.entrySet().iterator(); 102 for(int i=0;i<size;i++){ 103 Map.Entry entry = (Map.Entry )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 temp = entry.getValue(); 111 int arraySize = Array.getLength(temp); 113 for (int j=0;j<arraySize;j++){ 114 Object val = Array.get(temp,j); 115 BTOperations.insertValue(tree, key, (Comparable )val,((Integer )entry.getKey()).intValue()); 116 117 } 118 }else{ 119 BTOperations.insertValue(tree, key, (Comparable )entry.getValue(),((Integer )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 key, Object value, BTree tree,ClassIntrospector parser) throws IllegalArgumentException , JoftiException{ 131 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 key, Object value, BTree tree,ClassIntrospector parser) throws IllegalArgumentException , JoftiException{ 146 147 try { 148 Map attributes = parser.getAttributeValues(value); 149 150 for (Iterator it = attributes.entrySet().iterator();it.hasNext();){ 151 Map.Entry entry = (Map.Entry )it.next(); 152 if(log.isDebugEnabled()){ 153 log.debug("Attempting to remove key '" + key +"' value '" + entry.getValue()); 154 } 155 BTOperations.removeValue(tree, key, (Comparable )entry.getValue(),((Integer )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 added,BTree tree,ClassIntrospector parser ){ 166 for (Iterator it = added.iterator();it.hasNext();){ 167 Comparable obj =null; 168 try { 169 obj = (Comparable )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 key, List added,BTree tree ){ 179 for (Iterator it = added.iterator();it.hasNext();){ 180 Comparable obj =null; 181 try { 182 obj = (Comparable )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 value, BTree tree, ClassIntrospector parser) throws JoftiException 191 { 192 Collection matchingValues = BTOperations.getAllValuesForKey(tree,value,parser.getKeyDimension(value.getClass())); 193 194 for (Iterator it = matchingValues.iterator();it.hasNext();){ 195 BTOperations.removeValueObject(tree,value,(Comparable )it.next()); 196 197 } 198 } 199 200 public void removeByKey(Comparable value, BTree tree, ClassIntrospector parser) throws JoftiException 201 { 202 203 204 Collection matchingValues = BTOperations.getKeyAttributes(tree,value,parser.getKeyDimension(value.getClass())); 205 if (matchingValues != null){ 206 if (matchingValues.size() >0){ 207 for (Iterator it = matchingValues.iterator();it.hasNext();){ 208 BTOperations.removeValueObject(tree,value,(Comparable )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 getAttribsByKey(Comparable 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 key,BTree tree, ClassIntrospector parser)throws JoftiException{ 235 return BTOperations.contains(tree, key, parser.getKeyDimension(key.getClass())); 236 } 237 238 239 public Map getAllValuesForKey(Comparable key, BTree tree, ClassIntrospector parser)throws JoftiException{ 240 241 return BTOperations.match(tree,key, parser.getKeyDimension(key.getClass())); 242 243 } 244 245 public Map 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 |