Buffer.cs 6.72 KB
Newer Older
潘栩锋's avatar
潘栩锋 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
using FLY.OBJComponents.Common;
using FLY.OBJComponents.IService;
using FLY.Thick.RemoteHistory;
using FObjBase;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Threading;

namespace FLY.OBJComponents.Server
{

18
    public class Buffer<T> : IBufferAdd<T>
潘栩锋's avatar
潘栩锋 committed
19 20 21 22
    {
        public List<T> list = new List<T>();
        #region property

23
        public int NewestID { get; set; } = -1;
潘栩锋's avatar
潘栩锋 committed
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
        public int Count { get; private set; } = 0;

        /// <summary>
        /// 总容量,当总数量 大于等于 总容量的 100%, 前面的10%数据会被删除
        /// </summary>
        public int Capacity { get; set; }
        #endregion
        public event NotifyBufferChangedEventHandler<T> BufferChanged;
        public event PropertyChangedEventHandler PropertyChanged;
        

        /// <summary>
        /// 
        /// </summary>
        /// <param name="filepath">数据存储路径, null 不保存</param>
        /// <param name="saveInterval">数据保存周期时间 单位:分钟</param>
        /// <param name="capacity">数据总容量</param>
        public Buffer(int capacity = 1000)
        {
            Capacity = capacity;
        }


        public int GetID(int idx)
        {
49 50
            int offset = (NewestID - (list.Count() - 1));
            return offset + idx;
潘栩锋's avatar
潘栩锋 committed
51 52 53
        }
        public int GetIndex(int id)
        {
54 55 56
            int offset = (NewestID - (list.Count() - 1));

            return id - offset;
潘栩锋's avatar
潘栩锋 committed
57 58 59 60 61 62 63 64 65
        }
        public void Add(T t)
        {
            list.Add(t);

            if (list.Count() > Capacity)
            {
                list.RemoveRange(0, Capacity / 10);
            }
66 67
            
            NewestID++;
潘栩锋's avatar
潘栩锋 committed
68 69 70 71 72 73 74 75 76
            Count = list.Count();



            BufferChanged?.Invoke(this,
                new NotifyBufferChangedEventArgs<T>()
                {
                    Action = NotifyBufferChangedAction.Add,
                    Items = new T[] { t },
77
                    EndingID = NewestID
潘栩锋's avatar
潘栩锋 committed
78 79
                });
        }
80
        public void AddRange(IEnumerable<T> array)
潘栩锋's avatar
潘栩锋 committed
81 82 83 84 85 86 87
        {
            list.AddRange(array);

            if (list.Count() > Capacity)
            {
                list.RemoveRange(0, Capacity / 10);
            }
88 89
            
            NewestID += array.Count();
潘栩锋's avatar
潘栩锋 committed
90 91 92 93 94 95 96
            Count = list.Count();

            BufferChanged?.Invoke(this,
                new NotifyBufferChangedEventArgs<T>()
                {
                    Action = NotifyBufferChangedAction.Add,
                    Items = array,
97
                    EndingID = NewestID
潘栩锋's avatar
潘栩锋 committed
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
                });
        }
        public override string ToString()
        {
            return "{NewestID:" + NewestID.ToString() + ", " + "Count:" + Count.ToString() + "}";
        }
        public void Replace(int id, T t)
        {
            int idx = GetIndex(id);
            if (idx < 0 || idx >= list.Count())
                return;
            list[idx] = t;
            BufferChanged?.Invoke(this,
                new NotifyBufferChangedEventArgs<T>()
                {
                    Action = NotifyBufferChangedAction.Replace,
                    Items = new T[] { t },
115
                    EndingID = id
潘栩锋's avatar
潘栩锋 committed
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
                });
        }
        public void Remove(int id)
        {
            int idx = GetIndex(id);
            if (idx < 0 || idx >= list.Count())
                return;
            T t = list[idx];
            list.RemoveAt(idx);
            Count--;

            BufferChanged?.Invoke(this,
                new NotifyBufferChangedEventArgs<T>()
                {
                    Action = NotifyBufferChangedAction.Remove,
131
                    EndingID = id,
潘栩锋's avatar
潘栩锋 committed
132 133 134 135 136 137
                    Items = new T[] { t }
                });
        }
        public void Reset()
        {
            list.Clear();
138
            NewestID = -1;
潘栩锋's avatar
潘栩锋 committed
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
            Count = 0;
            BufferChanged?.Invoke(this,
                new NotifyBufferChangedEventArgs<T>()
                {
                    Action = NotifyBufferChangedAction.Reset
                });
        }

        /// <summary>
        /// 获取指定位置的N个数据
        /// </summary>
        /// <param name="last_id">最后的ID</param>
        /// <param name="count">数量</param>
        /// <param name="asyncCB">回调</param>
        /// <param name="asyncContext">回调里面的上下文</param>
        public void GetRecord(int last_id, int count, AsyncCBHandler asyncCB, object asyncContext)
        {
            if (list.Count() == 0)
            {
                asyncCB(asyncContext, new GetRecordReponse<T>()
                {
                    LastID = 0,
                    Items = null
                });
                return;
            }

            int firstID = last_id - count + 1;
            int lastID = last_id;

            int rFirstID = NewestID - list.Count() + 1;
            int rLastID = NewestID;

            //以 rFirstID 为偏移量
            int offset = -rFirstID;
            firstID += offset;
            lastID += offset;
            rLastID += offset;
            rFirstID = 0;


            //返回交集
            if (lastID < 0)//在前面
            {
                //没有数据
                asyncCB(asyncContext, new GetRecordReponse<T>()
                {
                    LastID = 0,
                    Items = null
                });
                return;
            }
            else if (firstID > rLastID)//在后面
            {
                //没有数据
                asyncCB(asyncContext, new GetRecordReponse<T>()
                {
                    LastID = 0,
                    Items = null
                });
                return;
            }
            else
            {
                if (firstID < 0)
                    firstID = 0;
                if (lastID > rLastID)
                    lastID = rLastID;
                int cnt = lastID - firstID + 1;
                T[] items = new T[cnt];
                for (int i = 0; i < cnt; i++)
                    items[i] = list[firstID + i];

                asyncCB(asyncContext, new GetRecordReponse<T>()
                {
                    LastID = lastID - offset,
                    Items = items
                });
            }
        }

        /// <summary>
        /// 获取最新的N个数据
        /// </summary>
        /// <param name="count">数量</param>
        /// <param name="asyncCB">回调</param>
        /// <param name="asyncContext">回调里面的上下文</param>
        public void GetRecord(int count, AsyncCBHandler asyncCB, object asyncContext)
        {
            GetRecord(NewestID, count, asyncCB, asyncContext);
        }
    }
}