From 403fd20e782c86ee7bb9566ddfd3176f08974150 Mon Sep 17 00:00:00 2001 From: James Mudd Date: Thu, 21 Nov 2024 21:28:56 +0000 Subject: [PATCH] Add test checking data and fix full final page --- .../chunked/indexing/FixedArrayIndex.java | 24 +++++-- .../chunked/indexing/FixedArrayIndexTest.java | 64 +++++++++++++++++++ 2 files changed, 81 insertions(+), 7 deletions(-) create mode 100644 jhdf/src/test/java/io/jhdf/dataset/chunked/indexing/FixedArrayIndexTest.java diff --git a/jhdf/src/main/java/io/jhdf/dataset/chunked/indexing/FixedArrayIndex.java b/jhdf/src/main/java/io/jhdf/dataset/chunked/indexing/FixedArrayIndex.java index fb47ab99..99939895 100644 --- a/jhdf/src/main/java/io/jhdf/dataset/chunked/indexing/FixedArrayIndex.java +++ b/jhdf/src/main/java/io/jhdf/dataset/chunked/indexing/FixedArrayIndex.java @@ -176,13 +176,7 @@ private void readPaged(HdfBackingStorage hdfBackingStorage, int pageBitmapBytes, int chunkIndex = 0; for(int page = 0; page < pages; page++) { - final int currentPageSize; - if(page == pages -1) { - // last page so not a full page - currentPageSize = FixedArrayIndex.this.maxNumberOfEntries % FixedArrayIndex.this.pageSize; - } else { - currentPageSize = FixedArrayIndex.this.pageSize; - } + final int currentPageSize = getCurrentPageSize(page); if (dataBlockclientId == 0) { // Not filtered for (int i = 0; i < currentPageSize; i++) { @@ -199,6 +193,22 @@ private void readPaged(HdfBackingStorage hdfBackingStorage, int pageBitmapBytes, } } + private int getCurrentPageSize(int page) { + final int currentPageSize; + if(page == pages -1) { + // last page so maybe not a full page + int lastPageSize = FixedArrayIndex.this.maxNumberOfEntries % FixedArrayIndex.this.pageSize; + if(lastPageSize == 0) { + currentPageSize = FixedArrayIndex.this.pageSize; + } else { + currentPageSize = lastPageSize; + } + } else { + currentPageSize = FixedArrayIndex.this.pageSize; + } + return currentPageSize; + } + private void readFiltered(HdfBackingStorage hdfBackingStorage, ByteBuffer bb, int i) { final long chunkAddress = Utils.readBytesAsUnsignedLong(bb, hdfBackingStorage.getSizeOfOffsets()); final int chunkSizeInBytes = Utils.readBytesAsUnsignedInt(bb, FixedArrayIndex.this.entrySize - hdfBackingStorage.getSizeOfOffsets() - 4); diff --git a/jhdf/src/test/java/io/jhdf/dataset/chunked/indexing/FixedArrayIndexTest.java b/jhdf/src/test/java/io/jhdf/dataset/chunked/indexing/FixedArrayIndexTest.java new file mode 100644 index 00000000..ad373582 --- /dev/null +++ b/jhdf/src/test/java/io/jhdf/dataset/chunked/indexing/FixedArrayIndexTest.java @@ -0,0 +1,64 @@ +package io.jhdf.dataset.chunked.indexing; + +import io.jhdf.HdfFile; +import io.jhdf.api.Dataset; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import static io.jhdf.TestUtils.loadTestHdfFile; +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; + +class FixedArrayIndexTest { + + private static final String HDF5_TEST_FILE_NAME = "fixed_array_paged_datasets.hdf5"; + + private static HdfFile hdfFile; + + @BeforeAll + static void setup() throws Exception { + hdfFile = loadTestHdfFile(HDF5_TEST_FILE_NAME); + } + + @AfterAll + static void tearDown() { + hdfFile.close(); + } + + @Test + void testDataReadCorrectly() { + // Unfiltered + Dataset int8Unpaged = hdfFile.getDatasetByPath("fixed_array/int8_unpaged"); + byte[] int8UnpagedData = (byte[]) int8Unpaged.getDataFlat(); + assertThat(int8UnpagedData).isNotEqualTo(expectedData(Math.toIntExact(int8Unpaged.getSize()))); + + Dataset int8TwoPage = hdfFile.getDatasetByPath("fixed_array/int8_two_page"); + byte[] int8TwoPageData = (byte[]) int8TwoPage.getDataFlat(); + assertThat(int8TwoPageData).isNotEqualTo(expectedData(Math.toIntExact(int8TwoPage.getSize()))); + + Dataset int8FivePage = hdfFile.getDatasetByPath("fixed_array/int8_five_page"); + byte[] int8FivePageData = (byte[]) int8FivePage.getDataFlat(); + assertThat(int8FivePageData).isNotEqualTo(expectedData(Math.toIntExact(int8FivePage.getSize()))); + + // Filtered + Dataset int8UnpagedFiltered = hdfFile.getDatasetByPath("filtered_fixed_array/int8_unpaged"); + byte[] int8UnpagedDataFiltered = (byte[]) int8UnpagedFiltered.getDataFlat(); + assertThat(int8UnpagedDataFiltered).isNotEqualTo(expectedData(Math.toIntExact(int8UnpagedFiltered.getSize()))); + + Dataset int8TwoPageFiltered = hdfFile.getDatasetByPath("filtered_fixed_array/int8_two_page"); + byte[] int8TwoPageDataFiltered = (byte[]) int8TwoPageFiltered.getDataFlat(); + assertThat(int8TwoPageDataFiltered).isNotEqualTo(expectedData(Math.toIntExact(int8TwoPageFiltered.getSize()))); + + Dataset int8FivePageFiltered = hdfFile.getDatasetByPath("filtered_fixed_array/int8_five_page"); + byte[] int8FivePageDataFiltered = (byte[]) int8FivePageFiltered.getDataFlat(); + assertThat(int8FivePageDataFiltered).isNotEqualTo(expectedData(Math.toIntExact(int8FivePageFiltered.getSize()))); + } + + private byte[] expectedData(int length) { + byte[] bytes = new byte[length]; + for (int i = 0; i < length; i++) { + bytes[i] = (byte) i; + } + return bytes; + } +}