Package Torello.Java

Class HiLiteMe.Cache

  • Enclosing class:
    HiLiteMe

    public static class HiLiteMe.Cache
    extends java.lang.Object
    A caching-system class that allows this tool to efficiently bypass calls to the server when an exact-copy of the hilited source-code already exists inside the cache.

    If using the HiLite.ME server to add color-coded (code-hilited) parts to your documentation files, html-files, or other uses for hiliting your code is going well; then try using the static class Cache with your HLMP (HiLite Me Parameters). When there is a local cache of all your request operations to the HiLite.ME server, the code will first check that a local copy of the results for you code-hiliting exits.

    The cache will be used, instead of request to the server under the following conditions:

    • If a local copy of a Source-Code Snippet is found in the cache, whose contents are identical.
    • If the HiLite.ME Style-Tag requested is identical to the last polling of the server
    • If the HiLite.ME Code-Type-Tag requested is identical to the one in the cache.


    Faster Build:
    If a build perpetually is taking minutes to hilite code snippets in every source-file of your class hierarchy, but most of the code in your classes rarely changes, then creating a directory where caches results can be saved will improve build times tremendously.


    • Constructor Summary

      Constructors 
      Constructor Description
      Cache​(String cacheSaveDirectory)
      This will load the hashCodes table to memory from the file-system directory identified by String-Parameter 'cacheSaveDirectory'.
    • Constructor Detail

      • Cache

        🡇     🗕  🗗  🗖
        public Cache​(java.lang.String cacheSaveDirectory)
              throws CacheError
        This will load the hashCodes table to memory from the file-system directory identified by String-Parameter 'cacheSaveDirectory'. An exception shall be thrown if this file is not found.
        Parameters:
        cacheSaveDirectory - This constructor presumes that this cache has been used and visited before. This directory name should point to your local-cache of the HiLite.ME Server Code hilite past-operations.
        Throws:
        CacheError - This error will throw if the cache has not been instantiated, or is corrupted. If the specified directory does not exist, then this Error shall also throw. The chain-cause Throwable should be visible, and is included as the Throwable.getCause().
    • Method Detail

      • totalSize

        🡅  🡇     🗕  🗗  🗖
        public long totalSize()
        Inform the user how much space (in bytes) is used by this Cache.
        Returns:
        The number of bytes being used on the file-system by this Cache.
        Code:
        Exact Method Body:
         return FileNode.createRoot(cacheSaveDirectory).loadTree().getDirTotalContentsSize();
        
      • totalNumber

        🡅  🡇     🗕  🗗  🗖
        public int totalNumber()
        Count how many files and directories are contained in this Cache.
        Returns:
        The total number of files and sub-directories in the Cache directory.
        Code:
        Exact Method Body:
         return FileNode.createRoot(cacheSaveDirectory).loadTree().count();
        
      • persistMasterHashToDisk

        🡅  🡇     🗕  🗗  🗖
        public void persistMasterHashToDisk()
                                     throws CacheError
        This will save the hash-code TreeSet<Integer> to disk. The Master Hash-Code List just keeps a record of the hashcodes of every String that was hilited by the Hiliter (and therefore saved inside the Cache). This method will save that Java TreeSet of Hash-Codes to disk.
        Throws:
        CacheError - This Error will throw if there is a problem writing the master cache-hash to disk. The chain-cause Throwable should be visible, and is included as the Throwable.getCause()
        Code:
        Exact Method Body:
         try
         {
             FileRW.writeObjectToFile
                 (hashCodes, this.cacheSaveDirectory + HASH_SAVE_TREE, true);
         } 
         catch (Throwable t)
         {
             throw new CacheError(
                 "There was an error writing the Master Hash-Code table to disk. " +
                 "File [" + this.cacheSaveDirectory + HASH_SAVE_TREE + "] was not saved. " +
                 "The cache-file will have to be refreshed at some point.  New Files " +
                 "Cache-Hash not saved.",
                 t
             );
         }
        
      • initializeOrClear

        🡅     🗕  🗗  🗖
        public static HiLiteMe.Cache initializeOrClear​
                    (java.lang.String cacheSaveDirectory,
                     StorageWriter sw)
                throws CacheError
        
        This will initialize a cache-file in the file-system directory identified by parameter String cacheSaveDirectory. If the directory specified does not exist, a CacheError is thrown. Any old cache files will be removed. To attempt to preserve old cache-files, call method initializeOrRepair(String, StorageWriter)

        OrClear: If the directory structure provided to this initialize method is not empty, the its entire contents shall be erased by a call to (Below)

        Java Line of Code:
         FileTransfer.deleteFilesRecursive
             (FileNode.createRoot(cacheSaveDirectory).loadTree(), sw);
        
        Parameters:
        cacheSaveDirectory - This constructor presumes that this cache has been used and visited before. This directory name should point to your local-cache of HiLite.ME Server Code hilite past-operations.
        sw - This receives log-writes from the call to FileTransfer.deleteFilesRecursive(Torello.Java.FileNode, Torello.Java.FileNodeFilter, Torello.Java.FileNodeFilter, java.lang.Appendable) which clears the files currently in the cache. This parameter may be null, and if it is, output-text will be shunted.
        Throws:
        CacheError - This exception will be throw if there are errors deleting any old-cache files currently in the directory; or if there is any error creating the new master hash-cache file. The chain-cause Throwable should be visible, and is included as the Throwable.getCause().
        Code:
        Exact Method Body:
         cacheSaveDirectory = checkCSD(cacheSaveDirectory);
        
         final String tempStrForStupidLambdaFinal = cacheSaveDirectory;
        
         try
         {
             File f = new File(cacheSaveDirectory);
        
             if (f.isDirectory())
                 FileTransfer.deleteFilesRecursive(
                     FileNode.createRoot(cacheSaveDirectory).loadTree(), null,
                     (FileNode fn) -> fn.getFullPathName().equals(tempStrForStupidLambdaFinal),
                     sw
                 );
        
             f.mkdirs();
         }
         catch (Throwable t)
         {
             throw new CacheError(
                 "There was an error emptying/clearing the directory " +
                 "[" + cacheSaveDirectory + "] of it's contents, please see cause " +
                 "throwable.getCause() for details.",
                 t
             );
         }
        
         try
             { writeNewTS(cacheSaveDirectory); }
        
         catch (Throwable t)
         {
             throw new CacheError(
                 "There was an error saving/creating the new cache-file " +
                 "[" + cacheSaveDirectory + "], please see cause chain throwable.getCause(), " +
                 "for more details.",
                 t
             );
         }
        
         return new Cache(cacheSaveDirectory);