Package org.chaidb.db.helper.cache.measurable

Examples of org.chaidb.db.helper.cache.measurable.Measurable


        final long sizeToBePlused;
        // check if specified key exists
        final boolean keyExists = map.containsKey(key);

        if (keyExists) {
            Measurable existingValue = (Measurable) map.get(key);
            final long existingValueSize = (existingValue == null) ? 0 : existingValue.getRetainedSize();
            sizeToBePlused = valueSize - existingValueSize;
        } else {
            sizeToBePlused = keySize + valueSize;
        }

        // throw exception if size of key/value pair is even larger than the max capacity
        if (keySize + valueSize > sizeBoundaryInBytes) {
            throw new SizeTooLargeException("required=" + sizeToBePlused + "; max capacity=" + sizeBoundaryInBytes);
        }
        // shortcut for key/value pair whose size equals to the max capacity
        if (keySize + valueSize == sizeBoundaryInBytes) {
            map.clear();
            retainedSizeInBytes = sizeBoundaryInBytes;
            return (Measurable) map.put(key, value);
        }

        if (sizeToBePlused + retainedSizeInBytes <= sizeBoundaryInBytes) {
            retainedSizeInBytes += sizeToBePlused;
            return (Measurable) map.put(key, value);
        } else { // have to kickout some existing key'value pairs
            final long smallestSizeToKickout = sizeToBePlused + retainedSizeInBytes - sizeBoundaryInBytes;
            long kickoutSize = 0;
            while (kickoutSize < smallestSizeToKickout) {
                // Get the eldest key.
                Measurable kickoutKey = getEldestKey();
                // If no more key available, break out.
                if (kickoutKey == NO_KEY) {
                    break;
                }
                // Remove the key/value pair.
                Measurable kickoutValue = (Measurable) map.remove(kickoutKey);
                if ((key == kickoutKey) || (key != null && key.equals(kickoutKey))) {
                    // If existing key is kicked out, don't count their size into kickout size.
                    // Because it will be added back later.
                } else {
                    // Calculate size of key/value pair which has been kicked out
                    final long kickoutKeySize = (kickoutKey == null) ? 0 : kickoutKey.getRetainedSize();
                    final long kickoutValueSize = (kickoutValue == null) ? 0 : kickoutValue.getRetainedSize();
                    kickoutSize += (kickoutKeySize + kickoutValueSize);
                }
            }
            retainedSizeInBytes += (sizeToBePlused - kickoutSize);
            return (Measurable) map.put(key, value);
View Full Code Here


    public Measurable remove(Measurable key) {
        // check if specified key exists
        final boolean keyExists = map.containsKey(key);
        if (keyExists) {
            Measurable value = (Measurable) map.remove(key);
            final long keySize = (key == null) ? 0 : key.getRetainedSize();
            final long valueSize = (value == null) ? 0 : value.getRetainedSize();
            retainedSizeInBytes -= (keySize + valueSize);
            return value;
        } else {
            return null;
        }
View Full Code Here

    public long calculateRetainedSize() {
        java.util.HashMap copy = new java.util.HashMap(map);
        Iterator keyIterator = copy.keySet().iterator();
        long retainedSize = 0;
        while (keyIterator.hasNext()) {
            Measurable key = (Measurable) keyIterator.next();
            Measurable value = (Measurable) copy.get(key);
            final long keySize = (key == null) ? 0 : key.getRetainedSize();
            final long valueSize = (value == null) ? 0 : value.getRetainedSize();
            retainedSize += (keySize + valueSize);
        }
        return retainedSize;
    }
View Full Code Here

        final long sizeToBePlused;
        // check if specified key exists
        final boolean keyExists = map.containsKey(key);

        if (keyExists) {
            Measurable existingValue = (Measurable) map.get(key);
            final long existingValueSize = (existingValue == null) ? 0 : existingValue.getRetainedSize();
            sizeToBePlused = valueSize - existingValueSize;
        } else {
            sizeToBePlused = keySize + valueSize;
        }

        // throw exception if size of key/value pair is even larger than the max capacity
        if (keySize + valueSize > sizeBoundaryInBytes) {
            throw new SizeTooLargeException("required=" + sizeToBePlused + "; max capacity=" + sizeBoundaryInBytes);
        }
        // shortcut for key/value pair whose size equals to the max capacity
        if (keySize + valueSize == sizeBoundaryInBytes) {
            map.clear();
            retainedSizeInBytes = sizeBoundaryInBytes;
            return (Measurable) map.put(key, value);
        }

        if (sizeToBePlused + retainedSizeInBytes <= sizeBoundaryInBytes) {
            retainedSizeInBytes += sizeToBePlused;
            return (Measurable) map.put(key, value);
        } else { // have to kickout some existing key'value pairs
            final long smallestSizeToKickout = sizeToBePlused + retainedSizeInBytes - sizeBoundaryInBytes;
            long kickoutSize = 0;
            while (kickoutSize < smallestSizeToKickout) {
                // Get the eldest key.
                Measurable kickoutKey = getEldestKey();
                // If no more key available, break out.
                if (kickoutKey == NO_KEY) {
                    break;
                }
                // Remove the key/value pair.
                Measurable kickoutValue = (Measurable) map.remove(kickoutKey);
                if ((key == kickoutKey) || (key != null && key.equals(kickoutKey))) {
                    // If existing key is kicked out, don't count their size into kickout size.
                    // Because it will be added back later.
                } else {
                    // Calculate size of key/value pair which has been kicked out
                    final long kickoutKeySize = (kickoutKey == null) ? 0 : kickoutKey.getRetainedSize();
                    final long kickoutValueSize = (kickoutValue == null) ? 0 : kickoutValue.getRetainedSize();
                    kickoutSize += (kickoutKeySize + kickoutValueSize);
                }
            }
            retainedSizeInBytes += (sizeToBePlused - kickoutSize);
            return (Measurable) map.put(key, value);
View Full Code Here

    public Measurable remove(Measurable key) {
        // check if specified key exists
        final boolean keyExists = map.containsKey(key);
        if (keyExists) {
            Measurable value = (Measurable) map.remove(key);
            final long keySize = (key == null) ? 0 : key.getRetainedSize();
            final long valueSize = (value == null) ? 0 : value.getRetainedSize();
            retainedSizeInBytes -= (keySize + valueSize);
            return value;
        } else {
            return null;
        }
View Full Code Here

    public long calculateRetainedSize() {
        java.util.HashMap copy = new java.util.HashMap(map);
        Iterator keyIterator = copy.keySet().iterator();
        long retainedSize = 0;
        while (keyIterator.hasNext()) {
            Measurable key = (Measurable) keyIterator.next();
            Measurable value = (Measurable) copy.get(key);
            final long keySize = (key == null) ? 0 : key.getRetainedSize();
            final long valueSize = (value == null) ? 0 : value.getRetainedSize();
            retainedSize += (keySize + valueSize);
        }
        return retainedSize;
    }
View Full Code Here

    public Measurable getFromCache(Measurable key) throws CacheException {
        synchronized (algorithm) {
            if (!algorithm.containsKey(key)) {
                try {
                    Measurable value = (Measurable) listener.retrieve(key);
                    return algorithm.put(key, value);
                } catch (CachePersistenceException e) {
                    throw new CacheException("retrieve value from underlying storage error", e);
                } catch (SizeTooLargeException e) {
                    throw new CacheException("in-memory cache is NOT able to hold specified key/value pair.", e);
View Full Code Here

TOP

Related Classes of org.chaidb.db.helper.cache.measurable.Measurable

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.