Collection lazy deserialization

pull/1/head
unknown 4 years ago
parent 59e855e31e
commit b69ee03bbf

@ -48,7 +48,7 @@ namespace ZeroLevel.Serialization
{
// Act
var data = MessageSerializer.SerializeCompatible<IEnumerable<T>>(value);
var clone = MessageSerializer.DeserializeCompatible<IEnumerable<T>>(data);
var clone = MessageSerializer.DeserializeCompatible<IEnumerable<T>>(data);
// Assert
if (value == null && clone != null && !clone.Any()) return; // OK
@ -511,6 +511,26 @@ namespace ZeroLevel.Serialization
MakePrimitiveTest<Document>(CompositeInstanceFactory.MakeDocument(), comparator);
}
[Fact]
public void SerializeCompositeOnjectCollection()
{
var comparator = new Func<Document, Document, bool>((left, right) =>
{
var l_bin = MessageSerializer.Serialize(left);
var r_bin = MessageSerializer.Serialize(right);
return ArrayExtensions.UnsafeEquals(l_bin, r_bin);
});
var collection = new Document[] { CompositeInstanceFactory.MakeDocument(), CompositeInstanceFactory.MakeDocument(), CompositeInstanceFactory.MakeDocument() };
var data = MessageSerializer.Serialize<Document>(collection);
var restored = MessageSerializer.DeserializeCollection<Document>(data);
var restored_lazy = MessageSerializer.DeserializeCollectionLazy<Document>(data);
// Assert
Assert.True(CollectionComparsionExtensions.OrderingEquals<Document>(collection, restored, comparator));
Assert.True(CollectionComparsionExtensions.OrderingEquals<Document>(collection, restored_lazy, comparator));
}
[Fact]
public void ReverseByteOrderTest()
{

@ -2,7 +2,6 @@
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Security.AccessControl;
using System.Threading;
namespace ZeroLevel.Services.FileSystem
@ -306,8 +305,17 @@ namespace ZeroLevel.Services.FileSystem
}
public static bool IsDirectoryEmpty(string path)
{
{
return !Directory.EnumerateFileSystemEntries(path).Any();
}
public static string GetAbsolutePath(string path)
{
if (Path.IsPathRooted(path) == false)
{
path = Path.Combine(Configuration.BaseDirectory, path);
}
return Path.GetFullPath(path);
}
}
}

@ -94,6 +94,30 @@ namespace ZeroLevel.Services.Serialization
List<ushort> ReadUShortCollection();
#endregion
#region Collections lazy
IEnumerable<T> ReadCollectionLazy<T>()
where T : IBinarySerializable, new();
IEnumerable<string> ReadStringCollectionLazy();
IEnumerable<IPAddress> ReadIPCollectionLazy();
IEnumerable<IPEndPoint> ReadIPEndPointCollectionLazy();
IEnumerable<Guid> ReadGuidCollectionLazy();
IEnumerable<DateTime> ReadDateTimeCollectionLazy();
IEnumerable<Int64> ReadInt64CollectionLazy();
IEnumerable<Int32> ReadInt32CollectionLazy();
IEnumerable<UInt64> ReadUInt64CollectionLazy();
IEnumerable<UInt32> ReadUInt32CollectionLazy();
IEnumerable<char> ReadCharCollectionLazy();
IEnumerable<short> ReadShortCollectionLazy();
IEnumerable<ushort> ReadUShortCollectionLazy();
IEnumerable<float> ReadFloatCollectionLazy();
IEnumerable<Double> ReadDoubleCollectionLazy();
IEnumerable<bool> ReadBooleanCollectionLazy();
IEnumerable<byte> ReadByteCollectionLazy();
IEnumerable<byte[]> ReadByteArrayCollectionLazy();
IEnumerable<decimal> ReadDecimalCollectionLazy();
IEnumerable<TimeSpan> ReadTimeSpanCollectionLazy();
#endregion
T Read<T>() where T : IBinarySerializable;
T Read<T>(object arg) where T : IBinarySerializable;

