github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/c-deps/libroach/timestamp.h (about) 1 // Copyright 2018 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 #pragma once 12 13 #include <libroach.h> 14 #include "protos/storage/enginepb/mvcc.pb.h" 15 16 namespace cockroach { 17 18 const DBTimestamp kZeroTimestamp = {0, 0}; 19 20 inline DBTimestamp ToDBTimestamp(const cockroach::util::hlc::LegacyTimestamp& timestamp) { 21 return DBTimestamp{timestamp.wall_time(), timestamp.logical()}; 22 } 23 24 inline DBTimestamp PrevTimestamp(DBTimestamp ts) { 25 if (ts.logical > 0) { 26 --ts.logical; 27 } else if (ts.wall_time == 0) { 28 fprintf(stderr, "no previous time for zero timestamp\n"); 29 abort(); 30 } else { 31 --ts.wall_time; 32 ts.logical = std::numeric_limits<int32_t>::max(); 33 } 34 return ts; 35 } 36 37 inline bool operator==(const DBTimestamp& a, const DBTimestamp& b) { 38 return a.wall_time == b.wall_time && a.logical == b.logical; 39 } 40 41 inline bool operator!=(const DBTimestamp& a, const DBTimestamp& b) { return !(a == b); } 42 43 inline bool operator<(const DBTimestamp& a, const DBTimestamp& b) { 44 return a.wall_time < b.wall_time || (a.wall_time == b.wall_time && a.logical < b.logical); 45 } 46 47 inline bool operator>(const DBTimestamp& a, const DBTimestamp& b) { return b < a; } 48 49 inline bool operator<=(const DBTimestamp& a, const DBTimestamp& b) { return !(b < a); } 50 51 inline bool operator>=(const DBTimestamp& a, const DBTimestamp& b) { return b <= a; } 52 53 } // namespace cockroach