Search code examples
jakarta-eesolrlucenesearch-enginelucene.net

Function of the Cache in FieldcacheImpl


I tried to analyse the FieldCacheImpl in Solr 3.5.0, can any one explain me or give me pointer to know what does the abstract Class Cache is performing upfront?

Thanks and Regards,

Jeyaprakash.


Solution

  • As you can see in FieldCache.java, there are several different methods to load the fieldcache (getBytes, getInts, getStringIndex, getStrings, ...). This is why FieldCacheImpl maintains a map (type -> cache).

    Lucene segments are write-once and then read-only, so you want to load the field cache only once, and then always re-use the same instance. Cache is a helper class that helps achieve this: it is a wrapper around a map (segment -> map (field -> fieldcache instance)). (By fieldcache instance, I mean a byte[] for the getBytes method, a String[] for the getStrings method, ...)

    • When a segment is not used anymore (if there is no more open reader using it), you want the GC to be able to reclaim the fieldcache, which is why this map is a WeakHashMap.

    • There are different Cache implementations because the logic is not the same when loading (for example) a String cache and a StringIndex cache (see Cache#createValue(AtomicReader, Entry, boolean)).