Skip to content
This repository has been archived by the owner on Sep 6, 2022. It is now read-only.

Commit

Permalink
Chaincode Pagination with query[Asset]s. (#131)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kelvin-M authored Dec 7, 2020
1 parent 38b1972 commit f2ba798
Show file tree
Hide file tree
Showing 30 changed files with 928 additions and 490 deletions.
610 changes: 314 additions & 296 deletions EXAMPLES.md

Large diffs are not rendered by default.

25 changes: 18 additions & 7 deletions chaincode/algo.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,21 +93,32 @@ func queryAlgo(db *LedgerDB, args []string) (out outputAlgo, err error) {
}

// queryAlgos returns all algos of the ledger
func queryAlgos(db *LedgerDB, args []string) (outAlgos []outputAlgo, err error) {
func queryAlgos(db *LedgerDB, args []string) (outAlgos []outputAlgo, bookmark string, err error) {
inp := inputBookmark{}
outAlgos = []outputAlgo{}
if len(args) != 0 {
err = errors.BadRequest("incorrect number of arguments, expecting nothing")

if len(args) > 1 {
err = errors.BadRequest("incorrect number of arguments, expecting at most one argument")
return
}
elementsKeys, err := db.GetIndexKeys("algo~owner~key", []string{"algo"})

if len(args) == 1 && args[0] != "" {
err = AssetFromJSON(args, &inp)
if err != nil {
return
}
}

elementsKeys, bookmark, err := db.GetIndexKeysWithPagination("algo~owner~key", []string{"algo"}, OutputPageSize, inp.Bookmark)

if err != nil {
return
}
nb := getLimitedNbSliceElements(elementsKeys)
for _, key := range elementsKeys[:nb] {

for _, key := range elementsKeys {
algo, err := db.GetAlgo(key)
if err != nil {
return outAlgos, err
return outAlgos, bookmark, err
}
var out outputAlgo
out.Fill(algo)
Expand Down
25 changes: 18 additions & 7 deletions chaincode/algo_aggregate.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,21 +93,32 @@ func queryAggregateAlgo(db *LedgerDB, args []string) (out outputAggregateAlgo, e
}

// queryAggregateAlgos returns all algos of the ledger
func queryAggregateAlgos(db *LedgerDB, args []string) (outAlgos []outputAggregateAlgo, err error) {
func queryAggregateAlgos(db *LedgerDB, args []string) (outAlgos []outputAggregateAlgo, bookmark string, err error) {
inp := inputBookmark{}
outAlgos = []outputAggregateAlgo{}
if len(args) != 0 {
err = errors.BadRequest("incorrect number of arguments, expecting nothing")

if len(args) > 1 {
err = errors.BadRequest("incorrect number of arguments, expecting at most one argument")
return
}
elementsKeys, err := db.GetIndexKeys("aggregateAlgo~owner~key", []string{"aggregateAlgo"})

if len(args) == 1 && args[0] != "" {
err = AssetFromJSON(args, &inp)
if err != nil {
return
}
}

elementsKeys, bookmark, err := db.GetIndexKeysWithPagination("aggregateAlgo~owner~key", []string{"aggregateAlgo"}, OutputPageSize, inp.Bookmark)

if err != nil {
return
}
nb := getLimitedNbSliceElements(elementsKeys)
for _, key := range elementsKeys[:nb] {

for _, key := range elementsKeys {
algo, err := db.GetAggregateAlgo(key)
if err != nil {
return outAlgos, err
return outAlgos, bookmark, err
}
var out outputAggregateAlgo
out.Fill(algo)
Expand Down
11 changes: 8 additions & 3 deletions chaincode/algo_aggregate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ import (
"github.com/stretchr/testify/assert"
)

type AggregateAlgoResponse struct {
Results []outputAggregateAlgo `json:"results"`
Bookmark string `json:"bookmark"`
}

func TestAggregateAlgo(t *testing.T) {
scc := new(SubstraChaincode)
mockStub := NewMockStubWithRegisterNode("substra", scc)
Expand Down Expand Up @@ -76,9 +81,9 @@ func TestAggregateAlgo(t *testing.T) {
args = [][]byte{[]byte("queryAggregateAlgos")}
resp = mockStub.MockInvoke(args)
assert.EqualValuesf(t, 200, resp.Status, "when querying aggregate algos - status %d and message %s", resp.Status, resp.Message)
var algos []outputAggregateAlgo
var algos AggregateAlgoResponse
err = json.Unmarshal(resp.Payload, &algos)
assert.NoError(t, err, "while unmarshalling aggregate algos")
assert.Len(t, algos, 1)
assert.Exactly(t, expectedAlgo, algos[0], "return aggregate algo different from registered one")
assert.Len(t, algos.Results, 1)
assert.Exactly(t, expectedAlgo, algos.Results[0], "return aggregate algo different from registered one")
}
25 changes: 18 additions & 7 deletions chaincode/algo_composite.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,21 +93,32 @@ func queryCompositeAlgo(db *LedgerDB, args []string) (out outputCompositeAlgo, e
}

// queryCompositeAlgos returns all algos of the ledger
func queryCompositeAlgos(db *LedgerDB, args []string) (outAlgos []outputCompositeAlgo, err error) {
func queryCompositeAlgos(db *LedgerDB, args []string) (outAlgos []outputCompositeAlgo, bookmark string, err error) {
inp := inputBookmark{}
outAlgos = []outputCompositeAlgo{}
if len(args) != 0 {
err = errors.BadRequest("incorrect number of arguments, expecting nothing")

if len(args) > 1 {
err = errors.BadRequest("incorrect number of arguments, expecting at most one argument")
return
}
elementsKeys, err := db.GetIndexKeys("compositeAlgo~owner~key", []string{"compositeAlgo"})

if len(args) == 1 && args[0] != "" {
err = AssetFromJSON(args, &inp)
if err != nil {
return
}
}

elementsKeys, bookmark, err := db.GetIndexKeysWithPagination("compositeAlgo~owner~key", []string{"compositeAlgo"}, OutputPageSize, inp.Bookmark)

if err != nil {
return
}
nb := getLimitedNbSliceElements(elementsKeys)
for _, key := range elementsKeys[:nb] {

for _, key := range elementsKeys {
algo, err := db.GetCompositeAlgo(key)
if err != nil {
return outAlgos, err
return outAlgos, bookmark, err
}
var out outputCompositeAlgo
out.Fill(algo)
Expand Down
11 changes: 8 additions & 3 deletions chaincode/algo_composite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ import (
"github.com/stretchr/testify/assert"
)

type CompositeAlgoResponse struct {
Results []outputCompositeAlgo `json:"results"`
Author map[string]string `json:"bookmarks"`
}

func TestCompositeAlgo(t *testing.T) {
scc := new(SubstraChaincode)
mockStub := NewMockStubWithRegisterNode("substra", scc)
Expand Down Expand Up @@ -76,9 +81,9 @@ func TestCompositeAlgo(t *testing.T) {
args = [][]byte{[]byte("queryCompositeAlgos")}
resp = mockStub.MockInvoke(args)
assert.EqualValuesf(t, 200, resp.Status, "when querying composite algos - status %d and message %s", resp.Status, resp.Message)
var algos []outputCompositeAlgo
var algos CompositeAlgoResponse
err = json.Unmarshal(resp.Payload, &algos)
assert.NoError(t, err, "while unmarshalling composite algos")
assert.Len(t, algos, 1)
assert.Exactly(t, expectedAlgo, algos[0], "return composite algo different from registered one")
assert.Len(t, algos.Results, 1)
assert.Exactly(t, expectedAlgo, algos.Results[0], "return composite algo different from registered one")
}
11 changes: 8 additions & 3 deletions chaincode/algo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ import (
"github.com/stretchr/testify/assert"
)

type AlgoResponse struct {
Results []outputAlgo `json:"results"`
Bookmark string `json:"bookmark"`
}

func TestAlgo(t *testing.T) {
scc := new(SubstraChaincode)
mockStub := NewMockStubWithRegisterNode("substra", scc)
Expand Down Expand Up @@ -71,9 +76,9 @@ func TestAlgo(t *testing.T) {
args = [][]byte{[]byte("queryAlgos")}
resp = mockStub.MockInvoke(args)
assert.EqualValuesf(t, 200, resp.Status, "when querying algos - status %d and message %s", resp.Status, resp.Message)
var algos []outputAlgo
var algos AlgoResponse
err = json.Unmarshal(resp.Payload, &algos)
assert.NoError(t, err, "while unmarshalling algos")
assert.Len(t, algos, 1)
assert.Exactly(t, expectedAlgo, algos[0], "return algo different from registered one")
assert.Len(t, algos.Results, 1)
assert.Exactly(t, expectedAlgo, algos.Results[0], "return algo different from registered one")
}
26 changes: 21 additions & 5 deletions chaincode/compute_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,21 +261,37 @@ func queryComputePlan(db *LedgerDB, args []string) (resp outputComputePlan, err
return getOutComputePlan(db, inp.Key)
}

func queryComputePlans(db *LedgerDB, args []string) (resp []outputComputePlan, err error) {
resp = []outputComputePlan{}
computePlanKeys, err := db.GetIndexKeys("computePlan~key", []string{"computePlan"})
func queryComputePlans(db *LedgerDB, args []string) (outComputePlans []outputComputePlan, bookmark string, err error) {
inp := inputBookmark{}
outComputePlans = []outputComputePlan{}

if len(args) > 1 {
err = errors.BadRequest("incorrect number of arguments, expecting at most one argument")
return
}

if len(args) == 1 && args[0] != "" {
err = AssetFromJSON(args, &inp)
if err != nil {
return
}
}

computePlanKeys, bookmark, err := db.GetIndexKeysWithPagination("computePlan~key", []string{"computePlan"}, OutputPageSize, inp.Bookmark)

if err != nil {
return
}

for _, key := range computePlanKeys {
var computePlan outputComputePlan
computePlan, err = getOutComputePlan(db, key)
if err != nil {
return
}
resp = append(resp, computePlan)
outComputePlans = append(outComputePlans, computePlan)
}
return resp, err
return
}

// getComputePlan returns details for a compute plan key.
Expand Down
22 changes: 11 additions & 11 deletions chaincode/compute_plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,14 +309,14 @@ func TestCreateComputePlanCompositeAggregate(t *testing.T) {
assert.NoError(t, err)

// Check the composite traintuples
traintuples, err := queryCompositeTraintuples(db, []string{})
traintuples, _, err := queryCompositeTraintuples(db, []string{})
assert.NoError(t, err)
require.Len(t, traintuples, 2)
require.Contains(t, outCP.CompositeTraintupleKeys, traintuples[0].Key)
require.Contains(t, outCP.CompositeTraintupleKeys, traintuples[1].Key)

// Check the aggregate traintuples
aggtuples, err := queryAggregatetuples(db, []string{})
aggtuples, _, err := queryAggregatetuples(db, []string{})
assert.NoError(t, err)
require.Len(t, aggtuples, 2)
require.Contains(t, outCP.AggregatetupleKeys, aggtuples[0].Key)
Expand All @@ -330,7 +330,7 @@ func TestCreateComputePlanCompositeAggregate(t *testing.T) {
assert.Equal(t, 2, len(cp.AggregatetupleKeys))

// Query compute plans
cps, err := queryComputePlans(db, []string{})
cps, _, err := queryComputePlans(db, []string{})
assert.NoError(t, err, "calling queryComputePlans should succeed")
assert.Len(t, cps, 1, "queryComputePlans should return one compute plan")
assert.Equal(t, 2, len(cps[0].CompositeTraintupleKeys))
Expand All @@ -352,7 +352,7 @@ func TestCreateComputePlan(t *testing.T) {
validateDefaultComputePlan(t, outCP)

// Check the traintuples
traintuples, err := queryTraintuples(db, []string{})
traintuples, _, err := queryTraintuples(db, []string{})
assert.NoError(t, err)
assert.Len(t, traintuples, 2)
require.Contains(t, outCP.TraintupleKeys, traintuples[0].Key)
Expand Down Expand Up @@ -387,7 +387,7 @@ func TestCreateComputePlan(t *testing.T) {
assert.Equal(t, StatusWaiting, second.Status)

// Check the testtuples
testtuples, err := queryTesttuples(db, []string{})
testtuples, _, err := queryTesttuples(db, []string{})
assert.NoError(t, err)
require.Len(t, testtuples, 1)
testtuple := testtuples[0]
Expand Down Expand Up @@ -430,7 +430,7 @@ func TestQueryComputePlans(t *testing.T) {
assert.NoError(t, err)
assert.NotNil(t, outCP)

cps, err := queryComputePlans(db, []string{})
cps, _, err := queryComputePlans(db, []string{})
assert.NoError(t, err, "calling queryComputePlans should succeed")
assert.Len(t, cps, 1, "queryComputePlans should return one compute plan")
validateDefaultComputePlan(t, cps[0])
Expand Down Expand Up @@ -487,7 +487,7 @@ func TestComputePlanEmptyTesttuples(t *testing.T) {
assert.NotNil(t, cp)
assert.Len(t, outCP.TesttupleKeys, 0)

cps, err := queryComputePlans(db, []string{})
cps, _, err := queryComputePlans(db, []string{})
assert.NoError(t, err, "calling queryComputePlans should succeed")
assert.Len(t, cps, 1, "queryComputePlans should return one compute plan")
assert.Len(t, cps[0].TesttupleKeys, 0)
Expand All @@ -501,7 +501,7 @@ func TestQueryComputePlanEmpty(t *testing.T) {
mockStub.MockTransactionStart("42")
db := NewLedgerDB(mockStub)

cps, err := queryComputePlans(db, []string{})
cps, _, err := queryComputePlans(db, []string{})
assert.NoError(t, err, "calling queryComputePlans should succeed")
assert.Equal(t, []outputComputePlan{}, cps)
}
Expand All @@ -525,7 +525,7 @@ func TestCancelComputePlan(t *testing.T) {
computePlan, err := getOutComputePlan(db, out.Key)
assert.Equal(t, StatusCanceled, computePlan.Status)

tuples, err := queryCompositeTraintuples(db, []string{})
tuples, _, err := queryCompositeTraintuples(db, []string{})
assert.NoError(t, err)

nbAborted, nbTodo := 0, 0
Expand All @@ -541,7 +541,7 @@ func TestCancelComputePlan(t *testing.T) {
assert.Equal(t, nbAborted, 2)
assert.Equal(t, nbTodo, 2)

tests, err := queryTesttuples(db, []string{})
tests, _, err := queryTesttuples(db, []string{})
assert.NoError(t, err)
for _, test := range tests {
assert.Equal(t, StatusAborted, test.Status)
Expand Down Expand Up @@ -569,7 +569,7 @@ func TestStartedTuplesOfCanceledComputePlan(t *testing.T) {
computePlan, err := getOutComputePlan(db, out.Key)
assert.Equal(t, StatusCanceled, computePlan.Status)

tuples, err := queryCompositeTraintuples(db, []string{})
tuples, _, err := queryCompositeTraintuples(db, []string{})
assert.NoError(t, err)
for _, tuple := range tuples {
if tuple.Rank == 0 {
Expand Down
Loading

0 comments on commit f2ba798

Please sign in to comment.