KickJava   Java API By Example, From Geeks To Geeks.

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


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

14
15 package org.apache.activemq.kaha.impl.container;
16
17 import java.io.File JavaDoc;
18 import java.io.IOException JavaDoc;
19 import java.util.Collection JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.Map JavaDoc;
22 import java.util.Set JavaDoc;
23 import org.apache.activemq.kaha.ContainerId;
24 import org.apache.activemq.kaha.MapContainer;
25 import org.apache.activemq.kaha.Marshaller;
26 import org.apache.activemq.kaha.RuntimeStoreException;
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.DataManager;
31 import org.apache.activemq.kaha.impl.data.Item;
32 import org.apache.activemq.kaha.impl.index.Index;
33 import org.apache.activemq.kaha.impl.index.IndexItem;
34 import org.apache.activemq.kaha.impl.index.IndexLinkedList;
35 import org.apache.activemq.kaha.impl.index.IndexManager;
36 import org.apache.activemq.kaha.impl.index.VMIndex;
37 import org.apache.activemq.kaha.impl.index.hash.HashIndex;
38 import org.apache.commons.logging.Log;
39 import org.apache.commons.logging.LogFactory;
40
41 /**
42  * Implementation of a MapContainer
43  *
44  * @version $Revision: 1.2 $
45  */