@ -242,7 +242,7 @@ namespace ZeroLevel.Services.Serialization
public bool CheckOutOfRange(int offset)
{
return (_stream.Position + offset) > _stream.Length;
}
}
#region Extensions
@ -531,6 +531,251 @@ namespace ZeroLevel.Services.Serialization
}
#endregion
#region Collections lazy
public IEnumerable<T> ReadCollectionLazy<T>()
where T : IBinarySerializable, new()
{
int count = ReadInt32();
if (count > 0)
{
for (int i = 0; i < count; i++)
{
var item = new T();
item.Deserialize(this);
yield return item;
}
}
}
public IEnumerable<string> ReadStringCollectionLazy()
{
int count = ReadInt32();
if (count > 0)
{
for (int i = 0; i < count; i++)
{
yield return ReadString();
}
}
}
public IEnumerable<IPAddress> ReadIPCollectionLazy()
{
int count = ReadInt32();
if (count > 0)
{
for (int i = 0; i < count; i++)
{
yield return ReadIP();
}
}
}
public IEnumerable<IPEndPoint> ReadIPEndPointCollectionLazy()
{
int count = ReadInt32();
if (count > 0)
{
for (int i = 0; i < count; i++)
{
yield return ReadIPEndpoint();
}
}
}
public IEnumerable<Guid> ReadGuidCollectionLazy()
{
int count = ReadInt32();
if (count > 0)
{
for (int i = 0; i < count; i++)
{
yield return ReadGuid();
}
}
}
public IEnumerable<DateTime> ReadDateTimeCollectionLazy()
{
int count = ReadInt32();
if (count > 0)
{
for (int i = 0; i < count; i++)
{
yield return ReadDateTime() ?? DateTime.MinValue;
}
}
}
public IEnumerable<Int64> ReadInt64CollectionLazy()
{
int count = ReadInt32();
if (count > 0)
{
for (int i = 0; i < count; i++)
{
yield return ReadLong();
}
}
}
public IEnumerable<Int32> ReadInt32CollectionLazy()
{
int count = ReadInt32();
if (count > 0)
{
for (int i = 0; i < count; i++)
{
yield return ReadInt32();
}
}
}
public IEnumerable<UInt64> ReadUInt64CollectionLazy()
{
int count = ReadInt32();
if (count > 0)
{
for (int i = 0; i < count; i++)
{
yield return ReadULong();
}
}
}
public IEnumerable<UInt32> ReadUInt32CollectionLazy()
{
int count = ReadInt32();
if (count > 0)
{
for (int i = 0; i < count; i++)
{
yield return ReadUInt32();
}
}
}
public IEnumerable<char> ReadCharCollectionLazy()
{
int count = ReadInt32();
if (count > 0)
{
for (int i = 0; i < count; i++)
{
yield return ReadChar();
}
}
}
public IEnumerable<short> ReadShortCollectionLazy()
{
int count = ReadInt32();
if (count > 0)
{
for (int i = 0; i < count; i++)
{
yield return ReadShort();
}
}
}
public IEnumerable<ushort> ReadUShortCollectionLazy()
{
int count = ReadInt32();
if (count > 0)
{
for (int i = 0; i < count; i++)
{
yield return ReadUShort();
}
}
}
public IEnumerable<float> ReadFloatCollectionLazy()
{
int count = ReadInt32();
if (count > 0)
{
for (int i = 0; i < count; i++)
{
yield return ReadFloat();
}
}
}
public IEnumerable<Double> ReadDoubleCollectionLazy()
{
int count = ReadInt32();
if (count > 0)
{
for (int i = 0; i < count; i++)
{
yield return ReadDouble();
}
}
}
public IEnumerable<bool> ReadBooleanCollectionLazy()
{
int count = ReadInt32();
if (count > 0)
{
for (int i = 0; i < count; i++)
{
yield return ReadBoolean();
}
}
}
public IEnumerable<byte> ReadByteCollectionLazy()
{
int count = ReadInt32();
if (count > 0)
{
for (int i = 0; i < count; i++)
{
yield return ReadByte();
}
}
}
public IEnumerable<byte[]> ReadByteArrayCollectionLazy()
{
int count = ReadInt32();
if (count > 0)
{
for (int i = 0; i < count; i++)
{
yield return ReadBytes();
}
}
}
public IEnumerable<decimal> ReadDecimalCollectionLazy()
{
int count = ReadInt32();
if (count > 0)
{
for (int i = 0; i < count; i++)
{
yield return ReadDecimal();
}
}
}
public IEnumerable<TimeSpan> ReadTimeSpanCollectionLazy()
{
int count = ReadInt32();
if (count > 0)
{
for (int i = 0; i < count; i++)
{
yield return ReadTimeSpan();
}
}
}
#endregion
#region Arrays
public T[] ReadArray<T>()
where T : IBinarySerializable, new()

@ -120,6 +120,27 @@ namespace ZeroLevel.Services.Serialization
return collection;
}
public static IEnumerable<T> DeserializeCollectionLazy<T>(byte[] data)
where T : IBinarySerializable
{
if (data != null && data.Length > 0)
{
using (var reader = new MemoryStreamReader(data))
{
int count = reader.ReadInt32();
if (count > 0)
{
for (int i = 0; i < count; i++)
{
var item = Activator.CreateInstance<T>();
item.Deserialize(reader);
yield return item;
}
}
}
}
}
public static T DeserializeCompatible<T>(byte[] data)
{
if (data == null || data.Length == 0) return default(T);
@ -322,6 +343,27 @@ namespace ZeroLevel.Services.Serialization
return collection;
}
public static IEnumerable<T> DeserializeCollectionLazy<T>(Stream stream)
where T : IBinarySerializable
{
if (stream != null)
{
using (var reader = new MemoryStreamReader(stream))
{
int count = reader.ReadInt32();
if (count > 0)
{
for (int i = 0; i < count; i++)
{
var item = Activator.CreateInstance<T>();
item.Deserialize(reader);
yield return item;
}
}
}
}
}
public static T DeserializeCompatible<T>(Stream stream)
{
if (stream == null) return default(T);

@ -6,16 +6,16 @@
</Description>
<Authors>ogoun</Authors>
<Company>ogoun</Company>
<AssemblyVersion>3.3.5.2</AssemblyVersion>
<PackageReleaseNotes>Added tcp proxy</PackageReleaseNotes>
<AssemblyVersion>3.3.5.3</AssemblyVersion>
<PackageReleaseNotes>Collection lazy deserialization</PackageReleaseNotes>
<PackageProjectUrl>https://github.com/ogoun/Zero/wiki</PackageProjectUrl>
<Copyright>Copyright Ogoun 2021</Copyright>
<PackageLicenseUrl></PackageLicenseUrl>
<PackageIconUrl></PackageIconUrl>
<RepositoryUrl>https://github.com/ogoun/Zero</RepositoryUrl>
<RepositoryType>GitHub</RepositoryType>
<Version>3.3.5.2</Version>
<FileVersion>3.3.5.2</FileVersion>
<Version>3.3.5.3</Version>
<FileVersion>3.3.5.3</FileVersion>
<Platforms>AnyCPU;x64;x86</Platforms>
<PackageIcon>zero.png</PackageIcon>
</PropertyGroup>

Loading…
Cancel
Save

Powered by TurnKey Linux.