public SequenceSize ReadNext(ReadOnlySequence <byte> sequence) { int bytesCount = 0; if (_baseColumnReader == null) { foreach (var mem in sequence) { if (_nullFlagPosition == _rowCount) { break; } foreach (var byteVal in mem.Span) { if (byteVal != 0) { if (_nullFlags == null) { _nullFlags = new BitArray(_rowCount, false); } _nullFlags[_nullFlagPosition] = true; } bytesCount++; if (_rowCount == ++_nullFlagPosition) { break; } } } if (_nullFlagPosition < _rowCount) { return(new SequenceSize(bytesCount, 0)); } _baseColumnReader = _underlyingType.CreateColumnReader(_rowCount); } var baseResult = _baseColumnReader.ReadNext(sequence.Slice(bytesCount)); return(new SequenceSize(bytesCount + baseResult.Bytes, baseResult.Elements)); }
public SequenceSize ReadNext(ReadOnlySequence <byte> sequence) { if (_position >= _rowCount) { throw new ClickHouseException(ClickHouseErrorCodes.DataReaderError, "Internal error. Attempt to read after the end of the column."); } var result = new SequenceSize(0, 0); var slice = sequence; if (_baseColumnReader == null) { var header = TryReadHeader(slice); if (header == null) { return(result); } _baseRowCount = header.Value.keyCount; _keySize = header.Value.keySize; _baseColumnReader = _baseType.CreateColumnReader(_baseRowCount); slice = slice.Slice(header.Value.bytesRead); result = result.AddBytes(header.Value.bytesRead); } if (_baseRowCount > 0) { var baseResult = _baseColumnReader.ReadNext(slice); _baseRowCount -= baseResult.Elements; slice = slice.Slice(baseResult.Bytes); result = result.AddBytes(baseResult.Bytes); } if (_baseRowCount > 0) { return(result); } if (_buffer == null) { if (slice.Length < sizeof(ulong)) { return(result); } ulong length = 0; slice.Slice(0, sizeof(ulong)).CopyTo(MemoryMarshal.AsBytes(MemoryMarshal.CreateSpan(ref length, 1))); if ((int)length != _rowCount) { throw new ClickHouseException(ClickHouseErrorCodes.DataReaderError, $"Internal error. Row count check failed: {_rowCount} rows expected, {length} rows detected."); } _buffer = new byte[_rowCount * _keySize]; slice = slice.Slice(sizeof(ulong)); result = result.AddBytes(sizeof(ulong)); } var elementCount = Math.Min(_rowCount - _position, (int)(slice.Length / _keySize)); var byteCount = elementCount * _keySize; slice.Slice(0, byteCount).CopyTo(new Span <byte>(_buffer, _position * _keySize, byteCount)); _position += elementCount; result += new SequenceSize(byteCount, elementCount); return(result); }