|
| 1 | +/* |
| 2 | + * Copyright 2021 DataCanvas |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.dingodb.sdk.common.serial.schema; |
| 18 | + |
| 19 | +import io.dingodb.sdk.common.serial.Buf; |
| 20 | + |
| 21 | +public class BitSchema implements DingoSchema<Long> { |
| 22 | + |
| 23 | + private int index; |
| 24 | + private boolean isKey; |
| 25 | + private boolean allowNull = true; |
| 26 | + |
| 27 | + public BitSchema() { |
| 28 | + } |
| 29 | + |
| 30 | + public BitSchema(int index) { |
| 31 | + this.index = index; |
| 32 | + } |
| 33 | + |
| 34 | + @Override |
| 35 | + public Type getType() { |
| 36 | + return Type.BIT; |
| 37 | + } |
| 38 | + |
| 39 | + @Override |
| 40 | + public void setIndex(int index) { |
| 41 | + this.index = index; |
| 42 | + } |
| 43 | + |
| 44 | + @Override |
| 45 | + public int getIndex() { |
| 46 | + return index; |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public void setIsKey(boolean isKey) { |
| 51 | + this.isKey = isKey; |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public boolean isKey() { |
| 56 | + return isKey; |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public int getLength() { |
| 61 | + if (allowNull) { |
| 62 | + return getWithNullTagLength(); |
| 63 | + } |
| 64 | + return getDataLength(); |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public int getValueLengthV2() { |
| 69 | + return getDataLength(); |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public int getWithNullTagLength() { |
| 74 | + return 9; |
| 75 | + } |
| 76 | + |
| 77 | + private int getDataLength() { |
| 78 | + return 8; |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public void setAllowNull(boolean allowNull) { |
| 83 | + this.allowNull = allowNull; |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public boolean isAllowNull() { |
| 88 | + return allowNull; |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public void encodeKey(Buf buf, Long data) { |
| 93 | + if (allowNull) { |
| 94 | + buf.ensureRemainder(getWithNullTagLength()); |
| 95 | + if (data == null) { |
| 96 | + buf.write(NULL); |
| 97 | + internalEncodeNull(buf); |
| 98 | + } else { |
| 99 | + buf.write(NOTNULL); |
| 100 | + internalEncodeKey(buf, data); |
| 101 | + } |
| 102 | + } else { |
| 103 | + buf.ensureRemainder(getDataLength()); |
| 104 | + internalEncodeKey(buf, data); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + public void encodeKeyV2(Buf buf, Long data) { |
| 109 | + encodeKey(buf, data); |
| 110 | + } |
| 111 | + |
| 112 | + @Override |
| 113 | + public void encodeKeyForUpdate(Buf buf, Long data) { |
| 114 | + if (allowNull) { |
| 115 | + if (data == null) { |
| 116 | + buf.write(NULL); |
| 117 | + internalEncodeNull(buf); |
| 118 | + } else { |
| 119 | + buf.write(NOTNULL); |
| 120 | + internalEncodeKey(buf, data); |
| 121 | + } |
| 122 | + } else { |
| 123 | + internalEncodeKey(buf, data); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + public void encodeKeyForUpdateV2(Buf buf, Long data) { |
| 128 | + encodeKeyForUpdate(buf, data); |
| 129 | + } |
| 130 | + |
| 131 | + private void internalEncodeNull(Buf buf) { |
| 132 | + buf.write((byte) 0); |
| 133 | + buf.write((byte) 0); |
| 134 | + buf.write((byte) 0); |
| 135 | + buf.write((byte) 0); |
| 136 | + buf.write((byte) 0); |
| 137 | + buf.write((byte) 0); |
| 138 | + buf.write((byte) 0); |
| 139 | + buf.write((byte) 0); |
| 140 | + } |
| 141 | + |
| 142 | + private void internalEncodeKey(Buf buf, Long data) { |
| 143 | + buf.write((byte) (data >>> 56 ^ 0x80)); |
| 144 | + buf.write((byte) (data >>> 48)); |
| 145 | + buf.write((byte) (data >>> 40)); |
| 146 | + buf.write((byte) (data >>> 32)); |
| 147 | + buf.write((byte) (data >>> 24)); |
| 148 | + buf.write((byte) (data >>> 16)); |
| 149 | + buf.write((byte) (data >>> 8)); |
| 150 | + buf.write((byte) data.longValue()); |
| 151 | + } |
| 152 | + |
| 153 | + @Override |
| 154 | + public Long decodeKey(Buf buf) { |
| 155 | + if (allowNull) { |
| 156 | + if (buf.read() == NULL) { |
| 157 | + buf.skip(getDataLength()); |
| 158 | + return null; |
| 159 | + } |
| 160 | + } |
| 161 | + long l = 0; |
| 162 | + l |= buf.read() & 0xFF ^ 0x80; |
| 163 | + for (int i = 0; i < 7; i++) { |
| 164 | + l <<= 8; |
| 165 | + l |= buf.read() & 0xFF; |
| 166 | + } |
| 167 | + return l; |
| 168 | + } |
| 169 | + |
| 170 | + public Long decodeKeyV2(Buf buf) { |
| 171 | + return decodeKey(buf); |
| 172 | + } |
| 173 | + |
| 174 | + @Override |
| 175 | + public Long decodeKeyPrefix(Buf buf) { |
| 176 | + return decodeKey(buf); |
| 177 | + } |
| 178 | + |
| 179 | + @Override |
| 180 | + public void skipKey(Buf buf) { |
| 181 | + buf.skip(getLength()); |
| 182 | + } |
| 183 | + |
| 184 | + public void skipKeyV2(Buf buf) { |
| 185 | + skipKey(buf); |
| 186 | + } |
| 187 | + |
| 188 | + @Override |
| 189 | + public void encodeKeyPrefix(Buf buf, Long data) { |
| 190 | + encodeKey(buf, data); |
| 191 | + } |
| 192 | + |
| 193 | + @Override |
| 194 | + public void encodeValue(Buf buf, Long data) { |
| 195 | + if (allowNull) { |
| 196 | + buf.ensureRemainder(getWithNullTagLength()); |
| 197 | + if (data == null) { |
| 198 | + buf.write(NULL); |
| 199 | + internalEncodeNull(buf); |
| 200 | + } else { |
| 201 | + buf.write(NOTNULL); |
| 202 | + internalEncodeValue(buf, data); |
| 203 | + } |
| 204 | + } else { |
| 205 | + buf.ensureRemainder(getDataLength()); |
| 206 | + internalEncodeValue(buf, data); |
| 207 | + } |
| 208 | + } |
| 209 | + |
| 210 | + @Override |
| 211 | + public int encodeValueV2(Buf buf, Long data) { |
| 212 | + int len = getValueLengthV2(); |
| 213 | + buf.ensureRemainder(len); |
| 214 | + |
| 215 | + if (allowNull) { |
| 216 | + if (data == null) { |
| 217 | + return 0; |
| 218 | + } else { |
| 219 | + internalEncodeValue(buf, data); |
| 220 | + } |
| 221 | + } else { |
| 222 | + internalEncodeValue(buf, data); |
| 223 | + } |
| 224 | + |
| 225 | + return len; |
| 226 | + } |
| 227 | + |
| 228 | + private void internalEncodeValue(Buf buf, Long data) { |
| 229 | + buf.write((byte) (data >>> 56)); |
| 230 | + buf.write((byte) (data >>> 48)); |
| 231 | + buf.write((byte) (data >>> 40)); |
| 232 | + buf.write((byte) (data >>> 32)); |
| 233 | + buf.write((byte) (data >>> 24)); |
| 234 | + buf.write((byte) (data >>> 16)); |
| 235 | + buf.write((byte) (data >>> 8)); |
| 236 | + buf.write((byte) data.longValue()); |
| 237 | + } |
| 238 | + |
| 239 | + @Override |
| 240 | + public Long decodeValue(Buf buf) { |
| 241 | + if (allowNull) { |
| 242 | + if (buf.read() == NULL) { |
| 243 | + buf.skip(getDataLength()); |
| 244 | + return null; |
| 245 | + } |
| 246 | + } |
| 247 | + long l = buf.read() & 0xFF; |
| 248 | + for (int i = 0; i < 7; i++) { |
| 249 | + l <<= 8; |
| 250 | + l |= buf.read() & 0xFF; |
| 251 | + } |
| 252 | + return l; |
| 253 | + } |
| 254 | + |
| 255 | + @Override |
| 256 | + public Long decodeValueV2(Buf buf) { |
| 257 | + long l = buf.read() & 0xFF; |
| 258 | + for (int i = 0; i < 7; i++) { |
| 259 | + l <<= 8; |
| 260 | + l |= buf.read() & 0xFF; |
| 261 | + } |
| 262 | + return l; |
| 263 | + } |
| 264 | + |
| 265 | + @Override |
| 266 | + public void skipValue(Buf buf) { |
| 267 | + buf.skip(getLength()); |
| 268 | + } |
| 269 | + |
| 270 | + @Override |
| 271 | + public void skipValueV2(Buf buf) { |
| 272 | + buf.skip(getValueLengthV2()); |
| 273 | + } |
| 274 | +} |
0 commit comments