-
Notifications
You must be signed in to change notification settings - Fork 2.6k
LUCENE-9476 Add getBulkPath API for the Taxonomy index #2247
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
Open
gautamworah96
wants to merge
5
commits into
apache:master
Choose a base branch
from
gautamworah96:LUCENE-9476
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
8a820f1
WIP: LUCENE-9476 Add basic functionality, basic tests
gautamworah96 93bbe5b
Misc. style fixes
gautamworah96 fd73d7b
Fixed small bugs, improved style. Perf test remaining
gautamworah96 f8425e4
Fixed a bug in multiple segments. The API now works for older indexes…
gautamworah96 0c53c3b
Style fix
gautamworah96 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,8 +16,10 @@ | |
*/ | ||
package org.apache.lucene.facet.taxonomy.directory; | ||
|
||
import com.carrotsearch.hppc.IntIntScatterMap; | ||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
@@ -320,18 +322,12 @@ public FacetLabel getPath(int ordinal) throws IOException { | |
// doOpenIfChanged, we need to ensure that the ordinal is one that this DTR | ||
// instance recognizes. Therefore we do this check up front, before we hit | ||
// the cache. | ||
if (ordinal < 0 || ordinal >= indexReader.maxDoc()) { | ||
return null; | ||
} | ||
int indexReaderMaxDoc = indexReader.maxDoc(); | ||
isOrdinalInIndexReaderRange(ordinal, indexReaderMaxDoc); | ||
gautamworah96 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// TODO: can we use an int-based hash impl, such as IntToObjectMap, | ||
// wrapped as LRU? | ||
Integer catIDInteger = Integer.valueOf(ordinal); | ||
synchronized (categoryCache) { | ||
FacetLabel res = categoryCache.get(catIDInteger); | ||
if (res != null) { | ||
return res; | ||
} | ||
FacetLabel ordinalPath = getPathFromCache(ordinal); | ||
gautamworah96 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (ordinalPath != null) { | ||
return ordinalPath; | ||
} | ||
|
||
int readerIndex = ReaderUtil.subIndex(ordinal, indexReader.leaves()); | ||
|
@@ -353,12 +349,104 @@ public FacetLabel getPath(int ordinal) throws IOException { | |
} | ||
|
||
synchronized (categoryCache) { | ||
categoryCache.put(catIDInteger, ret); | ||
categoryCache.put(ordinal, ret); | ||
} | ||
|
||
return ret; | ||
} | ||
|
||
private FacetLabel getPathFromCache(int ordinal) { | ||
// TODO: can we use an int-based hash impl, such as IntToObjectMap, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oooh that is a great idea, and low-hanging fruit, and would greatly reduce the RAM usage for this cache. I think Could you open a spinoff issue? |
||
// wrapped as LRU? | ||
synchronized (categoryCache) { | ||
return categoryCache.get(ordinal); | ||
} | ||
} | ||
|
||
private void isOrdinalInIndexReaderRange(int ordinal, int indexReaderMaxDoc) | ||
gautamworah96 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
throws IllegalArgumentException { | ||
if (ordinal < 0 || ordinal >= indexReaderMaxDoc) { | ||
throw new IllegalArgumentException( | ||
"ordinal " | ||
+ ordinal | ||
+ " is out of the range of the indexReader " | ||
+ indexReader.toString()); | ||
} | ||
} | ||
|
||
/** | ||
* Returns an array of FacetLabels for a given array of ordinals. | ||
* | ||
* <p>This API is generally faster than iteratively calling {@link | ||
* org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyReader#getPath} over an array of | ||
gautamworah96 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* ordinals. | ||
* | ||
* <p>This API is only available for Lucene indexes created with 8.7+ codec because it uses | ||
* BinaryDocValues instead of StoredFields. Use the {@link | ||
* org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyReader#getPath} method for indices | ||
* created with codec version older than 8.7. | ||
* | ||
* @param ordinals Array of ordinals that are assigned to categories inserted into the taxonomy | ||
* index | ||
* @throws IOException if the taxonomy index is created using the older StoredFields based codec. | ||
*/ | ||
public FacetLabel[] getBulkPath(int... ordinals) throws IOException { | ||
ensureOpen(); | ||
|
||
FacetLabel[] bulkPath = new FacetLabel[ordinals.length]; | ||
// remember the original positions of ordinals before they are sorted | ||
IntIntScatterMap originalPosition = new IntIntScatterMap(); | ||
int indexReaderMaxDoc = indexReader.maxDoc(); | ||
for (int i = 0; i < ordinals.length; i++) { | ||
// check whether the ordinal is valid before accessing the cache | ||
isOrdinalInIndexReaderRange(ordinals[i], indexReaderMaxDoc); | ||
// check the cache before trying to find it in the index | ||
FacetLabel ordinalPath = getPathFromCache(ordinals[i]); | ||
if (ordinalPath != null) { | ||
bulkPath[i] = ordinalPath; | ||
} | ||
originalPosition.put(ordinals[i], i); | ||
gautamworah96 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
Arrays.sort(ordinals); | ||
int readerIndex; | ||
int leafReaderMaxDoc = 0; | ||
int leafReaderDocBase = 0; | ||
LeafReader leafReader; | ||
LeafReaderContext leafReaderContext = null; | ||
BinaryDocValues values = null; | ||
|
||
for (int ord : ordinals) { | ||
if (bulkPath[originalPosition.get(ord)] == null) { | ||
gautamworah96 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (values == null || ord > leafReaderMaxDoc) { | ||
|
||
readerIndex = ReaderUtil.subIndex(ord, indexReader.leaves()); | ||
leafReaderContext = indexReader.leaves().get(readerIndex); | ||
leafReader = leafReaderContext.reader(); | ||
leafReaderMaxDoc = leafReader.maxDoc(); | ||
leafReaderDocBase = leafReaderContext.docBase; | ||
values = leafReader.getBinaryDocValues(Consts.FULL); | ||
|
||
// this check is only needed once to confirm that the index uses BinaryDocValues | ||
boolean success = values.advanceExact(ord - leafReaderDocBase); | ||
if (success == false) { | ||
throw new IOException( | ||
gautamworah96 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"the taxonomy index is created using the older StoredFields format which uses a Lucene " | ||
+ "codec older than 8.7. Use the getPath(int ordinal) API iteratively instead."); | ||
} | ||
} | ||
values.advanceExact(ord - leafReaderDocBase); | ||
gautamworah96 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
bulkPath[originalPosition.get(ord)] = | ||
new FacetLabel(FacetsConfig.stringToPath(values.binaryValue().utf8ToString())); | ||
synchronized (categoryCache) { | ||
gautamworah96 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
categoryCache.put(ord, bulkPath[originalPosition.get(ord)]); | ||
} | ||
} | ||
} | ||
|
||
return bulkPath; | ||
} | ||
|
||
@Override | ||
public int getSize() { | ||
ensureOpen(); | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.