Class DiskStore
- 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 classstatic final classAn immutable 80-bit hash code.static interfaceA function that computes an 80-bit hash from a string key.Nested classes/interfaces inherited from interface Store
Store.Editor, Store.EntryReader, Store.EntryWriter, Store.Viewer -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()Removes all entries from this store.voidclose()Closes this store.voiddispose()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.voidflush()Flushes any indexing data buffered by this store.iterator()Returns an iterator ofViewersover the entries in this store.longmaxSize()Returns this store's max size in bytes.static DiskStore.BuilderbooleanRemoves the entry associated with the given key.longsize()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
-
view
Description copied from interface:StoreSynchronous variant ofStore.view(String, Executor).- Specified by:
viewin interfaceStore- Throws:
IOException
-
view
Description copied from interface:StoreOpens 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:StoreSynchronous variant ofStore.edit(String, Executor).- Specified by:
editin interfaceStore- Throws:
IOException
-
edit
Description copied from interface:StoreOpens 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:StoreReturns an iterator ofViewersover the entries in this store. The iterator doesn't throwConcurrentModificationExceptionwhen the store is asynchronously modified, but there's no guarantee such changes are reflected. -
remove
Description copied from interface:StoreRemoves the entry associated with the given key.- Specified by:
removein interfaceStore- Throws:
IOException
-
clear
Description copied from interface:StoreRemoves all entries from this store.- Specified by:
clearin interfaceStore- Throws:
IOException
-
size
-
dispose
Description copied from interface:StoreAtomically clears and closes this store.- Specified by:
disposein interfaceStore- Throws:
IOException
-
close
Description copied from interface:StoreCloses 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:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Specified by:
closein interfaceStore- Throws:
IOException
-
flush
-
toString
-
entriesOnUnderlyingStorageForTesting
Description copied from interface:TestableStoreReturns 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:
entriesOnUnderlyingStorageForTestingin interfaceTestableStore
-
newBuilder
-