156ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson/*
256ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson * Copyright (C) 2010 Google Inc.
356ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson *
456ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson * Licensed under the Apache License, Version 2.0 (the "License");
556ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson * you may not use this file except in compliance with the License.
656ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson * You may obtain a copy of the License at
756ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson *
856ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson * http://www.apache.org/licenses/LICENSE-2.0
956ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson *
1056ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson * Unless required by applicable law or agreed to in writing, software
1156ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson * distributed under the License is distributed on an "AS IS" BASIS,
1256ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1356ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson * See the License for the specific language governing permissions and
1456ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson * limitations under the License.
1556ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson */
1656ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson
1756ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodsonpackage com.google.clearsilver.jsilver.adaptor;
1856ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson
1956ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodsonimport java.util.LinkedHashMap;
2056ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodsonimport java.util.List;
2156ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodsonimport java.util.Map;
2256ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodsonimport java.util.concurrent.locks.ReadWriteLock;
2356ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodsonimport java.util.concurrent.locks.ReentrantReadWriteLock;
2456ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson
2556ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson/**
2656ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson * This class implements a cache of a list of loadpaths and a file name to the absolute file name
2756ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson * where the file is located on the filesystem. The purpose is to avoid filesystem calls for common
2856ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson * operations, like in which of these directories does this file exist? This class is threadsafe.
2956ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson *
3056ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson * Some of this code is copied from {@link com.google.clearsilver.base.CSFileCache}.
3156ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson */
3256ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodsonpublic class LoadPathToFileCache {
3356ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson
3456ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson  private final LRUCache<String, String> cache;
3556ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson  private final ReadWriteLock cacheLock = new ReentrantReadWriteLock();
3656ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson
3756ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson  public LoadPathToFileCache(int capacity) {
3856ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson    cache = new LRUCache<String, String>(capacity);
3956ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson  }
4056ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson
4156ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson  /**
4256ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson   * Lookup in the cache to see if we have a mapping from the given loadpaths and filename to an
4356ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson   * absolute file path.
4456ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson   *
4556ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson   * @param loadPaths the ordered list of directories to search for the file.
4656ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson   * @param filename the name of the file.
4756ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson   * @return the absolute filepath location of the file, or {@code null} if not in the cache.
4856ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson   */
4956ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson  public String lookup(List<String> loadPaths, String filename) {
5056ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson    String filePathMapKey = makeCacheKey(loadPaths, filename);
5156ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson    cacheLock.readLock().lock();
5256ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson    try {
5356ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson      return cache.get(filePathMapKey);
5456ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson    } finally {
5556ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson      cacheLock.readLock().unlock();
5656ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson    }
5756ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson  }
5856ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson
5956ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson  /**
6056ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson   * Add a new mapping to the cache.
6156ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson   *
6256ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson   * @param loadPaths the ordered list of directories to search for the file.
6356ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson   * @param filename the name of the file.
6456ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson   * @param filePath the absolute filepath location of the file
6556ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson   */
6656ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson  public void add(List<String> loadPaths, String filename, String filePath) {
6756ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson    String filePathMapKey = makeCacheKey(loadPaths, filename);
6856ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson    cacheLock.writeLock().lock();
6956ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson    try {
7056ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson      cache.put(filePathMapKey, filePath);
7156ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson    } finally {
7256ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson      cacheLock.writeLock().unlock();
7356ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson    }
7456ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson  }
7556ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson
7656ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson  public static String makeCacheKey(List<String> loadPaths, String filename) {
7756ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson    if (loadPaths == null) {
7856ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson      throw new NullPointerException("Loadpaths cannot be null");
7956ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson    }
8056ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson    if (filename == null) {
8156ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson      throw new NullPointerException("Filename cannot be null");
8256ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson    }
8356ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson    String loadPathsHash = Long.toHexString(hashLoadPath(loadPaths));
8456ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson    StringBuilder sb = new StringBuilder(loadPathsHash);
8556ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson    sb.append('|').append(filename);
8656ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson    return sb.toString();
8756ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson  }
8856ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson
8956ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson  /**
9056ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson   * Generate a hashCode to represent the ordered list of loadpaths. Used as a key into the fileMap
9156ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson   * since a concatenation of the loadpaths is likely to be huge (greater than 1K) but very
9256ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson   * repetitive. Algorithm comes from Effective Java by Joshua Bloch.
9356ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson   * <p>
9456ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson   * We don't use the builtin hashCode because it is only 32-bits, and while the expect different
9556ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson   * values of loadpaths is very small, we want to minimize any chance of collision since we use the
9656ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson   * hash as the key and throw away the loadpath list
9756ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson   *
9856ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson   * @param list an ordered list of loadpaths.
9956ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson   * @return a long representing a hash of the loadpaths.
10056ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson   */
10156ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson  static long hashLoadPath(List<String> list) {
10256ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson    long hash = 17;
10356ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson    for (String path : list) {
10456ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson      hash = 37 * hash + path.hashCode();
10556ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson    }
10656ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson    return hash;
10756ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson  }
10856ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson
10956ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson  /**
11056ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson   * This code is copied from {@link com.google.common.cache.LRUCache} but is distilled to basics in
11156ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson   * order to not require importing Google code. Hopefully there is an open-source implementation,
11256ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson   * although given the design of LinkHashMap, this is trivial.
11356ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson   */
11456ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson  static class LRUCache<K, V> extends LinkedHashMap<K, V> {
11556ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson
11656ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson    private final int capacity;
11756ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson
11856ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson    LRUCache(int capacity) {
11956ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson      super(capacity, 0.75f, true);
12056ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson      this.capacity = capacity;
12156ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson    }
12256ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson
12356ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson    /**
12456ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson     * {@inheritDoc}
12556ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson     * <p>
12656ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson     * Necessary to override because HashMap increases the capacity of the hashtable before
12756ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson     * inserting the elements. However, here we have set the max capacity already and will instead
12856ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson     * remove eldest elements instead of increasing capacity.
12956ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson     */
13056ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson    @Override
13156ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson    public void putAll(Map<? extends K, ? extends V> m) {
13256ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson      for (Map.Entry<? extends K, ? extends V> e : m.entrySet()) {
13356ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson        put(e.getKey(), e.getValue());
13456ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson      }
13556ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson    }
13656ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson
13756ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson    /**
13856ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson     * This method is called by LinkedHashMap to check whether the eldest entry should be removed.
13956ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson     *
14056ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson     * @param eldest
14156ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson     * @return true if element should be removed.
14256ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson     */
14356ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson    @Override
14456ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson    protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
14556ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson      return size() > capacity;
14656ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson    }
14756ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson  }
14856ed4167b942ec265f9cee70ac4d71d10b3835ceBen Dodson}
149