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.util.CollectionFactory;
017:
018: //=======================================================================
019: /**
020: * Trivial implementation of the generic cache interface used to cache
021: * literals and resources. This implementation simple flushes the cache
022: * when the threshold limit is exceeded.
023: *
024: * @author <a href="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a>
025: * @version $Revision: 1.8 $ on $Date: 2008/01/02 12:08:25 $
026: */
027:
028: public class SimpleCache implements ICache {
029:
030: /** The cache itself */
031: protected Map cache = CollectionFactory.createHashedMap();
032:
033: /** The current size limit */
034: protected int threshold;
035:
036: /** The current number of entries (probably redundant, just use cache.size) */
037: protected int count = 0;
038:
039: /**
040: * Create an empty cache with the given threshold limit.
041: *
042: * @param threshold the cache size limit, use 0 for no cache, -1 for
043: * unlimited cache growth; any other number indicates the number of cache entries
044: */
045: public SimpleCache(int threshold) {
046: this .threshold = threshold;
047: }
048:
049: /**
050: * Add an entry to the cache
051: * @param id the database ID to be used as an index
052: * @param val the literal or resources to be stored
053: */
054: public void put(IDBID id, Object val) {
055: if (threshold == 0)
056: return;
057: if (threshold > 0 && count >= threshold) {
058: cache = CollectionFactory.createHashedMap();
059: count = 0;
060: }
061: count++;
062: cache.put(id, val);
063: }
064:
065: /**
066: * Retreive an object from the cache
067: * @param id the database ID of the object to be retrieved
068: * @return the object or null if it is not in the cache
069: */
070: public Object get(IDBID id) {
071: return cache.get(id);
072: }
073:
074: /**
075: * Set a threshold for the cache size in terms of the count of cache entries.
076: * For literals a storage limit rather than a count might be more useful but
077: * counts are easier, more general and sufficient for the current use.
078: *
079: * @param threshold the cache size limit, use 0 for no cache, -1 for
080: * unlimited cache growth; any other number indicates the number of cache entries
081: */
082: public void setLimit(int threshold) {
083: this .threshold = threshold;
084: if (threshold >= 0 && count > threshold) {
085: cache = CollectionFactory.createHashedMap();
086: count = 0;
087: }
088: }
089:
090: /**
091: * Return the current threshold limit for the cache size.
092: */
093: public int getLimit() {
094: return threshold;
095: }
096: }
097: /*
098: * (c) Copyright 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
099: * All rights reserved.
100: *
101: * Redistribution and use in source and binary forms, with or without
102: * modification, are permitted provided that the following conditions
103: * are met:
104: * 1. Redistributions of source code must retain the above copyright
105: * notice, this list of conditions and the following disclaimer.
106: * 2. Redistributions in binary form must reproduce the above copyright
107: * notice, this list of conditions and the following disclaimer in the
108: * documentation and/or other materials provided with the distribution.
109: * 3. The name of the author may not be used to endorse or promote products
110: * derived from this software without specific prior written permission.
111: *
112: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
113: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
114: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
115: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
116: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
117: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
118: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
119: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
120: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
121: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
122: */
|