Search in sources :

Example 1 with FilePart

use of com.hedera.services.state.merkle.internals.FilePart in project hedera-services by hashgraph.

the class MerkleSpecialFilesTest method hashSummarizesAsExpected.

@Test
void hashSummarizesAsExpected() throws IOException {
    subject.append(fid, Arrays.copyOfRange(stuff, 0, stuff.length / 2));
    subject.append(secondFid, Arrays.copyOfRange(stuff, stuff.length / 2, stuff.length));
    final var fcq = new FCQueue<FilePart>();
    fcq.add(new FilePart(subject.get(fid)));
    final var secondFcq = new FCQueue<FilePart>();
    secondFcq.add(new FilePart(subject.get(secondFid)));
    final var baos = new ByteArrayOutputStream();
    baos.write(Longs.toByteArray(fid.getFileNum()));
    baos.write(fcq.getHash().getValue());
    baos.write(Longs.toByteArray(secondFid.getFileNum()));
    baos.write(secondFcq.getHash().getValue());
    final var expected = CommonUtils.noThrowSha384HashOf(baos.toByteArray());
    assertArrayEquals(expected, subject.getHash().getValue());
}
Also used : FCQueue(com.swirlds.fcqueue.FCQueue) ByteArrayOutputStream(java.io.ByteArrayOutputStream) FilePart(com.hedera.services.state.merkle.internals.FilePart) Test(org.junit.jupiter.api.Test)

Example 2 with FilePart

use of com.hedera.services.state.merkle.internals.FilePart in project hedera-services by hashgraph.

the class MerkleSpecialFilesTest method liveFireSerdeWorksWithNonEmpty.

@Test
void liveFireSerdeWorksWithNonEmpty() throws IOException, ConstructableRegistryException {
    final var baos = new ByteArrayOutputStream();
    final var dos = new SerializableDataOutputStream(baos);
    ConstructableRegistry.registerConstructable(new ClassConstructorPair(MerkleSpecialFiles.class, MerkleSpecialFiles::new));
    ConstructableRegistry.registerConstructable(new ClassConstructorPair(FCQueue.class, FCQueue::new));
    ConstructableRegistry.registerConstructable(new ClassConstructorPair(FilePart.class, FilePart::new));
    subject.update(fid, Arrays.copyOfRange(stuff, 0, stuff.length / 2));
    subject.update(secondFid, Arrays.copyOfRange(stuff, stuff.length / 2, stuff.length));
    subject.serialize(dos);
    dos.flush();
    final var bytes = baos.toByteArray();
    final var bais = new ByteArrayInputStream(bytes);
    final var din = new SerializableDataInputStream(bais);
    final var newSubject = new MerkleSpecialFiles();
    newSubject.deserialize(din, MerkleSpecialFiles.CURRENT_VERSION);
    assertArrayEquals(subject.get(fid), newSubject.get(fid), "Deserialized contents should match for first file");
    assertArrayEquals(subject.get(secondFid), newSubject.get(secondFid), "Deserialized contents should match for second file");
}
Also used : FCQueue(com.swirlds.fcqueue.FCQueue) ByteArrayInputStream(java.io.ByteArrayInputStream) SerializableDataOutputStream(com.swirlds.common.io.SerializableDataOutputStream) SerializableDataInputStream(com.swirlds.common.io.SerializableDataInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) FilePart(com.hedera.services.state.merkle.internals.FilePart) ClassConstructorPair(com.swirlds.common.constructable.ClassConstructorPair) Test(org.junit.jupiter.api.Test)

Example 3 with FilePart

use of com.hedera.services.state.merkle.internals.FilePart in project hedera-services by hashgraph.

the class MerkleSpecialFiles method newFcqWith.

private FCQueue<FilePart> newFcqWith(byte[] initialContents) {
    final var fileByParts = new FCQueue<FilePart>();
    fileByParts.add(new FilePart(initialContents));
    return fileByParts;
}
Also used : FCQueue(com.swirlds.fcqueue.FCQueue) FilePart(com.hedera.services.state.merkle.internals.FilePart)

Example 4 with FilePart

use of com.hedera.services.state.merkle.internals.FilePart in project hedera-services by hashgraph.

the class MerkleSpecialFiles method append.

/**
 * Appends the given bytes to the contents of the requested file. (Or, if the file
 * does not yet exist, creates it with the given contents.)
 *
 * @param fid
 * 		the id of the file to append to
 * @param extraContents
 * 		the contents to append
 */
public synchronized void append(FileID fid, byte[] extraContents) {
    throwIfImmutable();
    final var fileByParts = fileContents.get(fid);
    if (fileByParts == null) {
        update(fid, extraContents);
        return;
    }
    fileByParts.add(new FilePart(extraContents));
    hashCache.remove(fid);
}
Also used : FilePart(com.hedera.services.state.merkle.internals.FilePart)

Example 5 with FilePart

use of com.hedera.services.state.merkle.internals.FilePart in project hedera-services by hashgraph.

the class MerkleSpecialFiles method get.

/**
 * Gets the contents of the given file.
 *
 * @param fid
 * 		the id of the file to get
 * @return the file's contents
 */
public synchronized byte[] get(FileID fid) {
    final var fileByParts = fileContents.get(fid);
    if (fileByParts == null) {
        return NO_CONTENTS;
    }
    final var baos = baosSupplier.get();
    for (final FilePart part : fileByParts) {
        try {
            baos.write(part.getData());
        } catch (IOException e) {
            log.error("Special file concatenation failed for {}", readableId(fid), e);
            throw new UncheckedIOException(e);
        }
    }
    return baos.toByteArray();
}
Also used : UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) FilePart(com.hedera.services.state.merkle.internals.FilePart)

Aggregations

FilePart (com.hedera.services.state.merkle.internals.FilePart)5 FCQueue (com.swirlds.fcqueue.FCQueue)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 Test (org.junit.jupiter.api.Test)2 ClassConstructorPair (com.swirlds.common.constructable.ClassConstructorPair)1 SerializableDataInputStream (com.swirlds.common.io.SerializableDataInputStream)1 SerializableDataOutputStream (com.swirlds.common.io.SerializableDataOutputStream)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 UncheckedIOException (java.io.UncheckedIOException)1