【问题标题】:Apache thrift, struct contain itselfApache thrift,结构包含自身
【发布时间】:2010-06-10 04:47:19
【问题描述】:

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

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

我们的要求之一是

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

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

他们的文档描述性不是很好。

可以在 Thrift 中使用吗? protobuf 支持吗?

【问题讨论】:

    标签: protocol-buffers thrift thrift-protocol


    【解决方案1】:

    根据this discussion,这在 Thrift 中是不可能的。但是,有一种使用整数索引到主列表的解决方法。本质上,这是一种穷人的指针。

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

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

    在协议缓冲区中,这很简单:

    message A {
        repeated A subitems = 1; 
    }
    

    【讨论】:

      【解决方案2】:

      是的,从 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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多