/// <summary>
        /// Load existing file to memory, set filenumber
        /// </summary>
        private void LoadExistingFilesBeforeStart()
        {
            IOrderedEnumerable<string> existingFilesBeforeStart = Directory.GetFiles(this._DirectoryName).OrderBy(o =>
            {
                int fileNumber = 0;
                if (int.TryParse(Path.GetFileNameWithoutExtension(Path.GetFileName(o)), out fileNumber))
                {
                    return fileNumber;
                }
                else
                {
                    return 0;
                }
            });

            MemoryBatch memoryBatch = new MemoryBatch();
            foreach (string fileName in existingFilesBeforeStart)
            {
                memoryBatch.LoadFromFile(fileName);

                if (memoryBatch.FileNames.Count >= this._BatchSize)
                {
                    this._Queue.Enqueue(memoryBatch);
                    memoryBatch = new MemoryBatch();
                }
            }
            if (memoryBatch.FileNames.Count > 0)
            {
                this._Queue.Enqueue(memoryBatch);
            }

            int result = 0;
            if (existingFilesBeforeStart != null)
            {
                foreach (string fileName in existingFilesBeforeStart)
                {
                    int fileNumber = 0;

                    if (int.TryParse(Path.GetFileNameWithoutExtension(Path.GetFileName(fileName)), out fileNumber) && fileNumber > result)
                    {
                        result = fileNumber;
                    }
                }
            }

            this._FileNumber = result + 1;
            this._HasPendingQuotationsBeforeStart = (this._Queue.Count > 0);
        }
        private bool Add(IEnumerable<iExchange.Common.OriginQuotation> originQuotations, IEnumerable<iExchange.Common.OverridedQuotation> overridedQuotations)
        {
            if (originQuotations == null && overridedQuotations == null) return true;
            try
            {
                string fileName;

                lock (this._QueueLock)
                {
                    fileName = string.Format("{0}\\{1}.txt", this._DirectoryName, this._FileNumber);
                    if (this._ActiveMemoryBatch == null)
                    {
                        this._ActiveMemoryBatch = new MemoryBatch();
                        this._Queue.Enqueue(this._ActiveMemoryBatch);
                    }
                    this._ActiveMemoryBatch.Add(originQuotations, overridedQuotations, fileName);

                    if (this._ActiveMemoryBatch.FileNames.Count >= this._BatchSize)
                    {
                        this._ActiveMemoryBatch = null;
                    }

                    this._FileNumber++;
                }

                using (StreamWriter streamWriter = new StreamWriter(fileName, true))
                {
                    if (originQuotations != null)
                    {
                        foreach (iExchange.Common.OriginQuotation originQuotation in originQuotations)
                        {
                            streamWriter.Write(originQuotation.ToQuotationFileItem());
                        }
                    }
                    if (overridedQuotations != null)
                    {
                        foreach (iExchange.Common.OverridedQuotation overridedQuotation in overridedQuotations)
                        {
                            streamWriter.Write(overridedQuotation.ToQuotationFileItem());
                        }
                    }
                }
                return true;
            }
            catch (Exception exception)
            {
                AppDebug.LogEvent("QuotationServer", string.Format("DatabaseBatch.Add Error: {0}", exception), EventLogEntryType.Error);
                return false;
            }
        }
        internal bool Add(MemoryBatch memoryBatch)
        {
            try
            {
                foreach (iExchange.Common.OriginQuotation originQuotation in memoryBatch.OriginQuotations)
                {
                    DataTable dataTable = null;
                    int day = originQuotation.Timestamp.Day;
                    if (this._OriginDataTables.ContainsKey(day))
                    {
                        dataTable = this._OriginDataTables[day];
                    }
                    else
                    {
                        dataTable = this.CreateEmptyDataTable(true);
                        this._OriginDataTables.Add(day, dataTable);
                    }
                    DataRow row = dataTable.NewRow();
                    row["InstrumentId"] = originQuotation.InstrumentID;
                    row["Timestamp"] = originQuotation.Timestamp;
                    row["Day"] = day;

                    row["Ask"] = originQuotation.Ask;
                    row["Bid"] = originQuotation.Bid;
                    row["High"] = originQuotation.High;
                    row["Low"] = originQuotation.Low;
                    if (!string.IsNullOrEmpty(originQuotation.Volume))
                    {
                        row["Volume"] = double.Parse(originQuotation.Volume);
                    }
                    dataTable.Rows.Add(row);

                    // update LastSessionLastOriginQuotations
                    DataRow originRow = null;
                    if (this._LastSessionLastOriginQuotationRows.ContainsKey(originQuotation.InstrumentID))
                    {
                        originRow = this._LastSessionLastOriginQuotationRows[originQuotation.InstrumentID];
                    }
                    else
                    {
                        originRow = this._LastSessionLastOriginQuotationTable.NewRow();
                        originRow["InstrumentId"] = originQuotation.InstrumentID;
                        this._LastSessionLastOriginQuotationTable.Rows.Add(originRow);
                        this._LastSessionLastOriginQuotationRows.Add(originQuotation.InstrumentID, originRow);
                    }
                    originRow["Timestamp"] = originQuotation.Timestamp;
                    originRow["Day"] = originQuotation.Timestamp.Day;
                    originRow["Ask"] = originQuotation.Ask;
                    originRow["Bid"] = originQuotation.Bid;
                    originRow["High"] = originQuotation.High;
                    originRow["Low"] = originQuotation.Low;
                    if (string.IsNullOrEmpty(originQuotation.Volume))
                    {
                        originRow["Volume"] = DBNull.Value;
                    }
                    else
                    {
                        originRow["Volume"] = double.Parse(originQuotation.Volume);
                    }
                    //
                }

                foreach (iExchange.Common.OverridedQuotation overridedQuotation in memoryBatch.OverridedQuotations)
                {
                    DataTable dataTable = null;
                    int day = overridedQuotation.Timestamp.Day;
                    if (this._OverridedDataTables.ContainsKey(day))
                    {
                        dataTable = this._OverridedDataTables[day];
                    }
                    else
                    {
                        dataTable = this.CreateEmptyDataTable(false);
                        this._OverridedDataTables.Add(day, dataTable);
                    }
                    DataRow row = dataTable.NewRow();
                    row["InstrumentId"] = overridedQuotation.InstrumentID;
                    row["QuotePolicyId"] = overridedQuotation.QuotePolicyID;
                    row["Timestamp"] = overridedQuotation.Timestamp;
                    row["Day"] = day;

                    row["Origin"] = overridedQuotation.Origin;
                    row["Ask"] = overridedQuotation.Ask;
                    row["Bid"] = overridedQuotation.Bid;
                    row["High"] = overridedQuotation.High;
                    row["Low"] = overridedQuotation.Low;
                    row["DealerId"] = overridedQuotation.DealerID;
                    if (string.IsNullOrEmpty(overridedQuotation.Volume))
                    {
                        row["Volume"] = DBNull.Value;
                    }
                    else
                    {
                        row["Volume"] = double.Parse(overridedQuotation.Volume);
                    }

                    dataTable.Rows.Add(row);

                    // update LastSessionLastOverridedQuotations
                    DataRow overridedRow = null;
                    Dictionary<Guid, DataRow> overridedRows = null;
                    if (this._LastSessionLastOverridedQuotationRows.ContainsKey(overridedQuotation.InstrumentID))
                    {
                        overridedRows = this._LastSessionLastOverridedQuotationRows[overridedQuotation.InstrumentID];
                    }
                    else
                    {
                        overridedRows = new Dictionary<Guid, DataRow>();
                        this._LastSessionLastOverridedQuotationRows.Add(overridedQuotation.InstrumentID, overridedRows);
                    }

                    if (overridedRows.ContainsKey(overridedQuotation.QuotePolicyID))
                    {
                        overridedRow = overridedRows[overridedQuotation.QuotePolicyID];
                    }
                    else
                    {
                        overridedRow = this._LastSessionLastOverridedQuotationTable.NewRow();
                        overridedRow["InstrumentId"] = overridedQuotation.InstrumentID;
                        overridedRow["QuotePolicyId"] = overridedQuotation.QuotePolicyID;

                        this._LastSessionLastOverridedQuotationTable.Rows.Add(overridedRow);
                        overridedRows.Add(overridedQuotation.QuotePolicyID, overridedRow);
                    }

                    overridedRow["Timestamp"] = overridedQuotation.Timestamp;
                    overridedRow["Day"] = overridedQuotation.Timestamp.Day;
                    overridedRow["Origin"] = overridedQuotation.Origin;
                    overridedRow["Ask"] = overridedQuotation.Ask;
                    overridedRow["Bid"] = overridedQuotation.Bid;
                    overridedRow["High"] = overridedQuotation.High;
                    overridedRow["Low"] = overridedQuotation.Low;
                    overridedRow["DealerId"] = overridedQuotation.DealerID;
                    if (string.IsNullOrEmpty(overridedQuotation.Volume))
                    {
                        overridedRow["Volume"] = DBNull.Value;
                    }
                    else
                    {
                        overridedRow["Volume"] = double.Parse(overridedQuotation.Volume);
                    }
                    //
                }

                this._FileNames.AddRange(memoryBatch.FileNames);
                return true;
            }
            catch (Exception exception)   // stream exception or file delete exception
            {
                Manager.Common.Logger.TraceEvent(TraceEventType.Error, "QuotationServer\r\nDatabaseBatch.Add error: {0}", exception);
                throw;
            }
        }
        ///////////////////////////////////////////////////
        //save memory quotations to database
        ///////////////////////////////////////////////////
        public bool SaveMemoryToDatabase()
        {
            try
            {
                //AppDebug.LogEvent("QuotationServer", string.Format("begin QuotationFileCache.SaveMemoryToDatabase, Queue.Count={0}", this._Queue.Count), EventLogEntryType.Information);
                lock (this._QueueLock)
                {
                    while (this._Queue.Count > 0)
                    {
                        MemoryBatch memoryBatch = this._Queue.Peek();
                        DatabaseBatch databaseBatch = new DatabaseBatch(memoryBatch);

                        if (this.SaveDatabaseBatch(databaseBatch))
                        {
                            databaseBatch.Clear();
                            this._Queue.Dequeue();
                        }

                        if (this._Queue.Count == 0 && this._ActiveMemoryBatch != null)
                        {
                            this._ActiveMemoryBatch = null;
                        }
                    }
                }
                //AppDebug.LogEvent("QuotationServer", string.Format("end QuotationFileCache.SaveMemoryToDatabase, Queue.Count={0}", this._Queue.Count), EventLogEntryType.Information);
                return true;
            }
            catch (Exception exception)
            {
                AppDebug.LogEvent("QuotationServer", string.Format("QuotationFileCache.SaveMemoryToDatabase error: {0}", exception), EventLogEntryType.Error);
                return false;
            }
        }
 internal DatabaseBatch(MemoryBatch memoryBatch)
     : this()
 {
     this.Add(memoryBatch);
 }