Module methanol

Interface Store

  • All Superinterfaces:
    java.lang.AutoCloseable, java.io.Closeable, java.io.Flushable
    All Known Implementing Classes:
    DiskStore, MemoryStore

    public interface Store
    extends java.io.Closeable, java.io.Flushable
    A repository of binary data entries each identified by a string key. Each entry has a metadata block and a data stream. An entry's metadata block and data stream can be accessed by a Store.Viewer and modified by an Store.Editor. An entry can have at most one active editor but can have multiple concurrent viewers. No data written through an editor is visible to subsequently opened viewers unless committed. A viewer sees a consistent snapshot of the entry as perceived at the time it was opened at.

    A store may bound its size by automatic eviction of entries, possibly in background. The size bound is not strict. A store's size() might temporarily exceed its bound in case the store is being actively expanded while eviction is in progress. Additionally, a store's size doesn't include the overhead of the underlying filesystem or any metadata the store itself uses for indexing purposes. Thus, a store's maxSize() is not exact and might be slightly exceeded as necessary.

    Entries on a store are volatile. It is not guaranteed that an entry can be viewed any time, even if immediately, after it has been committed.

    Functions that are expected to be called frequently have two variants: one synchronous, and another asynchronous variant that takes an additional Executor parameter. The former has a default implementation that calls the latter and waits for the result. The executor parameter only expresses caller's preferred asynchronous execution strategy, and may be ignored by the store if seen fit. The executor parameter can be Runnable::run if same-thread execution is preferred.

    Store is thread-safe and is suitable for concurrent use.

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Interface Description
      static interface  Store.Editor
      Writes an entry's metadata block and data stream.
      static interface  Store.EntryReader
      A reader for an entry's data stream.
      static interface  Store.EntryWriter
      A writer for an entry's data stream.
      static interface  Store.Viewer
      Reads an entry's metadata block and data stream.
    • Method Summary

      All Methods Instance Methods Abstract Methods Default Methods 
      Modifier and Type Method Description
      void clear()
      Removes all entries from this store.
      void close()
      Closes this store.
      void dispose()
      Atomically clears and closes this store.
      default java.util.Optional<Store.Editor> edit​(java.lang.String key)
      Synchronous variant of 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.
      void flush()
      Flushes any indexing data buffered by this store.
      java.util.Iterator<Store.Viewer> iterator()
      Returns an iterator of Viewers over the entries in this store.
      long maxSize()
      Returns this store's max size in bytes.
      boolean remove​(java.lang.String key)
      Removes the entry associated with the given key.
      default boolean removeAll​(java.util.List<java.lang.String> keys)
      Removes all the entries associated with the given keys.
      long size()
      Returns the size in bytes of all entries in this store.
      default java.util.Optional<Store.Viewer> view​(java.lang.String key)
      Synchronous variant of 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

      • maxSize

        long maxSize()
        Returns this store's max size in bytes.
      • view

        default java.util.Optional<Store.Viewer> view​(java.lang.String key)
                                               throws java.io.IOException
        Synchronous variant of view(String, Executor).
        Throws:
        java.lang.IllegalStateException - if closed
        java.io.IOException
      • view

        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. An empty optional is returned if there's no such entry.
        Throws:
        java.lang.IllegalStateException - if closed
      • edit

        default java.util.Optional<Store.Editor> edit​(java.lang.String key)
                                               throws java.io.IOException
        Synchronous variant of edit(String, Executor).
        Throws:
        java.lang.IllegalStateException - if closed
        java.io.IOException
      • edit

        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. An empty optional is returned either if there's no such entry, or such entry cannot be edited at the moment.
        Throws:
        java.lang.IllegalStateException - if closed
      • iterator

        java.util.Iterator<Store.Viewer> iterator()
                                           throws java.io.IOException
        Returns an iterator of Viewers over the entries in this store. The iterator doesn't throw ConcurrentModificationException when the store is asynchronously modified, but there's no guarantee such changes are reflected.
        Throws:
        java.io.IOException
      • remove

        @CanIgnoreReturnValue
        boolean remove​(java.lang.String key)
                throws java.io.IOException
        Removes the entry associated with the given key.
        Throws:
        java.lang.IllegalStateException - if closed
        java.io.IOException
      • removeAll

        @CanIgnoreReturnValue
        default boolean removeAll​(java.util.List<java.lang.String> keys)
                           throws java.io.IOException
        Removes all the entries associated with the given keys.
        Throws:
        java.lang.IllegalStateException - if closed
        java.io.IOException
      • clear

        void clear()
            throws java.io.IOException
        Removes all entries from this store.
        Throws:
        java.lang.IllegalStateException - if closed
        java.io.IOException
      • size

        long size()
           throws java.io.IOException
        Returns the size in bytes of all entries in this store.
        Throws:
        java.io.IOException
      • dispose

        void dispose()
              throws java.io.IOException
        Atomically clears and closes this store.
        Throws:
        java.io.IOException
      • close

        void close()
            throws java.io.IOException
        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 interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Throws:
        java.io.IOException
      • flush

        void flush()
            throws java.io.IOException
        Flushes any indexing data buffered by this store.
        Specified by:
        flush in interface java.io.Flushable
        Throws:
        java.lang.IllegalStateException - if the store is closed
        java.io.IOException