Add varint encoding for full data

Closes Merge !93
Thanks Ryan Hitchman!
This commit is contained in:
James Seibel
2025-11-12 07:21:54 -06:00
parent 6eb24ecde1
commit b9746381eb
4 changed files with 416 additions and 10 deletions
@@ -87,16 +87,23 @@ public class DhFullDataSourceRepoTests
Random seededRandom = new Random(3);
for (int i = 0; i < FullDataSourceV2.WIDTH * FullDataSourceV2.WIDTH; i++)
for (int arrayIndex = 0; arrayIndex < FullDataSourceV2.WIDTH * FullDataSourceV2.WIDTH; arrayIndex++)
{
fullDataArray[i] = new LongArrayList(1);
fullDataArray[arrayIndex] = new LongArrayList(1);
// random column heights so we can differentiate
// columns from each other
int columnCount = Math.abs(seededRandom.nextInt() % 31) + 1;
for (int j = 0; j < columnCount; j++)
for (int colIndex = 0; colIndex < columnCount; colIndex++)
{
fullDataArray[i].add(FullDataPointUtil.encode(j, 1, j, LodUtil.MAX_MC_LIGHT, LodUtil.MAX_MC_LIGHT));
long datapoint = FullDataPointUtil.encode(
colIndex, // id
1, // height
colIndex, // relative min Y
(byte)(colIndex % LodUtil.MAX_MC_LIGHT), // block light
(byte)((colIndex + 2) % LodUtil.MAX_MC_LIGHT) // sky light
);
fullDataArray[arrayIndex].add(datapoint);
}
}
@@ -113,6 +120,18 @@ public class DhFullDataSourceRepoTests
repo.save(originalDto);
// also create format-1 encoded version to ensure backwards compatibility
long posV1 = DhSectionPos.encode((byte) 6, 2, 3);
FullDataSourceV2 dataSourceFormatV1 = FullDataSourceV2.createWithData(posV1, dataMapping, fullDataArray, columnGenStep, columnWorldCompressionMode);
FullDataSourceV2DTO dtoFormatV1 = FullDataSourceV2DTO.CreateFromDataSource(dataSourceFormatV1, EDhApiDataCompressionMode.LZMA2);
FullDataSourceV2DTO.writeDataSourceDataArrayToBlobV1(
dataSourceFormatV1.dataPoints,
dtoFormatV1.compressedDataByteArray,
EDhApiDataCompressionMode.LZMA2);
dtoFormatV1.dataFormatVersion = FullDataSourceV2DTO.DATA_FORMAT.V1_NO_ADJACENT_DATA;
repo.save(dtoFormatV1);
//=======================//
// confirm DTO data is //
@@ -152,6 +171,21 @@ public class DhFullDataSourceRepoTests
}
}
// check that we have proper backwards compatability to V1
try (FullDataSourceV2 savedDataSource = repo.getByKey(posV1).createUnitTestDataSource())
{
Assert.assertNotNull("Failed to create DataSource", savedDataSource);
assertArraysAreEqual(originalDataSource.columnGenerationSteps, savedDataSource.columnGenerationSteps);
assertArraysAreEqual(originalDataSource.columnWorldCompressionMode,
savedDataSource.columnWorldCompressionMode);
Assert.assertTrue(originalDataSource.dataPoints.length == savedDataSource.dataPoints.length);
for (int i = 0; i < FullDataSourceV2.WIDTH * FullDataSourceV2.WIDTH; i++)
{
assertArraysAreEqual(originalDataSource.dataPoints[i], savedDataSource.dataPoints[i]);
}
}
//==============//
+94
View File
@@ -0,0 +1,94 @@
/*
* This file is part of the Distant Horizons mod
* licensed under the GNU LGPL v3 License.
*
* Copyright (C) 2020 James Seibel
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package tests;
import com.seibel.distanthorizons.api.enums.config.EDhApiDataCompressionMode;
import com.seibel.distanthorizons.core.sql.dto.FullDataSourceV2DTO;
import com.seibel.distanthorizons.core.sql.dto.util.VarintUtil;
import com.seibel.distanthorizons.core.util.objects.dataStreams.DhDataInputStream;
import com.seibel.distanthorizons.core.util.objects.dataStreams.DhDataOutputStream;
import org.junit.Assert;
import org.junit.Test;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
public class VarintTest
{
@Test
public void Test()
{
Assert.assertEquals(0x80, 128);
// zig zag encoding is needed for varint handling, so test it first
for (int i = -256; i < 256; i++)
{
//testZigZagEncoding(i);
}
for (int i = -256; i < 256; i++)
{
//testSingleVarint(i);
}
}
private static void testZigZagEncoding(int value)
{
int encodedValue = VarintUtil.zigzagEncode(value);
int decodedValue = VarintUtil.zigzagDecode(encodedValue);
Assert.assertEquals(value, decodedValue);
}
private static void testSingleVarint(int value)
{
// write to stream
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
try (DhDataOutputStream outputStream = new DhDataOutputStream(byteArrayOutputStream, EDhApiDataCompressionMode.UNCOMPRESSED))
{
int encodedValue = VarintUtil.zigzagEncode(value);
VarintUtil.writeVarint(outputStream, encodedValue); // varint requires zig-zag encoding to function
}
catch (IOException e)
{
e.printStackTrace();
Assert.fail("Fail writing varint ["+value+"], error: ["+e.getMessage()+"]");
}
// read stream
byte[] byteArray = byteArrayOutputStream.toByteArray();
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArray);
try (DhDataInputStream inputStream = new DhDataInputStream(byteArrayInputStream, EDhApiDataCompressionMode.UNCOMPRESSED))
{
int encodedValue = VarintUtil.readVarint(inputStream);
int decodedValue = VarintUtil.zigzagDecode(encodedValue);
Assert.assertEquals(value, decodedValue);
}
catch (IOException e)
{
e.printStackTrace();
Assert.fail("Fail reading varint ["+value+"], error: ["+e.getMessage()+"]");
}
}
}