-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added serialization based on Avro (#95)
* Added first serialization based on Avro Binary codecs * Added Json encoder for Avro * Update documentation
- Loading branch information
1 parent
1af11f6
commit 39ed4be
Showing
25 changed files
with
1,120 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
src/net/KEFCore.SerDes.Avro.Compiler/AvroSerializationHelper.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* Copyright 2023 MASES s.r.l. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* Refer to LICENSE for more information. | ||
*/ | ||
|
||
// #define DEBUG_PERFORMANCE | ||
|
||
#nullable enable | ||
|
||
using Avro; | ||
|
||
namespace MASES.EntityFrameworkCore.KNet.Serialization.Avro.Compiler; | ||
|
||
public static class AvroSerializationHelper | ||
{ | ||
public static void BuildSchemaClasses( string outputFolder, params string[] schemas) | ||
{ | ||
var codegen = new CodeGen(); | ||
foreach (var schema in schemas) | ||
{ | ||
codegen.AddSchema(schema); | ||
} | ||
codegen.GenerateCode(); | ||
codegen.WriteTypes(outputFolder, true); | ||
} | ||
|
||
public static void BuildSchemaClassesFromFiles( string outputFolder, params string[] schemaFiles) | ||
{ | ||
var codegen = new CodeGen(); | ||
foreach (var schemaFile in schemaFiles) | ||
{ | ||
var schema = File.ReadAllText(schemaFile); | ||
codegen.AddSchema(schema); | ||
} | ||
codegen.GenerateCode(); | ||
codegen.WriteTypes(outputFolder, true); | ||
} | ||
|
||
public static void BuildDefaultSchema(string outputFolder) | ||
{ | ||
BuildSchemaClassesFromFiles(outputFolder, "AvroValueContainer.avsc"); | ||
} | ||
} | ||
|
30 changes: 30 additions & 0 deletions
30
src/net/KEFCore.SerDes.Avro.Compiler/KEFCore.SerDes.Avro.Compiler.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<Import Project="..\Common\Common.props" /> | ||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<ImplicitUsings>true</ImplicitUsings> | ||
<AssemblyName>MASES.EntityFrameworkCore.KNet.Serialization.Avro.Compiler</AssemblyName> | ||
<RootNamespace>MASES.EntityFrameworkCore.KNet.Serialization.Avro</RootNamespace> | ||
<Title>EntityFrameworkCore KNet - Avro Serialization support for EntityFrameworkCore provider for Apache Kafka</Title> | ||
<Description>EntityFrameworkCore KNet - Avro Serialization support for EntityFrameworkCore provider for Apache Kafka</Description> | ||
<Product>MASES.EntityFrameworkCore.KNet.Serialization.Avro</Product> | ||
<OutputPath>..\..\..\bin\</OutputPath> | ||
<PackageTags>Entity Framework Core;entity-framework-core;ef;efcore;orm;avro;sql kafka apache-kafka dotnet clr netcore net5 net6 kafka connect streams producer consumer providers streamprovider confluent</PackageTags> | ||
<Nullable>enable</Nullable> | ||
<GeneratePackageOnBuild>false</GeneratePackageOnBuild> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Apache.Avro" Version="1.11.3" /> | ||
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
</PackageReference> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<None Update="AvroValueContainer.avsc"> | ||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> | ||
</None> | ||
</ItemGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* MIT License | ||
* | ||
* Copyright (c) 2022 MASES s.r.l. | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
|
||
using System.Diagnostics; | ||
|
||
namespace MASES.EntityFrameworkCore.KNet.Serialization.Avro.Compiler; | ||
|
||
partial class Program | ||
{ | ||
static void ReportString(string message) | ||
{ | ||
if (Debugger.IsAttached) | ||
{ | ||
Trace.WriteLine($"{DateTime.Now:HH::mm::ss:ffff} - {message}"); | ||
} | ||
else | ||
{ | ||
Console.WriteLine($"{DateTime.Now:HH::mm::ss:ffff} - {message}"); | ||
} | ||
} | ||
|
||
static void Main(string[] args) | ||
{ | ||
try | ||
{ | ||
AvroSerializationHelper.BuildDefaultSchema("Generated"); | ||
} | ||
catch (Exception ex) | ||
{ | ||
ReportString(ex.ToString()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
{ | ||
"namespace": "MASES.EntityFrameworkCore.KNet.Serialization.Avro.Storage", | ||
"type": "record", | ||
"name": "AvroValueContainer", | ||
"doc": "Represents the storage container type to be used from KEFCore", | ||
"fields": [ | ||
{ | ||
"name": "EntityName", | ||
"type": "string" | ||
}, | ||
{ | ||
"name": "ClrType", | ||
"type": "string" | ||
}, | ||
{ | ||
"name": "Data", | ||
"type": { | ||
"type": "array", | ||
"items": { | ||
"namespace": "MASES.EntityFrameworkCore.KNet.Serialization.Avro.Storage", | ||
"type": "record", | ||
"name": "PropertyDataRecord", | ||
"doc": "Represents the single container for Entity properties stored in AvroValueContainer and used from KEFCore", | ||
"fields": [ | ||
{ | ||
"name": "PropertyIndex", | ||
"type": "int" | ||
}, | ||
{ | ||
"name": "PropertyName", | ||
"type": "string" | ||
}, | ||
{ | ||
"name": "ClrType", | ||
"type": "string" | ||
}, | ||
{ | ||
"name": "Value", | ||
"type": [ | ||
"null", | ||
"boolean", | ||
"int", | ||
"long", | ||
"float", | ||
"double", | ||
"string" | ||
] | ||
} | ||
] | ||
} | ||
} | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/* | ||
* Copyright 2023 MASES s.r.l. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* Refer to LICENSE for more information. | ||
*/ | ||
|
||
// #define DEBUG_PERFORMANCE | ||
|
||
#nullable enable | ||
|
||
using Avro; | ||
|
||
namespace MASES.EntityFrameworkCore.KNet.Serialization.Avro.Storage; | ||
|
||
/// <summary> | ||
/// The default ValueContainer used from KEFCore | ||
/// </summary> | ||
/// <typeparam name="TKey">It is the key <see cref="Type"/> passed from Entity Framework associated to the Entity data will be stored in the <see cref="AvroValueContainer{TKey}"/></typeparam> | ||
public partial class AvroValueContainer<TKey> : AvroValueContainer, IValueContainer<TKey> where TKey : notnull | ||
{ | ||
/// <summary> | ||
/// Initialize a new instance of <see cref="AvroValueContainer{TKey}"/> | ||
/// </summary> | ||
/// <remarks>It is mainly used from the JSON serializer</remarks> | ||
public AvroValueContainer() { } | ||
/// <summary> | ||
/// Initialize a new instance of <see cref="AvroValueContainer{TKey}"/> | ||
/// </summary> | ||
/// <param name="tName">The <see cref="IEntityType"/> requesting the <see cref="AvroValueContainer{TKey}"/> for <paramref name="rData"/></param> | ||
/// <param name="rData">The data, built from EFCore, to be stored in the <see cref="AvroValueContainer{TKey}"/></param> | ||
/// <remarks>This constructor is mandatory and it is used from KEFCore to request a <see cref="AvroValueContainer{TKey}"/></remarks> | ||
public AvroValueContainer(IEntityType tName, object[] rData) | ||
{ | ||
EntityName = tName.Name; | ||
ClrType = tName.ClrType.FullName!; | ||
Data = new List<PropertyDataRecord>(); | ||
foreach (var item in tName.GetProperties()) | ||
{ | ||
int index = item.GetIndex(); | ||
var pRecord = new PropertyDataRecord | ||
{ | ||
PropertyIndex = index, | ||
PropertyName = item.Name, | ||
ClrType = item.ClrType?.FullName, | ||
Value = rData[index] | ||
}; | ||
Data.Add(pRecord); | ||
} | ||
} | ||
/// <inheritdoc/> | ||
public void GetData(IEntityType tName, ref object[] array) | ||
{ | ||
#if DEBUG_PERFORMANCE | ||
Stopwatch fullSw = new Stopwatch(); | ||
Stopwatch newSw = new Stopwatch(); | ||
Stopwatch iterationSw = new Stopwatch(); | ||
try | ||
{ | ||
fullSw.Start(); | ||
#endif | ||
if (Data == null) { return; } | ||
#if DEBUG_PERFORMANCE | ||
newSw.Start(); | ||
#endif | ||
array = new object[Data.Count]; | ||
#if DEBUG_PERFORMANCE | ||
newSw.Stop(); | ||
iterationSw.Start(); | ||
#endif | ||
for (int i = 0; i < Data.Count; i++) | ||
{ | ||
array[i] = Data[i].Value!; | ||
} | ||
#if DEBUG_PERFORMANCE | ||
iterationSw.Stop(); | ||
fullSw.Stop(); | ||
} | ||
finally | ||
{ | ||
if (Infrastructure.KafkaDbContext.TraceEntityTypeDataStorageGetData) | ||
{ | ||
Infrastructure.KafkaDbContext.ReportString($"Time to GetData with length {Data.Count}: {fullSw.Elapsed} - new array took: {newSw.Elapsed} - Iteration took: {iterationSw.Elapsed}"); | ||
} | ||
} | ||
#endif | ||
} | ||
} |
Oops, something went wrong.