/** * @param inputChannel the map file from which the index should be read and cached. * @param capacity the maximum number of entries in the cache. * @throws IllegalArgumentException if the capacity is negative. */ IndexCache(FileChannel inputChannel, int capacity) { this.fileChannel = inputChannel; this.map = new LRUCache<IndexCacheEntryKey, byte[]>(capacity); }
/** * @param capacity the maximum capacity of this cache. * @throws IllegalArgumentException if the capacity is negative. */ public LRUCache(int capacity) { super(calculateInitialCapacity(capacity), LOAD_FACTOR, true); this.capacity = capacity; }
/** * Matches a node with the given parameters against this RenderTheme. * * @param renderCallback the callback implementation which will be executed on each match. * @param renderContext * @param poi the point of interest. */ public synchronized void matchNode(RenderCallback renderCallback, final RenderContext renderContext, PointOfInterest poi) { MatchingCacheKey matchingCacheKey = new MatchingCacheKey(poi.tags, renderContext.rendererJob.tile.zoomLevel, Closed.NO); List<RenderInstruction> matchingList = this.poiMatchingCache.get(matchingCacheKey); if (matchingList != null) { // cache hit for (int i = 0, n = matchingList.size(); i < n; ++i) { matchingList.get(i).renderNode(renderCallback, renderContext, poi); } return; } // cache miss matchingList = new ArrayList<RenderInstruction>(); for (int i = 0, n = this.rulesList.size(); i < n; ++i) { this.rulesList.get(i).matchNode(renderCallback, renderContext, matchingList, poi); } this.poiMatchingCache.put(matchingCacheKey, matchingList); }
@Test public void lruCacheTest() { LRUCache<String, String> lruCache = createLRUCache(2); lruCache.put(KEY1, VALUE1); Assert.assertEquals(VALUE1, lruCache.get(KEY1)); Assert.assertFalse(lruCache.containsKey(KEY2)); Assert.assertFalse(lruCache.containsKey(KEY3)); lruCache.put(KEY2, VALUE2); Assert.assertEquals(VALUE1, lruCache.get(KEY1)); Assert.assertEquals(VALUE2, lruCache.get(KEY2)); Assert.assertFalse(lruCache.containsKey(KEY3)); lruCache.put(KEY3, VALUE3); Assert.assertFalse(lruCache.containsKey(KEY1)); Assert.assertEquals(VALUE2, lruCache.get(KEY2)); Assert.assertEquals(VALUE3, lruCache.get(KEY3)); lruCache.put(KEY1, VALUE1); Assert.assertEquals(VALUE1, lruCache.get(KEY1)); Assert.assertFalse(lruCache.containsKey(KEY2)); Assert.assertEquals(VALUE3, lruCache.get(KEY3)); }
@Test public void lruCacheWithCapacityZeroTest() { LRUCache<String, String> lruCache = createLRUCache(0); lruCache.put(KEY1, VALUE1); Assert.assertFalse(lruCache.containsKey(KEY1)); }
@Override protected boolean removeEldestEntry(Map.Entry<K, V> eldest) { return size() > this.capacity; } }
private synchronized void matchWay(RenderCallback renderCallback, final RenderContext renderContext, Closed closed, PolylineContainer way) { MatchingCacheKey matchingCacheKey = new MatchingCacheKey(way.getTags(), way.getUpperLeft().zoomLevel, closed); List<RenderInstruction> matchingList = this.wayMatchingCache.get(matchingCacheKey); if (matchingList != null) { // cache hit for (int i = 0, n = matchingList.size(); i < n; ++i) { matchingList.get(i).renderWay(renderCallback, renderContext, way); } return; } // cache miss matchingList = new ArrayList<RenderInstruction>(); for (int i = 0, n = this.rulesList.size(); i < n; ++i) { this.rulesList.get(i).matchWay(renderCallback, way, way.getUpperLeft(), closed, matchingList, renderContext); } this.wayMatchingCache.put(matchingCacheKey, matchingList); }
@Override protected boolean removeEldestEntry(Map.Entry<K, V> eldest) { return size() > this.capacity; } }
/** * Matches a node with the given parameters against this RenderTheme. * * @param renderCallback the callback implementation which will be executed on each match. * @param renderContext * @param poi the point of interest. */ public synchronized void matchNode(RenderCallback renderCallback, final RenderContext renderContext, PointOfInterest poi) { MatchingCacheKey matchingCacheKey = new MatchingCacheKey(poi.tags, renderContext.rendererJob.tile.zoomLevel, Closed.NO); List<RenderInstruction> matchingList = this.poiMatchingCache.get(matchingCacheKey); if (matchingList != null) { // cache hit for (int i = 0, n = matchingList.size(); i < n; ++i) { matchingList.get(i).renderNode(renderCallback, renderContext, poi); } return; } // cache miss matchingList = new ArrayList<RenderInstruction>(); for (int i = 0, n = this.rulesList.size(); i < n; ++i) { this.rulesList.get(i).matchNode(renderCallback, renderContext, matchingList, poi); } this.poiMatchingCache.put(matchingCacheKey, matchingList); }
RenderTheme(RenderThemeBuilder renderThemeBuilder) { this.baseStrokeWidth = renderThemeBuilder.baseStrokeWidth; this.baseTextSize = renderThemeBuilder.baseTextSize; this.hasBackgroundOutside = renderThemeBuilder.hasBackgroundOutside; this.mapBackground = renderThemeBuilder.mapBackground; this.mapBackgroundOutside = renderThemeBuilder.mapBackgroundOutside; this.rulesList = new ArrayList<>(); this.poiMatchingCache = new LRUCache<>(MATCHING_CACHE_SIZE); this.wayMatchingCache = new LRUCache<>(MATCHING_CACHE_SIZE); }
/** * @param capacity the maximum capacity of this cache. * @throws IllegalArgumentException if the capacity is negative. */ public LRUCache(int capacity) { super(calculateInitialCapacity(capacity), LOAD_FACTOR, true); this.capacity = capacity; }
private synchronized void matchWay(RenderCallback renderCallback, final RenderContext renderContext, Closed closed, PolylineContainer way) { MatchingCacheKey matchingCacheKey = new MatchingCacheKey(way.getTags(), way.getUpperLeft().zoomLevel, closed); List<RenderInstruction> matchingList = this.wayMatchingCache.get(matchingCacheKey); if (matchingList != null) { // cache hit for (int i = 0, n = matchingList.size(); i < n; ++i) { matchingList.get(i).renderWay(renderCallback, renderContext, way); } return; } // cache miss matchingList = new ArrayList<RenderInstruction>(); for (int i = 0, n = this.rulesList.size(); i < n; ++i) { this.rulesList.get(i).matchWay(renderCallback, way, way.getUpperLeft(), closed, matchingList, renderContext); } this.wayMatchingCache.put(matchingCacheKey, matchingList); }
private static LRUCache<String, String> createLRUCache(int capacity) { LRUCache<String, String> lruCache = new LRUCache<>(capacity); Assert.assertEquals(capacity, lruCache.capacity); return lruCache; }
/** * @param inputChannel the map file from which the index should be read and cached. * @param capacity the maximum number of entries in the cache. * @throws IllegalArgumentException if the capacity is negative. */ IndexCache(FileChannel inputChannel, int capacity) { this.fileChannel = inputChannel; this.map = new LRUCache<IndexCacheEntryKey, byte[]>(capacity); }
RenderTheme(RenderThemeBuilder renderThemeBuilder) { this.baseStrokeWidth = renderThemeBuilder.baseStrokeWidth; this.baseTextSize = renderThemeBuilder.baseTextSize; this.hasBackgroundOutside = renderThemeBuilder.hasBackgroundOutside; this.mapBackground = renderThemeBuilder.mapBackground; this.mapBackgroundOutside = renderThemeBuilder.mapBackgroundOutside; this.rulesList = new ArrayList<>(); this.poiMatchingCache = new LRUCache<>(MATCHING_CACHE_SIZE); this.wayMatchingCache = new LRUCache<>(MATCHING_CACHE_SIZE); }