/// <summary> /// 执行存储到磁盘 /// </summary> public void SaveToFile(MarshalMemoryBlock mProcessMemory, long dataOffset, DateTime time) { /* * 1. 检查变量ID是否变动,如果变动则重新记录变量的ID列表 * 2. 拷贝数据块 * 3. 更新数据块指针 */ //LoggerService.Service.Info("SeriseFileItem" + Id, "*********开始执行存储**********"); try { Stopwatch sw = new Stopwatch(); sw.Start(); var totalsize = mProcessMemory.ReadInt(dataOffset); var count = mProcessMemory.ReadInt(dataOffset + 4); mTagCount = count; mCurrentTime = time; //to do 计算变量信息是否改变 ////判断变量的ID列表是否被修改了 //for (int i = 0; i < count; i++) //{ // var id = mProcessMemory.ReadInt(offset); // if (!mIdAddrs.ContainsKey(id)) // { // mNeedUpdateTagHeads = true; // break; // } // offset += 8; //} var ltmp = sw.ElapsedMilliseconds; if (!CheckFile(time)) { return; } var ltmp2 = sw.ElapsedMilliseconds; long offset = 8 + dataOffset; long start = count * 8 + offset;//计算出数据起始地址 //LoggerService.Service.Info("SeriseFileItem" + Id, "开始更新指针区域"); var dataAddr = this.mFileWriter.GoToEnd().CurrentPostion; mBlockPointMemory.CheckAndResize(mTagCount * 8); mBlockPointMemory.Clear(); //更新BlockPoint for (int i = 0; i < count; i++) { var id = mProcessMemory.ReadInt(offset); var addr = mProcessMemory.ReadInt(offset + 4) - start + dataAddr; offset += 8; if (id > -1) { mBlockPointMemory.WriteLong(i * 8, addr); } } //StringBuilder sb = new StringBuilder(); //foreach (var vv in mBlockPointMemory.ToLongList()) //{ // sb.Append(vv + ","); //} //计算本次更新对应的指针区域的起始地址 FileStartHour = (time.Hour / FileDuration) * FileDuration; int bid = ((time.Hour - FileStartHour) * 60 + time.Minute) / BlockDuration; var pointAddr = mBlockPointOffset + count * 8 * bid; var ltmp3 = sw.ElapsedMilliseconds; //lock (mFileLocker) { mFileWriter.GoToEnd(); long lpp = mFileWriter.CurrentPostion; mProcessMemory.WriteToStream(mFileWriter.GetStream(), start, totalsize - start);//直接拷贝数据块 // LoggerService.Service.Info("SeriseFileItem", "数据写入地址:" + lpp + ",更新指针地址:" + pointAddr+" block index:"+bid+" tagcount:"+count+" block point Start Addr:"+ mBlockPointOffset+" point values:"+sb.ToString()); //this.mFileWriter.Append(mProcessMemory.Buffers, (int)start, (int)(totalsize - start)); mFileWriter.Write(mBlockPointMemory.Buffers, pointAddr, 0, (int)mBlockPointMemory.AllocSize); Flush(); } sw.Stop(); LoggerService.Service.Info("SeriseFileItem" + Id, "写入数据 " + mCurrentFileName + " 数据大小:" + ((totalsize - start) + mBlockPointMemory.AllocSize) / 1024.0 / 1024 + " m" + "其他脚本耗时:" + ltmp + "," + (ltmp2 - ltmp) + "," + (ltmp3 - ltmp2) + "存储耗时:" + (sw.ElapsedMilliseconds - ltmp3)); } catch (System.IO.IOException ex) { LoggerService.Service.Erro("SeriseEnginer" + Id, ex.Message); } //LoggerService.Service.Info("SeriseEnginer" + Id, ">>>>>>>>>完成执行存储>>>>>>>>>"); }
/// <summary> /// /// </summary> /// <typeparam name="T"></typeparam> /// <param name="source"></param> /// <param name="offset"></param> /// <param name="count"></param> /// <param name="emptyIds"></param> /// <returns></returns> protected override Memory <byte> CompressValues <T>(MarshalMemoryBlock source, long offset, int count, Queue <int> emptyIds) { var deadArea = this.Parameters.ContainsKey("DeadValue") ? this.Parameters["DeadValue"] : 0; var deadType = (int)(this.Parameters.ContainsKey("DeadType") ? this.Parameters["DeadType"] : 0); mMarshalMemory.Position = 0; mVarintMemory.Position = 0; bool isFirst = true; int ig = -1; emptyIds.TryDequeue(out ig); if (typeof(T) == typeof(byte)) { byte sval = 0; for (int i = 0; i < count; i++) { if (i != ig) { var id = source.ReadByte(offset + i); if (isFirst) { sval = id; mMarshalMemory.Write(id); isFirst = false; } else { if (CheckIsNeedRecord(sval, id, deadArea, deadType)) { mMarshalMemory.Write(id); sval = id; } } } else { if (emptyIds.Count > 0) { emptyIds.TryDequeue(out ig); } } } return(mMarshalMemory.StartMemory.AsMemory <byte>(0, (int)mMarshalMemory.Position)); } else if (typeof(T) == typeof(short)) { short sval = 0; for (int i = 0; i < count; i++) { if (i != ig) { var id = source.ReadShort(offset + i * 2); if (isFirst) { mVarintMemory.WriteSInt32(id); isFirst = false; sval = id; } else { if (CheckIsNeedRecord(sval, id, deadArea, deadType)) { mVarintMemory.WriteSInt32(id - sval); sval = id; } } } else { if (emptyIds.Count > 0) { emptyIds.TryDequeue(out ig); } } } } else if (typeof(T) == typeof(ushort)) { ushort sval = 0; for (int i = 0; i < count; i++) { if (i != ig) { var id = source.ReadUShort(offset + i * 2); if (isFirst) { mVarintMemory.WriteSInt32(id); isFirst = false; sval = id; } else { if (CheckIsNeedRecord(sval, id, deadArea, deadType)) { mVarintMemory.WriteSInt32(id - sval); sval = id; } } } else { if (emptyIds.Count > 0) { emptyIds.TryDequeue(out ig); } } } } else if (typeof(T) == typeof(int)) { int sval = 0; for (int i = 0; i < count; i++) { if (i != ig) { var id = source.ReadInt(offset + i * 4); if (isFirst) { mVarintMemory.WriteInt32(id); isFirst = false; sval = id; } else { if (CheckIsNeedRecord(sval, id, deadArea, deadType)) { mVarintMemory.WriteSInt32(id - sval); sval = id; } } } else { if (emptyIds.Count > 0) { emptyIds.TryDequeue(out ig); } } } } else if (typeof(T) == typeof(uint)) { uint sval = 0; for (int i = 0; i < count; i++) { if (i != ig) { var id = source.ReadUInt(offset + i * 4); if (isFirst) { mVarintMemory.WriteInt32(id); isFirst = false; sval = id; } else { if (CheckIsNeedRecord(sval, id, deadArea, deadType)) { mVarintMemory.WriteSInt32((int)(id - sval)); sval = id; } } } else { if (emptyIds.Count > 0) { emptyIds.TryDequeue(out ig); } } } } else if (typeof(T) == typeof(long)) { long sval = 0; for (int i = 0; i < count; i++) { if (i != ig) { var id = source.ReadLong(offset + i * 8); if (isFirst) { mVarintMemory.WriteInt64(id); isFirst = false; sval = id; } else { if (CheckIsNeedRecord(sval, id, deadArea, deadType)) { mVarintMemory.WriteSInt64((id - sval)); sval = id; } } } else { if (emptyIds.Count > 0) { emptyIds.TryDequeue(out ig); } } } } else if (typeof(T) == typeof(ulong)) { ulong sval = 0; for (int i = 0; i < count; i++) { if (i != ig) { var id = source.ReadULong(offset + i * 8); if (isFirst) { mVarintMemory.WriteInt64(id); isFirst = false; sval = id; } else { if (CheckIsNeedRecord(sval, id, deadArea, deadType)) { mVarintMemory.WriteSInt64((long)(id - sval)); sval = id; } } } else { if (emptyIds.Count > 0) { emptyIds.TryDequeue(out ig); } } } } else if (typeof(T) == typeof(double)) { double sval = 0; for (int i = 0; i < count; i++) { if (i != ig) { var id = source.ReadDouble(offset + i * 8); if (isFirst) { mMarshalMemory.Write(id); isFirst = false; sval = id; } else { if (CheckIsNeedRecord(sval, id, deadArea, deadType)) { mMarshalMemory.Write(id); sval = id; } } } else { if (emptyIds.Count > 0) { emptyIds.TryDequeue(out ig); } } } return(mMarshalMemory.StartMemory.AsMemory <byte>(0, (int)mMarshalMemory.Position)); } else if (typeof(T) == typeof(float)) { float sval = 0; for (int i = 0; i < count; i++) { if (i != ig) { var id = source.ReadFloat(offset + i * 4); if (isFirst) { mMarshalMemory.Write(id); isFirst = false; sval = id; } else { if (CheckIsNeedRecord(sval, id, deadArea, deadType)) { mMarshalMemory.Write(id); sval = id; } } } else { if (emptyIds.Count > 0) { emptyIds.TryDequeue(out ig); } } } return(mMarshalMemory.StartMemory.AsMemory <byte>(0, (int)mMarshalMemory.Position)); } return(mVarintMemory.Buffer.AsMemory <byte>(0, (int)mVarintMemory.Position)); }
/// <summary> /// /// </summary> /// <typeparam name="T"></typeparam> /// <param name="source"></param> /// <param name="offset"></param> /// <param name="count"></param> /// <param name="emptyIds"></param> /// <param name="mTimers"></param> /// <param name="usedTimerIndex"></param> /// <returns></returns> protected Memory <byte> CompressValues <T>(MarshalMemoryBlock source, long offset, int count, Queue <int> emptyIds, List <ushort> mTimers, out Queue <int> usedTimerIndex) { int ig = -1; emptyIds.TryDequeue(out ig); if (typeof(T) == typeof(byte)) { Dictionary <ushort, byte> mavaibleValues = new Dictionary <ushort, byte>(); for (int i = 0; i < count; i++) { if (i != ig) { var id = source.ReadByte(offset + i); mavaibleValues.Add(mTimers[i], id); } else { if (emptyIds.Count > 0) { emptyIds.TryDequeue(out ig); } } } return(SlopeCompress(mavaibleValues, out usedTimerIndex)); } else if (typeof(T) == typeof(short)) { Dictionary <ushort, short> mavaibleValues = new Dictionary <ushort, short>(); for (int i = 0; i < count; i++) { if (i != ig) { var id = source.ReadShort(offset + i); mavaibleValues.Add(mTimers[i], id); } else { if (emptyIds.Count > 0) { emptyIds.TryDequeue(out ig); } } } return(SlopeCompress(mavaibleValues, out usedTimerIndex)); } else if (typeof(T) == typeof(ushort)) { Dictionary <ushort, ushort> mavaibleValues = new Dictionary <ushort, ushort>(); for (int i = 0; i < count; i++) { if (i != ig) { var id = source.ReadUShort(offset + i); mavaibleValues.Add(mTimers[i], id); } else { if (emptyIds.Count > 0) { emptyIds.TryDequeue(out ig); } } } return(SlopeCompress(mavaibleValues, out usedTimerIndex)); } else if (typeof(T) == typeof(int)) { Dictionary <ushort, int> mavaibleValues = new Dictionary <ushort, int>(); for (int i = 0; i < count; i++) { if (i != ig) { var id = source.ReadInt(offset + i); mavaibleValues.Add(mTimers[i], id); } else { if (emptyIds.Count > 0) { emptyIds.TryDequeue(out ig); } } } return(SlopeCompress(mavaibleValues, out usedTimerIndex)); } else if (typeof(T) == typeof(uint)) { Dictionary <ushort, uint> mavaibleValues = new Dictionary <ushort, uint>(); for (int i = 0; i < count; i++) { if (i != ig) { var id = source.ReadUInt(offset + i); mavaibleValues.Add(mTimers[i], id); } else { if (emptyIds.Count > 0) { emptyIds.TryDequeue(out ig); } } } return(SlopeCompress(mavaibleValues, out usedTimerIndex)); } else if (typeof(T) == typeof(long)) { Dictionary <ushort, long> mavaibleValues = new Dictionary <ushort, long>(); for (int i = 0; i < count; i++) { if (i != ig) { var id = source.ReadLong(offset + i * 8); mavaibleValues.Add(mTimers[i], id); } else { if (emptyIds.Count > 0) { emptyIds.TryDequeue(out ig); } } } return(SlopeCompress(mavaibleValues, out usedTimerIndex)); } else if (typeof(T) == typeof(ulong)) { Dictionary <ushort, ulong> mavaibleValues = new Dictionary <ushort, ulong>(); for (int i = 0; i < count; i++) { if (i != ig) { var id = source.ReadULong(offset + i * 8); mavaibleValues.Add(mTimers[i], id); } else { if (emptyIds.Count > 0) { emptyIds.TryDequeue(out ig); } } } return(SlopeCompress(mavaibleValues, out usedTimerIndex)); } else if (typeof(T) == typeof(double)) { Dictionary <ushort, double> mavaibleValues = new Dictionary <ushort, double>(); for (int i = 0; i < count; i++) { if (i != ig) { var id = source.ReadDouble(offset + i * 8); mavaibleValues.Add(mTimers[i], id); } else { if (emptyIds.Count > 0) { emptyIds.TryDequeue(out ig); } } } return(SlopeCompress(mavaibleValues, out usedTimerIndex)); } else if (typeof(T) == typeof(float)) { Dictionary <ushort, float> mavaibleValues = new Dictionary <ushort, float>(); for (int i = 0; i < count; i++) { if (i != ig) { var id = source.ReadFloat(offset + i * 4); mavaibleValues.Add(mTimers[i], id); } else { if (emptyIds.Count > 0) { emptyIds.TryDequeue(out ig); } } } return(SlopeCompress(mavaibleValues, out usedTimerIndex)); } usedTimerIndex = null; return(null); }