ehcache

net.sf.ehcache.store.disk
Class DiskStore

java.lang.Object
  extended by net.sf.ehcache.store.AbstractStore
      extended by net.sf.ehcache.store.disk.DiskStore
All Implemented Interfaces:
AuthoritativeTier, Store, StripedReadWriteLockProvider

public final class DiskStore
extends AbstractStore
implements StripedReadWriteLockProvider, AuthoritativeTier

Implements a persistent-to-disk store.

All new elements are automatically scheduled for writing to disk.

Author:
Chris Dennis, Ludovic Orban

Field Summary
 
Fields inherited from class net.sf.ehcache.store.AbstractStore
attributeExtractors, searchManager
 
Fields inherited from interface net.sf.ehcache.store.Store
CLUSTER_COHERENT, NODE_COHERENT
 
Method Summary
 boolean bufferFull()
          Some store types, such as the disk stores can fill their write buffers if puts come in too fast.
 void changeDiskCapacity(int newCapacity)
          Change the disk capacity, in number of elements
 void clearFaultedBit()
          Marks all entries has flushed (i.e.
 boolean containsKey(Object key)
          A check to see if a key is in the Store.
 boolean containsKeyInMemory(Object key)
          A check to see if a key is in the Store and is currently held in memory.
 boolean containsKeyOffHeap(Object key)
          A check to see if a key is in the Store and is currently held off-heap.
 boolean containsKeyOnDisk(Object key)
          A check to see if a key is in the Store and is currently held on disk.
static DiskStore create(Cache cache)
          Creates a persitent-to-disk store for the given cache, using the given disk path.
static DiskStore create(Ehcache cache, Pool onHeapPool, Pool onDiskPool)
          Creates a persitent-to-disk store for the given cache, using the given disk path.
static Store createCacheStore(Ehcache cache, Pool onHeapPool, Pool onDiskPool)
          Create a DiskBackedMemoryStore instance
 StripedReadWriteLock createStripedReadWriteLock()
          Will create a StripedReadWriteLock always using the same spreading function
 void dispose()
          Prepares for shutdown.
 boolean evict(Object key, DiskStorageFactory.DiskSubstitute substitute)
          Remove the matching mapping.
 Element evictElement(Object key, DiskStorageFactory.DiskSubstitute substitute)
          Remove the matching mapping.
 void expireElements()
          Expire all elements.
 Element fault(Object key, boolean updateStats)
          Marks the entry as not evictable and returns it atomically
 boolean fault(Object key, net.sf.ehcache.store.disk.DiskStorageFactory.Placeholder expect, DiskStorageFactory.DiskMarker fault)
          Atomically switch (CAS) the expect representation of this element for the fault representation.
 void flush()
          Flush elements to persistent store.
 boolean flush(Element element)
          This marks the entry as evictable again and updates relevant access stats
 Element get(Object key)
          Gets an item from the cache.
 File getDataFile()
          Return a reference to the data file backing this store.
 File getIndexFile()
          Return a reference to the index file for this store.
 Policy getInMemoryEvictionPolicy()
          
 int getInMemorySize()
          Returns the current local in-memory store size
 long getInMemorySizeInBytes()
          Gets the size of the in-memory portion of the store, in bytes.
 Object getInternalContext()
          This should not be used, and will generally return null
 List getKeys()
          Gets an Array of the keys for all elements in the disk store.
 Object getMBean()
          Optional implementation specific MBean exposed by the store.
 int getOffHeapSize()
          Returns the current local off-heap store size
 long getOffHeapSizeInBytes()
          Gets the size of the off-heap portion of the store, in bytes.
 int getOnDiskSize()
          Returns the current local on-disk store size
 long getOnDiskSizeInBytes()
          Gets the size of the on-disk portion of the store, in bytes.
 Element getQuiet(Object key)
          Gets an Element from the Store, without updating statistics
 List<DiskStorageFactory.DiskSubstitute> getRandomSample(ElementSubstituteFilter factory, int sampleSize, Object keyHint)
          Select a random sample of elements generated by the supplied factory.
 int getSize()
          Returns the current local store size
 Status getStatus()
          Returns the cache status.
 int getTerracottaClusteredSize()
          Returns the current Terracotta clustered store size
 boolean isFaulted(Object key)
          Verifies if the mapping for a key is marked as faulted
 Set<Object> keySet()
          Get a set view of the keys in this store
 boolean put(Element element)
          Puts an item into the store.
 boolean putFaulted(Element element)
          Stupid "implicit" contract in tests that dictates that entries put, will be in highest tier!
 Element putIfAbsent(Element element)
          Put an element in the store if no element is currently mapped to the elements key.
 boolean putRawIfAbsent(Object key, DiskStorageFactory.DiskMarker encoded)
          Put the given encoded element directly into the store
 boolean putWithWriter(Element element, CacheWriterManager writerManager)
          Puts an item into the store and the cache writer manager in an atomic operation
 Element remove(Object key)
          Removes an item from the cache.
 void removeAll()
          Remove all of the elements from the store.
 Element removeElement(Element element, ElementValueComparator comparator)
          Remove the Element mapped to the key for the supplied element if the value of the supplied Element is equal to the value of the cached Element.
 Element removeWithWriter(Object key, CacheWriterManager writerManager)
          Removes an item from the store and the cache writer manager in an atomic operation.
 Element replace(Element element)
          Replace the cached element only if an Element is currently cached for this key
 boolean replace(Element old, Element element, ElementValueComparator comparator)
          Replace the cached element only if the value of the current Element is equal to the value of the supplied old Element.
 void setInMemoryEvictionPolicy(Policy policy)
          Sets the eviction policy strategy.
 Object unretrievedGet(Object key)
          Return the unretrieved (undecoded) value for this key
 
Methods inherited from class net.sf.ehcache.store.AbstractStore
addStoreListener, executeQuery, getAll, getAllQuiet, getEventListenerList, getSearchAttribute, getSearchAttributes, hasAbortedSizeOf, isCacheCoherent, isClusterCoherent, isNodeCoherent, putAll, recalculateSize, removeAll, removeStoreListener, setAttributeExtractors, setNodeCoherent, waitUntilClusterCoherent
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface net.sf.ehcache.store.Store
addStoreListener, executeQuery, getAll, getAllQuiet, getSearchAttribute, getSearchAttributes, hasAbortedSizeOf, isCacheCoherent, isClusterCoherent, isNodeCoherent, putAll, recalculateSize, removeAll, removeStoreListener, setAttributeExtractors, setNodeCoherent, waitUntilClusterCoherent
 

Method Detail

create

public static DiskStore create(Ehcache cache,
                               Pool onHeapPool,
                               Pool onDiskPool)
Creates a persitent-to-disk store for the given cache, using the given disk path.

Parameters:
cache - cache that fronts this store
onHeapPool - pool to track heap usage
onDiskPool - pool to track disk usage
Returns:
a fully initialized store

create

public static DiskStore create(Cache cache)
Creates a persitent-to-disk store for the given cache, using the given disk path. Heap and disk usage are not tracked by the returned store.

Parameters:
cache - cache that fronts this store
Returns:
a fully initialized store

createCacheStore

public static Store createCacheStore(Ehcache cache,
                                     Pool onHeapPool,
                                     Pool onDiskPool)
Create a DiskBackedMemoryStore instance

Parameters:
cache - the cache
onHeapPool - the pool tracking on-heap usage
onDiskPool - the pool tracking on-disk usage
Returns:
a DiskBackedMemoryStore instance

createStripedReadWriteLock

public StripedReadWriteLock createStripedReadWriteLock()
Will create a StripedReadWriteLock always using the same spreading function

Specified by:
createStripedReadWriteLock in interface StripedReadWriteLockProvider
Returns:
a newly created StripedReadWriteLock

fault

public Element fault(Object key,
                     boolean updateStats)
Description copied from interface: AuthoritativeTier
Marks the entry as not evictable and returns it atomically

Specified by:
fault in interface AuthoritativeTier
Returns:

putFaulted

public boolean putFaulted(Element element)
Description copied from interface: AuthoritativeTier
Stupid "implicit" contract in tests that dictates that entries put, will be in highest tier!

Specified by:
putFaulted in interface AuthoritativeTier
Returns:

flush

public boolean flush(Element element)
Description copied from interface: AuthoritativeTier
This marks the entry as evictable again and updates relevant access stats

Specified by:
flush in interface AuthoritativeTier
Returns:

isFaulted

public boolean isFaulted(Object key)
Verifies if the mapping for a key is marked as faulted

Parameters:
key - the key to check the mapping for
Returns:
true if faulted, false otherwise (including no mapping)

changeDiskCapacity

public void changeDiskCapacity(int newCapacity)
Change the disk capacity, in number of elements

Parameters:
newCapacity - the new max elements on disk

bufferFull

public boolean bufferFull()
Some store types, such as the disk stores can fill their write buffers if puts come in too fast. The thread will wait for a short time before checking again.

Specified by:
bufferFull in interface Store
Returns:
true if the store write buffer is backed up.

containsKeyInMemory

public boolean containsKeyInMemory(Object key)
A check to see if a key is in the Store and is currently held in memory.

Specified by:
containsKeyInMemory in interface Store
Parameters:
key - The Element key
Returns:
true if found. No check is made to see if the Element is expired.

containsKeyOffHeap

public boolean containsKeyOffHeap(Object key)
A check to see if a key is in the Store and is currently held off-heap.

Specified by:
containsKeyOffHeap in interface Store
Parameters:
key - The Element key
Returns:
true if found. No check is made to see if the Element is expired.

containsKeyOnDisk

public boolean containsKeyOnDisk(Object key)
A check to see if a key is in the Store and is currently held on disk.

Specified by:
containsKeyOnDisk in interface Store
Parameters:
key - The Element key
Returns:
true if found. No check is made to see if the Element is expired.

expireElements

public void expireElements()
Expire all elements.

Specified by:
expireElements in interface Store

flush

public void flush()
           throws IOException
Flush elements to persistent store.

Specified by:
flush in interface Store
Throws:
IOException - if any IO error occurs

getInMemoryEvictionPolicy

public Policy getInMemoryEvictionPolicy()

Specified by:
getInMemoryEvictionPolicy in interface Store
Returns:
the current eviction policy. This may not be the configured policy, if it has been dynamically set.
See Also:
Store.setInMemoryEvictionPolicy(Policy)

getInMemorySize

public int getInMemorySize()
Returns the current local in-memory store size

Specified by:
getInMemorySize in interface Store
Returns:
the count of the Elements in the Store and in-memory on the local machine

getInMemorySizeInBytes

public long getInMemorySizeInBytes()
Gets the size of the in-memory portion of the store, in bytes.

This method may be expensive to run, depending on implementation. Implementers may choose to return an approximate size.

Specified by:
getInMemorySizeInBytes in interface Store
Returns:
the approximate in-memory size of the store in bytes

getOffHeapSize

public int getOffHeapSize()
Returns the current local off-heap store size

Specified by:
getOffHeapSize in interface Store
Returns:
the count of the Elements in the Store and off-heap on the local machine

getOffHeapSizeInBytes

public long getOffHeapSizeInBytes()
Gets the size of the off-heap portion of the store, in bytes.

Specified by:
getOffHeapSizeInBytes in interface Store
Returns:
the approximate off-heap size of the store in bytes

getOnDiskSize

public int getOnDiskSize()
Returns the current local on-disk store size

Specified by:
getOnDiskSize in interface Store
Returns:
the count of the Elements in the Store and on-disk on the local machine

getOnDiskSizeInBytes

public long getOnDiskSizeInBytes()
Gets the size of the on-disk portion of the store, in bytes.

Specified by:
getOnDiskSizeInBytes in interface Store
Returns:
the on-disk size of the store in bytes

getTerracottaClusteredSize

public int getTerracottaClusteredSize()
Returns the current Terracotta clustered store size

Specified by:
getTerracottaClusteredSize in interface Store
Returns:
the count of the Elements in the Store across the cluster

setInMemoryEvictionPolicy

public void setInMemoryEvictionPolicy(Policy policy)
Sets the eviction policy strategy. The Store will use a policy at startup. The store may allow changing the eviction policy strategy dynamically. Otherwise implementations will throw an exception if this method is called.

Specified by:
setInMemoryEvictionPolicy in interface Store
Parameters:
policy - the new policy

getDataFile

public File getDataFile()
Return a reference to the data file backing this store.

Returns:
a reference to the data file backing this store.

getIndexFile

public File getIndexFile()
Return a reference to the index file for this store.

Returns:
a reference to the index file for this store.

getMBean

public Object getMBean()
Optional implementation specific MBean exposed by the store.

Specified by:
getMBean in interface Store
Returns:
implementation specific management bean

put

public boolean put(Element element)
Puts an item into the store.

Specified by:
put in interface Store
Returns:
true if this is a new put for the key or element is null. Returns false if it was an update.

putWithWriter

public boolean putWithWriter(Element element,
                             CacheWriterManager writerManager)
Puts an item into the store and the cache writer manager in an atomic operation

Specified by:
putWithWriter in interface Store
Returns:
true if this is a new put for the key or element is null. Returns false if it was an update.

get

public Element get(Object key)
Gets an item from the cache.

Specified by:
get in interface Store

getQuiet

public Element getQuiet(Object key)
Gets an Element from the Store, without updating statistics

Specified by:
getQuiet in interface Store
Returns:
The element

unretrievedGet

public Object unretrievedGet(Object key)
Return the unretrieved (undecoded) value for this key

Parameters:
key - key to lookup
Returns:
Element or ElementSubstitute

putRawIfAbsent

public boolean putRawIfAbsent(Object key,
                              DiskStorageFactory.DiskMarker encoded)
                       throws IllegalArgumentException
Put the given encoded element directly into the store

Parameters:
key - the key of the element
encoded - the encoded element
Returns:
true if the encoded element was installed
Throws:
IllegalArgumentException - if the supplied key is already present

getKeys

public List getKeys()
Gets an Array of the keys for all elements in the disk store.

Specified by:
getKeys in interface Store
Returns:
An List of Serializable keys

keySet

public Set<Object> keySet()
Get a set view of the keys in this store

Returns:
a set view of the keys in this store

remove

public Element remove(Object key)
Removes an item from the cache.

Specified by:
remove in interface Store

removeWithWriter

public Element removeWithWriter(Object key,
                                CacheWriterManager writerManager)
Removes an item from the store and the cache writer manager in an atomic operation.

Specified by:
removeWithWriter in interface Store

removeAll

public void removeAll()
Remove all of the elements from the store.

If there are registered CacheEventListeners they are notified of the expiry or removal of the Element as each is removed.

Specified by:
removeAll in interface Store

dispose

public void dispose()
Prepares for shutdown.

Specified by:
dispose in interface Store

clearFaultedBit

public void clearFaultedBit()
Marks all entries has flushed (i.e. not faulted)


getSize

public int getSize()
Returns the current local store size

Specified by:
getSize in interface Store
Returns:
the count of the Elements in the Store on the local machine

getStatus

public Status getStatus()
Returns the cache status.

Specified by:
getStatus in interface Store

containsKey

public boolean containsKey(Object key)
A check to see if a key is in the Store.

Specified by:
containsKey in interface Store
Parameters:
key - The Element key
Returns:
true if found. No check is made to see if the Element is expired. 1.2

getInternalContext

public Object getInternalContext()
This should not be used, and will generally return null

Specified by:
getInternalContext in interface Store
Returns:
some internal context (probably null)

putIfAbsent

public Element putIfAbsent(Element element)
                    throws NullPointerException
Put an element in the store if no element is currently mapped to the elements key.

Specified by:
putIfAbsent in interface Store
Parameters:
element - element to be added
Returns:
the element previously cached for this key, or null if none.
Throws:
NullPointerException - if the element is null, or has a null key

removeElement

public Element removeElement(Element element,
                             ElementValueComparator comparator)
                      throws NullPointerException
Remove the Element mapped to the key for the supplied element if the value of the supplied Element is equal to the value of the cached Element. This is a CAS operation. It is consistent even against a distributed cache that is not coherent. If the old value is stale when this operation is attempted the remove does not take place.

Specified by:
removeElement in interface Store
Parameters:
element - Element to be removed
comparator - ElementValueComparator to use to compare elements
Returns:
the Element removed or null if no Element was removed
Throws:
NullPointerException - if the element is null, or has a null key

replace

public boolean replace(Element old,
                       Element element,
                       ElementValueComparator comparator)
                throws NullPointerException,
                       IllegalArgumentException
Replace the cached element only if the value of the current Element is equal to the value of the supplied old Element.

Specified by:
replace in interface Store
Parameters:
old - Element to be test against
element - Element to be cached
comparator - ElementValueComparator to use to compare elements
Returns:
true is the Element was replaced
Throws:
NullPointerException - if the either Element is null or has a null key
IllegalArgumentException - if the two Element keys are non-null but not equal

replace

public Element replace(Element element)
                throws NullPointerException
Replace the cached element only if an Element is currently cached for this key

Specified by:
replace in interface Store
Parameters:
element - Element to be cached
Returns:
the Element previously cached for this key, or null if no Element was cached
Throws:
NullPointerException - if the Element is null or has a null key

fault

public boolean fault(Object key,
                     net.sf.ehcache.store.disk.DiskStorageFactory.Placeholder expect,
                     DiskStorageFactory.DiskMarker fault)
Atomically switch (CAS) the expect representation of this element for the fault representation.

A successful switch will return true, and free the replaced element/element-proxy. A failed switch will return false and free the element/element-proxy which was not installed.

Parameters:
key - key to which this element (proxy) is mapped
expect - element (proxy) expected
fault - element (proxy) to install
Returns:
true if fault was installed

evict

public boolean evict(Object key,
                     DiskStorageFactory.DiskSubstitute substitute)
Remove the matching mapping. The evict method does referential comparison of the unretrieved substitute against the argument value.

Parameters:
key - key to match against
substitute - optional value to match against
Returns:
true on a successful remove

evictElement

public Element evictElement(Object key,
                            DiskStorageFactory.DiskSubstitute substitute)
Remove the matching mapping. The evict method does referential comparison of the unretrieved substitute against the argument value.

Parameters:
key - key to match against
substitute - optional value to match against
Returns:
the evicted element on a successful remove

getRandomSample

public List<DiskStorageFactory.DiskSubstitute> getRandomSample(ElementSubstituteFilter factory,
                                                               int sampleSize,
                                                               Object keyHint)
Select a random sample of elements generated by the supplied factory.

Parameters:
factory - generator of the given type
sampleSize - minimum number of elements to return
keyHint - a key on which we are currently working
Returns:
list of sampled elements/element substitute

ehcache

Copyright 2001-2016, Terracotta, Inc.