github.com/matrixorigin/matrixone@v0.7.0/pkg/txn/storage/memorystorage/memtable/time.go (about) 1 // Copyright 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 memtable 16 17 import ( 18 "fmt" 19 20 "github.com/matrixorigin/matrixone/pkg/container/types" 21 "github.com/matrixorigin/matrixone/pkg/pb/timestamp" 22 "github.com/matrixorigin/matrixone/pkg/txn/clock" 23 ) 24 25 type Time struct { 26 Timestamp timestamp.Timestamp 27 Statement int 28 } 29 30 func Now(clock clock.Clock) Time { 31 now, _ := clock.Now() 32 return Time{ 33 Timestamp: now, 34 } 35 } 36 37 func (t *Time) Tick() { 38 t.Statement++ 39 } 40 41 func (t Time) After(t2 Time) bool { 42 if t.Timestamp.Greater(t2.Timestamp) { 43 return true 44 } 45 if t.Timestamp.Less(t2.Timestamp) { 46 return false 47 } 48 return t.Statement > t2.Statement 49 } 50 51 func (t Time) Before(t2 Time) bool { 52 if t.Timestamp.Less(t2.Timestamp) { 53 return true 54 } 55 if t.Timestamp.Greater(t2.Timestamp) { 56 return false 57 } 58 return t.Statement < t2.Statement 59 } 60 61 func (t Time) Equal(t2 Time) bool { 62 if !t.Timestamp.Equal(t2.Timestamp) { 63 return false 64 } 65 if t.Statement != t2.Statement { 66 return false 67 } 68 return true 69 } 70 71 func (t Time) String() string { 72 return fmt.Sprintf("Time{%d, %d, %d}", 73 t.Timestamp.PhysicalTime, 74 t.Timestamp.LogicalTime, 75 t.Statement, 76 ) 77 } 78 79 func (t Time) IsZero() bool { 80 return t.Timestamp.IsEmpty() && t.Statement == 0 81 } 82 83 func (t Time) ToTxnTS() types.TS { 84 return types.BuildTS( 85 t.Timestamp.PhysicalTime, 86 t.Timestamp.LogicalTime, 87 ) 88 }