Search in sources :

Example 16 with Accountable

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;
    }
}
Also used : Accountable(org.apache.lucene.util.Accountable) SolrException(org.apache.solr.common.SolrException)

Aggregations

Accountable (org.apache.lucene.util.Accountable)16 ShardId (org.elasticsearch.index.shard.ShardId)6 Document (org.apache.lucene.document.Document)5 DirectoryReader (org.apache.lucene.index.DirectoryReader)4 IndexWriter (org.apache.lucene.index.IndexWriter)4 RAMDirectory (org.apache.lucene.store.RAMDirectory)4 ElasticsearchDirectoryReader (org.elasticsearch.common.lucene.index.ElasticsearchDirectoryReader)4 StringField (org.apache.lucene.document.StringField)3 IndexWriterConfig (org.apache.lucene.index.IndexWriterConfig)3 IndexSearcher (org.apache.lucene.search.IndexSearcher)3 BitSetProducer (org.apache.lucene.search.join.BitSetProducer)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 StandardAnalyzer (org.apache.lucene.analysis.standard.StandardAnalyzer)2 IndexReader (org.apache.lucene.index.IndexReader)2 LeafReaderContext (org.apache.lucene.index.LeafReaderContext)2 LogByteSizeMergePolicy (org.apache.lucene.index.LogByteSizeMergePolicy)2 Term (org.apache.lucene.index.Term)2 TermQuery (org.apache.lucene.search.TermQuery)2 Directory (org.apache.lucene.store.Directory)2 IndexService (org.elasticsearch.index.IndexService)2