public static ClockValues Decode2(byte[] bytes, int startindex) { byte datatype = bytes[startindex]; startindex++; int index; index = startindex + Varint.Read(bytes, startindex, out int tsLen);//时标段; TimeDecoder tdec = (TimeDecoder)DecoderFactory.Get(DataTypeEnum.DateTime); tdec.SetBytes(bytes, index, tsLen); index = index + tsLen; index = index + Varint.Read(bytes, index, out int vsLen);//数值段; IDecoder vdec = DecoderFactory.Get(datatype); vdec.SetBytes(bytes, index, vsLen); index = index + vsLen; index = index + Varint.Read(bytes, index, out int qsLen);//质量段; IntegerDecoder qdec = (IntegerDecoder)DecoderFactory.Get(DataTypeEnum.Integer); qdec.SetBytes(bytes, index, qsLen); ClockValues result = new ClockValues(); if (datatype == DataTypeEnum.Double) { FloatDecoder decoder = (FloatDecoder)vdec; while (tdec.Next() && vdec.Next() && qdec.Next()) { result.Append(decoder.Create(tdec.Read(), decoder.Read(), (int)qdec.Read())); } } else if (datatype == DataTypeEnum.Boolean) { BooleanDecoder decoder = (BooleanDecoder)vdec; while (tdec.Next() && vdec.Next() && qdec.Next()) { result.Append(decoder.Create(tdec.Read(), decoder.Read(), (int)qdec.Read())); } } else if (datatype == DataTypeEnum.Integer) { IntegerDecoder decoder = (IntegerDecoder)vdec; while (tdec.Next() && vdec.Next() && qdec.Next()) { result.Append(decoder.Create(tdec.Read(), decoder.Read(), (int)qdec.Read())); } } else if (datatype == DataTypeEnum.String) { StringDecoder decoder = (StringDecoder)vdec; while (tdec.Next() && vdec.Next() && qdec.Next()) { result.Append(decoder.Create(tdec.Read(), decoder.Read(), (int)qdec.Read())); } } DecoderFactory.Put(DataTypeEnum.DateTime, tdec); DecoderFactory.Put(DataTypeEnum.Integer, qdec); DecoderFactory.Put(datatype, vdec); return(result); }
public static ClockValues Decode(byte[] bytes, int startindex, long start, long end) { Span <byte> span = new Span <byte>(bytes); byte datatype = bytes[startindex]; span = span.Slice(1); //解析时标段; int nn = Varint.Read(span, out int tsLen); Span <byte> ts_src_span = span.Slice(nn, tsLen); ulong[] ts = ArrayPool <ulong> .Shared.Rent(Constants.DefaultMaxPointsPerBlock); Span <ulong> ts_to_span = new Span <ulong>(ts, 0, Constants.DefaultMaxPointsPerBlock); BatchTimeStamp tdec = (BatchTimeStamp)CoderFactory.Get(DataTypeEnum.DateTime); (int tscount, string tserror) = tdec.DecodeAll(ts_src_span, ts_to_span); CoderFactory.Put(DataTypeEnum.DateTime, tdec); if (tscount == 0 || tserror != null) { ArrayPool <ulong> .Shared.Return(ts); return(null); } span = span.Slice(nn + tsLen); //读取数值段. nn = Varint.Read(span, out int vsLen); Span <byte> vs_src_span = span.Slice(nn, vsLen); span = span.Slice(nn + vsLen); //解析质量段. nn = Varint.Read(span, out int qsLen); Span <byte> qs_src_span = span.Slice(nn, qsLen); BatchInt64 qdec = (BatchInt64)CoderFactory.Get(DataTypeEnum.Integer); long[] qs = ArrayPool <long> .Shared.Rent(tscount); Span <long> qs_to_span = new Span <long>(qs, 0, tscount); (int qscount, string qserror) = qdec.DecodeAll(qs_src_span, qs_to_span); CoderFactory.Put(DataTypeEnum.Integer, qdec); if (qscount != tscount || qserror != null) { ArrayPool <long> .Shared.Return(qs); return(null); } //解析数据段. ClockValues result = null; if (datatype == DataTypeEnum.Double) { BatchDouble decoder = (BatchDouble)CoderFactory.Get(datatype); double[] vs = ArrayPool <double> .Shared.Rent(tscount + 1); Span <double> vs_to_span = new Span <double>(vs, 0, tscount + 1); (int vscount, string vserror) = decoder.DecodeAll(vs_src_span, vs_to_span); CoderFactory.Put(datatype, decoder); if (vscount >= tscount && vserror == null) { result = new ClockValues(tscount); for (int i = 0; i < tscount; i++) { long clock = (long)ts_to_span[i]; if (clock > end) { break; } if (clock >= start) { result.Append(new ClockDouble(clock, vs_to_span[i], (int)qs_to_span[i])); } } } ArrayPool <double> .Shared.Return(vs); } else if (datatype == DataTypeEnum.Boolean) { BatchBoolean decoder = (BatchBoolean)CoderFactory.Get(datatype); bool[] vs = ArrayPool <bool> .Shared.Rent(tscount); Span <bool> vs_to_span = new Span <bool>(vs, 0, tscount); (int vscount, string vserror) = decoder.DecodeAll(vs_src_span, vs_to_span); CoderFactory.Put(datatype, decoder); if (vscount == tscount && vserror == null) { result = new ClockValues(tscount); for (int i = 0; i < tscount; i++) { long clock = (long)ts_to_span[i]; if (clock > end) { break; } if (clock >= start) { result.Append(new ClockBoolean(clock, vs_to_span[i], (int)qs_to_span[i])); } } } ArrayPool <bool> .Shared.Return(vs); } else if (datatype == DataTypeEnum.Integer) { BatchInt64 decoder = (BatchInt64)CoderFactory.Get(datatype); long[] vs = ArrayPool <long> .Shared.Rent(tscount); Span <long> vs_to_span = new Span <long>(vs, 0, tscount); (int vscount, string vserror) = decoder.DecodeAll(vs_src_span, vs_to_span); CoderFactory.Put(datatype, decoder); if (vscount == tscount && vserror == null) { result = new ClockValues(tscount); for (int i = 0; i < tscount; i++) { long clock = (long)ts_to_span[i]; if (clock > end) { break; } if (clock >= start) { result.Append(new ClockInt64(clock, vs_to_span[i], (int)qs_to_span[i])); } } } ArrayPool <long> .Shared.Return(vs); } else if (datatype == DataTypeEnum.String) { BatchString decoder = (BatchString)CoderFactory.Get(datatype); string[] vs = ArrayPool <string> .Shared.Rent(tscount); Span <string> vs_to_span = new Span <string>(vs, 0, tscount); (int vscount, string vserror) = decoder.DecodeAll(vs_src_span, vs_to_span); CoderFactory.Put(datatype, decoder); if (vscount == tscount && vserror == null) { result = new ClockValues(tscount); for (int i = 0; i < tscount; i++) { long clock = (long)ts_to_span[i]; if (clock > end) { break; } if (clock >= start) { result.Append(new ClockString(clock, vs_to_span[i], (int)qs_to_span[i])); } } } ArrayPool <string> .Shared.Return(vs); } return(result); }
public string UnmarshalBinary(byte[] b, int startindex, int endindex) { Values.Clear(); int i = startindex; while (i < endindex) { byte typ = b[i]; i++; if (i + 8 > endindex) { return(Constants.ErrWALCorrupt); } ulong sid = BitConverter.ToUInt64(b, i); i += 8; if (i + 4 > endindex) { return(Constants.ErrWALCorrupt); } int nvals = BitConverter.ToInt32(b, i); i += 4; switch (typ) { case DataTypeEnum.Double: if (i + 16 * nvals > endindex) { return(Constants.ErrWALCorrupt); } ClockValues values = new ClockValues(nvals); for (int j = 0; j < nvals; j++) { long un = BitConverter.ToInt64(b, i); i += 8; double v = FloatDecoder.Float64frombits(BitConverter.ToUInt64(b, i)); i += 8; int q = BitConverter.ToInt32(b, i); i += 4; values.Append(new ClockDouble(un, v, q)); } Values[sid] = values; break; case DataTypeEnum.Integer: if (i + 16 * nvals > endindex) { return(Constants.ErrWALCorrupt); } values = new ClockValues(nvals); for (int j = 0; j < nvals; j++) { long un = BitConverter.ToInt64(b, i); i += 8; long v = (long)BitConverter.ToUInt64(b, i); i += 8; int q = BitConverter.ToInt32(b, i); i += 4; values.Append(new ClockInt64(un, v, q)); } Values[sid] = values; break; case DataTypeEnum.Boolean: if (i + 9 * nvals > endindex) { return(Constants.ErrWALCorrupt); } values = new ClockValues(nvals); for (int j = 0; j < nvals; j++) { long un = BitConverter.ToInt64(b, i); i += 8; bool v = b[i] == 1 ? true : false; i += 1; int q = BitConverter.ToInt32(b, i); i += 4; values.Append(new ClockBoolean(un, v, q)); } Values[sid] = values; break; case DataTypeEnum.String: values = new ClockValues(nvals); for (int j = 0; j < nvals; j++) { if (i + 12 > endindex) { return(Constants.ErrWALCorrupt); } long un = BitConverter.ToInt64(b, i); i += 8; int length = BitConverter.ToInt32(b, i); i += 4; if (i + length > endindex) { return(Constants.ErrWALCorrupt); } string v = System.Text.Encoding.Default.GetString(b, i, length); i += length; int q = BitConverter.ToInt32(b, i); i += 4; values.Append(new ClockString(un, v, q)); } Values[sid] = values; break; default: return(string.Format("unsupported value type: {0}", typ)); } } return(null); }