KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > base > util > cache > CacheLineTable


1 /*
2  * $Id: CacheLineTable.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2004-2005 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  */

25 package org.ofbiz.base.util.cache;
26
27 import java.io.IOException JavaDoc;
28 import java.io.Serializable JavaDoc;
29 import java.util.Collection JavaDoc;
30 import java.util.Collections JavaDoc;
31 import java.util.HashSet JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import java.util.List JavaDoc;
34 import java.util.Map JavaDoc;
35 import java.util.Set JavaDoc;
36
37 import javolution.util.FastList;
38 import javolution.util.FastMap;
39
40 import org.apache.commons.collections.map.LRUMap;
41 import org.ofbiz.base.util.Debug;
42 import org.ofbiz.base.util.ObjectType;
43
44 /**
45  *
46  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
47  * @version $Rev: 5462 $
48  * @since 3.2
49  */

50 public class CacheLineTable implements Serializable JavaDoc {
51
52     public static final String JavaDoc module = CacheLineTable.class.getName();
53     protected static transient jdbm.RecordManager jdbmMgr = null;
54
55     protected transient jdbm.htree.HTree fileTable = null;
56     protected Map JavaDoc memoryTable = null;
57     protected String JavaDoc fileStore = null;
58     protected String JavaDoc cacheName = null;
59     protected int maxInMemory = 0;
60
61     public CacheLineTable(String JavaDoc fileStore, String JavaDoc cacheName, boolean useFileSystemStore, int maxInMemory) {
62         this.fileStore = fileStore;
63         this.cacheName = cacheName;
64         this.maxInMemory = maxInMemory;
65         if (useFileSystemStore) {
66             // create the manager the first time it is needed
67
if (CacheLineTable.jdbmMgr == null) {
68                 synchronized (this) {
69                     if (CacheLineTable.jdbmMgr == null) {
70                         try {
71                             Debug.logImportant("Creating file system cache store for cache with name: " + cacheName, module);
72                             CacheLineTable.jdbmMgr = new JdbmRecordManager(fileStore);
73                         } catch (IOException JavaDoc e) {
74                             Debug.logError(e, "Error creating file system cache store for cache with name: " + cacheName, module);
75                         }
76                     }
77                 }
78             }
79             if (CacheLineTable.jdbmMgr != null) {
80                 try {
81                     long recno = CacheLineTable.jdbmMgr.getNamedObject(cacheName);
82                     if (recno != 0) {
83                         this.fileTable = jdbm.htree.HTree.load(CacheLineTable.jdbmMgr, recno);
84                     } else {
85                         this.fileTable = jdbm.htree.HTree.createInstance(CacheLineTable.jdbmMgr);
86                         CacheLineTable.jdbmMgr.setNamedObject(cacheName, this.fileTable.getRecid());
87                         CacheLineTable.jdbmMgr.commit();
88                     }
89                 } catch (IOException JavaDoc e) {
90                     Debug.logError(e, module);
91                 }
92             }
93         }
94         this.setLru(maxInMemory);
95     }
96
97     public synchronized Object JavaDoc put(Object JavaDoc key, Object JavaDoc value) {
98         if (key == null) {
99             if (Debug.verboseOn()) Debug.logVerbose("In CacheLineTable tried to put with null key, using NullObject" + this.cacheName, module);
100             key = ObjectType.NULL;
101         }
102         memoryTable.put(key, value);
103         if (fileTable != null) {
104             try {
105                 fileTable.put(key, value);
106                 CacheLineTable.jdbmMgr.commit();
107             } catch (IOException JavaDoc e) {
108                 Debug.logError(e, module);
109             }
110         }
111         return value;
112     }
113
114     public Object JavaDoc get(Object JavaDoc key) {
115         if (key == null) {
116             if (Debug.verboseOn()) Debug.logVerbose("In CacheLineTable tried to get with null key, using NullObject" + this.cacheName, module);
117             key = ObjectType.NULL;
118         }
119         Object JavaDoc value = memoryTable.get(key);
120         if (value == null) {
121             if (fileTable != null) {
122                 try {
123                     value = fileTable.get(key);
124                 } catch (IOException JavaDoc e) {
125                     Debug.logError(e, module);
126                 }
127             }
128         }
129         return value;
130     }
131
132     public synchronized Object JavaDoc remove(Object JavaDoc key) {
133         if (key == null) {
134             if (Debug.verboseOn()) Debug.logVerbose("In CacheLineTable tried to remove with null key, using NullObject" + this.cacheName, module);
135             key = ObjectType.NULL;
136         }
137         Object JavaDoc value = this.get(key);
138         if (fileTable != null) {
139             try {
140                 fileTable.remove(key);
141             } catch (IOException JavaDoc e) {
142                 Debug.logError(e, module);
143             }
144         }
145         memoryTable.remove(key);
146         return value;
147     }
148
149     public synchronized Collection JavaDoc values() {
150         List JavaDoc values = FastList.newInstance();
151
152         if (fileTable != null) {
153             try {
154                 jdbm.helper.FastIterator iter = fileTable.values();
155                 Object JavaDoc value = iter.next();
156                 while (value != null) {
157                     values.add(value);
158                     value = iter.next();
159                 }
160             } catch (IOException JavaDoc e) {
161                 Debug.logError(e, module);
162             }
163         } else {
164             values.addAll(memoryTable.values());
165         }
166
167         return values;
168     }
169
170     /**
171      *
172      * @return An unmodifiable Set for the keys for this cache; to remove while iterating call the remove method on this class.
173      */

174     public synchronized Set JavaDoc keySet() {
175         // note that this must be a HashSet and not a FastSet in order to have a null value
176
Set JavaDoc keys = new HashSet JavaDoc();
177
178         if (fileTable != null) {
179             try {
180                 jdbm.helper.FastIterator iter = fileTable.keys();
181                 Object JavaDoc key = null;
182                 while ((key = iter.next()) != null) {
183                     if (key instanceof ObjectType.NullObject) {
184                         keys.add(null);
185                     } else {
186                         keys.add(key);
187                     }
188                 }
189             } catch (IOException JavaDoc e) {
190                 Debug.logError(e, module);
191             }
192         } else {
193             keys.addAll(memoryTable.keySet());
194             if (keys.contains(ObjectType.NULL)) {
195                 keys.remove(ObjectType.NULL);
196                 keys.add(null);
197             }
198         }
199
200         return Collections.unmodifiableSet(keys);
201     }
202
203     public synchronized void clear() {
204         if (fileTable != null && this.size() > 0) {
205             try {
206                 // remove this table
207
long recid = fileTable.getRecid();
208                 CacheLineTable.jdbmMgr.delete(recid);
209                 CacheLineTable.jdbmMgr.commit();
210                 this.fileTable = null;
211
212                 // create a new table
213
this.fileTable = jdbm.htree.HTree.createInstance(CacheLineTable.jdbmMgr);
214                 CacheLineTable.jdbmMgr.setNamedObject(cacheName, this.fileTable.getRecid());
215                 CacheLineTable.jdbmMgr.commit();
216             } catch (IOException JavaDoc e) {
217                 Debug.logError(e, module);
218             }
219         }
220         memoryTable.clear();
221     }
222
223     public int size() {
224         if (fileTable != null) {
225             return this.keySet().size();
226         } else {
227             return memoryTable.size();
228         }
229     }
230
231     public synchronized void setLru(int newSize) {
232         this.maxInMemory = newSize;
233
234         Map JavaDoc oldmap = null;
235         if (this.memoryTable != null) {
236             // using linked map to preserve the order when using LRU (FastMap is a linked map)
237
oldmap = FastMap.newInstance();
238             oldmap.putAll(this.memoryTable);
239         }
240
241         if (newSize > 0) {
242             this.memoryTable = new LRUMap(newSize);
243         } else {
244             this.memoryTable = FastMap.newInstance();
245         }
246
247         if (oldmap != null) {
248             this.memoryTable.putAll(oldmap);
249         }
250     }
251
252     public synchronized Object JavaDoc getKeyFromMemory(int index) {
253         Iterator JavaDoc i = null;
254         if (memoryTable instanceof LRUMap) {
255             i = ((LRUMap) memoryTable).orderedMapIterator();
256         } else {
257             i = memoryTable.keySet().iterator();
258         }
259
260         int currentIdx = 0;
261         while (i.hasNext()) {
262             Object JavaDoc key = i.next();
263             if (currentIdx == index) {
264                 return key;
265             }
266         }
267         return null;
268     }
269 }
270
271
Popular Tags