use of org.apache.lucene.util.Accountable in project lucene-solr by apache.
the class LRUCache method put.
@Override
public V put(K key, V value) {
synchronized (map) {
if (getState() == State.LIVE) {
stats.inserts.increment();
}
// increment local inserts regardless of state???
// it does make it more consistent with the current size...
inserts++;
// important to calc and add new ram bytes first so that removeEldestEntry can compare correctly
long keySize = DEFAULT_RAM_BYTES_USED;
if (maxRamBytes != Long.MAX_VALUE) {
if (key != null && key instanceof Accountable) {
keySize = ((Accountable) key).ramBytesUsed();
}
long valueSize = 0;
if (value != null) {
if (value instanceof Accountable) {
Accountable accountable = (Accountable) value;
valueSize = accountable.ramBytesUsed();
} else {
throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Cache: " + getName() + " is configured with maxRamBytes=" + RamUsageEstimator.humanReadableUnits(maxRamBytes) + " but its values do not implement org.apache.lucene.util.Accountable");
}
}
ramBytesUsed += keySize + valueSize + LINKED_HASHTABLE_RAM_BYTES_PER_ENTRY;
}
V old = map.put(key, value);
if (maxRamBytes != Long.MAX_VALUE && old != null) {
long bytesToDecrement = ((Accountable) old).ramBytesUsed();
// the key existed in the map but we added its size before the put, so let's back out
bytesToDecrement += LINKED_HASHTABLE_RAM_BYTES_PER_ENTRY;
if (key != null) {
if (key instanceof Accountable) {
Accountable aKey = (Accountable) key;
bytesToDecrement += aKey.ramBytesUsed();
} else {
bytesToDecrement += DEFAULT_RAM_BYTES_USED;
}
}
ramBytesUsed -= bytesToDecrement;
}
return old;
}
}
Aggregations