github.com/matrixorigin/matrixone@v1.2.0/pkg/pb/timestamp/timestamp.go (about)

     1  // Copyright 2021 - 2022 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 timestamp
    16  
    17  import (
    18  	"fmt"
    19  	"math"
    20  	"strings"
    21  	"time"
    22  
    23  	"github.com/fagongzi/util/format"
    24  	"github.com/matrixorigin/matrixone/pkg/common/moerr"
    25  )
    26  
    27  /*
    28  // String returns the string representation of the HLC timestamp.
    29  func (lhs Timestamp) String() string {
    30  	panic("not implemented")
    31  }
    32  */
    33  
    34  // IsEmpty returns a boolean value indicating whether the current timestamp
    35  // is an empty value.
    36  func (m Timestamp) IsEmpty() bool {
    37  	return m.PhysicalTime == 0 && m.LogicalTime == 0
    38  }
    39  
    40  // ToStdTime converts the HLC timestamp to a regular golang stdlib UTC
    41  // timestamp. The logical time component of the HLC is lost after the
    42  // conversion.
    43  func (m Timestamp) ToStdTime() time.Time {
    44  	return time.Unix(0, m.PhysicalTime).UTC()
    45  }
    46  
    47  // Equal returns a boolean value indicating whether the lhs timestamp equals
    48  // to the rhs timestamp.
    49  func (m Timestamp) Equal(rhs Timestamp) bool {
    50  	return m.PhysicalTime == rhs.PhysicalTime &&
    51  		m.LogicalTime == rhs.LogicalTime
    52  }
    53  
    54  // Less returns a boolean value indicating whether the lhs timestamp is less
    55  // than the rhs timestamp value.
    56  func (m Timestamp) Less(rhs Timestamp) bool {
    57  	return m.PhysicalTime < rhs.PhysicalTime ||
    58  		(m.PhysicalTime == rhs.PhysicalTime && m.LogicalTime < rhs.LogicalTime)
    59  }
    60  
    61  // Greater returns a boolean value indicating whether the lhs timestamp is
    62  // greater than the rhs timestamp value.
    63  func (m Timestamp) Greater(rhs Timestamp) bool {
    64  	return m.PhysicalTime > rhs.PhysicalTime ||
    65  		(m.PhysicalTime == rhs.PhysicalTime && m.LogicalTime > rhs.LogicalTime)
    66  }
    67  
    68  // LessEq returns a boolean value indicating whether the lhs timestamp is
    69  // less than or equal to the rhs timestamp value.
    70  func (m Timestamp) LessEq(rhs Timestamp) bool {
    71  	return m.Less(rhs) || m.Equal(rhs)
    72  }
    73  
    74  // GreaterEq returns a boolean value indicating whether the lhs timestamp is
    75  // greater than or equal to the rhs timestamp value.
    76  func (m Timestamp) GreaterEq(rhs Timestamp) bool {
    77  	return m.Greater(rhs) || m.Equal(rhs)
    78  }
    79  
    80  // Next returns the smallest timestamp that is greater than the current
    81  // timestamp.
    82  func (m Timestamp) Next() Timestamp {
    83  	if m.LogicalTime == math.MaxUint32 {
    84  		return Timestamp{PhysicalTime: m.PhysicalTime + 1}
    85  	}
    86  
    87  	return Timestamp{
    88  		PhysicalTime: m.PhysicalTime,
    89  		LogicalTime:  m.LogicalTime + 1,
    90  	}
    91  }
    92  
    93  // Prev returns the smallest timestamp that is less than the current
    94  // timestamp.
    95  func (m Timestamp) Prev() Timestamp {
    96  	if m.LogicalTime == 0 {
    97  		return Timestamp{PhysicalTime: m.PhysicalTime - 1, LogicalTime: math.MaxUint32}
    98  	}
    99  
   100  	return Timestamp{
   101  		PhysicalTime: m.PhysicalTime,
   102  		LogicalTime:  m.LogicalTime - 1,
   103  	}
   104  }
   105  
   106  // DebugString returns debug string
   107  func (m Timestamp) DebugString() string {
   108  	return fmt.Sprintf("%d-%d", m.PhysicalTime, m.LogicalTime)
   109  }
   110  
   111  func (m Timestamp) ProtoSize() int {
   112  	return m.Size()
   113  }
   114  
   115  // ParseTimestamp parse timestamp from debug string
   116  func ParseTimestamp(value string) (Timestamp, error) {
   117  	values := strings.Split(value, "-")
   118  	if len(values) != 2 {
   119  		return Timestamp{}, moerr.NewInvalidInputNoCtx("invalid debug timestamp string: %s", value)
   120  	}
   121  
   122  	p, err := format.ParseStringInt64(values[0])
   123  	if err != nil {
   124  		return Timestamp{}, moerr.NewInvalidInputNoCtx("invalid debug timestamp string: %s", value)
   125  	}
   126  	l, err := format.ParseStringUint32(values[1])
   127  	if err != nil {
   128  		return Timestamp{}, moerr.NewInvalidInputNoCtx("invalid debug timestamp string: %s", value)
   129  	}
   130  	return Timestamp{PhysicalTime: p, LogicalTime: l}, nil
   131  }