46 public final class MapContainerImpl extends BaseContainerImpl implements MapContainer{
47
48     private static final Log log=LogFactory.getLog(MapContainerImpl.class);
49     protected Index index;
50     protected Marshaller keyMarshaller=Store.ObjectMarshaller;
51     protected Marshaller valueMarshaller=Store.ObjectMarshaller;
52     protected File JavaDoc directory;
53
54     public MapContainerImpl(File JavaDoc directory,ContainerId id,IndexItem root,IndexManager indexManager,
55             DataManager dataManager,boolean persistentIndex){
56         super(id,root,indexManager,dataManager,persistentIndex);
57         this.directory=directory;
58     }
59
60     public synchronized void init(){
61         super.init();
62         if(index==null){
63             if(persistentIndex){
64                 String JavaDoc name=containerId.getDataContainerName()+"_"+containerId.getKey();
65                 name=name.replaceAll("[^a-zA-Z0-9\\.\\_\\-]", "_");
66                 try{
67                     this.index=new HashIndex(directory,name,indexManager);
68                 }catch(IOException JavaDoc e){
69                     log.error("Failed to create HashIndex",e);
70                     throw new RuntimeException JavaDoc(e);
71                 }
72             }else{
73                 this.index=new VMIndex(indexManager);
74             }
75         }
76         index.setKeyMarshaller(keyMarshaller);
77     }
78
79     /*
80      * (non-Javadoc)
81      *
82      * @see org.apache.activemq.kaha.MapContainer#load()
83      */

84     public synchronized void load(){
85         checkClosed();
86         if(!loaded){
87             if(!loaded){
88                 loaded=true;
89                 try{
90                     init();
91                     index.load();
92                     long nextItem=root.getNextItem();
93                     while(nextItem!=Item.POSITION_NOT_SET){
94                         IndexItem item=indexManager.getIndex(nextItem);
95                         StoreLocation data=item.getKeyDataItem();
96                         Object JavaDoc key=dataManager.readItem(keyMarshaller,data);
97                         if(index.isTransient()){
98                             index.store(key,item);
99                         }
100                         indexList.add(item);
101                         nextItem=item.getNextItem();
102                     }
103                 }catch(IOException JavaDoc e){
104                     log.error("Failed to load container "+getId(),e);
105                     throw new RuntimeStoreException(e);
106                 }
107             }
108         }
109     }
110
111     /*
112      * (non-Javadoc)
113      *
114      * @see org.apache.activemq.kaha.MapContainer#unload()
115      */

116     public synchronized void unload(){
117         checkClosed();
118         if(loaded){
119             loaded=false;
120             try{
121                 index.unload();
122             }catch(IOException JavaDoc e){
123                 log.warn("Failed to unload the index",e);
124             }
125             indexList.clear();
126         }
127     }
128
129     public synchronized void setKeyMarshaller(Marshaller keyMarshaller){
130         checkClosed();
131         this.keyMarshaller=keyMarshaller;
132         if(index!=null){
133             index.setKeyMarshaller(keyMarshaller);
134         }
135     }
136
137     public synchronized void setValueMarshaller(Marshaller valueMarshaller){
138         checkClosed();
139         this.valueMarshaller=valueMarshaller;
140     }
141
142     /*
143      * (non-Javadoc)
144      *
145      * @see org.apache.activemq.kaha.MapContainer#size()
146      */

147     public synchronized int size(){
148         load();
149         return indexList.size();
150     }
151
152     /*
153      * (non-Javadoc)
154      *
155      * @see org.apache.activemq.kaha.MapContainer#isEmpty()
156      */

157     public synchronized boolean isEmpty(){
158         load();
159         return indexList.isEmpty();
160     }
161
162     /*
163      * (non-Javadoc)
164      *
165      * @see org.apache.activemq.kaha.MapContainer#containsKey(java.lang.Object)
166      */

167     public synchronized boolean containsKey(Object JavaDoc key){
168         load();
169         try{
170             return index.containsKey(key);
171         }catch(IOException JavaDoc e){
172             log.error("Failed trying to find key: "+key,e);
173             throw new RuntimeException JavaDoc(e);
174         }
175     }
176
177     /*
178      * (non-Javadoc)
179      *
180      * @see org.apache.activemq.kaha.MapContainer#get(java.lang.Object)
181      */

182     public synchronized Object JavaDoc get(Object JavaDoc key){
183         load();
184         Object JavaDoc result=null;
185         StoreEntry item=null;
186         try{
187             item=index.get(key);
188         }catch(IOException JavaDoc e){
189             log.error("Failed trying to get key: "+key,e);
190             throw new RuntimeException JavaDoc(e);
191         }
192         if(item!=null){
193             result=getValue(item);
194         }
195         return result;
196     }
197     
198     /**
199      * Get the StoreEntry associated with the key
200      * @param key
201      * @return the StoreEntry
202      */

203     public synchronized StoreEntry getEntry(Object JavaDoc key) {
204         load();
205         StoreEntry item=null;
206         try{
207             item=index.get(key);
208         }catch(IOException JavaDoc e){
209             log.error("Failed trying to get key: "+key,e);
210             throw new RuntimeException JavaDoc(e);
211         }
212         return item;
213     }
214
215     /*
216      * (non-Javadoc)
217      *
218      * @see org.apache.activemq.kaha.MapContainer#containsValue(java.lang.Object)
219      */

220     public synchronized boolean containsValue(Object JavaDoc o){
221         load();
222         boolean result=false;
223         if(o!=null){
224             IndexItem item=indexList.getFirst();
225             while(item!=null){
226                 Object JavaDoc value=getValue(item);
227                 if(value!=null&&value.equals(o)){
228                     result=true;
229                     break;
230                 }
231                 item=indexList.getNextEntry(item);
232             }
233         }
234         return result;
235     }
236
237     /*
238      * (non-Javadoc)
239      *
240      * @see org.apache.activemq.kaha.MapContainer#putAll(java.util.Map)
241      */

242     public synchronized void putAll(Map JavaDoc t){
243         load();
244         if(t!=null){
245             for(Iterator JavaDoc i=t.entrySet().iterator();i.hasNext();){
246                 Map.Entry JavaDoc entry=(Map.Entry JavaDoc)i.next();
247                 put(entry.getKey(),entry.getValue());
248             }
249         }
250     }
251
252     /*
253      * (non-Javadoc)
254      *
255      * @see org.apache.activemq.kaha.MapContainer#keySet()
256      */

257     public synchronized Set JavaDoc keySet(){
258         load();
259         return new ContainerKeySet(this);
260     }
261
262     /*
263      * (non-Javadoc)
264      *
265      * @see org.apache.activemq.kaha.MapContainer#values()
266      */

267     public synchronized Collection JavaDoc values(){
268         load();
269         return new ContainerValueCollection(this);
270     }
271
272     /*
273      * (non-Javadoc)
274      *
275      * @see org.apache.activemq.kaha.MapContainer#entrySet()
276      */

277     public synchronized Set JavaDoc entrySet(){
278         load();
279         return new ContainerEntrySet(this);
280     }
281
282     /*
283      * (non-Javadoc)
284      *
285      * @see org.apache.activemq.kaha.MapContainer#put(java.lang.Object, java.lang.Object)
286      */

287     public synchronized Object JavaDoc put(Object JavaDoc key,Object JavaDoc value){
288         load();
289         Object JavaDoc result=remove(key);
290         IndexItem item=write(key,value);
291         try{
292             index.store(key,item);
293         }catch(IOException JavaDoc e){
294             log.error("Failed trying to insert key: "+key,e);
295             throw new RuntimeException JavaDoc(e);
296         }
297         indexList.add(item);
298         return result;
299     }
300
301     /*
302      * (non-Javadoc)
303      *
304      * @see org.apache.activemq.kaha.MapContainer#remove(java.lang.Object)
305      */

306     public synchronized Object JavaDoc remove(Object JavaDoc key){
307         load();
308         try{
309             Object JavaDoc result=null;
310             IndexItem item=(IndexItem)index.remove(key);
311             if(item!=null){
312                 // refresh the index
313
item=(IndexItem)indexList.refreshEntry(item);
314                 result=getValue(item);
315                 IndexItem prev=indexList.getPrevEntry(item);
316                 IndexItem next=indexList.getNextEntry(item);
317                 indexList.remove(item);
318                 delete(item,prev,next);
319             }
320             return result;
321         }catch(IOException JavaDoc e){
322             log.error("Failed trying to remove key: "+key,e);
323             throw new RuntimeException JavaDoc(e);
324         }
325     }
326
327     public synchronized boolean removeValue(Object JavaDoc o){
328         load();
329         boolean result=false;
330         if(o!=null){
331             IndexItem item=indexList.getFirst();
332             while(item!=null){
333                 Object JavaDoc value=getValue(item);
334                 if(value!=null&&value.equals(o)){
335                     result=true;
336                     // find the key
337
Object JavaDoc key=getKey(item);
338                     if(key!=null){
339                         remove(key);
340                     }
341                     break;
342                 }
343                 item=indexList.getNextEntry(item);
344             }
345         }
346         return result;
347     }
348
349     protected synchronized void remove(IndexItem item){
350         Object JavaDoc key=getKey(item);
351         if(key!=null){
352             remove(key);
353         }
354     }
355
356     /*
357      * (non-Javadoc)
358      *
359      * @see org.apache.activemq.kaha.MapContainer#clear()
360      */

361     public synchronized void clear(){
362         checkClosed();
363         loaded=true;
364         init();
365         if(index!=null){
366             try{
367                 index.clear();
368             }catch(IOException JavaDoc e){
369                 log.error("Failed trying clear index",e);
370                 throw new RuntimeException JavaDoc(e);
371             }
372         }
373         super.clear();
374         doClear();
375     }
376
377     /**
378      * Add an entry to the Store Map
379      *
380      * @param key
381      * @param value
382      * @return the StoreEntry associated with the entry
383      */

384     public synchronized StoreEntry place(Object JavaDoc key,Object JavaDoc value){
385         load();
386         try{
387             remove(key);
388             IndexItem item=write(key,value);
389             index.store(key,item);
390             indexList.add(item);
391             return item;
392         }catch(IOException JavaDoc e){
393             log.error("Failed trying to place key: "+key,e);
394             throw new RuntimeException JavaDoc(e);
395         }
396     }
397
398     /**
399      * Remove an Entry from ther Map
400      *
401      * @param entry
402      * @throws IOException
403      */

404     public synchronized void remove(StoreEntry entry){
405         load();
406         IndexItem item=(IndexItem)entry;
407         if(item!=null){
408             Object JavaDoc key=getKey(item);
409             try{
410                 index.remove(key);
411             }catch(IOException JavaDoc e){
412                 log.error("Failed trying to remove entry: "+entry,e);
413                 throw new RuntimeException JavaDoc(e);
414             }
415             IndexItem prev=indexList.getPrevEntry(item);
416             IndexItem next=indexList.getNextEntry(item);
417             indexList.remove(item);
418             delete(item,prev,next);
419         }
420     }
421
422     public synchronized StoreEntry getFirst(){
423         load();
424         return indexList.getFirst();
425     }
426
427     public synchronized StoreEntry getLast(){
428         load();
429         return indexList.getLast();
430     }
431
432     public synchronized StoreEntry getNext(StoreEntry entry){
433         load();
434         IndexItem item=(IndexItem)entry;
435         return indexList.getNextEntry(item);
436     }
437
438     public synchronized StoreEntry getPrevious(StoreEntry entry){
439         load();
440         IndexItem item=(IndexItem)entry;
441         return indexList.getPrevEntry(item);
442     }
443
444     public synchronized StoreEntry refresh(StoreEntry entry){
445         load();
446         return indexList.getEntry(entry);
447     }
448
449     /**
450      * Get the value from it's location
451      *
452      * @param item
453      * @return the value associated with the store entry
454      */

455     public synchronized Object JavaDoc getValue(StoreEntry item){
456         load();
457         Object JavaDoc result=null;
458         if(item!=null){
459             try{
460                 // ensure this value is up to date
461
// item=indexList.getEntry(item);
462
StoreLocation data=item.getValueDataItem();
463                 result=dataManager.readItem(valueMarshaller,data);
464             }catch(IOException JavaDoc e){
465                 log.error("Failed to get value for "+item,e);
466                 throw new RuntimeStoreException(e);
467             }
468         }
469         return result;
470     }
471
472     /**
473      * Get the Key object from it's location
474      *
475      * @param item
476      * @return the Key Object associated with the StoreEntry
477      */

478     public synchronized Object JavaDoc getKey(StoreEntry item){
479         load();
480         Object JavaDoc result=null;
481         if(item!=null){
482             try{
483                 StoreLocation data=item.getKeyDataItem();
484                 result=dataManager.readItem(keyMarshaller,data);
485             }catch(IOException JavaDoc e){
486                 log.error("Failed to get key for "+item,e);
487                 throw new RuntimeStoreException(e);
488             }
489         }
490         return result;
491     }
492
493     protected IndexLinkedList getItemList(){
494         return indexList;
495     }
496
497     protected synchronized IndexItem write(Object JavaDoc key,Object JavaDoc value){
498         IndexItem index=null;
499         try{
500             if(key!=null){
501                 index=indexManager.createNewIndex();
502                 StoreLocation data=dataManager.storeDataItem(keyMarshaller,key);
503                 index.setKeyData(data);
504             }
505             if(value!=null){
506                 StoreLocation data=dataManager.storeDataItem(valueMarshaller,value);
507                 index.setValueData(data);
508             }
509             IndexItem prev=indexList.getLast();
510             prev=prev!=null?prev:indexList.getRoot();
511             IndexItem next=indexList.getNextEntry(prev);
512             prev.setNextItem(index.getOffset());
513             index.setPreviousItem(prev.getOffset());
514             updateIndexes(prev);
515             if(next!=null){
516                 next.setPreviousItem(index.getOffset());
517                 index.setNextItem(next.getOffset());
518                 updateIndexes(next);
519             }
520             storeIndex(index);
521         }catch(IOException JavaDoc e){
522             log.error("Failed to write "+key+" , "+value,e);
523             throw new RuntimeStoreException(e);
524         }
525         return index;
526     }
527 }
528
Popular Tags