github.com/matrixorigin/matrixone@v1.2.0/pkg/container/types/encoding.go (about) 1 // Copyright 2021 Matrix Origin 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 //go:build !debug 16 // +build !debug 17 18 package types 19 20 import ( 21 "bytes" 22 "encoding" 23 "fmt" 24 "io" 25 "unsafe" 26 27 "github.com/matrixorigin/matrixone/pkg/common/moerr" 28 "github.com/matrixorigin/matrixone/pkg/common/util" 29 "github.com/matrixorigin/matrixone/pkg/container/bytejson" 30 ) 31 32 const ( 33 TSize int = int(unsafe.Sizeof(Type{})) 34 DateSize int = 4 35 TimeSize int = 8 36 DatetimeSize int = 8 37 TimestampSize int = 8 38 Decimal64Size int = 8 39 Decimal128Size int = 16 40 UuidSize int = 16 41 ) 42 43 func EncodeSliceWithCap[T any](v []T) []byte { 44 var t T 45 sz := int(unsafe.Sizeof(t)) 46 if cap(v) > 0 { 47 return unsafe.Slice((*byte)(unsafe.Pointer(&v[0])), cap(v)*sz)[:len(v)*sz] 48 } 49 return nil 50 } 51 52 func EncodeSlice[T any](v []T) []byte { 53 var t T 54 sz := int(unsafe.Sizeof(t)) 55 if len(v) > 0 { 56 return unsafe.Slice((*byte)(unsafe.Pointer(&v[0])), len(v)*sz)[:len(v)*sz] 57 } 58 return nil 59 } 60 61 func DecodeSlice[T any](v []byte) []T { 62 var t T 63 sz := int(unsafe.Sizeof(t)) 64 65 if len(v)%sz != 0 { 66 panic(moerr.NewInternalErrorNoCtx("decode slice that is not a multiple of element size")) 67 } 68 69 if len(v) > 0 { 70 return unsafe.Slice((*T)(unsafe.Pointer(&v[0])), len(v)/sz)[:len(v)/sz] 71 } 72 return nil 73 } 74 75 func Encode(v encoding.BinaryMarshaler) ([]byte, error) { 76 return v.MarshalBinary() 77 } 78 79 func Decode(data []byte, v encoding.BinaryUnmarshaler) error { 80 return v.UnmarshalBinary(data) 81 } 82 83 func EncodeJson(v bytejson.ByteJson) ([]byte, error) { 84 //TODO handle error 85 buf, _ := v.Marshal() 86 return buf, nil 87 } 88 89 func DecodeJson(buf []byte) bytejson.ByteJson { 90 bj := bytejson.ByteJson{} 91 //TODO handle error 92 _ = bj.Unmarshal(buf) 93 return bj 94 } 95 96 func EncodeType(v *Type) []byte { 97 return unsafe.Slice((*byte)(unsafe.Pointer(v)), TSize) 98 } 99 100 func DecodeType(v []byte) Type { 101 return *(*Type)(unsafe.Pointer(&v[0])) 102 } 103 104 func EncodeFixed[T FixedSizeT](v T) []byte { 105 sz := unsafe.Sizeof(v) 106 return unsafe.Slice((*byte)(unsafe.Pointer(&v)), sz) 107 } 108 func DecodeFixed[T FixedSizeT](v []byte) T { 109 return *(*T)(unsafe.Pointer(&v[0])) 110 } 111 112 func DecodeBool(v []byte) bool { 113 return *(*bool)(unsafe.Pointer(&v[0])) 114 } 115 116 func EncodeBool(v *bool) []byte { 117 return unsafe.Slice((*byte)(unsafe.Pointer(v)), 1) 118 } 119 120 func EncodeInt8(v *int8) []byte { 121 return unsafe.Slice((*byte)(unsafe.Pointer(v)), 1) 122 } 123 124 func DecodeInt8(v []byte) int8 { 125 return *(*int8)(unsafe.Pointer(&v[0])) 126 } 127 128 func EncodeUint8(v *uint8) []byte { 129 return unsafe.Slice((*byte)(unsafe.Pointer(v)), 1) 130 } 131 132 func DecodeUint8(v []byte) uint8 { 133 return v[0] 134 } 135 136 func EncodeInt16(v *int16) []byte { 137 return unsafe.Slice((*byte)(unsafe.Pointer(v)), 2) 138 } 139 140 func DecodeInt16(v []byte) int16 { 141 return *(*int16)(unsafe.Pointer(&v[0])) 142 } 143 144 func EncodeUint16(v *uint16) []byte { 145 return unsafe.Slice((*byte)(unsafe.Pointer(v)), 2) 146 } 147 148 func DecodeUint16(v []byte) uint16 { 149 return *(*uint16)(unsafe.Pointer(&v[0])) 150 } 151 152 func EncodeInt32(v *int32) []byte { 153 return unsafe.Slice((*byte)(unsafe.Pointer(v)), 4) 154 } 155 156 func DecodeInt32(v []byte) int32 { 157 return *(*int32)(unsafe.Pointer(&v[0])) 158 } 159 160 func EncodeUint32(v *uint32) []byte { 161 return unsafe.Slice((*byte)(unsafe.Pointer(v)), 4) 162 } 163 164 func DecodeUint32(v []byte) uint32 { 165 return *(*uint32)(unsafe.Pointer(&v[0])) 166 } 167 168 func EncodeInt64(v *int64) []byte { 169 return unsafe.Slice((*byte)(unsafe.Pointer(v)), 8) 170 } 171 172 func DecodeInt64(v []byte) int64 { 173 return *(*int64)(unsafe.Pointer(&v[0])) 174 } 175 176 func EncodeUint64(v *uint64) []byte { 177 return unsafe.Slice((*byte)(unsafe.Pointer(v)), 8) 178 } 179 180 func DecodeUint64(v []byte) uint64 { 181 return *(*uint64)(unsafe.Pointer(&v[0])) 182 } 183 184 func EncodeFloat32(v *float32) []byte { 185 return unsafe.Slice((*byte)(unsafe.Pointer(v)), 4) 186 } 187 188 func DecodeFloat32(v []byte) float32 { 189 return *(*float32)(unsafe.Pointer(&v[0])) 190 } 191 192 func EncodeFloat64(v *float64) []byte { 193 return unsafe.Slice((*byte)(unsafe.Pointer(v)), 8) 194 } 195 196 func DecodeFloat64(v []byte) float64 { 197 return *(*float64)(unsafe.Pointer(&v[0])) 198 } 199 200 func EncodeDate(v *Date) []byte { 201 return unsafe.Slice((*byte)(unsafe.Pointer(v)), 4) 202 } 203 204 func DecodeDate(v []byte) Date { 205 return *(*Date)(unsafe.Pointer(&v[0])) 206 } 207 208 func EncodeTime(v *Time) []byte { 209 return unsafe.Slice((*byte)(unsafe.Pointer(v)), 8) 210 } 211 212 func DecodeTime(v []byte) Time { 213 return *(*Time)(unsafe.Pointer(&v[0])) 214 } 215 216 func EncodeDatetime(v *Datetime) []byte { 217 return unsafe.Slice((*byte)(unsafe.Pointer(v)), 8) 218 } 219 220 func DecodeDatetime(v []byte) Datetime { 221 return *(*Datetime)(unsafe.Pointer(&v[0])) 222 } 223 224 func EncodeTimestamp(v *Timestamp) []byte { 225 return unsafe.Slice((*byte)(unsafe.Pointer(v)), 8) 226 } 227 228 func DecodeTimestamp(v []byte) Timestamp { 229 return *(*Timestamp)(unsafe.Pointer(&v[0])) 230 } 231 232 func EncodeEnum(v *Enum) []byte { 233 return unsafe.Slice((*byte)(unsafe.Pointer(v)), 2) 234 } 235 236 func DecodeEnum(v []byte) Enum { 237 return *(*Enum)(unsafe.Pointer(&v[0])) 238 } 239 240 func EncodeDecimal64(v *Decimal64) []byte { 241 return unsafe.Slice((*byte)(unsafe.Pointer(v)), Decimal64Size) 242 } 243 244 func DecodeDecimal64(v []byte) Decimal64 { 245 return *(*Decimal64)(unsafe.Pointer(&v[0])) 246 } 247 248 func EncodeDecimal128(v *Decimal128) []byte { 249 return unsafe.Slice((*byte)(unsafe.Pointer(v)), Decimal128Size) 250 } 251 252 func DecodeDecimal128(v []byte) Decimal128 { 253 return *(*Decimal128)(unsafe.Pointer(&v[0])) 254 } 255 256 func EncodeUuid(v *Uuid) []byte { 257 return unsafe.Slice((*byte)(unsafe.Pointer(v)), UuidSize) 258 } 259 260 func DecodeUuid(v []byte) Uuid { 261 return *(*Uuid)(unsafe.Pointer(&v[0])) 262 } 263 264 func EncodeStringSlice(vs []string) []byte { 265 var o int32 266 var buf bytes.Buffer 267 268 cnt := int32(len(vs)) 269 buf.Write(EncodeInt32(&cnt)) 270 if cnt == 0 { 271 return buf.Bytes() 272 } 273 os := make([]int32, cnt) 274 for i, v := range vs { 275 os[i] = o 276 o += int32(len(v)) 277 } 278 buf.Write(EncodeSlice(os)) 279 for _, v := range vs { 280 buf.WriteString(v) 281 } 282 return buf.Bytes() 283 } 284 285 func DecodeStringSlice(data []byte) []string { 286 var tm []byte 287 288 cnt := DecodeInt32(data) 289 if cnt == 0 { 290 return nil 291 } 292 data = data[4:] 293 vs := make([]string, cnt) 294 os := DecodeSlice[int32](data[:4*cnt]) 295 data = data[4*cnt:] 296 for i := int32(0); i < cnt; i++ { 297 if i == cnt-1 { 298 tm = data[os[i]:] 299 vs[i] = util.UnsafeBytesToString(tm) 300 } else { 301 tm = data[os[i]:os[i+1]] 302 vs[i] = util.UnsafeBytesToString(tm) 303 } 304 } 305 return vs 306 } 307 308 func DecodeValue(val []byte, t T) any { 309 switch t { 310 case T_bool: 311 return DecodeFixed[bool](val) 312 case T_bit: 313 return DecodeFixed[uint64](val) 314 case T_int8: 315 return DecodeFixed[int8](val) 316 case T_int16: 317 return DecodeFixed[int16](val) 318 case T_int32: 319 return DecodeFixed[int32](val) 320 case T_int64: 321 return DecodeFixed[int64](val) 322 case T_uint8: 323 return DecodeFixed[uint8](val) 324 case T_uint16: 325 return DecodeFixed[uint16](val) 326 case T_uint32: 327 return DecodeFixed[uint32](val) 328 case T_uint64: 329 return DecodeFixed[uint64](val) 330 case T_float32: 331 return DecodeFixed[float32](val) 332 case T_float64: 333 return DecodeFixed[float64](val) 334 case T_date: 335 return DecodeFixed[Date](val) 336 case T_time: 337 return DecodeFixed[Time](val) 338 case T_datetime: 339 return DecodeFixed[Datetime](val) 340 case T_timestamp: 341 return DecodeFixed[Timestamp](val) 342 case T_decimal64: 343 return DecodeFixed[Decimal64](val) 344 case T_decimal128: 345 return DecodeFixed[Decimal128](val) 346 case T_uuid: 347 return DecodeFixed[Uuid](val) 348 case T_TS: 349 return DecodeFixed[TS](val) 350 case T_Rowid: 351 return DecodeFixed[Rowid](val) 352 case T_char, T_varchar, T_blob, T_json, T_text, T_binary, T_varbinary, T_array_float32, T_array_float64: 353 return val 354 case T_enum: 355 return DecodeFixed[Enum](val) 356 default: 357 panic(fmt.Sprintf("unsupported type %v", t)) 358 } 359 } 360 361 func EncodeValue(val any, t T) []byte { 362 switch t { 363 case T_bool: 364 return EncodeFixed(val.(bool)) 365 case T_bit: 366 return EncodeFixed(val.(uint64)) 367 case T_int8: 368 return EncodeFixed(val.(int8)) 369 case T_int16: 370 return EncodeFixed(val.(int16)) 371 case T_int32: 372 return EncodeFixed(val.(int32)) 373 case T_int64: 374 return EncodeFixed(val.(int64)) 375 case T_uint8: 376 return EncodeFixed(val.(uint8)) 377 case T_uint16: 378 return EncodeFixed(val.(uint16)) 379 case T_uint32: 380 return EncodeFixed(val.(uint32)) 381 case T_uint64: 382 return EncodeFixed(val.(uint64)) 383 case T_float32: 384 return EncodeFixed(val.(float32)) 385 case T_float64: 386 return EncodeFixed(val.(float64)) 387 case T_decimal64: 388 return EncodeFixed(val.(Decimal64)) 389 case T_decimal128: 390 return EncodeFixed(val.(Decimal128)) 391 case T_date: 392 return EncodeFixed(val.(Date)) 393 case T_time: 394 return EncodeFixed(val.(Time)) 395 case T_timestamp: 396 return EncodeFixed(val.(Timestamp)) 397 case T_datetime: 398 return EncodeFixed(val.(Datetime)) 399 case T_uuid: 400 return EncodeFixed(val.(Uuid)) 401 case T_TS: 402 return EncodeFixed(val.(TS)) 403 case T_Rowid: 404 return EncodeFixed(val.(Rowid)) 405 case T_char, T_varchar, T_blob, T_json, T_text, T_binary, T_varbinary, 406 T_array_float32, T_array_float64: 407 // Mainly used by Zonemap, which receives val input from DN batch/vector. 408 // This val is mostly []bytes and not []float32 or []float64 409 return val.([]byte) 410 case T_enum: 411 return EncodeFixed(val.(Enum)) 412 default: 413 panic(fmt.Sprintf("unsupported type %v", t)) 414 } 415 } 416 417 func WriteValues(w io.Writer, vals ...any) (n int64, err error) { 418 var nr int 419 for _, val := range vals { 420 switch v := val.(type) { 421 case []byte: 422 if nr, err = w.Write(v); err != nil { 423 return 424 } 425 n += int64(nr) 426 case bool: 427 if nr, err = w.Write(EncodeFixed(v)); err != nil { 428 return 429 } 430 n += int64(nr) 431 case int8: 432 if nr, err = w.Write(EncodeFixed(v)); err != nil { 433 return 434 } 435 n += int64(nr) 436 case int16: 437 if nr, err = w.Write(EncodeFixed(v)); err != nil { 438 return 439 } 440 n += int64(nr) 441 case int32: 442 if nr, err = w.Write(EncodeFixed(v)); err != nil { 443 return 444 } 445 n += int64(nr) 446 case int64: 447 if nr, err = w.Write(EncodeFixed(v)); err != nil { 448 return 449 } 450 n += int64(nr) 451 case uint8: 452 if nr, err = w.Write(EncodeFixed(v)); err != nil { 453 return 454 } 455 n += int64(nr) 456 case uint16: 457 if nr, err = w.Write(EncodeFixed(v)); err != nil { 458 return 459 } 460 n += int64(nr) 461 case uint32: 462 if nr, err = w.Write(EncodeFixed(v)); err != nil { 463 return 464 } 465 n += int64(nr) 466 case uint64: 467 if nr, err = w.Write(EncodeFixed(v)); err != nil { 468 return 469 } 470 n += int64(nr) 471 case float32: 472 if nr, err = w.Write(EncodeFixed(v)); err != nil { 473 return 474 } 475 n += int64(nr) 476 case float64: 477 if nr, err = w.Write(EncodeFixed(v)); err != nil { 478 return 479 } 480 n += int64(nr) 481 case Date: 482 if nr, err = w.Write(EncodeFixed(v)); err != nil { 483 return 484 } 485 n += int64(nr) 486 case Time: 487 if nr, err = w.Write(EncodeFixed(v)); err != nil { 488 return 489 } 490 n += int64(nr) 491 case Datetime: 492 if nr, err = w.Write(EncodeFixed(v)); err != nil { 493 return 494 } 495 n += int64(nr) 496 case Timestamp: 497 if nr, err = w.Write(EncodeFixed(v)); err != nil { 498 return 499 } 500 n += int64(nr) 501 case Decimal64: 502 if nr, err = w.Write(EncodeFixed(v)); err != nil { 503 return 504 } 505 n += int64(nr) 506 case Decimal128: 507 if nr, err = w.Write(EncodeFixed(v)); err != nil { 508 return 509 } 510 n += int64(nr) 511 case Uuid: 512 if nr, err = w.Write(EncodeFixed(v)); err != nil { 513 return 514 } 515 n += int64(nr) 516 case TS: 517 if nr, err = w.Write(EncodeFixed(v)); err != nil { 518 return 519 } 520 n += int64(nr) 521 case Rowid: 522 if nr, err = w.Write(EncodeFixed(v)); err != nil { 523 return 524 } 525 n += int64(nr) 526 default: 527 panic(moerr.NewInternalErrorNoCtx("%T:%v not supported", v, v)) 528 } 529 } 530 return 531 } 532 533 func Int32ToUint32(x int32) uint32 { 534 ux := uint32(x) << 1 535 if x < 0 { 536 ux = ^ux 537 } 538 return ux 539 } 540 541 func Uint32ToInt32(ux uint32) int32 { 542 x := int32(ux >> 1) 543 if ux&1 != 0 { 544 x = ^x 545 } 546 return x 547 }