DeadAreaCompressUnit2.cs 43.8 KB
Newer Older
1 2 3 4 5 6 7 8
//==============================================================
//  Copyright (C) 2019  Inc. All rights reserved.
//
//==============================================================
//  Create by 种道洋 at 2019/12/27 18:45:02.
//  Version 1.0
//  种道洋
//==============================================================
9
using DBRuntime.His;
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
using DBRuntime.His.Compress;
using System;
using System.Collections.Generic;
using System.Text;

namespace Cdy.Tag
{
    /// <summary>
    /// 
    /// </summary>
    public class DeadAreaCompressUnit2 : LosslessCompressUnit2
    {
        /// <summary>
        /// 
        /// </summary>
        public override int TypeCode => 2;

        /// <summary>
        /// 
        /// </summary>
        public override string Desc => "死区压缩";

        protected CustomQueue<int> emptys2 = new CustomQueue<int>(604);

        protected ProtoMemory mVarintMemory2;

36 37 38 39
        //Dictionary<int, double> dvals = new Dictionary<int, double>();

        //Dictionary<int, int> dtims = new Dictionary<int, int>();

40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override CompressUnitbase2 Clone()
        {
            return new DeadAreaCompressUnit2();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="preVal"></param>
        /// <param name="newVal"></param>
        /// <param name="deadArea"></param>
        /// <param name="Deadtype"></param>
        /// <returns></returns>
        private bool CheckIsNeedRecord(double preVal,double newVal,double deadArea,int Deadtype)
        {
            if(Deadtype==0)
            {
                return Math.Abs(newVal - preVal) > deadArea;
            }
            else
            {
65
                return preVal > 0 ? Math.Abs((newVal - preVal) / preVal) > deadArea : true;
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
            }
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="timerVals"></param>
        /// <param name="startaddr"></param>
        /// <param name="count"></param>
        /// <param name="emptyIds"></param>
        protected void FindEmpityIds(IMemoryBlock timerVals, long startaddr, int count, CustomQueue<int> emptyIds)
        {
            emptys.Reset();
            int id = 0;
            for (int i = 0; i < count; i++)
            {
                id = timerVals.ReadUShort((int)startaddr + i * 2);
                if (id > 0 || i == 0)
                {
                    continue;
                }
                else
                {
                    emptyIds.Insert(i);
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="timerVals"></param>
        /// <param name="startaddr"></param>
        /// <param name="count"></param>
        /// <param name="emptyIds"></param>
        /// <returns></returns>
        protected virtual Memory<byte> CompressTimers2(IMemoryBlock timerVals, long startaddr, int count, CustomQueue<int> emptyIds)
        {
            int preids = 0;
            mVarintMemory2.Reset();
107
            //dtims.Clear();
108 109 110 111
            bool isFirst = true;
            int id = 0;

            int ig = -1;
cdy816's avatar
cdy816 已提交
112
            ig = emptyIds.ReadIndex <= emptyIds.WriteIndex ? emptyIds.IncRead() : -1;
113

114 115
            byte tlen = (timerVals as HisDataMemoryBlock).TimeLen;

cdy816's avatar
cdy816 已提交
116 117
            int rcount = 0;

118 119 120 121
            for (int i = 0; i < count; i++)
            {
                if (i != ig)
                {
122 123 124
                    //id = timerVals.ReadUShort((int)startaddr + i * 2);

                    id = tlen == 2 ? timerVals.ReadUShort((int)startaddr + i * 2) : timerVals.ReadInt((int)startaddr + i * 4);
125 126 127 128 129 130 131 132 133 134

                    if (isFirst)
                    {
                        mVarintMemory2.WriteInt32(id);
                        isFirst = false;
                    }
                    else
                    {
                        mVarintMemory2.WriteInt32(id - preids);
                    }
cdy816's avatar
cdy816 已提交
135
                    rcount++;
136 137
                    //dtims.Add(i, id);
                    preids = id;
138 139 140
                }
                else
                {
cdy816's avatar
cdy816 已提交
141
                    ig = emptyIds.ReadIndex <= emptyIds.WriteIndex ? emptyIds.IncRead() : -1;
142 143 144
                    //    emptyIds.TryDequeue(out ig);
                }
            }
cdy816's avatar
cdy816 已提交
145 146 147 148

            LoggerService.Service.Debug("DeadAreaCompress", "记录时间个数:"+rcount +" 空时间个数:"+ (emptyIds.WriteIndex+1));

            return mVarintMemory2.DataBuffer.AsMemory(0, (int)mVarintMemory2.WritePosition);
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
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="source"></param>
        /// <param name="sourceAddr"></param>
        /// <param name="target"></param>
        /// <param name="targetAddr"></param>
        /// <param name="size"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        protected override long Compress<T>(IMemoryBlock source, long sourceAddr, MarshalMemoryBlock target, long targetAddr, long size, TagType type)
        {
            var count = (int)(size - this.QulityOffset);
            //var tims = source.ReadUShorts(sourceAddr, (int)count);

            if (mMarshalMemory == null)
            {
                mMarshalMemory = new MemoryBlock(count * 10);
            }

            if (mVarintMemory == null)
            {
                mVarintMemory = new ProtoMemory(count * 10);
            }

            if (mVarintMemory2 == null)
            {
                mVarintMemory2 = new ProtoMemory(count * 10);
            }

182 183 184 185 186 187 188 189
            if (emptys2.Length < count)
            {
                emptys2 = new CustomQueue<int>(count * 2);
            }
            else
            {
                emptys2.Reset();
            }
190 191 192 193 194 195 196 197 198 199 200

            long rsize = 0;

            switch (type)
            {
                case TagType.Bool:

                    var datas = CompressTimers(source, sourceAddr, (int)count, emptys);

                    var cval = CompressBoolValues(source, count * 2 + sourceAddr, count, emptys);

cdy816's avatar
cdy816 已提交
201
                    int rcount = count - emptys.WriteIndex - 1;
202
                    //写入时间
cdy816's avatar
cdy816 已提交
203 204
                    target.WriteInt(targetAddr, rcount);
                    rsize += 4;
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
                    target.Write((int)datas.Length);
                    target.Write(datas);
                    rsize += 4;
                    rsize += datas.Length;

                    //写入数据
                    target.Write(cval.Length);
                    target.Write(cval);
                    rsize += 4;
                    rsize += cval.Length;
                    emptys.ReadIndex = 0;

                    //写入质量戳
                    var cqus = CompressQulitys(source, count * 3 + sourceAddr, count, emptys);
                    target.Write(cqus.Length);
                    target.Write(cqus);
                    rsize += 4;
                    rsize += cqus.Length;
                    break;
                case TagType.Byte:

                    FindEmpityIds(source, sourceAddr, (int)count, emptys);
                    cval = CompressValues<byte>(source, count * 2 + sourceAddr, count, emptys, TagType);

                    datas = CompressTimers2(source, sourceAddr, (int)count, emptys2);
cdy816's avatar
cdy816 已提交
230
                    rcount = count - emptys2.WriteIndex - 1;
231
                    //写入时间
cdy816's avatar
cdy816 已提交
232 233
                    target.WriteInt(targetAddr, rcount);
                    rsize += 4;
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
                    target.Write((int)datas.Length);
                    target.Write(datas);
                    rsize += 4;
                    rsize += datas.Length;

                    //写入数据
                    target.Write(cval.Length);
                    target.Write(cval);
                    rsize += 4;
                    rsize += cval.Length;
                    emptys2.ReadIndex = 0;

                    //写入质量戳
                    cqus = CompressQulitys(source, count * 3 + sourceAddr, count, emptys2);
                    target.Write(cqus.Length);
                    target.Write(cqus);
                    rsize += 4;
                    rsize += cqus.Length;
                    break;
                case TagType.UShort:
                    FindEmpityIds(source, sourceAddr, (int)count, emptys);
                    var ures = CompressValues<ushort>(source, count * 2 + sourceAddr, count, emptys, TagType);

                    datas = CompressTimers2(source, sourceAddr, (int)count, emptys2);
cdy816's avatar
cdy816 已提交
258
                    rcount = count - emptys2.WriteIndex - 1;
259
                    //写入时间
cdy816's avatar
cdy816 已提交
260 261
                    target.WriteInt(targetAddr, rcount);
                    rsize += 4;
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
                    target.Write((int)datas.Length);
                    target.Write(datas);
                    rsize += 4;
                    rsize += datas.Length;
                    //写入数据
                    target.Write(ures.Length);
                    target.Write(ures);
                    rsize += 4;
                    rsize += ures.Length;
                    //质量戳
                    emptys2.ReadIndex = 0;
                    cqus = CompressQulitys(source, count * 4 + sourceAddr, count, emptys2);
                    target.Write(cqus.Length);
                    target.Write(cqus);
                    rsize += 4;
                    rsize += cqus.Length;
                    break;
                case TagType.Short:
                    FindEmpityIds(source, sourceAddr, (int)count, emptys);
                    var res = CompressValues<short>(source, count * 2 + sourceAddr, count, emptys, TagType);

                    datas = CompressTimers2(source, sourceAddr, (int)count, emptys2);
cdy816's avatar
cdy816 已提交
284
                    rcount = count - emptys2.WriteIndex - 1;
285
                    //写入时间
cdy816's avatar
cdy816 已提交
286 287
                    target.WriteInt(targetAddr, rcount);
                    rsize += 4;
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
                    target.Write((int)datas.Length);
                    target.Write(datas);
                    rsize += 4;
                    rsize += datas.Length;
                    //写入数据
                    target.Write(res.Length);
                    target.Write(res);
                    rsize += 4;
                    rsize += res.Length;

                    emptys2.ReadIndex = 0;
                    cqus = CompressQulitys(source, count * 4 + sourceAddr, count, emptys2);
                    target.Write(cqus.Length);
                    target.Write(cqus);
                    rsize += 4;
                    rsize += cqus.Length;
                    break;
                case TagType.UInt:
                    FindEmpityIds(source, sourceAddr, (int)count, emptys);
                    var uires = CompressValues<uint>(source, count * 2 + sourceAddr, count, emptys, TagType);

                    datas = CompressTimers2(source, sourceAddr, (int)count, emptys2);
cdy816's avatar
cdy816 已提交
310
                    rcount = count - emptys2.WriteIndex - 1;
311
                    //写入时间
cdy816's avatar
cdy816 已提交
312 313
                    target.WriteInt(targetAddr, rcount);
                    rsize += 4;
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
                    target.Write((int)datas.Length);
                    target.Write(datas);
                    rsize += 4;
                    rsize += datas.Length;
                    //写入数据
                    target.Write(uires.Length);
                    target.Write(uires);
                    rsize += 4;
                    rsize += uires.Length;
                    //质量
                    emptys2.ReadIndex = 0;
                    cqus = CompressQulitys(source, count * 6 + sourceAddr, count, emptys2);
                    target.Write(cqus.Length);
                    target.Write(cqus);
                    rsize += 4;
                    rsize += cqus.Length;
                    break;
                case TagType.Int:
                    FindEmpityIds(source, sourceAddr, (int)count, emptys);
                    var ires = CompressValues<int>(source, count * 2 + sourceAddr, count, emptys, TagType);
                    datas = CompressTimers2(source, sourceAddr, (int)count, emptys2);
cdy816's avatar
cdy816 已提交
335
                    rcount = count - emptys2.WriteIndex - 1;
336
                    //写入时间
cdy816's avatar
cdy816 已提交
337 338
                    target.WriteInt(targetAddr, rcount);
                    rsize += 4;
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
                    target.Write((int)datas.Length);
                    target.Write(datas);
                    rsize += 4;
                    rsize += datas.Length;
                    //写入数据
                    target.Write(ires.Length);
                    target.Write(ires);
                    rsize += 4;
                    rsize += ires.Length;
                    emptys2.ReadIndex = 0;
                    //质量
                    cqus = CompressQulitys(source, count * 6 + sourceAddr, count, emptys2);
                    target.Write(cqus.Length);
                    target.Write(cqus);
                    rsize += 4;
                    rsize += cqus.Length;
                    break;
                case TagType.ULong:
                    FindEmpityIds(source, sourceAddr, (int)count, emptys);
                    var ulres = CompressValues<ulong>(source, count * 2 + sourceAddr, count, emptys, TagType);
                    datas = CompressTimers2(source, sourceAddr, (int)count, emptys2);
cdy816's avatar
cdy816 已提交
360
                    rcount = count - emptys2.WriteIndex - 1;
361
                    //写入时间
cdy816's avatar
cdy816 已提交
362 363
                    target.WriteInt(targetAddr, rcount);
                    rsize += 4;
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
                    target.Write((int)datas.Length);
                    target.Write(datas);
                    rsize += 4;
                    rsize += datas.Length;
                    //写入数据
                    target.Write(ulres.Length);
                    target.Write(ulres);
                    rsize += 4;
                    rsize += ulres.Length;
                    //质量
                    emptys2.ReadIndex = 0;
                    cqus = CompressQulitys(source, count * 10 + sourceAddr, count, emptys2);
                    target.Write(cqus.Length);
                    target.Write(cqus);
                    rsize += 4;
                    rsize += cqus.Length;
                    break;
                case TagType.Long:
                    FindEmpityIds(source, sourceAddr, (int)count, emptys);
                    var lres = CompressValues<long>(source, count * 2 + sourceAddr, count, emptys, TagType);
                    datas = CompressTimers2(source, sourceAddr, (int)count, emptys2);
cdy816's avatar
cdy816 已提交
385
                    rcount = count - emptys2.WriteIndex - 1;
386
                    //写入时间
cdy816's avatar
cdy816 已提交
387 388
                    target.WriteInt(targetAddr, rcount);
                    rsize += 4;
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
                    target.Write((int)datas.Length);
                    target.Write(datas);
                    rsize += 4;
                    rsize += datas.Length;
                    //写入数据
                    target.Write(lres.Length);
                    target.Write(lres);
                    rsize += 4;
                    rsize += lres.Length;
                    //质量
                    emptys2.ReadIndex = 0;
                    cqus = CompressQulitys(source, count * 10 + sourceAddr, count, emptys2);
                    target.Write(cqus.Length);
                    target.Write(cqus);
                    rsize += 4;
                    rsize += cqus.Length;
                    break;
                case TagType.DateTime:
                    datas = CompressTimers(source, sourceAddr, (int)count, emptys);

                    var dres = CompressValues<ulong>(source, count * 2 + sourceAddr, count, emptys, TagType);

cdy816's avatar
cdy816 已提交
411
                    rcount = count - emptys2.WriteIndex - 1;
412
                    //写入时间
cdy816's avatar
cdy816 已提交
413 414
                    target.WriteInt(targetAddr, rcount);
                    rsize += 4;
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
                    target.Write((int)datas.Length);
                    target.Write(datas);
                    rsize += 4;
                    rsize += datas.Length;
                    //写入数据
                    target.Write(dres.Length);
                    target.Write(dres);
                    rsize += 4;
                    rsize += dres.Length;
                    //质量
                    emptys.ReadIndex = 0;
                    cqus = CompressQulitys(source, count * 10 + sourceAddr, count, emptys);
                    target.Write(cqus.Length);
                    target.Write(cqus);
                    rsize += 4;
                    rsize += cqus.Length;
                    break;
                case TagType.Double:
                    FindEmpityIds(source, sourceAddr, (int)count, emptys);
cdy816's avatar
cdy816 已提交
434 435 436 437 438 439 440 441
                    if (mDCompress == null)
                    {
                        mDCompress = new DoubleCompressBuffer(count) { MemoryBlock = mMarshalMemory, VarintMemory = mVarintMemory };
                    }
                    else
                    {
                        mDCompress.CheckAndResizeTo(count);
                    }
442 443 444

                    var ddres = CompressValues<double>(source, count * 2 + sourceAddr, count, emptys, TagType);
                    datas = CompressTimers2(source, sourceAddr, (int)count, emptys2);
cdy816's avatar
cdy816 已提交
445
                    rcount = count - emptys2.WriteIndex - 1;
446
                    //写入时间
cdy816's avatar
cdy816 已提交
447 448
                    target.WriteInt(targetAddr, rcount);
                    rsize += 4;
449 450
                    target.Write((int)datas.Length);
                    rsize += 4;
cdy816's avatar
cdy816 已提交
451
                    target.Write(datas);
452
                    rsize += datas.Length;
cdy816's avatar
cdy816 已提交
453 454 455 456 457 458

                    if(rcount>0 && datas.Length==0)
                    {
                        LoggerService.Service.Debug("DeadAreaCompressUnit", "压缩后数据长度为0 :"+rcount);
                    }

459 460 461 462 463 464 465 466 467 468 469 470
                    //写入数据
                    target.Write(ddres.Length);
                    target.Write(ddres);
                    rsize += 4;
                    rsize += ddres.Length;
                    //质量
                    emptys2.ReadIndex = 0;
                    cqus = CompressQulitys(source, count * 10 + sourceAddr, count, emptys2);
                    target.Write(cqus.Length);
                    target.Write(cqus);
                    rsize += 4;
                    rsize += cqus.Length;
471 472 473 474 475 476 477 478

                    //StringBuilder sb = new StringBuilder();
                    //foreach(var vv in dvals)
                    //{
                    //    sb.Append(dtims[vv.Key] + "," + vv.Value+";");
                    //}
                    //System.IO.File.WriteAllText(DateTime.Now.Ticks.ToString() + ".deadarea", sb.ToString());

479 480 481
                    break;
                case TagType.Float:
                    FindEmpityIds(source, sourceAddr, (int)count, emptys);
cdy816's avatar
cdy816 已提交
482 483 484 485 486 487 488 489
                    if (mFCompress == null)
                    {
                        mFCompress = new FloatCompressBuffer(count) { MemoryBlock = mMarshalMemory, VarintMemory = mVarintMemory };
                    }
                    else
                    {
                        mFCompress.CheckAndResizeTo(count);
                    }
490 491 492

                    var fres = CompressValues<float>(source, count * 2 + sourceAddr, count, emptys, TagType);
                    datas = CompressTimers2(source, sourceAddr, (int)count, emptys2);
cdy816's avatar
cdy816 已提交
493
                    rcount = count - emptys2.WriteIndex - 1;
494
                    //写入时间
cdy816's avatar
cdy816 已提交
495 496
                    target.WriteInt(targetAddr, rcount);
                    rsize += 4;
497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
                    target.Write((int)datas.Length);
                    target.Write(datas);
                    rsize += 4;
                    rsize += datas.Length;
                    //写入数据
                    target.Write(fres.Length);
                    target.Write(fres);
                    rsize += 4;
                    rsize += fres.Length;
                    //质量
                    emptys2.ReadIndex = 0;
                    cqus = CompressQulitys(source, count * 6 + sourceAddr, count, emptys2);
                    target.Write(cqus.Length);
                    target.Write(cqus);
                    rsize += 4;
                    rsize += cqus.Length;
                    break;
                case TagType.String:

                    datas = CompressTimers(source, sourceAddr, (int)count, emptys);
cdy816's avatar
cdy816 已提交
517
                    rcount = count - emptys.WriteIndex - 1;
518
                    //写入时间
cdy816's avatar
cdy816 已提交
519 520
                    target.WriteInt(targetAddr, rcount);
                    rsize += 4;
521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543
                    target.Write((int)datas.Length);
                    target.Write(datas);
                    rsize += 4;
                    rsize += datas.Length;

                    //写入数据

                    var vals = source.ReadStrings(count * 2 + (int)sourceAddr, count);
                    var qus = source.ReadBytes(count);
                    var sres = CompressValues(vals, emptys);
                    target.Write(sres.Length);
                    target.Write(sres);
                    rsize += 4;
                    rsize += sres.Length;
                    emptys.ReadIndex = 0;
                    cqus = CompressQulitys(qus, emptys);
                    target.Write(cqus.Length);
                    target.Write(cqus);
                    rsize += 4;
                    rsize += cqus.Length;
                    break;
                case TagType.IntPoint:
                    datas = CompressTimers(source, sourceAddr, (int)count, emptys);
cdy816's avatar
cdy816 已提交
544
                    rcount = count - emptys.WriteIndex - 1;
545
                    //写入时间
cdy816's avatar
cdy816 已提交
546 547
                    target.WriteInt(targetAddr, rcount);
                    rsize += 4;
548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567
                    target.Write((int)datas.Length);
                    target.Write(datas);
                    rsize += 4;
                    rsize += datas.Length;
                    //数值
                    var ipres = CompressValues<IntPointData>(source, count * 2 + sourceAddr, count, emptys, TagType);
                    target.Write(ipres.Length);
                    target.Write(ipres);
                    rsize += 4;
                    rsize += ipres.Length;
                    //质量
                    emptys.ReadIndex = 0;
                    cqus = CompressQulitys(source, count * 10 + sourceAddr, count, emptys);
                    target.Write(cqus.Length);
                    target.Write(cqus);
                    rsize += 4;
                    rsize += cqus.Length;
                    break;
                case TagType.UIntPoint:
                    datas = CompressTimers(source, sourceAddr, (int)count, emptys);
cdy816's avatar
cdy816 已提交
568
                    rcount = count - emptys.WriteIndex - 1;
569
                    //写入时间
cdy816's avatar
cdy816 已提交
570 571
                    target.WriteInt(targetAddr, rcount);
                    rsize += 4;
572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591
                    target.Write((int)datas.Length);
                    target.Write(datas);
                    rsize += 4;
                    rsize += datas.Length;
                    //数值
                    ipres = CompressValues<UIntPointData>(source, count * 2 + sourceAddr, count, emptys, TagType);
                    target.Write(ipres.Length);
                    target.Write(ipres);
                    rsize += 4;
                    rsize += ipres.Length;
                    //质量
                    emptys.ReadIndex = 0;
                    cqus = CompressQulitys(source, count * 10 + sourceAddr, count, emptys);
                    target.Write(cqus.Length);
                    target.Write(cqus);
                    rsize += 4;
                    rsize += cqus.Length;
                    break;
                case TagType.LongPoint:
                    datas = CompressTimers(source, sourceAddr, (int)count, emptys);
cdy816's avatar
cdy816 已提交
592
                    rcount = count - emptys.WriteIndex - 1;
593
                    //写入时间
cdy816's avatar
cdy816 已提交
594 595
                    target.WriteInt(targetAddr, rcount);
                    rsize += 4;
596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615
                    target.Write((int)datas.Length);
                    target.Write(datas);
                    rsize += 4;
                    rsize += datas.Length;
                    //值
                    ipres = CompressValues<LongPointData>(source, count * 2 + sourceAddr, count, emptys, TagType);
                    target.Write(ipres.Length);
                    target.Write(ipres);
                    rsize += 4;
                    rsize += ipres.Length;
                    //质量
                    emptys.ReadIndex = 0;
                    cqus = CompressQulitys(source, count * 18 + sourceAddr, count, emptys);
                    target.Write(cqus.Length);
                    target.Write(cqus);
                    rsize += 4;
                    rsize += cqus.Length;
                    break;
                case TagType.ULongPoint:
                    datas = CompressTimers(source, sourceAddr, (int)count, emptys);
cdy816's avatar
cdy816 已提交
616
                    rcount = count - emptys.WriteIndex - 1;
617
                    //写入时间
cdy816's avatar
cdy816 已提交
618 619
                    target.WriteInt(targetAddr, rcount);
                    rsize += 4;
620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639
                    target.Write((int)datas.Length);
                    target.Write(datas);
                    rsize += 4;
                    rsize += datas.Length;
                    //值
                    ipres = CompressValues<ULongPointData>(source, count * 2 + sourceAddr, count, emptys, TagType);
                    target.Write(ipres.Length);
                    target.Write(ipres);
                    rsize += 4;
                    rsize += ipres.Length;
                    //质量
                    emptys.ReadIndex = 0;
                    cqus = CompressQulitys(source, count * 18 + sourceAddr, count, emptys);
                    target.Write(cqus.Length);
                    target.Write(cqus);
                    rsize += 4;
                    rsize += cqus.Length;
                    break;
                case TagType.IntPoint3:
                    datas = CompressTimers(source, sourceAddr, (int)count, emptys);
cdy816's avatar
cdy816 已提交
640
                    rcount = count - emptys.WriteIndex - 1;
641
                    //写入时间
cdy816's avatar
cdy816 已提交
642 643 644
                    target.WriteInt(targetAddr, rcount);
                    rsize += 4;

645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664
                    target.Write((int)datas.Length);
                    target.Write(datas);
                    rsize += 4;
                    rsize += datas.Length;
                    //值
                    ipres = CompressValues<IntPoint3Data>(source, count * 2 + sourceAddr, count, emptys, TagType);
                    target.Write(ipres.Length);
                    target.Write(ipres);
                    rsize += 4;
                    rsize += ipres.Length;
                    //质量
                    emptys.ReadIndex = 0;
                    cqus = CompressQulitys(source, count * 14 + sourceAddr, count, emptys);
                    target.Write(cqus.Length);
                    target.Write(cqus);
                    rsize += 4;
                    rsize += cqus.Length;
                    break;
                case TagType.UIntPoint3:
                    datas = CompressTimers(source, sourceAddr, (int)count, emptys);
cdy816's avatar
cdy816 已提交
665
                    rcount = count - emptys.WriteIndex - 1;
666
                    //写入时间
cdy816's avatar
cdy816 已提交
667 668
                    target.WriteInt(targetAddr, rcount);
                    rsize += 4;
669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688
                    target.Write((int)datas.Length);
                    target.Write(datas);
                    rsize += 4;
                    rsize += datas.Length;
                    //值
                    ipres = CompressValues<UIntPoint3Data>(source, count * 2 + sourceAddr, count, emptys, TagType);
                    target.Write(ipres.Length);
                    target.Write(ipres);
                    rsize += 4;
                    rsize += ipres.Length;
                    //质量
                    emptys.ReadIndex = 0;
                    cqus = CompressQulitys(source, count * 14 + sourceAddr, count, emptys);
                    target.Write(cqus.Length);
                    target.Write(cqus);
                    rsize += 4;
                    rsize += cqus.Length;
                    break;
                case TagType.LongPoint3:
                    datas = CompressTimers(source, sourceAddr, (int)count, emptys);
cdy816's avatar
cdy816 已提交
689
                    rcount = count - emptys.WriteIndex - 1;
690
                    //写入时间
cdy816's avatar
cdy816 已提交
691 692
                    target.WriteInt(targetAddr, rcount);
                    rsize += 4;
693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712
                    target.Write((int)datas.Length);
                    target.Write(datas);
                    rsize += 4;
                    rsize += datas.Length;
                    //值
                    ipres = CompressValues<LongPoint3Data>(source, count * 2 + sourceAddr, count, emptys, TagType);
                    target.Write(ipres.Length);
                    target.Write(ipres);
                    rsize += 4;
                    rsize += ipres.Length;
                    //质量
                    emptys.ReadIndex = 0;
                    cqus = CompressQulitys(source, count * 26 + sourceAddr, count, emptys);
                    target.Write(cqus.Length);
                    target.Write(cqus);
                    rsize += 4;
                    rsize += cqus.Length;
                    break;
                case TagType.ULongPoint3:
                    datas = CompressTimers(source, sourceAddr, (int)count, emptys);
cdy816's avatar
cdy816 已提交
713
                    rcount = count - emptys.WriteIndex - 1;
714
                    //写入时间
cdy816's avatar
cdy816 已提交
715 716
                    target.WriteInt(targetAddr, rcount);
                    rsize += 4;
717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760
                    target.Write((int)datas.Length);
                    target.Write(datas);
                    rsize += 4;
                    rsize += datas.Length;
                    //值
                    ipres = CompressValues<ULongPoint3Data>(source, count * 2 + sourceAddr, count, emptys, TagType);
                    target.Write(ipres.Length);
                    target.Write(ipres);
                    rsize += 4;
                    rsize += ipres.Length;
                    //质量
                    emptys.ReadIndex = 0;
                    cqus = CompressQulitys(source, count * 26 + sourceAddr, count, emptys);
                    target.Write(cqus.Length);
                    target.Write(cqus);
                    rsize += 4;
                    rsize += cqus.Length;
                    break;
            }
            return rsize;

        }


        /// <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>(IMemoryBlock source, long offset, int count, CustomQueue<int> emptys, TagType type)
        {
            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.Reset();

            bool isFirst = true;

            int ig = -1;
cdy816's avatar
cdy816 已提交
761
            ig = emptys.ReadIndex <= emptys.WriteIndex ? emptys.IncRead() : -1;
762
            
763 764


765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795
            switch (type)
            {
                case TagType.Byte:
                    byte sval = 0;
                    for (int i = 0; i < count; i++)
                    {
                        if (i != ig)
                        {
                            var id = source.ReadByte((int)offset + i);
                            if (isFirst)
                            {
                                sval = id;
                                mMarshalMemory.Write(id);
                                isFirst = false;
                            }
                            else
                            {
                                if (CheckIsNeedRecord(sval, id, deadArea, deadType))
                                {
                                    mMarshalMemory.Write(id);
                                    sval = id;
                                }
                                else
                                {
                                    emptys2.Insert(i);
                                }
                            }
                        }
                        else
                        {

cdy816's avatar
cdy816 已提交
796
                            ig = emptys.ReadIndex <= emptys.WriteIndex ? emptys.IncRead() : -1;
797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829
                            emptys2.Insert(i);
                        }
                    }
                    return mMarshalMemory.StartMemory.AsMemory<byte>(0, (int)mMarshalMemory.Position);
                case TagType.Short:
                    short ssval = 0;
                    for (int i = 0; i < count; i++)
                    {
                        if (i != ig)
                        {
                            var id = source.ReadShort((int)offset + i * 2);
                            if (isFirst)
                            {
                                mVarintMemory.WriteSInt32(id);
                                isFirst = false;
                                ssval = id;
                            }
                            else
                            {
                                if (CheckIsNeedRecord(ssval, id, deadArea, deadType))
                                {
                                    mVarintMemory.WriteSInt32(id - ssval);
                                    ssval = id;
                                }
                                else
                                {
                                    emptys2.Insert(i);
                                }
                            }
                        }
                        else
                        {

cdy816's avatar
cdy816 已提交
830
                            ig = emptys.ReadIndex <= emptys.WriteIndex ? emptys.IncRead() : -1;
831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863
                            emptys2.Insert(i);
                        }
                    }
                    break;
                case TagType.UShort:
                    ushort ussval = 0;
                    for (int i = 0; i < count; i++)
                    {
                        if (i != ig)
                        {
                            var id = source.ReadUShort((int)offset + i * 2);
                            if (isFirst)
                            {
                                mVarintMemory.WriteSInt32(id);
                                isFirst = false;
                                ussval = id;
                            }
                            else
                            {
                                if (CheckIsNeedRecord(ussval, id, deadArea, deadType))
                                {
                                    mVarintMemory.WriteSInt32(id - ussval);
                                    ussval = id;
                                }
                                else
                                {
                                    emptys2.Insert(i);
                                }
                            }
                        }
                        else
                        {

cdy816's avatar
cdy816 已提交
864
                            ig = emptys.ReadIndex <= emptys.WriteIndex ? emptys.IncRead() : -1;
865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896
                            emptys2.Insert(i);
                        }
                    }
                    break;
                case TagType.Int:
                    int isval = 0;
                    for (int i = 0; i < count; i++)
                    {
                        if (i != ig)
                        {
                            var id = source.ReadInt((int)offset + i * 4);
                            if (isFirst)
                            {
                                mVarintMemory.WriteInt32(id);
                                isFirst = false;
                                isval = id;
                            }
                            else
                            {
                                if (CheckIsNeedRecord(isval, id, deadArea, deadType))
                                {
                                    mVarintMemory.WriteSInt32(id - isval);
                                    isval = id;
                                }
                                else
                                {
                                    emptys2.Insert(i);
                                }
                            }
                        }
                        else
                        {
cdy816's avatar
cdy816 已提交
897
                            ig = emptys.ReadIndex <= emptys.WriteIndex ? emptys.IncRead() : -1;
898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929
                            emptys2.Insert(i);
                        }
                    }
                    break;
                case TagType.UInt:
                    uint usval = 0;
                    for (int i = 0; i < count; i++)
                    {
                        if (i != ig)
                        {
                            var id = source.ReadUInt((int)offset + i * 4);
                            if (isFirst)
                            {
                                mVarintMemory.WriteInt32(id);
                                isFirst = false;
                                usval = id;
                            }
                            else
                            {
                                if (CheckIsNeedRecord(usval, id, deadArea, deadType))
                                {
                                    mVarintMemory.WriteSInt32((int)(id - usval));
                                    usval = id;
                                }
                                else
                                {
                                    emptys2.Insert(i);
                                }
                            }
                        }
                        else
                        {
cdy816's avatar
cdy816 已提交
930
                            ig = emptys.ReadIndex <= emptys.WriteIndex ? emptys.IncRead() : -1;
931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962
                            emptys2.Insert(i);
                        }
                    }
                    break;
                case TagType.Long:
                    long lsval = 0;
                    for (int i = 0; i < count; i++)
                    {
                        if (i != ig)
                        {
                            var id = source.ReadLong((int)offset + i * 8);
                            if (isFirst)
                            {
                                mVarintMemory.WriteInt64(id);
                                isFirst = false;
                                lsval = id;
                            }
                            else
                            {
                                if (CheckIsNeedRecord(lsval, id, deadArea, deadType))
                                {
                                    mVarintMemory.WriteSInt64((id - lsval));
                                    lsval = id;
                                }
                                else
                                {
                                    emptys2.Insert(i);
                                }
                            }
                        }
                        else
                        {
cdy816's avatar
cdy816 已提交
963
                            ig = emptys.ReadIndex <= emptys.WriteIndex ? emptys.IncRead() : -1;
964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995
                            emptys2.Insert(i);
                        }
                    }
                    break;
                case TagType.ULong:
                    ulong ulsval = 0;
                    for (int i = 0; i < count; i++)
                    {
                        if (i != ig)
                        {
                            var id = source.ReadULong((int)offset + i * 8);
                            if (isFirst)
                            {
                                mVarintMemory.WriteInt64(id);
                                isFirst = false;
                                ulsval = id;
                            }
                            else
                            {
                                if (CheckIsNeedRecord(ulsval, id, deadArea, deadType))
                                {
                                    mVarintMemory.WriteSInt64((long)(id - ulsval));
                                    ulsval = id;
                                }
                                else
                                {
                                    emptys2.Insert(i);
                                }
                            }
                        }
                        else
                        {
cdy816's avatar
cdy816 已提交
996
                            ig = emptys.ReadIndex <= emptys.WriteIndex ? emptys.IncRead() : -1;
997 998 999 1000 1001 1002 1003 1004
                            emptys2.Insert(i);
                        }
                    }
                    break;
                case TagType.Double:
                    double dsval = 0;
                    mDCompress.Reset();
                    mDCompress.Precision = this.Precision;
1005 1006
                    //dvals.Clear();

1007 1008 1009 1010 1011 1012 1013 1014 1015 1016
                    for (int i = 0; i < count; i++)
                    {
                        if (i != ig)
                        {
                            var id = source.ReadDouble((int)offset + i * 8);
                            if (isFirst)
                            {
                                mDCompress.Append(id);
                                isFirst = false;
                                dsval = id;
1017
                                //dvals.Add(i, id);
1018 1019 1020 1021 1022 1023 1024 1025
                            }
                            else
                            {
                                if (CheckIsNeedRecord(dsval, id, deadArea, deadType))
                                {
                                    mDCompress.Append(id);
                                    // mMarshalMemory.Write(id);
                                    dsval = id;
1026
                                    //dvals.Add(i, id);
1027 1028 1029 1030 1031 1032 1033 1034 1035
                                }
                                else
                                {
                                    emptys2.Insert(i);
                                }
                            }
                        }
                        else
                        {
cdy816's avatar
cdy816 已提交
1036
                            ig = emptys.ReadIndex <= emptys.WriteIndex ? emptys.IncRead() : -1;
1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071
                            emptys2.Insert(i);
                        }
                    }
                    mDCompress.Compress();
                    return mMarshalMemory.StartMemory.AsMemory<byte>(0, (int)mMarshalMemory.Position);
                case TagType.Float:
                    mFCompress.Reset();
                    mFCompress.Precision = this.Precision;
                    float fsval = 0;
                    for (int i = 0; i < count; i++)
                    {
                        if (i != ig)
                        {
                            var id = source.ReadFloat((int)offset + i * 4);
                            if (isFirst)
                            {
                                mFCompress.Append(id);
                                isFirst = false;
                                fsval = id;
                            }
                            else
                            {
                                if (CheckIsNeedRecord(fsval, id, deadArea, deadType))
                                {
                                    mFCompress.Append(id);
                                    fsval = id;
                                }
                                else
                                {
                                    emptys2.Insert(i);
                                }
                            }
                        }
                        else
                        {
cdy816's avatar
cdy816 已提交
1072
                            ig = emptys.ReadIndex <= emptys.WriteIndex ? emptys.IncRead() : -1;
1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084
                            emptys2.Insert(i);
                        }
                    }
                    mFCompress.Compress();
                    return mMarshalMemory.StartMemory.AsMemory<byte>(0, (int)mMarshalMemory.Position);
                default:
                   return base.CompressValues<T>(source, offset, count, emptys, type);
            }
            return mVarintMemory.DataBuffer.AsMemory<byte>(0, (int)mVarintMemory.WritePosition);
        }
    }
}