github.com/cockroachdb/cockroachdb-parser@v0.23.3-0.20240213214944-911057d40c9a/pkg/sql/pgrepl/lsn/lsn.go (about)

     1  // Copyright 2023 The Cockroach Authors.
     2  //
     3  // Use of this software is governed by the Business Source License
     4  // included in the file licenses/BSL.txt.
     5  //
     6  // As of the Change Date specified in that file, in accordance with
     7  // the Business Source License, use of this software will be governed
     8  // by the Apache License, Version 2.0, included in the file
     9  // licenses/APL.txt.
    10  
    11  // Package lsn contains logic for handling the pg_lsn type.
    12  package lsn
    13  
    14  import (
    15  	"fmt"
    16  
    17  	"github.com/cockroachdb/apd/v3"
    18  )
    19  
    20  type LSN uint64
    21  
    22  func (lsn LSN) String() string {
    23  	return fmt.Sprintf("%X/%X", uint32(lsn>>32), uint32(lsn))
    24  }
    25  
    26  func ParseLSN(str string) (LSN, error) {
    27  	var lo, hi uint32
    28  	if _, err := fmt.Sscanf(str, "%X/%X", &hi, &lo); err != nil {
    29  		return 0, err
    30  	}
    31  	return (LSN(hi) << 32) | LSN(lo), nil
    32  }
    33  
    34  func (lsn LSN) Decimal() (*apd.Decimal, error) {
    35  	ret, _, err := apd.NewFromString(fmt.Sprintf("%d", lsn))
    36  	return ret, err
    37  }
    38  
    39  func (lsn LSN) Compare(other LSN) int {
    40  	if lsn > other {
    41  		return 1
    42  	}
    43  	if lsn < other {
    44  		return -1
    45  	}
    46  	return 0
    47  }
    48  
    49  func (lsn LSN) Add(val LSN) LSN {
    50  	return lsn + val
    51  }
    52  
    53  func (lsn LSN) Sub(val LSN) LSN {
    54  	return lsn - val
    55  }