Custom pruning strategy with SearcherLifetimeManager

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Custom pruning strategy with SearcherLifetimeManager

ivan cruces
Hi,

I am using SearcherLifetimeManager to implement pagination. The built-in
pruner PruneByAge doesn’t fit in my application as it doesn’t take into
account the last time each searcher was used.

I am trying to imlement a custom pruning strategy that prunes a searcher
based on the last time it was used. To do that I am implementing the
SearcherLifetimeManager.Pruner interface and I am using a Map to keep track
of the last used time of each searcher using the token returned by the
record method as a key:

private final SearcherLifetimeManager slm = new SearcherLifetimeManager();

public long record(IndexSearcher searcher) throws IOException {
        long token = this.slm.record(searcher);
        lastUsedTime.put(token, System.currentTimeMillis());
        return readerVersion;
    }

public void release(IndexSearcher searcher) throws IOException {
    this.slm.release(searcher);
    lastUsedTime.put(token, System.currentTimeMillis());
}

Unfortunatelly, the doPrune method in the Pruner interface doesn’t get the
searcher token as a parameter so I cannot get the lastUsedTime from the Map.


Inspecting the SearcherLifetimeManager source code I see that the token
value is actually the IndexReader *version* and that is what I am using as
a key in the doPrune method. But I think this approach is risky, as these
are implementation details that if change will break my code. Actually,
there is a comment in that class that suggest a future change on that:

// TODO: we don't have to use IR.getVersion to track;

// could be risky (if it's buggy); we could get better

// bug isolation if we assign our own private ID

private static class PruneByLastUsedTime implements
SearcherLifetimeManager.Pruner {

        @Override
        public boolean doPrune(double age, IndexSearcher searcher) {
            long readerVersion =
((DirectoryReader)searcher.getIndexReader()).getVersion();
            long now = System.currentTimeMillis();
            if (lastUsedTime.containsKey(readerVersion)) {
                return (lastUsedTime.get(readerVersion) +
EXPIRATION_TIMEOUT_MS) < now;
            }
            return false;
        }

Is this the right approach to implement a custom pruning strategy or am I
missing something?

Many thanks,
Iván.