DiskStore.java

Index Score
net.sf.ehcache.store
ehcache

View: Reasons, Metrics, Source Code

These are the metrics that contribute to the Enerjy Score for this file, ranked by impact. So the metrics listed at the top influence the score to a greater extent that the metrics listed at the bottom.

MetricDescription
EXEC_COMMENTSComments in executable code
DECL_COMMENTSComments in declarations
SIZESize of the file in bytes
BLOCKSNumber of blocks
EXITSProcedure exits
LINE_COMMENTNumber of line comments
JAVA0143JAVA0143 Synchronized method
DOC_COMMENTNumber of javadoc comment lines
LINESNumber of lines in the source file
RETURNSNumber of return points from functions
LOGICAL_LINESNumber of statements
JAVA0166JAVA0166 Generic exception caught
CYCLOMATICCyclomatic complexity
ELOCEffective lines of code
LOCLines of code
OPERATORSNumber of operators
COMMENTSComment lines
PROGRAM_LENGTHHalstead program length
COMPARISONSNumber of comparison operators
JAVA0177JAVA0177 Variable declaration missing initializer
UNIQUE_OPERANDSNumber of unique operands
OPERANDSNumber of operands
FUNCTIONSNumber of function declarations
PROGRAM_VOCABHalstead program vocabulary
INTERFACE_COMPLEXITYInterface complexity
JAVA0110JAVA0110 Incorrect javadoc: no @return tag
JAVA0115JAVA0115 Incorrect javadoc: no @throws or @exception tag for 'exception'
JAVA0077JAVA0077 Private field not used in declaring class
JAVA0034JAVA0034 Missing braces in if statement
JAVA0144JAVA0144 Line exceeds maximum M characters
WHITESPACENumber of whitespace lines
JAVA0170JAVA0170 Caught exception not derived from java.lang.Exception
UNIQUE_OPERATORSNumber of unique operators
JAVA0049JAVA0049 Nested block at depth N (maximum: M)
LOOPSNumber of loops
JAVA0282JAVA0282 Call to Iterator.next() in loop which does not test Iterator.hasNext()
JAVA0128JAVA0128 Public constructor in non-public class
NEST_DEPTHMaximum nesting depth
PROGRAM_VOLUMEHalstead program volume
JAVA0136JAVA0136 N methods defined in class (maximum: M)
JAVA0174JAVA0174 Assigned local variable never used
JAVA0160JAVA0160 Method does not throw specified exception
JAVA0273JAVA0273 Non-final derivative of Thread calls start() in constructor
JAVA0264JAVA0264 Integer math in long context - check for overflow
JAVA0087JAVA0087 Use of Thread.sleep()
JAVA0117JAVA0117 Missing javadoc: method 'method'
JAVA0100JAVA0100 Class contains N non-final fields (maximum: M)
JAVA0130JAVA0130 Non-static method does not use instance fields
JAVA0108JAVA0108 Incorrect javadoc: no @param tag for 'parameter'
JAVA0145JAVA0145 Tab character used in source file
/** * Copyright 2003-2008 Luck Consulting Pty Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package net.sf.ehcache.store; import net.sf.ehcache.CacheException; import net.sf.ehcache.Ehcache; import net.sf.ehcache.Element; import net.sf.ehcache.Status; import net.sf.ehcache.util.MemoryEfficientByteArrayOutputStream; import net.sf.ehcache.event.RegisteredEventListeners; import java.io.ByteArrayInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.ObjectStreamClass; import java.io.RandomAccessFile; import java.io.Serializable; import java.io.StreamCorruptedException; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; import java.util.logging.Logger; import java.util.logging.Level; /** * A disk store implementation. * <p/> * As of ehcache-1.2 (v1.41 of this file) DiskStore has been changed to a mix of finer grained locking using synchronized collections * and synchronizing on the whole instance, as was the case with earlier versions. * <p/> * The DiskStore, as of ehcache-1.2.4, supports eviction using an LFU policy, if a maximum disk * store size is set. LFU uses statistics held at the Element level which survive moving between * maps in the MemoryStore and DiskStores. * * @author Adam Murdoch * @author Greg Luck * @author patches contributed: Ben Houston * @version $Id: DiskStore.java 744 2008-08-16 20:10:49Z gregluck $ */ public class DiskStore implements Store { /** * If the CacheManager needs to resolve a conflict with the disk path, it will create a * subdirectory in the given disk path with this prefix followed by a number. The presence of this * name is used to determined whether it makes sense for a persistent DiskStore to be loaded. Loading * persistent DiskStores will only have useful semantics where the diskStore path has not changed. */ public static final String AUTO_DISK_PATH_DIRECTORY_PREFIX = "ehcache_auto_created"; private static final Logger LOG = Logger.getLogger(DiskStore.class.getName()); private static final int MS_PER_SECOND = 1000; private static final int SPOOL_THREAD_INTERVAL = 200; private static final int ESTIMATED_MINIMUM_PAYLOAD_SIZE = 512; private static final int ONE_MEGABYTE = 1048576; private long expiryThreadInterval; private final String name; private boolean active; private RandomAccessFile randomAccessFile; private Map diskElements = Collections.synchronizedMap(new HashMap()); private List freeSpace = Collections.synchronizedList(new ArrayList()); private Map spool = new HashMap(); private Object spoolLock = new Object(); private Thread spoolAndExpiryThread; private Ehcache cache; /** * If persistent, the disk file will be kept * and reused on next startup. In addition the * memory store will flush all contents to spool, * and spool will flush all to disk. */ private final boolean persistent; private final String diskPath; private File dataFile; /** * Used to persist elements */ private File indexFile; private Status status; /** * The size in bytes of the disk elements */ private long totalSize; /** * The maximum elements to allow in the disk file. */ private final long maxElementsOnDisk; /** * Whether the cache is eternal */ private boolean eternal; private int lastElementSize; private int diskSpoolBufferSizeBytes; /** * Creates a disk store. * * @param cache the {@link net.sf.ehcache.Cache} that the store is part of * @param diskPath the directory in which to create data and index files */ public DiskStore(Ehcache cache, String diskPath) { status = Status.STATUS_UNINITIALISED; this.cache = cache; name = cache.getName(); this.diskPath = diskPath; expiryThreadInterval = cache.getDiskExpiryThreadIntervalSeconds(); persistent = cache.isDiskPersistent(); maxElementsOnDisk = cache.getMaxElementsOnDisk(); eternal = cache.isEternal(); diskSpoolBufferSizeBytes = cache.getCacheConfiguration().getDiskSpoolBufferSizeMB() * ONE_MEGABYTE; try { initialiseFiles(); active = true; // Always start up the spool thread spoolAndExpiryThread = new SpoolAndExpiryThread(); spoolAndExpiryThread.start(); status = Status.STATUS_ALIVE; } catch (final Exception e) { // Cleanup on error dispose(); throw new CacheException(name + "Cache: Could not create disk store. " + "Initial cause was " + e.getMessage(), e); } } private void initialiseFiles() throws Exception { // Make sure the cache directory exists final File diskDir = new File(diskPath); if (diskDir.exists() && !diskDir.isDirectory()) { throw new Exception("Store directory \"" + diskDir.getCanonicalPath() + "\" exists and is not a directory."); } if (!diskDir.exists() && !diskDir.mkdirs()) { throw new Exception("Could not create cache directory \"" + diskDir.getCanonicalPath() + "\"."); } dataFile = new File(diskDir, getDataFileName()); indexFile = new File(diskDir, getIndexFileName()); deleteIndexIfNoData(); if (persistent) { //if diskpath contains auto generated string if (diskPath.indexOf(AUTO_DISK_PATH_DIRECTORY_PREFIX) != -1) { LOG.warning("Data in persistent disk stores is ignored for stores from automatically created directories" + " (they start with " + AUTO_DISK_PATH_DIRECTORY_PREFIX + ").\n" + "Remove diskPersistent or resolve the conflicting disk paths in cache configuration.\n" + "Deleting data file " + getDataFileName()); dataFile.delete(); } else if (!readIndex()) { if (LOG.isLoggable(Level.FINE)) { LOG.fine("Index file dirty or empty. Deleting data file " + getDataFileName()); } dataFile.delete(); } } else { if (LOG.isLoggable(Level.FINE)) { LOG.fine("Deleting data file " + getDataFileName()); } dataFile.delete(); indexFile = null; } // Open the data file as random access. The dataFile is created if necessary. randomAccessFile = new RandomAccessFile(dataFile, "rw"); } private void deleteIndexIfNoData() { boolean dataFileExists = dataFile.exists(); boolean indexFileExists = indexFile.exists(); if (!dataFileExists && indexFileExists) { if (LOG.isLoggable(Level.FINE)) { LOG.fine("Matching data file missing for index file. Deleting index file " + getIndexFileName()); } indexFile.delete(); } } /** * Asserts that the store is active. */ private void checkActive() throws CacheException { if (!active) { throw new CacheException(name + " Cache: The Disk store is not active."); } } /** * Gets an {@link Element} from the Disk Store. * * @return The element */ public final synchronized Element get(final Object key) { try { checkActive(); // Check in the spool. Remove if present Element element; synchronized (spoolLock) { element = (Element) spool.remove(key); } if (element != null) { element.updateAccessStatistics(); return element; } // Check if the element is on disk final DiskElement diskElement = (DiskElement) diskElements.get(key); if (diskElement == null) { // Not on disk return null; } element = loadElementFromDiskElement(diskElement); element.updateAccessStatistics(); return element; } catch (Exception exception) { LOG.log(Level.SEVERE, name + "Cache: Could not read disk store element for key " + key + ". Error was " + exception.getMessage(), exception); } return null; } /** * An unsynchronized and very low cost check to see if a key is in the Store. No check is made to see if the Element is expired. * * @param key The Element key * @return true if found. If this method return false, it means that an Element with the given key is definitely not in the MemoryStore. * If it returns true, there is an Element there. An attempt to get it may return null if the Element has expired. */ public final boolean containsKey(Object key) { return diskElements.containsKey(key) || spool.containsKey(key); } private Element loadElementFromDiskElement(DiskElement diskElement) throws IOException, ClassNotFoundException { Element element; // Load the element randomAccessFile.seek(diskElement.position); final byte[] buffer = new byte[diskElement.payloadSize]; randomAccessFile.readFully(buffer); final ByteArrayInputStream instr = new ByteArrayInputStream(buffer); final ObjectInputStream objstr = new ObjectInputStream(instr) { /** * Overridden because of: * Bug 1324221 ehcache DiskStore has issues when used in Tomcat */ protected Class resolveClass(ObjectStreamClass clazz) throws ClassNotFoundException, IOException { try { ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); return Class.forName(clazz.getName(), false, classLoader); } catch (ClassNotFoundException e) { // Use the default as a fallback because of // bug 1517565 - DiskStore loadElementFromDiskElement return super.resolveClass(clazz); } } }; element = (Element) objstr.readObject(); objstr.close(); return element; } /** * Gets an {@link Element} from the Disk Store, without updating statistics * * @return The element */ public final synchronized Element getQuiet(final Object key) { try { checkActive(); // Check in the spool. Remove if present Element element; synchronized (spoolLock) { element = (Element) spool.remove(key); } if (element != null) { //element.updateAccessStatistics(); Don't update statistics return element; } // Check if the element is on disk final DiskElement diskElement = (DiskElement) diskElements.get(key); if (diskElement == null) { // Not on disk return null; } element = loadElementFromDiskElement(diskElement); //element.updateAccessStatistics(); Don't update statistics return element; } catch (Exception e) { LOG.log(Level.SEVERE, name + "Cache: Could not read disk store element for key " + key + ". Initial cause was " + e.getMessage(), e); } return null; } /** * Gets an Array of the keys for all elements in the disk store. * * @return An Object[] of {@link Serializable} keys */ public final synchronized Object[] getKeyArray() { Set elementKeySet; synchronized (diskElements) { elementKeySet = diskElements.keySet(); } Set spoolKeySet; synchronized (spoolLock) { spoolKeySet = spool.keySet(); } Set allKeysSet = new HashSet(elementKeySet.size() + spoolKeySet.size()); allKeysSet.addAll(elementKeySet); allKeysSet.addAll(spoolKeySet); return allKeysSet.toArray(); } /** * Returns the current store size. * */ public final synchronized int getSize() { try { checkActive(); int spoolSize; synchronized (spoolLock) { spoolSize = spool.size(); } int diskSize; synchronized (diskElements) { diskSize = diskElements.size(); } return spoolSize + diskSize; } catch (Exception e) { LOG.log(Level.SEVERE, name + "Cache: Could not determine size of disk store.. Initial cause was " + e.getMessage(), e); return 0; } } /** * Returns the store status. */ public final Status getStatus() { return status; } /** * Puts an element into the disk store. * <p/> * This method is not synchronized. It is however threadsafe. It uses fine-grained * synchronization on the spool. */ public final void put(final Element element) { try { checkActive(); // Spool the element if (spoolAndExpiryThread.isAlive()) { synchronized (spoolLock) { spool.put(element.getObjectKey(), element); } } else { LOG.severe(name + "Cache: Elements cannot be written to disk store because the" + " spool thread has died."); synchronized (spoolLock) { spool.clear(); } } } catch (Exception e) { LOG.log(Level.SEVERE, name + "Cache: Could not write disk store element for " + element.getObjectKey() + ". Initial cause was " + e.getMessage(), e); } } /** * In some circumstances data can be written so quickly to the spool that the VM runs out of memory * while waiting for the spooling to disk. * <p/> * This is a very simple and quick test which estimates the spool size based on the last element's written size. * * @return true if the spool is not being cleared fast enough */ public boolean backedUp() { long estimatedSpoolSize = spool.size() * lastElementSize; boolean backedUp = estimatedSpoolSize > diskSpoolBufferSizeBytes; if (backedUp && LOG.isLoggable(Level.FINEST)) { LOG.finest("A back up on cache puts occurred. Consider increasing diskSpoolBufferSizeMB for cache " + name); } return backedUp; } /** * Removes an item from the disk store. * */ public final synchronized Element remove(final Object key) { Element element; try { checkActive(); // Remove the entry from the spool synchronized (spoolLock) { element = (Element) spool.remove(key); } // Remove the entry from the file. Could be in both places. synchronized (diskElements) { final DiskElement diskElement = (DiskElement) diskElements.remove(key); if (diskElement != null) { element = loadElementFromDiskElement(diskElement); freeBlock(diskElement); } } } catch (Exception exception) { String message = name + "Cache: Could not remove disk store entry for key " + key + ". Error was " + exception.getMessage(); LOG.log(Level.SEVERE, message, exception); throw new CacheException(message); } return element; } /** * Marks a block as free. * * @param diskElement the DiskElement to move to the free space list */ private void freeBlock(final DiskElement diskElement) { totalSize -= diskElement.payloadSize; diskElement.payloadSize = 0; //reset Element meta data diskElement.key = null; diskElement.hitcount = 0; diskElement.expiryTime = 0; freeSpace.add(diskElement); } /** * Remove all of the elements from the store. * <p/> * If there are registered <code>CacheEventListener</code>s they are notified of the expiry or removal * of the <code>Element</code> as each is removed. */ public final synchronized void removeAll() { try { checkActive(); // Ditch all the elements, and truncate the file spool = Collections.synchronizedMap(new HashMap()); diskElements = Collections.synchronizedMap(new HashMap()); freeSpace = Collections.synchronizedList(new ArrayList()); totalSize = 0; randomAccessFile.setLength(0); if (persistent) { indexFile.delete(); indexFile.createNewFile(); } } catch (Exception e) { // Clean up LOG.log(Level.SEVERE, name + " Cache: Could not rebuild disk store. Initial cause was " + e.getMessage(), e); dispose(); } } /** * Shuts down the disk store in preparation for cache shutdown * <p/> * If a VM crash happens, the shutdown hook will not run. The data file and the index file * will be out of synchronisation. At initialisation we always delete the index file * after we have read the elements, so that it has a zero length. On a dirty restart, it still will have * and the data file will automatically be deleted, thus preserving safety. */ public final synchronized void dispose() { if (!active) { return; } // Close the cache try { flush(); //stop the spool thread if (spoolAndExpiryThread != null) { spoolAndExpiryThread.interrupt(); } //Clear in-memory data structures spool.clear(); diskElements.clear(); freeSpace.clear(); if (randomAccessFile != null) { randomAccessFile.close(); } if (!persistent) { LOG.fine("Deleting file " + dataFile.getName()); dataFile.delete(); } } catch (Exception e) { LOG.log(Level.SEVERE, name + "Cache: Could not shut down disk cache. Initial cause was " + e.getMessage(), e); } finally { active = false; randomAccessFile = null; notifyAll(); //release reference to cache cache = null; } } /** * Flush the spool if persistent, so we don't lose any data. * * @throws IOException */ public final void flush() throws IOException { if (persistent) { flushSpool(); writeIndex(); } } /** * both flushing and expiring contend for the same lock on diskElement, so * might as well do them sequentially in the one thread. * <p/> * This thread is protected from Throwables by only calling methods that guard * against these. */ private void spoolAndExpiryThreadMain() { long nextExpiryTime = System.currentTimeMillis(); while (true) { try { Thread.sleep(SPOOL_THREAD_INTERVAL); } catch (InterruptedException e) { LOG.fine("Spool Thread interrupted."); return; } if (!active) { return; } throwableSafeFlushSpoolIfRequired(); if (!active) { return; } nextExpiryTime = throwableSafeExpireElementsIfRequired(nextExpiryTime); } } private long throwableSafeExpireElementsIfRequired(long nextExpiryTime) { long updatedNextExpiryTime = nextExpiryTime; // Expire elements if (!eternal && System.currentTimeMillis() > nextExpiryTime) { try { updatedNextExpiryTime += expiryThreadInterval * MS_PER_SECOND; expireElements(); } catch (Throwable e) { LOG.log(Level.SEVERE, name + " Cache: Could not expire elements from disk due to " + e.getMessage() + ". Continuing...", e); } } return updatedNextExpiryTime; } private void throwableSafeFlushSpoolIfRequired() { if (spool != null && spool.size() != 0) { // Write elements to disk try { flushSpool(); } catch (Throwable e) { LOG.log(Level.SEVERE, name + " Cache: Could not flush elements to disk due to " + e.getMessage() + ". Continuing...", e); } } } /** * Flushes all spooled elements to disk. * Note that the cache is locked for the entire time that the spool is being flushed. * */ private synchronized void flushSpool() throws IOException { if (spool.size() == 0) { return; } Map copyOfSpool = swapSpoolReference(); //does not guarantee insertion order Iterator valuesIterator = copyOfSpool.values().iterator(); while (valuesIterator.hasNext()) { writeOrReplaceEntry(valuesIterator.next()); valuesIterator.remove(); } } private Map swapSpoolReference() { Map copyOfSpool = null; synchronized (spoolLock) { // Copy the reference of the old spool, not the contents. Avoid potential spike in memory usage copyOfSpool = spool; // use a new map making the reference swap above SAFE spool = Collections.synchronizedMap(new HashMap()); } return copyOfSpool; } private void writeOrReplaceEntry(Object object) throws IOException { Element element = (Element) object; if (element == null) { return; } final Serializable key = (Serializable) element.getObjectKey(); removeOldEntryIfAny(key); if (maxElementsOnDisk > 0 && diskElements.size() >= maxElementsOnDisk) { evictLfuDiskElement(); } writeElement(element, key); } private void writeElement(Element element, Serializable key) throws IOException { try { int bufferLength; long expirationTime = element.getExpirationTime(); try { MemoryEfficientByteArrayOutputStream buffer = MemoryEfficientByteArrayOutputStream.serialize(element, estimatedPayloadSize()); bufferLength = buffer.size(); DiskElement diskElement = checkForFreeBlock(bufferLength); // Write the record randomAccessFile.seek(diskElement.position); randomAccessFile.write(buffer.toByteArray(), 0, bufferLength); buffer = null; // Add to index, update stats diskElement.payloadSize = bufferLength; diskElement.key = key; diskElement.expiryTime = expirationTime; diskElement.hitcount = element.getHitCount(); totalSize += bufferLength; lastElementSize = bufferLength; synchronized (diskElements) { diskElements.put(key, diskElement); } } catch (OutOfMemoryError e) { LOG.severe("OutOfMemoryError on serialize: " + key); } } catch (Exception e) { // Catch any exception that occurs during serialization LOG.log(Level.SEVERE, name + "Cache: Failed to write element to disk '" + key + "'. Initial cause was " + e.getMessage(), e); } } private int estimatedPayloadSize() { int size = 0; try { size = (int) (totalSize / diskElements.size()); } catch (Exception e) { // } if (size <= 0) { size = ESTIMATED_MINIMUM_PAYLOAD_SIZE; } return size; } /** * Remove the old entry, if any * * @param key */ private void removeOldEntryIfAny(Serializable key) { final DiskElement oldBlock; synchronized (diskElements) { oldBlock = (DiskElement) diskElements.remove(key); } if (oldBlock != null) { freeBlock(oldBlock); } } private DiskElement checkForFreeBlock(int bufferLength) throws IOException { DiskElement diskElement = findFreeBlock(bufferLength); if (diskElement == null) { diskElement = new DiskElement(); diskElement.position = randomAccessFile.length(); diskElement.blockSize = bufferLength; } return diskElement; } /** * Writes the Index to disk on shutdown * <p/> * The index consists of the elements Map and the freeSpace List * <p/> * Note that the cache is locked for the entire time that the index is being written */ private synchronized void writeIndex() throws IOException { ObjectOutputStream objectOutputStream = null; try { FileOutputStream fout = new FileOutputStream(indexFile); objectOutputStream = new ObjectOutputStream(fout); objectOutputStream.writeObject(diskElements); objectOutputStream.writeObject(freeSpace); } finally { if (objectOutputStream != null) { objectOutputStream.close(); } } } /** * Reads Index to disk on startup. * <p/> * if the index file does not exist, it creates a new one. * <p/> * Note that the cache is locked for the entire time that the index is being written * * @return True if the index was read successfully, false otherwise */ private synchronized boolean readIndex() throws IOException { ObjectInputStream objectInputStream = null; FileInputStream fin = null; boolean success = false; if (indexFile.exists()) { try { fin = new FileInputStream(indexFile); objectInputStream = new ObjectInputStream(fin); diskElements = (Map) objectInputStream.readObject(); freeSpace = (List) objectInputStream.readObject(); success = true; } catch (StreamCorruptedException e) { LOG.severe("Corrupt index file. Creating new index."); } catch (IOException e) { //normal when creating the cache for the first time if (LOG.isLoggable(Level.FINE)) { LOG.fine("IOException reading index. Creating new index. "); } } catch (ClassNotFoundException e) { LOG.log(Level.SEVERE, "Class loading problem reading index. Creating new index. Initial cause was " + e.getMessage(), e); } finally { try { if (objectInputStream != null) { objectInputStream.close(); } else if (fin != null) { fin.close(); } } catch (IOException e) { LOG.severe("Problem closing the index file."); } //Always zero out file. That way if there is a dirty shutdown, the file will still be empty //the next time we start up and readIndex will automatically fail. //If there was a problem reading the index this time we also want to zero it out. createNewIndexFile(); } } else { createNewIndexFile(); } //Return the success flag return success; } private void createNewIndexFile() throws IOException { if (indexFile.exists()) { indexFile.delete(); if (LOG.isLoggable(Level.FINE)) { LOG.fine("Index file " + indexFile + " deleted."); } } if (indexFile.createNewFile()) { if (LOG.isLoggable(Level.FINE)) { LOG.fine("Index file " + indexFile + " created successfully"); } } else { throw new IOException("Index file " + indexFile + " could not created."); } } /** * Removes expired elements. * <p/> * Note that the DiskStore cannot efficiently expire based on TTI. It does it on TTL. However any gets out * of the DiskStore are check for both before return. * */ public void expireElements() { final long now = System.currentTimeMillis(); // Clean up the spool synchronized (spoolLock) { for (Iterator iterator = spool.values().iterator(); iterator.hasNext();) { final Element element = (Element) iterator.next(); if (element.isExpired()) { // An expired element if (LOG.isLoggable(Level.FINE)) { LOG.fine(name + "Cache: Removing expired spool element " + element.getObjectKey()); } iterator.remove(); notifyExpiryListeners(element); } } } Element element = null; RegisteredEventListeners listeners = cache.getCacheEventNotificationService(); synchronized (diskElements) { // Clean up disk elements for (Iterator iterator = diskElements.entrySet().iterator(); iterator.hasNext();) { final Map.Entry entry = (Map.Entry) iterator.next(); final DiskElement diskElement = (DiskElement) entry.getValue(); if (now >= diskElement.expiryTime) { // An expired element if (LOG.isLoggable(Level.FINE)) { LOG.fine(name + "Cache: Removing expired spool element " + entry.getKey() + " from Disk Store"); } iterator.remove(); // only load the element from the file if there is a listener interested in hearing about its expiration if (listeners.hasCacheEventListeners()) { try { element = loadElementFromDiskElement(diskElement); notifyExpiryListeners(element); } catch (Exception exception) { LOG.log(Level.SEVERE, name + "Cache: Could not remove disk store entry for " + entry.getKey() + ". Error was " + exception.getMessage(), exception); } } freeBlock(diskElement); } } } } /** * It is enough that an element is expiring here. Notify even though there might be another * element with the same key elsewhere in the stores. * * @param element */ private void notifyExpiryListeners(Element element) { cache.getCacheEventNotificationService().notifyElementExpiry(element, false); } /** * Allocates a free block. */ private DiskElement findFreeBlock(final int length) { for (int i = 0; i < freeSpace.size(); i++) { final DiskElement element = (DiskElement) freeSpace.get(i); if (element.blockSize >= length) { freeSpace.remove(i); return element; } } return null; } /** * Returns a {@link String} representation of the {@link DiskStore} */ public final String toString() { StringBuffer sb = new StringBuffer(); sb.append("[ dataFile = ").append(dataFile.getAbsolutePath()) .append(", active=").append(active) .append(", totalSize=").append(totalSize) .append(", status=").append(status) .append(", expiryThreadInterval = ").append(expiryThreadInterval) .append(" ]"); return sb.toString(); } /** * Generates a unique directory name for use in automatically creating a diskStorePath where there is a conflict. * * @return a path consisting of {@link #AUTO_DISK_PATH_DIRECTORY_PREFIX} followed by "_" followed by the current * time as a long e.g. ehcache_auto_created_1149389837006 */ public static String generateUniqueDirectory() { return DiskStore.AUTO_DISK_PATH_DIRECTORY_PREFIX + "_" + System.currentTimeMillis(); } /** * A reference to an on-disk elements. * <p/> * Copies of expiryTime and hitcount are held here as a performance optimisation, so * that we do not need to load the data from Disk to get this often used information. * */ private static final class DiskElement implements Serializable, LfuPolicy.Metadata { private static final long serialVersionUID = -717310932566592289L; /** * the file pointer */ private long position; /** * The size used for data. */ private int payloadSize; /** * the size of this element. */ private int blockSize; /** * The key this element is mapped with in DiskElements. This is only a reference * to the key. It is used in DiskElements and therefore the only memory cost is the * reference. */ private Object key; /** * The expiry time in milliseconds */ private long expiryTime; /** * The numbe of times the element has been requested and found in the cache. */ private long hitcount; /** * @return the key of this object */ public Object getObjectKey() { return key; } /** * @return the hit count for the element */ public long getHitCount() { return hitcount; } } /** * A background daemon thread that writes objects to the file. */ private final class SpoolAndExpiryThread extends Thread { public SpoolAndExpiryThread() { super("Store " + name + " Spool Thread"); setDaemon(true); setPriority(Thread.NORM_PRIORITY); } /** * RemoteDebugger thread method. */ public final void run() { spoolAndExpiryThreadMain(); } } /** * @return the total size of the data file and the index file, in bytes. */ public final long getTotalFileSize() { return getDataFileSize() + getIndexFileSize(); } /** * @return the size of the data file in bytes. */ public final long getDataFileSize() { return dataFile.length(); } /** * The design of the layout on the data file means that there will be small gaps created when DiskElements * are reused. * * @return the sparseness, measured as the percentage of space in the Data File not used for holding data */ public final float calculateDataFileSparseness() { return 1 - ((float) getUsedDataSize() / (float) getDataFileSize()); } /** * When elements are deleted, spaces are left in the file. These spaces are tracked and are reused * when new elements need to be written. * <p/> * This method indicates the actual size used for data, excluding holes. It can be compared with * {@link #getDataFileSize()} as a measure of fragmentation. */ public final long getUsedDataSize() { return totalSize; } /** * @return the size of the index file, in bytes. */ public final long getIndexFileSize() { if (indexFile == null) { return 0; } else { return indexFile.length(); } } /** * @return the file name of the data file where the disk store stores data, without any path information. */ public final String getDataFileName() { return name + ".data"; } /** * @return the disk path, which will be dependent on the operating system */ public final String getDataFilePath() { return diskPath; } /** * @return the file name of the index file, which maintains a record of elements and their addresses * on the data file, without any path information. */ public final String getIndexFileName() { return name + ".index"; } /** * The spool thread is started when the disk store is created. * <p/> * It will continue to run until the {@link #dispose()} method is called, * at which time it should be interrupted and then die. * * @return true if the spoolThread is still alive. */ public final boolean isSpoolThreadAlive() { if (spoolAndExpiryThread == null) { return false; } else { return spoolAndExpiryThread.isAlive(); } } private void evictLfuDiskElement() { synchronized (diskElements) { DiskElement diskElement = findRelativelyUnused(); diskElements.remove(diskElement.key); notifyEvictionListeners(diskElement); freeBlock(diskElement); } } /** * Find a "relatively" unused disk element, but not the element just added. */ private DiskElement findRelativelyUnused() { LfuPolicy.Metadata[] elements = sampleElements(diskElements); LfuPolicy.Metadata metadata = LfuPolicy.leastHit(elements, null); return (DiskElement) metadata; } /** * Uses random numbers to sample the entire map. * * @return an array of sampled elements */ private LfuPolicy.Metadata[] sampleElements(Map map) { int[] offsets = LfuPolicy.generateRandomSample(map.size()); DiskElement[] elements = new DiskElement[offsets.length]; Iterator iterator = map.values().iterator(); for (int i = 0; i < offsets.length; i++) { for (int j = 0; j < offsets[i]; j++) { iterator.next(); } elements[i] = (DiskElement) iterator.next(); } return elements; } private void notifyEvictionListeners(DiskElement diskElement) { RegisteredEventListeners listeners = cache.getCacheEventNotificationService(); // only load the element from the file if there is a listener interested in hearing about its expiration if (listeners.hasCacheEventListeners()) { Element element = null; try { element = loadElementFromDiskElement(diskElement); cache.getCacheEventNotificationService().notifyElementEvicted(element, false); } catch (Exception exception) { LOG.log(Level.SEVERE, name + "Cache: Could not notify disk store eviction of " + element.getObjectKey() + ". Error was " + exception.getMessage(), exception); } } } }

The table below shows all metrics for DiskStore.java.

MetricValueDescription
BLOCKS178.00Number of blocks
BLOCK_COMMENT 0.00Number of block comment lines
COMMENTS329.00Comment lines
COMMENT_DENSITY 0.60Comment density
COMPARISONS89.00Number of comparison operators
CYCLOMATIC141.00Cyclomatic complexity
DECL_COMMENTS56.00Comments in declarations
DOC_COMMENT283.00Number of javadoc comment lines
ELOC545.00Effective lines of code
EXEC_COMMENTS43.00Comments in executable code
EXITS126.00Procedure exits
FUNCTIONS54.00Number of function declarations
HALSTEAD_DIFFICULTY97.07Halstead difficulty
HALSTEAD_EFFORT 0.00Halstead effort
INTERFACE_COMPLEXITY113.00Interface complexity
JAVA0001 0.00JAVA0001 Package name does not contain only lower case letters
JAVA0002 0.00JAVA0002 Package name does not begin with a top level domain name or country code
JAVA0003 0.00JAVA0003 Minimize use of on-demand (.*) imports
JAVA0004 0.00JAVA0004 Unnecessary import from java.lang
JAVA0005 1.00JAVA0005 Imports not in specified order
JAVA0006 0.00JAVA0006 Empty finally block
JAVA0007 0.00JAVA0007 Should not declare public field
JAVA0008 0.00JAVA0008 Empty catch block
JAVA0009 0.00JAVA0009 Protected member in final class
JAVA0010 0.00JAVA0010 Non-instantiable class does not contain a non-private static member
JAVA0011 0.00JAVA0011 Abstract class does not contain an abstract method
JAVA0012 0.00JAVA0012 Non-constructor method with same name as declaring class
JAVA0013 0.00JAVA0013 Non-blank final field is not static
JAVA0014 0.00JAVA0014 Class with only static members has non-private constructor
JAVA0015 0.00JAVA0015 Package class contains public nested type
JAVA0016 0.00JAVA0016 Abstract class contains public constructor
JAVA0017 0.00JAVA0017 Class name does not have required form
JAVA0018 0.00JAVA0018 Method name does not have required form
JAVA0019 0.00JAVA0019 Interface name does not have required form
JAVA0020 0.00JAVA0020 Field name does not have required form
JAVA0021 0.00JAVA0021 Interface method name does not have required form
JAVA0022 0.00JAVA0022 Static final field name does not have required form
JAVA0023 0.00JAVA0023 Empty finalize method
JAVA0024 0.00JAVA0024 Empty class
JAVA0025 0.00JAVA0025 Method override is empty
JAVA0026 0.00JAVA0026 Finalize method with parameters
JAVA0029 0.00JAVA0029 Private method not used
JAVA0030 0.00JAVA0030 Private field not used
JAVA0031 0.00JAVA0031 Case statement not properly closed
JAVA0032 0.00JAVA0032 Switch statement missing default
JAVA0033 0.00JAVA0033 default: not last case in switch statement
JAVA0034 0.00JAVA0034 Missing braces in if statement
JAVA0035 0.00JAVA0035 Missing braces in for statement
JAVA0036 0.00JAVA0036 Missing braces in while statement
JAVA0038 0.00JAVA0038 Non-case label in switch statement
JAVA0039 0.00JAVA0039 Break statement with label
JAVA0040 0.00JAVA0040 Switch statement contains N cases (maximum: M)
JAVA0041 0.00JAVA0041 Nested synchronized block
JAVA0042 0.00JAVA0042 Empty synchronized statement
JAVA0043 0.00JAVA0043 Inner class does not use outer class
JAVA0044 0.00JAVA0044 Serializable class with no instance variables
JAVA0045 0.00JAVA0045 Serializable class with only transient fields
JAVA0046 0.00JAVA0046 Name of class not derived from Exception ends with 'Exception'
JAVA0047 0.00JAVA0047 Serializable class derives from invalid base class
JAVA0048 0.00JAVA0048 Name of class derived from Exception does not end with 'Exception'
JAVA0049 2.00JAVA0049 Nested block at depth N (maximum: M)
JAVA0050 0.00JAVA0050 Class derives from java.lang.Error
JAVA0051 0.00JAVA0051 Class derives from java.lang.RuntimeException
JAVA0052 0.00JAVA0052 Class derives from java.lang.Throwable
JAVA0053 0.00JAVA0053 Unused label
JAVA0054 0.00JAVA0054 Inheritance depth N exceeds maximum M
JAVA0055 0.00JAVA0055 Class should be interface
JAVA0056 0.00JAVA0056 Unnecessary abstract modifier for interface or annotation
JAVA0057 0.00JAVA0057 Unnecessary default constructor
JAVA0058 0.00JAVA0058 Constructor calls super()
JAVA0059 0.00JAVA0059 Method override only calls super()
JAVA0061 0.00JAVA0061 Inaccessible member in anonymous class
JAVA0062 0.00JAVA0062 Public class missing public member or protected constructor
JAVA0063 0.00JAVA0063 Identifier name should not contain '$'
JAVA0064 0.00JAVA0064 N variations of identifier name (maximum: M)
JAVA0065 1.00JAVA0065 Unnecessary final modifier for method in final class
JAVA0066 0.00JAVA0066 Unnecessary modifier for interface nested type
JAVA0067 0.00JAVA0067 Array descriptor on identifier name
JAVA0068 0.00JAVA0068 Modifiers not declared in recommended order
JAVA0071 0.00JAVA0071 Strings compared with ==
JAVA0073 0.00JAVA0073 Integer division in floating-point context
JAVA0074 0.00JAVA0074 Use of Object.notify()
JAVA0075 0.00JAVA0075 Method parameter hides field
JAVA0076 0.00JAVA0076 Use of magic number
JAVA0077 4.00JAVA0077 Private field not used in declaring class
JAVA0078 0.00JAVA0078 Floating point values compared with ==
JAVA0079 0.00JAVA0079 Use of instance to reference static member
JAVA0080 0.00JAVA0080 Import declaration not used
JAVA0081 0.00JAVA0081 Boolean literal in comparison
JAVA0082 0.00JAVA0082 Unnecessary widening cast
JAVA0083 0.00JAVA0083 Unnecessary instanceof test
JAVA0084 0.00JAVA0084 Should use compound assignment operator
JAVA0085 0.00JAVA0085 Use of sun.* class
JAVA0087 1.00JAVA0087 Use of Thread.sleep()
JAVA0089 0.00JAVA0089 Use of restricted package
JAVA0092 0.00JAVA0092 Use of restricted type
JAVA0093 0.00JAVA0093 Redundant assignment
JAVA0094 0.00JAVA0094 Field hides a superclass field
JAVA0095 0.00JAVA0095 Uninitialized private field
JAVA0096 0.00JAVA0096 Field in nested class hides outer field
JAVA0098 0.00JAVA0098 Minimize use of implicit field initializers
JAVA0100 1.00JAVA0100 Class contains N non-final fields (maximum: M)
JAVA0101 0.00JAVA0101 Unnecessary modifier for field in interface
JAVA0102 0.00JAVA0102 Last statement in finalize() not super.finalize()
JAVA0103 0.00JAVA0103 Explicit call to finalize()
JAVA0104 0.00JAVA0104 finalize() only calls super.finalize()
JAVA0105 0.00JAVA0105 Duplicate import declaration
JAVA0106 0.00JAVA0106 Unnecessary import from current package
JAVA0108 2.00JAVA0108 Incorrect javadoc: no @param tag for 'parameter'
JAVA0109 0.00JAVA0109 Incorrect javadoc: no parameter 'parameter'
JAVA0110 8.00JAVA0110 Incorrect javadoc: no @return tag
JAVA0111 0.00JAVA0111 Incorrect javadoc: @return tag for void method
JAVA0112 0.00JAVA0112 Incorrect javadoc: no exception 'exception' in throws
JAVA0113 0.00JAVA0113 Incorrect javadoc: no @author tag
JAVA0114 0.00JAVA0114 Incorrect javadoc: no @version tag
JAVA0115 6.00JAVA0115 Incorrect javadoc: no @throws or @exception tag for 'exception'
JAVA0116 0.00JAVA0116 Missing javadoc: field 'field'
JAVA0117 1.00JAVA0117 Missing javadoc: method 'method'
JAVA0118 0.00JAVA0118 Missing javadoc: type 'type'
JAVA0119 0.00JAVA0119 Control variable changed within body of for loop
JAVA0123 0.00JAVA0123 Use all three components of for loop
JAVA0125 0.00JAVA0125 Continue statement with label
JAVA0126 1.00JAVA0126 Method declares unchecked exception in throws
JAVA0128 1.00JAVA0128 Public constructor in non-public class
JAVA0130 1.00JAVA0130 Non-static method does not use instance fields
JAVA0131 0.00JAVA0131 Compatible method does not override base
JAVA0132 0.00JAVA0132 Method overload with compatible signature
JAVA0133 0.00JAVA0133 Non-synchronized method overrides synchronized method
JAVA0135 0.00JAVA0135 Only one of Object.equals and Object.hashCode defined: missing 'method'
JAVA0136 1.00JAVA0136 N methods defined in class (maximum: M)
JAVA0137 1.00JAVA0137 Non-abstract class missing constructor
JAVA0138 0.00JAVA0138 N parameters defined for method (maximum: M)
JAVA0139 0.00JAVA0139 Definition of main other than public static void main(java.lang.String[])
JAVA0141 0.00JAVA0141 Unnecessary modifier for method in interface
JAVA014310.00JAVA0143 Synchronized method
JAVA0144 4.00JAVA0144 Line exceeds maximum M characters
JAVA0145 0.00JAVA0145 Tab character used in source file
JAVA0150 0.00JAVA0150 java.lang.Error (or subclass) thrown
JAVA0153 0.00JAVA0153 Inefficient conversion of integer to string
JAVA0159 0.00JAVA0159 Inefficient conversion of string to integer
JAVA0160 1.00JAVA0160 Method does not throw specified exception
JAVA0161 0.00JAVA0161 Conditional wait() not in loop
JAVA0163 0.00JAVA0163 Empty statement
JAVA0165 0.00JAVA0165 Conflicting return statement in finally block
JAVA016614.00JAVA0166 Generic exception caught
JAVA0167 0.00JAVA0167 ThreadDeath not rethrown
JAVA0169 0.00JAVA0169 Unnecessary catch block: exception 'exception'
JAVA0170 3.00JAVA0170 Caught exception not derived from java.lang.Exception
JAVA0171 0.00JAVA0171 Unused local variable
JAVA0173 0.00JAVA0173 Unused method parameter
JAVA0174 1.00JAVA0174 Assigned local variable never used
JAVA0175 0.00JAVA0175 Successive assignment to variable
JAVA0176 0.00JAVA0176 Local variable name does not have required form
JAVA017710.00JAVA0177 Variable declaration missing initializer
JAVA0179 0.00JAVA0179 Local variable hides visible field
JAVA0233 0.00JAVA0233 Definition of serialVersionUID other than 'private static final long serialVersionUID'
JAVA0234 0.00JAVA0234 Class is Serializable but does not define serialVersionUID
JAVA0235 0.00JAVA0235 Class defines serialVersionUID but does not implement Serializable
JAVA0236 0.00JAVA0236 Attempt to clone an object which does not implement Cloneable
JAVA0237 0.00JAVA0237 Class implements Cloneable but does not have public clone method
JAVA0238 0.00JAVA0238 Clone method does not call super.clone()
JAVA0239 0.00JAVA0239 Class declares 'readObject' or 'writeObject' but does not implement Serializable
JAVA0240 0.00JAVA0240 Serializable class which declares readObject or writeObject but not both
JAVA0241 0.00JAVA0241 'readObject' or 'writeObject' should be declared private in Serializable class
JAVA0242 0.00JAVA0242 Transient field in non-Serializable class
JAVA0243 0.00JAVA0243 'readResolve' or 'writeReplace' should be declared private or protected
JAVA0244 0.00JAVA0244 Field or method name in subclass differs only by case from inherited field or method
JAVA0245 0.00JAVA0245 JUnit TestCase with non-trivial constructor
JAVA0246 0.00JAVA0246 JUnit assertXXX statement missing message parameter
JAVA0247 0.00JAVA0247 JUnit 'setUp()' and 'tearDown()' should call super method
JAVA0248 0.00JAVA0248 JUnit method 'setUp' or 'tearDown' with incorrect signature
JAVA0249 0.00JAVA0249 JUnit TestCase 'suite()' should be declared static
JAVA0250 0.00JAVA0250 JUnit TestCase declares testXXX method with incorrect signature
JAVA0251 0.00JAVA0251 Use '%n' for line breaks in printf/format for platform independence
JAVA0252 0.00JAVA0252 'enum' is a Java 1.5 reserved word
JAVA0253 0.00JAVA0253 Not all enum constants consumed in switch statement
JAVA0254 0.00JAVA0254 Use enhanced for loop construct instead of Iterator
JAVA0255 0.00JAVA0255 Result of method invocation not used
JAVA0256 0.00JAVA0256 Assignment of external collection/array to field
JAVA0257 0.00JAVA0257 Use of 'Constant Interface' anti-pattern
JAVA0258 0.00JAVA0258 Implement Iterable for foreach compatibility
JAVA0259 0.00JAVA0259 Return of collection/array field
JAVA0260 0.00JAVA0260 Use 'enum' instead of Enumerated Type pattern
JAVA0261 0.00JAVA0261 Use specialized Enum collection types
JAVA0262 0.00JAVA0262 Use of char in integer context
JAVA0263 0.00JAVA0263 Long literal ends with 'l' instead of 'L'
JAVA0264 1.00JAVA0264 Integer math in long context - check for overflow
JAVA0265 0.00JAVA0265 Use of Throwable.printStackTrace()
JAVA0266 0.00JAVA0266 Use of System.out
JAVA0267 0.00JAVA0267 Use of System.err
JAVA0269 0.00JAVA0269 Contents of StringBuffer never used
JAVA0270 0.00JAVA0270 Use Java 5.0 enhanced for loop construct to iterate over all elements in an array
JAVA0271 0.00JAVA0271 Minimize use of on-demand (.*) static imports
JAVA0272 0.00JAVA0272 Thread.run() called
JAVA0273 1.00JAVA0273 Non-final derivative of Thread calls start() in constructor
JAVA0274 0.00JAVA0274 Serializable class has a synchronized readObject()
JAVA0275 0.00JAVA0275 Serializable class has a synchronized writeObject() and no other synchronized methods
JAVA0276 0.00JAVA0276 Unnecessary use of String constructor
JAVA0277 0.00JAVA0277 Iterator.next() implementation does not throw NoSuchElementException
JAVA0278 0.00JAVA0278 Unnecessary use of Boolean constructor
JAVA0279 0.00JAVA0279 Serialization method readObject or readObjectNoData calls an overridable method
JAVA0280 0.00JAVA0280 IllegalMonitorStateException caught
JAVA0281 0.00JAVA0281 Iterator.next() not called in loop
JAVA0282 2.00JAVA0282 Call to Iterator.next() in loop which does not test Iterator.hasNext()
JAVA0283 0.00JAVA0283 Control variable not updated in loop body
JAVA0284 0.00JAVA0284 Explicit garbage collection
JAVA0285 0.00JAVA0285 Dereference of potentially null variable
JAVA0286 1.00JAVA0286 Dereference of null variable
JAVA0287 0.00JAVA0287 Unnecessary null check
JAVA0288 0.00JAVA0288 Inconsistent null check
LINES1176.00Number of lines in the source file
LINE_COMMENT46.00Number of line comments
LOC694.00Lines of code
LOGICAL_LINES354.00Number of statements
LOOPS 7.00Number of loops
NEST_DEPTH 6.00Maximum nesting depth
OPERANDS1382.00Number of operands
OPERATORS2875.00Number of operators
PARAMS20.00Number of formal parameter declarations
PROGRAM_LENGTH4257.00Halstead program length
PROGRAM_VOCAB479.00Halstead program vocabulary
PROGRAM_VOLUME 0.00Halstead program volume
RETURNS93.00Number of return points from functions
SIZE40381.00Size of the file in bytes
UNIQUE_OPERANDS420.00Number of unique operands
UNIQUE_OPERATORS59.00Number of unique operators
WHITESPACE153.00Number of whitespace lines