From 1b56bba95dd8a0c2fc316ff69951234228e2cb20 Mon Sep 17 00:00:00 2001 From: j33green Date: Mon, 18 Aug 2025 18:10:26 -0400 Subject: [PATCH 1/4] Test fixes with errors --- .../rerank/GenerateRerankerRequests.java | 27 +++++++++++++++---- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/src/main/java/io/anserini/rerank/GenerateRerankerRequests.java b/src/main/java/io/anserini/rerank/GenerateRerankerRequests.java index db41dff5c0..4901e900a0 100644 --- a/src/main/java/io/anserini/rerank/GenerateRerankerRequests.java +++ b/src/main/java/io/anserini/rerank/GenerateRerankerRequests.java @@ -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)); } } From 9050c226e254aba832c1e631c1e5281ae276f297 Mon Sep 17 00:00:00 2001 From: Suraj Subrahmanyan Date: Mon, 18 Aug 2025 23:18:47 -0400 Subject: [PATCH 2/4] Add ambiguity handling --- .../io/anserini/index/IndexReaderUtils.java | 34 +++++++++++++++---- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/src/main/java/io/anserini/index/IndexReaderUtils.java b/src/main/java/io/anserini/index/IndexReaderUtils.java index 19adea2fb2..cffc210447 100755 --- a/src/main/java/io/anserini/index/IndexReaderUtils.java +++ b/src/main/java/io/anserini/index/IndexReaderUtils.java @@ -844,24 +844,44 @@ public static Map 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 From 9b9f532685c8d01c29fb825dc618857d7ea784cb Mon Sep 17 00:00:00 2001 From: j33green Date: Tue, 19 Aug 2025 13:09:13 -0400 Subject: [PATCH 3/4] New test for GenerateRerankerRequests pt.1 --- .../rerank/GenerateRerankerRequestsTest.java | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src/test/java/io/anserini/rerank/GenerateRerankerRequestsTest.java b/src/test/java/io/anserini/rerank/GenerateRerankerRequestsTest.java index 16c73139dd..1b861bfd81 100644 --- a/src/test/java/io/anserini/rerank/GenerateRerankerRequestsTest.java +++ b/src/test/java/io/anserini/rerank/GenerateRerankerRequestsTest.java @@ -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(); + } + } } From acdf453a89438f883d596ac8e43305b3dc9a8c3a Mon Sep 17 00:00:00 2001 From: Suraj Subrahmanyan Date: Tue, 19 Aug 2025 13:13:57 -0400 Subject: [PATCH 4/4] Add unittest to IndexReaderUtilsTest.java --- .../anserini/index/IndexReaderUtilsTest.java | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/test/java/io/anserini/index/IndexReaderUtilsTest.java b/src/test/java/io/anserini/index/IndexReaderUtilsTest.java index bc75637201..d467567f4f 100755 --- a/src/test/java/io/anserini/index/IndexReaderUtilsTest.java +++ b/src/test/java/io/anserini/index/IndexReaderUtilsTest.java @@ -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); + } + } + } } \ No newline at end of file