/** * Set the read cache size in MB. * * @param mb the cache size in MB. */ public void setCacheSize(int mb) { final long bytes = (long) mb * 1024 * 1024; if (cache != null) { cache.setMaxMemory(bytes); cache.clear(); } if (cacheChunkRef != null) { cacheChunkRef.setMaxMemory(bytes / 4); cacheChunkRef.clear(); } }
/** * Get the maximum cache size, in MB. * Note that this does not include the page chunk references cache, which is * 25% of the size of the page cache. * * @return the cache size */ public int getCacheSize() { if (cache == null) { return 0; } return (int) (cache.getMaxMemory() / 1024 / 1024); }
/** * Check whether there is a resident entry for the given key. This * method does not adjust the internal state of the cache. * * @param key the key (may not be null) * @return true if there is a resident entry */ public boolean containsKey(long key) { int hash = getHash(key); return getSegment(hash).containsKey(key, hash); }
/** * Remove an entry. Both resident and non-resident entries can be * removed. * * @param key the key (may not be null) * @return the old value, or null if there was no resident entry */ public V remove(long key) { int hash = getHash(key); int segmentIndex = getSegmentIndex(hash); Segment<V> s = segments[segmentIndex]; // check whether resize is required: synchronize on s, to avoid // concurrent resizes (concurrent reads read // from the old segment) synchronized (s) { s = resizeIfNeeded(s, segmentIndex); return s.remove(key, hash); } }
/** * Put the page in the cache. * * @param pos the page position * @param page the page * @param memory the memory used */ void cachePage(long pos, Page page, int memory) { if (cache != null) { cache.put(pos, page, memory); } }
@Override public synchronized FileChannel truncate(long newSize) throws IOException { cache.clear(); base.truncate(newSize); return this; }
/** * Get the amount of memory used for caching, in MB. * Note that this does not include the page chunk references cache, which is * 25% of the size of the page cache. * * @return the amount of memory used for caching */ public int getCacheSizeUsed() { if (cache == null) { return 0; } return (int) (cache.getUsedMemory() / 1024 / 1024); }
/** * Read a page. * * @param map the map * @param pos the page position * @return the page */ Page readPage(MVMap<?, ?> map, long pos) { if (pos == 0) { throw DataUtils.newIllegalStateException( DataUtils.ERROR_FILE_CORRUPT, "Position 0"); } Page p = cache == null ? null : cache.get(pos); if (p == null) { Chunk c = getChunk(pos); long filePos = c.block * BLOCK_SIZE; filePos += DataUtils.getPageOffset(pos); if (filePos < 0) { throw DataUtils.newIllegalStateException( DataUtils.ERROR_FILE_CORRUPT, "Negative position {0}", filePos); } long maxPos = (c.block + c.len) * BLOCK_SIZE; p = Page.read(fileStore, pos, map, filePos, maxPos); cachePage(pos, p, p.getMemory()); } return p; }
cache.remove(pos);
cache = new CacheLongKeyLIRS<>(cc); cc.maxMemory /= 4; cacheChunkRef = new CacheLongKeyLIRS<>(cc); } else { cache = null; if (cache != null && pgSplitSize > cache.getMaxItemSize()) { pgSplitSize = (int)cache.getMaxItemSize();
private void addToMap(Entry<V> e) { int index = getHash(e.key) & mask; e.mapNext = entries[index]; entries[index] = e; usedMemory += e.memory; mapSize++; }
/** * Specifies max allowed size of cache for deserialized offheap rows to avoid deserialization costs for most * frequently used ones. In general performance is better with greater cache size. Must be more than 128 items. * * @param size Cache size in items. */ @GridSpiConfiguration(optional = true) public void setMaxOffheapRowsCacheSize(int size) { A.ensure(size >= 128, "Offheap rows cache size must be not less than 128."); rowCache = new CacheLongKeyLIRS<>(size, 1, 128, 256); }
/** * Get the value for the given key if the entry is cached. This method does * not modify the internal state. * * @param key the key (may not be null) * @return the value, or null if there is no resident entry */ public V peek(long key) { Entry<V> e = find(key); return e == null ? null : e.value; }
/** * Check whether the given value is stored. * * @param value the value * @return true if it is stored */ public boolean containsValue(Object value) { return getMap().containsValue(value); }
/** * Get the value for the given key if the entry is cached. This method * adjusts the internal state of the cache sometimes, to ensure commonly * used entries stay in the cache. * * @param key the key (may not be null) * @return the value, or null if there is no resident entry */ public V get(long key) { int hash = getHash(key); return getSegment(hash).get(key, hash); }