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

Commit 075d2fc

Browse files
authored
Merge pull request #19 from hyperledger-labs/iterator-methods
Iterator methods
2 parents 22f78c3 + c7f1499 commit 075d2fc

34 files changed

+4658
-1336
lines changed

examples/Marbles.hs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import Shim ( start
1616
)
1717

1818
import Peer.ProposalResponse as Pb
19+
import Peer.ChaincodeShim as Pb
1920
import Ledger.Queryresult.KvQueryResult as Pb
2021

2122
import Data.Text ( Text
@@ -177,22 +178,23 @@ getMarblesByRange s params = if Prelude.length params == 2
177178
trace (show resultBytes) (pure $ successPayload Nothing)
178179
else pure $ errorPayload "Incorrect arguments. Need a start key and an end key"
179180

181+
-- TODO: include retrieval of next set of results using the returned bookmark (next TODO)
180182
getMarblesByRangeWithPagination :: DefaultChaincodeStub -> [Text] -> IO Pb.Response
181183
getMarblesByRangeWithPagination s params = if Prelude.length params == 4
182184
then do
183185
e <- getStateByRangeWithPagination s (params !! 0) (params !! 1) (read (unpack $ params !! 2) :: Int) (params !! 3)
184186
case e of
185187
Left _ -> pure $ errorPayload "Failed to get marbles"
186-
Right _ -> pure $ successPayload $ Just "The payload"
188+
Right (sqi, metadata) -> do
189+
resultBytes <- generateResultBytesForPagination (sqi, metadata) ""
190+
trace (show resultBytes) (pure $ successPayload Nothing)
187191
else pure $ errorPayload "Incorrect arguments. Need start key, end key, pageSize and bookmark"
188192

189193
generateResultBytes :: StateQueryIterator -> Text -> IO (Either Error BSU.ByteString)
190194
generateResultBytes sqi text = do
191195
hasNextBool <- hasNext sqi
192-
if hasNextBool then do
196+
if (trace $ "hasNext in generateResultBytes: " ++ show hasNextBool) hasNextBool then do
193197
eeKV <- next sqi
194-
-- TODO: We need to check that the Either Error KV returned from next
195-
-- is correct and append the showable version of KVs instead of "abc".
196198
case eeKV of
197199
Left e -> pure $ Left e
198200
Right kv ->
@@ -203,6 +205,21 @@ generateResultBytes sqi text = do
203205
generateResultBytes sqi (append text (makeKVString kv))
204206
else pure $ Right $ TSE.encodeUtf8 text
205207

208+
generateResultBytesForPagination:: (StateQueryIterator, Pb.QueryResponseMetadata) -> Text -> IO (Either Error BSU.ByteString)
209+
generateResultBytesForPagination (sqi, md) text = do
210+
hasNextBool <- hasNext sqi
211+
if (trace $ "hasNext in generateResultBytesForPagination: " ++ show hasNextBool) hasNextBool then do
212+
eeKV <- next sqi
213+
case eeKV of
214+
Left e -> pure $ Left e
215+
Right kv ->
216+
let
217+
makeKVString :: Pb.KV -> Text
218+
makeKVString kv_ = pack "Key: " <> TL.toStrict (Pb.kvKey kv_) <> pack ", Value: " <> TSE.decodeUtf8 (kvValue kv_)
219+
in
220+
generateResultBytesForPagination (sqi, md) (append text (makeKVString kv))
221+
else pure $ Right $ TSE.encodeUtf8 text
222+
206223
parseMarble :: [Text] -> Marble
207224
parseMarble params = Marble { objectType = "marble"
208225
, name = params !! 0

examples/readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ peer chaincode invoke -n mycc -c '{"Args":["readMarble","marble1"]}' -C myc
3232
peer chaincode invoke -n mycc -c '{"Args":["deleteMarble","marble1"]}' -C myc
3333
peer chaincode invoke -n mycc -c '{"Args":["transferMarble","marble1", "Nick"]}' -C myc
3434
peer chaincode invoke -n mycc -c '{"Args":["getMarblesByRange","marble1", "marble3"]}' -C myc
35+
peer chaincode invoke -n mycc -c '{"Args":["getMarblesByRangeWithPagination","marble1", "marble3", "1", ""]}' -C myc
3536
```
3637

3738
## Fabcar Chaincode

0 commit comments

Comments
 (0)