KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mr > core > persistent > PersistentMap


1 /*
2  * Copyright 2002 by
3  * <a HREF="http://www.coridan.com">Coridan</a>
4  * <a HREF="mailto: support@coridan.com ">support@coridan.com</a>
5  *
6  * The contents of this file are subject to the Mozilla Public License Version
7  * 1.1 (the "License"); you may not use this file except in compliance with the
8  * License. You may obtain a copy of the License at
9  * http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is "MantaRay" (TM).
17  *
18  * The Initial Developer of the Original Code is Amir Shevat.
19  * Portions created by the Initial Developer are Copyright (C) 2006
20  * Coridan Inc. All Rights Reserved.
21  *
22  * Contributor(s): all the names of the contributors are added in the source
23  * code where applicable.
24  *
25  * Alternatively, the contents of this file may be used under the terms of the
26  * LGPL license (the "GNU LESSER GENERAL PUBLIC LICENSE"), in which case the
27  * provisions of LGPL are applicable instead of those above. If you wish to
28  * allow use of your version of this file only under the terms of the LGPL
29  * License and not to allow others to use your version of this file under
30  * the MPL, indicate your decision by deleting the provisions above and
31  * replace them with the notice and other provisions required by the LGPL.
32  * If you do not delete the provisions above, a recipient may use your version
33  * of this file under either the MPL or the GNU LESSER GENERAL PUBLIC LICENSE.
34  
35  *
36  * This library is free software; you can redistribute it and/or modify it
37  * under the terms of the MPL as stated above or under the terms of the GNU
38  * Lesser General Public License as published by the Free Software Foundation;
39  * either version 2.1 of the License, or any later version.
40  *
41  * This library is distributed in the hope that it will be useful, but WITHOUT
42  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
43  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
44  * License for more details.
45  */

46 /*
47  * Created on Dec 11, 2003
48  *
49  * Acts like a map (Hash table) but keeps data in file storage for future recovery
50  */

51 package org.mr.core.persistent;
52
53 import java.io.IOException JavaDoc;
54 import java.util.Collection JavaDoc;
55 import java.util.HashMap JavaDoc;
56 import java.util.Iterator JavaDoc;
57 import java.util.LinkedHashMap JavaDoc;
58 import java.util.LinkedList JavaDoc;
59 import java.util.Map JavaDoc;
60 import java.util.Set JavaDoc;
61
62
63 import org.apache.commons.logging.Log;
64 import org.apache.commons.logging.LogFactory;
65
66 import org.mr.core.util.byteable.Byteable;
67 import org.mr.core.util.byteable.ByteableMapEntry;
68
69 /**
70  * PersistentMap holds entries just like any other Map and keeps them on disk for Persistent.
71  *
72  * @author Amir Shevat
73  *
74  */

