KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > kaha > impl > container > BaseContainerImpl


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with this
5  * work for additional information regarding copyright ownership. The ASF
6  * licenses this file to You under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance with the License.
8  * 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, WITHOUT
14  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15  * License for the specific language governing permissions and limitations under
16  * the License.
17  */

18
19 package org.apache.activemq.kaha.impl.container;
20
21 import java.io.IOException JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.List JavaDoc;
24 import org.apache.activemq.kaha.ContainerId;
25 import org.apache.activemq.kaha.RuntimeStoreException;
26 import org.apache.activemq.kaha.Store;
27 import org.apache.activemq.kaha.StoreEntry;
28 import org.apache.activemq.kaha.impl.DataManager;
29 import org.apache.activemq.kaha.impl.data.Item;
30 import org.apache.activemq.kaha.impl.index.DiskIndexLinkedList;
31 import org.apache.activemq.kaha.impl.index.IndexItem;
32 import org.apache.activemq.kaha.impl.index.IndexLinkedList;
33 import org.apache.activemq.kaha.impl.index.IndexManager;
34 import org.apache.activemq.kaha.impl.index.VMIndexLinkedList;
35 import org.apache.commons.logging.Log;
36 import org.apache.commons.logging.LogFactory;
37
38 /**
39  * Implementation of a ListContainer
40  *
41  * @version $Revision: 1.2 $
42  */

43 public abstract class BaseContainerImpl{
44
45     private static final Log log=LogFactory.getLog(BaseContainerImpl.class);
46     protected IndexItem root;
47     protected IndexLinkedList indexList;
48     protected IndexManager indexManager;
49     protected DataManager dataManager;
50     protected ContainerId containerId;
51     protected boolean loaded=false;
52     protected boolean closed=false;
53     protected boolean initialized=false;
54     protected boolean persistentIndex;
55
56     protected BaseContainerImpl(ContainerId id,IndexItem root,IndexManager indexManager,
57             DataManager dataManager,boolean persistentIndex){
58         this.containerId=id;
59         this.root=root;
60         this.indexManager=indexManager;
61         this.dataManager=dataManager;
62         this.persistentIndex = persistentIndex;
63     }
64
65     public ContainerId getContainerId(){
66         return containerId;
67     }
68
69     public synchronized void init(){
70         if(!initialized){
71             if(!initialized){
72                 initialized=true;
73                 if(this.indexList==null){
74                     if(persistentIndex){
75                         this.indexList=new DiskIndexLinkedList(indexManager,root);
76                     }else{
77                         this.indexList=new VMIndexLinkedList(root);
78                     }
79                 }
80             }
81         }
82     }
83
84     
85     public synchronized void clear(){
86         if(indexList!=null){
87             indexList.clear();
88         }
89     }
90     /**
91      * @return the indexList
92      */

93     public IndexLinkedList getList(){
94         return indexList;
95     }
96
97     /**
98      * @param indexList the indexList to set
99      */

100     public void setList(IndexLinkedList indexList){
101         this.indexList=indexList;
102     }
103
104     public abstract void unload();
105
106     public abstract void load();
107
108     public abstract int size();
109
110     protected abstract Object JavaDoc getValue(StoreEntry currentItem);
111
112     protected abstract void remove(IndexItem currentItem);
113
114     protected synchronized final IndexLinkedList getInternalList(){
115         return indexList;
116     }
117
118     public synchronized final void close(){
119         unload();
120         closed=true;
121     }
122
123     /*
124      * (non-Javadoc)
125      *
126      * @see org.apache.activemq.kaha.ListContainer#isLoaded()
127      */

128     public synchronized final boolean isLoaded(){
129         checkClosed();
130         return loaded;
131     }
132
133     /*
134      * (non-Javadoc)
135      *
136      * @see org.apache.activemq.kaha.ListContainer#getId()
137      */

138     public final Object JavaDoc getId(){
139         checkClosed();
140         return containerId.getKey();
141     }
142     
143     public DataManager getDataManager(){
144         return dataManager;
145     }
146
147     
148     public IndexManager getIndexManager(){
149         return indexManager;
150     }
151
152     public synchronized final void expressDataInterest() throws IOException JavaDoc{
153         long nextItem=root.getNextItem();
154         while(nextItem!=Item.POSITION_NOT_SET){
155             IndexItem item=indexManager.getIndex(nextItem);
156             item.setOffset(nextItem);
157             dataManager.addInterestInFile(item.getKeyFile());
158             dataManager.addInterestInFile(item.getValueFile());
159             nextItem=item.getNextItem();
160         }
161     }
162
163     protected final void doClear(){
164         checkClosed();
165         loaded=true;
166         List JavaDoc indexList=new ArrayList JavaDoc();
167         try{
168             init();
169             long nextItem=root.getNextItem();
170             while(nextItem!=Item.POSITION_NOT_SET){
171                 IndexItem item=new IndexItem();
172                 item.setOffset(nextItem);
173                 indexList.add(item);
174                 nextItem=item.getNextItem();
175             }
176             root.setNextItem(Item.POSITION_NOT_SET);
177             storeIndex(root);
178             for(int i=0;i<indexList.size();i++){
179                 IndexItem item=(IndexItem)indexList.get(i);
180                 dataManager.removeInterestInFile(item.getKeyFile());
181                 dataManager.removeInterestInFile(item.getValueFile());
182                 indexManager.freeIndex(item);
183             }
184             indexList.clear();
185         }catch(IOException JavaDoc e){
186             log.error("Failed to clear Container "+getId(),e);
187             throw new RuntimeStoreException(e);
188         }
189     }
190
191     protected final void delete(IndexItem key,IndexItem prev,IndexItem next){
192         try{
193             dataManager.removeInterestInFile(key.getKeyFile());
194             dataManager.removeInterestInFile(key.getValueFile());
195             prev=prev==null?root:prev;
196             next=next!=root?next:null;
197             if(next!=null){
198                 prev.setNextItem(next.getOffset());
199                 next.setPreviousItem(prev.getOffset());
200                 updateIndexes(next);
201             }else{
202                 prev.setNextItem(Item.POSITION_NOT_SET);
203             }
204             updateIndexes(prev);
205             indexManager.freeIndex(key);
206         }catch(IOException JavaDoc e){
207             log.error("Failed to delete "+key,e);
208             throw new RuntimeStoreException(e);
209         }
210     }
211
212     protected final void checkClosed(){
213         if(closed){
214             throw new RuntimeStoreException("The store is closed");
215         }
216     }
217
218     protected void storeIndex(IndexItem item) throws IOException JavaDoc{
219         indexManager.storeIndex(item);
220     }
221     
222     protected void updateIndexes(IndexItem item) throws IOException JavaDoc{
223         indexManager.updateIndexes(item);
224     }
225
226     protected final boolean isRoot(StoreEntry item){
227         return item!=null&&root!=null&&(root==item||root.getOffset()==item.getOffset());
228         // return item != null && indexRoot != null && indexRoot == item;
229
}
230
231     
232    
233 }
234
Popular Tags