Apache thrift,结构包含自身

发布于 2024-09-05 14:27:05 字数 346 浏览 0 评论 0原文

我正在研究数据序列化的节俭。但文档说

循环结构 - 结构只能包含在其之前声明的结构。结构体也不能包含自身

我们的要求之一是

  • 结构体 A
    • 子项目列表
      • 项目(项目是结构 A )

所以阅读要求我不能在任何级别上拥有 Struct 本身?我可以像上面那样将它放在循环模型中吗? Struct 不是 Struct 的直接成员,但它有一些其他成员并且包含 struct。

他们的文档描述性不太好。

Thrift 中可能吗? protobuf支持吗?

I am looking into thrift for serialization of data. But Document says

cyclic structs - Structs can only contain structs that have been declared before it. A struct also cannot contain itself

One of our requirement is

  • Struct A
    • List of Child items
      • Items(Items are Struct A )

So reading requirement i can't have Struct within itself at any level? can i have it in cyclic model as i have it above. Struct is not member of Struct directly but it has some other member and it contains struct.

Their document is not so well descriptive.

Is it possible in Thrift? Does protobuf supports it?

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

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

发布评论

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

评论(2

守望孤独 2024-09-12 14:27:05

根据 此讨论,这在 Thrift 中是不可能的。但是,有一种解决方法,可以使用整数对主列表进行索引。本质上,这是穷人指针的一种形式。

struct A 
{ 
1: list<i32> subitems; 
}

struct AllAs 
{ 
1: list<A> items; 
} 

subitems 本质上是指向 AllAs.items 的指针列表。

在 Protocol Buffers 中,它很简单:

message A {
    repeated A subitems = 1; 
}

According to this discussion, it's not possible in Thrift. However, there is a workaround of using integers to index into a master list. Essentially, this is a form of poor man's pointers.

struct A 
{ 
1: list<i32> subitems; 
}

struct AllAs 
{ 
1: list<A> items; 
} 

subitems is essentially a list of pointers into AllAs.items

In Protocol Buffers, it's trivial:

message A {
    repeated A subitems = 1; 
}
胡渣熟男 2024-09-12 14:27:05

是的,从 Thrift 0.9.2 开始支持此场景

对于任何早期版本的 thrift,以下内容(故意)会导致编译器错误消息:

struct Foo {
  1 : Foo foo     // error - Foo not fully defined yet
  2 : Bar bar     // error - Bar not defined yet
}

struct Bar {
  1 : Foo left     // ok, Foo has been defined earlier
  2 : Foo right    // ok, Foo has been defined earlier
}

但仍然有一些警告。开发人员有责任不产生无限循环或钻石参考,例如

var foo = new Foo();
foo.foo = foo;    // will crash on serialization with stack overflow

var bar = new Bar();
bar.left = foo;
bar.right = foo;   // points to same object only BEFORE deserialization

Yes, starting with Thrift 0.9.2 this scenario is supported.

With any earlier version of thrift the following (intentionally) lead to an compiler error message:

struct Foo {
  1 : Foo foo     // error - Foo not fully defined yet
  2 : Bar bar     // error - Bar not defined yet
}

struct Bar {
  1 : Foo left     // ok, Foo has been defined earlier
  2 : Foo right    // ok, Foo has been defined earlier
}

There are still a few caveats though. The deveoper is responsible for not producing endless loops or diamond references, like

var foo = new Foo();
foo.foo = foo;    // will crash on serialization with stack overflow

var bar = new Bar();
bar.left = foo;
bar.right = foo;   // points to same object only BEFORE deserialization
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文