升级Binary,支持从压缩流中读取实体对象,自动探测数据流已到末尾

This commit is contained in:
大石头 2025-07-24 23:45:35 +08:00
parent 7c442f0f05
commit 67734969d6
5 changed files with 26 additions and 21 deletions

View File

@ -361,7 +361,7 @@ public class DbPackage
try
{
// 二进制读写器
var bn = new Binary
var binary = new Binary
{
FullTime = true,
EncodeInt = true,
@ -369,7 +369,7 @@ public class DbPackage
};
var dt = new DbTable();
dt.ReadHeader(bn);
dt.ReadHeader(binary);
WriteLog("恢复[{0}/{1}]开始,共[{2:n0}]行", table.Name, connName, dt.Total);
// 输出日志
@ -394,7 +394,7 @@ public class DbPackage
//修复总行数是pageSize的倍数无法退出循环的情况
if (dt.Total == row) break;
// 读取数据
dt.ReadData(bn, Math.Min(dt.Total - row, pageSize));
dt.ReadData(binary, Math.Min(dt.Total - row, pageSize));
var rs = dt.Rows;
if (rs == null || rs.Count == 0) break;

View File

@ -1942,13 +1942,16 @@ public partial class Entity<TEntity> : EntityBase, IAccessor where TEntity : Ent
/// <param name="extend">是否序列化扩展属性</param>
protected virtual Boolean OnRead(Stream stream, Object? context, Boolean extend)
{
if (context is not Binary bn) bn = new Binary { Stream = stream, EncodeInt = true, FullTime = true };
if (context is not Binary binary) binary = new Binary { Stream = stream, EncodeInt = true, FullTime = true };
var fs = extend ? Meta.AllFields : Meta.Fields;
foreach (var fi in fs)
{
Object? value = null;
if (!binary.TryRead(fi.Type, ref value)) return false;
// 顺序要求很高
SetItem(fi.Name, bn.Read(fi.Type));
SetItem(fi.Name, value);
}
return true;
@ -1960,12 +1963,12 @@ public partial class Entity<TEntity> : EntityBase, IAccessor where TEntity : Ent
/// <param name="extend">是否序列化扩展属性</param>
protected virtual Boolean OnWrite(Stream stream, Object? context, Boolean extend)
{
if (context is not Binary bn) bn = new Binary { Stream = stream, EncodeInt = true, FullTime = true };
if (context is not Binary binary) binary = new Binary { Stream = stream, EncodeInt = true, FullTime = true };
var fs = extend ? Meta.AllFields : Meta.Fields;
foreach (var fi in fs)
{
bn.Write(this[fi.Name], fi.Type);
binary.Write(this[fi.Name], fi.Type);
}
return true;

View File

@ -1228,15 +1228,14 @@ public static class EntityExtension
{
if (list == null) return 0;
//todo Binary需要字段记录已经写入多少字节部分数据流不支持Position
var bn = new Binary { Stream = stream, EncodeInt = true, FullTime = true };
var p = stream.Position;
// Binary需要字段记录已经写入多少字节部分数据流不支持Position
var binary = new Binary { Stream = stream, EncodeInt = true, FullTime = true };
foreach (var entity in list)
{
if (entity is IAccessor acc) acc.Write(stream, bn);
if (entity is IAccessor acc) acc.Write(stream, binary);
}
return stream.Position - p;
return binary.Total;
}
/// <summary>写入文件,二进制格式</summary>
@ -1250,10 +1249,10 @@ public static class EntityExtension
var compressed = file.EndsWithIgnoreCase(".gz");
return file.AsFile().OpenWrite(compressed, fs =>
{
var bn = new Binary { Stream = fs, EncodeInt = true, FullTime = true };
var binary = new Binary { Stream = fs, EncodeInt = true, FullTime = true };
foreach (var entity in list)
{
if (entity is IAccessor acc) acc.Write(fs, bn);
if (entity is IAccessor acc) acc.Write(fs, binary);
}
});
}
@ -1331,11 +1330,14 @@ public static class EntityExtension
{
if (factory == null || stream == null) yield break;
var bn = new Binary { Stream = stream, EncodeInt = true, FullTime = true };
while (stream.Position < stream.Length)
var binary = new Binary { Stream = stream, EncodeInt = true, FullTime = true };
while (!binary.EndOfStream)
{
var entity = factory.Create();
if (entity is IAccessor acc) acc.Read(stream, bn);
if (entity is IAccessor acc)
{
if (!acc.Read(stream, binary)) yield break;
}
yield return entity;
}

View File

@ -46,7 +46,7 @@
</PackageReference>
</ItemGroup>
<ItemGroup>
<PackageReference Include="NewLife.Core" Version="11.5.2025.701" />
<PackageReference Include="NewLife.Core" Version="11.5.2025.724-beta1230" />
</ItemGroup>
<ItemGroup>
<Using Include="NewLife" />

View File

@ -85,12 +85,12 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.14.1" />
<PackageReference Include="NewLife.Core" Version="11.5.2025.701" />
<PackageReference Include="NewLife.Core" Version="11.5.2025.724-beta1230" />
<PackageReference Include="NewLife.IP" Version="2.3.2025.601" />
<PackageReference Include="NewLife.UnitTest" Version="1.0.2025.101" />
<PackageReference Include="System.Diagnostics.PerformanceCounter" Version="9.0.6" />
<PackageReference Include="System.Diagnostics.PerformanceCounter" Version="9.0.7" />
<PackageReference Include="xunit" Version="2.9.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.1">
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.3">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>