Example usage for com.google.common.cache LoadingCache size

List of usage examples for com.google.common.cache LoadingCache size

Introduction

In this page you can find the example usage for com.google.common.cache LoadingCache size.

Prototype

long size();

Source Link

Document

Returns the approximate number of entries in this cache.

Usage

From source file:org.jclouds.byon.suppliers.NodesParsedFromSupplier.java

@Override
public LoadingCache<String, Node> get() {
    LoadingCache<String, Node> nodes = parser.apply(supplier.get());
    checkState(nodes != null && nodes.size() > 0, "no nodes parsed from supplier: %s", supplier);
    return nodes;
}

From source file:org.apache.tajo.pullserver.PullServerUtil.java

private static SearchResult searchCorrespondPart(String queryId, String ebSeqId, Path outDir, String startKey,
        String endKey, boolean last, LoadingCache<IndexCacheKey, BSTIndexReader> indexReaderCache,
        int lowCacheHitCheckThreshold) throws IOException, ExecutionException {
    BSTIndexReader idxReader = indexReaderCache.get(new IndexCacheKey(outDir, queryId, ebSeqId));
    idxReader.retain();/*  w w w.  ja  v  a2  s  .c  om*/

    File data;
    long startOffset;
    long endOffset;
    try {
        if (LOG.isDebugEnabled()) {
            if (indexReaderCache.size() > lowCacheHitCheckThreshold
                    && indexReaderCache.stats().hitRate() < 0.5) {
                LOG.debug("Too low cache hit rate: " + indexReaderCache.stats());
            }
        }

        Tuple indexedFirst = idxReader.getFirstKey();
        Tuple indexedLast = idxReader.getLastKey();

        if (indexedFirst == null && indexedLast == null) { // if # of rows is zero
            if (LOG.isDebugEnabled()) {
                LOG.debug("There is no contents");
            }
            return null;
        }

        byte[] startBytes = Base64.decodeBase64(startKey);
        byte[] endBytes = Base64.decodeBase64(endKey);

        Tuple start;
        Tuple end;
        Schema keySchema = idxReader.getKeySchema();
        RowStoreDecoder decoder = RowStoreUtil.createDecoder(keySchema);

        try {
            start = decoder.toTuple(startBytes);
        } catch (Throwable t) {
            throw new IllegalArgumentException(
                    "StartKey: " + startKey + ", decoded byte size: " + startBytes.length, t);
        }

        try {
            end = decoder.toTuple(endBytes);
        } catch (Throwable t) {
            throw new IllegalArgumentException("EndKey: " + endKey + ", decoded byte size: " + endBytes.length,
                    t);
        }

        data = new File(URI.create(outDir.toUri() + "/output"));
        if (LOG.isDebugEnabled()) {
            LOG.debug("GET Request for " + data.getAbsolutePath() + " (start=" + start + ", end=" + end
                    + (last ? ", last=true" : "") + ")");
        }

        TupleComparator comparator = idxReader.getComparator();

        if (comparator.compare(end, indexedFirst) < 0 || comparator.compare(indexedLast, start) < 0) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Out of Scope (indexed data [" + indexedFirst + ", " + indexedLast
                        + "], but request start:" + start + ", end: " + end);
            }
            return null;
        }

        try {
            idxReader.init();
            startOffset = idxReader.find(start);
        } catch (IOException ioe) {
            LOG.error("State Dump (the requested range: " + "[" + start + ", " + end + ")" + ", idx min: "
                    + idxReader.getFirstKey() + ", idx max: " + idxReader.getLastKey());
            throw ioe;
        }
        try {
            endOffset = idxReader.find(end);
            if (endOffset == -1) {
                endOffset = idxReader.find(end, true);
            }
        } catch (IOException ioe) {
            LOG.error("State Dump (the requested range: " + "[" + start + ", " + end + ")" + ", idx min: "
                    + idxReader.getFirstKey() + ", idx max: " + idxReader.getLastKey());
            throw ioe;
        }

        // if startOffset == -1 then case 2-1 or case 3
        if (startOffset == -1) { // this is a hack
            // if case 2-1 or case 3
            try {
                startOffset = idxReader.find(start, true);
            } catch (IOException ioe) {
                LOG.error("State Dump (the requested range: " + "[" + start + ", " + end + ")" + ", idx min: "
                        + idxReader.getFirstKey() + ", idx max: " + idxReader.getLastKey());
                throw ioe;
            }
        }

        if (startOffset == -1) {
            throw new IllegalStateException("startOffset " + startOffset + " is negative \n"
                    + "State Dump (the requested range: " + "[" + start + ", " + end + ")" + ", idx min: "
                    + idxReader.getFirstKey() + ", idx max: " + idxReader.getLastKey());
        }

        // if greater than indexed values
        if (last || (endOffset == -1 && comparator.compare(idxReader.getLastKey(), end) < 0)) {
            endOffset = data.length();
        }
    } finally {
        idxReader.release();
    }

    return new SearchResult(data, startOffset, endOffset);
}

From source file:org.waveprotocol.box.server.persistence.memory.MemorySnapshotStore.java

@Override
public void delete(WaveletName waveletName) throws PersistenceException {
    LoadingCache<WaveletId, MemorySnapshotAccess> waveData = access.getIfPresent(waveletName.waveId);
    if (waveData != null) {
        waveData.invalidate(waveletName.waveletId);
        if (waveData.size() == 0) {
            access.invalidate(waveletName.waveId);
        }//from w  ww  . ja  v a  2  s  .c  o m
    }
}

From source file:org.apache.s4.core.util.S4Metrics.java

public void createCacheGauges(ProcessingElement prototype,
        final LoadingCache<String, ProcessingElement> cache) {

    Metrics.newGauge(prototype.getClass(), prototype.getClass().getName() + "-cache-entries",
            new Gauge<Long>() {

                @Override/*from  www. ja v a2 s . c  o  m*/
                public Long value() {
                    return cache.size();
                }
            });
    Metrics.newGauge(prototype.getClass(), prototype.getClass().getName() + "-cache-evictions",
            new Gauge<Long>() {

                @Override
                public Long value() {
                    return cache.stats().evictionCount();
                }
            });
    Metrics.newGauge(prototype.getClass(), prototype.getClass().getName() + "-cache-misses", new Gauge<Long>() {

        @Override
        public Long value() {
            return cache.stats().missCount();
        }
    });
}