- All Implemented Interfaces:
Store
,Closeable
,Flushable
,AutoCloseable
Store
implementation that saves entries on disk under a specified directory.
A DiskStore
instance assumes exclusive ownership of its directory; only a single
DiskStore
from a single JVM process can safely operate on a given directory. This assumption is
cooperatively enforced among DiskStore
instances such that attempting to initialize a
store with a directory that is in use by another store in the same or a different JVM process
will cause an IOException
to be thrown.
The store keeps track of entries known to it across sessions by maintaining an on-disk
hashtable called the index. As changes are made to the store by adding, accessing or removing
entries, the index is transparently updated in a time-limited manner. By default, there's at most
one index update every 2 seconds. This rate can be changed by setting the system property:
com.github.mizosoft.methanol.internal.cache.DiskStore.indexUpdateDelayMillis
. Setting a small
delay can result in too often index updates, which extracts a noticeable toll on IO and CPU,
especially if there's a relatively large number of entries (updating entails reconstructing then
rewriting the whole index). On the other hand, scarcely updating the index affords less
durability against crashes as entries that aren't indexed are dropped on initialization. Calling
the flush
method forces an index update, regardless of the time limit.
To ensure entries are not lost across sessions, a store must be closed
after
it has been done with. The dispose()
method can be called to atomically close the store
and clear its directory if persistence isn't needed (e.g. using temp directories for storage). A
closed store usually throws an IllegalStateException
when used.
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic final class
static final class
An immutable 80-bit hash code.static interface
A function that computes an 80-bit hash from a string key.Nested classes/interfaces inherited from interface com.github.mizosoft.methanol.internal.cache.Store
Store.Editor, Store.EntryReader, Store.EntryWriter, Store.Viewer
-
Method Summary
Modifier and TypeMethodDescriptionvoid
clear()
Removes all entries from this store.void
close()
Closes this store.void
dispose()
Atomically clears and closes this store.Returns anOptional<Editor>
for the entry associated with the given key (atomically creating a new one if necessary), or an empty optional if such entry can't be edited.executor()
Returns the optional executor used for asynchronous or background operations.void
flush()
Flushes any data buffered by this store.iterator()
Returns an iterator ofViewers
over the entries in this store.long
maxSize()
Returns this store's max size in bytes.static DiskStore.Builder
boolean
Removes the entry associated with the given key.long
size()
Returns the size in bytes of all entries in this store.Returns aOptional<Viewer>
for the entry associated with the given key, or an empty optional if there's no such entry.
-
Method Details
-
directory
-
maxSize
public long maxSize()Description copied from interface:Store
Returns this store's max size in bytes. -
executor
Description copied from interface:Store
Returns the optional executor used for asynchronous or background operations. -
view
Description copied from interface:Store
Returns aOptional<Viewer>
for the entry associated with the given key, or an empty optional if there's no such entry.- Specified by:
view
in interfaceStore
- Throws:
IOException
InterruptedException
-
edit
Description copied from interface:Store
Returns anOptional<Editor>
for the entry associated with the given key (atomically creating a new one if necessary), or an empty optional if such entry can't be edited.- Specified by:
edit
in interfaceStore
- Throws:
IOException
-
iterator
Description copied from interface:Store
Returns an iterator ofViewers
over the entries in this store. The iterator doesn't throwConcurrentModificationException
when the store is modified, but there's no guarantee these changes are reflected.- Specified by:
iterator
in interfaceStore
- Throws:
IOException
-
remove
Description copied from interface:Store
Removes the entry associated with the given key.- Specified by:
remove
in interfaceStore
- Throws:
IOException
InterruptedException
-
clear
Description copied from interface:Store
Removes all entries from this store.- Specified by:
clear
in interfaceStore
- Throws:
IOException
-
size
Description copied from interface:Store
Returns the size in bytes of all entries in this store.- Specified by:
size
in interfaceStore
- Throws:
IOException
-
dispose
Description copied from interface:Store
Atomically clears and closes this store.- Specified by:
dispose
in interfaceStore
- Throws:
IOException
-
close
Description copied from interface:Store
Closes this store. Once the store is closed, all ongoing edits will silently fail to write or commit anything.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Specified by:
close
in interfaceStore
- Throws:
IOException
-
flush
Description copied from interface:Store
Flushes any data buffered by this store.- Specified by:
flush
in interfaceFlushable
- Specified by:
flush
in interfaceStore
- Throws:
IOException
-
newBuilder
-