KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > kaha > impl > IndexRootContainer


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.kaha.impl;
19
20 import java.io.IOException JavaDoc;
21 import java.util.LinkedList JavaDoc;
22 import java.util.Map JavaDoc;
23 import java.util.Set JavaDoc;
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 JavaDoc;
37
38 /**
39 * A container of roots for other Containers
40 *
41 * @version $Revision: 1.2 $
42 */

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 JavaDoc map = new ConcurrentHashMap JavaDoc();
51     protected LinkedList JavaDoc list = new LinkedList JavaDoc();
52     
53     
54     IndexRootContainer(IndexItem root,IndexManager im,DataManager dfm) throws IOException JavaDoc{
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 JavaDoc 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 JavaDoc getKeys(){
71         return map.keySet();
72     }
73     
74     
75     
76     IndexItem addRoot(IndexManager containerIndexManager,ContainerId key) throws IOException JavaDoc{
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 JavaDoc{
101         StoreEntry oldRoot=(StoreEntry)map.remove(key);
102         if(oldRoot!=null){
103             dataManager.removeInterestInFile(oldRoot.getKeyFile());
104             // get the container root
105
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 JavaDoc{
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 JavaDoc key){
135         return map.containsKey(key);
136     }
137
138     
139
140 }
141
Popular Tags