DeadAreaCompressUnit2.cs 43.0 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;

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

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

                    if (isFirst)
                    {
                        mVarintMemory2.WriteInt32(id);
                        isFirst = false;
                    }
                    else
                    {
                        mVarintMemory2.WriteInt32(id - preids);
                    }
133 134
                    //dtims.Add(i, id);
                    preids = id;
135 136 137
                }
                else
                {
cdy816's avatar
cdy816 已提交
138
                    ig = emptyIds.ReadIndex <= emptyIds.WriteIndex ? emptyIds.IncRead() : -1;
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
                    //    emptyIds.TryDequeue(out ig);
                }
            }
            return mVarintMemory2.DataBuffer.AsMemory(0, (int)mVarintMemory.WritePosition);
        }

        /// <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);
            }

176 177 178 179 180 181 182 183
            if (emptys2.Length < count)
            {
                emptys2 = new CustomQueue<int>(count * 2);
            }
            else
            {
                emptys2.Reset();
            }
184 185 186 187 188 189 190 191 192 193 194

            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 已提交
195
                    int rcount = count - emptys.WriteIndex - 1;
196
                    //写入时间
cdy816's avatar
cdy816 已提交
197 198
                    target.WriteInt(targetAddr, rcount);
                    rsize += 4;
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
                    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 已提交
224
                    rcount = count - emptys2.WriteIndex - 1;
225
                    //写入时间
cdy816's avatar
cdy816 已提交
226 227
                    target.WriteInt(targetAddr, rcount);
                    rsize += 4;
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
                    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 已提交
252
                    rcount = count - emptys2.WriteIndex - 1;
253
                    //写入时间
cdy816's avatar
cdy816 已提交
254 255
                    target.WriteInt(targetAddr, rcount);
                    rsize += 4;
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
                    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 已提交
278
                    rcount = count - emptys2.WriteIndex - 1;
279
                    //写入时间
cdy816's avatar
cdy816 已提交
280 281
                    target.WriteInt(targetAddr, rcount);
                    rsize += 4;
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
                    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 已提交
304
                    rcount = count - emptys2.WriteIndex - 1;
305
                    //写入时间
cdy816's avatar
cdy816 已提交
306 307
                    target.WriteInt(targetAddr, rcount);
                    rsize += 4;
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
                    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 已提交
329
                    rcount = count - emptys2.WriteIndex - 1;
330
                    //写入时间
cdy816's avatar
cdy816 已提交
331 332
                    target.WriteInt(targetAddr, rcount);
                    rsize += 4;
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
                    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 已提交
354
                    rcount = count - emptys2.WriteIndex - 1;
355
                    //写入时间
cdy816's avatar
cdy816 已提交
356 357
                    target.WriteInt(targetAddr, rcount);
                    rsize += 4;
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
                    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 已提交
379
                    rcount = count - emptys2.WriteIndex - 1;
380
                    //写入时间
cdy816's avatar
cdy816 已提交
381 382
                    target.WriteInt(targetAddr, rcount);
                    rsize += 4;
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
                    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 已提交
405
                    rcount = count - emptys2.WriteIndex - 1;
406
                    //写入时间
cdy816's avatar
cdy816 已提交
407 408
                    target.WriteInt(targetAddr, rcount);
                    rsize += 4;
409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
                    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);
                    if (mDCompress == null) mDCompress = new DoubleCompressBuffer(310) { MemoryBlock = mMarshalMemory, VarintMemory = mVarintMemory };

                    var ddres = CompressValues<double>(source, count * 2 + sourceAddr, count, emptys, TagType);
                    datas = CompressTimers2(source, sourceAddr, (int)count, emptys2);
cdy816's avatar
cdy816 已提交
432
                    rcount = count - emptys2.WriteIndex - 1;
433
                    //写入时间
cdy816's avatar
cdy816 已提交
434 435
                    target.WriteInt(targetAddr, rcount);
                    rsize += 4;
436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451
                    target.Write((int)datas.Length);
                    target.Write(datas);
                    rsize += 4;
                    rsize += datas.Length;
                    //写入数据
                    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;
452 453 454 455 456 457 458 459

                    //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());

460 461 462 463 464 465 466
                    break;
                case TagType.Float:
                    FindEmpityIds(source, sourceAddr, (int)count, emptys);
                    if (mFCompress == null) mFCompress = new FloatCompressBuffer(310) { MemoryBlock = mMarshalMemory, VarintMemory = mVarintMemory };

                    var fres = CompressValues<float>(source, count * 2 + sourceAddr, count, emptys, TagType);
                    datas = CompressTimers2(source, sourceAddr, (int)count, emptys2);
cdy816's avatar
cdy816 已提交
467
                    rcount = count - emptys2.WriteIndex - 1;
468
                    //写入时间
cdy816's avatar
cdy816 已提交
469 470
                    target.WriteInt(targetAddr, rcount);
                    rsize += 4;
