Skip to content

Commit

Permalink
Merge pull request #138 from jamesmudd/btree-v2-records
Browse files Browse the repository at this point in the history
Btree v2 records
  • Loading branch information
jamesmudd authored Nov 27, 2019
2 parents 90e0562 + e667120 commit 07f1cb5
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 12 deletions.
2 changes: 1 addition & 1 deletion jhdf/src/main/java/io/jhdf/btree/BTreeV2.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ private void readRecords(HdfFileChannel hdfFc, long address, int depth, int numb
throw new HdfException("Unsupported B tree v2 internal node version detected. Version: " + version);
}

final byte type = bb.get();
final int type = bb.get();

for (int i = 0; i < numberOfRecords; i++) {
records.add(readRecord(type, createSubBuffer(bb, recordSize), datasetInfo));
Expand Down
38 changes: 27 additions & 11 deletions jhdf/src/main/java/io/jhdf/btree/record/BTreeRecord.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,41 @@

import io.jhdf.dataset.chunked.DatasetInfo;
import io.jhdf.exceptions.HdfException;
import io.jhdf.exceptions.UnsupportedHdfException;

import java.nio.ByteBuffer;

public abstract class BTreeRecord {

@SuppressWarnings("unchecked") // Requires that the b-tree is of the correct type for the record
public static <T extends BTreeRecord> T readRecord(byte type, ByteBuffer buffer, DatasetInfo datasetInfo) {
public static <T extends BTreeRecord> T readRecord(int type, ByteBuffer buffer, DatasetInfo datasetInfo) {
switch (type) {
case 5:
return (T) new LinkNameForIndexedGroupRecord(buffer);
case 8:
return (T) new AttributeNameForIndexedAttributesRecord(buffer);
case 10:
return (T) new NonFilteredDatasetChunks(buffer, datasetInfo);
case 0:
throw new HdfException("b-tree record type 0. Should only be used for testing");
case 1:
throw new UnsupportedHdfException("b-tree record type 1. Currently not supported");
case 2:
throw new UnsupportedHdfException("b-tree record type 2. Currently not supported");
case 3:
throw new UnsupportedHdfException("b-tree record type 3. Currently not supported");
case 4:
throw new UnsupportedHdfException("b-tree record type 4. Currently not supported");
case 5:
return (T) new LinkNameForIndexedGroupRecord(buffer);
case 6:
throw new UnsupportedHdfException("b-tree record type 6. Currently not supported");
case 7:
throw new UnsupportedHdfException("b-tree record type 7. Currently not supported");
case 8:
return (T) new AttributeNameForIndexedAttributesRecord(buffer);
case 9:
throw new UnsupportedHdfException("b-tree record type 9. Currently not supported");
case 10:
return (T) new NonFilteredDatasetChunks(buffer, datasetInfo);
case 11:
return (T) new FilteredDatasetChunks(buffer, datasetInfo);

default:
throw new HdfException("Unknown b-tree record type. Type = " + type);
return (T) new FilteredDatasetChunks(buffer, datasetInfo);
default:
throw new HdfException("Unknown b-tree record type. Type = " + type);
}
}

Expand Down
36 changes: 36 additions & 0 deletions jhdf/src/test/java/io/jhdf/btree/record/BTreeRecordTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* This file is part of jHDF. A pure Java library for accessing HDF5 files.
*
* http://jhdf.io
*
* Copyright 2019 James Mudd
*
* MIT License see 'LICENSE' file
*/
package io.jhdf.btree.record;

import io.jhdf.exceptions.HdfException;
import io.jhdf.exceptions.UnsupportedHdfException;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertThrows;

class BTreeRecordTest {

@Test
void testUnsupportedRecordTypesThrow() {
assertThrows(HdfException.class, () -> BTreeRecord.readRecord(0, null, null));
assertThrows(UnsupportedHdfException.class, () -> BTreeRecord.readRecord(1, null, null));
assertThrows(UnsupportedHdfException.class, () -> BTreeRecord.readRecord(2, null, null));
assertThrows(UnsupportedHdfException.class, () -> BTreeRecord.readRecord(3, null, null));
assertThrows(UnsupportedHdfException.class, () -> BTreeRecord.readRecord(4, null, null));
assertThrows(UnsupportedHdfException.class, () -> BTreeRecord.readRecord(6, null, null));
assertThrows(UnsupportedHdfException.class, () -> BTreeRecord.readRecord(7, null, null));
assertThrows(UnsupportedHdfException.class, () -> BTreeRecord.readRecord(9, null, null));
}

@Test
void testUnreconizedRecordTypeThrows() {
assertThrows(HdfException.class, () -> BTreeRecord.readRecord(63, null, null));
}
}
8 changes: 8 additions & 0 deletions jhdf/src/test/java/io/jhdf/examples/TestAllFiles.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,20 @@ private void verifyDataset(Dataset dataset, Group group) {
assertThat(dataset.getParent(), is(sameInstance(group)));
int[] dims = dataset.getDimensions();
assertThat(dims, is(notNullValue()));

// Call getAttributes twice to check lazy initialisation
assertThat(dataset.getAttributes(), is(notNullValue()));
assertThat(dataset.getAttributes(), is(notNullValue()));

assertThat(dataset.isGroup(), is(false));
assertThat(dataset.isLink(), is(false));
assertThat(dataset.getType(), is(NodeType.DATASET));
assertThat(dataset.getDataLayout(), is(notNullValue()));

// Call getData twice to check cases of lazy initialisation are working correctly
dataset.getData();
final Object data = dataset.getData();

if (dataset.isEmpty()) {
assertThat(data, is(nullValue()));
// Empty so should have 0 size
Expand Down

0 comments on commit 07f1cb5

Please sign in to comment.