示例#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 List <HistoryRecord> GetRecords(ServerInfo Server, bool IsServer, DateTypes DateType, DateTime StartDate)
        {
            var records = new List <HistoryRecord>();
            var key     = GetKey(Server, IsServer);

            using (FileStream dateIndexStream = new FileStream(GetFile(false, true), FileMode.OpenOrCreate))
            {
                using (FileStream 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;
                    System.Diagnostics.Debug.WriteLine("all date count:" + count);
                    System.Diagnostics.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);
                        System.Diagnostics.Debug.WriteLine("current date:" + dateTime);
                        if (StartDate.Date <= dateTime && dateTime <= endDate)
                        {
                            dates.Add(date);
                        }
                    }
                    System.Diagnostics.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 (FileStream dataIndexStream = new FileStream(GetFile(true, true), FileMode.OpenOrCreate))
                        {
                            using (FileStream dataContentStream = new FileStream(GetFile(true, false), FileMode.OpenOrCreate))
                            {
                                var historyFormatter = new CustomBinaryFormatter(dataIndexStream, dataContentStream);
                                historyFormatter.Register <HistoryRecord>(1);

                                for (long 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);
        }
示例#3
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();
                            }
                        }
                    }
                }
            }
        }
示例#4
0
        internal static void AddRecords(List <HistoryRecord> Records)
        {
            lock (_syncRoot)
            {
                using (FileStream dataIndexStream = new FileStream(GetFile(true, true), FileMode.OpenOrCreate))
                {
                    using (FileStream 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 (FileStream dateIndexStream = new FileStream(GetFile(false, true), FileMode.OpenOrCreate))
                        {
                            using (FileStream 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();
                            }
                        }
                    }
                }
            }
        }