Class BaseDirCacheEditor

  • Direct Known Subclasses:
    DirCacheBuilder, DirCacheEditor

    abstract class BaseDirCacheEditor
    extends java.lang.Object
    Generic update/editing support for DirCache.

    The different update strategies extend this class to provide their own unique services to applications.

    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      protected BaseDirCacheEditor​(DirCache dc, int ecnt)
      Construct a new editor.
    • Method Summary

      All Methods Static Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      private void checkNameConflicts()  
      boolean commit()
      Finish, write, commit this change, and release the index lock.
      protected void fastAdd​(DirCacheEntry newEntry)
      Append one entry into the resulting entry list.
      protected void fastKeep​(int pos, int cnt)
      Add a range of existing entries from the destination cache.
      abstract void finish()
      Finish this builder and update the destination DirCache.
      DirCache getDirCache()
      Get the DirCache
      private static int lastSlash​(byte[] path)  
      private static int nextSlash​(byte[] b, int p)  
      protected void replace()
      Update the DirCache with the contents of entries.
      private static boolean startsWith​(byte[] a, byte[] b, int n)  
      • Methods inherited from class java.lang.Object

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

      • cache

        protected DirCache cache
        The cache instance this editor updates during finish().
      • entries

        protected DirCacheEntry[] entries
        Entry table this builder will eventually replace into cache.

        Use fastAdd(DirCacheEntry) or fastKeep(int, int) to make additions to this table. The table is automatically expanded if it is too small for a new addition.

        Typically the entries in here are sorted by their path names, just like they are in the DirCache instance.

      • entryCnt

        protected int entryCnt
        Total number of valid entries in entries.
    • Constructor Detail

      • BaseDirCacheEditor

        protected BaseDirCacheEditor​(DirCache dc,
                                     int ecnt)
        Construct a new editor.
        Parameters:
        dc - the cache this editor will eventually update.
        ecnt - estimated number of entries the editor will have upon completion. This sizes the initial entry table.
    • Method Detail

      • getDirCache

        public DirCache getDirCache()
        Get the DirCache
        Returns:
        the cache we will update on finish().
      • fastAdd

        protected void fastAdd​(DirCacheEntry newEntry)
        Append one entry into the resulting entry list.

        The entry is placed at the end of the entry list. The caller is responsible for making sure the final table is correctly sorted.

        The entries table is automatically expanded if there is insufficient space for the new addition.

        Parameters:
        newEntry - the new entry to add.
      • fastKeep

        protected void fastKeep​(int pos,
                                int cnt)
        Add a range of existing entries from the destination cache.

        The entries are placed at the end of the entry list, preserving their current order. The caller is responsible for making sure the final table is correctly sorted.

        This method copies from the destination cache, which has not yet been updated with this editor's new table. So all offsets into the destination cache are not affected by any updates that may be currently taking place in this editor.

        The entries table is automatically expanded if there is insufficient space for the new additions.

        Parameters:
        pos - first entry to copy from the destination cache.
        cnt - number of entries to copy.
      • finish

        public abstract void finish()
        Finish this builder and update the destination DirCache.

        When this method completes this builder instance is no longer usable by the calling application. A new builder must be created to make additional changes to the index entries.

        After completion the DirCache returned by getDirCache() will contain all modifications.

        Note to implementors: Make sure entries is fully sorted then invoke replace() to update the DirCache with the new table.

      • replace

        protected void replace()
        Update the DirCache with the contents of entries.

        This method should be invoked only during an implementation of finish(), and only after entries is sorted.

      • checkNameConflicts

        private void checkNameConflicts()
      • lastSlash

        private static int lastSlash​(byte[] path)
      • nextSlash

        private static int nextSlash​(byte[] b,
                                     int p)
      • startsWith

        private static boolean startsWith​(byte[] a,
                                          byte[] b,
                                          int n)
      • commit

        public boolean commit()
                       throws java.io.IOException
        Finish, write, commit this change, and release the index lock.

        If this method fails (returns false) the lock is still released.

        This is a utility method for applications as the finish-write-commit pattern is very common after using a builder to update entries.

        Returns:
        true if the commit was successful and the file contains the new data; false if the commit failed and the file remains with the old data.
        Throws:
        java.lang.IllegalStateException - the lock is not held.
        java.io.IOException - the output file could not be created. The caller no longer holds the lock.