Based on what kind of data store we are using, there are cases when the size of a batch is limited. For example if we want to execute an update batch command over Windows Azure Tables we will notify that the maxim size of a batch is 100. Also all the updated command needs to be on the same partition key.
What should we do if we have a batch that with more than 100 items. The simples’ solution is to create groups of chunks of 100 items and execute and execute them in parallel. In the next part of the post we will see how we can create chunks of items from a list:
What should we do if we have a batch that with more than 100 items. The simples’ solution is to create groups of chunks of 100 items and execute and execute them in parallel. In the next part of the post we will see how we can create chunks of items from a list:
List<Foo> items = new List<Foo>();
….
int chunkSize = 100;
List<List<Foo>> chunks = new List<List<Foo>>();
int index = 0;
int itemsCount = items.Count;
while( index < itemsCount )
{
int count = itemsCount > chunkSize
? itemsCount
: index;
Chunks.Add( items.GetRange( index, count));
}
After this step if we need to execute the update command over the table context we can do this very easily:foreach( var chunk in chunks )
{
// execute the update command using the Save command.
}
In this post we saw how we can create chunks of items to be able to execute different command on them. If we don’t do this step we will get error when we will try, for example, to update 101 items using only a batch command.
An useful post!
ReplyDeleteBased on your idea, I wrote the following complete generic methods:
https://gist.github.com/4410294
(I tried to put the code here but it lost formating)
Lucian