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(); } } } } } }
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(); } } } } } }