Skip to content
This repository has been archived by the owner on Mar 5, 2023. It is now read-only.

Commit

Permalink
Compkeys mgmt (#53)
Browse files Browse the repository at this point in the history
* Fix CI (#47)

* remove log msg

Signed-off-by: vadiminshakov <[email protected]>

* trigger CI on push && PR to 1.x, 2.x branches

Signed-off-by: vadiminshakov <[email protected]>

* new example request

Signed-off-by: vadiminshakov <[email protected]>

* handle nil and null block values

Signed-off-by: vadiminshakov <[email protected]>

* add test case for querying all db entries

Signed-off-by: vadiminshakov <[email protected]>

* del Explore, add GetRange rpc

Signed-off-by: vadiminshakov <[email protected]>

* increment go version

Signed-off-by: vadiminshakov <[email protected]>

* add handler for GerRange rpc, handle case of querying all

Signed-off-by: vadiminshakov <[email protected]>

* vendor chaincode before integration tests

Signed-off-by: vadiminshakov <[email protected]>

* refactored API and fixes (#51)

* ignore case

Signed-off-by: vadiminshakov <[email protected]>

* fix example

Signed-off-by: vadiminshakov <[email protected]>

* fix test case

Signed-off-by: vadiminshakov <[email protected]>

* fix REST API test

Signed-off-by: vadiminshakov <[email protected]>

* Fix CI (#47)

* remove log msg

Signed-off-by: vadiminshakov <[email protected]>

* trigger CI on push && PR to 1.x, 2.x branches

Signed-off-by: vadiminshakov <[email protected]>

* new example request

Signed-off-by: vadiminshakov <[email protected]>

* handle nil and null block values

Signed-off-by: vadiminshakov <[email protected]>

* add test case for querying all db entries

Signed-off-by: vadiminshakov <[email protected]>

* del Explore, add GetRange rpc

Signed-off-by: vadiminshakov <[email protected]>

* increment go version

Signed-off-by: vadiminshakov <[email protected]>

* add handler for GerRange rpc, handle case of querying all

Signed-off-by: vadiminshakov <[email protected]>

* vendor chaincode before integration tests

Signed-off-by: vadiminshakov <[email protected]>

* ignore case

Signed-off-by: vadiminshakov <[email protected]>

* fix example

Signed-off-by: vadiminshakov <[email protected]>

* fix test case

Signed-off-by: vadiminshakov <[email protected]>

* fix REST API test

Signed-off-by: vadiminshakov <[email protected]>

* add test case for querying all db entries

Signed-off-by: vadiminshakov <[email protected]>

* no need to extract part of the key from the composite key

Signed-off-by: vadiminshakov <[email protected]>

* add CreateCompositeKey helper func

Signed-off-by: vadiminshakov <[email protected]>

* add example of constructing composite key for quering txs with composite keys, add more comments

Signed-off-by: vadiminshakov <[email protected]>

* add example of constructing composite key for quering txs with composite keys, add more comments

Signed-off-by: vadiminshakov <[email protected]>
  • Loading branch information
vadiminshakov committed Jun 24, 2020
1 parent 66b27f3 commit 9ebfb34
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 12 deletions.
23 changes: 19 additions & 4 deletions client/example/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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)
}
Expand Down
10 changes: 2 additions & 8 deletions db/cassandra.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package db

import (
"bytes"
"encoding/json"
"fmt"
"github.com/gocql/gocql"
Expand Down Expand Up @@ -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()
Expand Down
35 changes: 35 additions & 0 deletions helpers/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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
}

0 comments on commit 9ebfb34

Please sign in to comment.