Find previous TxId applying to a particular key in a stream

+1 vote
I am treating a stream as a time-series, and using the Key to identify objects (which therefore have multiple transactions reflecting changes in state over time).

Is there a way to retrieve "adjacent" (next or previous) transaction against the same key, in the same stream?

I want to try avoid doing this by fetching all items by Key and figuring it out in memory.
asked May 15, 2018 by KevinV

1 Answer

+1 vote

That depends on how you are retrieving the initial item. If you are using liststreamkeyitems with appropriate start and count parameters, you can easily adjust those parameters to get the previous or next item.

If you're retrieving in some other way, I'm afraid there's no easy answer. If your stream has few different keys, you could just check for other items with the same key in that same block using liststreamblockitems with the initial item's blockhash. You can also page through blocks using the previousblockhash and nextblockhash fields of the getblock API with that same block hash. But this won't be efficient if there are many different keys in your stream.

answered May 15, 2018 by MultiChain
ah ok thanks for that. Currently I *do* get it via liststreamkeyitems, but I *also* get it using the TxId in "getstreamitem" specifically. Is there anything that could be added in to future versions to make this a possibility (efficiently, that is)?
For liststreamkeyitems, right now MultiChain uses an index:
(stream identifier, stream key, offset) -> txid

To support efficient paging given only the txid would require an additional index:
(stream identifier, stream key, txid) -> offset

Of course this is possible but it would require source code modifications.