github.com/matrixorigin/matrixone@v1.2.0/pkg/vm/engine/disttae/logtailreplay/encode.go (about)

     1  // Copyright 2023 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  package logtailreplay
    16  
    17  import (
    18  	"fmt"
    19  
    20  	"github.com/matrixorigin/matrixone/pkg/container/types"
    21  	"github.com/matrixorigin/matrixone/pkg/container/vector"
    22  )
    23  
    24  func EncodePrimaryKeyVector(vec *vector.Vector, packer *types.Packer) (ret [][]byte) {
    25  	packer.Reset()
    26  
    27  	if vec.IsConstNull() {
    28  		return make([][]byte, vec.Length())
    29  	}
    30  
    31  	switch vec.GetType().Oid {
    32  
    33  	case types.T_bool:
    34  		s := vector.MustFixedCol[bool](vec)
    35  		for _, v := range s {
    36  			packer.EncodeBool(v)
    37  			ret = append(ret, packer.Bytes())
    38  			packer.Reset()
    39  		}
    40  
    41  	case types.T_bit:
    42  		s := vector.MustFixedCol[uint64](vec)
    43  		for _, v := range s {
    44  			packer.EncodeUint64(v)
    45  			ret = append(ret, packer.Bytes())
    46  			packer.Reset()
    47  		}
    48  
    49  	case types.T_int8:
    50  		s := vector.MustFixedCol[int8](vec)
    51  		for _, v := range s {
    52  			packer.EncodeInt8(v)
    53  			ret = append(ret, packer.Bytes())
    54  			packer.Reset()
    55  		}
    56  
    57  	case types.T_int16:
    58  		s := vector.MustFixedCol[int16](vec)
    59  		for _, v := range s {
    60  			packer.EncodeInt16(v)
    61  			ret = append(ret, packer.Bytes())
    62  			packer.Reset()
    63  		}
    64  
    65  	case types.T_int32:
    66  		s := vector.MustFixedCol[int32](vec)
    67  		for _, v := range s {
    68  			packer.EncodeInt32(v)
    69  			ret = append(ret, packer.Bytes())
    70  			packer.Reset()
    71  		}
    72  
    73  	case types.T_int64:
    74  		s := vector.MustFixedCol[int64](vec)
    75  		for _, v := range s {
    76  			packer.EncodeInt64(v)
    77  			ret = append(ret, packer.Bytes())
    78  			packer.Reset()
    79  		}
    80  
    81  	case types.T_uint8:
    82  		s := vector.MustFixedCol[uint8](vec)
    83  		for _, v := range s {
    84  			packer.EncodeUint8(v)
    85  			ret = append(ret, packer.Bytes())
    86  			packer.Reset()
    87  		}
    88  
    89  	case types.T_uint16:
    90  		s := vector.MustFixedCol[uint16](vec)
    91  		for _, v := range s {
    92  			packer.EncodeUint16(v)
    93  			ret = append(ret, packer.Bytes())
    94  			packer.Reset()
    95  		}
    96  
    97  	case types.T_uint32:
    98  		s := vector.MustFixedCol[uint32](vec)
    99  		for _, v := range s {
   100  			packer.EncodeUint32(v)
   101  			ret = append(ret, packer.Bytes())
   102  			packer.Reset()
   103  		}
   104  
   105  	case types.T_uint64:
   106  		s := vector.MustFixedCol[uint64](vec)
   107  		for _, v := range s {
   108  			packer.EncodeUint64(v)
   109  			ret = append(ret, packer.Bytes())
   110  			packer.Reset()
   111  		}
   112  
   113  	case types.T_float32:
   114  		s := vector.MustFixedCol[float32](vec)
   115  		for _, v := range s {
   116  			packer.EncodeFloat32(v)
   117  			ret = append(ret, packer.Bytes())
   118  			packer.Reset()
   119  		}
   120  
   121  	case types.T_float64:
   122  		s := vector.MustFixedCol[float64](vec)
   123  		for _, v := range s {
   124  			packer.EncodeFloat64(v)
   125  			ret = append(ret, packer.Bytes())
   126  			packer.Reset()
   127  		}
   128  
   129  	case types.T_date:
   130  		s := vector.MustFixedCol[types.Date](vec)
   131  		for _, v := range s {
   132  			packer.EncodeDate(v)
   133  			ret = append(ret, packer.Bytes())
   134  			packer.Reset()
   135  		}
   136  
   137  	case types.T_time:
   138  		s := vector.MustFixedCol[types.Time](vec)
   139  		for _, v := range s {
   140  			packer.EncodeTime(v)
   141  			ret = append(ret, packer.Bytes())
   142  			packer.Reset()
   143  		}
   144  
   145  	case types.T_datetime:
   146  		s := vector.MustFixedCol[types.Datetime](vec)
   147  		for _, v := range s {
   148  			packer.EncodeDatetime(v)
   149  			ret = append(ret, packer.Bytes())
   150  			packer.Reset()
   151  		}
   152  
   153  	case types.T_timestamp:
   154  		s := vector.MustFixedCol[types.Timestamp](vec)
   155  		for _, v := range s {
   156  			packer.EncodeTimestamp(v)
   157  			ret = append(ret, packer.Bytes())
   158  			packer.Reset()
   159  		}
   160  
   161  	case types.T_enum:
   162  		s := vector.MustFixedCol[types.Enum](vec)
   163  		for _, v := range s {
   164  			packer.EncodeEnum(v)
   165  			ret = append(ret, packer.Bytes())
   166  			packer.Reset()
   167  		}
   168  
   169  	case types.T_decimal64:
   170  		s := vector.MustFixedCol[types.Decimal64](vec)
   171  		for _, v := range s {
   172  			packer.EncodeDecimal64(v)
   173  			ret = append(ret, packer.Bytes())
   174  			packer.Reset()
   175  		}
   176  
   177  	case types.T_decimal128:
   178  		s := vector.MustFixedCol[types.Decimal128](vec)
   179  		for _, v := range s {
   180  			packer.EncodeDecimal128(v)
   181  			ret = append(ret, packer.Bytes())
   182  			packer.Reset()
   183  		}
   184  
   185  	case types.T_uuid:
   186  		s := vector.MustFixedCol[types.Uuid](vec)
   187  		for _, v := range s {
   188  			packer.EncodeStringType(v[:])
   189  			ret = append(ret, packer.Bytes())
   190  			packer.Reset()
   191  		}
   192  
   193  	case types.T_json, types.T_char, types.T_varchar,
   194  		types.T_binary, types.T_varbinary, types.T_blob, types.T_text, types.T_Rowid,
   195  		types.T_array_float32, types.T_array_float64:
   196  		for i := 0; i < vec.Length(); i++ {
   197  			packer.EncodeStringType(vec.GetBytesAt(i))
   198  			ret = append(ret, packer.Bytes())
   199  			packer.Reset()
   200  		}
   201  	default:
   202  		panic(fmt.Sprintf("unknown type: %v", vec.GetType().String()))
   203  
   204  	}
   205  
   206  	l := vec.Length()
   207  
   208  	if vec.IsConst() {
   209  		for len(ret) < l {
   210  			ret = append(ret, ret[0])
   211  		}
   212  	}
   213  
   214  	if len(ret) != l {
   215  		panic(fmt.Sprintf("bad result, expecting %v, got %v", l, len(ret)))
   216  	}
   217  
   218  	return
   219  }
   220  
   221  func EncodePrimaryKey(v any, packer *types.Packer) []byte {
   222  	packer.Reset()
   223  
   224  	switch v := v.(type) {
   225  
   226  	case bool:
   227  		packer.EncodeBool(v)
   228  
   229  	case int8:
   230  		packer.EncodeInt8(v)
   231  
   232  	case int16:
   233  		packer.EncodeInt16(v)
   234  
   235  	case int32:
   236  		packer.EncodeInt32(v)
   237  
   238  	case int64:
   239  		packer.EncodeInt64(v)
   240  
   241  	case uint8:
   242  		packer.EncodeUint8(v)
   243  
   244  	case uint16:
   245  		packer.EncodeUint16(v)
   246  
   247  	case uint32:
   248  		packer.EncodeUint32(v)
   249  
   250  	case uint64:
   251  		packer.EncodeUint64(v)
   252  
   253  	case float32:
   254  		packer.EncodeFloat32(v)
   255  
   256  	case float64:
   257  		packer.EncodeFloat64(v)
   258  
   259  	case types.Date:
   260  		packer.EncodeDate(v)
   261  
   262  	case types.Time:
   263  		packer.EncodeTime(v)
   264  
   265  	case types.Datetime:
   266  		packer.EncodeDatetime(v)
   267  
   268  	case types.Timestamp:
   269  		packer.EncodeTimestamp(v)
   270  
   271  	case types.Decimal64:
   272  		packer.EncodeDecimal64(v)
   273  
   274  	case types.Decimal128:
   275  		packer.EncodeDecimal128(v)
   276  
   277  	case types.Uuid:
   278  		packer.EncodeStringType(v[:])
   279  
   280  	case types.Enum:
   281  		packer.EncodeEnum(v)
   282  
   283  	case string:
   284  		packer.EncodeStringType([]byte(v))
   285  
   286  	case []byte:
   287  		packer.EncodeStringType(v)
   288  
   289  	default:
   290  		panic(fmt.Sprintf("unknown type: %T", v))
   291  
   292  	}
   293  
   294  	return packer.Bytes()
   295  }