- All Implemented Interfaces:
Store
,TestableStore
,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
Nested ClassesModifier 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.Synchronous variant ofStore.edit(String, Executor)
.Opens an editor for the entry associated with the given key.Returns string representations for the resources an entry with the given key occupies on the underlying store.void
flush()
Flushes any indexing 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.toString()
Synchronous variant ofStore.view(String, Executor)
.Opens a viewer for the entry associated with the given key.
-
Method Details
-
directory
-
maxSize
public long maxSize()Description copied from interface:Store
Returns this store's max size in bytes. -
view
Description copied from interface:Store
Synchronous variant ofStore.view(String, Executor)
.- Specified by:
view
in interfaceStore
- Throws:
IOException
-
view
Description copied from interface:Store
Opens a viewer for the entry associated with the given key. An empty optional is returned if there's no such entry. -
edit
Description copied from interface:Store
Synchronous variant ofStore.edit(String, Executor)
.- Specified by:
edit
in interfaceStore
- Throws:
IOException
-
edit
Description copied from interface:Store
Opens an editor for the entry associated with the given key. An empty optional is returned either if there's no such entry, or such entry cannot be edited at the moment. -
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 asynchronously modified, but there's no guarantee such changes are reflected. -
remove
Description copied from interface:Store
Removes the entry associated with the given key.- Specified by:
remove
in interfaceStore
- Throws:
IOException
-
clear
Description copied from interface:Store
Removes all entries from this store.- Specified by:
clear
in interfaceStore
- Throws:
IOException
-
size
public long size()Description copied from interface:Store
Returns the size in bytes of all entries in this store. -
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 fail, either silently or by throwing an exception, 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 indexing data buffered by this store.- Specified by:
flush
in interfaceFlushable
- Specified by:
flush
in interfaceStore
- Throws:
IOException
-
toString
-
entriesOnUnderlyingStorageForTesting
Description copied from interface:TestableStore
Returns string representations for the resources an entry with the given key occupies on the underlying store. Implementation must skip intermediary data structures and directly interact with the underlying store (e.g. filesystem).- Specified by:
entriesOnUnderlyingStorageForTesting
in interfaceTestableStore
-
newBuilder
-