// from bytes public override void Unpack(byte[] data, int startIndex) { uint tmpUInt; VarintType type; // remaining size int startOffsetOfRemainingSize = 0; (tmpUInt, type) = VarintBitConverter.FromVarint(data, startIndex + startOffsetOfRemainingSize); // header size int startOffsetOfHeaderSize = startOffsetOfRemainingSize + VarintSize.GetVarintSize(tmpUInt); (tmpUInt, type) = VarintBitConverter.FromVarint(data, startIndex + startOffsetOfHeaderSize); // key-idx size int startOffsetOfKeyIdxSize = startOffsetOfHeaderSize + VarintSize.GetVarintSize(tmpUInt); (tmpUInt, type) = VarintBitConverter.FromVarint(data, startIndex + startOffsetOfKeyIdxSize); // key-pk size int startOffsetOfKeyPKSize = startOffsetOfKeyIdxSize + VarintSize.GetVarintSize(tmpUInt); (tmpUInt, type) = VarintBitConverter.FromVarint(data, startIndex + startOffsetOfKeyPKSize); // key-idx int startOffsetOfKeyIdx = startOffsetOfKeyPKSize + VarintSize.GetVarintSize(tmpUInt); this.Key = new DBRecord(data, startOffsetOfKeyIdx + startIndex); // key-pk int startOffsetOfKeyPK = startOffsetOfKeyIdx + this.Key.RecordSize; this.PrimaryKey = new DBRecord(data, startOffsetOfKeyPK + startIndex); }
// from raw data to this object public void Unpack(byte[] data, int startIndex) { InitializeEmpty(); // first byte this.HeaderSize = data[startIndex]; // header int headerOffset = 1; while (headerOffset < this.HeaderSize) { uint value; VarintType varintType; (value, varintType) = VarintBitConverter.FromVarint(data, headerOffset + startIndex); this.HeaderList.Add(value); if (varintType == VarintType.Varint32) { // string headerOffset += 4; } else { headerOffset += 1; } } int fieldStartIndex = headerOffset + startIndex; // field int fieldOffset = 0; foreach (int headerValue in this.HeaderList) { this.FieldOffsets.Add(fieldOffset); if (headerValue == (int)HeaderValue.INTEGER) { fieldOffset += 4; } else if (headerValue == (int)HeaderValue.FloatingPoint) { fieldOffset += 8; } else if (headerValue == (int)HeaderValue.NULL) { fieldOffset += 0; } else if ((headerValue - (int)HeaderValue.TEXT) % 2 == 0) { int stringLength = (headerValue - (int)HeaderValue.TEXT) / 2; fieldOffset += stringLength; } else { throw new Exception($"Header value {headerValue} does not exists"); } } int fieldLength = fieldOffset; this.FieldData = data.Skip(fieldStartIndex).Take(fieldLength).ToArray(); }