dqr6434
dqr6434
2014-04-20 09:40

以结构值作为键的节俭地图支持

已采纳

I experimented golang generation with Thrift 0.9.1, for example,

thrift definition,

struct AppIdLeveledHashKeyTimeKeyHour {
    1: required i32 appId
    2: required LeveledHashKey leveledHashKey
    3: required TimeKeyHour timeKeyHour
}
typedef map<AppIdLeveledHashKeyTimeKeyHour, ...sth else...> EventSliceShardIdValue

in the generated code, EventSliceShardIdValue would be,

type EventSliceShardIdValue map[*AppIdLeveledHashKeyTimeKeyHour]EventSliceAppIdLeveledHashKeyTimeKeyHourValue

you can find the key part is a pointer which represents memory address. In golang a pointer as map key (instead of a value, or hash of the obj) is useless in most cases. To use a combination of some fields as map key, the definition should use a value type like

map[AppIdLeveledHashKeyTimeKeyHour]EventSliceAppIdLeveledHashKeyTimeKeyHourValue

Is it a problem of Thrift's go support (or I misused sth)? Any workaround to solve this problem in thrift?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • duanbiaojin8860 duanbiaojin8860 7年前

    Structs (without pointers) can only be used as map keys under certain limited circumstances (they must be comparable per http://golang.org/ref/spec#Comparison_operators); it's possible that AppIdLeveledHashKeyTimeKeyHour doesn't fit this definition, so it's not actually possible to build a map without using a pointer for the key.

    点赞 评论 复制链接分享