You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@thrift.apache.org by Leon Mergen <l....@solatis.com> on 2008/10/26 14:01:09 UTC

Recursive data structures

Hello,


I can imagine this being asked before, but I have a little problem: I need to use Thrift to transport recursive (tree-like) data structures. Is this at all possible ? Have any efforts been made to implement this functionality ?

To illustrate the use case, this is an example of what I'm trying to achieve:


struct node {
        1:string id,
        2:map <string, node> children
}


And, ofcourse, Thrift generates the error "Type "node" has not been defined".

Any thoughts ?


Regards,

Leon Mergen

Re: Recursive data structures

Posted by David Reiss <dr...@facebook.com>.
One way of doing it is to use integers as pointers instead of recursive
containment.  For example...

struct Thing {
  1: optional string value;
  2: optional map<string,i32> hash;
}

typedef list<Thing> BunchOfHashes;

struct TopLevel {
  1: BunchOfHashes hashes;
  2: i32 top;
}

Then you build something like...

TopLevel {
  hashes = [
    Thing { value = 'b' },
    Thing { value = 'd' },
    Thing { hash = {'c': 1} },
    Thing { hash = {'a': 0, 'b': 2} },
  ],
  top = 3,
}

Basically, the ints the the "hash" map in "Thing" are indexes into the
"hashes" list.

There is also a patch out for review to add a variant type that would
make something like this pretty easy.

There's been talk of writing support for forward declarations, but AFAIK
no one is working on it.

--David

Hector Yuen wrote:
> hello, I am also interested in that, I have been in the need to transport
> hashes of hashes
> 
> {'a':'b', 'b':{'c':'d'}}
> 
> So far the only thing I have accomplished is to store the hashes as blobs,
> but this cannot be transported between languages
> 
> Is it possible to do something like that?
> 
> -h
> 
> On Sun, Oct 26, 2008 at 6:01 AM, Leon Mergen <l....@solatis.com> wrote:
> 
>> Hello,
>>
>>
>> I can imagine this being asked before, but I have a little problem: I need
>> to use Thrift to transport recursive (tree-like) data structures. Is this at
>> all possible ? Have any efforts been made to implement this functionality ?
>>
>> To illustrate the use case, this is an example of what I'm trying to
>> achieve:
>>
>>
>> struct node {
>>        1:string id,
>>        2:map <string, node> children
>> }
>>
>>
>> And, ofcourse, Thrift generates the error "Type "node" has not been
>> defined".
>>
>> Any thoughts ?
>>
>>
>> Regards,
>>
>> Leon Mergen
>>
> 
> 
> 
> --
> -h

Re: Recursive data structures

Posted by Hector Yuen <hy...@alum.mit.edu>.
hello, I am also interested in that, I have been in the need to transport
hashes of hashes

{'a':'b', 'b':{'c':'d'}}

So far the only thing I have accomplished is to store the hashes as blobs,
but this cannot be transported between languages

Is it possible to do something like that?

-h

On Sun, Oct 26, 2008 at 6:01 AM, Leon Mergen <l....@solatis.com> wrote:

> Hello,
>
>
> I can imagine this being asked before, but I have a little problem: I need
> to use Thrift to transport recursive (tree-like) data structures. Is this at
> all possible ? Have any efforts been made to implement this functionality ?
>
> To illustrate the use case, this is an example of what I'm trying to
> achieve:
>
>
> struct node {
>        1:string id,
>        2:map <string, node> children
> }
>
>
> And, ofcourse, Thrift generates the error "Type "node" has not been
> defined".
>
> Any thoughts ?
>
>
> Regards,
>
> Leon Mergen
>



-- 
-h