From e4406dca5ecc216d2325ae85e8b2cadfe0e4796e Mon Sep 17 00:00:00 2001 From: Jozef Gajdos Date: Fri, 14 Jun 2024 14:57:13 +0200 Subject: [PATCH] Migration to .NET 8 and enable AOT compilation. --- .github/workflows/dotnet.yml | 1 + .github/workflows/publish-nuget.yml | 1 + src/Playground/Playground.csproj | 2 +- .../ZoneTree.UnitTests.csproj | 2 +- src/ZoneTree/Core/ZoneTreeMetaWAL.cs | 43 ++++++++++++++++--- src/ZoneTree/ZoneTree.csproj | 2 +- 6 files changed, 41 insertions(+), 10 deletions(-) diff --git a/.github/workflows/dotnet.yml b/.github/workflows/dotnet.yml index 8865d38..9051748 100644 --- a/.github/workflows/dotnet.yml +++ b/.github/workflows/dotnet.yml @@ -20,6 +20,7 @@ jobs: uses: actions/setup-dotnet@v3 with: dotnet-version: | + 8.0.x 7.0.x 6.0.x - name: Display dotnet version diff --git a/.github/workflows/publish-nuget.yml b/.github/workflows/publish-nuget.yml index a494b4c..50b42ad 100644 --- a/.github/workflows/publish-nuget.yml +++ b/.github/workflows/publish-nuget.yml @@ -16,6 +16,7 @@ jobs: uses: actions/setup-dotnet@v3 with: dotnet-version: | + 8.0.x 7.0.x 6.0.x - name: Display dotnet version diff --git a/src/Playground/Playground.csproj b/src/Playground/Playground.csproj index c77937c..810ac00 100644 --- a/src/Playground/Playground.csproj +++ b/src/Playground/Playground.csproj @@ -2,7 +2,7 @@ Exe - net7.0 + net8.0 enable disable Debug;Release;ReleaseWithDoc diff --git a/src/ZoneTree.UnitTests/ZoneTree.UnitTests.csproj b/src/ZoneTree.UnitTests/ZoneTree.UnitTests.csproj index f770f3c..f423a79 100644 --- a/src/ZoneTree.UnitTests/ZoneTree.UnitTests.csproj +++ b/src/ZoneTree.UnitTests/ZoneTree.UnitTests.csproj @@ -1,7 +1,7 @@ - net7.0 + net8.0 enable disable diff --git a/src/ZoneTree/Core/ZoneTreeMetaWAL.cs b/src/ZoneTree/Core/ZoneTreeMetaWAL.cs index a239a2d..d6a8ed5 100644 --- a/src/ZoneTree/Core/ZoneTreeMetaWAL.cs +++ b/src/ZoneTree/Core/ZoneTreeMetaWAL.cs @@ -1,6 +1,7 @@ using System; using System.Runtime.InteropServices; using System.Text.Json; +using System.Text.Json.Serialization; using Tenray.ZoneTree.Exceptions; using Tenray.ZoneTree.Options; using Tenray.ZoneTree.Segments.RandomAccess; @@ -235,12 +236,7 @@ public void SaveMetaData( BottomSegments = bottomSegments, }; - var bytes = JsonSerializer.SerializeToUtf8Bytes( - newZoneTreeMeta, - new JsonSerializerOptions - { - WriteIndented = true - }); + var bytes = JsonSerializeToUtf8Bytes(newZoneTreeMeta); var deviceManager = Options.RandomAccessDeviceManager; var metaFilePath = deviceManager.GetFilePath(0, MetaFileCategory); @@ -289,10 +285,43 @@ public static ZoneTreeMeta LoadZoneTreeMetaWithoutWALRecords( var bytes = device.GetBytes(0, (int)device.Length); device.Close(); deviceManager.RemoveReadOnlyDevice(device.SegmentId, MetaFileCategory); - var meta = JsonSerializer.Deserialize(bytes); + var meta = JsonDeserialize(bytes); return meta; } + + private static byte[] JsonSerializeToUtf8Bytes(ZoneTreeMeta meta) + { +#if NET8_0_OR_GREATER + return JsonSerializer.SerializeToUtf8Bytes( + meta, + ZoneTreeMetaSourceGenerationContext.Default.ZoneTreeMeta); +#else + return JsonSerializer.SerializeToUtf8Bytes( + meta, + new JsonSerializerOptions() + { + WriteIndented = true + }); +#endif + } + + private static ZoneTreeMeta JsonDeserialize(byte[] bytes) + { +#if NET8_0_OR_GREATER + return JsonSerializer.Deserialize(bytes, ZoneTreeMetaSourceGenerationContext.Default.ZoneTreeMeta); +#else + return JsonSerializer.Deserialize(bytes); +#endif + } +} + +#if NET8_0_OR_GREATER +[JsonSourceGenerationOptions(WriteIndented = true)] +[JsonSerializable(typeof(ZoneTreeMeta))] +internal partial class ZoneTreeMetaSourceGenerationContext : JsonSerializerContext +{ } +#endif public enum MetaWalOperation { diff --git a/src/ZoneTree/ZoneTree.csproj b/src/ZoneTree/ZoneTree.csproj index 7949db6..8159f28 100644 --- a/src/ZoneTree/ZoneTree.csproj +++ b/src/ZoneTree/ZoneTree.csproj @@ -4,7 +4,7 @@ true true en-US - net7.0;net6.0 + net8.0;net7.0;net6.0 https://github.com/koculu/ZoneTree true enable