使用 Protobuf-net 将大型数据文件作为 IEnumerable 进行流式传输

发布于 2024-12-01 14:04:11 字数 2066 浏览 0 评论 0原文

我正在尝试使用 Protobuf-net 将数据保存和加载到磁盘,但卡住了。

我有一个需要处理的资产组合,并且我希望能够尽快完成。我已经可以从 CSV 中读取数据,但使用二进制文件会更快,所以我正在研究 Protobuf-Net。

我无法将所有资源放入内存中,因此我想对它们进行流式传输,而不是将它们全部加载到内存中。

所以我需要做的是将大量记录公开为 IEnumerable。 Protobuf-Net 可以实现这一点吗?我尝试了一些方法,但未能使其运行。

序列化似乎有效,但我无法再次读回它们,我得到 0 资产。有人可以指出我正确的方向吗?查看了 Serializer 类中的方法,但找不到任何涵盖这种情况的方法。 Protobuf-net 支持这个用例吗?顺便说一句,我正在使用V2。

提前致谢,

Gert-Jan

这是我尝试过的一些示例代码:

public partial class MainWindow : Window {

    // Generate x Assets
    IEnumerable<Asset> GenerateAssets(int Count) {
        var rnd = new Random();
        for (int i = 1; i < Count; i++) {
            yield return new Asset {
                ID = i,
                EAD = i * 12345,
                LGD = (float)rnd.NextDouble(),
                PD = (float)rnd.NextDouble()
            };
        }
    }

    // write assets to file
    private void Write(string path, IEnumerable<Asset> assets){
        using (var file = File.Create(path)) {
            Serializer.Serialize<IEnumerable<Asset>>(file, assets);
        }
    }

    // read assets from file
    IEnumerable<Asset> Read(string path) {
        using (var file = File.OpenRead(path)) {
            return Serializer.DeserializeItems<Asset>(file, PrefixStyle.None, -1);
        }
    }

    // try it 
    private void Test() {
        Write("Data.bin", GenerateAssets(100)); // this creates a file with binary gibberish that I assume are the assets
        var x = Read("Data.bin");
        MessageBox.Show(x.Count().ToString()); // returns 0 instead of 100
    }

    public MainWindow() {
        InitializeComponent();
    }

    private void button2_Click(object sender, RoutedEventArgs e) {
        Test();
    }
}

[ProtoContract]
class Asset {

    [ProtoMember(1)]
    public int ID { get; set; }

    [ProtoMember(2)]
    public double EAD { get; set; }

    [ProtoMember(3)]
    public float LGD { get; set; }

    [ProtoMember(4)]
    public float PD { get; set; }
}

I'm trying to use Protobuf-net to save and load data to disk but got stuck.

I have a portfolio of assets that I need to process, and I want to be able to do that as fast as possible. I can already read from a CSV but it would be faster to use a binary file, so I'm looking into Protobuf-Net.

I can't fit all assets into memory so I want to stream them, not load them all into memory.

So what I need to do is expose a large set of records as an IEnumerable. Is this possible with Protobuf-Net? I've tried a couple of things but haven't been able to get it running.

Serializing seems to work, but I haven't been able to read them back in again, I get 0 assets back. Could someone point me in the right direction please? Looked at the methods in the Serializer class but can't find any that covers this case. I this use-case supported by Protobuf-net? I'm using V2 by the way.

Thanks in advance,

Gert-Jan

Here's some sample code I tried:

public partial class MainWindow : Window {

    // Generate x Assets
    IEnumerable<Asset> GenerateAssets(int Count) {
        var rnd = new Random();
        for (int i = 1; i < Count; i++) {
            yield return new Asset {
                ID = i,
                EAD = i * 12345,
                LGD = (float)rnd.NextDouble(),
                PD = (float)rnd.NextDouble()
            };
        }
    }

    // write assets to file
    private void Write(string path, IEnumerable<Asset> assets){
        using (var file = File.Create(path)) {
            Serializer.Serialize<IEnumerable<Asset>>(file, assets);
        }
    }

    // read assets from file
    IEnumerable<Asset> Read(string path) {
        using (var file = File.OpenRead(path)) {
            return Serializer.DeserializeItems<Asset>(file, PrefixStyle.None, -1);
        }
    }

    // try it 
    private void Test() {
        Write("Data.bin", GenerateAssets(100)); // this creates a file with binary gibberish that I assume are the assets
        var x = Read("Data.bin");
        MessageBox.Show(x.Count().ToString()); // returns 0 instead of 100
    }

    public MainWindow() {
        InitializeComponent();
    }

    private void button2_Click(object sender, RoutedEventArgs e) {
        Test();
    }
}

[ProtoContract]
class Asset {

    [ProtoMember(1)]
    public int ID { get; set; }

    [ProtoMember(2)]
    public double EAD { get; set; }

    [ProtoMember(3)]
    public float LGD { get; set; }

    [ProtoMember(4)]
    public float PD { get; set; }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

演出会有结束 2024-12-08 14:04:11

想通了。要反序列化,请使用 PrefixBase.Base128 ,显然是默认设置

现在它就像一个魅力!

国杰

        using (var file = File.Create("Data.bin")) {
            Serializer.Serialize<IEnumerable<Asset>>(file, Generate(10));
        }

        using (var file = File.OpenRead("Data.bin")) {
            var ps = Serializer.DeserializeItems<Asset>(file, PrefixStyle.Base128, 1);
            int i = ps.Count(); // got them all back :-)
        }

figured it out. To deserialize use PrefixBase.Base128 wich apparently is the default.

Now it works like a charm!

GJ

        using (var file = File.Create("Data.bin")) {
            Serializer.Serialize<IEnumerable<Asset>>(file, Generate(10));
        }

        using (var file = File.OpenRead("Data.bin")) {
            var ps = Serializer.DeserializeItems<Asset>(file, PrefixStyle.Base128, 1);
            int i = ps.Count(); // got them all back :-)
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文