public byte[] GetRecord(AnalogIndex indexRecord)
        {
            if (indexRecord.RecordLength > 10 * 1024 * 1024 || indexRecord.RecordLength < 8)
            {
                return(null);                                                                           //超过10M的记录或者不存在一条记录
            }
            byte[] data = new byte[indexRecord.RecordLength];

            long fileSize = fileStream.Length;

            fileStream.Position = indexRecord.BeginOffset;

            if (fileSize >= indexRecord.BeginOffset + indexRecord.RecordLength)
            {
                fileStream.Read(data, 0, data.Length);
            }
            else
            {
                int size1 = (int)(fileSize - indexRecord.BeginOffset);
                if (size1 > 0)
                {
                    fileStream.Read(data, 0, size1);
                }
                int size2 = indexRecord.RecordLength - size1;
                if (size2 > 0)
                {
                    fileStream.Position = 0;
                    fileStream.Read(data, size1, size2);
                }
            }

            return(data);
        }
        public byte[] GetRecord(AnalogIndex indexRecord)
        {
            if (indexRecord.RecordLength > 10 * 1024 * 1024 || indexRecord.RecordLength<8) return null; //超过10M的记录或者不存在一条记录

            byte[] data=new byte[indexRecord.RecordLength];

            long fileSize = fileStream.Length;
            fileStream.Position = indexRecord.BeginOffset;

            if (fileSize >= indexRecord.BeginOffset + indexRecord.RecordLength)
            {
                fileStream.Read(data, 0, data.Length);
            }
            else
            {
                int size1=(int)(fileSize-indexRecord.BeginOffset);
                if(size1>0)
                {
                    fileStream.Read(data, 0, size1);
                }
                int size2 = indexRecord.RecordLength - size1;
                if (size2 > 0)
                {
                    fileStream.Position = 0;
                    fileStream.Read(data, size1, size2);
                }

            }

            return data;
        }
        public void Store(AnalogIndex record)
        {
            if (fileStream != null)
            {
                byte[] data = record.GetBytes();

                fileStream.Position = record.Index * AnalogIndex.IndexSize;
                fileStream.Write(data, 0, data.Length);
                fileStream.Flush();
            }
        }
Beispiel #4
0
        public int CompareTo(object obj)
        {
            AnalogIndex record = obj as AnalogIndex;

            if (record.FileIndex == this.FileIndex)
            {
                return(record.Index - this.Index);
            }
            else
            {
                return(record.FileIndex - this.FileIndex);
            }
        }
