Skip to content

Apache Lucene open-source search software

LUCENE-5969: port two remaining TestCompoundFile tests

Robert Muir5fb280a3d462
diff --git a/lucene/core/src/test/org/apache/lucene/index/TestCompoundFile.java b/lucene/core/src/test/org/apache/lucene/index/TestCompoundFile.java
deleted file mode 100644
index 7289e406759..00000000000
--- a/lucene/core/src/test/org/apache/lucene/index/TestCompoundFile.java
+++ /dev/null
@@ -1,37 +0,0 @@
-package org.apache.lucene.index;
-
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-import org.apache.lucene.document.Document;
-import org.apache.lucene.document.Field;
-import org.apache.lucene.store.Directory;
-import org.apache.lucene.store.IOContext;
-import org.apache.lucene.store.IndexInput;
-import org.apache.lucene.store.IndexOutput;
-import org.apache.lucene.store.MockDirectoryWrapper;
-import org.apache.lucene.util.LuceneTestCase;
-import org.apache.lucene.util.StringHelper;
-import org.apache.lucene.util.TestUtil;
-
-import java.io.IOException;
-import java.nio.file.Path;
-
-public class TestCompoundFile extends LuceneTestCase {
- /* nocommit: fold all these tests into BaseCompoundFormatTestCase */
-
-}
diff --git a/lucene/test-framework/src/java/org/apache/lucene/index/BaseCompoundFormatTestCase.java b/lucene/test-framework/src/java/org/apache/lucene/index/BaseCompoundFormatTestCase.java
index 8e51c5f48cf..84dcddc23b1 100644
--- a/lucene/test-framework/src/java/org/apache/lucene/index/BaseCompoundFormatTestCase.java
+++ b/lucene/test-framework/src/java/org/apache/lucene/index/BaseCompoundFormatTestCase.java
@@ -314,6 +314,88 @@ public abstract class BaseCompoundFormatTestCase extends BaseIndexFileFormatTest
dir.close();
}
+ /**
+ * This test creates a compound file based on a large number of files of
+ * various length. The file content is generated randomly. The sizes range
+ * from 0 to 1Mb. Some of the sizes are selected to test the buffering
+ * logic in the file reading code. For this the chunk variable is set to
+ * the length of the buffer used internally by the compound file logic.
+ */
+ public void testRandomFiles() throws IOException {
+ Directory dir = newDirectory();
+ // Setup the test segment
+ String segment = "_123";
+ int chunk = 1024; // internal buffer size used by the stream
+ createRandomFile(dir, segment + ".zero", 0);
+ createRandomFile(dir, segment + ".one", 1);
+ createRandomFile(dir, segment + ".ten", 10);
+ createRandomFile(dir, segment + ".hundred", 100);
+ createRandomFile(dir, segment + ".big1", chunk);
+ createRandomFile(dir, segment + ".big2", chunk - 1);
+ createRandomFile(dir, segment + ".big3", chunk + 1);
+ createRandomFile(dir, segment + ".big4", 3 * chunk);
+ createRandomFile(dir, segment + ".big5", 3 * chunk - 1);
+ createRandomFile(dir, segment + ".big6", 3 * chunk + 1);
+ createRandomFile(dir, segment + ".big7", 1000 * chunk);
+
+ String files[] = dir.listAll();
+
+ SegmentInfo si = newSegmentInfo(dir, "_123");
+ si.getCodec().compoundFormat().write(dir, si, Arrays.asList(files), MergeState.CheckAbort.NONE, IOContext.DEFAULT);
+ Directory cfs = si.getCodec().compoundFormat().getCompoundReader(dir, si, IOContext.DEFAULT);
+
+ for (int i = 0; i < files.length; i++) {
+ IndexInput check = dir.openInput(files[i], newIOContext(random()));
+ IndexInput test = cfs.openInput(files[i], newIOContext(random()));
+ assertSameStreams(files[i], check, test);
+ assertSameSeekBehavior(files[i], check, test);
+ test.close();
+ check.close();
+ }
+ cfs.close();
+ dir.close();
+ }
+
+ // Make sure we don't somehow use more than 1 descriptor
+ // when reading a CFS with many subs:
+ public void testManySubFiles() throws IOException {
+ final MockDirectoryWrapper dir = newMockFSDirectory(createTempDir("CFSManySubFiles"));
+
+ final int FILE_COUNT = atLeast(500);
+
+ for (int fileIdx = 0; fileIdx < FILE_COUNT; fileIdx++) {
+ IndexOutput out = dir.createOutput("_123." + fileIdx, newIOContext(random()));
+ out.writeByte((byte) fileIdx);
+ out.close();
+ }
+
+ assertEquals(0, dir.getFileHandleCount());
+
+ SegmentInfo si = newSegmentInfo(dir, "_123");
+ si.getCodec().compoundFormat().write(dir, si, Arrays.asList(dir.listAll()), MergeState.CheckAbort.NONE, IOContext.DEFAULT);
+ Directory cfs = si.getCodec().compoundFormat().getCompoundReader(dir, si, IOContext.DEFAULT);
+
+ final IndexInput[] ins = new IndexInput[FILE_COUNT];
+ for (int fileIdx = 0; fileIdx < FILE_COUNT; fileIdx++) {
+ ins[fileIdx] = cfs.openInput("file." + fileIdx, newIOContext(random()));
+ }
+
+ assertEquals(1, dir.getFileHandleCount());
+
+ for (int fileIdx = 0; fileIdx < FILE_COUNT; fileIdx++) {
+ assertEquals((byte) fileIdx, ins[fileIdx].readByte());
+ }
+
+ assertEquals(1, dir.getFileHandleCount());
+
+ for(int fileIdx=0;fileIdx<FILE_COUNT;fileIdx++) {
+ ins[fileIdx].close();
+ }
+ cfs.close();
+
+ dir.close();
+ }
+
/** Returns a new fake segment */
protected static SegmentInfo newSegmentInfo(Directory dir, String name) {
return new SegmentInfo(dir, Version.LATEST, name, 10000, false, Codec.getDefault(), null, StringHelper.randomId());
diff --git a/lucene/test-framework/src/java/org/apache/lucene/store/MockDirectoryWrapper.java b/lucene/test-framework/src/java/org/apache/lucene/store/MockDirectoryWrapper.java
index dddc59528aa..5d04281b5db 100644
--- a/lucene/test-framework/src/java/org/apache/lucene/store/MockDirectoryWrapper.java
+++ b/lucene/test-framework/src/java/org/apache/lucene/store/MockDirectoryWrapper.java
@@ -466,6 +466,11 @@ public class MockDirectoryWrapper extends BaseDirectoryWrapper {
}
}
}
+
+ /** returns current open file handle count */
+ public synchronized long getFileHandleCount() {
+ return openFileHandles.size();
+ }
@Override
public synchronized void deleteFile(String name) throws IOException {