Skip to content

Commit 451050c

Browse files
committed
Some Optimizations and revert JSON-Lib update, cassues Issues with JSON Validation
1 parent a9ff3c0 commit 451050c

4 files changed

Lines changed: 224 additions & 206 deletions

File tree

‎RDMSharp/Metadata/MetadataFactory.cs‎

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using System.Reflection;
1010
using System.Runtime.CompilerServices;
1111
using System.Text.Json;
12+
using System.Threading;
1213
using System.Threading.Tasks;
1314

1415
[assembly: InternalsVisibleTo("RDMSharpTests")]
@@ -24,6 +25,8 @@ public static class MetadataFactory
2425
private static ConcurrentDictionary<MetadataVersion, List<MetadataJSONObjectDefine>> metadataVersionDefinesBagDictionary;
2526
private static ConcurrentDictionary<ParameterBag, MetadataJSONObjectDefine> parameterBagDefineCache;
2627
private static List<Assembly> resourceProvider = new List<Assembly>() { typeof(MetadataFactory).Assembly };
28+
public static bool IsInitialized = false;
29+
private static readonly SemaphoreSlim fillDefaultMetadataVersionListSemaphoreSlim = new SemaphoreSlim(1, 1);
2730

2831
public static void AddResourceProvider(Assembly assembly)
2932
{
@@ -58,8 +61,11 @@ public static IReadOnlyCollection<string> GetResources(Assembly assembly)
5861
{
5962
return assembly.GetManifestResourceNames().Where(p => p.EndsWith(JSON_ENDING)).ToList().AsReadOnly();
6063
}
61-
private static void fillDefaultMetadataVersionList()
64+
private static async Task fillDefaultMetadataVersionList()
6265
{
66+
if (fillDefaultMetadataVersionListSemaphoreSlim.CurrentCount == 0)
67+
return;
68+
await fillDefaultMetadataVersionListSemaphoreSlim.WaitAsync();
6369
if (metadataVersionDefinesBagDictionary != null)
6470
return;
6571

@@ -87,7 +93,7 @@ private static void fillDefaultMetadataVersionList()
8793
{
8894
MaxDegreeOfParallelism = Environment.ProcessorCount, // Optional: Set the maximum degree of parallelism
8995
};
90-
Parallel.ForEach(nonSchemaVersions, parallelOptions, mv =>
96+
await Parallel.ForEachAsync(nonSchemaVersions, parallelOptions, async (mv, token) =>
9197
{
9298
try
9399
{
@@ -98,7 +104,8 @@ private static void fillDefaultMetadataVersionList()
98104
versionSchemas.TryAdd(schema.Version, jsonSchema);
99105
}
100106
MetadataBag metadataBag = new MetadataBag(mv);
101-
var result = jsonSchema.Evaluate(System.Text.Json.JsonElement.Parse(metadataBag.Content));
107+
var doc = JsonDocument.Parse(metadataBag.Content);
108+
var result = jsonSchema.Evaluate(doc.RootElement);
102109
if (result.IsValid)
103110
{
104111
MetadataJSONObjectDefine jsonDefine = JsonSerializer.Deserialize<MetadataJSONObjectDefine>(metadataBag.Content);
@@ -121,6 +128,8 @@ private static void fillDefaultMetadataVersionList()
121128
Logger?.LogError($"Exception while Deserialize {mv.Name}", e);
122129
}
123130
});
131+
IsInitialized = true;
132+
fillDefaultMetadataVersionListSemaphoreSlim.Release();
124133
}
125134

126135
public static IReadOnlyCollection<MetadataVersion> GetMetadataSchemaVersions()
@@ -466,4 +475,13 @@ public static Type GetDefinedDataTreeObjectType(ERDM_Parameter parameter, Comman
466475
return false;
467476
}).FirstOrDefault();
468477
}
478+
479+
public static async Task AwaitInitialize()
480+
{
481+
while (!IsInitialized)
482+
{
483+
GetMetadataDefineVersions();
484+
await Task.Delay(100);
485+
}
486+
}
469487
}

‎RDMSharp/RDMSharp.cs‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
using RDMSharp.Metadata;
2+
using System;
23
using System.Collections.Concurrent;
34
using System.Threading;
45
using System.Threading.Tasks;
@@ -71,13 +72,14 @@ public bool RequestReceived(RDMMessage request, out RDMMessage response)
7172
return response is not null;
7273
}
7374

74-
public static void Initialize(UID controllerUID, Func<RDMMessage, Task> sendMethode)
75+
public static async Task Initialize(UID controllerUID, Func<RDMMessage, Task> sendMethode)
7576
{
7677
if (_instance != null)
7778
{
7879
throw new InvalidOperationException("RDMSharp instance already exists. Use Instance property to access it.");
7980
}
8081
_instance = new RDMSharp(controllerUID, sendMethode);
82+
await MetadataFactory.AwaitInitialize();
8183
}
8284

8385
public class RequestReceivedEventArgs : EventArgs

‎RDMSharp/RDMSharp.csproj‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
33
<TargetFrameworks>net6.0;net7.0;net8.0;net9.0;net10.0</TargetFrameworks>
44
<PackageLicenseFile>LICENSE.md</PackageLicenseFile>
@@ -35,7 +35,7 @@
3535
</ItemGroup>
3636
<ItemGroup>
3737
<PackageReference Include="DMXControlProjects.WellKnownDataTypes" Version="0.0.5" />
38-
<PackageReference Include="JsonSchema.Net" Version="8.0.3" />
38+
<PackageReference Include="JsonSchema.Net" Version="7.4.0" />
3939
</ItemGroup>
4040
<ItemGroup Condition="'$(TargetFramework)' == 'net8.0' Or '$(TargetFramework)' == 'net9.0'Or '$(TargetFramework)' == 'net10.0'">
4141
<PackageReference Include="Microsoft.Extensions.Logging" Version="10.0.1" />

0 commit comments

Comments
 (0)