@@ -19,19 +19,18 @@ limitations under the License.
19
19
package shim
20
20
21
21
import (
22
- "bytes"
23
22
"errors"
24
23
"flag"
25
24
"fmt"
26
25
"io"
27
26
"os"
28
- "strconv"
29
27
"strings"
30
28
31
29
"github.com/golang/protobuf/proto"
32
30
"github.com/golang/protobuf/ptypes/timestamp"
33
31
"github.com/hyperledger/fabric/common/util"
34
32
"github.com/hyperledger/fabric/core/comm"
33
+ coder "github.com/hyperledger/fabric/core/ledger/util"
35
34
pb "github.com/hyperledger/fabric/protos/peer"
36
35
"github.com/op/go-logging"
37
36
"github.com/spf13/viper"
@@ -337,20 +336,41 @@ func (stub *ChaincodeStub) RangeQueryState(startKey, endKey string) (StateRangeQ
337
336
return & StateRangeQueryIterator {stub .handler , stub .TxID , response , 0 }, nil
338
337
}
339
338
340
- //Given a list of attributes, createCompositeKey function combines these attributes
339
+ //Given a list of attributes, CreateCompositeKey function combines these attributes
341
340
//to form a composite key.
342
341
func (stub * ChaincodeStub ) CreateCompositeKey (objectType string , attributes []string ) (string , error ) {
343
342
return createCompositeKey (stub , objectType , attributes )
344
343
}
345
344
346
345
func createCompositeKey (stub ChaincodeStubInterface , objectType string , attributes []string ) (string , error ) {
347
- var compositeKey bytes.Buffer
348
- compositeKey .WriteString (objectType )
346
+ var compositeKey []byte
347
+ compositeKey = append (compositeKey , coder .EncodeOrderPreservingVarUint64 (uint64 (len (objectType )))... )
348
+ compositeKey = append (compositeKey , []byte (objectType )... )
349
349
for _ , attribute := range attributes {
350
- compositeKey . WriteString ( strconv . Itoa ( len (attribute )))
351
- compositeKey . WriteString ( attribute )
350
+ compositeKey = append ( compositeKey , coder . EncodeOrderPreservingVarUint64 ( uint64 ( len (attribute ))) ... )
351
+ compositeKey = append ( compositeKey , [] byte ( attribute ) ... )
352
352
}
353
- return compositeKey .String (), nil
353
+ return string (compositeKey ), nil
354
+ }
355
+
356
+ //Given a composite key, SplitCompositeKey function splits the key into attributes
357
+ //on which the composite key was formed.
358
+ func (stub * ChaincodeStub ) SplitCompositeKey (compositeKey string ) (string , []string , error ) {
359
+ return splitCompositeKey (stub , compositeKey )
360
+ }
361
+
362
+ func splitCompositeKey (stub ChaincodeStubInterface , compositeKey string ) (string , []string , error ) {
363
+ startIndex := 0
364
+ cKey := []byte (compositeKey )
365
+ attributes := []string {}
366
+ for startIndex < len (compositeKey ) {
367
+ len , bytesConsumed := coder .DecodeOrderPreservingVarUint64 (cKey [startIndex :])
368
+ attrBeginIndex := startIndex + int (bytesConsumed )
369
+ attrEndIndex := attrBeginIndex + int (len )
370
+ attributes = append (attributes , compositeKey [attrBeginIndex :attrEndIndex ])
371
+ startIndex = attrEndIndex
372
+ }
373
+ return attributes [0 ], attributes [1 :], nil
354
374
}
355
375
356
376
//PartialCompositeKeyQuery function can be invoked by a chaincode to query the
@@ -365,7 +385,7 @@ func (stub *ChaincodeStub) PartialCompositeKeyQuery(objectType string, attribute
365
385
366
386
func partialCompositeKeyQuery (stub ChaincodeStubInterface , objectType string , attributes []string ) (StateRangeQueryIteratorInterface , error ) {
367
387
partialCompositeKey , _ := stub .CreateCompositeKey (objectType , attributes )
368
- keysIter , err := stub .RangeQueryState (partialCompositeKey + "1" , partialCompositeKey + ": " )
388
+ keysIter , err := stub .RangeQueryState (partialCompositeKey , partialCompositeKey + "\xFF " )
369
389
if err != nil {
370
390
return nil , fmt .Errorf ("Error fetching rows: %s" , err )
371
391
}
0 commit comments