471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490
                    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 已提交
491
                    rcount = count - emptys.WriteIndex - 1;
492
                    //写入时间
cdy816's avatar
cdy816 已提交
493 494
                    target.WriteInt(targetAddr, rcount);
                    rsize += 4;
495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517
                    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 已提交
518
                    rcount = count - emptys.WriteIndex - 1;
519
                    //写入时间
cdy816's avatar
cdy816 已提交
520 521
                    target.WriteInt(targetAddr, rcount);
                    rsize += 4;
522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541
                    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 已提交
542
                    rcount = count - emptys.WriteIndex - 1;
543
                    //写入时间
cdy816's avatar
cdy816 已提交
544 545
                    target.WriteInt(targetAddr, rcount);
                    rsize += 4;
546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565
                    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 已提交
566
                    rcount = count - emptys.WriteIndex - 1;
567
                    //写入时间
cdy816's avatar
cdy816 已提交
568 569
                    target.WriteInt(targetAddr, rcount);
                    rsize += 4;
570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589
                    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 已提交
590
                    rcount = count - emptys.WriteIndex - 1;
591
                    //写入时间
cdy816's avatar
cdy816 已提交
592 593
                    target.WriteInt(targetAddr, rcount);
                    rsize += 4;
594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613
                    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 已提交
614
                    rcount = count - emptys.WriteIndex - 1;
615
                    //写入时间
cdy816's avatar
cdy816 已提交
616 617 618
                    target.WriteInt(targetAddr, rcount);
                    rsize += 4;

619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638
                    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 已提交
639
                    rcount = count - emptys.WriteIndex - 1;
640
                    //写入时间
cdy816's avatar
cdy816 已提交
641 642
                    target.WriteInt(targetAddr, rcount);
                    rsize += 4;
643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662
                    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 已提交
663
                    rcount = count - emptys.WriteIndex - 1;
664
                    //写入时间
cdy816's avatar
cdy816 已提交
665 666
                    target.WriteInt(targetAddr, rcount);
                    rsize += 4;
667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686
                    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 已提交
687
                    rcount = count - emptys.WriteIndex - 1;
688
                    //写入时间
cdy816's avatar
cdy816 已提交
689 690
                    target.WriteInt(targetAddr, rcount);
                    rsize += 4;
691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734
                    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 已提交
735
            ig = emptys.ReadIndex <= emptys.WriteIndex ? emptys.IncRead() : -1;
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 761 762 763 764 765 766 767 768 769
            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 已提交
770
                            ig = emptys.ReadIndex <= emptys.WriteIndex ? emptys.IncRead() : -1;
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 796 797 798 799 800 801 802 803
                            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 已提交
804
                            ig = emptys.ReadIndex <= emptys.WriteIndex ? emptys.IncRead() : -1;
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 830 831 832 833 834 835 836 837
                            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 已提交
838
                            ig = emptys.ReadIndex <= emptys.WriteIndex ? emptys.IncRead() : -1;
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 864 865 866 867 868 869 870
                            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 已提交
871
                            ig = emptys.ReadIndex <= emptys.WriteIndex ? emptys.IncRead() : -1;
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 897 898 899 900 901 902 903
                            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 已提交
904
                            ig = emptys.ReadIndex <= emptys.WriteIndex ? emptys.IncRead() : -1;
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 930 931 932 933 934 935 936
                            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 已提交
937
                            ig = emptys.ReadIndex <= emptys.WriteIndex ? emptys.IncRead() : -1;
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 963 964 965 966 967 968 969
                            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 已提交
970
                            ig = emptys.ReadIndex <= emptys.WriteIndex ? emptys.IncRead() : -1;
971 972 973 974 975 976 977 978
                            emptys2.Insert(i);
                        }
                    }
                    break;
                case TagType.Double:
                    double dsval = 0;
                    mDCompress.Reset();
                    mDCompress.Precision = this.Precision;
979 980
                    //dvals.Clear();

981 982 983 984 985 986 987 988 989 990
                    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;
991
                                //dvals.Add(i, id);
992 993 994 995 996 997 998 999
                            }
                            else
                            {
                                if (CheckIsNeedRecord(dsval, id, deadArea, deadType))
                                {
                                    mDCompress.Append(id);
                                    // mMarshalMemory.Write(id);
                                    dsval = id;
1000
                                    //dvals.Add(i, id);
1001 1002 1003 1004 1005 1006 1007 1008 1009
                                }
                                else
                                {
                                    emptys2.Insert(i);
                                }
                            }
                        }
                        else
                        {
cdy816's avatar
cdy816 已提交
1010
                            ig = emptys.ReadIndex <= emptys.WriteIndex ? emptys.IncRead() : -1;
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045
                            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 已提交
1046
                            ig = emptys.ReadIndex <= emptys.WriteIndex ? emptys.IncRead() : -1;
1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058
                            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);
        }
    }
}