KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > db > impl > LRUCache


1 /*
2  * (c) Copyright 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
3  * All rights reserved.
4  *
5  *
6  */

7
8
9 //=======================================================================
10
// Package
11
package com.hp.hpl.jena.db.impl;
12
13 //=======================================================================
14
// Imports
15
import java.util.*;
16
17 import com.hp.hpl.jena.shared.JenaException;
18 import com.hp.hpl.jena.util.CollectionFactory;
19
20
21 //=======================================================================
22
/**
23 * As simple LRU cache based on LinkedHashMap. otherwise, pretty much
24 * the same as SimpleCache.
25 *
26 * @author <a HREF="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a>
27 * @version $Revision: 1.7 $ on $Date: 2005/02/21 12:03:06 $
28 */

29
30 public class LRUCache implements ICache {
31     
32     /* don't use until jena moves to jre 1.4
33     public class myLinkedHashMap extends LinkedHashMap {
34         
35         int threshold;
36         
37         public myLinkedHashMap ( int max ) {
38             super(max);
39             threshold = max;
40         }
41         
42         protected boolean removeEldestEntry ( Map.Entry eldest ) {
43             return size() > threshold;
44         }
45      }
46     protected myLinkedHashMap cache;
47     
48     public LRUCache(int max) {
49         maxCount = max;
50         cache = new myLinkedHashMap(max);
51     }
52     
53     */

54     
55     protected Map keyCache;
56     protected Map valCache;
57
58     protected IDBID Keys[];
59     protected Random rand;
60
61     public LRUCache(int max) {
62         rand = new Random();
63         resize(max);
64     }
65     
66     protected void resize ( int max ) {
67         maxCount = max;
68         keyCache = CollectionFactory.createHashedMap(max);
69         valCache = CollectionFactory.createHashedMap(max);
70         Keys = new IDBID[max];
71         count = 0;
72     }
73
74     protected int maxCount;
75     protected int count;
76
77     public Object JavaDoc get(IDBID id) {
78         return keyCache.get(id);
79     }
80     
81     public Object JavaDoc getByValue(String JavaDoc val) {
82         return valCache.get(val);
83     }
84
85
86     public void put(IDBID id, Object JavaDoc val) {
87         synchronized (this) {
88             int curSize = keyCache.size();
89             keyCache.put(id, val);
90             valCache.put(val,id);
91             if (keyCache.size() > curSize) {
92                 int ix = count++;
93                 if (count > maxCount) {
94                     // pick an entry at random and remove it.
95
// not exactly LRU
96
ix = rand.nextInt(maxCount);
97                     Object JavaDoc keyval = keyCache.get(Keys[ix]);
98                     if ( (keyval == null) || (keyCache.remove(Keys[ix]) == null) )
99                         throw new JenaException("LRUCache keyCache corrupted");
100                     if ( valCache.remove(keyval) == null )
101                         throw new JenaException("LRUCache valCache corrupted");
102                     count--;
103                     Keys[ix] = id;
104                     if (keyCache.size() > maxCount)
105                         throw new JenaException("LRUCache exceeds threshold");
106                 }
107                 Keys[ix] = id;
108             }
109         }
110     }
111
112     /* save for java 1.4
113     public void put(IDBID id, Object val) {
114         cache.put(id, val);
115         if ( cache.size() > maxCount ) {
116             throw new JenaException("LRUCache exceeds threshold");
117         }
118     }
119     */

120     
121     public void clear() {
122         keyCache.clear();
123         valCache.clear();
124         count = 0;
125     }
126     
127     /*
128     public void setLimit(int max) {
129         maxCount = max;
130         cache = new myLinkedHashMap(max);
131     }
132     */

133     public void setLimit(int max) {
134         resize(max);
135     }
136
137     public int getLimit() {
138         return maxCount;
139     }
140
141 }
142 /*
143  * (c) Copyright 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
144  * All rights reserved.
145  *
146  * Redistribution and use in source and binary forms, with or without
147  * modification, are permitted provided that the following conditions
148  * are met:
149  * 1. Redistributions of source code must retain the above copyright
150  * notice, this list of conditions and the following disclaimer.
151  * 2. Redistributions in binary form must reproduce the above copyright
152  * notice, this list of conditions and the following disclaimer in the
153  * documentation and/or other materials provided with the distribution.
154  * 3. The name of the author may not be used to endorse or promote products
155  * derived from this software without specific prior written permission.
156  *
157  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
158  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
159  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
160  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
161  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
162  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
163  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
164  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
165  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
166  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
167  */

168
169
170
Popular Tags