/// <summary> /// /// </summary> public void Save(System.IO.Stream stream) { stream.Write(BitConverter.GetBytes((int)mHead.AllocSize)); mHead.WriteToStream(stream, 0, mHead.AllocSize); stream.Write(BitConverter.GetBytes(mAvaiableDataLen)); mData.WriteToStream(stream, 0, mAvaiableDataLen); }
/// <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, ">>>>>>>>>完成执行存储>>>>>>>>>"); }