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 by the number of exhibits (keyed from their names plus canonicalised forms of those names for shared results) and may be capped to a fixed ceiling below that to retain only the most popular results.


Field Summary
static int ABS_MAX_RESULTS
          Absolute maximum number of results to store/cache per exhibit.
private static DataSourceBean.AEPLinkedKey fullSearchCacheKey
          Private lookup key for full searches of similar items; never null.
private  MemoryTools.SimpleLRUMap<Name,java.util.List<Name.ExhibitFull>> searchResults
          Map from full exhibit name (and from canonicalised search term) to immutable ordered list of full-name results, best match first.
 
Constructor Summary
SearchResultSimpleCache()
           
 
Method Summary
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.
 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

searchResults

private final MemoryTools.SimpleLRUMap<Name,java.util.List<Name.ExhibitFull>> searchResults
Map from full exhibit name (and from canonicalised search term) 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 that slow anyway.

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

Note also that the penalty of a cache miss is not that high, merely involving a slightly-slow lookup in the underlying in-memory by-word index.


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

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.


DHD Multimedia Gallery V1.53.0

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