private RemoteBulkInsertOperation GetBulkInsertOperation()
        {
            if (current == null)
            {
                return(current = CreateBulkInsertOperation(Task.FromResult(0)));
            }

            if (processedItemsInCurrentOperation < chunkSize)
            {
                if (!documentSizeInChunkLimit.HasValue || documentSizeInChunk < documentSizeInChunkLimit.Value)
                {
                    return(current);
                }
            }

            // if we haven't flushed the previous one yet, we will force
            // a disposal of both the previous one and the one before, to avoid
            // consuming a lot of memory, and to have _too_ much concurrency.
            if (previousTask != null)
            {
                previousTask.ConfigureAwait(false).GetAwaiter().GetResult();
            }
            previousTask = current.DisposeAsync();

            documentSizeInChunk = 0;
            processedItemsInCurrentOperation = 0;
            current = CreateBulkInsertOperation(previousTask);
            return(current);
        }
        private RemoteBulkInsertOperation GetBulkInsertOperation()
        {
            if (current == null)
            {
                return(current = CreateBulkInsertOperation());
            }

            //
            if (processedItemsInCurrentOperation < chunkSize)
            {
                if (!documentSizeInChunkLimit.HasValue || documentSizeInChunk < documentSizeInChunkLimit.Value)
                {
                    return(current);
                }
            }

            documentSizeInChunk = 0;
            processedItemsInCurrentOperation = 0;
            tasks.Add(current.DisposeAsync());
            return(current = CreateBulkInsertOperation());
        }