Changeset 7486


Ignore:
Timestamp:
01/05/11 16:15:26 (9 years ago)
Author:
melissa
Message:

Added optional cacheing of directory listings, courtesy of Lee Kamentsky.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/components/common/src/loci/common/Location.java

    r6916 r7486  
    5858  private static HashMap<String, Object> idMap = new HashMap<String, Object>(); 
    5959 
     60  private static boolean cacheListings = false; 
     61 
     62  // By default, cache for one hour. 
     63  private static long cacheNanos = 60L * 60L * 1000L * 1000L * 1000L; 
     64 
     65  protected class ListingsResult { 
     66    public final String [] listing; 
     67    public final long time; 
     68    ListingsResult(String [] listing, long time) { 
     69      this.listing = listing; 
     70      this.time = time; 
     71    } 
     72  } 
     73  private static HashMap<String, ListingsResult> fileListings = 
     74    new HashMap<String, ListingsResult>(); 
     75 
    6076  // -- Fields -- 
    6177 
     
    93109 
    94110  // -- Location API methods -- 
     111 
     112  /** 
     113   * Turn cacheing of directory listings on or off. 
     114   * Cacheing is turned off by default. 
     115   * 
     116   * Reasons to cache - directory listings over network shares 
     117   * can be very expensive, especially in HCS experiments with thousands 
     118   * of files in the same directory. Technically, if you use a directory 
     119   * listing and then go and access the file, you are using stale information. 
     120   * Unlike a database, there's no transactional integrity to file system 
     121   * operations, so the directory could change by the time you access the file. 
     122   * 
     123   * Reasons not to cache - the contents of the directories might change 
     124   * during the program invocation. 
     125   * 
     126   * @param cache - true to turn cacheing on, false to leave it off. 
     127   */ 
     128  public static void cacheDirectoryListings(boolean cache) { 
     129    cacheListings = cache; 
     130  } 
     131 
     132  /** 
     133   * Cache directory listings for this many seconds before relisting. 
     134   * 
     135   * @param sec - use the cache if a directory list was done within this many 
     136   * seconds. 
     137   */ 
     138  public static void setCacheDirectoryTimeout(double sec) { 
     139    cacheNanos = (long)(sec * 1000. * 1000. * 1000.); 
     140  } 
     141 
     142  /** 
     143   * Clear the directory listings cache. 
     144   * 
     145   * Do this if directory contents might have changed in a significant way. 
     146   */ 
     147  public static void clearDirectoryListingsCache() { 
     148    fileListings = new HashMap<String, ListingsResult>(); 
     149  } 
     150 
     151  /** 
     152   * Remove any cached directory listings that have expired. 
     153   */ 
     154  public static void cleanStaleCacheEntries() { 
     155    long t = System.nanoTime() - cacheNanos; 
     156    ArrayList<String> staleKeys = new ArrayList(); 
     157    for (String key : fileListings.keySet()) { 
     158      if (fileListings.get(key).time < t) { 
     159        staleKeys.add(key); 
     160      } 
     161    } 
     162    for (String key : staleKeys) { 
     163      fileListings.remove(key); 
     164    } 
     165  } 
    95166 
    96167  /** 
     
    208279   */ 
    209280  public String[] list(boolean noHiddenFiles) { 
     281    String key = getAbsolutePath() + Boolean.toString(noHiddenFiles); 
     282    String [] result = null; 
     283    if (cacheListings) { 
     284      cleanStaleCacheEntries(); 
     285      ListingsResult listingsResult = fileListings.get(key); 
     286      if (listingsResult != null) { 
     287        return listingsResult.listing; 
     288      } 
     289    } 
    210290    ArrayList<String> files = new ArrayList<String>(); 
    211291    if (isURL) { 
     
    255335      } 
    256336    } 
    257     return files.toArray(new String[files.size()]); 
     337    result = files.toArray(new String[files.size()]); 
     338    if (cacheListings) { 
     339      fileListings.put(key, new ListingsResult(result, System.nanoTime())); 
     340    } 
     341    return result; 
    258342  } 
    259343 
Note: See TracChangeset for help on using the changeset viewer.