public void Constructor_with_enumerator_argument_should_initialize_instance()
        {
            var items = new List <int> {
                1, 2
            };
            var subject = new BatchableSource <int>(items.GetEnumerator());

            subject.Batch.Should().BeNull();
            subject.HasMore.Should().BeTrue();
            subject.IsBatchable.Should().BeTrue();
        }
        public void Constructor_with_enumerable_argument_should_initialize_instance()
        {
            var items = new List <int> {
                1, 2
            };
            var subject = new BatchableSource <int>(items);

            subject.Batch.Should().Equal(items);
            subject.HasMore.Should().BeFalse();
            subject.IsBatchable.Should().BeFalse();
        }
        public void EndBatch_with_no_overflow_should_set_batch_and_set_HashMore_to_false()
        {
            var subject = new BatchableSource <int>(Enumerable.Empty <int>().GetEnumerator());

            subject.Batch.Should().BeNull();
            subject.HasMore.Should().BeTrue();
            var batch = new int[] { 1, 2 };

            subject.EndBatch(batch);
            subject.Batch.Should().BeSameAs(batch);
            subject.HasMore.Should().BeFalse();
        }
        public void StartBatch_should_return_and_clear_any_overflow()
        {
            var subject  = new BatchableSource <int>(Enumerable.Empty <int>().GetEnumerator());
            var batch    = new int[0];
            var overflow = new BatchableSource <int> .Overflow {
                Item = 1, State = null
            };

            subject.EndBatch(batch, overflow);
            subject.StartBatch().Should().BeSameAs(overflow);
            subject.StartBatch().Should().BeNull();
        }
        public void MoveNext_and_Current_should_enumerate_the_items()
        {
            var expectedItems = new List <int> {
                1, 2
            };
            var subject = new BatchableSource <int>(expectedItems.GetEnumerator());

            var items = new List <int>();

            while (subject.MoveNext())
            {
                items.Add(subject.Current);
            }

            items.Should().Equal(expectedItems);
        }
        public void EndBatch_with_overflow_should_set_batch_and_set_HashMore_to_true()
        {
            var subject = new BatchableSource <int>(Enumerable.Empty <int>().GetEnumerator());

            subject.Batch.Should().BeNull();
            subject.HasMore.Should().BeTrue();
            var batch    = new int[] { 1, 2 };
            var overflow = new BatchableSource <int> .Overflow {
                Item = 3, State = 4
            };

            subject.EndBatch(batch, overflow);
            subject.Batch.Should().BeSameAs(batch);
            subject.HasMore.Should().BeTrue();
            subject.StartBatch().Should().BeSameAs(overflow);
        }
        public void ClearBatch_should_clear_batch()
        {
            var items = new List <int> {
                1, 2
            };
            var subject = new BatchableSource <int>(items.GetEnumerator());

            subject.StartBatch();
            subject.MoveNext();
            var batch = new[] { subject.Current };

            subject.EndBatch(batch);
            subject.Batch.Should().NotBeNull();

            subject.ClearBatch();
            subject.Batch.Should().BeNull();
        }
        public void constructor_with_list_should_initialize_instance(
            [Values(0, 1, 2, 3)] int length)
        {
            var list = new List <int>();

            for (var i = 0; i < length; i++)
            {
                list.Add(i);
            }

            var result = new BatchableSource <int>(list);

            result.CanBeSplit.Should().BeFalse();
            result.Count.Should().Be(length);
            result.Items.Should().Equal(list);
            result.Offset.Should().Be(0);
            result.ProcessedCount.Should().Be(0);
        }
        public void constructor_with_list_offset_count_and_canBeSplit_should_initialize_instance(
            [Values(3, 4)] int length,
            [Values(0, 1)] int offset,
            [Values(1, 2)] int count,
            [Values(false, true)] bool canBeSplit)
        {
            var list = new List <int>();

            for (var i = 0; i < length; i++)
            {
                list.Add(i);
            }

            var result = new BatchableSource <int>(list, offset, count, canBeSplit);

            result.CanBeSplit.Should().Be(canBeSplit);
            result.Count.Should().Be(count);
            result.Items.Should().Equal(list);
            result.Offset.Should().Be(offset);
            result.ProcessedCount.Should().Be(0);
        }
Exemple #10
0
        // public methods
        /// <inheritdoc />
        public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, BatchableSource <TItem> value)
        {
            Ensure.IsNotNull(value, nameof(value));

            var writer = context.Writer;

            while (writer is WrappingBsonWriter)
            {
                writer = ((WrappingBsonWriter)writer).Wrapped;
            }

            var binaryWriter  = writer as BsonBinaryWriter;
            var startPosition = binaryWriter?.Position;

            writer.PushSettings(s => { var bs = s as BsonBinaryWriterSettings; if (bs != null)
                                       {
                                           bs.MaxDocumentSize = _maxItemSize;
                                       }
                                });
            writer.PushElementNameValidator(_itemElementNameValidator);
            try
            {
                var batchCount = Math.Min(value.Count, _maxBatchCount);
                if (batchCount != value.Count && !value.CanBeSplit)
                {
                    throw new ArgumentException("Batch is too large.");
                }

                for (var i = 0; i < batchCount; i++)
                {
                    var itemPosition = binaryWriter?.Position;

                    var item = value.Items[value.Offset + i];
                    _itemSerializer.Serialize(context, args, item);

                    var batchSize = binaryWriter?.Position - startPosition;
                    if (batchSize > _maxBatchSize)
                    {
                        if (i > 0 && value.CanBeSplit)
                        {
                            binaryWriter.BaseStream.Position = itemPosition.Value; // remove the last item
                            binaryWriter.BaseStream.SetLength(itemPosition.Value);
                            value.SetProcessedCount(i);
                            return;
                        }
                        else
                        {
                            throw new ArgumentException("Batch is too large.");
                        }
                    }
                }
                value.SetProcessedCount(batchCount);
            }
            finally
            {
                writer.PopElementNameValidator();
                writer.PopSettings();
            }
        }
        // public methods
        /// <inheritdoc />
        public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, BatchableSource <TItem> value)
        {
            Ensure.IsNotNull(value, nameof(value));
            var writer = context.Writer;

            writer.PushElementNameValidator(_itemElementNameValidator);
            try
            {
                for (var i = 0; i < _count; i++)
                {
                    var item = value.Items[value.Offset + i];
                    _itemSerializer.Serialize(context, item);
                }
            }
            finally
            {
                writer.PopElementNameValidator();
            }
        }