github.com/matrixorigin/matrixone@v0.7.0/pkg/vm/engine/tae/logstore/store/index.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 store
    16  
    17  import (
    18  	"encoding/binary"
    19  	"fmt"
    20  	"io"
    21  )
    22  
    23  type Index struct {
    24  	LSN  uint64
    25  	CSN  uint32
    26  	Size uint32
    27  }
    28  
    29  func NewIndex(lsn uint64, csn, size uint32) *Index {
    30  	return &Index{
    31  		LSN:  lsn,
    32  		CSN:  csn,
    33  		Size: size,
    34  	}
    35  }
    36  
    37  func (index *Index) Compare(o *Index) int {
    38  	if index.LSN > o.LSN {
    39  		return 1
    40  	} else if index.LSN < o.LSN {
    41  		return -1
    42  	}
    43  	if index.CSN > o.CSN {
    44  		return 1
    45  	} else if index.CSN < o.CSN {
    46  		return -1
    47  	}
    48  	return 0
    49  }
    50  
    51  func (index *Index) WriteTo(w io.Writer) (n int64, err error) {
    52  	if err = binary.Write(w, binary.BigEndian, index.LSN); err != nil {
    53  		return
    54  	}
    55  	if err = binary.Write(w, binary.BigEndian, index.CSN); err != nil {
    56  		return
    57  	}
    58  	if err = binary.Write(w, binary.BigEndian, index.Size); err != nil {
    59  		return
    60  	}
    61  	n = 16
    62  	return
    63  }
    64  
    65  func (index *Index) ReadFrom(r io.Reader) (n int64, err error) {
    66  	if err = binary.Read(r, binary.BigEndian, &index.LSN); err != nil {
    67  		return
    68  	}
    69  	if err = binary.Read(r, binary.BigEndian, &index.CSN); err != nil {
    70  		return
    71  	}
    72  	if err = binary.Read(r, binary.BigEndian, &index.Size); err != nil {
    73  		return
    74  	}
    75  	n = 16
    76  	return
    77  }
    78  
    79  func (index *Index) Clone() *Index {
    80  	if index == nil {
    81  		return nil
    82  	}
    83  	return &Index{
    84  		LSN:  index.LSN,
    85  		CSN:  index.CSN,
    86  		Size: index.Size,
    87  	}
    88  }
    89  func (index *Index) String() string {
    90  	if index == nil {
    91  		return "<nil index>"
    92  	}
    93  	return fmt.Sprintf("<Index[%d:%d/%d]>", index.LSN, index.CSN, index.Size)
    94  }