Working with thrift structures
While developing thrudb I’ve been using thrift a lot and have a couple tricks to share.
First trick is how to do simple reflection. Thrift lets you do things like serialize a structure to a binary string and store it on disk. The problem is thrift doesn’t store the structure’s definition along with it since this information would bloat the message and frankly goes against the design of thrift, which allows loose structure definitions (see section 4 of the thrift whitepaper)
To get around this we need to encode the type of structure we have as a field in the struct itself.
Lets start with an example: Say I want to store a mixed list of Email and RSS articles in a file for backup purposes or better yet in thrudb.
Heres our thrift definition file:
#this is a thrift definition
enum ObjectType {
UNKNOWN = 0,
EMAIL = 1,
RSS _ARTICLE = 2
}
struct SimpleObject {
100:ObjectType type = UNKNOWN
}
struct Email {
1:string subject,
2:string to_address,
3:string from_address,
4:i32 date,
5:string body,
100: ObjectType type=EMAIL
}
struct RssArticle {
1:string uri,
2:string title,
3:string body,
4:i32 date,
100:ObjectType type=RSS_ARTICLE
}
So what we did here is set the 100th parameter to be the struct type, then assigned it an default enumeration key from the list of possible types so when a struct is instantiated its type is automatically set. This information is included when a struct is serialized to disk, so when we read the message back we can use our DUMMY stuct “SimpleObject” to check it’s type. The SimpleObject stuct will ignore all the other fields in the message, only loading the 100th param (enum key). Now we know which structure to allocate.
Heres a pseudo example of this in action:
$serialized_object = get_random_serialized_object();
$type_obj = new SimpleObject( $serialized_object );
switch($type_obj->type){
case EMAIL:
return new Email( $serialized_object );
case RSS_ARTICLE:
return new RssArticle( $serialized_object );
default:
print "Unknown type!";
};
}
Writen by




January 6th, 2008 at 12:10 am
[…] Thrudb has gotten a lot of attention recently (thanks Ilya). […]