Class MetaDataStateFormat<T>

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

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

      • write

        public final long write​(T state,
                                Path... locations)
                         throws WriteStateException
        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. If this method returns without exception there is a guarantee that state is persisted to the disk and loadLatestState will return it.
        This method always performs cleanup of temporary files regardless whether it succeeds or fails. Cleanup logic for state files is more involved. If this method fails with an exception, it performs cleanup of newly created state file. But if this method succeeds, it does not perform cleanup of old state files. If this write succeeds, but some further write fails, you may want to rollback the transaction and keep old file around. After transaction is finished use cleanupOldFiles(long, Path[]) for the clean-up. If this write is not a part of bigger transaction, consider using writeAndCleanup(Object, Path...) method instead.
        Parameters:
        state - the state object to write
        locations - the locations where the state should be written to.
        Returns:
        generation of newly written state.
        Throws:
        WriteStateException - if some exception during writing state occurs. See also WriteStateException.isDirty().
      • toXContent

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

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

        public void cleanupOldFiles​(long currentGeneration,
                                    Path[] locations)
        Clean ups all state files not matching passed generation.
        Parameters:
        currentGeneration - state generation to keep.
        locations - state paths.
      • getStateFileName

        public String getStateFileName​(long generation)
      • loadGeneration

        public T loadGeneration​(org.apache.logging.log4j.Logger logger,
                                NamedXContentRegistry namedXContentRegistry,
                                long generation,
                                Path... dataLocations)
        Tries to load the state of particular generation from the given data-locations. If any of data locations contain state files with given generation, state will be loaded from these state files.
        Parameters:
        logger - a logger instance.
        generation - the generation to be loaded.
        dataLocations - the data-locations to try.
        Returns:
        the state of asked generation or null if no state was found.
      • loadLatestStateWithGeneration

        public Tuple<T,​Long> loadLatestStateWithGeneration​(org.apache.logging.log4j.Logger logger,
                                                                 NamedXContentRegistry namedXContentRegistry,
                                                                 Path... dataLocations)
                                                          throws IOException
        Tries to load the latest state from the given data-locations.
        Parameters:
        logger - a logger instance.
        dataLocations - the data-locations to try.
        Returns:
        tuple of the latest state and generation. (-1, null) if no state is found.
        Throws:
        IOException
      • loadLatestState

        public T loadLatestState​(org.apache.logging.log4j.Logger logger,
                                 NamedXContentRegistry namedXContentRegistry,
                                 Path... dataLocations)
                          throws IOException
        Tries to load the latest state from the given data-locations.
        Parameters:
        logger - a logger instance.
        dataLocations - the data-locations to try.
        Returns:
        the latest state or null if no state was found.
        Throws:
        IOException
      • deleteMetaState

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

        public String getPrefix()