001: /*
002: * (c) Copyright 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: * All rights reserved.
004: *
005: *
006: */
007:
008: //=======================================================================
009: // Package
010: package com.hp.hpl.jena.db.impl;
011:
012: //=======================================================================
013: // Imports
014: import java.util.*;
015:
016: import com.hp.hpl.jena.shared.JenaException;
017: import com.hp.hpl.jena.util.CollectionFactory;
018:
019: //=======================================================================
020: /**
021: * As simple LRU cache based on LinkedHashMap. otherwise, pretty much
022: * the same as SimpleCache.
023: *
024: * @author <a href="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a>
025: * @version $Revision: 1.10 $ on $Date: 2008/01/02 12:08:24 $
026: */
027:
028: public class LRUCache implements ICache {
029:
030: /* don't use until jena moves to jre 1.4
031: public class myLinkedHashMap extends LinkedHashMap {
032:
033: int threshold;
034:
035: public myLinkedHashMap ( int max ) {
036: super(max);
037: threshold = max;
038: }
039:
040: protected boolean removeEldestEntry ( Map.Entry eldest ) {
041: return size() > threshold;
042: }
043: }
044: protected myLinkedHashMap cache;
045:
046: public LRUCache(int max) {
047: maxCount = max;
048: cache = new myLinkedHashMap(max);
049: }
050:
051: */
052:
053: protected Map keyCache;
054: protected Map valCache;
055:
056: protected IDBID Keys[];
057: protected Random rand;
058:
059: public LRUCache(int max) {
060: rand = new Random();
061: resize(max);
062: }
063:
064: protected void resize(int max) {
065: maxCount = max;
066: keyCache = CollectionFactory.createHashedMap(max);
067: valCache = CollectionFactory.createHashedMap(max);
068: Keys = new IDBID[max];
069: count = 0;
070: }
071:
072: protected int maxCount;
073: protected int count;
074:
075: public Object get(IDBID id) {
076: return keyCache.get(id);
077: }
078:
079: public Object getByValue(String val) {
080: return valCache.get(val);
081: }
082:
083: public void put(IDBID id, Object val) {
084: synchronized (this ) {
085: int curSize = keyCache.size();
086: keyCache.put(id, val);
087: valCache.put(val, id);
088: if (keyCache.size() > curSize) {
089: int ix = count++;
090: if (count > maxCount) {
091: // pick an entry at random and remove it.
092: // not exactly LRU
093: ix = rand.nextInt(maxCount);
094: Object keyval = keyCache.get(Keys[ix]);
095: if ((keyval == null)
096: || (keyCache.remove(Keys[ix]) == null))
097: throw new JenaException(
098: "LRUCache keyCache corrupted");
099: if (valCache.remove(keyval) == null)
100: throw new JenaException(
101: "LRUCache valCache corrupted");
102: count--;
103: Keys[ix] = id;
104: if (keyCache.size() > maxCount)
105: throw new JenaException(
106: "LRUCache exceeds threshold");
107: }
108: Keys[ix] = id;
109: }
110: }
111: }
112:
113: /* save for java 1.4
114: public void put(IDBID id, Object val) {
115: cache.put(id, val);
116: if ( cache.size() > maxCount ) {
117: throw new JenaException("LRUCache exceeds threshold");
118: }
119: }
120: */
121:
122: public void clear() {
123: keyCache.clear();
124: valCache.clear();
125: count = 0;
126: }
127:
128: /*
129: public void setLimit(int max) {
130: maxCount = max;
131: cache = new myLinkedHashMap(max);
132: }
133: */
134: public void setLimit(int max) {
135: resize(max);
136: }
137:
138: public int getLimit() {
139: return maxCount;
140: }
141:
142: }
143: /*
144: * (c) Copyright 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
145: * All rights reserved.
146: *
147: * Redistribution and use in source and binary forms, with or without
148: * modification, are permitted provided that the following conditions
149: * are met:
150: * 1. Redistributions of source code must retain the above copyright
151: * notice, this list of conditions and the following disclaimer.
152: * 2. Redistributions in binary form must reproduce the above copyright
153: * notice, this list of conditions and the following disclaimer in the
154: * documentation and/or other materials provided with the distribution.
155: * 3. The name of the author may not be used to endorse or promote products
156: * derived from this software without specific prior written permission.
157: *
158: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
159: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
160: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
161: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
162: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
163: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
164: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
165: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
166: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
167: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
168: */
|