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

Commit f2ba798

Browse files
authored
Chaincode Pagination with query[Asset]s. (#131)
1 parent 38b1972 commit f2ba798

30 files changed

+928
-490
lines changed

EXAMPLES.md

Lines changed: 314 additions & 296 deletions
Large diffs are not rendered by default.

chaincode/algo.go

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -93,21 +93,32 @@ func queryAlgo(db *LedgerDB, args []string) (out outputAlgo, err error) {
9393
}
9494

9595
// queryAlgos returns all algos of the ledger
96-
func queryAlgos(db *LedgerDB, args []string) (outAlgos []outputAlgo, err error) {
96+
func queryAlgos(db *LedgerDB, args []string) (outAlgos []outputAlgo, bookmark string, err error) {
97+
inp := inputBookmark{}
9798
outAlgos = []outputAlgo{}
98-
if len(args) != 0 {
99-
err = errors.BadRequest("incorrect number of arguments, expecting nothing")
99+
100+
if len(args) > 1 {
101+
err = errors.BadRequest("incorrect number of arguments, expecting at most one argument")
100102
return
101103
}
102-
elementsKeys, err := db.GetIndexKeys("algo~owner~key", []string{"algo"})
104+
105+
if len(args) == 1 && args[0] != "" {
106+
err = AssetFromJSON(args, &inp)
107+
if err != nil {
108+
return
109+
}
110+
}
111+
112+
elementsKeys, bookmark, err := db.GetIndexKeysWithPagination("algo~owner~key", []string{"algo"}, OutputPageSize, inp.Bookmark)
113+
103114
if err != nil {
104115
return
105116
}
106-
nb := getLimitedNbSliceElements(elementsKeys)
107-
for _, key := range elementsKeys[:nb] {
117+
118+
for _, key := range elementsKeys {
108119
algo, err := db.GetAlgo(key)
109120
if err != nil {
110-
return outAlgos, err
121+
return outAlgos, bookmark, err
111122
}
112123
var out outputAlgo
113124
out.Fill(algo)

chaincode/algo_aggregate.go

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -93,21 +93,32 @@ func queryAggregateAlgo(db *LedgerDB, args []string) (out outputAggregateAlgo, e
9393
}
9494

9595
// queryAggregateAlgos returns all algos of the ledger
96-
func queryAggregateAlgos(db *LedgerDB, args []string) (outAlgos []outputAggregateAlgo, err error) {
96+
func queryAggregateAlgos(db *LedgerDB, args []string) (outAlgos []outputAggregateAlgo, bookmark string, err error) {
97+
inp := inputBookmark{}
9798
outAlgos = []outputAggregateAlgo{}
98-
if len(args) != 0 {
99-
err = errors.BadRequest("incorrect number of arguments, expecting nothing")
99+
100+
if len(args) > 1 {
101+
err = errors.BadRequest("incorrect number of arguments, expecting at most one argument")
100102
return
101103
}
102-
elementsKeys, err := db.GetIndexKeys("aggregateAlgo~owner~key", []string{"aggregateAlgo"})
104+
105+
if len(args) == 1 && args[0] != "" {
106+
err = AssetFromJSON(args, &inp)
107+
if err != nil {
108+
return
109+
}
110+
}
111+
112+
elementsKeys, bookmark, err := db.GetIndexKeysWithPagination("aggregateAlgo~owner~key", []string{"aggregateAlgo"}, OutputPageSize, inp.Bookmark)
113+
103114
if err != nil {
104115
return
105116
}
106-
nb := getLimitedNbSliceElements(elementsKeys)
107-
for _, key := range elementsKeys[:nb] {
117+
118+
for _, key := range elementsKeys {
108119
algo, err := db.GetAggregateAlgo(key)
109120
if err != nil {
110-
return outAlgos, err
121+
return outAlgos, bookmark, err
111122
}
112123
var out outputAggregateAlgo
113124
out.Fill(algo)

chaincode/algo_aggregate_test.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ import (
2121
"github.com/stretchr/testify/assert"
2222
)
2323

24+
type AggregateAlgoResponse struct {
25+
Results []outputAggregateAlgo `json:"results"`
26+
Bookmark string `json:"bookmark"`
27+
}
28+
2429
func TestAggregateAlgo(t *testing.T) {
2530
scc := new(SubstraChaincode)
2631
mockStub := NewMockStubWithRegisterNode("substra", scc)
@@ -76,9 +81,9 @@ func TestAggregateAlgo(t *testing.T) {
7681
args = [][]byte{[]byte("queryAggregateAlgos")}
7782
resp = mockStub.MockInvoke(args)
7883
assert.EqualValuesf(t, 200, resp.Status, "when querying aggregate algos - status %d and message %s", resp.Status, resp.Message)
79-
var algos []outputAggregateAlgo
84+
var algos AggregateAlgoResponse
8085
err = json.Unmarshal(resp.Payload, &algos)
8186
assert.NoError(t, err, "while unmarshalling aggregate algos")
82-
assert.Len(t, algos, 1)
83-
assert.Exactly(t, expectedAlgo, algos[0], "return aggregate algo different from registered one")
87+
assert.Len(t, algos.Results, 1)
88+
assert.Exactly(t, expectedAlgo, algos.Results[0], "return aggregate algo different from registered one")
8489
}

chaincode/algo_composite.go

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -93,21 +93,32 @@ func queryCompositeAlgo(db *LedgerDB, args []string) (out outputCompositeAlgo, e
9393
}
9494

9595
// queryCompositeAlgos returns all algos of the ledger
96-
func queryCompositeAlgos(db *LedgerDB, args []string) (outAlgos []outputCompositeAlgo, err error) {
96+
func queryCompositeAlgos(db *LedgerDB, args []string) (outAlgos []outputCompositeAlgo, bookmark string, err error) {
97+
inp := inputBookmark{}
9798
outAlgos = []outputCompositeAlgo{}
98-
if len(args) != 0 {
99-
err = errors.BadRequest("incorrect number of arguments, expecting nothing")
99+
100+
if len(args) > 1 {
101+
err = errors.BadRequest("incorrect number of arguments, expecting at most one argument")
100102
return
101103
}
102-
elementsKeys, err := db.GetIndexKeys("compositeAlgo~owner~key", []string{"compositeAlgo"})
104+
105+
if len(args) == 1 && args[0] != "" {
106+
err = AssetFromJSON(args, &inp)
107+
if err != nil {
108+
return
109+
}
110+
}
111+
112+
elementsKeys, bookmark, err := db.GetIndexKeysWithPagination("compositeAlgo~owner~key", []string{"compositeAlgo"}, OutputPageSize, inp.Bookmark)
113+
103114
if err != nil {
104115
return
105116
}
106-
nb := getLimitedNbSliceElements(elementsKeys)
107-
for _, key := range elementsKeys[:nb] {
117+
118+
for _, key := range elementsKeys {
108119
algo, err := db.GetCompositeAlgo(key)
109120
if err != nil {
110-
return outAlgos, err
121+
return outAlgos, bookmark, err
111122
}
112123
var out outputCompositeAlgo
113124
out.Fill(algo)

chaincode/algo_composite_test.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ import (
2121
"github.com/stretchr/testify/assert"
2222
)
2323

24+
type CompositeAlgoResponse struct {
25+
Results []outputCompositeAlgo `json:"results"`
26+
Author map[string]string `json:"bookmarks"`
27+
}
28+
2429
func TestCompositeAlgo(t *testing.T) {
2530
scc := new(SubstraChaincode)
2631
mockStub := NewMockStubWithRegisterNode("substra", scc)
@@ -76,9 +81,9 @@ func TestCompositeAlgo(t *testing.T) {
7681
args = [][]byte{[]byte("queryCompositeAlgos")}
7782
resp = mockStub.MockInvoke(args)
7883
assert.EqualValuesf(t, 200, resp.Status, "when querying composite algos - status %d and message %s", resp.Status, resp.Message)
79-
var algos []outputCompositeAlgo
84+
var algos CompositeAlgoResponse
8085
err = json.Unmarshal(resp.Payload, &algos)
8186
assert.NoError(t, err, "while unmarshalling composite algos")
82-
assert.Len(t, algos, 1)
83-
assert.Exactly(t, expectedAlgo, algos[0], "return composite algo different from registered one")
87+
assert.Len(t, algos.Results, 1)
88+
assert.Exactly(t, expectedAlgo, algos.Results[0], "return composite algo different from registered one")
8489
}

chaincode/algo_test.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ import (
2121
"github.com/stretchr/testify/assert"
2222
)
2323

24+
type AlgoResponse struct {
25+
Results []outputAlgo `json:"results"`
26+
Bookmark string `json:"bookmark"`
27+
}
28+
2429
func TestAlgo(t *testing.T) {
2530
scc := new(SubstraChaincode)
2631
mockStub := NewMockStubWithRegisterNode("substra", scc)
@@ -71,9 +76,9 @@ func TestAlgo(t *testing.T) {
7176
args = [][]byte{[]byte("queryAlgos")}
7277
resp = mockStub.MockInvoke(args)
7378
assert.EqualValuesf(t, 200, resp.Status, "when querying algos - status %d and message %s", resp.Status, resp.Message)
74-
var algos []outputAlgo
79+
var algos AlgoResponse
7580
err = json.Unmarshal(resp.Payload, &algos)
7681
assert.NoError(t, err, "while unmarshalling algos")
77-
assert.Len(t, algos, 1)
78-
assert.Exactly(t, expectedAlgo, algos[0], "return algo different from registered one")
82+
assert.Len(t, algos.Results, 1)
83+
assert.Exactly(t, expectedAlgo, algos.Results[0], "return algo different from registered one")
7984
}

chaincode/compute_plan.go

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -261,21 +261,37 @@ func queryComputePlan(db *LedgerDB, args []string) (resp outputComputePlan, err
261261
return getOutComputePlan(db, inp.Key)
262262
}
263263

264-
func queryComputePlans(db *LedgerDB, args []string) (resp []outputComputePlan, err error) {
265-
resp = []outputComputePlan{}
266-
computePlanKeys, err := db.GetIndexKeys("computePlan~key", []string{"computePlan"})
264+
func queryComputePlans(db *LedgerDB, args []string) (outComputePlans []outputComputePlan, bookmark string, err error) {
265+
inp := inputBookmark{}
266+
outComputePlans = []outputComputePlan{}
267+
268+
if len(args) > 1 {
269+
err = errors.BadRequest("incorrect number of arguments, expecting at most one argument")
270+
return
271+
}
272+
273+
if len(args) == 1 && args[0] != "" {
274+
err = AssetFromJSON(args, &inp)
275+
if err != nil {
276+
return
277+
}
278+
}
279+
280+
computePlanKeys, bookmark, err := db.GetIndexKeysWithPagination("computePlan~key", []string{"computePlan"}, OutputPageSize, inp.Bookmark)
281+
267282
if err != nil {
268283
return
269284
}
285+
270286
for _, key := range computePlanKeys {
271287
var computePlan outputComputePlan
272288
computePlan, err = getOutComputePlan(db, key)
273289
if err != nil {
274290
return
275291
}
276-
resp = append(resp, computePlan)
292+
outComputePlans = append(outComputePlans, computePlan)
277293
}
278-
return resp, err
294+
return
279295
}
280296

281297
// getComputePlan returns details for a compute plan key.

chaincode/compute_plan_test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -309,14 +309,14 @@ func TestCreateComputePlanCompositeAggregate(t *testing.T) {
309309
assert.NoError(t, err)
310310

311311
// Check the composite traintuples
312-
traintuples, err := queryCompositeTraintuples(db, []string{})
312+
traintuples, _, err := queryCompositeTraintuples(db, []string{})
313313
assert.NoError(t, err)
314314
require.Len(t, traintuples, 2)
315315
require.Contains(t, outCP.CompositeTraintupleKeys, traintuples[0].Key)
316316
require.Contains(t, outCP.CompositeTraintupleKeys, traintuples[1].Key)
317317

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

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

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

389389
// Check the testtuples
390-
testtuples, err := queryTesttuples(db, []string{})
390+
testtuples, _, err := queryTesttuples(db, []string{})
391391
assert.NoError(t, err)
392392
require.Len(t, testtuples, 1)
393393
testtuple := testtuples[0]
@@ -430,7 +430,7 @@ func TestQueryComputePlans(t *testing.T) {
430430
assert.NoError(t, err)
431431
assert.NotNil(t, outCP)
432432

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

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

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

528-
tuples, err := queryCompositeTraintuples(db, []string{})
528+
tuples, _, err := queryCompositeTraintuples(db, []string{})
529529
assert.NoError(t, err)
530530

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

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

572-
tuples, err := queryCompositeTraintuples(db, []string{})
572+
tuples, _, err := queryCompositeTraintuples(db, []string{})
573573
assert.NoError(t, err)
574574
for _, tuple := range tuples {
575575
if tuple.Rank == 0 {

0 commit comments

Comments
 (0)