org.hd.d.pg2k.webSvr.util
Class SearchResultSimpleCache

java.lang.Object
  extended by org.hd.d.pg2k.webSvr.util.SearchResultSimpleCache

public final class SearchResultSimpleCache
extends java.lang.Object

Bean for cached search results. Uses SearchUtils.doRelatedExhibitsSearch() to compute results lazily and caches the results.

Cache size is limited at most in proportion to the number of exhibits (keyed from their names and from canonicalised forms of those names for shared results) and may be capped to a fixed ceiling below that to retain only the most popular lookups.


Field Summary
static int ABS_MAX_RESULTS
          Absolute maximum number of results to store/cache per exhibit.
private  MemoryTools.CacheMiniMap<java.lang.Integer,java.util.List<Name.ExhibitFull>> fromCanonKey
          Map from condensed canonical key to immutable ordered list of full-name results, best match first.
private  MemoryTools.CacheMiniMap<Name.ExhibitFull,java.util.List<Name.ExhibitFull>> fromFullName
          Map from full exhibit name to immutable ordered list of full-name results, best match first.
private static DataSourceBean.AEPLinkedKey fullSearchCacheKey
          Private lookup key for full searches of similar items; never null.
 
Constructor Summary
SearchResultSimpleCache()
           
 
Method Summary
private static int computeCacheMaxSize()
          Compute maximum cache size, partly based on heap size; strictly positive.
static Name computeRawSearchTermFromExhibitName(Name.ExhibitFull exhibitFullName)
          Compute the (immutable) raw core lookup term in the by-word index from text in the exhibit name itself; never null.
static java.util.List<Name.ExhibitFull> doCachedCatPageSimilarItems(DataSourceBean dsb, Name.ExhibitFull exhibitName, int maxToShow)
          Do cached lookup for cat page "similar items"; never null.
static java.util.concurrent.Future<java.util.List<Name.ExhibitFull>> doCachedCatPageSimilarItemsFuture(DataSourceBean dsb, Name.ExhibitFull exhibitFullName, int maxResults)
          Do cached search lookup as for doCachedCatPageSimilarItems() but asynchronously; never null.
static java.util.concurrent.Future<java.util.List<Name.ExhibitFull>> doCachedCatPageSimilarItemsFuture(DataSourceBean dsb, Name.ExhibitFull exhibitFullName, int maxResults, boolean discardable)
          Do cached search lookup as for doCachedCatPageSimilarItems() but asynchronously; never null.
 java.util.List<Name.ExhibitFull> getRelatedExhibits(DataSourceBean dataSource, Name.ExhibitFull exhibitFullName, int maxResults)
          Do cached search lookup on given full exhibit name returning full exhibit name results; never null.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

fromFullName

private final MemoryTools.CacheMiniMap<Name.ExhibitFull,java.util.List<Name.ExhibitFull>> fromFullName
Map from full exhibit name to immutable ordered list of full-name results, best match first. Thread-safe.

Capped size to minimise memory footprint while still giving good performance. Falling through the cache and doing the query from scratch shouldn't be hideously slow anyway.

Since the actual cached items are small (even compared to Map.Entry overheads), a relatively large number can be cached and it is not worth a complex high-overhead scheme, eg using SoftReferences, to manage them.

Limit maximum cache size in proportion to heap size; strictly positive. Aim to allow ~10000 entries per 1GB of heap space (at initialisation of this instance), with a minimum of tens/hundreds to cover popular pages, etc, at relatively low memory cost.

We're prepared to discard everything under acute memory pressure.


fromCanonKey

private final MemoryTools.CacheMiniMap<java.lang.Integer,java.util.List<Name.ExhibitFull>> fromCanonKey
Map from condensed canonical key to immutable ordered list of full-name results, best match first. Thread-safe. As fromFullName.


ABS_MAX_RESULTS

public static final int ABS_MAX_RESULTS
Absolute maximum number of results to store/cache per exhibit. Limits storage requirements, and is as many as we should ever want to show on one catalogue page.

If the catalogue page logic actually uses this value as the maximum request size then we should get optimal CPU and memory use, and minimal heap churn.

See Also:
Constant Field Values

fullSearchCacheKey

private static final DataSourceBean.AEPLinkedKey fullSearchCacheKey
Private lookup key for full searches of similar items; never null.

Constructor Detail

SearchResultSimpleCache

public SearchResultSimpleCache()
Method Detail

computeCacheMaxSize

private static int computeCacheMaxSize()
Compute maximum cache size, partly based on heap size; strictly positive.


getRelatedExhibits

public java.util.List<Name.ExhibitFull> getRelatedExhibits(DataSourceBean dataSource,
                                                           Name.ExhibitFull exhibitFullName,
                                                           int maxResults)
                                                    throws java.io.IOException
Do cached search lookup on given full exhibit name returning full exhibit name results; never null. If fewer items are requested than computed/cached, then the best (first) cached items are returned.

The (immutable) results are limited to at most ABS_MAX_RESULTS items.

Excludes the exhibit itself and variants of the exhibit (same main words stem, etc, giving same canonicalised search term) on the assumption that they may already have been shown in other ways.

This allows potentially-redundant requests to execute in parallel at the risk of a little wasted work so as to maximise potential concurrency.

We do NOT cache any results if we don't believe the index to be up-to-date (eg to avoid cacheing stale/empty values).

Returns:
private String[] of result short exhibit names, best first; possibly zero-length but never null
Throws:
java.io.IOException

computeRawSearchTermFromExhibitName

public static Name computeRawSearchTermFromExhibitName(Name.ExhibitFull exhibitFullName)
Compute the (immutable) raw core lookup term in the by-word index from text in the exhibit name itself; never null. This uses the main words component and attribute words, mono-cased.

This keeps the constituent words in the order supplied, and always retains the first word (later duplicates may be discarded for example) as earlier words are taken to be more important.

Returns:
non-null, immutable, intern()ed, core lookup term for automated searches

doCachedCatPageSimilarItems

public static java.util.List<Name.ExhibitFull> doCachedCatPageSimilarItems(DataSourceBean dsb,
                                                                           Name.ExhibitFull exhibitName,
                                                                           int maxToShow)
Do cached lookup for cat page "similar items"; never null. The (immutable) result is keyed to the exhibit name and AEP.


doCachedCatPageSimilarItemsFuture

public static java.util.concurrent.Future<java.util.List<Name.ExhibitFull>> doCachedCatPageSimilarItemsFuture(DataSourceBean dsb,
                                                                                                              Name.ExhibitFull exhibitFullName,
                                                                                                              int maxResults)
Do cached search lookup as for doCachedCatPageSimilarItems() but asynchronously; never null. The request is always run, possibly in this thread blocking the caller.


doCachedCatPageSimilarItemsFuture

public static java.util.concurrent.Future<java.util.List<Name.ExhibitFull>> doCachedCatPageSimilarItemsFuture(DataSourceBean dsb,
                                                                                                              Name.ExhibitFull exhibitFullName,
                                                                                                              int maxResults,
                                                                                                              boolean discardable)
Do cached search lookup as for doCachedCatPageSimilarItems() but asynchronously; never null.

Parameters:
discardable - if true then this call will never block but the request is discardable, ie may never even be scheduled to run or may run at low priority, or may immediately return a real result or may immediately return an empty result if there seems no prospect of getting a task queued, so code should not wait indefinitely for a task completion (eg with get()) that may never come

DHD Multimedia Gallery V1.57.21

Copyright (c) 1996-2011, Damon Hart-Davis. All rights reserved.