Skip to content

Commit

Permalink
Merge pull request #63 from jamesmudd/b-tree-cleanup
Browse files Browse the repository at this point in the history
B tree cleanup
  • Loading branch information
jamesmudd authored Mar 11, 2019
2 parents efaed02 + e89f8ec commit 4af166c
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 43 deletions.
9 changes: 4 additions & 5 deletions jhdf/src/main/java/io/jhdf/AbstractNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@
import io.jhdf.api.Group;
import io.jhdf.api.Node;
import io.jhdf.api.NodeType;
import io.jhdf.btree.AttributeNameForIndexedAttributesRecord;
import io.jhdf.btree.BTreeRecord;
import io.jhdf.btree.BTreeV2;
import io.jhdf.btree.record.AttributeNameForIndexedAttributesRecord;
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
12 changes: 6 additions & 6 deletions jhdf/src/main/java/io/jhdf/GroupImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@
import io.jhdf.api.Group;
import io.jhdf.api.Node;
import io.jhdf.api.NodeType;
import io.jhdf.btree.BTreeRecord;
import io.jhdf.btree.BTreeV1;
import io.jhdf.btree.BTreeV2;
import io.jhdf.btree.LinkNameForIndexedGroupRecord;
import io.jhdf.btree.record.LinkNameForIndexedGroupRecord;
import io.jhdf.dataset.DatasetLoader;
import io.jhdf.exceptions.HdfException;
import io.jhdf.exceptions.HdfInvalidPathException;
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
20 changes: 0 additions & 20 deletions jhdf/src/main/java/io/jhdf/btree/BTreeRecord.java

This file was deleted.

15 changes: 6 additions & 9 deletions jhdf/src/main/java/io/jhdf/btree/BTreeV2.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@

import io.jhdf.HdfFileChannel;
import io.jhdf.Utils;
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 @@ -25,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 @@ -81,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
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.jhdf.btree;
package io.jhdf.btree.record;

import java.nio.ByteBuffer;
import java.util.BitSet;
Expand Down
21 changes: 21 additions & 0 deletions jhdf/src/main/java/io/jhdf/btree/record/BTreeRecord.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package io.jhdf.btree.record;

import java.nio.ByteBuffer;

import io.jhdf.exceptions.HdfException;

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) {
switch (type) {
case 5:
return (T) new LinkNameForIndexedGroupRecord(buffer);
case 8:
return (T) new AttributeNameForIndexedAttributesRecord(buffer);
default:
throw new HdfException("Unknown b-tree record type. Type = " + type);
}
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.jhdf.btree;
package io.jhdf.btree.record;

import java.nio.ByteBuffer;

Expand Down
6 changes: 6 additions & 0 deletions jhdf/src/main/java/io/jhdf/btree/record/package-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* B-Tree record classes
*
* @author James Mudd
*/
package io.jhdf.btree.record;
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.jhdf.btree;
package io.jhdf.btree.record;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
Expand All @@ -9,6 +9,7 @@

import org.junit.jupiter.api.Test;

import io.jhdf.btree.record.LinkNameForIndexedGroupRecord;
import io.jhdf.exceptions.HdfException;

class LinkNameForIndexedGroupRecordTest {
Expand Down

0 comments on commit 4af166c

Please sign in to comment.