Class MetaDataStateFormat<T>

  • Type Parameters:
    T - the type of the XContent base data-structure

    public abstract class MetaDataStateFormat<T>
    extends java.lang.Object
    MetaDataStateFormat is a base class to write checksummed XContent based files to one or more directories in a standardized directory structure.
    • Method Summary

      Modifier and Type Method Description
      static void deleteMetaState​(java.nio.file.Path... dataLocations)
      Deletes all meta state directories recursively for the given data locations
      abstract T fromXContent​(XContentParser parser)
      Reads a new instance of the state from the given XContentParser Subclasses need to implement this class for theirs specific state.
      T loadLatestState​(org.apache.logging.log4j.Logger logger, NamedXContentRegistry namedXContentRegistry, java.nio.file.Path... dataLocations)
      Tries to load the latest state from the given data-locations.
      protected org.apache.lucene.store.Directory newDirectory​(java.nio.file.Path dir)  
      protected XContentBuilder newXContentBuilder​(XContentType type, java.io.OutputStream stream)  
      T read​(NamedXContentRegistry namedXContentRegistry, java.nio.file.Path file)
      Reads the state from a given file and compares the expected version against the actual version of the state.
      abstract void toXContent​(XContentBuilder builder, T state)
      Writes the given state to the given XContentBuilder Subclasses need to implement this class for theirs specific state.
      void write​(T state, java.nio.file.Path... locations)
      Writes the given state to the given directories.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • MetaDataStateFormat

        protected MetaDataStateFormat​(java.lang.String prefix)
        Creates a new MetaDataStateFormat instance
    • Method Detail

      • write

        public final void write​(T state,
                                java.nio.file.Path... locations)
                         throws java.io.IOException
        Writes the given state to the given directories. The state is written to a state directory ("_state") underneath each of the given file locations and is created if it doesn't exist. The state is serialized to a temporary file in that directory and is then atomically moved to it's target filename of the pattern {prefix}{version}.st.
        Parameters:
        state - the state object to write
        locations - the locations where the state should be written to.
        Throws:
        java.io.IOException - if an IOException occurs
      • newXContentBuilder

        protected XContentBuilder newXContentBuilder​(XContentType type,
                                                     java.io.OutputStream stream)
                                              throws java.io.IOException
        Throws:
        java.io.IOException
      • toXContent

        public abstract void toXContent​(XContentBuilder builder,
                                        T state)
                                 throws java.io.IOException
        Writes the given state to the given XContentBuilder Subclasses need to implement this class for theirs specific state.
        Throws:
        java.io.IOException
      • fromXContent

        public abstract T fromXContent​(XContentParser parser)
                                throws java.io.IOException
        Reads a new instance of the state from the given XContentParser Subclasses need to implement this class for theirs specific state.
        Throws:
        java.io.IOException
      • read

        public final T read​(NamedXContentRegistry namedXContentRegistry,
                            java.nio.file.Path file)
                     throws java.io.IOException
        Reads the state from a given file and compares the expected version against the actual version of the state.
        Throws:
        java.io.IOException
      • newDirectory

        protected org.apache.lucene.store.Directory newDirectory​(java.nio.file.Path dir)
                                                          throws java.io.IOException
        Throws:
        java.io.IOException
      • loadLatestState

        public T loadLatestState​(org.apache.logging.log4j.Logger logger,
                                 NamedXContentRegistry namedXContentRegistry,
                                 java.nio.file.Path... dataLocations)
                          throws java.io.IOException
        Tries to load the latest state from the given data-locations. It tries to load the latest state determined by the states version from one or more data directories and if none of the latest states can be loaded an exception is thrown to prevent accidentally loading a previous state and silently omitting the latest state.
        Parameters:
        logger - a logger instance
        dataLocations - the data-locations to try.
        Returns:
        the latest state or null if no state was found.
        Throws:
        java.io.IOException
      • deleteMetaState

        public static void deleteMetaState​(java.nio.file.Path... dataLocations)
                                    throws java.io.IOException
        Deletes all meta state directories recursively for the given data locations
        Parameters:
        dataLocations - the data location to delete
        Throws:
        java.io.IOException