|
| 1 | +/******************************************************************************* |
| 2 | + * ___ _ ____ ____ |
| 3 | + * / _ \ _ _ ___ ___| |_| _ \| __ ) |
| 4 | + * | | | | | | |/ _ \/ __| __| | | | _ \ |
| 5 | + * | |_| | |_| | __/\__ \ |_| |_| | |_) | |
| 6 | + * \__\_\\__,_|\___||___/\__|____/|____/ |
| 7 | + * |
| 8 | + * Copyright (c) 2014-2019 Appsicle |
| 9 | + * Copyright (c) 2019-2024 QuestDB |
| 10 | + * |
| 11 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 12 | + * you may not use this file except in compliance with the License. |
| 13 | + * You may obtain a copy of the License at |
| 14 | + * |
| 15 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 16 | + * |
| 17 | + * Unless required by applicable law or agreed to in writing, software |
| 18 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 19 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 20 | + * See the License for the specific language governing permissions and |
| 21 | + * limitations under the License. |
| 22 | + * |
| 23 | + ******************************************************************************/ |
| 24 | + |
| 25 | +using System.Buffers.Binary; |
| 26 | +using System.Runtime.InteropServices; |
| 27 | +using QuestDB.Enums; |
| 28 | +using QuestDB.Utils; |
| 29 | + |
| 30 | +namespace QuestDB.Buffers; |
| 31 | + |
| 32 | +/// <summary /> |
| 33 | +public class BufferV3 : BufferV2 |
| 34 | +{ |
| 35 | + /// <summary /> |
| 36 | + public BufferV3(int bufferSize, int maxNameLen, int maxBufSize) : base(bufferSize, maxNameLen, maxBufSize) |
| 37 | + { |
| 38 | + } |
| 39 | + |
| 40 | + // Sign mask for the flags field. A value of zero in this bit indicates a |
| 41 | + // positive Decimal value, and a value of one in this bit indicates a |
| 42 | + // negative Decimal value. |
| 43 | + private const int SignMask = unchecked((int)0x80000000); |
| 44 | + |
| 45 | + // Scale mask for the flags field. This byte in the flags field contains |
| 46 | + // the power of 10 to divide the Decimal value by. The scale byte must |
| 47 | + // contain a value between 0 and 28 inclusive. |
| 48 | + private const int ScaleMask = 0x00FF0000; |
| 49 | + |
| 50 | + // Number of bits scale is shifted by. |
| 51 | + private const int ScaleShift = 16; |
| 52 | + |
| 53 | + /// <inheritdoc /> |
| 54 | + public override IBuffer Column(ReadOnlySpan<char> name, decimal? value) |
| 55 | + { |
| 56 | + // # Binary Format |
| 57 | + // 1. Binary format marker: `'='` (0x3D) |
| 58 | + // 2. Type identifier: BinaryFormatType.DECIMAL byte |
| 59 | + // 3. Scale: 1 byte (0-76 inclusive) - number of decimal places |
| 60 | + // 4. Length: 1 byte - number of bytes in the unscaled value |
| 61 | + // 5. Unscaled value: variable-length byte array in two's complement format, big-endian |
| 62 | + SetTableIfAppropriate(); |
| 63 | + Column(name) |
| 64 | + .PutAscii(Constants.BINARY_FORMAT_FLAG) |
| 65 | + .Put((byte)BinaryFormatType.DECIMAL); |
| 66 | + if (value is null) |
| 67 | + { |
| 68 | + Put((byte)0); // Scale |
| 69 | + Put((byte)0); // Length |
| 70 | + return this; |
| 71 | + } |
| 72 | + |
| 73 | + Span<int> parts = stackalloc int[4]; |
| 74 | + decimal.GetBits(value.Value, parts); |
| 75 | + |
| 76 | + int flags = parts[3]; |
| 77 | + byte scale = (byte)((flags & ScaleMask) >> ScaleShift); |
| 78 | + |
| 79 | + // 3. Scale |
| 80 | + Put(scale); |
| 81 | + |
| 82 | + int low = parts[0]; |
| 83 | + int mid = parts[1]; |
| 84 | + int high = parts[2]; |
| 85 | + bool bitSign = false; |
| 86 | + bool negative = (flags & SignMask) != 0; |
| 87 | + |
| 88 | + if (negative) |
| 89 | + { |
| 90 | + // QuestDB expects negative mantissas in two's complement. |
| 91 | + low = ~low + 1; |
| 92 | + int c = low == 0 ? 1 : 0; |
| 93 | + mid = ~mid + c; |
| 94 | + c = mid == 0 && c == 1 ? 1 : 0; |
| 95 | + high = ~high + c; |
| 96 | + // We may overflow, we need an extra byte to convey the sign. |
| 97 | + bitSign = high == 0 && c == 1; |
| 98 | + } |
| 99 | + else if ((high & 0x80000000) != 0) |
| 100 | + { |
| 101 | + // If the highest bit is set, we need an extra byte of 0 to convey the sign. |
| 102 | + bitSign = true; |
| 103 | + } |
| 104 | + |
| 105 | + var size = bitSign ? 13 : 12; |
| 106 | + |
| 107 | + // 4. Length |
| 108 | + Put((byte)size); |
| 109 | + |
| 110 | + // 5. Unscaled value |
| 111 | + EnsureCapacity(size); |
| 112 | + var span = Chunk.AsSpan(Position, size); |
| 113 | + var offset = 0; |
| 114 | + if (bitSign) |
| 115 | + { |
| 116 | + span[offset++] = (byte)(negative ? 255 : 0); |
| 117 | + } |
| 118 | + BinaryPrimitives.WriteInt32BigEndian(span.Slice(offset, 4), high); |
| 119 | + offset += 4; |
| 120 | + BinaryPrimitives.WriteInt32BigEndian(span.Slice(offset, 4), mid); |
| 121 | + offset += 4; |
| 122 | + BinaryPrimitives.WriteInt32BigEndian(span.Slice(offset, 4), low); |
| 123 | + Advance(size); |
| 124 | + |
| 125 | + return this; |
| 126 | + } |
| 127 | +} |
0 commit comments