From bf62ecff0bb883340bc4d28cd287ee18077a2d35 Mon Sep 17 00:00:00 2001 From: James Seibel Date: Sat, 7 Oct 2023 17:00:01 -0500 Subject: [PATCH] Improve metaDataFile saving logic --- .../file/fullDatafile/FullDataMetaFile.java | 9 ++------ .../AbstractMetaDataContainerFile.java | 21 ++++++++++++------- .../file/renderfile/RenderDataMetaFile.java | 7 +------ 3 files changed, 16 insertions(+), 21 deletions(-) diff --git a/core/src/main/java/com/seibel/distanthorizons/core/file/fullDatafile/FullDataMetaFile.java b/core/src/main/java/com/seibel/distanthorizons/core/file/fullDatafile/FullDataMetaFile.java index f3e1bfc59..8b9c54de6 100644 --- a/core/src/main/java/com/seibel/distanthorizons/core/file/fullDatafile/FullDataMetaFile.java +++ b/core/src/main/java/com/seibel/distanthorizons/core/file/fullDatafile/FullDataMetaFile.java @@ -691,13 +691,8 @@ public class FullDataMetaFile extends AbstractMetaDataContainerFile implements I this.baseMetaData.binaryDataFormatVersion = fullDataSource.getBinaryDataFormatVersion(); - // save the data to the database - - ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); - super.writeData((bufferedOutputStream) -> fullDataSource.writeToStream((bufferedOutputStream), this.level), byteArrayOutputStream); - - MetaDataDto dto = new MetaDataDto(this.baseMetaData, byteArrayOutputStream.toByteArray()); - this.fullDataSourceProvider.getRepo().save(dto); + // save the data to the database // + super.writeToDatabase((bufferedOutputStream) -> fullDataSource.writeToStream((bufferedOutputStream), this.level), this.fullDataSourceProvider.getRepo()); this.doesDtoExist = true; } catch (ClosedByInterruptException e) // thrown by buffers that are interrupted diff --git a/core/src/main/java/com/seibel/distanthorizons/core/file/metaData/AbstractMetaDataContainerFile.java b/core/src/main/java/com/seibel/distanthorizons/core/file/metaData/AbstractMetaDataContainerFile.java index db3b7455e..ddd724f4f 100644 --- a/core/src/main/java/com/seibel/distanthorizons/core/file/metaData/AbstractMetaDataContainerFile.java +++ b/core/src/main/java/com/seibel/distanthorizons/core/file/metaData/AbstractMetaDataContainerFile.java @@ -20,15 +20,15 @@ package com.seibel.distanthorizons.core.file.metaData; import java.io.*; -import java.nio.ByteBuffer; import java.nio.channels.ClosedChannelException; import java.nio.file.*; import java.util.zip.Adler32; import java.util.zip.CheckedOutputStream; -import com.seibel.distanthorizons.api.enums.worldGeneration.EDhApiWorldGenerationStep; import com.seibel.distanthorizons.core.logging.DhLoggerBuilder; import com.seibel.distanthorizons.core.pos.DhSectionPos; +import com.seibel.distanthorizons.core.sql.AbstractDhRepo; +import com.seibel.distanthorizons.core.sql.MetaDataDto; import com.seibel.distanthorizons.core.util.LodUtil; import com.seibel.distanthorizons.core.util.objects.dataStreams.DhDataOutputStream; import org.apache.logging.log4j.Logger; @@ -104,25 +104,30 @@ public abstract class AbstractMetaDataContainerFile // file writing // //==============// - public void writeData(IMetaDataWriterFunc dataWriterFunc, OutputStream outputStream) throws IOException + public void writeToDatabase(IMetaDataWriterFunc dataWriterFunc, AbstractDhRepo repo) throws IOException { LodUtil.assertTrue(this.baseMetaData != null); try { + ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); + // the order of these streams is important, otherwise the checksum won't be calculated - CheckedOutputStream checkedOut = new CheckedOutputStream(outputStream, new Adler32()); + CheckedOutputStream checkedOut = new CheckedOutputStream(byteArrayOutputStream, new Adler32()); // normally a DhStream should be the topmost stream to prevent closing the stream accidentally, but since this stream will be closed immediately after writing anyway, it won't be an issue DhDataOutputStream compressedOut = new DhDataOutputStream(checkedOut); - // write the contained data - dataWriterFunc.writeBufferToFile(compressedOut); + dataWriterFunc.writeBinaryDataToStream(compressedOut); compressedOut.flush(); this.baseMetaData.checksum = (int) checkedOut.getChecksum().getValue(); - outputStream.close(); + byteArrayOutputStream.close(); + + + MetaDataDto dto = new MetaDataDto(this.baseMetaData, byteArrayOutputStream.toByteArray()); + repo.save(dto); } catch (ClosedChannelException e) // includes ClosedByInterruptException { @@ -138,6 +143,6 @@ public abstract class AbstractMetaDataContainerFile //================// @FunctionalInterface - public interface IMetaDataWriterFunc { void writeBufferToFile(T t) throws IOException; } + public interface IMetaDataWriterFunc { void writeBinaryDataToStream(T t) throws IOException; } } diff --git a/core/src/main/java/com/seibel/distanthorizons/core/file/renderfile/RenderDataMetaFile.java b/core/src/main/java/com/seibel/distanthorizons/core/file/renderfile/RenderDataMetaFile.java index a3e990cde..abb2d9f36 100644 --- a/core/src/main/java/com/seibel/distanthorizons/core/file/renderfile/RenderDataMetaFile.java +++ b/core/src/main/java/com/seibel/distanthorizons/core/file/renderfile/RenderDataMetaFile.java @@ -396,13 +396,8 @@ public class RenderDataMetaFile extends AbstractMetaDataContainerFile implements //LOGGER.info("Saving updated render file v[{}] at sect {}", this.metaData.dataVersion.get(), this.pos); try { - ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); - - super.writeData((dhDataOutputStream) -> renderSource.writeData(dhDataOutputStream), byteArrayOutputStream); + super.writeToDatabase((dhDataOutputStream) -> renderSource.writeData(dhDataOutputStream), this.renderDataSourceProvider.getRepo()); this.doesDtoExist = true; - - MetaDataDto dto = new MetaDataDto(this.baseMetaData, byteArrayOutputStream.toByteArray()); - this.renderDataSourceProvider.getRepo().save(dto); } catch (IOException e) {