Skip to content

Commit

Permalink
Progress towards attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesmudd committed May 23, 2024
1 parent a29bce5 commit 26a8b21
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 3 deletions.
11 changes: 11 additions & 0 deletions jhdf/src/main/java/io/jhdf/AttributeImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import io.jhdf.dataset.DatasetReader;
import io.jhdf.object.datatype.DataType;
import io.jhdf.object.message.AttributeMessage;
import io.jhdf.object.message.DataSpace;
import io.jhdf.storage.HdfBackingStorage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -101,4 +102,14 @@ public Class<?> getJavaType() {
public ByteBuffer getBuffer() {
return message.getDataBuffer();
}

@Override
public DataSpace getDataSpace() {
return message.getDataSpace();
}

@Override
public DataType getDataType() {
return message.getDataType();
}
}
8 changes: 8 additions & 0 deletions jhdf/src/main/java/io/jhdf/WritableGroupImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

package io.jhdf;

import io.jhdf.api.Attribute;
import io.jhdf.api.Dataset;
import io.jhdf.api.Group;
import io.jhdf.api.Node;
Expand All @@ -18,6 +19,7 @@
import io.jhdf.api.WritableNode;
import io.jhdf.api.WritiableDataset;
import io.jhdf.exceptions.UnsupportedHdfException;
import io.jhdf.object.message.AttributeMessage;
import io.jhdf.object.message.GroupInfoMessage;
import io.jhdf.object.message.LinkInfoMessage;
import io.jhdf.object.message.LinkMessage;
Expand Down Expand Up @@ -131,6 +133,12 @@ public long write(HdfFileChannel hdfFileChannel, long position) {
messages.add(linkMessage);
}

for (Map.Entry<String, Attribute> attribute : getAttributes().entrySet()) {
logger.info("Writing attribute [{}]", attribute.getKey());
AttributeMessage attributeMessage = AttributeMessage.create(attribute.getKey(), attribute.getValue());
messages.add(attributeMessage);
}

ObjectHeader.ObjectHeaderV2 objectHeader = new ObjectHeader.ObjectHeaderV2(position, messages);

ByteBuffer tempBuffer = objectHeader.toBuffer();
Expand Down
6 changes: 6 additions & 0 deletions jhdf/src/main/java/io/jhdf/api/Attribute.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
*/
package io.jhdf.api;

import io.jhdf.object.datatype.DataType;
import io.jhdf.object.message.DataSpace;

import java.nio.ByteBuffer;

/**
Expand Down Expand Up @@ -109,4 +112,7 @@ public interface Attribute {
ByteBuffer getBuffer();


DataSpace getDataSpace();

DataType getDataType();
}
13 changes: 11 additions & 2 deletions jhdf/src/main/java/io/jhdf/api/WritableAttributeImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,16 @@ public boolean isEmpty() {

@Override
public ByteBuffer getBuffer() {
// TODO
return null;
return dataType.encodeData(data);
}

@Override
public DataSpace getDataSpace() {
return dataSpace;
}

@Override
public DataType getDataType() {
return dataType;
}
}
5 changes: 5 additions & 0 deletions jhdf/src/main/java/io/jhdf/object/datatype/DataType.java
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,11 @@ public int getSize() {
*/
public abstract Object fillData(ByteBuffer buffer, int[] dimensions, HdfBackingStorage hdfBackingStorage);

// TODO could be abstract when there are more impls
public ByteBuffer encodeData(Object data){
throw new UnsupportedHdfException("Data type [" + getClass().getSimpleName() + "] does not support writing");
}

