Exemplo n.º 1
0
        public static (ByteWriter, string error) Encode2(IList <IClockValue> values, int startIndex, int count)
        {
            if (values == null || values.Count == 0)
            {
                return(null, null);
            }
            IClockValue    value0   = values[0];
            byte           datatype = value0.DataType;
            TimeEncoder    tenc     = (TimeEncoder)EncoderFactory.Get(DataTypeEnum.DateTime, count);
            IntegerEncoder qenc     = (IntegerEncoder)EncoderFactory.Get(DataTypeEnum.Integer, count);
            IEncoder       venc     = EncoderFactory.Get(datatype, count);

            if (datatype == DataTypeEnum.Double)
            {
                FloatEncoder encoder = (FloatEncoder)venc;
                for (int i = startIndex; i < startIndex + count; i++)
                {
                    ClockDouble value = (ClockDouble)values[i];
                    tenc.Write(value.Clock);
                    qenc.Write(value.Quality);
                    encoder.Write(value.Value);
                }
            }
            else if (datatype == DataTypeEnum.Boolean)
            {
                BooleanEncoder encoder = (BooleanEncoder)venc;
                for (int i = startIndex; i < startIndex + count; i++)
                {
                    ClockBoolean value = (ClockBoolean)values[i];
                    tenc.Write(value.Clock);
                    qenc.Write(value.Quality);
                    encoder.Write(value.Value);
                }
            }
            else if (datatype == DataTypeEnum.Integer)
            {
                IntegerEncoder encoder = (IntegerEncoder)venc;
                for (int i = startIndex; i < startIndex + count; i++)
                {
                    ClockInt64 value = (ClockInt64)values[i];
                    tenc.Write(value.Clock);
                    qenc.Write(value.Quality);
                    encoder.Write(value.Value);
                }
            }
            else if (datatype == DataTypeEnum.String)
            {
                StringEncoder encoder = (StringEncoder)venc;
                for (int i = startIndex; i < startIndex + count; i++)
                {
                    ClockString value = (ClockString)values[i];
                    tenc.Write(value.Clock);
                    qenc.Write(value.Quality);
                    encoder.Write(value.Value);
                }
            }
            var ts = tenc.Bytes();

            venc.Flush();//for floatencoder
            var qs = qenc.Bytes();
            var vs = venc.Bytes();

            EncoderFactory.Put(DataTypeEnum.DateTime, tenc);
            EncoderFactory.Put(DataTypeEnum.Integer, qenc);
            EncoderFactory.Put(datatype, venc);
            if (ts.Item2 != null)
            {
                return(null, ts.Item2);
            }
            if (vs.Item2 != null)
            {
                return(null, vs.Item2);
            }
            if (qs.Item2 != null)
            {
                return(null, qs.Item2);
            }
            ByteWriter tswriter = ts.Item1;
            ByteWriter vswriter = vs.Item1;
            ByteWriter qswriter = qs.Item1;
            ByteWriter result   = new ByteWriter(1 + Varint.MaxVarintLen64 + tswriter.Length + vswriter.Length + qswriter.Length);

            result.Write(datatype); //first byte valuetype
            result.Write(Varint.GetBytes(tswriter.Length));
            result.Write(tswriter.EndWrite(), 0, tswriter.Length);
            tswriter.Release();
            result.Write(Varint.GetBytes(vswriter.Length));
            result.Write(vswriter.EndWrite(), 0, vswriter.Length);
            vswriter.Release();
            result.Write(qswriter.EndWrite(), 0, qswriter.Length);
            qswriter.Release();
            return(result, null);
        }
