diff --git a/shim/interfaces.go b/shim/interfaces.go index 66993fb..816429a 100644 --- a/shim/interfaces.go +++ b/shim/interfaces.go @@ -184,6 +184,13 @@ type ChaincodeStubInterface interface { GetStateByPartialCompositeKeyWithPagination(objectType string, keys []string, pageSize int32, bookmark string) (StateQueryIteratorInterface, *peer.QueryResponseMetadata, error) + // GetAllStatesCompositeKeyWithPagination returns a range iterator over all set of + // keys with composite keys in the ledger. + // The bookmark works the same way as when GetStateByPartialCompositeKeyWithPagination is called. + // This call is only supported in a read only transaction. + GetAllStatesCompositeKeyWithPagination(pageSize int32, + bookmark string) (StateQueryIteratorInterface, *peer.QueryResponseMetadata, error) + // CreateCompositeKey combines the given `attributes` to form a composite // key. The objectType and attributes are expected to have only valid utf8 // strings and should not contain U+0000 (nil byte) and U+10FFFF diff --git a/shim/stub.go b/shim/stub.go index 2553f24..943b940 100644 --- a/shim/stub.go +++ b/shim/stub.go @@ -634,6 +634,19 @@ func (s *ChaincodeStub) GetQueryResultWithPagination(query string, pageSize int3 return s.handleGetQueryResult(collection, query, metadata) } +// GetAllStatesCompositeKeyWithPagination ... +func (s *ChaincodeStub) GetAllStatesCompositeKeyWithPagination(pageSize int32, + bookmark string) (StateQueryIteratorInterface, *peer.QueryResponseMetadata, error) { + collection := "" + metadata, err := createQueryMetadata(pageSize, bookmark) + if err != nil { + return nil, nil, err + } + startKey := compositeKeyNamespace + endKey := compositeKeyNamespace + string(maxUnicodeRuneValue) + return s.handleGetStateByRange(collection, startKey, endKey, metadata) +} + // --------- Batch State functions ---------- // StartWriteBatch documentation can be found in interfaces.go diff --git a/shim/stub_test.go b/shim/stub_test.go index 8ab02bc..1f31910 100644 --- a/shim/stub_test.go +++ b/shim/stub_test.go @@ -540,6 +540,16 @@ func TestChaincodeStubHandlers(t *testing.T) { assert.Equal(t, "book", qrm.GetBookmark()) assert.Equal(t, int32(1), qrm.GetFetchedRecordsCount()) + sqi, qrm, err = s.GetAllStatesCompositeKeyWithPagination(1, "book") + assert.NoError(t, err) + kv, err = sqi.Next() + if err != nil { + t.Fatalf("Unexpected error for GetAllStatesCompositeKeyWithPagination: %s", err) + } + requireProtoEqual(t, expectedResult, kv) + assert.Equal(t, "book", qrm.GetBookmark()) + assert.Equal(t, int32(1), qrm.GetFetchedRecordsCount()) + sqi, qrm, err = s.GetQueryResultWithPagination("query", 1, "book") assert.NoError(t, err) kv, err = sqi.Next()