Beispiel #5
0
        public bool ConflictWith(AnalogIndex other, long maxFileSize)
        {
            long[] sect1 = new long[4];
            long[] sect2 = new long[4];

            long leftLen = maxFileSize - this.BeginOffset;

            sect1[0] = this.BeginOffset;
            sect1[1] = leftLen > this.RecordLength ? this.RecordLength : leftLen;
            sect1[2] = 0;
            sect1[3] = this.RecordLength - leftLen;

            leftLen  = maxFileSize - other.BeginOffset;
            sect2[0] = other.BeginOffset;
            sect2[1] = leftLen > other.RecordLength ? other.RecordLength : leftLen;
            sect2[2] = 0;
            sect2[3] = other.RecordLength - leftLen;

            for (int i = 0; i < sect1.Length / 2; i++)
            {
                long sec1Size = sect1[i * 2 + 1];
                if (sec1Size <= 0)
                {
                    continue;
                }
                long sec1Begin = sect1[i * 2];
                for (int j = 0; j < sect2.Length / 2; j++)
                {
                    long sec2Size = sect2[j * 2 + 1];
                    if (sec2Size <= 0)
                    {
                        continue;
                    }
                    long sec2Begin = sect2[j * 2];

                    if (sec2Begin >= sec1Begin + sec1Size)
                    {
                        continue;
                    }
                    if (sec1Begin >= sec2Begin + sec2Size)
                    {
                        continue;
                    }

                    return(true);
                }
            }

            return(false);
        }
        public AnalogIndexFile(AnalogFile analog)
        {
            this.fileAnalog = analog;
            byte[] buffer = new byte[IndexCount * 32];
            Array.Clear(buffer, 0, buffer.Length);
            try
            {
                string indexName = Path.Combine(analog.DataManager.StoreDir, analog.Type.ToString("X2") + "H-" + analog.Index.ToString("000") + ".idx");
                fileStream = new FileStream(indexName, FileMode.OpenOrCreate, FileAccess.ReadWrite);


                if (fileStream.Length < buffer.Length)
                {
                    fileStream.SetLength(buffer.Length); //默认建立16KB的文件
                }

                fileStream.Read(buffer, 0, buffer.Length);
            }
            catch
            {
                fileStream = null;
            }
            for (int i = 0; i < IndexCount; i++)
            {
                AnalogIndex indexRecord = new AnalogIndex(i, buffer, i * 32);
                listIndex.Add(indexRecord);
            }
            listIndex.Sort();

            //TODO:必须加入对重叠的Index处理
            for (int i = 0; i < listIndex.Count; i++)
            {
                if (listIndex[i].IsValid == false)
                {
                    break;
                }

                for (int j = i + 1; j < listIndex.Count; j++)
                {
                    if (listIndex[j].IsValid && listIndex[j].ConflictWith(listIndex[i], this.fileAnalog.MaxFileSize))
                    {
                        for (int k = j; k < listIndex.Count; k++)
                        {
                            listIndex[k].FileIndex = 0; //设置为无效
                        }
                        break;
                    }
                }
            }
        }
        List<AnalogIndex> listIndex = new List<AnalogIndex>(); //按文件记录索引排序

        #endregion Fields

        #region Constructors

        public AnalogIndexFile(AnalogFile analog)
        {
            this.fileAnalog = analog;
            byte[] buffer = new byte[IndexCount * 32];
            Array.Clear(buffer, 0, buffer.Length);
            try
            {
                string indexName = Path.Combine(analog.DataManager.StoreDir, analog.Type.ToString("X2") + "H-" + analog.Index.ToString("000") + ".idx");
                fileStream = new FileStream(indexName, FileMode.OpenOrCreate, FileAccess.ReadWrite);

                if (fileStream.Length < buffer.Length)
                {
                    fileStream.SetLength(buffer.Length); //默认建立16KB的文件
                }

                fileStream.Read(buffer, 0, buffer.Length);
            }
            catch
            {
                fileStream = null;
            }
            for (int i = 0; i < IndexCount; i++)
            {
                AnalogIndex indexRecord = new AnalogIndex(i, buffer, i * 32);
                listIndex.Add(indexRecord);
            }
            listIndex.Sort();

            //TODO:必须加入对重叠的Index处理
            for (int i = 0; i < listIndex.Count; i++)
            {
                if (listIndex[i].IsValid == false) break;

                for (int j = i + 1; j < listIndex.Count; j++)
                {
                    if (listIndex[j].IsValid && listIndex[j].ConflictWith(listIndex[i], this.fileAnalog.MaxFileSize))
                    {
                        for (int k = j; k < listIndex.Count; k++)
                        {
                            listIndex[k].FileIndex = 0; //设置为无效
                        }
                        break;

                    }
                }

            }
        }
        public void Store(AnalogIndex index, byte[] data, int length)
        {
            long offset = index.BeginOffset;
            long fileSize=fileStream.Length;
            long needSize = offset + index.RecordLength + length;
            long wrPos = (index.RecordLength + offset)%fileAnalog.MaxFileSize;
            if (needSize <= fileAnalog.MaxFileSize)//直接写入
            {
                if (fileSize < needSize)
                {
                    fileStream.SetLength(fileSize + IncreaseSize);
                }
                fileStream.Position = wrPos;
                fileStream.Write(data, 0, length);
                fileStream.Flush();
            }
            else
            {

                long leftSize = fileSize - wrPos;

                int size1 = (int)(leftSize < length ? leftSize : length);
                int size2 = length - size1;

                if (size1 > 0)
                {
                    fileStream.Position = wrPos;
                    fileStream.Write(data, 0, size1);
                }
                if (size2 > 0)
                {
                    fileStream.Position = 0;
                    fileStream.Write(data, size1, size2);
                }
                fileStream.Flush();
            }

            index.RecordLength = index.RecordLength + length;
        }
        public void Store(AnalogIndex index, byte[] data, int length)
        {
            long offset   = index.BeginOffset;
            long fileSize = fileStream.Length;
            long needSize = offset + index.RecordLength + length;
            long wrPos    = (index.RecordLength + offset) % fileAnalog.MaxFileSize;

            if (needSize <= fileAnalog.MaxFileSize)//直接写入
            {
                if (fileSize < needSize)
                {
                    fileStream.SetLength(fileSize + IncreaseSize);
                }
                fileStream.Position = wrPos;
                fileStream.Write(data, 0, length);
                fileStream.Flush();
            }
            else
            {
                long leftSize = fileSize - wrPos;

                int size1 = (int)(leftSize < length ? leftSize : length);
                int size2 = length - size1;

                if (size1 > 0)
                {
                    fileStream.Position = wrPos;
                    fileStream.Write(data, 0, size1);
                }
                if (size2 > 0)
                {
                    fileStream.Position = 0;
                    fileStream.Write(data, size1, size2);
                }
                fileStream.Flush();
            }

            index.RecordLength = index.RecordLength + length;
        }
