public ReactiveBatchRepository(
            IDatabase database)
        {
            var options = new BatchOperatorOptions <int, int>
            {
                BufferTime  = TimeSpan.FromMilliseconds(10),
                BufferCount = 10000,
                DoManyFunc  = database.InsertMany,
            };

            _batchInsertOperator = new ReactiveBatchOperator <int, int>(options);
        }
Exemplo n.º 2
0
        public ReactiveBatchOperator(
            BatchOperatorOptions <TInput, TOutput> options)
        {
            if (options.BufferTime == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            if (options.BufferCount == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            _subject
            .Buffer(options.BufferTime.Value, options.BufferCount.Value)
            .Where(x => x.Count > 0)
            .Select(x => Observable.FromAsync(async() =>
            {
                try
                {
                    var result = await options.DoManyFunc.Invoke(x.Select(a => a.Input)).ConfigureAwait(false);
                    foreach (var savingItem in x)
                    {
                        savingItem.Tcs.SetResult(result);
                    }
                }
                catch (Exception e)
                {
                    foreach (var savingItem in x)
                    {
                        savingItem.Tcs.SetException(e);
                    }
                }
            }))
            .Merge()
            .Subscribe();
        }