github.com/matrixorigin/matrixone@v0.7.0/pkg/vm/engine/tae/txn/txnbase/helper.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  package txnbase
    16  
    17  import (
    18  	"bytes"
    19  	"encoding/binary"
    20  
    21  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/common"
    22  )
    23  
    24  const (
    25  	IDSize = 8 + 8 + 8 + 4 + 2 + 1
    26  )
    27  
    28  func MarshalID(id *common.ID) []byte {
    29  	var err error
    30  	var w bytes.Buffer
    31  	if err = binary.Write(&w, binary.BigEndian, id.TableID); err != nil {
    32  		panic(err)
    33  	}
    34  	if err = binary.Write(&w, binary.BigEndian, id.SegmentID); err != nil {
    35  		panic(err)
    36  	}
    37  	if err = binary.Write(&w, binary.BigEndian, id.BlockID); err != nil {
    38  		panic(err)
    39  	}
    40  	if err = binary.Write(&w, binary.BigEndian, id.PartID); err != nil {
    41  		panic(err)
    42  	}
    43  	if err = binary.Write(&w, binary.BigEndian, id.Idx); err != nil {
    44  		panic(err)
    45  	}
    46  	if err = binary.Write(&w, binary.BigEndian, id.Iter); err != nil {
    47  		panic(err)
    48  	}
    49  	return w.Bytes()
    50  }
    51  
    52  func UnmarshalID(buf []byte) *common.ID {
    53  	var err error
    54  	r := bytes.NewBuffer(buf)
    55  	id := common.ID{}
    56  	if err = binary.Read(r, binary.BigEndian, &id.TableID); err != nil {
    57  		panic(err)
    58  	}
    59  	if err = binary.Read(r, binary.BigEndian, &id.SegmentID); err != nil {
    60  		panic(err)
    61  	}
    62  	if err = binary.Read(r, binary.BigEndian, &id.BlockID); err != nil {
    63  		panic(err)
    64  	}
    65  	if err = binary.Read(r, binary.BigEndian, &id.PartID); err != nil {
    66  		panic(err)
    67  	}
    68  	if err = binary.Read(r, binary.BigEndian, &id.Idx); err != nil {
    69  		panic(err)
    70  	}
    71  	if err = binary.Read(r, binary.BigEndian, &id.Iter); err != nil {
    72  		panic(err)
    73  	}
    74  	return &id
    75  }