Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.apache.lucene.store.NIOFSDirectory;
import org.apache.lucene.tests.util.LuceneTestCase;
import org.elasticsearch.test.ESTestCase;
import org.junit.BeforeClass;

import java.io.IOException;
import java.lang.foreign.ValueLayout;
Expand All @@ -32,6 +33,11 @@
@LuceneTestCase.SuppressFileSystems("*") // we do our own mocking
public class MemorySegmentUtilsTests extends ESTestCase {

@BeforeClass
public static void beforeClass() {
assumeTrue("needs final FFI API", Runtime.version().feature() >= 22);
}

private FSDirectory newRandomDirectory() throws IOException {
return randomBoolean() ? new MMapDirectory(createTempDir()) : new NIOFSDirectory(createTempDir());
}
Expand All @@ -41,18 +47,18 @@ public void testCopyInputToTempFile() throws IOException {
var data = randomByteArrayOfLength(dataSize);

try (FSDirectory dir = newRandomDirectory()) {
try (IndexOutput out = dir.createOutput("tests.bin", IOContext.DEFAULT)) {
try (IndexOutput out = dir.createOutput("tests.bin1", IOContext.DEFAULT)) {
out.writeBytes(data, 0, dataSize);
}

try (IndexInput in = dir.openInput("tests.bin", IOContext.DEFAULT)) {
try (IndexInput in = dir.openInput("tests.bin1", IOContext.DEFAULT)) {
if (rarely()) {
in.seek(randomIntBetween(0, dataSize - 1));
}

var initialPosition = in.getFilePointer();

var tempFilePath = MemorySegmentUtils.copyInputToTempFile(in, dir, "tests.bin");
var tempFilePath = MemorySegmentUtils.copyInputToTempFile(in, dir, "tests.bin3");

assertThat(
tempFilePath.toString(),
Expand All @@ -74,22 +80,29 @@ public void testCopyInputToTempFilePacked() throws IOException {
var scratch = new byte[paddingSize];

try (FSDirectory dir = newRandomDirectory()) {
try (IndexOutput out = dir.createOutput("tests.bin", IOContext.DEFAULT)) {
try (IndexOutput out = dir.createOutput("tests.bin4", IOContext.DEFAULT)) {
for (int i = 0; i < rows; ++i) {
out.writeBytes(data, i * rowSize, rowSize);
randomBytesBetween(scratch, (byte) 0, (byte) 127);
out.writeBytes(scratch, 0, paddingSize);
}
}

try (IndexInput in = dir.openInput("tests.bin", IOContext.DEFAULT)) {
try (IndexInput in = dir.openInput("tests.bin4", IOContext.DEFAULT)) {
if (rarely()) {
in.seek(randomIntBetween(0, data.length - 1));
}

var initialPosition = in.getFilePointer();

var tempFilePath = MemorySegmentUtils.copyInputToTempFilePacked(in, dir, "tests.bin", rows, rowSize + paddingSize, rowSize);
var tempFilePath = MemorySegmentUtils.copyInputToTempFilePacked(
in,
dir,
"tests.bin6",
rows,
rowSize + paddingSize,
rowSize
);

assertThat(
tempFilePath.toString(),
Expand Down Expand Up @@ -119,14 +132,14 @@ public void testGetContiguousMemorySegmentBelowMaxChunkSize() throws IOException
var data = randomByteArrayOfLength(dataSize);

try (FSDirectory dir = new MMapDirectory(createTempDir(), maxChunkSize)) {
try (IndexOutput out = dir.createOutput("tests.bin", IOContext.DEFAULT)) {
try (IndexOutput out = dir.createOutput("tests.bin7", IOContext.DEFAULT)) {
out.writeBytes(data, 0, dataSize);
}

try (IndexInput in = dir.openInput("tests.bin", IOContext.DEFAULT)) {
try (IndexInput in = dir.openInput("tests.bin7", IOContext.DEFAULT)) {

var msai = (MemorySegmentAccessInput) in;
var holder = MemorySegmentUtils.getContiguousMemorySegment(msai, dir, "tests.bin");
var holder = MemorySegmentUtils.getContiguousMemorySegment(msai, dir, "tests.bin9");

assertThat(holder, isA(MemorySegmentUtils.DirectMemorySegmentHolder.class));
assertNotNull(holder.memorySegment());
Expand All @@ -142,19 +155,19 @@ public void testGetContiguousMemorySegmentAboveMaxChunkSize() throws IOException
var data = randomByteArrayOfLength(dataSize);

try (FSDirectory dir = new MMapDirectory(createTempDir(), maxChunkSize)) {
try (IndexOutput out = dir.createOutput("tests.bin", IOContext.DEFAULT)) {
try (IndexOutput out = dir.createOutput("tests.bin10", IOContext.DEFAULT)) {
out.writeBytes(data, 0, dataSize);
}

try (IndexInput in = dir.openInput("tests.bin", IOContext.DEFAULT)) {

try (IndexInput in = dir.openInput("tests.bin10", IOContext.DEFAULT)) {
var msai = (MemorySegmentAccessInput) in;
var holder = MemorySegmentUtils.getContiguousMemorySegment(msai, dir, "tests.bin");
try (var holder = MemorySegmentUtils.getContiguousMemorySegment(msai, dir, "tests.bin12")) {

assertThat(holder, isA(MemorySegmentUtils.FileBackedMemorySegmentHolder.class));
assertNotNull(holder.memorySegment());
assertThat(holder.memorySegment().address(), is(not(0)));
assertArrayEquals(data, holder.memorySegment().toArray(ValueLayout.JAVA_BYTE));
assertThat(holder, isA(MemorySegmentUtils.FileBackedMemorySegmentHolder.class));
assertNotNull(holder.memorySegment());
assertThat(holder.memorySegment().address(), is(not(0)));
assertArrayEquals(data, holder.memorySegment().toArray(ValueLayout.JAVA_BYTE));
}
}
}
}
Expand Down