Beispiel #10
0
        public AnalogIndex NewIndex()
        {
            AnalogIndex recordLast  = listIndex[listIndex.Count - 1];
            AnalogIndex recordFirst = listIndex[0];

            recordLast.BeginOffset = 0;
            if (recordFirst.IsValid) //有效
            {
                recordLast.BeginOffset = (uint)((recordFirst.BeginOffset + recordFirst.RealLength) % fileAnalog.MaxFileSize);
            }
            recordLast.FileIndex    = recordFirst.FileIndex + 1; //文件记录索引
            recordLast.RecordLength = 0;                         //记录长度
            recordLast.RealLength   = 0;
            recordLast.BeginTime    = DateTime.MinValue;
            recordLast.EndTime      = DateTime.MinValue;

            lock (listIndex)
            {
                listIndex.Remove(recordLast);
                listIndex.Insert(0, recordLast);
            }
            return(recordLast);
        }
        public bool Process()
        {
            List <AnalogPoint>  listDayPoint = new List <AnalogPoint>();
            IList <AnalogIndex> indexAll     = fileAnalog.IndexFile.AllIndex;

            for (int i = indexAll.Count - 1; i >= 0; i--)
            {
                AnalogIndex recordIndex = indexAll[i];
                if (recordIndex.IsValid == false)
                {
                    continue;
                }
                if (recordIndex.BeginTime > this.timeEnd)
                {
                    continue;
                }
                if (recordIndex.EndTime < this.timeBegin)
                {
                    continue;
                }

                if (recordIndex.Type == FallbackType.Fallback) //回溯数据
                {
                    if (listDayPoint.Count > 0)
                    {
                        listAllPoit.Add(listDayPoint);
                        listDayPoint = new List <AnalogPoint>();
                    }
                }
                //读取全部数据
                byte[] data = fileAnalog.DataFile.GetRecord(recordIndex);

                if (data == null)
                {
                    continue;
                }

                int      cnt      = data.Length / 8;
                DateTime timeBase = recordIndex.BeginTime.Date;
                if ((recordIndex.BeginTime >= this.timeBegin) && (recordIndex.EndTime <= this.timeEnd))
                {
                    for (int j = 0; j < cnt; j++)
                    {
                        AnalogPoint pt = new AnalogPoint(timeBase, data, j * 8);
                        listDayPoint.Add(pt);
                    }
                }
                else
                {
                    for (int j = 0; j < cnt; j++)
                    {
                        uint timeDiff = BitConverter.ToUInt32(data, j * 8);
                        byte digit    = (byte)(timeDiff & 0x0f);

                        DateTime time = timeBase.AddMilliseconds(timeDiff >> 4);
                        if (time < this.timeBegin)
                        {
                            continue;
                        }
                        if (time > this.timeEnd)
                        {
                            break;
                        }

                        float analogValue = BitConverter.ToSingle(data, j * 8 + 4);

                        AnalogPoint pt = new AnalogPoint(time, analogValue, digit);
                        listDayPoint.Add(pt);
                    }
                }
            }
            if (listDayPoint.Count > 0)
            {
                listAllPoit.Add(listDayPoint);
            }

            finishedEvent.Set();

            return(true);
        }
        public void Store(AnalogIndex record)
        {
            if (fileStream != null)
            {
                byte[] data = record.GetBytes();

                fileStream.Position = record.Index * AnalogIndex.IndexSize;
                fileStream.Write(data, 0, data.Length);
                fileStream.Flush();
            }
        }
Beispiel #13
0
        public bool ConflictWith(AnalogIndex other,long maxFileSize)
        {
            long[] sect1 = new long[4];
            long[] sect2 = new long[4];

            long leftLen=maxFileSize-this.BeginOffset;

            sect1[0] = this.BeginOffset;
            sect1[1] = leftLen > this.RecordLength ? this.RecordLength : leftLen;
            sect1[2] = 0;
            sect1[3] = this.RecordLength-leftLen;

            leftLen = maxFileSize - other.BeginOffset;
            sect2[0] = other.BeginOffset;
            sect2[1] = leftLen > other.RecordLength ? other.RecordLength : leftLen;
            sect2[2] = 0;
            sect2[3] = other.RecordLength - leftLen;

            for (int i = 0; i < sect1.Length/2; i++)
            {
                long sec1Size = sect1[i * 2 + 1];
                if (sec1Size <= 0) continue;
                long sec1Begin = sect1[i * 2];
                for (int j = 0; j < sect2.Length / 2; j++)
                {
                    long sec2Size = sect2[j * 2 + 1];
                    if (sec2Size <= 0) continue;
                    long sec2Begin = sect2[j * 2];

                    if (sec2Begin >= sec1Begin + sec1Size) continue;
                    if (sec1Begin >= sec2Begin + sec2Size) continue;

                    return true;
                }

            }

            return false;
        }