- java.lang.Object
-
- com.github.mizosoft.methanol.internal.cache.DiskStore
-
- All Implemented Interfaces:
Store,TestableStore,java.io.Closeable,java.io.Flushable,java.lang.AutoCloseable
public final class DiskStore extends java.lang.Object implements Store, TestableStore
A persistentStoreimplementation that saves entries on disk under a specified directory. ADiskStoreinstance assumes exclusive ownership of its directory; only a singleDiskStorefrom a single JVM process can safely operate on a given directory. This assumption is cooperatively enforced amongDiskStoreinstances 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 anIOExceptionto 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 theflushmethod forces an index update, regardless of the time limit.To ensure entries are not lost across sessions, a store must be
closedafter it has been done with. Thedispose()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 anIllegalStateExceptionwhen used.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classDiskStore.Builderstatic classDiskStore.HashAn immutable 80-bit hash code.static interfaceDiskStore.HasherA 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
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclear()Removes all entries from this store.voidclose()Closes this store.java.nio.file.Pathdirectory()voiddispose()Atomically clears and closes this store.java.util.Optional<Store.Editor>edit(java.lang.String key)Synchronous variant ofStore.edit(String, Executor).java.util.concurrent.CompletableFuture<java.util.Optional<Store.Editor>>edit(java.lang.String key, java.util.concurrent.Executor executor)Opens an editor for the entry associated with the given key.java.util.List<java.lang.String>entriesOnUnderlyingStorageForTesting(java.lang.String 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.java.util.Iterator<Store.Viewer>iterator()Returns an iterator ofViewersover the entries in this store.longmaxSize()Returns this store's max size in bytes.static DiskStore.BuildernewBuilder()booleanremove(java.lang.String key)Removes the entry associated with the given key.longsize()Returns the size in bytes of all entries in this store.java.lang.StringtoString()java.util.Optional<Store.Viewer>view(java.lang.String key)Synchronous variant ofStore.view(String, Executor).java.util.concurrent.CompletableFuture<java.util.Optional<Store.Viewer>>view(java.lang.String key, java.util.concurrent.Executor executor)Opens a viewer for the entry associated with the given key.
-
-
-
Method Detail
-
directory
public java.nio.file.Path directory()
-
maxSize
public long maxSize()
Description copied from interface:StoreReturns this store's max size in bytes.
-
view
public java.util.Optional<Store.Viewer> view(java.lang.String key) throws java.io.IOException
Description copied from interface:StoreSynchronous variant ofStore.view(String, Executor).
-
view
public java.util.concurrent.CompletableFuture<java.util.Optional<Store.Viewer>> view(java.lang.String key, java.util.concurrent.Executor executor)
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
public java.util.Optional<Store.Editor> edit(java.lang.String key) throws java.io.IOException
Description copied from interface:StoreSynchronous variant ofStore.edit(String, Executor).
-
edit
public java.util.concurrent.CompletableFuture<java.util.Optional<Store.Editor>> edit(java.lang.String key, java.util.concurrent.Executor executor)
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
public java.util.Iterator<Store.Viewer> 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
public boolean remove(java.lang.String key) throws java.io.IOExceptionDescription copied from interface:StoreRemoves the entry associated with the given key.
-
clear
public void clear() throws java.io.IOExceptionDescription copied from interface:StoreRemoves all entries from this store.
-
size
public long size()
Description copied from interface:StoreReturns the size in bytes of all entries in this store.
-
dispose
public void dispose() throws java.io.IOExceptionDescription copied from interface:StoreAtomically clears and closes this store.
-
close
public void close() throws java.io.IOExceptionDescription 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.
-
flush
public void flush() throws java.io.IOExceptionDescription copied from interface:StoreFlushes any indexing data buffered by this store.
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
entriesOnUnderlyingStorageForTesting
public java.util.List<java.lang.String> entriesOnUnderlyingStorageForTesting(java.lang.String key)
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
public static DiskStore.Builder newBuilder()
-
-