1 18 package org.apache.activemq.kaha.impl; 19 20 import java.io.IOException ; 21 import java.util.LinkedList ; 22 import java.util.Map ; 23 import java.util.Set ; 24 25 import org.apache.activemq.kaha.ContainerId; 26 import org.apache.activemq.kaha.Marshaller; 27 import org.apache.activemq.kaha.Store; 28 import org.apache.activemq.kaha.StoreEntry; 29 import org.apache.activemq.kaha.StoreLocation; 30 import org.apache.activemq.kaha.impl.data.Item; 31 import org.apache.activemq.kaha.impl.index.IndexItem; 32 import org.apache.activemq.kaha.impl.index.IndexManager; 33 import org.apache.commons.logging.Log; 34 import org.apache.commons.logging.LogFactory; 35 36 import java.util.concurrent.ConcurrentHashMap ; 37 38 43 44 class IndexRootContainer { 45 private static final Log log=LogFactory.getLog(IndexRootContainer.class); 46 protected static final Marshaller rootMarshaller = Store.ObjectMarshaller; 47 protected IndexItem root; 48 protected IndexManager indexManager; 49 protected DataManager dataManager; 50 protected Map map = new ConcurrentHashMap (); 51 protected LinkedList list = new LinkedList (); 52 53 54 IndexRootContainer(IndexItem root,IndexManager im,DataManager dfm) throws IOException { 55 this.root=root; 56 this.indexManager=im; 57 this.dataManager=dfm; 58 long nextItem=root.getNextItem(); 59 while(nextItem!=Item.POSITION_NOT_SET){ 60 StoreEntry item=indexManager.getIndex(nextItem); 61 StoreLocation data=item.getKeyDataItem(); 62 Object key = dataManager.readItem(rootMarshaller,data); 63 map.put(key,item); 64 list.add(item); 65 nextItem=item.getNextItem(); 66 dataManager.addInterestInFile(item.getKeyFile()); 67 } 68 } 69 70 Set getKeys(){ 71 return map.keySet(); 72 } 73 74 75 76 IndexItem addRoot(IndexManager containerIndexManager,ContainerId key) throws IOException { 77 if (map.containsKey(key)){ 78 removeRoot(containerIndexManager,key); 79 } 80 81 StoreLocation data = dataManager.storeDataItem(rootMarshaller, key); 82 IndexItem newRoot = indexManager.createNewIndex(); 83 newRoot.setKeyData(data); 84 IndexItem containerRoot = containerIndexManager.createNewIndex(); 85 containerIndexManager.storeIndex(containerRoot); 86 newRoot.setValueOffset(containerRoot.getOffset()); 87 88 IndexItem last=list.isEmpty()?null:(IndexItem) list.getLast(); 89 last=last==null?root:last; 90 long prev=last.getOffset(); 91 newRoot.setPreviousItem(prev); 92 indexManager.storeIndex(newRoot); 93 last.setNextItem(newRoot.getOffset()); 94 indexManager.storeIndex(last); 95 map.put(key, newRoot); 96 list.add(newRoot); 97 return containerRoot; 98 } 99 100 void removeRoot(IndexManager containerIndexManager,ContainerId key) throws IOException { 101 StoreEntry oldRoot=(StoreEntry)map.remove(key); 102 if(oldRoot!=null){ 103 dataManager.removeInterestInFile(oldRoot.getKeyFile()); 104 IndexItem containerRoot=containerIndexManager.getIndex(oldRoot.getValueOffset()); 106 if(containerRoot!=null){ 107 containerIndexManager.freeIndex(containerRoot); 108 } 109 int index=list.indexOf(oldRoot); 110 IndexItem prev=index>0?(IndexItem)list.get(index-1):root; 111 prev=prev==null?root:prev; 112 IndexItem next=index<(list.size()-1)?(IndexItem)list.get(index+1):null; 113 if(next!=null){ 114 prev.setNextItem(next.getOffset()); 115 next.setPreviousItem(prev.getOffset()); 116 indexManager.updateIndexes(next); 117 }else{ 118 prev.setNextItem(Item.POSITION_NOT_SET); 119 } 120 indexManager.updateIndexes(prev); 121 list.remove(oldRoot); 122 indexManager.freeIndex((IndexItem)oldRoot); 123 } 124 } 125 126 IndexItem getRoot(IndexManager containerIndexManager,ContainerId key) throws IOException { 127 StoreEntry index = (StoreEntry) map.get(key); 128 if (index != null){ 129 return containerIndexManager.getIndex(index.getValueOffset()); 130 } 131 return null; 132 } 133 134 boolean doesRootExist(Object key){ 135 return map.containsKey(key); 136 } 137 138 139 140 } 141 | Popular Tags |