Recommended strategy to update JSON objects

+3 votes

When we publish JSON objects to streams, the problem comes up about how to store an edited or updated object.

I see two approaches:

Option 1

Create a JSON holding only the changed attributes and add it to the stream in a new block. In this case the full updated JSON can be read by getstreamkeysummary API call using the jsonobjectmerge mode.

Option 2

Make a copy of the full JSON and update the edited attributes. Store the updated JSON in the stream. After that we can read the most recent object by liststreamkeyitems API call (count=1 offset=-1).

I noticed that getstreamkeysummary can be slow when there are few 1000 objects, but I assume if there are few blocks matching the key, it's not an issue.

So what is the recommended approach to store updated JSON objects in a stream?

 

asked Feb 16, 2022 by geza

1 Answer

+2 votes
 
Best answer

First, just FYI, you should be thinking in terms of transactions rather than blocks, since each stream item is contained by a single transaction. There can be a large number of transactions in a single block.

Second, there is no explicit recommendation on this question. But as you say, when there are 1000s of matching JSON objects to be merged together, the getstreamkeysummary API command can become slow. So if this becomes an issue for your application, you could use the second option you mention.

The other possibility is to think about your schema design, and change that design so that there aren't so many items with the same key. What are the fields you are updating for this key? Perhaps there's another way to represent things.

answered Feb 16, 2022 by MultiChain
selected Feb 17, 2022 by geza
I don't know much about blockchain transactions, but there could be days spent between two updates, so I don't think it helps me out this time. I've read the page https://www.multichain.com/developers/raw-transactions/ about that, but did not make it clear how that works. Is there other documentation worth reading?

For now I try to design the schema to keep the number of changes minimized.
Thank you for your help.
Regarding blocks vs transactions, there's no need for you to do anything different, I just wanted to clear up a misunderstanding.
...