Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 193f68d

Browse files
committed
支持解析V1与V2同时出现在同一文件中的功能
1 parent a6afefe commit 193f68d

File tree

6 files changed

+61
-25
lines changed

6 files changed

+61
-25
lines changed

QuantBox.Data.Serializer/Converter/V1_to_V2.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,13 @@ public class V1_to_V2
1616
/// <returns></returns>
1717
public static V2.PbTick Converter(V1.PbTick oldTick)
1818
{
19+
if (oldTick == null)
20+
return null;
21+
1922
V2.PbTick tick = new V2.PbTick();
2023

2124
tick.Config = new V2.ConfigInfo().Default();
25+
tick.Config.Version = oldTick.Config.Version;
2226
tick.Config.AveragePriceMultiplier = oldTick.Config.AveragePriceMultiplier;
2327
tick.Config.ContractMultiplier = oldTick.Config.ContractMultiplier;
2428
tick.Config.SettlementPriceMultiplier = oldTick.Config.SettlementPriceMultiplier;

QuantBox.Data.Serializer/PbTickSerializer.cs

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,40 +15,53 @@ public class PbTickSerializer
1515
{
1616
private V1.PbTickSerializer v1Reader = new V1.PbTickSerializer();
1717
private V2.PbTickSerializer v2Reader = new V2.PbTickSerializer();
18-
private bool bV2 = true;
19-
20-
//public PbTickCodec Codec = new PbTickCodec();
21-
18+
private int nVersion = 2;
19+
private int nMaxVersion = 2;
20+
2221
public V2.PbTick ReadOne(Stream stream)
2322
{
23+
// 计划如果简单的将V2和V1的文件合并成一个文件也能读取
2424
long Position = stream.Position;
2525
V2.PbTick tick = null;
2626

27-
try
27+
bool bFisrtException = true;
28+
29+
do
2830
{
29-
if(bV2)
31+
try
3032
{
31-
tick = v2Reader.ReadOne(stream);
33+
switch (nVersion)
34+
{
35+
case 1:
36+
tick = V1_to_V2.Converter(v1Reader.ReadOne(stream));
37+
break;
38+
case 2:
39+
tick = v2Reader.ReadOne(stream);
40+
break;
41+
default:
42+
throw new Exception(string.Format("Invalid file, version {0}", nVersion));
43+
}
44+
return tick;
3245
}
33-
else
46+
catch (ProtobufDataZeroException ex)
3447
{
35-
V1.PbTick tmp = v1Reader.ReadOne(stream);
36-
if (tmp == null)
37-
return null;
38-
tick = V1_to_V2.Converter(tmp);
48+
bFisrtException = false;
49+
nVersion = ex.CurrentVersion;
50+
stream.Seek(Position, SeekOrigin.Begin);
3951
}
40-
}
41-
catch
42-
{
43-
bV2 = false;
44-
stream.Seek(Position, SeekOrigin.Begin);
45-
V1.PbTick tmp = v1Reader.ReadOne(stream);
46-
if (tmp == null)
47-
return null;
48-
tick = V1_to_V2.Converter(tmp);
49-
}
52+
catch(ProtoException ex)
53+
{
54+
// 第一次出现问题后,从最新格式开始向后进行解析
55+
if (bFisrtException)
56+
nVersion = nMaxVersion;
57+
else
58+
--nVersion;
59+
60+
bFisrtException = false;
61+
stream.Seek(Position, SeekOrigin.Begin);
62+
}
63+
}while(true);
5064

51-
//Codec.Config = tick.Config;
5265
return tick;
5366
}
5467

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace QuantBox.Data.Serializer
7+
{
8+
public class ProtobufDataZeroException:Exception
9+
{
10+
public int CurrentVersion;
11+
public int ExpectVersion;
12+
public ProtobufDataZeroException(string message,int currentVersion,int expectVersion):base(message)
13+
{
14+
CurrentVersion = currentVersion;
15+
ExpectVersion = expectVersion;
16+
}
17+
}
18+
}

QuantBox.Data.Serializer/QuantBox.Data.Serializer.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
</ItemGroup>
4747
<ItemGroup>
4848
<Compile Include="Converter\V1_to_V2.cs" />
49+
<Compile Include="ProtobufDataZeroException.cs" />
4950
<Compile Include="PbTickSerializer.cs" />
5051
<Compile Include="V1\Double2IntConverter.cs" />
5152
<Compile Include="V1\DoubleContract.cs" />

QuantBox.Data.Serializer/V1/PbTickSerializer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public PbTick ReadOne(Stream source)
5252
_lastRead = Codec.Restore(_lastRead, raw);
5353
if (_lastRead.Config.Version != 1)
5454
{
55-
throw new InvalidDataException("only support pd0 file version 1");
55+
throw new ProtobufDataZeroException("only support pd0 file version 1", _lastRead.Config.Version, 1);
5656
}
5757
return _lastRead;
5858
}

QuantBox.Data.Serializer/V2/PbTickSerializer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public PbTick ReadOne(Stream source)
9494
_lastRead = Codec.Restore(_lastRead, raw);
9595
if (_lastRead.Config.Version != 2)
9696
{
97-
throw new InvalidDataException("only support pd0 file version 2");
97+
throw new ProtobufDataZeroException("only support pd0 file version 2", _lastRead.Config.Version, 2);
9898
}
9999

100100
return _lastRead;

0 commit comments

Comments
 (0)