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

LUCENE-9476 Add getBulkPath API for the Taxonomy index #2247

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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();
gautamworah96 marked this conversation as resolved.
Show resolved Hide resolved

// 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);
gautamworah96 marked this conversation as resolved.
Show resolved Hide resolved
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();
gautamworah96 marked this conversation as resolved.
Show resolved Hide resolved

// 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;
gautamworah96 marked this conversation as resolved.
Show resolved Hide resolved
}
}
return null;
}

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

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

for (int ord : ordinal) {
if (bulkPath[originalPosition.get(ord)] == null) {
gautamworah96 marked this conversation as resolved.
Show resolved Hide resolved
if (values == null
|| values.advanceExact(ord - indexReader.leaves().get(readerIndex).docBase) == false) {
gautamworah96 marked this conversation as resolved.
Show resolved Hide resolved
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);
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();
Expand Down
Expand Up @@ -569,4 +569,31 @@ public void testAccountable() throws Exception {
taxoReader.close();
dir.close();
}

public void testBulkPath() throws Exception {
gautamworah96 marked this conversation as resolved.
Show resolved Hide resolved
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);
gautamworah96 marked this conversation as resolved.
Show resolved Hide resolved
r1.close();
src.close();
}
}