Skip to content

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
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
Expand Down Expand Up @@ -314,7 +316,6 @@ public int getOrdinal(FacetLabel cp) throws IOException {

@Override
public FacetLabel getPath(int ordinal) throws IOException {
ensureOpen();

// Since the cache is shared with DTR instances allocated from
// doOpenIfChanged, we need to ensure that the ordinal is one that this DTR
Expand All @@ -324,14 +325,9 @@ public FacetLabel getPath(int ordinal) throws IOException {
return null;
}

// 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);
if (ordinalPath != null) {
return ordinalPath;
}

int readerIndex = ReaderUtil.subIndex(ordinal, indexReader.leaves());
Expand All @@ -353,12 +349,65 @@ public FacetLabel getPath(int ordinal) throws IOException {
}

synchronized (categoryCache) {
categoryCache.put(catIDInteger, ret);
categoryCache.put(ordinal, ret);
}

return ret;
}

private FacetLabel getPathFromCache(int ordinal) {
ensureOpen();

// TODO: can we use an int-based hash impl, such as IntToObjectMap,
Copy link
Member

Choose a reason for hiding this comment

The 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 DirectoryTaxonomyWriter also has such a cache that we could change to a native map.

Could you open a spinoff issue?

// wrapped as LRU?
synchronized (categoryCache) {
FacetLabel res = categoryCache.get(ordinal);
if (res != null) {
return res;
}
}
return null;
}

/* This API is only supported for indexes created with Lucene 8.7+ codec **/
public FacetLabel[] getBulkPath(int[] ordinal) throws IOException {
FacetLabel[] bulkPath = new FacetLabel[ordinal.length];
Map<Integer, Integer> originalPosition = new HashMap<>();
for (int i = 0; i < ordinal.length; i++) {
if (ordinal[i] < 0 || ordinal[i] >= indexReader.maxDoc()) {
return null;
}
FacetLabel ordinalPath = getPathFromCache(ordinal[i]);
if (ordinalPath != null) {
bulkPath[i] = ordinalPath;
}
originalPosition.put(ordinal[i], i);
}

Arrays.sort(ordinal);
int readerIndex = 0;
BinaryDocValues values = null;

for (int ord : ordinal) {
if (bulkPath[originalPosition.get(ord)] == null) {
if (values == null
|| values.advanceExact(ord - indexReader.leaves().get(readerIndex).docBase) == false) {
readerIndex = ReaderUtil.subIndex(ord, indexReader.leaves());
LeafReader leafReader = indexReader.leaves().get(readerIndex).reader();
values = leafReader.getBinaryDocValues(Consts.FULL);
assert values.advanceExact(ord - indexReader.leaves().get(readerIndex).docBase);
}
bulkPath[originalPosition.get(ord)] =
new FacetLabel(FacetsConfig.stringToPath(values.binaryValue().utf8ToString()));
synchronized (categoryCache) {
categoryCache.put(ord, bulkPath[originalPosition.get(ord)]);
}
}
}

return bulkPath;
}

@Override
public int getSize() {
ensureOpen();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -569,4 +569,31 @@ public void testAccountable() throws Exception {
taxoReader.close();
dir.close();
}

public void testBulkPath() throws Exception {
Directory src = newDirectory();
DirectoryTaxonomyWriter w = new DirectoryTaxonomyWriter(src);
String arr[] = new String[] {"a", "b", "c", "d", "e"};

FacetLabel allpaths[] = new FacetLabel[arr.length];
int allords[] = new int[arr.length];

for (int i = 0; i < arr.length; i++) {
allpaths[i] = new FacetLabel(arr[i]);
w.addCategory(allpaths[i]);
}
w.commit();
w.close();

DirectoryTaxonomyReader r1 = new DirectoryTaxonomyReader(src);

for (int i = 0; i < allpaths.length; i++) {
allords[i] = r1.getOrdinal(allpaths[i]);
}

FacetLabel allBulkpath[] = r1.getBulkPath(allords);
assertArrayEquals(allpaths, allBulkpath);
r1.close();
src.close();
}
}