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