state databese 检索方法介绍
1,根据key范围进行检索
GetStateByRange(startKey, endKey string) (StateQueryIteratorInterface, error)
例如
startKey:ob01
endKey :ob03
以下的结构会返回
[
{ "key":"ob01"
"Record":{"name":"tom","old":"20"}
},
{
"key":"ob02"
"Record":{......}
},
{
"key":"ob03"
"Record":{......}
}
]
2,根据混合key检索
GetStateByPartialCompositeKey("ob01",[]string{"li","男"})
{
"key":"ob01"
"Record":{"name":"li","old":"10","sex":"男"}
},
{
"key":"ob01"
"Record":{"name":"li","old":"90","sex":"男"}
},
{
"key":"ob01"
"Record":{"name":"li","old":"10","sex":"女"}
}
]
1,根据key范围进行检索
GetStateByRange(startKey, endKey string) (StateQueryIteratorInterface, error)
例如
startKey:ob01
endKey :ob03
以下的结构会返回
[
{ "key":"ob01"
"Record":{"name":"tom","old":"20"}
},
{
"key":"ob02"
"Record":{......}
},
{
"key":"ob03"
"Record":{......}
}
]
2,根据混合key检索
前提:在stateDB存的是下面这样的结构体,而不是example02那样简单的结构
其中name+old+sex最为复合key
参照:marbles_chaincode.goGetStateByPartialCompositeKey("ob01",[]string{"li","男"})
取出两条记录
[{
"key":"ob01"
"Record":{"name":"li","old":"10","sex":"男"}
},
{
"key":"ob01"
"Record":{"name":"li","old":"90","sex":"男"}
},
{
"key":"ob01"
"Record":{"name":"li","old":"10","sex":"女"}
}
]
------------------------------------------------------------------------------------------------------------------------
3,关于存储结构体
参照:marbles_chaincode.go
第一步(存储结构体):
PutState("结构体名称", json形式的bytes)
第二步(给结构体构建索引):
key = CreateCompositeKey("上面结构体对应的索引名称",复合key)
第三步(存储创建好的索引)
PutState(key, []byte{0x00})