Exemplo n.º 2
0
        public static (ByteWriter, string error) Encode(IList <IClockValue> values, int startIndex, int count)
        {
            if (values == null || values.Count == 0)
            {
                return(null, null);
            }
            IClockValue value0   = values[0];
            byte        datatype = value0.DataType;

            ulong[] ts = ArrayPool <ulong> .Shared.Rent(count);

            Span <ulong> ts_span = new Span <ulong>(ts, 0, count);

            long[] qs = ArrayPool <long> .Shared.Rent(count);

            Span <long> qs_span = new Span <long>(qs, 0, count);

            ByteWriter tswriter = null, vswriter = null, qswriter = null;
            string     tserror = null, vserror = null, qserror = null;

            if (datatype == DataTypeEnum.Double)
            {
                double[] vs = ArrayPool <double> .Shared.Rent(count);

                Span <double> vs_span = new Span <double>(vs, 0, count);
                int           j       = 0;
                for (int i = startIndex; i < startIndex + count; i++, j++)
                {
                    ClockDouble value = (ClockDouble)values[i];
                    ts_span[j] = (ulong)value.Clock;
                    vs_span[j] = value.Value;
                    qs_span[j] = value.Quality;
                }
                BatchDouble encoder = (BatchDouble)CoderFactory.Get(datatype);
                (vswriter, vserror) = encoder.EncodingAll(vs_span);
                ArrayPool <double> .Shared.Return(vs);

                CoderFactory.Put(datatype, encoder);
            }
            else if (datatype == DataTypeEnum.Boolean)
            {
                bool[] vs = ArrayPool <bool> .Shared.Rent(count);

                Span <bool> vs_span = new Span <bool>(vs, 0, count);
                int         j       = 0;
                for (int i = startIndex; i < startIndex + count; i++, j++)
                {
                    ClockBoolean value = (ClockBoolean)values[i];
                    ts_span[j] = (ulong)value.Clock;
                    vs_span[j] = value.Value;
                    qs_span[j] = value.Quality;
                }
                BatchBoolean encoder = (BatchBoolean)CoderFactory.Get(datatype);
                (vswriter, vserror) = encoder.EncodingAll(vs_span);
                ArrayPool <bool> .Shared.Return(vs);

                CoderFactory.Put(datatype, encoder);
            }
            else if (datatype == DataTypeEnum.Integer)
            {
                long[] vs = ArrayPool <long> .Shared.Rent(count);

                Span <long> vs_span = new Span <long>(vs, 0, count);
                int         j       = 0;
                for (int i = startIndex; i < startIndex + count; i++, j++)
                {
                    ClockInt64 value = (ClockInt64)values[i];
                    ts_span[j] = (ulong)value.Clock;
                    vs_span[j] = value.Value;
                    qs_span[j] = value.Quality;
                }
                BatchInt64 encoder = (BatchInt64)CoderFactory.Get(datatype);
                (vswriter, vserror) = encoder.EncodingAll(vs_span);
                ArrayPool <long> .Shared.Return(vs);

                CoderFactory.Put(datatype, encoder);
            }
            else if (datatype == DataTypeEnum.String)
            {
                string[] vs = ArrayPool <string> .Shared.Rent(count);

                Span <string> vs_span = new Span <string>(vs, 0, count);
                int           j       = 0;
                for (int i = startIndex; i < startIndex + count; i++, j++)
                {
                    ClockString value = (ClockString)values[i];
                    ts_span[j] = (ulong)value.Clock;
                    vs_span[j] = value.Value;
                    qs_span[j] = value.Quality;
                }
                BatchString encoder = (BatchString)CoderFactory.Get(datatype);
                (vswriter, vserror) = encoder.EncodingAll(vs_span);
                ArrayPool <string> .Shared.Return(vs);

                CoderFactory.Put(datatype, encoder);
            }
            bool good = (vserror == null && vswriter != null);

            if (good)
            {
                BatchTimeStamp tenc = (BatchTimeStamp)CoderFactory.Get(DataTypeEnum.DateTime);
                (tswriter, tserror) = tenc.EncodingAll(ts_span);
                CoderFactory.Put(DataTypeEnum.DateTime, tenc);
                good = (tswriter != null && tserror == null);
                if (good)
                {
                    BatchInt64 qenc = (BatchInt64)CoderFactory.Get(DataTypeEnum.Integer);
                    (qswriter, qserror) = qenc.EncodingAll(qs_span);
                    CoderFactory.Put(DataTypeEnum.Integer, qenc);
                    good = (qswriter != null && qserror == null);
                }
            }
            ArrayPool <ulong> .Shared.Return(ts);

            ArrayPool <long> .Shared.Return(qs);

            if (good)
            {
                ByteWriter result = new ByteWriter(1 + Varint.MaxVarintLen64 + tswriter.Length + vswriter.Length + qswriter.Length);
                result.Write(datatype); //first byte valuetype
                result.Write(Varint.GetBytes(tswriter.Length));
                result.Write(tswriter.EndWrite(), 0, tswriter.Length);
                result.Write(Varint.GetBytes(vswriter.Length));
                result.Write(vswriter.EndWrite(), 0, vswriter.Length);
                result.Write(Varint.GetBytes(qswriter.Length));
                result.Write(qswriter.EndWrite(), 0, qswriter.Length);
                return(result, null);
            }
            if (tswriter != null)
            {
                tswriter.Release();
            }
            if (vswriter != null)
            {
                vswriter.Release();
            }
            if (qswriter != null)
            {
                qswriter.Release();
            }
            return(null, vserror != null ? vserror : tserror != null ? tserror : qserror);
        }