public override unsafe bool CheckValueChangeToLastRecordValue(void *startMemory, long offset) { var mTmpValue = MemoryHelper.ReadInt32(startMemory, offset); if (mTmpValue != mLastValue || mLastValue == int.MinValue) { mLastValue = mTmpValue; return(true); } else { return(false); } }
/// <summary> /// /// </summary> /// <param name="startMemory"></param> /// <param name="offset"></param> /// <returns></returns> public override unsafe bool CheckValueChangeToLastRecordValue(void *startMemory, long offset) { var xx = MemoryHelper.ReadInt32(startMemory, offset); var yy = MemoryHelper.ReadInt32(startMemory, offset + 4); if (xx != x || yy != y || xx == int.MinValue) { x = xx; y = yy; return(true); } else { return(false); } }
/// <summary> /// /// </summary> /// <param name="datafile"></param> /// <param name="startTime"></param> /// <param name="tid"></param> /// <param name="offset"></param> /// <param name="start"></param> /// <param name="end"></param> /// <param name="timetick"></param> /// <returns></returns> private static IEnumerable <Tuple <MarshalMemoryBlock, DateTime, DateTime, int> > ReadTagDataBlock2(DataFileSeriserbase datafile, int tid, long offset, DateTime start, DateTime end) { int fileDuration, blockDuration = 0; int tagCount = 0; long blockpointer = 0; int timetick = 0; DateTime time; //var tagIndex = ReadTagIndexInDataPointer(datafile,tid, offset, out tagCount, out fileDuration, out blockDuration, out timetick, out blockpointer, out time); ReadRegionHead(datafile, offset, out tagCount, out fileDuration, out blockDuration, out timetick, out blockpointer, out time); var tagIndex = tid % tagCount; int blockcount = fileDuration * 60 / blockDuration; //读取文件开始时间 var startTime = datafile.ReadDateTime(0); DateTime sstart = start; DateTime send = end; int buffersize = 1024 * 1024 * 2; //分配读缓存 IntPtr mdataBuffer = Marshal.AllocHGlobal(buffersize); long mbufferadderss = 0; int bufferLen = 0; //var headdata = GetHeadBlock(datafile, offset + blockpointer, tagCount * blockcount * 12); var headdata = datafile.Read(offset + blockpointer + tagIndex * blockcount * 8, blockcount * 8); while (sstart < end) { var ttmp = Math.Round((sstart - startTime).TotalSeconds, 3); var vv = blockDuration * 60 - (ttmp % (blockDuration * 60)); send = sstart.AddSeconds(vv); if (send > end) { send = end; } int blockindex = (int)(ttmp / (blockDuration * 60)); if (blockindex >= blockcount) { break; //throw new Exception("DataPointer index is out of total block number"); } var dataPointer = headdata.ReadInt(blockindex * 8); //读取DataBlock的相对地址 if (dataPointer > 0) { MarshalMemoryBlock vmm = null; //说明数据没有采用Zip压缩,可以直接读取使用 var dp = dataPointer; int datasize = 0; int dataloc = 0; if (dp >= mbufferadderss && (dp - mbufferadderss + 4) <= bufferLen && (dp - mbufferadderss + 4 + MemoryHelper.ReadInt32(mdataBuffer, dp - mbufferadderss)) <= bufferLen) { datasize = MemoryHelper.ReadInt32(mdataBuffer, dp - mbufferadderss); dataloc = (int)(dp - mbufferadderss + 4); } else { bufferLen = datafile.Read(mdataBuffer, dp, buffersize); mbufferadderss = dp; datasize = MemoryHelper.ReadInt32(mdataBuffer, 0); dataloc = (int)(dp - mbufferadderss + 4); } if (datasize > 0 && datasize < datafile.Length) { vmm = new MarshalMemoryBlock(datasize, datasize); MemoryHelper.MemoryCopy(mdataBuffer, dataloc, vmm.Buffers[0], 0, datasize); } if (vmm != null) { yield return(new Tuple <MarshalMemoryBlock, DateTime, DateTime, int>(vmm, sstart, send, timetick)); } } sstart = send; } headdata.Dispose(); Marshal.FreeHGlobal(mdataBuffer); }
/// <summary> /// /// </summary> /// <param name="datafile"></param> /// <param name="tid"></param> /// <param name="offset"></param> /// <param name="dataTimes"></param> /// <param name="timetick"></param> /// <returns></returns> private static Dictionary <MarshalMemoryBlock, Tuple <List <DateTime>, int> > ReadTagDataBlock2(DataFileSeriserbase datafile, int tid, long offset, List <DateTime> dataTimes, out int timetick) { int fileDuration, blockDuration = 0; int tagCount = 0; long blockpointer = 0; DateTime time; //var tagIndex = ReadTagIndexInDataPointer(datafile,tid, offset, out tagCount, out fileDuration, out blockDuration, out timetick, out blockpointer, out time); ReadRegionHead(datafile, offset, out tagCount, out fileDuration, out blockDuration, out timetick, out blockpointer, out time); var tagIndex = tid % tagCount; Dictionary <long, MarshalMemoryBlock> rtmp = new Dictionary <long, MarshalMemoryBlock>(); Dictionary <MarshalMemoryBlock, Tuple <List <DateTime>, int> > re = new Dictionary <MarshalMemoryBlock, Tuple <List <DateTime>, int> >(); if (tagCount == 0) { return(re); } int blockcount = fileDuration * 60 / blockDuration; var startTime = datafile.ReadDateTime(0); int buffersize = 1024 * 1024 * 2; //分配读缓存 IntPtr mdataBuffer = Marshal.AllocHGlobal(buffersize); long mbufferadderss = 0; int bufferLen = buffersize; // var headdata = GetHeadBlock(datafile, offset + blockpointer, tagCount * blockcount * 12); var headdata = datafile.Read(offset + blockpointer + tagIndex * blockcount * 8, blockcount * 8); long mLastBuffer = 0; int mLastDataLoc = 0; int mLastDataSize = 0; MarshalMemoryBlock vmm = null; foreach (var vdd in dataTimes) { var ttmp = (vdd - startTime).TotalMinutes; int blockindex = (int)(ttmp / blockDuration); if (blockindex > blockcount) { throw new Exception("DataPointer index is out of total block number"); } //var dataPointer = headdata.ReadInt(tagIndex * blockcount * 12 + blockindex * 12); //读取DataBlock的相对地址 //var dataPointerbase = headdata.ReadLong(tagIndex * blockcount * 12 + blockindex * 12 + 4); //读取DataBlock的基地址 var dataPointer = headdata.ReadInt(blockindex * 8); //读取DataBlock的相对地址 //var dataPointerbase = headdata.ReadLong(blockindex * 12 + 4); //读取DataBlock的基地址 if (dataPointer > 0) { ////var datasize = datafile.ReadInt(dataPointer); //读取DataBlock 的大小 //var vmm = GetDataMemory(datafile, dataPointerbase, dataPointer); //说明数据没有采用Zip压缩,可以直接读取使用 var dp = dataPointer; int datasize = 0; int dataloc = 0; if (dp >= mbufferadderss && (dp - mbufferadderss + 4) <= bufferLen && (dp - mbufferadderss + 4 + MemoryHelper.ReadInt32(mdataBuffer, dp - mbufferadderss)) <= bufferLen) { datasize = MemoryHelper.ReadInt32(mdataBuffer, dp - mbufferadderss); dataloc = (int)(dp - mbufferadderss + 4); } else { bufferLen = datafile.Read(mdataBuffer, dp, buffersize); mbufferadderss = dp; datasize = MemoryHelper.ReadInt32(mdataBuffer, 0); dataloc = (int)(dp - mbufferadderss + 4); } if (datasize > 0 && (mLastBuffer != mbufferadderss || mLastDataLoc != dataloc || mLastDataSize != datasize)) { vmm = new MarshalMemoryBlock(datasize, datasize); MemoryHelper.MemoryCopy(mdataBuffer, dataloc, vmm.Buffers[0], 0, datasize); mLastBuffer = mbufferadderss; mLastDataLoc = dataloc; mLastDataSize = datasize; } else if (datasize <= 0) { vmm = null; } if (vmm != null) { if (!rtmp.ContainsKey(dataPointer)) { //var rmm = datafile.Read(dataPointer + 4, datasize); if (!re.ContainsKey(vmm)) { re.Add(vmm, new Tuple <List <DateTime>, int>(new List <DateTime>() { vdd }, blockindex)); } else { re[vmm].Item1.Add(vdd); } rtmp.Add(dataPointer, vmm); } else { //var rmm = rtmp[dataPointer]; if (!re.ContainsKey(vmm)) { re.Add(vmm, new Tuple <List <DateTime>, int>(new List <DateTime>() { vdd }, blockindex)); } else { re[vmm].Item1.Add(vdd); } } } } } headdata.Dispose(); Marshal.FreeHGlobal(mdataBuffer); return(re); }
/// <summary> /// 拷贝DataRegion数据 /// </summary> /// <param name="msource">源流</param> /// <param name="sourceheadpoint">源数据块指针</param> /// <param name="tagcount">变量个数</param> /// <param name="blockcount">数据块个数</param> /// <param name="mtarget">目标流</param> /// <param name="targetheadpoint">目标数据块</param> private unsafe void CopyRegionData(System.IO.Stream msource, MarshalFixedMemoryBlock sourceheadpoint, int tagcount, int blockcount, System.IO.Stream mtarget, out MarshalFixedMemoryBlock targetheadpoint) { //Stopwatch sw = new Stopwatch(); //sw.Start(); MarshalFixedMemoryBlock re = new MarshalFixedMemoryBlock(sourceheadpoint.Length); MarshalFixedMemoryBlock data = new MarshalFixedMemoryBlock(blockcount * 302 * 264 * 2); //int bufferLenght = 2*1024 * 1024; //IntPtr[] databuffers = new IntPtr[blockcount]; //long[] databufferLocations = new long[blockcount]; //int[] databufferLens = new int[blockcount]; //for(int i=0;i<blockcount;i++) //{ // databuffers[i] = Marshal.AllocHGlobal(bufferLenght); //} //sw.Stop(); //LoggerService.Service.Debug("HisDataArrange", "初始化分配内存 耗时:"+ ltmp1 +"," +(sw.ElapsedMilliseconds-ltmp1)); mtarget.Position += tagcount * 8 * blockcount; long mtargetposition = mtarget.Position; int offset = 0; for (int tagid = 0; tagid < tagcount; tagid++) { data.Clear(); offset = 0; mtargetposition = mtarget.Position; for (int blockid = 0; blockid < blockcount; blockid++) { CheckPaused(); var baseaddress = sourceheadpoint.ReadInt(blockid * tagcount * 8 + tagid * 8); //var baseaddress = sourceheadpoint.ReadLong(j * tagcount * 8 + i * 8 + 4); if (baseaddress > 0) { //重新组织数据块指针的分布形式,使得一个变量的数据块指针在一起 //re.WriteInt(j * 12 + i * blockcount * 12, (int)(offset | 0x80000000)); //re.WriteLong(j * 12 + i * blockcount * 12 + 4, mtargetposition); re.WriteLong(blockid * 8 + tagid * blockcount * 8, mtargetposition + offset); int datasize = 0; int dataloc = 0; if (baseaddress >= databufferLocations[blockid] && (baseaddress - databufferLocations[blockid] + 4) <= databufferLens[blockid] && (baseaddress - databufferLocations[blockid] + 4 + MemoryHelper.ReadInt32((void *)databuffers[blockid], baseaddress - databufferLocations[blockid])) <= databufferLens[blockid]) { datasize = MemoryHelper.ReadInt32((void *)databuffers[blockid], baseaddress - databufferLocations[blockid]); dataloc = (int)(baseaddress - databufferLocations[blockid] + 4); } else { int len = (int)Math.Min(bufferLenght, msource.Length - msource.Position); msource.Position = baseaddress; msource.Read(new Span <byte>((void *)databuffers[blockid], len)); databufferLocations[blockid] = baseaddress; databufferLens[blockid] = len; datasize = MemoryHelper.ReadInt32((void *)databuffers[blockid], 0); dataloc = (int)(baseaddress - databufferLocations[blockid] + 4); } data.CheckAndResize(data.Position + datasize + 4, 0.5); data.WriteInt(offset, datasize); offset += 4; if ((dataloc + datasize) > bufferLenght || datasize <= 0) { targetheadpoint = null; continue; } Buffer.MemoryCopy((void *)(databuffers[blockid] + dataloc), (void *)(data.Buffers + offset), datasize, datasize); data.Position += datasize; offset += datasize; } else { //重新组织数据块指针的分布形式,使得一个变量的数据块指针在一起 //re.WriteInt(j * 12 + i * blockcount * 12, 0); re.WriteLong(blockid * 8 + tagid * blockcount * 8, 0); } } data.WriteToStream(mtarget, 0, offset); } data.Dispose(); //for (int i = 0; i < blockcount; i++) //{ // Marshal.FreeHGlobal(databuffers[i]); //} targetheadpoint = re; }
/// <summary> /// /// </summary> /// <param name="offset"></param> /// <returns></returns> public virtual int ReadInt(long offset) { mPosition = offset + sizeof(int); return(MemoryHelper.ReadInt32((void *)mHandles, offset)); }
/// <summary> /// /// </summary> /// <param name="index"></param> /// <returns></returns> public T GetValue(int index, out DateTime time, out byte qulity) { object re = null; switch (mDataType) { case 0: re = Convert.ToBoolean(MemoryHelper.ReadByte((void *)handle, index)); break; case 1: re = MemoryHelper.ReadByte((void *)handle, index); break; case 2: re = MemoryHelper.ReadShort((void *)handle, index * 2); break; case 3: re = MemoryHelper.ReadUShort((void *)handle, index * 2); break; case 4: re = MemoryHelper.ReadInt32((void *)handle, index * 4); break; case 5: re = MemoryHelper.ReadUInt32((void *)handle, index * 4); break; case 6: re = MemoryHelper.ReadInt64((void *)handle, index * 8); break; case 7: re = MemoryHelper.ReadUInt64((void *)handle, index * 8); break; case 8: re = MemoryHelper.ReadFloat((void *)handle, index * 4); break; case 9: re = MemoryHelper.ReadDouble((void *)handle, index * 8); break; case 10: re = MemoryHelper.ReadDateTime((void *)handle, index * 8); break; case 11: if (mStringCach != null) { re = mStringCach[index]; } else { mStringCach = new List <string>(mCount); int cc = 0; int pos = 0; int len = 0; while (cc < this.mCount) { len = MemoryHelper.ReadByte((void *)handle, pos); mStringCach.Add(new string((sbyte *)handle, pos + 1, len, Encoding.Unicode)); pos += len + 1; cc++; } re = mStringCach[index]; } break; case 12: var x = MemoryHelper.ReadInt32((void *)handle, index * 8); var y = MemoryHelper.ReadInt32((void *)handle, index * 8 + 4); re = new IntPointData(x, y); break; case 13: re = new UIntPointData(MemoryHelper.ReadUInt32((void *)handle, index * 8), MemoryHelper.ReadUInt32((void *)handle, index * 8 + 4)); break; case 14: re = new IntPoint3Data(MemoryHelper.ReadInt32((void *)handle, index * 12), MemoryHelper.ReadInt32((void *)handle, index * 12 + 4), MemoryHelper.ReadInt32((void *)handle, index * 12 + 8)); break; case 15: re = new UIntPoint3Data(MemoryHelper.ReadUInt32((void *)handle, index * 12), MemoryHelper.ReadUInt32((void *)handle, index * 12 + 4), MemoryHelper.ReadUInt32((void *)handle, index * 12 + 8)); break; case 16: re = new LongPointData(MemoryHelper.ReadInt64((void *)handle, index * 16), MemoryHelper.ReadInt64((void *)handle, index * 16 + 8)); break; case 17: re = new ULongPointData(MemoryHelper.ReadUInt64((void *)handle, index * 16), MemoryHelper.ReadUInt64((void *)handle, index * 16 + 8)); break; case 18: re = new LongPoint3Data(MemoryHelper.ReadInt64((void *)handle, index * 24), MemoryHelper.ReadInt64((void *)handle, index * 24 + 8), MemoryHelper.ReadInt64((void *)handle, index * 24 + 16)); break; case 19: re = new ULongPoint3Data(MemoryHelper.ReadUInt64((void *)handle, index * 24), MemoryHelper.ReadUInt64((void *)handle, index * 24 + 8), MemoryHelper.ReadUInt64((void *)handle, index * 24 + 16)); break; } time = MemoryHelper.ReadDateTime((void *)handle, index * 8 + mTimeAddr); qulity = MemoryHelper.ReadByte((void *)handle, mQulityAddr + index); return((T)re); }
/// <summary> /// /// </summary> /// <param name="index"></param> /// <returns></returns> public T GetValue(int index, out DateTime time, out byte qulity) { object re = null; switch (mDataType) { case 0: re = Convert.ToBoolean(MemoryHelper.ReadByte((void *)handle, index)); break; case 1: re = MemoryHelper.ReadByte((void *)handle, index); break; case 2: re = MemoryHelper.ReadShort((void *)handle, index * 2); break; case 3: re = MemoryHelper.ReadShort((void *)handle, index * 2); break; case 4: re = MemoryHelper.ReadInt32((void *)handle, index * 4); break; case 5: re = MemoryHelper.ReadInt32((void *)handle, index * 4); break; case 6: re = MemoryHelper.ReadInt32((void *)handle, index * 8); break; case 7: re = MemoryHelper.ReadInt32((void *)handle, index * 8); break; case 8: re = MemoryHelper.ReadFloat((void *)handle, index * 4); break; case 9: re = MemoryHelper.ReadDouble((void *)handle, index * 8); break; case 10: re = MemoryHelper.ReadDateTime((void *)handle, index * 8); break; case 11: int cc = 0; int pos = 0; while (true) { if (cc >= index) { break; } //pos += (mDataBuffer[pos]+1); pos += MemoryHelper.ReadByte((void *)handle, pos) + 1; cc++; } re = new string((char *)handle, pos + 1, MemoryHelper.ReadByte((void *)handle, pos)); break; } time = MemoryHelper.ReadDateTime((void *)handle, index * 8 + mTimeAddr); qulity = MemoryHelper.ReadByte((void *)handle, mQulityAddr + index); return((T)re); }