Skip to content

Commit

Permalink
Make BTreeV2 generic with the record type
Browse files Browse the repository at this point in the history
This allows b-trees to be used without casts.
  • Loading branch information
jamesmudd committed Mar 11, 2019
1 parent 2ba1ce4 commit e89f8ec
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 21 deletions.
7 changes: 3 additions & 4 deletions jhdf/src/main/java/io/jhdf/AbstractNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import io.jhdf.api.NodeType;
import io.jhdf.btree.BTreeV2;
import io.jhdf.btree.record.AttributeNameForIndexedAttributesRecord;
import io.jhdf.btree.record.BTreeRecord;
import io.jhdf.exceptions.HdfException;
import io.jhdf.object.message.AttributeInfoMessage;
import io.jhdf.object.message.AttributeMessage;
Expand Down Expand Up @@ -49,11 +48,11 @@ protected Map<String, Attribute> initialize() throws ConcurrentException {
if (attributeInfoMessage.getFractalHeapAddress() != Constants.UNDEFINED_ADDRESS) {
// Create the heap and btree
FractalHeap fractalHeap = new FractalHeap(hdfFc, attributeInfoMessage.getFractalHeapAddress());
BTreeV2 btree = BTreeV2.createBTree(hdfFc, attributeInfoMessage.getAttributeNameBTreeAddress());
BTreeV2<AttributeNameForIndexedAttributesRecord> btree = new BTreeV2<>(hdfFc,
attributeInfoMessage.getAttributeNameBTreeAddress());

// Read the attribute messages from the btree+heap
for (BTreeRecord record : btree.getRecords()) {
AttributeNameForIndexedAttributesRecord attributeRecord = (AttributeNameForIndexedAttributesRecord) record;
for (AttributeNameForIndexedAttributesRecord attributeRecord : btree.getRecords()) {
ByteBuffer bb = fractalHeap.getId(attributeRecord.getHeapId());
AttributeMessage attributeMessage = new AttributeMessage(bb, hdfFc.getSuperblock(),
attributeRecord.getFlags());
Expand Down
10 changes: 5 additions & 5 deletions jhdf/src/main/java/io/jhdf/GroupImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import io.jhdf.api.NodeType;
import io.jhdf.btree.BTreeV1;
import io.jhdf.btree.BTreeV2;
import io.jhdf.btree.record.BTreeRecord;
import io.jhdf.btree.record.LinkNameForIndexedGroupRecord;
import io.jhdf.dataset.DatasetLoader;
import io.jhdf.exceptions.HdfException;
Expand Down Expand Up @@ -64,12 +63,13 @@ private Map<String, Node> createNewStyleGroup(final ObjectHeader oh) {
logger.debug("Loaded group links from object header");
} else {
// Links are not stored compactly i.e in the fractal heap
final BTreeV2 bTreeNode = BTreeV2.createBTree(hdfFc, linkInfoMessage.getbTreeNameIndexAddress());
final BTreeV2<LinkNameForIndexedGroupRecord> bTreeNode = new BTreeV2<>(hdfFc,
linkInfoMessage.getbTreeNameIndexAddress());
final FractalHeap fractalHeap = new FractalHeap(hdfFc, linkInfoMessage.getFractalHeapAddress());

links = new ArrayList<>(); // TODO would be good to get the size here from the b-tree
for (BTreeRecord record : bTreeNode.getRecords()) {
LinkNameForIndexedGroupRecord linkName = (LinkNameForIndexedGroupRecord) record;
List<LinkNameForIndexedGroupRecord> records = bTreeNode.getRecords();
links = new ArrayList<>(records.size());
for (LinkNameForIndexedGroupRecord linkName : records) {
ByteBuffer id = linkName.getId();
// Get the name data from the fractal heap
ByteBuffer bb = fractalHeap.getId(id);
Expand Down
14 changes: 5 additions & 9 deletions jhdf/src/main/java/io/jhdf/btree/BTreeV2.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import io.jhdf.btree.record.BTreeRecord;
import io.jhdf.exceptions.HdfException;

public class BTreeV2 {
public class BTreeV2<T extends BTreeRecord> {

private static final int NODE_OVERHEAD_BYTES = 10;

Expand All @@ -26,17 +26,13 @@ public class BTreeV2 {
/** Type of node. */
private final short nodeType;

private final List<BTreeRecord> records;
private final List<T> records;

public List<BTreeRecord> getRecords() {
public List<T> getRecords() {
return records;
}

public static BTreeV2 createBTree(HdfFileChannel hdfFc, long address) {
return new BTreeV2(hdfFc, address);
}

private BTreeV2(HdfFileChannel hdfFc, long address) {
public BTreeV2(HdfFileChannel hdfFc, long address) {
this.address = address;
try {
// B Tree V2 Header
Expand Down Expand Up @@ -82,7 +78,7 @@ private BTreeV2(HdfFileChannel hdfFc, long address) {
}

private void readRecords(HdfFileChannel hdfFc, long address, int nodeSize, int recordSize, int depth,
int numberOfRecords, int totalRecords, List<BTreeRecord> records) {
int numberOfRecords, int totalRecords, List<T> records) {

ByteBuffer bb = hdfFc.readBufferFromAddress(address, nodeSize);

Expand Down
7 changes: 4 additions & 3 deletions jhdf/src/main/java/io/jhdf/btree/record/BTreeRecord.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@

public abstract class BTreeRecord {

public static BTreeRecord readRecord(byte type, ByteBuffer buffer) {
@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) {
switch (type) {
case 5:
return new LinkNameForIndexedGroupRecord(buffer);
return (T) new LinkNameForIndexedGroupRecord(buffer);
case 8:
return new AttributeNameForIndexedAttributesRecord(buffer);
return (T) new AttributeNameForIndexedAttributesRecord(buffer);
default:
throw new HdfException("Unknown b-tree record type. Type = " + type);
}
Expand Down

0 comments on commit e89f8ec

Please sign in to comment.