/** * Let t = percentage of max memory. * Let e = round(log_2 t). * Then, we choose capacity = 2^e/(size of reference), * unless it is outside the close interval [1, 2^30]. */ public static int computeCapacity(double percentage, String mapName) { return computeCapacity(Runtime.getRuntime().maxMemory(), percentage, mapName); }
@Override public Iterator<E> iterator() { final Iterator<E> iter = super.iterator(); return new Iterator<E>() { @Override public boolean hasNext() { return iter.hasNext(); } @Override public E next() { return iter.next(); } @Override public void remove() { // It would be tricky to support this because LightWeightCache#remove // may evict multiple elements via evictExpiredEntries. throw new UnsupportedOperationException("Remove via iterator is " + "not supported for LightWeightCache"); } }; } }
@Override public E get(final K key) { //validate key if (key == null) { throw new NullPointerException("key == null"); } //find element final int index = getIndex(key); for(LinkedElement e = entries[index]; e != null; e = e.getNext()) { if (e.equals(key)) { return convert(e); } } //element not found return null; }
@Test(timeout=60000) public void testRemoveAllViaIterator() { ArrayList<Integer> list = getRandomList(100, 123); LightWeightGSet<TestElement, TestElement> set = new LightWeightGSet<TestElement, TestElement>(16); for (Integer i : list) { set.put(new TestElement(i)); } for (Iterator<TestElement> iter = set.iterator(); iter.hasNext(); ) { iter.next(); iter.remove(); } Assert.assertEquals(0, set.size()); }
static INodeMap newInstance(INodeDirectory rootDir) { // Compute the map capacity by allocating 1% of total memory int capacity = LightWeightGSet.computeCapacity(1, "INodeMap"); GSet<INode, INodeWithAdditionalFields> map = new LightWeightGSet<>(capacity); map.put(rootDir); return new INodeMap(map); }
= new LightWeightGSet<Integer, Integer>(16); try { gset.contains(null); Assert.fail(); } catch(NullPointerException e) { = new LightWeightGSet<Integer, Integer>(16); try { gset.get(null); Assert.fail(); } catch(NullPointerException e) { = new LightWeightGSet<Integer, Integer>(16); try { gset.put(null); Assert.fail(); } catch(NullPointerException e) { gset.put(1); Assert.fail(); } catch(IllegalArgumentException e) {
@Override public E put(final E element) { E existing = super.put(element); expandIfNecessary(); return existing; }
@Override public boolean contains(final K key) { return get(key) != null; }
private E evict() { @SuppressWarnings("unchecked") final E polled = (E)queue.poll(); final E removed = super.remove(polled); Preconditions.checkState(removed == polled); return polled; }
public static void clear(RetryCache cache) { if (cache != null) { cache.set.clear(); cache.incrCacheClearedCounter(); } } }
/** * @param recommended_length Recommended size of the internal array. */ public LightWeightGSet(final int recommended_length) { final int actual = actualArrayLength(recommended_length); if (LOG.isDebugEnabled()) { LOG.debug("recommended=" + recommended_length + ", actual=" + actual); } entries = new LinkedElement[actual]; hash_mask = entries.length - 1; }
private static GSet<IntElement, IntElement> createGSet(final IntElement[] data) { final GSet<IntElement, IntElement> gset = new LightWeightGSet<IntElement, IntElement>(8); for(int i = 1; i < data.length; i++) { gset.put(data[i]); } return gset; }
@Test(timeout=60000) public void testRemoveAllViaIterator() { ArrayList<Integer> list = getRandomList(100, 123); LightWeightGSet<TestElement, TestElement> set = new LightWeightGSet<TestElement, TestElement>(16); for (Integer i : list) { set.put(new TestElement(i)); } for (Iterator<TestElement> iter = set.iterator(); iter.hasNext(); ) { iter.next(); iter.remove(); } Assert.assertEquals(0, set.size()); }
CacheManager(FSNamesystem namesystem, Configuration conf, BlockManager blockManager) { this.namesystem = namesystem; this.blockManager = blockManager; this.nextDirectiveId = 1; this.enabled = conf.getBoolean(DFS_NAMENODE_CACHING_ENABLED_KEY, DFS_NAMENODE_CACHING_ENABLED_DEFAULT); this.maxListCachePoolsResponses = conf.getInt( DFS_NAMENODE_LIST_CACHE_POOLS_NUM_RESPONSES, DFS_NAMENODE_LIST_CACHE_POOLS_NUM_RESPONSES_DEFAULT); this.maxListCacheDirectivesNumResponses = conf.getInt( DFS_NAMENODE_LIST_CACHE_DIRECTIVES_NUM_RESPONSES, DFS_NAMENODE_LIST_CACHE_DIRECTIVES_NUM_RESPONSES_DEFAULT); scanIntervalMs = conf.getLong( DFS_NAMENODE_PATH_BASED_CACHE_REFRESH_INTERVAL_MS, DFS_NAMENODE_PATH_BASED_CACHE_REFRESH_INTERVAL_MS_DEFAULT); float cachedBlocksPercent = conf.getFloat( DFS_NAMENODE_PATH_BASED_CACHE_BLOCK_MAP_ALLOCATION_PERCENT, DFS_NAMENODE_PATH_BASED_CACHE_BLOCK_MAP_ALLOCATION_PERCENT_DEFAULT); if (cachedBlocksPercent < MIN_CACHED_BLOCKS_PERCENT) { LOG.info("Using minimum value {} for {}", MIN_CACHED_BLOCKS_PERCENT, DFS_NAMENODE_PATH_BASED_CACHE_BLOCK_MAP_ALLOCATION_PERCENT); cachedBlocksPercent = MIN_CACHED_BLOCKS_PERCENT; } this.cachedBlocks = enabled ? new LightWeightGSet<CachedBlock, CachedBlock>( LightWeightGSet.computeCapacity(cachedBlocksPercent, "cachedBlocks")) : new LightWeightGSet<>(0); }
@Override public E put(final E element) { // validate element if (element == null) { throw new NullPointerException("Null element is not supported."); } LinkedElement e = null; try { e = (LinkedElement)element; } catch (ClassCastException ex) { throw new HadoopIllegalArgumentException( "!(element instanceof LinkedElement), element.getClass()=" + element.getClass()); } // find index final int index = getIndex(element); // remove if it already exists final E existing = remove(index, element); // insert the element to the head of the linked list modification++; size++; e.setNext(entries[index]); entries[index] = e; return existing; }
public void addCacheEntryWithPayload(byte[] clientId, int callId, Object payload) { // since the entry is loaded from editlog, we can assume it succeeded. CacheEntry newEntry = new CacheEntryWithPayload(clientId, callId, payload, System.nanoTime() + expirationTime, true); lock.lock(); try { set.put(newEntry); } finally { lock.unlock(); } retryCacheMetrics.incrCacheUpdated(); }
@Override public E get(final K key) { //validate key if (key == null) { throw new NullPointerException("key == null"); } //find element final int index = getIndex(key); for(LinkedElement e = entries[index]; e != null; e = e.getNext()) { if (e.equals(key)) { return convert(e); } } //element not found return null; }