75 public class PersistentMap implements Map JavaDoc {
76     // this object writes/deletes the entrys to/from file
77
private Log log;
78     // every entry is given a name (file name) this is it
79
private int itemsCount = 0;
80     // inner maps to keep track of (entry key) ->(file name)
81
private HashMap JavaDoc entryToFileMap = new HashMap JavaDoc();
82     // delegated map
83
private LinkedHashMap JavaDoc delegated = new LinkedHashMap JavaDoc();
84     //private HashMap delegated = new HashMap();
85
private boolean defaultPersistent ;
86     
87     private LinkedList JavaDoc freeList = new LinkedList JavaDoc();
88     
89     private PersistentManager persistentManager ;
90     
91     /**
92      * @param name - the name of the file these maps will be save to.
93      * @param defaultPersistent - when invoking method 'put(Object key, Object value)' the file will
94      * be persistent if this value is true.
95      * @param blocking - true if return only after file has been written esle false.
96      */

97     public PersistentMap(String JavaDoc name ,boolean defaultPersistent, boolean blocking){
98         super();
99         try {
100             this.defaultPersistent = defaultPersistent;
101             log=LogFactory.getLog("PersistentMap");
102             persistentManager = PersistentManagerFactory.getPersistentManager(name);
103             recover(name);
104         } catch (Throwable JavaDoc e) {
105             if(log.isFatalEnabled()){
106                 log.fatal("Can not init persistent stracture. ",e);
107             }
108         }
109     }//PersistentMap
110

111     /**
112      * recovers the data of the stract from file.
113      * @param name the name of the file this stract was save to.
114      * @throws IOException
115      */

116     private void recover(String JavaDoc name) throws IOException JavaDoc{
117          
118         persistentManager.recover();
119         int[] keySet = persistentManager.getKeys();
120         if(log.isInfoEnabled()&& keySet.length >0 ){
121             log.info("Starting to recover "+name+". There are "+keySet.length+" elements to recover, this might take time.");
122         }
123         
124         for(int index =0 ; index < keySet.length ; index++){
125             
126             
127             ByteableMapEntry entry =(ByteableMapEntry)persistentManager.getPersistentObject(keySet[index]);
128             
129             if(entry!= null){
130                 Integer JavaDoc fileKey = new Integer JavaDoc(keySet[index]);
131                 entryToFileMap.put(entry.getKey() ,fileKey);
132                 delegated.put(entry.getKey() ,entry.getValue() );
133                 itemsCount++;
134             }else{
135                 if(log.isFatalEnabled()){
136                     log.fatal("Could not recover "+name+":"+keySet[index]+".");
137                 }//if
138
}
139         }
140         if(log.isInfoEnabled()&& keySet.length >0 ){
141             log.info("Recovery of " + name + " is complete.");
142         }
143
144         //System.out.println(delegated);
145
}//recover
146

147     
148     /**
149      * same as super but with file save.
150      */

151     public final synchronized Object JavaDoc put(Object JavaDoc key, Object JavaDoc value){
152         return put( (String JavaDoc)key, (Byteable)value , defaultPersistent);
153     }
154     
155     /**
156      * Puts the entry in the map and saves it if persisnent == true
157      * @param key key with which the specified value is to be associated.
158      * @param value value to be associated with the specified key.
159      * @param persisnent - if true this key value pair are kept on disk
160      * @return previous value associated with specified key, or <tt>null</tt>
161      * if there was no mapping for key. A <tt>null</tt> return can
162      * also indicate that the HashMap previously associated
163      * <tt>null</tt> with the specified key.
164      */

165     public final synchronized Object JavaDoc put(String JavaDoc key, Byteable value , boolean persisnent){
166         if(persisnent){
167             ByteableMapEntry entry = null;
168             try{
169                 // if this is a overwrite of the same key free the name
170
Integer JavaDoc freeKey = (Integer JavaDoc)entryToFileMap.remove(key);
171                 if(freeKey != null)
172                     freeList.add(freeKey);
173                 
174                 // create the file key (name) and put in EntryToFileMap
175
int fileName = getValidFreePersistentName();
176                 Integer JavaDoc fileKey = new Integer JavaDoc(fileName);
177                 entryToFileMap.put(key ,fileKey);
178                 // create a mini table and save it with the file key (name)
179
entry = new ByteableMapEntry(key,value);
180                 
181                 persistentManager.savePersistentObject(fileName ,entry );
182             }catch(Exception JavaDoc io){
183                 if(log.isFatalEnabled()){
184                     log.fatal("Can not save entry "+entry+"." , io );
185                 }
186             }
187         }// if
188
return delegated.put(key ,value );
189     }// put
190

191     
192     /**
193      * @return a valid file name with the flowing disclamer that the map is not bigger then the max file of fat
194      */

195     private final int getValidFreePersistentName(){
196         // TODO : make this work for any number of elements in queue
197
if(!freeList.isEmpty()){
198             return ((Integer JavaDoc)freeList.removeFirst()).intValue();
199         }
200         int result = itemsCount;
201         itemsCount++;
202         if(itemsCount >= PersistentManager.MAX_FILES_PER_STORAGE){
203             throw new IllegalStateException JavaDoc("MAP IS FULL CAN NOT ALLOCATE MORE PERSISTENT SPACE.");
204         }
205         return result;
206     }
207     
208     /**
209      * same as super but with file delete
210      */

211     public final synchronized Object JavaDoc remove(Object JavaDoc key){
212         
213         Integer JavaDoc freeKey = (Integer JavaDoc)entryToFileMap.remove(key);
214         if(freeKey != null){
215             persistentManager.deletePersistentObject(freeKey.intValue());
216             freeList.add(freeKey);
217         }
218         return delegated.remove(key);
219     }
220     
221     public final synchronized void removeAll(){
222         Object JavaDoc key;
223         Integer JavaDoc freeKey;
224         Iterator JavaDoc i = entryToFileMap.keySet().iterator();
225         while (i.hasNext()) {
226             key = i.next();
227             freeKey = (Integer JavaDoc)entryToFileMap.get(key);
228             i.remove();
229             if(freeKey != null){
230                 persistentManager.deletePersistentObject(freeKey.intValue());
231                 freeList.add(freeKey);
232             }
233             delegated.remove(key);
234         }
235     }
236     
237     /**
238      * same as super but with file delete all
239      * @throws IOException
240      */

241     public final synchronized void clear() {
242         try {
243             persistentManager.clearStorage();
244         } catch (IOException JavaDoc e) {
245             if(log.isFatalEnabled()){
246                 log.fatal("Can not clear map "+persistentManager.getStorageName()+"." , e );
247             }
248         }
249         entryToFileMap.clear();
250         delegated.clear();
251         freeList.clear();
252     }
253
254     /* (non-Javadoc)
255      * @see java.util.Map#size()
256      */

257     public final synchronized int size() {
258         return delegated.size();
259     }
260
261     /* (non-Javadoc)
262      * @see java.util.Map#isEmpty()
263      */

264     public final synchronized boolean isEmpty() {
265
266         return delegated.isEmpty();
267     }
268
269     /* (non-Javadoc)
270      * @see java.util.Map#containsKey(java.lang.Object)
271      */

272     public final synchronized boolean containsKey(Object JavaDoc key) {
273         return delegated.containsKey(key);
274     }
275
276     /* (non-Javadoc)
277      * @see java.util.Map#containsValue(java.lang.Object)
278      */

279     public final synchronized boolean containsValue(Object JavaDoc value) {
280
281         return delegated.containsValue(value);
282     }
283
284     /* (non-Javadoc)
285      * @see java.util.Map#values()
286      */

287     public final synchronized Collection JavaDoc values() {
288         return delegated.values();
289     }
290
291     /* (non-Javadoc)
292      * @see java.util.Map#putAll(java.util.Map)
293      */

294     public final synchronized void putAll(Map JavaDoc t) {
295         Iterator JavaDoc i = t.entrySet().iterator();
296         while (i.hasNext()) {
297             Map.Entry JavaDoc e = (Map.Entry JavaDoc) i.next();
298             put(e.getKey(), e.getValue());
299         }
300         
301     }
302
303     /* (non-Javadoc)
304      * @see java.util.Map#entrySet()
305      */

306     public final synchronized Set JavaDoc entrySet() {
307         return delegated.entrySet();
308     }
309
310     /* (non-Javadoc)
311      * @see java.util.Map#keySet()
312      */

313     public final synchronized Set JavaDoc keySet() {
314         return delegated.keySet();
315     }
316
317     /* (non-Javadoc)
318      * @see java.util.Map#get(java.lang.Object)
319      */

320     public final synchronized Object JavaDoc get(Object JavaDoc key) {
321         return delegated.get(key);
322     }
323 }
324
Popular Tags