示例#1
0
        public Task WriteAsync(IDataItem dataItem, CancellationToken cancellation)
        {
            if (String.IsNullOrEmpty(storedProcedureLink))
            {
                throw Errors.SinkIsNotInitialized();
            }

            var currentItemIndex = Interlocked.Increment(ref numberOfItems);

            buffer.Add(new BulkItemSurrogate(currentItemIndex, Transformation.Transform(dataItem)));

            var waitTaskCompletionSource = new TaskCompletionSource <object>();

            if (!activeBulkItems.TryAdd(currentItemIndex, waitTaskCompletionSource))
            {
                throw Errors.BufferSlotIsOccupied();
            }

            if (HaveEnoughRecordsForBatch() && flushSemaphore.Wait(0))
            {
                // If we are not flushing yet - fire and forget flush task
                FlushWhileAsync(HaveEnoughRecordsForBatch, true, cancellation)
                .ContinueWith(HandleUnexpectedFlushFailure, TaskContinuationOptions.OnlyOnFaulted);
            }

            return(waitTaskCompletionSource.Task);
        }
        public Task WriteAsync(IDataItem dataItem, CancellationToken cancellation)
        {
            if (String.IsNullOrEmpty(storedProcedureLink))
            {
                throw Errors.SinkIsNotInitialized();
            }

            currentItemIndex = Interlocked.Increment(ref currentItemIndex);
            buffer.Add(new BulkItemSurrogate(currentItemIndex, Transformation.Transform(dataItem)));

            TaskCompletionSource <object> waitTaskCompletionSource;

            if (buffer.Count < Configuration.BatchSize)
            {
                if (!activeBulkItems.TryAdd(currentItemIndex, waitTaskCompletionSource = new TaskCompletionSource <object>()))
                {
                    throw Errors.BufferSlotIsOccupied();
                }

                return(waitTaskCompletionSource.Task);
            }

            return(FlushCurrentBufferSynchronizedAsync(currentItemIndex));
        }