/// <summary>
        /// Execute a flush operation to clear data buffered in memory
        /// to the underlying database table
        /// </summary>
        /// <param name="final">Flag indicating if this is the last flush, all source data is read</param>
        /// <param name="tableName">The underlying database table name to flush to</param>
        /// <returns>True, if a flush was performed</returns>
        public bool ExecuteFlush(bool final, string tableName)
        {
            if (Buffer.Rows.Count < (BatchSize ?? BATCH_SIZE_DEFAULT) && !final)
            {
                return(false);
            }
            if (Buffer.Rows.Count == 0)
            {
                return(false);
            }

            Buffer.EndLoadData();

            if (_flushTask != null)
            {
                _flushTask.Wait(); // wait for any previous flush to finish before starting the next one
            }
            DataTable toFlush = Buffer;

            Buffer = final ? null : CreateTable(tableName);
            Buffer?.BeginLoadData();

            _flushTask = Task.Run(() =>
            {
                Stopwatch writeTime = Stopwatch.StartNew();

                using (SqlBulkCopy sqlBulkCopy = new SqlBulkCopy(Connection, SqlBulkCopyOptions.Default, Transaction))
                {
                    if (BulkCopyTimeout.HasValue)
                    {
                        sqlBulkCopy.BulkCopyTimeout = BulkCopyTimeout.Value;
                    }
                    sqlBulkCopy.DestinationTableName = tableName;
                    sqlBulkCopy.WriteToServer(toFlush);
                    RowsSaved += toFlush.Rows.Count;
                }

                writeTime.Stop();

                OnFlush?.Invoke(new FlushEventArgs()
                {
                    RowsSaved = RowsSaved, TotalElapsedTime = _writeTimer.Elapsed, DatabaseWriteTime = writeTime.Elapsed
                });
            });

            if (final || AlwaysSyncronousFlush)
            {
                _flushTask.Wait();
            }

            return(true);
        }
示例#2
0
 int IMFSourceReaderCallback.OnFlush(int dwStreamIndex)
 {
     return(OnFlush?.Invoke(dwStreamIndex) ?? 0);
 }
示例#3
0
 internal static void Flush()
 {
     OnFlush?.Invoke();
 }
示例#4
0
 protected void RaiseOnFlush()
 {
     OnFlush?.Invoke(this, new EventArgs());
 }
示例#5
0
 public void Flush() => OnFlush?.Invoke();
示例#6
0
 public void Flush()
 {
     OnFlush?.Invoke();
 }