public ProtoBlockCollection Add()

in code/KustoCopyConsole/Runner/PlanningRunner.cs [74:115]


            public ProtoBlockCollection Add(IEnumerable<ProtoBlock> blocks)
            {
                var remainingBlocks = _remainingBlock == null
                    ? blocks
                    : blocks.Prepend(_remainingBlock);

                //  We sort descending since the stack serves them upside-down
                var stack = new Stack<ProtoBlock>(remainingBlocks
                    .OrderByDescending(d => d.IngestionTimeStart)
                    .ThenByDescending(d => d.IngestionTimeEnd));
                var completedBlocks = new List<ProtoBlock>(_completedBlocks);

                while (stack.Any())
                {
                    var first = stack.Pop();

                    if (stack.Any())
                    {
                        var second = stack.Pop();
                        var merge = first.Merge(second);

                        if (first.IngestionTimeEnd == second.IngestionTimeStart
                            || (merge.RowCount <= RECORDS_PER_BLOCK
                            && merge.CreationTimeDelta < TimeSpan.FromDays(1)))
                        {   //  We merge
                            stack.Push(merge);
                        }
                        else
                        {   //  We don't merge
                            completedBlocks.Add(first);
                            stack.Push(second);
                        }
                    }
                    else
                    {   //  Only way to get out if you got into the while loop
                        return new ProtoBlockCollection(completedBlocks, first);
                    }
                }

                //  If you didn't get into the while loop, there was no remaining block
                return new ProtoBlockCollection(completedBlocks, null);
            }