Open-NET-Libraries / Open.ChannelExtensions

A set of extensions for optimizing/simplifying System.Threading.Channels usage.
https://open-net-libraries.github.io/Open.ChannelExtensions/api/Open.ChannelExtensions.Extensions.html#methods
MIT License
401 stars 25 forks source link

Unbatch / Split #27

Closed KenBonny closed 1 year ago

KenBonny commented 1 year ago

Hello and first of all, awesome library. Making my life a lot easier already. But I seem to missing something. I batched results to perform database lookups efficiently (I don't want 500 db looksup, I want 5 queries that fetch 100 rows each). Now I want to split them back out. But I can't seem to find how to do this.

Am I missing or overlooking something? Is there a pattern to do it (return IAsyncEnumerable<T>)? Or is there no extension to easily do this?

electricessence commented 1 year ago

I may not understand your use-case exactly, but 'unbatch' = .Join. Which allows for any IEnumerable<T> to be the type of the contents within a channel. You can simply call .Join() on a ChannelReader<T[]> (for example) to convert to a ChannelReader<T>.

Another thing to consider is that if this doesn't solve your problem, then .Pipe and .PipeAsync methods might be what you need.

Lemme know if that helps.

electricessence commented 1 year ago

I also think that if .Join(), .Pipe(), or .PipeAsync() can't do it, then you might simply need another channel to act as the batch receiver with an intermediate consumer that creates the batches.

KenBonny commented 1 year ago

You are completely correct. I misunderstood the Join transform. I thought it was to join everything in one list, but that is what the ReadAll transform is for. So Join is the inverse of Batch. That is what I indeed want to do. 🤦

Thank you for your time. ❤️