From 547920cb9c0ccee61d14980be5e629c9340f5d79 Mon Sep 17 00:00:00 2001 From: Candy Date: Fri, 16 Oct 2020 17:15:24 +0800 Subject: [PATCH] =?UTF-8?q?BUG=20=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Common/Cdy.Tag/His/CompressUnitManager2.cs | 10 + .../HisQuery/TimeFile/DataFileInfo.cs | 104 ++---- .../His/Compress/CompressEnginer2.cs | 1 + .../DBRuntime/His/Compress/CompressMemory2.cs | 27 +- RunTime/DBRuntime/His/HisEnginer2.cs | 209 ++--------- RunTime/DBRuntime/His/SeriseEnginer2.cs | 327 +++++++++--------- 6 files changed, 262 insertions(+), 416 deletions(-) diff --git a/Common/Cdy.Tag/His/CompressUnitManager2.cs b/Common/Cdy.Tag/His/CompressUnitManager2.cs index 1ee5d58..36f891a 100644 --- a/Common/Cdy.Tag/His/CompressUnitManager2.cs +++ b/Common/Cdy.Tag/His/CompressUnitManager2.cs @@ -49,6 +49,16 @@ namespace Cdy.Tag #endregion ...Constructor... #region ... Properties ... + /// + /// + /// + public Dictionary CompressUnit + { + get + { + return mCompressUnit; + } + } #endregion ...Properties... diff --git a/RunTime/DBHisData/HisQuery/TimeFile/DataFileInfo.cs b/RunTime/DBHisData/HisQuery/TimeFile/DataFileInfo.cs index f20504b..6855ed2 100644 --- a/RunTime/DBHisData/HisQuery/TimeFile/DataFileInfo.cs +++ b/RunTime/DBHisData/HisQuery/TimeFile/DataFileInfo.cs @@ -170,87 +170,49 @@ namespace Cdy.Tag { return; } - - do + try { - //读取数据区时间 - time = ss.ReadDateTime(offset + 16); - - if (time == DateTime.MinValue) + do { - tmp = time; - continue; - } + //读取数据区时间 + time = ss.ReadDateTime(offset + 16); - long oset = offset; - //读取下个区域位置 - offset = ss.ReadLong(offset + 8); + if (time == DateTime.MinValue) + { + tmp = time; + continue; + } - if (offset > 0) - { - var dt2 = ss.ReadDateTime(offset + 16); - mTimeOffsets.Add(time, new Tuple(dt2 - time, oset, dt2)); - tmp = dt2; + long oset = offset; + //读取下个区域位置 + offset = ss.ReadLong(offset + 8); + + if (offset > 0) + { + var dt2 = ss.ReadDateTime(offset + 16); + mTimeOffsets.Add(time, new Tuple(dt2 - time, oset, dt2)); + tmp = dt2; + } + else + { + var tspan = StartTime + Duration - time; + if (tspan.TotalMilliseconds > 0) + mTimeOffsets.Add(time, new Tuple(tspan, oset, time + tspan)); + tmp = time + tspan; + } + mLastProcessOffset = oset; } - else + while (offset > 0); + + if (mLastTime <= fileTime) { - var tspan = StartTime + Duration - time; - if (tspan.TotalMilliseconds > 0) - mTimeOffsets.Add(time, new Tuple(tspan, oset, time + tspan)); - tmp = time + tspan; + mLastTime = tmp; } - mLastProcessOffset = oset; } - while (offset > 0); - - if (mLastTime <= fileTime) + catch { - mLastTime = tmp; - } - //if (mLastProcessOffset == -1) - //{ - - //} - //else - //{ - // offset = mLastProcessOffset; - // //读取数据区时间 - // time = ss.ReadDateTime(offset + 16); - // long oset = offset; - // //读取下个区域位置 - // offset = ss.ReadLong(offset + 8); - - // if (offset != 0) - // { - // var dt2 = ss.ReadDateTime(offset + 16); - // if (mTimeOffsets.ContainsKey(time)) - // { - // mTimeOffsets[time] = new Tuple(dt2 - time, oset, dt2); - // } - // else - // { - // mTimeOffsets.Add(time, new Tuple(dt2 - time, oset, dt2)); - // } - // tmp = dt2; - // } - // else - // { - // var tspan = StartTime + Duration - time; - // if (tspan.TotalMilliseconds > 0) - // { - // if (mTimeOffsets.ContainsKey(time)) - // { - // mTimeOffsets[time] = new Tuple(tspan, oset, time + tspan); - // } - // else - // { - // mTimeOffsets.Add(time, new Tuple(tspan, oset, time + tspan)); - // } - // } - // tmp = time + tspan; - // } - //} + } mInited = true; } diff --git a/RunTime/DBRuntime/His/Compress/CompressEnginer2.cs b/RunTime/DBRuntime/His/Compress/CompressEnginer2.cs index cd1926d..600b765 100644 --- a/RunTime/DBRuntime/His/Compress/CompressEnginer2.cs +++ b/RunTime/DBRuntime/His/Compress/CompressEnginer2.cs @@ -304,6 +304,7 @@ namespace Cdy.Tag sm.Clear(); sm.MakeMemoryNoBusy(); + System.Threading.Tasks.Parallel.ForEach(mTargetMemorys.Where(e => e.Value.HasManualCompressItems), (mm) => { ThreadHelper.AssignToCPU(CPUAssignHelper.Helper.CPUArray2); diff --git a/RunTime/DBRuntime/His/Compress/CompressMemory2.cs b/RunTime/DBRuntime/His/Compress/CompressMemory2.cs index ae83fd0..8b3738c 100644 --- a/RunTime/DBRuntime/His/Compress/CompressMemory2.cs +++ b/RunTime/DBRuntime/His/Compress/CompressMemory2.cs @@ -207,17 +207,30 @@ namespace Cdy.Tag { mTagIds.Clear(); long lsize = 0; + + //var tagserver = ServiceLocator.Locator.Resolve(); + + //var tags = tagserver.ListAllTags().Where(e => e.Id >= Id * TagCountPerMemory && e.Id < (Id + 1) * TagCountPerMemory).OrderBy(e => e.Id); + + foreach(var vv in CompressUnitManager2.Manager.CompressUnit) + { + mCompressCach.Add(vv.Key, vv.Value.Clone()); + } + + //foreach(var vv in tags) + //{ + // var cpt = vv.CompressType; + // if (!mCompressCach.ContainsKey(cpt)) + // { + // mCompressCach.Add(cpt, CompressUnitManager2.Manager.GetCompressQuick(cpt).Clone()); + // } + //} + foreach (var vv in sourceM.TagAddress.Where(e => e.Key >= Id * TagCountPerMemory && e.Key < (Id + 1) * TagCountPerMemory)) { mTagIds.Add(vv.Key); dtmp.Add(vv.Key, 0); - var cpt = mHisTagService.GetHisTag(vv.Key).CompressType; - if (!mCompressCach.ContainsKey(cpt)) - { - mCompressCach.Add(cpt, CompressUnitManager2.Manager.GetCompressQuick(cpt).Clone()); - } - if (vv.Value != null) - lsize += vv.Value.Length; + lsize += vv.Value.Length; } this.ReAlloc(HeadSize + (long)(lsize*1.2)); diff --git a/RunTime/DBRuntime/His/HisEnginer2.cs b/RunTime/DBRuntime/His/HisEnginer2.cs index b0704c9..f41fe80 100644 --- a/RunTime/DBRuntime/His/HisEnginer2.cs +++ b/RunTime/DBRuntime/His/HisEnginer2.cs @@ -848,14 +848,14 @@ namespace Cdy.Tag cachHeadSize += css; } - else - { - mMergeMemory1.AddTagAddress(vv.Value.Id, null); - mMergeMemory2.AddTagAddress(vv.Value.Id, null); - - mCachMemory1.AddTagAddress(vv.Value.Id, null); - mCachMemory2.AddTagAddress(vv.Value.Id, null); - } + //else + //{ + // mMergeMemory1.AddTagAddress(vv.Value.Id, null); + // mMergeMemory2.AddTagAddress(vv.Value.Id, null); + + // mCachMemory1.AddTagAddress(vv.Value.Id, null); + // mCachMemory2.AddTagAddress(vv.Value.Id, null); + //} } @@ -1054,14 +1054,18 @@ namespace Cdy.Tag Stopwatch sw = new Stopwatch(); sw.Start(); - foreach (var tag in mHisTags) + + //foreach (var tag in mHisTags) + foreach(var vv in mcc.TagAddress) { - var taddrs = mCurrentMergeMemory.TagAddress[tag.Value.Id]; + var tag = mHisTags[vv.Key]; + var taddrs = mCurrentMergeMemory.TagAddress[tag.Id]; - var saddrs = mcc.TagAddress[tag.Value.Id]; + //var saddrs = mcc.TagAddress[tag.Value.Id]; + var saddrs = vv.Value; // - if (taddrs == null || saddrs == null) continue; + //if (taddrs == null || saddrs == null) continue; //拷贝时间 var dlen = saddrs.ValueAddress; @@ -1071,12 +1075,12 @@ namespace Cdy.Tag //拷贝数值 dlen = saddrs.QualityAddress - saddrs.ValueAddress; - vtimeaddr = taddrs.ValueAddress + dlen * count + tag.Value.SizeOfValue; + vtimeaddr = taddrs.ValueAddress + dlen * count + tag.SizeOfValue; saddrs.CopyTo(taddrs, saddrs.ValueAddress, vtimeaddr, dlen); //拷贝质量戳 - dlen = tag.Value.DataSize - saddrs.QualityAddress; + dlen = tag.DataSize - saddrs.QualityAddress; vtimeaddr = taddrs.QualityAddress + dlen * count + 1; saddrs.CopyTo(taddrs, saddrs.QualityAddress, vtimeaddr, dlen); @@ -1166,6 +1170,7 @@ namespace Cdy.Tag sw.Start(); foreach(var vv in mHisTags) { + if(vv.Value.Type != RecordType.Driver) vv.Value.Snape(); } sw.Stop(); @@ -1314,9 +1319,12 @@ namespace Cdy.Tag mNeedSnapAllTag = true; mForceSubmiteToCompress = true; mIsClosed = true; + + SaveManualCachData(); + SubmiteMemory(DateTime.Now); while (!mMegerProcessIsClosed) Thread.Sleep(1); - SaveManualCachData(); + } /// @@ -1619,8 +1627,8 @@ namespace Cdy.Tag isNeedSubmite = true; } } - if(isNeedSubmite) - ServiceLocator.Locator.Resolve().SubmitManualToCompress(); + if (isNeedSubmite) + ServiceLocator.Locator.Resolve().SubmitManualToCompress(); } return true; @@ -1641,173 +1649,6 @@ namespace Cdy.Tag private bool ManualRecordHisValues(long id, Cdy.Tag.TagValue value, int timeUnit = 100) { return ManualRecordHisValues(id, value.Time, value.Value, value.Quality, timeUnit); - //if (mIsClosed) return false; - - //int valueOffset, qulityOffset = 0; - - //DateTime mLastTime = DateTime.MinValue; - - //SortedDictionary datacach; - - //if (mHisTags.ContainsKey(id) && mHisTags[id].Type == RecordType.Manual) - //{ - // if (mManualHisDataCach.ContainsKey(id)) - // { - // datacach = mManualHisDataCach[id]; - // } - // else - // { - // datacach = new SortedDictionary(); - // mManualHisDataCach.Add(id, datacach); - // } - - // var tag = mHisTags[id]; - // ManualHisDataMemoryBlock hb = null; - - // var vdata = value.Time.Date; - // var mms = (int)(value.Time.Subtract(vdata).TotalSeconds / MergeMemoryTime); - // var time = vdata.AddSeconds(mms * MergeMemoryTime); - // if (datacach.ContainsKey(time)) - // { - // hb = datacach[time]; - // } - // else - // { - // var css = CalCachDatablockSize(tag.TagType, 0, MergeMemoryTime * 1000 / timeUnit, out valueOffset, out qulityOffset); - // hb = ManualHisDataMemoryBlockPool.Pool.Get(css); - // hb.Time = time; - // hb.MaxCount = MergeMemoryTime * 1000 / timeUnit; - // hb.TimeUnit = timeUnit; - // hb.TimeLen = 4; - // hb.TimerAddress = 0; - // hb.ValueAddress = valueOffset; - // hb.QualityAddress = qulityOffset; - // hb.Id = (int)id; - // hb.CurrentCount = 0; - - // datacach.Add(time, hb); - // } - // mLastTime = time; - - // if (hb.CurrentCount < hb.MaxCount && value.Time > hb.EndTime) - // { - // hb.Lock(); - // var vtime = (int)((value.Time - hb.Time).TotalMilliseconds / timeUnit); - // //写入时间戳 - // hb.WriteInt(hb.TimerAddress + hb.CurrentCount * 4, vtime); - // switch (tag.TagType) - // { - // case TagType.Bool: - // hb.WriteByteDirect(hb.ValueAddress + hb.CurrentCount * tag.SizeOfValue, Convert.ToByte(Convert.ToBoolean(value.Value))); - // break; - // case TagType.Byte: - // hb.WriteByteDirect(hb.ValueAddress + hb.CurrentCount * tag.SizeOfValue, Convert.ToByte(value.Value)); - // break; - // case TagType.Short: - // hb.WriteShortDirect(hb.ValueAddress + hb.CurrentCount * tag.SizeOfValue, Convert.ToInt16(value.Value)); - // break; - // case TagType.UShort: - // hb.WriteUShortDirect(hb.ValueAddress + hb.CurrentCount * tag.SizeOfValue, Convert.ToUInt16(value.Value)); - // break; - // case TagType.Int: - // hb.WriteIntDirect(hb.ValueAddress + hb.CurrentCount * tag.SizeOfValue, Convert.ToInt32(value.Value)); - // break; - // case TagType.UInt: - // hb.WriteUIntDirect(hb.ValueAddress + hb.CurrentCount * tag.SizeOfValue, Convert.ToUInt32(value.Value)); - // break; - // case TagType.Long: - // hb.WriteLongDirect(hb.ValueAddress + hb.CurrentCount * tag.SizeOfValue, Convert.ToInt64(value.Value)); - // break; - // case TagType.ULong: - // hb.WriteULongDirect(hb.ValueAddress + hb.CurrentCount * tag.SizeOfValue, Convert.ToUInt64(value.Value)); - // break; - // case TagType.Float: - // hb.WriteFloatDirect(hb.ValueAddress + hb.CurrentCount * tag.SizeOfValue, Convert.ToSingle(value.Value)); - // break; - // case TagType.Double: - // hb.WriteDoubleDirect(hb.ValueAddress + hb.CurrentCount * tag.SizeOfValue, Convert.ToDouble(value.Value)); - // break; - // case TagType.String: - // hb.WriteStringDirect(hb.ValueAddress + hb.CurrentCount * tag.SizeOfValue, Convert.ToString(value.Value), Encoding.Unicode); - // break; - // case TagType.DateTime: - // hb.WriteDatetime(hb.ValueAddress + hb.CurrentCount * tag.SizeOfValue, Convert.ToDateTime(value.Value)); - // break; - // case TagType.UIntPoint: - // UIntPointData data = (UIntPointData)value.Value; - // hb.WriteUIntDirect(hb.ValueAddress + hb.CurrentCount * tag.SizeOfValue, data.X); - // hb.WriteUIntDirect(hb.ValueAddress + hb.CurrentCount * tag.SizeOfValue + 4, data.Y); - // break; - // case TagType.IntPoint: - // IntPointData idata = (IntPointData)value.Value; - // hb.WriteIntDirect(hb.ValueAddress + hb.CurrentCount * tag.SizeOfValue, idata.X); - // hb.WriteIntDirect(hb.ValueAddress + hb.CurrentCount * tag.SizeOfValue + 4, idata.Y); - // break; - // case TagType.UIntPoint3: - // UIntPoint3Data udata3 = (UIntPoint3Data)value.Value; - // hb.WriteUIntDirect(hb.ValueAddress + hb.CurrentCount * tag.SizeOfValue, udata3.X); - // hb.WriteUIntDirect(hb.ValueAddress + hb.CurrentCount * tag.SizeOfValue + 4, udata3.Y); - // hb.WriteUIntDirect(hb.ValueAddress + hb.CurrentCount * tag.SizeOfValue + 8, udata3.Z); - // break; - // case TagType.IntPoint3: - // IntPoint3Data idata3 = (IntPoint3Data)value.Value; - // hb.WriteIntDirect(hb.ValueAddress + hb.CurrentCount * tag.SizeOfValue, idata3.X); - // hb.WriteIntDirect(hb.ValueAddress + hb.CurrentCount * tag.SizeOfValue + 4, idata3.Y); - // hb.WriteIntDirect(hb.ValueAddress + hb.CurrentCount * tag.SizeOfValue + 8, idata3.Z); - // break; - - // case TagType.ULongPoint: - // ULongPointData udata = (ULongPointData)value.Value; - // hb.WriteULongDirect(hb.ValueAddress + hb.CurrentCount * tag.SizeOfValue, udata.X); - // hb.WriteULongDirect(hb.ValueAddress + hb.CurrentCount * tag.SizeOfValue + 8, udata.Y); - // break; - // case TagType.LongPoint: - // LongPointData lidata = (LongPointData)value.Value; - // hb.WriteLongDirect(hb.ValueAddress + hb.CurrentCount * tag.SizeOfValue, lidata.X); - // hb.WriteLongDirect(hb.ValueAddress + hb.CurrentCount * tag.SizeOfValue + 8, lidata.Y); - // break; - // case TagType.ULongPoint3: - // ULongPoint3Data ludata3 = (ULongPoint3Data)value.Value; - // hb.WriteULongDirect(hb.ValueAddress + hb.CurrentCount * tag.SizeOfValue, ludata3.X); - // hb.WriteULongDirect(hb.ValueAddress + hb.CurrentCount * tag.SizeOfValue + 16, ludata3.Y); - // hb.WriteULongDirect(hb.ValueAddress + hb.CurrentCount * tag.SizeOfValue + 24, ludata3.Z); - // break; - // case TagType.LongPoint3: - // LongPoint3Data lidata3 = (LongPoint3Data)value.Value; - // hb.WriteLongDirect(hb.ValueAddress + hb.CurrentCount * tag.SizeOfValue, lidata3.X); - // hb.WriteLongDirect(hb.ValueAddress + hb.CurrentCount * tag.SizeOfValue + 16, lidata3.Y); - // hb.WriteLongDirect(hb.ValueAddress + hb.CurrentCount * tag.SizeOfValue + 24, lidata3.Z); - // break; - // } - // hb.WriteInt(hb.QualityAddress + hb.CurrentCount, value.Quality); - // hb.EndTime = value.Time; - // hb.CurrentCount++; - // hb.Relase(); - - // HisDataMemoryQueryService.Service.RegistorManual(id, hb.Time, hb.EndTime, hb); - - // } - - // bool isNeedSubmite = false; - - // foreach (var vv in datacach.ToArray()) - // { - // if (vv.Key < mLastTime || vv.Value.CurrentCount >= vv.Value.MaxCount) - // { - // ServiceLocator.Locator.Resolve().RequestManualToCompress(vv.Value); - // datacach.Remove(vv.Key); - // isNeedSubmite = true; - // } - // } - // if (isNeedSubmite) - // ServiceLocator.Locator.Resolve().SubmitManualToCompress(); - - // return true; - //} - //else - //{ - // return false; - //} } /// diff --git a/RunTime/DBRuntime/His/SeriseEnginer2.cs b/RunTime/DBRuntime/His/SeriseEnginer2.cs index 7050981..5fefd1a 100644 --- a/RunTime/DBRuntime/His/SeriseEnginer2.cs +++ b/RunTime/DBRuntime/His/SeriseEnginer2.cs @@ -262,11 +262,9 @@ namespace Cdy.Tag if (id >= vv.Value.IdStart && id < vv.Value.IdEnd) { vv.Value.AppendManualSeriseFile(id, data); - //mManualRequestSaveCount++; break; } } - //if (mManualRequestSaveCount > 10) RequestToSave(); } /// /// @@ -313,9 +311,6 @@ namespace Cdy.Tag sw.Stop(); LoggerService.Service.Info("SeriseEnginer", ">>>>>>>>>完成执行存储>>>>>>> ElapsedMilliseconds:" + sw.ElapsedMilliseconds, ConsoleColor.Cyan); //#endif - - if (mIsClosed) - break; } closedEvent.Set(); } @@ -435,9 +430,11 @@ namespace Cdy.Tag private List mTagIdsCach; - private Dictionary> mPointerCach = new Dictionary>(); + //private Dictionary> mPointerCach = new Dictionary>(); + + private Dictionary> mManualHisDataCach = new Dictionary>(); - private Dictionary> mManualHisDataCach = new Dictionary>(); + private Dictionary mTagIndexCach = new Dictionary(); private int mId = 0; @@ -801,10 +798,10 @@ namespace Cdy.Tag } - private Dictionary GetDataRegionHeadPoint(string sfile, SortedDictionary ids, DateTime time, out DataFileSeriserbase mFileReader) + private Dictionary> GetDataRegionHeadPoint(string sfile, SortedDictionary> ids, DateTime time, out DataFileSeriserbase mFileReader) { - Dictionary re = new Dictionary(); + Dictionary> re = new Dictionary>(); DataFileSeriserbase dfs; @@ -853,32 +850,43 @@ namespace Cdy.Tag foreach (var vv in ids) { - long ltmp = 0; - //计算本次更新对应的指针区域的起始地址 - var fsh = (vv.Value.Hour / FileDuration) * FileDuration; - int bid = ((vv.Value.Hour - fsh) * 60 + time.Minute) / BlockDuration; - - if (mLastRegionStartTime == DateTime.MaxValue || vv.Value < mLastRegionStartTime || vv.Value > mLastRegionEndTime) - { - regionOffset = SearchDataRegionToDatetime(dfs, vv.Value,out mLastRegionStartTime,out mLastRegionEndTime); - ltmp = regionOffset; - } - else + foreach (var vvv in vv.Value) { - ltmp = regionOffset; - } + long ltmp = 0; + //计算本次更新对应的指针区域的起始地址 + var fsh = (vvv.Hour / FileDuration) * FileDuration; + int bid = ((vvv.Hour - fsh) * 60 + time.Minute) / BlockDuration; - if (ltmp < 0) - { - LoggerService.Service.Warn("SeriseEnginer2", "不允许修改本次运行之前时间的历史记录!"); - return re; - } + if (mLastRegionStartTime == DateTime.MaxValue || vvv < mLastRegionStartTime || vvv > mLastRegionEndTime) + { + regionOffset = SearchDataRegionToDatetime(dfs, vvv, out mLastRegionStartTime, out mLastRegionEndTime); + ltmp = regionOffset; + } + else + { + ltmp = regionOffset; + } - var icount = mTagIdsCach.IndexOf(vv.Key); + if (ltmp < 0) + { + LoggerService.Service.Warn("SeriseEnginer2", "不允许修改本次运行之前时间的历史记录!"); + return re; + } - ltmp += mDataRegionHeadSize + mTagCount * 8 * bid + icount * 8; + //var icount = mTagIdsCach.IndexOf(vv.Key); + var icount = mTagIndexCach[vv.Key]; - re.Add(vv.Key,ltmp); + ltmp += mDataRegionHeadSize + mTagCount * 8 * bid + icount * 8; + if (re.ContainsKey(vv.Key)) + { + re[vv.Key].Add(ltmp); + } + else + { + re.Add(vv.Key,new List() { ltmp }); + } + // re.Add(vv.Key, ltmp); + } } //LoggerService.Service.Debug("SeriseEnginer", "DataRegion Pointer:" + ltmp + ",mDataRegionHeadSize:" + mDataRegionHeadSize + ",BlockIndex:" + bid + " tag index:" + icount); @@ -972,17 +980,12 @@ namespace Cdy.Tag { if (mManualHisDataCach.ContainsKey(sfile)) { - if (!mManualHisDataCach[sfile].ContainsKey(id)) - mManualHisDataCach[sfile].Add(id, datablock); - else - { - LoggerService.Service.Warn("SeriseEnginer", "数据存储出现阻塞:"+id); - } + mManualHisDataCach[sfile].Enqueue(datablock); } else { - Dictionary blocks = new Dictionary(); - blocks.Add(id, datablock); + Queue blocks = new Queue(); + blocks.Enqueue(datablock); mManualHisDataCach.Add(sfile, blocks); } } @@ -996,8 +999,8 @@ namespace Cdy.Tag string oldFile = string.Empty; DataFileSeriserbase mwriter; - Dictionary mHeadAddress = new Dictionary(); - Dictionary mHeadValue = new Dictionary(); + Dictionary> mHeadAddress = new Dictionary>(); + Dictionary> mHeadValue = new Dictionary>(); while(mManualHisDataCach.Count>0) { @@ -1007,31 +1010,40 @@ namespace Cdy.Tag mManualHisDataCach.Remove(vv.Key); } - //Stopwatch sw = new Stopwatch(); - //sw.Start(); + Stopwatch sw = new Stopwatch(); + sw.Start(); - //LoggerService.Service.Info("SeriseEnginer", "SeriseFileItem" + this.Id + " 开始执行存储,数据块:" + vv.Value.Count, ConsoleColor.Cyan); + //LoggerService.Service.Info("SeriseEnginer", "SeriseFileItem" + this.Id + " 开始执行存储,数据块:" + vv.Value.Count+" 剩余:"+mManualHisDataCach.Count, ConsoleColor.Cyan); - SortedDictionary times = new SortedDictionary(); + SortedDictionary> times = new SortedDictionary>(); DateTime maxTime = DateTime.MinValue; DateTime mLastModifyTime = DateTime.MinValue; foreach(var vvv in vv.Value) { - DateTime time = vvv.Value.ReadDateTime(4); + int id = vvv.ReadInt(0); + DateTime time = vvv.ReadDateTime(4); - DateTime endTime = vvv.Value.ReadDateTime(12); + DateTime endTime = vvv.ReadDateTime(12); - mTagCount = vvv.Value.ReadInt(24);//变量个数 + //mTagCount = vvv.ReadInt(24);//变量个数 - times.Add(vvv.Key, time); + if(times.ContainsKey(id)) + { + times[id].Add(time); + } + else + { + times.Add(id, new List() { time }); + } + maxTime = time > maxTime ? time : maxTime; mLastModifyTime = endTime > mLastModifyTime ? endTime : mLastModifyTime; } mHeadAddress = GetDataRegionHeadPoint(vv.Key, times, maxTime, out mwriter); - //long ltmp = sw.ElapsedMilliseconds; + long ltmp = sw.ElapsedMilliseconds; mHeadValue.Clear(); mwriter.GoToEnd(); @@ -1040,21 +1052,34 @@ namespace Cdy.Tag //写入数据,同时获取数据块地址 foreach (var vvv in vv.Value) { - int size = vvv.Value.ReadInt(20); - mHeadValue.Add(vvv.Key, vpointer); - vvv.Value.WriteToStream(mwriter.GetStream(), 28, size - 28);//直接拷贝数据块 + int id = vvv.ReadInt(0); + int size = vvv.ReadInt(20); + if(mHeadValue.ContainsKey(id)) + { + mHeadValue[id].Add(vpointer); + } + else + { + mHeadValue.Add(id, new List() { vpointer }); + } + // mHeadValue.Add(vvv.Key, vpointer); + vvv.WriteToStream(mwriter.GetStream(), 28, size - 28);//直接拷贝数据块 vpointer += (size - 28); } - //long ltmp2 = sw.ElapsedMilliseconds; + long ltmp2 = sw.ElapsedMilliseconds; //更新数据块指针 foreach (var hd in mHeadAddress) { - mwriter.Write(mHeadValue[hd.Key], hd.Value); + for(int i=0;i mCurrentTime) - { - if (mPointerCach.ContainsKey(vtime)) - { - var dd = mPointerCach[vtime]; - if (dd.ContainsKey(tm.Key)) - { - dd[tm.Key] = mHeadValue[tm.Key]; - } - else - { - dd.Add(tm.Key, mHeadValue[tm.Key]); - } - } - else - { - Dictionary dtmp = new Dictionary(); - dtmp.Add(tm.Key, mHeadValue[tm.Key]); - mPointerCach.Add(vtime, dtmp); - } - } - } - } - - //LoggerService.Service.Info("SeriseEnginer", "SeriseFileItem" + this.Id + " 完成存储,数据块:" + vv.Value.Count + " ReadHeadPoint:" + ltmp + " WriteData:" + (ltmp2 - ltmp) + " UpdateHead:" + (ltmp3 - ltmp2), ConsoleColor.Cyan); + LoggerService.Service.Info("SeriseEnginer", "SeriseFileItem" + this.Id + " 完成存储,数据块:" + vv.Value.Count + " ReadHeadPoint:" + ltmp + " WriteData:" + (ltmp2 - ltmp) + " UpdateHead:" + (ltmp3 - ltmp2), ConsoleColor.Cyan); } @@ -1131,7 +1125,7 @@ namespace Cdy.Tag DateTime time = datablock.ReadDateTime(4); DateTime endTime = datablock.ReadDateTime(12); int size = datablock.ReadInt(20); - mTagCount = datablock.ReadInt(24);//变量个数 + //mTagCount = datablock.ReadInt(24);//变量个数 var heads = GetDataRegionHeadPoint(id, time, out mwriter); @@ -1160,34 +1154,34 @@ namespace Cdy.Tag datablock.MakeMemoryNoBusy(); MarshalMemoryBlockPool.Pool.Release(datablock); - var vtime = FormateTime(time); - - //如果时间大于上次自动存储的时间,则需要将地址指针记录下来,等到下次自动存储内容更新时,将当前更新的数据的指针区同步过去 - //仿制被覆盖过去 - lock (mPointerCach) - { - if (time > mCurrentTime) - { - if (mPointerCach.ContainsKey(vtime)) - { - var dd = mPointerCach[vtime]; - if (dd.ContainsKey(id)) - { - dd[id] = vpointer; - } - else - { - dd.Add(id, vpointer); - } - } - else - { - Dictionary dtmp = new Dictionary(); - dtmp.Add(id, vpointer); - mPointerCach.Add(vtime, dtmp); - } - } - } + //var vtime = FormateTime(time); + + ////如果时间大于上次自动存储的时间,则需要将地址指针记录下来,等到下次自动存储内容更新时,将当前更新的数据的指针区同步过去 + ////仿制被覆盖过去 + //lock (mPointerCach) + //{ + // if (time > mCurrentTime) + // { + // if (mPointerCach.ContainsKey(vtime)) + // { + // var dd = mPointerCach[vtime]; + // if (dd.ContainsKey(id)) + // { + // dd[id] = vpointer; + // } + // else + // { + // dd.Add(id, vpointer); + // } + // } + // else + // { + // Dictionary dtmp = new Dictionary(); + // dtmp.Add(id, vpointer); + // mPointerCach.Add(vtime, dtmp); + // } + // } + //} } } @@ -1211,7 +1205,9 @@ namespace Cdy.Tag private int CalTagIdsSize() { if (mTagIdMemoryCach != null) mTagIdMemoryCach.Dispose(); - mTagIdsCach = ServiceLocator.Locator.Resolve().ListAllTags().Where(e => e.Id >= Id * TagCountOneFile && e.Id < (Id + 1) * TagCountOneFile).OrderBy(e => e.Id).Select(e => e.Id).ToList(); + //mTagIdsCach = ServiceLocator.Locator.Resolve().ListAllTags().Where(e => e.Id >= Id * TagCountOneFile && e.Id < (Id + 1) * TagCountOneFile).OrderBy(e => e.Id).Select(e => e.Id).ToList(); + + mTagIndexCach.Clear(); mTagIdSum = 0; @@ -1221,9 +1217,13 @@ namespace Cdy.Tag int preids = mTagIdsCach[0]; mTagIdSum += preids; mTagIdMemoryCach.WriteInt32(preids); + + mTagIndexCach.Add(preids, 0); + for (int i = 1; i < mTagIdsCach.Count; i++) { var id = mTagIdsCach[i]; + mTagIndexCach.Add(id, i); mTagIdMemoryCach.WriteInt32(id - preids); mTagIdSum += id; preids = id; @@ -1242,13 +1242,19 @@ namespace Cdy.Tag var vv = ServiceLocator.Locator.Resolve(); var tags = vv.ListAllTags().Where(e => e.Id >= Id * TagCountOneFile && e.Id < (Id + 1) * TagCountOneFile).OrderBy(e => e.Id); + mTagIdsCach = tags.Select(e => e.Id).ToList(); + if (mBlockPointMemory != null) mBlockPointMemory.Dispose(); mBlockPointMemory = new MemoryBlock(tags.Count() * 8, 4 * 1024 * 1024); mBlockPointMemory.Clear(); - LoggerService.Service.Info("SeriseEnginer", "Cal BlockPointMemory memory size:" + (mBlockPointMemory.AllocSize) / 1024.0 / 1024 + "M", ConsoleColor.Cyan); CalTagIdsSize(); + + mTagCount = mTagIdsCach.Count(); + + LoggerService.Service.Info("SeriseEnginer", "Cal BlockPointMemory memory size:" + (mBlockPointMemory.AllocSize) / 1024.0 / 1024 + "M", ConsoleColor.Cyan); + } /// @@ -1356,7 +1362,7 @@ namespace Cdy.Tag //数据大小 var datasize = mProcessMemory.ReadInt(dataOffset); var count = mProcessMemory.ReadInt(dataOffset + 4);//变量个数 - mTagCount = count; + //mTagCount = count; mCurrentTime = time; var ltmp = sw.ElapsedMilliseconds; @@ -1389,20 +1395,23 @@ namespace Cdy.Tag mBlockPointMemory.CheckAndResize(mTagCount * 8); mBlockPointMemory.Clear(); - var vtime = FormateTime(time); - Dictionary timecach; - lock (mPointerCach) - { - if (mPointerCach.ContainsKey(time)) - { - timecach = mPointerCach[time]; - } - else - { - timecach = new Dictionary(); - } - } + Dictionary mHeadValues = new Dictionary(); + + var vtime = FormateTime(time); + //Dictionary timecach; + + //lock (mPointerCach) + //{ + // if (mPointerCach.ContainsKey(time)) + // { + // timecach = mPointerCach[time]; + // } + // else + // { + // timecach = new Dictionary(); + // } + //} //更新数据块指针 for (int i = 0; i < count; i++) @@ -1413,28 +1422,30 @@ namespace Cdy.Tag offset += 8; if (id > -1) { - //如果之前通过,手动记录已经更新了,则需要同步指针 - if (timecach.ContainsKey(id)) - { - mBlockPointMemory.WriteLong(i * 8, timecach[id]); - } - else - { - mBlockPointMemory.WriteLong(i * 8, addr); - } + ////如果之前通过,手动记录已经更新了,则需要同步指针 + //if (timecach.ContainsKey(id)) + //{ + // mBlockPointMemory.WriteLong(i * 8, timecach[id]); + //} + //else + //{ + //mBlockPointMemory.WriteLong(i * 8, addr); + //} + + mHeadValues.Add(id, addr); } } - lock (mPointerCach) - { - foreach (var vv in mPointerCach.Keys.ToArray()) - { - if (vv <= mCurrentTime) - { - mPointerCach.Remove(vv); - } - } - } + //lock (mPointerCach) + //{ + // foreach (var vv in mPointerCach.Keys.ToArray()) + // { + // if (vv <= mCurrentTime) + // { + // mPointerCach.Remove(vv); + // } + // } + //} //计算本次更新对应的指针区域的起始地址 @@ -1444,14 +1455,22 @@ namespace Cdy.Tag //计算出本次更新的头地址地址 var pointAddr = mBlockPointOffset + count * 8 * bid; + var ltmp3 = sw.ElapsedMilliseconds; mFileWriter.GoToEnd(); //long lpp = mFileWriter.CurrentPostion; mProcessMemory.WriteToStream(mFileWriter.GetStream(), start, datasize);//直接拷贝数据块 + //写入指针头部区域 - mFileWriter.Write(mBlockPointMemory.Buffers, pointAddr, 0, (int)mBlockPointMemory.AllocSize); + // mFileWriter.Write(mBlockPointMemory.Buffers, pointAddr, 0, (int)mBlockPointMemory.AllocSize); + + foreach(var vv in mHeadValues) + { + mFileWriter.Write(pointAddr + mTagIndexCach[vv.Key] * 8, vv.Value); + } + Flush(); -- GitLab