예제 #1
0
        internal static List <HistoryRecord> GetRecords(ServerInfo server, bool isServer, DateTypes dateType, DateTime startDate)
        {
            var records = new List <HistoryRecord>();
            var key     = GetKey(server, isServer);

            using (var dateIndexStream = new FileStream(GetFile(false, true), FileMode.OpenOrCreate))
            {
                using (var dateContentStream = new FileStream(GetFile(false, false), FileMode.OpenOrCreate))
                {
                    var dateFormatter = new CustomBinaryFormatter(dateIndexStream, dateContentStream);
                    dateFormatter.Register <HistoryDate>(1);

                    var endDate      = DateTime.Now.Date;
                    var samplingSpan = 1;
                    switch (dateType)
                    {
                    case DateTypes.Hour:
                        endDate      = startDate.AddDays(1);
                        samplingSpan = 1;
                        break;

                    case DateTypes.Day:
                        endDate      = startDate.AddDays(1);
                        samplingSpan = 24;
                        break;

                    case DateTypes.Week:
                        endDate      = startDate.AddDays(7);
                        samplingSpan = 7 * 24;
                        break;

                    case DateTypes.Month:
                        endDate      = startDate.AddMonths(1);
                        samplingSpan = 31 * 24;
                        break;

                    case DateTypes.Year:
                        endDate      = startDate.AddYears(1);
                        samplingSpan = 365 * 24;
                        break;

                    default:
                        break;
                    }
                    var count = dateFormatter.Count;
                    Debug.WriteLine("all date count:" + count);
                    Debug.WriteLine("start date:" + startDate);
                    var dates = new List <HistoryDate>();
                    for (long i = 0; i < count; i++)
                    {
                        var date     = dateFormatter.Deserialize <HistoryDate>(false);
                        var dateTime = DateTime.Parse(date.Date);
                        Debug.WriteLine("current date:" + dateTime);
                        if (startDate.Date <= dateTime && dateTime <= endDate)
                        {
                            dates.Add(date);
                        }
                    }
                    Debug.WriteLine("valid date count:" + dates.Count);
                    if (dates.Count > 0)
                    {
                        var start = dates.Aggregate((d1, d2) => DateTime.Parse(d1.Date) < DateTime.Parse(d2.Date) ? d1 : d2);
                        var end   = dates.Aggregate((d1, d2) => DateTime.Parse(d1.Date) > DateTime.Parse(d2.Date) ? d1 : d2);

                        using (var dataIndexStream = new FileStream(GetFile(true, true), FileMode.OpenOrCreate))
                        {
                            using (var dataContentStream = new FileStream(GetFile(true, false), FileMode.OpenOrCreate))
                            {
                                var historyFormatter = new CustomBinaryFormatter(dataIndexStream, dataContentStream);
                                historyFormatter.Register <HistoryRecord>(1);

                                for (var i = start.Index; i < end.Index; i += samplingSpan)
                                {
                                    historyFormatter.MoveTo(i);
                                    var record = historyFormatter.Deserialize <HistoryRecord>(false);
                                    if (record.Key == key)
                                    {
                                        records.Add(record);
                                    }
                                }
                                historyFormatter.Close();
                                dataContentStream.Close();
                            }
                            dataIndexStream.Close();
                        }
                    }
                    dateFormatter.Close();
                    dateContentStream.Close();
                }
                dateIndexStream.Close();
            }
            return(records);
        }
예제 #2
0
        internal static void AddRecords(List <HistoryRecord> records)
        {
            lock (SyncRoot)
            {
                using (var dataIndexStream = new FileStream(GetFile(true, true), FileMode.OpenOrCreate))
                {
                    using (var dataContentStream = new FileStream(GetFile(true, false), FileMode.OpenOrCreate))
                    {
                        var historyFormatter = new CustomBinaryFormatter(dataIndexStream, dataContentStream);
                        historyFormatter.Register <HistoryRecord>(1);

                        historyFormatter.MoveToEnd();
                        var historyCount = historyFormatter.Count;
                        records.ForEach(r =>
                        {
                            historyFormatter.Serialize <HistoryRecord>(r);
                        });

                        historyFormatter.Flush();

                        using (var dateIndexStream = new FileStream(GetFile(false, true), FileMode.OpenOrCreate))
                        {
                            using (var dateContentStream = new FileStream(GetFile(false, false), FileMode.OpenOrCreate))
                            {
                                var dateFormatter = new CustomBinaryFormatter(dateIndexStream, dateContentStream);
                                dateFormatter.Register <HistoryDate>(1);

                                var  dateCount      = dateFormatter.Count;
                                var  today          = DateTime.Now.Date.AddDays(1);
                                var  yesterday      = DateTime.Now.Date;
                                long todayIndex     = -1;
                                var  foundYesterday = false;
                                for (long i = 0; i < dateCount; i++)
                                {
                                    var date = dateFormatter.Deserialize <HistoryDate>(false);
                                    if (DateTime.Parse(date.Date) == today)
                                    {
                                        todayIndex = i;
                                    }
                                    if (DateTime.Parse(date.Date) == yesterday)
                                    {
                                        foundYesterday = true;
                                    }
                                }
                                if (!foundYesterday)
                                {
                                    dateFormatter.MoveToEnd();
                                    dateFormatter.Serialize <HistoryDate>(new HistoryDate {
                                        Date = yesterday.ToString(), Index = historyCount
                                    });
                                }

                                if (todayIndex != -1)
                                {
                                    dateFormatter.MoveTo(todayIndex);
                                    dateFormatter.Serialize <HistoryDate>(new HistoryDate {
                                        Date = today.ToString(), Index = historyFormatter.Count
                                    }, true);
                                }
                                else
                                {
                                    dateFormatter.MoveToEnd();
                                    dateFormatter.Serialize <HistoryDate>(new HistoryDate {
                                        Date = today.ToString(), Index = historyFormatter.Count
                                    });
                                }

                                dateFormatter.Flush();
                            }
                        }
                    }
                }
            }
        }