// TODO could be abstract when there are more impls
public ByteBuffer toBuffer() {
throw new UnsupportedHdfException("Data type [" + getClass().getSimpleName() + "] does not support writing");
Expand Down
20 changes: 19 additions & 1 deletion jhdf/src/main/java/io/jhdf/object/datatype/FixedPoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@
*/
package io.jhdf.object.datatype;

import io.jhdf.Utils;
import io.jhdf.exceptions.HdfTypeException;
import io.jhdf.exceptions.UnsupportedHdfException;
import io.jhdf.storage.HdfBackingStorage;
import org.apache.commons.lang3.ArrayUtils;

import java.lang.reflect.Array;
import java.math.BigInteger;
Expand All @@ -19,7 +22,9 @@
import java.nio.IntBuffer;
import java.nio.LongBuffer;
import java.nio.ShortBuffer;
import java.util.Arrays;

import static io.jhdf.Utils.flatten;
import static io.jhdf.Utils.stripLeadingIndex;

public class FixedPoint extends DataType implements OrderedDataType, WritableDataType {
Expand Down Expand Up @@ -163,7 +168,20 @@ public Object fillData(ByteBuffer buffer, int[] dimensions, HdfBackingStorage hd
return data;
}

// Signed Fixed Point
@Override
public ByteBuffer encodeData(Object data) {
Class<?> type = Utils.getArrayType(data);
// TODO multi dimensional and scalar and empty
if(type == int.class) {
Object[] flattened = flatten(data);
ByteBuffer buffer = ByteBuffer.allocate(flattened.length * 4);
buffer.asIntBuffer().put((int[]) data);
return buffer;
}
throw new UnsupportedHdfException("Cant write type");
}

// Signed Fixed Point

private static void fillData(Object data, int[] dims, ByteBuffer buffer) {
if (dims.length > 1) {
Expand Down
34 changes: 34 additions & 0 deletions jhdf/src/main/java/io/jhdf/object/message/AttributeMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
*/
package io.jhdf.object.message;

import io.jhdf.BufferBuilder;
import io.jhdf.ObjectHeader;
import io.jhdf.Utils;
import io.jhdf.api.Attribute;
import io.jhdf.exceptions.UnsupportedHdfException;
import io.jhdf.object.datatype.DataType;
import io.jhdf.storage.HdfBackingStorage;
Expand Down Expand Up @@ -164,4 +166,36 @@ public int getMessageType() {
return MESSAGE_TYPE;
}

public static AttributeMessage create(String name, Attribute attribute) {
return new AttributeMessage(name, attribute.getDataSpace(), attribute.getDataType(), attribute.getData());
}

private AttributeMessage(String name, DataSpace dataSpace, DataType dataType, Object data) {
this.name = name;
this.version = 3;
this.dataSpace = dataSpace;
this.dataType = dataType;
this.data = dataType.encodeData(data);
}

@Override
public ByteBuffer toBuffer() {

byte[] nameBytes = name.getBytes(StandardCharsets.UTF_8);
ByteBuffer dataTypeBytes = dataType.toBuffer();
ByteBuffer dataSpaceBytes = dataSpace.toBuffer();

return new BufferBuilder()
.writeByte(3) // version
.writeByte(0) // flags
.writeShort(nameBytes.length)
.writeShort(dataTypeBytes.capacity())
.writeShort(dataSpaceBytes.capacity())
.writeByte(1) // name charset
.writeBytes(nameBytes)
.writeBuffer(dataTypeBytes)
.writeBuffer(dataSpaceBytes)
.writeBuffer(data)
.build();
}
}
22 changes: 22 additions & 0 deletions jhdf/src/test/java/io/jhdf/SimpleWritingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

package io.jhdf;

import io.jhdf.api.Attribute;
import io.jhdf.api.Dataset;
import io.jhdf.api.Node;
import io.jhdf.api.WritableGroup;
Expand Down Expand Up @@ -145,4 +146,25 @@ void readSimpleFileWithDatasetsWithH5Dump() throws Exception {
H5Dump.assetXmlAndHdfFileMatch(hdf5FileXml, hdfFile);
}
}

@Test
@Order(5)
void writeAttributes() throws Exception {
WritableHdfFile writableHdfFile = HdfFile.write(tempFile);

WritableGroup intGroup = writableHdfFile.putGroup("intGroup");
int[] intData1 = new int[]{-5, -4, -3, -2, -1, 0, 1,2,3,4,5 };
intGroup.putDataset("intData1", intData1);

writableHdfFile.putAttribute("rootAttribute", new int[] {1,2,3});

// Actually flush and write everything
writableHdfFile.close();

// Now read it back
try(HdfFile hdfFile = new HdfFile(tempFile)) {
Map<String, Attribute> attributes = hdfFile.getAttributes();
assertThat(attributes).containsKeys("rootAttribute");
}
}
}

0 comments on commit 26a8b21

Please sign in to comment.