Class FileReftableStack

  • All Implemented Interfaces:
    java.lang.AutoCloseable

    public class FileReftableStack
    extends java.lang.Object
    implements java.lang.AutoCloseable
    A mutable stack of reftables on local filesystem storage. Not thread-safe. This is an AutoCloseable because this object owns the file handles to the open reftables.
    • Field Detail

      • lastNextUpdateIndex

        private long lastNextUpdateIndex
      • stackPath

        private final java.io.File stackPath
      • reftableDir

        private final java.io.File reftableDir
      • onChange

        private final java.lang.Runnable onChange
      • random

        private final java.security.SecureRandom random
      • configSupplier

        private final java.util.function.Supplier<Config> configSupplier
      • OVERHEAD

        private static long OVERHEAD
    • Constructor Detail

      • FileReftableStack

        public FileReftableStack​(java.io.File stackPath,
                                 java.io.File reftableDir,
                                 @Nullable
                                 java.lang.Runnable onChange,
                                 java.util.function.Supplier<Config> configSupplier)
                          throws java.io.IOException
        Creates a stack corresponding to the list of reftables in the argument
        Parameters:
        stackPath - the filename for the stack.
        reftableDir - the dir holding the tables.
        onChange - hook to call if we notice a new write
        configSupplier - Config supplier
        Throws:
        java.io.IOException - on I/O problems
    • Method Detail

      • reloadOnce

        private void reloadOnce​(java.util.List<java.lang.String> names)
                         throws java.io.IOException,
                                java.io.FileNotFoundException
        Reloads the stack, potentially reusing opened reftableReaders.
        Parameters:
        names - holds the names of the tables to load.
        Throws:
        java.io.FileNotFoundException - load must be retried.
        java.io.IOException - on other IO errors.
      • reload

        void reload()
             throws java.io.IOException
        Throws:
        java.io.IOException
      • getMergedReftable

        public MergedReftable getMergedReftable()
        Returns:
        the merged reftable
      • readTableNames

        private java.util.List<java.lang.String> readTableNames()
                                                         throws java.io.IOException
        Throws:
        java.io.IOException
      • isUpToDate

        boolean isUpToDate()
                    throws java.io.IOException
        Returns:
        true if the on-disk file corresponds to the in-memory data.
        Throws:
        java.io.IOException - on IO problem
      • close

        public void close()
        Specified by:
        close in interface java.lang.AutoCloseable
      • nextUpdateIndex

        private long nextUpdateIndex()
                              throws java.io.IOException
        Throws:
        java.io.IOException
      • filename

        private java.lang.String filename​(long low,
                                          long high)
      • addReftable

        public boolean addReftable​(FileReftableStack.Writer w)
                            throws java.io.IOException
        Tries to add a new reftable to the stack. Returns true if it succeeded, or false if there was a lock failure, due to races with other processes. This is package private so FileReftableDatabase can call into here.
        Parameters:
        w - writer to write data to a reftable under construction
        Returns:
        true if the transaction was successful.
        Throws:
        java.io.IOException - on I/O problems
      • compactLocked

        private java.io.File compactLocked​(int first,
                                           int last)
                                    throws java.io.IOException
        Write the reftable for the given range into a temp file.
        Parameters:
        first - index of first stack entry to be written
        last - index of last stack entry to be written
        Returns:
        the file holding the replacement table.
        Throws:
        java.io.IOException - on I/O problem
      • compactRange

        boolean compactRange​(int first,
                             int last)
                      throws java.io.IOException
        Compacts a range of the stack, following the file locking protocol documented in the spec.
        Parameters:
        first - index of first stack entry to be considered in compaction
        last - index of last stack entry to be considered in compaction
        Returns:
        true if a compaction was successfully applied.
        Throws:
        java.io.IOException - on I/O problem
      • log

        static int log​(long sz)
        Calculate an approximate log2.
        Parameters:
        sz -
        Returns:
        log2
      • autoCompact

        private void autoCompact()
                          throws java.io.IOException
        Heuristically tries to compact the stack if the stack has a suitable shape.
        Throws:
        java.io.IOException
      • tableSizes

        private long[] tableSizes()
                           throws java.io.IOException
        Throws:
        java.io.IOException
      • compactFully

        void compactFully()
                   throws java.io.IOException
        Throws:
        java.io.IOException