diff --git a/ZeroLevel.UnitTests/SerializationTests.cs b/ZeroLevel.UnitTests/SerializationTests.cs index b78c0ca..4f2b1fa 100644 --- a/ZeroLevel.UnitTests/SerializationTests.cs +++ b/ZeroLevel.UnitTests/SerializationTests.cs @@ -48,7 +48,7 @@ namespace ZeroLevel.Serialization { // Act var data = MessageSerializer.SerializeCompatible>(value); - var clone = MessageSerializer.DeserializeCompatible>(data); + var clone = MessageSerializer.DeserializeCompatible>(data); // Assert if (value == null && clone != null && !clone.Any()) return; // OK @@ -511,6 +511,26 @@ namespace ZeroLevel.Serialization MakePrimitiveTest(CompositeInstanceFactory.MakeDocument(), comparator); } + [Fact] + public void SerializeCompositeOnjectCollection() + { + var comparator = new Func((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(collection); + var restored = MessageSerializer.DeserializeCollection(data); + var restored_lazy = MessageSerializer.DeserializeCollectionLazy(data); + + // Assert + Assert.True(CollectionComparsionExtensions.OrderingEquals(collection, restored, comparator)); + Assert.True(CollectionComparsionExtensions.OrderingEquals(collection, restored_lazy, comparator)); + } + [Fact] public void ReverseByteOrderTest() { diff --git a/ZeroLevel/Services/FileSystem/FSUtils.cs b/ZeroLevel/Services/FileSystem/FSUtils.cs index da1b9cf..f637f41 100644 --- a/ZeroLevel/Services/FileSystem/FSUtils.cs +++ b/ZeroLevel/Services/FileSystem/FSUtils.cs @@ -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); + } } } \ No newline at end of file diff --git a/ZeroLevel/Services/Serialization/IBinaryReader.cs b/ZeroLevel/Services/Serialization/IBinaryReader.cs index 0f431f2..d58ff82 100644 --- a/ZeroLevel/Services/Serialization/IBinaryReader.cs +++ b/ZeroLevel/Services/Serialization/IBinaryReader.cs @@ -94,6 +94,30 @@ namespace ZeroLevel.Services.Serialization List ReadUShortCollection(); #endregion + #region Collections lazy + IEnumerable ReadCollectionLazy() + where T : IBinarySerializable, new(); + IEnumerable ReadStringCollectionLazy(); + IEnumerable ReadIPCollectionLazy(); + IEnumerable ReadIPEndPointCollectionLazy(); + IEnumerable ReadGuidCollectionLazy(); + IEnumerable ReadDateTimeCollectionLazy(); + IEnumerable ReadInt64CollectionLazy(); + IEnumerable ReadInt32CollectionLazy(); + IEnumerable ReadUInt64CollectionLazy(); + IEnumerable ReadUInt32CollectionLazy(); + IEnumerable ReadCharCollectionLazy(); + IEnumerable ReadShortCollectionLazy(); + IEnumerable ReadUShortCollectionLazy(); + IEnumerable ReadFloatCollectionLazy(); + IEnumerable ReadDoubleCollectionLazy(); + IEnumerable ReadBooleanCollectionLazy(); + IEnumerable ReadByteCollectionLazy(); + IEnumerable ReadByteArrayCollectionLazy(); + IEnumerable ReadDecimalCollectionLazy(); + IEnumerable ReadTimeSpanCollectionLazy(); + #endregion + T Read() where T : IBinarySerializable; T Read(object arg) where T : IBinarySerializable; diff --git a/ZeroLevel/Services/Serialization/MemoryStreamReader.cs b/ZeroLevel/Services/Serialization/MemoryStreamReader.cs index 972be57..94674be 100644 --- a/ZeroLevel/Services/Serialization/MemoryStreamReader.cs +++ b/ZeroLevel/Services/Serialization/MemoryStreamReader.cs @@ -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 ReadCollectionLazy() + 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 ReadStringCollectionLazy() + { + int count = ReadInt32(); + if (count > 0) + { + for (int i = 0; i < count; i++) + { + yield return ReadString(); + } + } + } + + public IEnumerable ReadIPCollectionLazy() + { + int count = ReadInt32(); + if (count > 0) + { + for (int i = 0; i < count; i++) + { + yield return ReadIP(); + } + } + } + + public IEnumerable ReadIPEndPointCollectionLazy() + { + int count = ReadInt32(); + if (count > 0) + { + for (int i = 0; i < count; i++) + { + yield return ReadIPEndpoint(); + } + } + } + + public IEnumerable ReadGuidCollectionLazy() + { + int count = ReadInt32(); + if (count > 0) + { + for (int i = 0; i < count; i++) + { + yield return ReadGuid(); + } + } + } + + public IEnumerable ReadDateTimeCollectionLazy() + { + int count = ReadInt32(); + if (count > 0) + { + for (int i = 0; i < count; i++) + { + yield return ReadDateTime() ?? DateTime.MinValue; + } + } + } + + public IEnumerable ReadInt64CollectionLazy() + { + int count = ReadInt32(); + if (count > 0) + { + for (int i = 0; i < count; i++) + { + yield return ReadLong(); + } + } + } + + public IEnumerable ReadInt32CollectionLazy() + { + int count = ReadInt32(); + if (count > 0) + { + for (int i = 0; i < count; i++) + { + yield return ReadInt32(); + } + } + } + + public IEnumerable ReadUInt64CollectionLazy() + { + int count = ReadInt32(); + if (count > 0) + { + for (int i = 0; i < count; i++) + { + yield return ReadULong(); + } + } + } + + public IEnumerable ReadUInt32CollectionLazy() + { + int count = ReadInt32(); + if (count > 0) + { + for (int i = 0; i < count; i++) + { + yield return ReadUInt32(); + } + } + } + + public IEnumerable ReadCharCollectionLazy() + { + int count = ReadInt32(); + if (count > 0) + { + for (int i = 0; i < count; i++) + { + yield return ReadChar(); + } + } + } + + public IEnumerable ReadShortCollectionLazy() + { + int count = ReadInt32(); + if (count > 0) + { + for (int i = 0; i < count; i++) + { + yield return ReadShort(); + } + } + } + + public IEnumerable ReadUShortCollectionLazy() + { + int count = ReadInt32(); + if (count > 0) + { + for (int i = 0; i < count; i++) + { + yield return ReadUShort(); + } + } + } + + public IEnumerable ReadFloatCollectionLazy() + { + int count = ReadInt32(); + if (count > 0) + { + for (int i = 0; i < count; i++) + { + yield return ReadFloat(); + } + } + } + + public IEnumerable ReadDoubleCollectionLazy() + { + int count = ReadInt32(); + if (count > 0) + { + for (int i = 0; i < count; i++) + { + yield return ReadDouble(); + } + } + } + + public IEnumerable ReadBooleanCollectionLazy() + { + int count = ReadInt32(); + if (count > 0) + { + for (int i = 0; i < count; i++) + { + yield return ReadBoolean(); + } + } + } + + public IEnumerable ReadByteCollectionLazy() + { + int count = ReadInt32(); + if (count > 0) + { + for (int i = 0; i < count; i++) + { + yield return ReadByte(); + } + } + } + + public IEnumerable ReadByteArrayCollectionLazy() + { + int count = ReadInt32(); + if (count > 0) + { + for (int i = 0; i < count; i++) + { + yield return ReadBytes(); + } + } + } + + public IEnumerable ReadDecimalCollectionLazy() + { + int count = ReadInt32(); + if (count > 0) + { + for (int i = 0; i < count; i++) + { + yield return ReadDecimal(); + } + } + } + + public IEnumerable ReadTimeSpanCollectionLazy() + { + int count = ReadInt32(); + if (count > 0) + { + for (int i = 0; i < count; i++) + { + yield return ReadTimeSpan(); + } + } + } + #endregion + #region Arrays public T[] ReadArray() where T : IBinarySerializable, new() diff --git a/ZeroLevel/Services/Serialization/MessageSerializer.cs b/ZeroLevel/Services/Serialization/MessageSerializer.cs index 57880ba..2bea9c0 100644 --- a/ZeroLevel/Services/Serialization/MessageSerializer.cs +++ b/ZeroLevel/Services/Serialization/MessageSerializer.cs @@ -120,6 +120,27 @@ namespace ZeroLevel.Services.Serialization return collection; } + public static IEnumerable DeserializeCollectionLazy(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(); + item.Deserialize(reader); + yield return item; + } + } + } + } + } + public static T DeserializeCompatible(byte[] data) { if (data == null || data.Length == 0) return default(T); @@ -322,6 +343,27 @@ namespace ZeroLevel.Services.Serialization return collection; } + public static IEnumerable DeserializeCollectionLazy(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(); + item.Deserialize(reader); + yield return item; + } + } + } + } + } + public static T DeserializeCompatible(Stream stream) { if (stream == null) return default(T); diff --git a/ZeroLevel/ZeroLevel.csproj b/ZeroLevel/ZeroLevel.csproj index cf2666b..9f628bd 100644 --- a/ZeroLevel/ZeroLevel.csproj +++ b/ZeroLevel/ZeroLevel.csproj @@ -6,16 +6,16 @@ ogoun ogoun - 3.3.5.2 - Added tcp proxy + 3.3.5.3 + Collection lazy deserialization https://github.com/ogoun/Zero/wiki Copyright Ogoun 2021 https://github.com/ogoun/Zero GitHub - 3.3.5.2 - 3.3.5.2 + 3.3.5.3 + 3.3.5.3 AnyCPU;x64;x86 zero.png