Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
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
34 changes: 27 additions & 7 deletions src/main/java/io/anserini/index/IndexReaderUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -844,24 +844,44 @@ public static Map<String, String> getFieldInfoDescription(IndexReader reader) {
}

public static Path getIndex(String index) {
boolean isPrebuiltLabel = false;
try {
PrebuiltIndexHandler handler = new PrebuiltIndexHandler(index);
handler.initialize();
handler.download();
String indexLocation = handler.decompressIndex();
return Paths.get(indexLocation);
isPrebuiltLabel = true;
} catch (Exception e) {
// Not a prebuilt index -- continue to check local path
// isPrebuiltLabel remains false
}


boolean localExists = Files.exists(Paths.get(index));

if (isPrebuiltLabel && localExists) {
throw new IllegalArgumentException(String.format(
"Ambiguous index reference \"%s\": both a prebuilt index label and a local path exist. " +
"Please disambiguate by specifying a full local path or removing/renaming the local directory.", index));
}

if (isPrebuiltLabel) {
try {
PrebuiltIndexHandler handler = new PrebuiltIndexHandler(index);
handler.initialize();
handler.download();
String indexLocation = handler.decompressIndex();
return Paths.get(indexLocation);
} catch (Exception e) {
//Fall through.
}
}

// Try local path
Path indexPath = Paths.get(index);
if (Files.exists(indexPath)) {
return indexPath;
}

// Path doesn't exist locally + it's not a prebuilt index.
throw new IllegalArgumentException(String.format("\"%s\" does not appear to be a valid index.", index));
}
}

// This is needed by src/main/python/run_regression.py

Expand Down
27 changes: 22 additions & 5 deletions src/main/java/io/anserini/rerank/GenerateRerankerRequests.java
Original file line number Diff line number Diff line change
Expand Up @@ -194,15 +194,32 @@ public void getTopics(String topicsFile) throws IOException {
}

public IndexReader getIndexReader(String index) {
if (!Files.exists(Paths.get(index))) { // get inverted index if index doesn't exist locally
String resolvedIndex;

boolean isPrebuiltLabel = IndexInfo.contains(index);
boolean localExists = Files.exists(Paths.get(index));

// If both a prebuilt label and a local path exist, fail fast with
// a clear error to force disambiguation.
if (isPrebuiltLabel && localExists) {
throw new IllegalArgumentException(String.format(
"Ambiguous index reference \"%s\": both a prebuilt index label and a local path exist. " +
"Please disambiguate by specifying a full local path or removing/renaming the local directory.", index));
}

if (isPrebuiltLabel) {
IndexInfo currentIndex = IndexInfo.get(index);
index = IndexReaderUtils.getIndex(currentIndex.invertedIndex).toString();
resolvedIndex = IndexReaderUtils.getIndex(currentIndex.invertedIndex).toString();
} else {
// Not a known prebuilt label; resolve as prebuilt (if any) or local path.
resolvedIndex = IndexReaderUtils.getIndex(index).toString();
}
LOG.info("Generating reranker requests with raw documents from index: " + index);

LOG.info("Generating reranker requests with raw documents from index: " + resolvedIndex);
try {
return IndexReaderUtils.getReader(index);
return IndexReaderUtils.getReader(resolvedIndex);
} catch (IOException e) {
throw new IllegalArgumentException(String.format("\"%s\" does not appear to have a valid inverted index.", index));
throw new IllegalArgumentException(String.format("\"%s\" does not appear to have a valid inverted index.", resolvedIndex));
}
}

Expand Down
29 changes: 29 additions & 0 deletions src/test/java/io/anserini/index/IndexReaderUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -659,4 +659,33 @@ public void testFormatSize() {
assertEquals("1.0 MB", IndexReaderUtils.formatSize(1048576));
assertEquals("1.0 GB", IndexReaderUtils.formatSize(1073741824));
}

private static final String PREBUILT_LABEL = "msmarco-v1-passage";

@Test
public void testAmbiguousPrebuiltLabelAndLocalPathThrows() throws IOException {
Path cwd = java.nio.file.Paths.get("");
Path localDir = cwd.resolve(PREBUILT_LABEL);

// Ensure clean state, create the conflicting local directory, then clean it up in finally.
if (java.nio.file.Files.exists(localDir)) {
// If it already exists for some reason, fail fast to avoid breaking other tests.
fail("Test setup error: unexpected existing directory " + localDir);
}

try {
java.nio.file.Files.createDirectory(localDir);
try {
IndexReaderUtils.getIndex(PREBUILT_LABEL);
fail("Expected IllegalArgumentException due to ambiguous index reference");
} catch (IllegalArgumentException e) {
assertTrue(e.getMessage().contains("Ambiguous index reference"));
assertTrue(e.getMessage().contains(PREBUILT_LABEL));
}
} finally {
if (java.nio.file.Files.exists(localDir)) {
java.nio.file.Files.delete(localDir);
}
}
}
}
35 changes: 35 additions & 0 deletions src/test/java/io/anserini/rerank/GenerateRerankerRequestsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -163,4 +163,39 @@ public void testGenerate() throws Exception {
assertTrue(new File("test_reranker_requests.jsonl").delete());
restoreStderr();
}

@Test
public void testAmbiguousIndexLabelAndLocalPath() throws Exception {
// Create a local directory that matches a known prebuilt label to force ambiguity.
final String prebuiltLabel = "msmarco-v1-passage";
File localDir = new File(prebuiltLabel);
if (localDir.exists()) {
// If it already exists, fail to avoid interfering with other tests.
throw new IllegalStateException("Unexpected pre-existing directory: " + localDir.getAbsolutePath());
}

try {
if (!localDir.mkdir()) {
throw new IllegalStateException("Failed to create test directory: " + localDir.getAbsolutePath());
}

GenerateRerankerRequests.Args args = new GenerateRerankerRequests.Args();
args.index = prebuiltLabel; // Ambiguous: label exists and local dir exists
args.run = "src/test/resources/sample_runs/run4";
args.topics = "cacm";
args.output = "test_reranker_requests.jsonl";

try (GenerateRerankerRequests<?> ignored = new GenerateRerankerRequests<>(args)) {
assertTrue("Expected IllegalArgumentException due to ambiguous index reference", false);
} catch (IllegalArgumentException e) {
assertTrue(e.getMessage().contains("Ambiguous index reference"));
}
} finally {
if (localDir.exists()) {
assertTrue(localDir.delete());
}
// Ensure we don't leave the output file around if it was accidentally created
new File("test_reranker_requests.jsonl").delete();
}
}
}