Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds ability to disable caching in DirectoryTaxonomyReader #238

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions src/Lucene.Net.Facet/Taxonomy/Directory/DirectoryTaxonomyReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ private class Int32Class
// TODO: test DoubleBarrelLRUCache and consider using it instead
private LRUHashMap<FacetLabel, Int32Class> ordinalCache;
private LRUHashMap<int, FacetLabel> categoryCache;

// Disable cache
public bool CacheDisabled { get; set; }

private volatile TaxonomyIndexArrays taxoArrays;

Expand All @@ -85,7 +88,7 @@ private DirectoryTaxonomyReader(DirectoryReader indexReader, DirectoryTaxonomyWr
// use the same instance of the cache, note the protective code in getOrdinal and getPath
this.ordinalCache = ordinalCache ?? new LRUHashMap<FacetLabel, Int32Class>(DEFAULT_CACHE_VALUE);
this.categoryCache = categoryCache ?? new LRUHashMap<int, FacetLabel>(DEFAULT_CACHE_VALUE);

this.CacheDisabled = false;
this.taxoArrays = taxoArrays != null ? new TaxonomyIndexArrays(indexReader, taxoArrays) : null;
}

Expand All @@ -106,6 +109,7 @@ public DirectoryTaxonomyReader(Directory directory)

ordinalCache = new LRUHashMap<FacetLabel, Int32Class>(DEFAULT_CACHE_VALUE);
categoryCache = new LRUHashMap<int, FacetLabel>(DEFAULT_CACHE_VALUE);
CacheDisabled = false;
}

/// <summary>
Expand All @@ -126,6 +130,7 @@ public DirectoryTaxonomyReader(DirectoryTaxonomyWriter taxoWriter)

ordinalCache = new LRUHashMap<FacetLabel, Int32Class>(DEFAULT_CACHE_VALUE);
categoryCache = new LRUHashMap<int, FacetLabel>(DEFAULT_CACHE_VALUE);
CacheDisabled = false;
}

private void InitTaxoArrays()
Expand Down Expand Up @@ -332,7 +337,8 @@ public override int GetOrdinal(FacetLabel cp)

// LUCENENET: Lock was removed here because the underlying cache is thread-safe,
// and removing the lock seems to make the performance better.
ordinalCache.Put(cp, new Int32Class { Value = Convert.ToInt32(ret, CultureInfo.InvariantCulture) });
if(!CacheDisabled)
ordinalCache.Put(cp, new Int32Class { Value = Convert.ToInt32(ret, CultureInfo.InvariantCulture) });
}

return ret;
Expand Down Expand Up @@ -367,7 +373,8 @@ public override FacetLabel GetPath(int ordinal)
res = new FacetLabel(FacetsConfig.StringToPath(doc.Get(Consts.FULL)));
// LUCENENET: Lock was removed here because the underlying cache is thread-safe,
// and removing the lock seems to make the performance better.
categoryCache.Put(ordinal, res);
if(!CacheDisabled)
categoryCache.Put(ordinal, res);

return res;
}
Expand Down