001: /*
002: * Created on 13-May-2005
003: *
004: */
005: package com.jofti.tree;
006:
007: import java.util.ArrayList;
008: import java.util.Collection;
009: import java.util.Iterator;
010: import java.util.LinkedHashSet;
011: import java.util.List;
012: import java.util.Map;
013: import java.util.Set;
014:
015: import org.apache.commons.logging.Log;
016: import org.apache.commons.logging.LogFactory;
017:
018: import com.jofti.api.NameSpaceKey;
019: import com.jofti.btree.BTOperations;
020: import com.jofti.btree.BTree;
021: import com.jofti.cache.adapter.NameSpaceWrapper;
022: import com.jofti.exception.JoftiException;
023: import com.jofti.introspect.ClassIntrospector;
024: import com.jofti.util.ObjectProcedureAdapter;
025: import com.jofti.util.OpenHashMap;
026:
027: /**
028: * The class that the TreeIndex delegates to for operations involving NameSpace caches.<p>
029: *
030: *
031: * @author Steve Woodcock (steve@jofti.com)
032: *
033: */
034: public class NameSpaceTreeOperationAdapter extends TreeOperationAdapter {
035:
036: public NameSpaceTreeOperationAdapter() {
037: }
038:
039: private static Log log = LogFactory
040: .getLog(NameSpaceTreeOperationAdapter.class);
041:
042: /* (non-Javadoc)
043: * @see com.jofti.tree.TreeOperationAdapter#contains(java.lang.Comparable, com.jofti.btree.BTree, com.jofti.introspect.ClassIntrospector)
044: */
045: public boolean contains(Comparable key, BTree tree,
046: ClassIntrospector parser) throws JoftiException {
047: return BTOperations.contains(tree, key, parser
048: .getKeyDimension(((NameSpaceKey) key).getKey()
049: .getClass()));
050: }
051:
052: /* (non-Javadoc)
053: * @see com.jofti.tree.TreeOperationAdapter#insert(java.lang.Comparable, java.lang.Object, com.jofti.btree.BTree, com.jofti.introspect.ClassIntrospector)
054: */
055: public void insert(Comparable key, Object value, BTree tree,
056: ClassIntrospector parser) throws IllegalArgumentException,
057: JoftiException {
058: // first is it the right type
059:
060: Map attributes = parser.getAttributeValues(value);
061: if (attributes == null || attributes.isEmpty()) {
062: if (log.isDebugEnabled()) {
063: log.debug("No attributes found for '" + key
064: + "' ignoring in index");
065: }
066: return;
067: }
068:
069: List addedObjects = new ArrayList();
070: NameSpaceKey tempKey = (NameSpaceKey) key;
071:
072: // we index the key first
073: if (log.isDebugEnabled()) {
074: log.debug("Attempting to insert name space '" + tempKey
075: + "'");
076: }
077: BTOperations.insertValue(tree, key, new NameSpaceWrapper(
078: tempKey.getNameSpace()), parser
079: .getKeyDimension(NameSpaceWrapper.class));
080:
081: // first is it the right type
082:
083: try {
084:
085: if (log.isDebugEnabled()) {
086: log.debug("Attempting to insert key '" + key + "'");
087: }
088: // we index the key first
089: BTOperations.insertKeyValue(tree, key, attributes, parser
090: .getKeyDimension(((NameSpaceKey) key).getClass()));
091: addedObjects.add(key);
092: if (log.isDebugEnabled()) {
093: log.debug("inserted key '" + key + "'");
094: }
095:
096: treeInsert(key, tree, attributes, addedObjects);
097:
098: } catch (JoftiException e) {
099: log.error("Error encountered - removing indexed objects "
100: + addedObjects, e);
101: removeIndexedValues(key, addedObjects, tree, parser);
102: }
103:
104: }
105:
106: /* (non-Javadoc)
107: * @see com.jofti.tree.TreeOperationAdapter#removeByKey(java.lang.Comparable, com.jofti.btree.BTree, com.jofti.introspect.ClassIntrospector)
108: */
109: public void removeByKey(Comparable value, BTree tree,
110: ClassIntrospector parser) throws JoftiException {
111:
112: // Collection matchingValues = BTOperations.getKeyAttributes(tree,value,parser.getKeyDimension(((NameSpaceKey)value).getKey().getClass()));
113: Collection matchingValues = BTOperations.getKeyAttributes(tree,
114: value, parser.getKeyDimension(((NameSpaceKey) value)
115: .getClass()));
116:
117: if (matchingValues != null) {
118: if (matchingValues.size() > 0) {
119: for (Iterator it = matchingValues.iterator(); it
120: .hasNext();) {
121: BTOperations.removeValueObject(tree, value,
122: (Comparable) it.next());
123:
124: }
125: }
126: if (log.isDebugEnabled()) {
127: log.debug("Attempting to remove key '" + value + "'");
128: }
129:
130: BTOperations.removeValue(tree, value, value, parser
131: .getKeyDimension(value.getClass()));
132: if (log.isDebugEnabled()) {
133: log.debug("Removed key '" + value + "'");
134: }
135: NameSpaceKey tempKey = (NameSpaceKey) value;
136: if (log.isDebugEnabled()) {
137: log.debug("Attempting to remove name space entry'"
138: + value + "'");
139: }
140: BTOperations.removeValue(tree, value, new NameSpaceWrapper(
141: tempKey.getNameSpace()), parser
142: .getKeyDimension(NameSpaceWrapper.class));
143:
144: }
145:
146: }
147:
148: /* (non-Javadoc)
149: * @see com.jofti.tree.TreeOperationAdapter#remove(java.lang.Comparable, java.lang.Object, com.jofti.btree.BTree, com.jofti.introspect.ClassIntrospector)
150: */
151: public void remove(Comparable key, Object value, BTree tree,
152: ClassIntrospector parser) throws IllegalArgumentException,
153: JoftiException {
154: // first is it the right type
155: try {
156:
157: if (log.isDebugEnabled()) {
158: log.debug("Attempting to remove key '" + key + "'");
159: }
160:
161: BTOperations.removeValue(tree, key, key, parser
162: .getKeyDimension(((NameSpaceKey) key).getKey()
163: .getClass()));
164: if (log.isDebugEnabled()) {
165: log.debug("Removed key '" + key + "'");
166: }
167:
168: treeRemove(key, value, tree, parser);
169:
170: NameSpaceKey tempKey = (NameSpaceKey) key;
171: // we index the key first
172: if (log.isDebugEnabled()) {
173: log.debug("Attempting to remove name space entry'"
174: + tempKey + "'");
175: }
176: BTOperations.removeValue(tree, key, new NameSpaceWrapper(
177: tempKey.getNameSpace()), parser
178: .getKeyDimension(NameSpaceWrapper.class));
179:
180: } catch (JoftiException e) {
181: throw e;
182: }
183: }
184:
185: /**
186: * @param key
187: * @param added
188: * @param tree
189: * @param parser
190: */
191: private void removeIndexedValues(Object key, List added,
192: BTree tree, ClassIntrospector parser) {
193: removeIndexedValues(key, added, tree);
194: if (key instanceof NameSpaceKey) {
195: NameSpaceKey tempKey = (NameSpaceKey) key;
196: // we index the key first
197: if (log.isDebugEnabled()) {
198: log.debug("Attempting to remove name space entry'"
199: + tempKey + "'");
200: }
201: try {
202: BTOperations.removeValue(tree, new NameSpaceWrapper(
203: tempKey.getNameSpace()), (Comparable) key,
204: parser.getKeyDimension(NameSpaceWrapper.class));
205: } catch (JoftiException e) {
206: log.warn(e);
207: }
208: }
209: }
210:
211: /* (non-Javadoc)
212: * @see com.jofti.tree.TreeOperationAdapter#getAllValuesForKey(java.lang.Comparable, com.jofti.btree.BTree, com.jofti.introspect.ClassIntrospector)
213: */
214: public Map getAllValuesForKey(Comparable key, BTree tree,
215: ClassIntrospector parser) throws JoftiException {
216:
217: return BTOperations.match(tree, key, parser.getKeyDimension(key
218: .getClass()));
219:
220: }
221:
222: /**
223: * @param tree
224: * @param parser
225: * @return A Collection of all the values in that dimension
226: * @throws JoftiException
227: */
228: public Collection getAllValuesForDimension(BTree tree,
229: ClassIntrospector parser) throws JoftiException {
230: final Set tempSet = new LinkedHashSet();
231: Map keyDimensions = parser.getKeyDimensions();
232:
233: for (Iterator it = keyDimensions.entrySet().iterator(); it
234: .hasNext();) {
235: Map.Entry entry = (Map.Entry) it.next();
236: if (!(entry.getKey() instanceof NameSpaceWrapper)) {
237: OpenHashMap tempMap = (OpenHashMap) BTOperations
238: .getAllResultsForDimension(tree,
239: ((Integer) entry.getValue()).intValue());
240: tempMap.forEachKey(new ObjectProcedureAdapter() {
241: public boolean apply(Object key) {
242: tempSet.add(key);
243: return true;
244: }
245: });
246: //tempSet.addAll(.keySet());
247: }
248:
249: }
250: return tempSet;
251:
252: }
253: }
|