From 9ebfb3415ec17aba0bd1025f4c84dae5f52c83ad Mon Sep 17 00:00:00 2001 From: Vadim Inshakov Date: Wed, 24 Jun 2020 15:39:10 +0300 Subject: [PATCH] Compkeys mgmt (#53) * Fix CI (#47) * remove log msg Signed-off-by: vadiminshakov * trigger CI on push && PR to 1.x, 2.x branches Signed-off-by: vadiminshakov * new example request Signed-off-by: vadiminshakov * handle nil and null block values Signed-off-by: vadiminshakov * add test case for querying all db entries Signed-off-by: vadiminshakov * del Explore, add GetRange rpc Signed-off-by: vadiminshakov * increment go version Signed-off-by: vadiminshakov * add handler for GerRange rpc, handle case of querying all Signed-off-by: vadiminshakov * vendor chaincode before integration tests Signed-off-by: vadiminshakov * refactored API and fixes (#51) * ignore case Signed-off-by: vadiminshakov * fix example Signed-off-by: vadiminshakov * fix test case Signed-off-by: vadiminshakov * fix REST API test Signed-off-by: vadiminshakov * Fix CI (#47) * remove log msg Signed-off-by: vadiminshakov * trigger CI on push && PR to 1.x, 2.x branches Signed-off-by: vadiminshakov * new example request Signed-off-by: vadiminshakov * handle nil and null block values Signed-off-by: vadiminshakov * add test case for querying all db entries Signed-off-by: vadiminshakov * del Explore, add GetRange rpc Signed-off-by: vadiminshakov * increment go version Signed-off-by: vadiminshakov * add handler for GerRange rpc, handle case of querying all Signed-off-by: vadiminshakov * vendor chaincode before integration tests Signed-off-by: vadiminshakov * ignore case Signed-off-by: vadiminshakov * fix example Signed-off-by: vadiminshakov * fix test case Signed-off-by: vadiminshakov * fix REST API test Signed-off-by: vadiminshakov * add test case for querying all db entries Signed-off-by: vadiminshakov * no need to extract part of the key from the composite key Signed-off-by: vadiminshakov * add CreateCompositeKey helper func Signed-off-by: vadiminshakov * add example of constructing composite key for quering txs with composite keys, add more comments Signed-off-by: vadiminshakov * add example of constructing composite key for quering txs with composite keys, add more comments Signed-off-by: vadiminshakov --- client/example/client.go | 23 +++++++++++++++++++---- db/cassandra.go | 10 ++-------- helpers/helpers.go | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 12 deletions(-) diff --git a/client/example/client.go b/client/example/client.go index 5f29d15..6c171ad 100644 --- a/client/example/client.go +++ b/client/example/client.go @@ -20,7 +20,6 @@ import ( "fmt" fabcli "github.com/hyperledger-labs/fabex/client" "github.com/hyperledger-labs/fabex/helpers" - pb "github.com/hyperledger-labs/fabex/proto" "log" ) @@ -41,11 +40,27 @@ func main() { Use this commented lines for your experiments! */ + // get txs from blocks with block number range //txs, err := client.GetRange(1, 15) + + // get tx with tx ID //txs, err := client.Get(&pb.Entry{Txid:"3a3e933a3d9953b0b10e6573254b6d3cf2347d72058c0347a55054babdd8e1a1"}) - //txs, err := client.Get(&pb.Entry{Payload: "car0"}) - //txs, err := client.Get(nil) - txs, err := client.Get(&pb.Entry{Blocknum: 5}) + + // get tx with payload key X + //txs, err := client.Get(&pb.Entry{Payload: "X"}) + + // get txs from specific block + //txs, err := client.Get(&pb.Entry{Blocknum: 5}) + + // get entry with composite key + //key, err := helpers.CreateCompositeKey("RAIL", []string{"1"}) + //if err != nil { + // log.Fatal(err) + //} + //txs, err := client.Get(&pb.Entry{Payload: key}) + + // get all + txs, err := client.Get(nil) if err != nil { log.Fatal(err) } diff --git a/db/cassandra.go b/db/cassandra.go index 181f7ae..6d985c5 100644 --- a/db/cassandra.go +++ b/db/cassandra.go @@ -1,7 +1,6 @@ package db import ( - "bytes" "encoding/json" "fmt" "github.com/gocql/gocql" @@ -97,15 +96,10 @@ func (c *Cassandra) Insert(tx Tx) error { return err } - // extract metadata from RWSet and insert to column (clear from separators) + // extract keys from RWSet var payloadkeys []string for _, kv := range Payload { - if bytes.Index([]byte(kv.Key), nsKeySep) != -1 { - split := bytes.SplitN([]byte(kv.Key), nsKeySep, -1) - payloadkeys = append(payloadkeys, string(split[2][0:])) - } else { - payloadkeys = append(payloadkeys, kv.Key) - } + payloadkeys = append(payloadkeys, kv.Key) } id := gocql.TimeUUID() diff --git a/helpers/helpers.go b/helpers/helpers.go index 3a5ab1e..07a112d 100644 --- a/helpers/helpers.go +++ b/helpers/helpers.go @@ -19,6 +19,7 @@ package helpers import ( "encoding/hex" "encoding/json" + "fmt" "github.com/hyperledger-labs/fabex/blockfetcher" "github.com/hyperledger-labs/fabex/db" "github.com/hyperledger-labs/fabex/models" @@ -29,6 +30,7 @@ import ( "github.com/hyperledger/fabric-sdk-go/pkg/fabsdk" "github.com/pkg/errors" log "github.com/sirupsen/logrus" + "unicode/utf8" ) const NOT_FOUND_ERR = "not found" @@ -173,3 +175,36 @@ func PackTxsToBlocks(blocks []db.Tx) ([]models.Block, error) { return Blocks, nil } + +const ( + minUnicodeRuneValue = 0 //U+0000 + maxUnicodeRuneValue = utf8.MaxRune //U+10FFFF - maximum (and unallocated) code point + compositeKeyNamespace = "\x00" +) + +func CreateCompositeKey(objectType string, attributes []string) (string, error) { + if err := validateCompositeKeyAttribute(objectType); err != nil { + return "", err + } + ck := compositeKeyNamespace + objectType + string(minUnicodeRuneValue) + for _, att := range attributes { + if err := validateCompositeKeyAttribute(att); err != nil { + return "", err + } + ck += att + string(minUnicodeRuneValue) + } + return ck, nil +} + +func validateCompositeKeyAttribute(str string) error { + if !utf8.ValidString(str) { + return fmt.Errorf("not a valid utf8 string: [%x]", str) + } + for index, runeValue := range str { + if runeValue == minUnicodeRuneValue || runeValue == maxUnicodeRuneValue { + return fmt.Errorf(`input contains unicode %#U starting at position [%d]. %#U and %#U are not allowed in the input attribute of a composite key`, + runeValue, index, minUnicodeRuneValue, maxUnicodeRuneValue) + } + } + return nil +}