Consider an author trying to maintain a library on the ledger with
template Book
with
author : Party
isin : Text
chapters : [Int]
where
signatory author
key (author, isin) : (Party, Text)
maintainer key._1
template Chapter
with
author : Party
bookIsin : Text
chapter : Int
text : Text
bookId : ContractId Book
where
signatory author
key (author, bookIsin, chapter) : (Party, Text, Int)
maintainer key._1
Now the author uses the CreateAndExercise pattern to upload the library to the ledger. But within the processing of the upload, there are two options to keep track of the association between Book
s and Chapter
s: via the ledger or via a specific Map.
template UploadLibrary
with
author : Party
books : [(Text, [Int])] -- isin, chapters
chapters : [(Text, Int, Text)] -- isin, chapter, text
where
signatory author
choice CreateViaLedger : ([ContractId Book], [ContractId Chapter])
controller author
do
bookIds <- forA books (\(isin, chapters) -> create Book with ..)
chapterIds <- forA chapters (\(bookIsin, chapter, text) -> do
-- books have been created
Some bookId <- lookupByKey @Book (author, bookIsin)
create Chapter with ..)
return (bookIds, chapterIds)
choice CreateViaMap : ([ContractId Book], [ContractId Chapter])
controller author
do
(bookMap, bookIds) <- foldlA (\(bookMap, bookIds) (isin, chapters) -> do
bookId <- create Book with ..
return (insert isin bookId bookMap, bookId::bookIds) )
(mempty, []) books
chapterIds <- forA chapters (\(bookIsin, chapter, text) -> do
let Some bookId = lookup bookIsin bookMap
create Chapter with ..)
return (bookIds, chapterIds)
What do you think is the better approach ?
- The two methods are not equivalent as the former allows one to send updates on
Chapter
s outside of theUploadLibrary
payload; those that might exist previously. - Is there a performance tradeoff to using the
lookupByKey
instead of the regularMap.lookup
?