github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/c-deps/libroach/encoding.h (about) 1 // Copyright 2015 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 <rocksdb/slice.h> 15 #include <stdint.h> 16 #include "defines.h" 17 #include "protos/storage/enginepb/mvcc.pb.h" 18 19 namespace cockroach { 20 21 const int kIntZero = 136; 22 const int kIntSmall = 109; 23 const int kIntMax = 253; 24 const int kIntMin = 128; 25 26 // EncodeUint32 encodes the uint32 value using a big-endian 4 byte 27 // representation. The bytes are appended to the supplied buffer. 28 void EncodeUint32(std::string* buf, uint32_t v); 29 30 // EncodeUint64 encodes the uint64 value using a big-endian 8 byte 31 // representation. The encoded bytes are appended to the supplied buffer. 32 void EncodeUint64(std::string* buf, uint64_t v); 33 34 // EncodeUvarint64 encodes the uint64 value using a variable-length 35 // representation. The encoded bytes are appended to the supplied buffer. 36 void EncodeUvarint64(std::string* buf, uint64_t v); 37 38 // DecodedUint32 decodes a fixed-length encoded uint32 from a buffer, returning 39 // true on a successful decode. The decoded value is returned in *value. 40 bool DecodeUint32(rocksdb::Slice* buf, uint32_t* value); 41 42 // DecodedUint64 decodes a fixed-length encoded uint64 from a buffer, returning 43 // true on a successful decode. The decoded value is returned in *value. 44 bool DecodeUint64(rocksdb::Slice* buf, uint64_t* value); 45 46 // DecodeUvarint64 decodes a variable-length encoded uint64 from a buffer, 47 // returning true on a successful decode. The decoded value is returned in 48 // *value. 49 bool DecodeUvarint64(rocksdb::Slice* buf, uint64_t* value); 50 51 const int kMVCCVersionTimestampSize = 12; 52 53 void EncodeTimestamp(std::string& s, int64_t wall_time, int32_t logical); 54 std::string EncodeTimestamp(DBTimestamp ts); 55 56 // MVCC keys are encoded as <key>\x00[<wall_time>[<logical>]]<#timestamp-bytes>. A 57 // custom RocksDB comparator (DBComparator) is used to maintain the desired 58 // ordering as these keys do not sort lexicographically correctly. 59 std::string EncodeKey(const rocksdb::Slice& key, int64_t wall_time, int32_t logical); 60 61 // MVCC keys are encoded as <key>\x00[<wall_time>[<logical>]]<#timestamp-bytes>. A 62 // custom RocksDB comparator (DBComparator) is used to maintain the desired 63 // ordering as these keys do not sort lexicographically correctly. 64 std::string EncodeKey(DBKey k); 65 66 // SplitKey splits an MVCC key into key and timestamp slices. See also 67 // DecodeKey if you want to decode the timestamp. Returns true on 68 // success and false on any decoding error. 69 WARN_UNUSED_RESULT inline bool SplitKey(rocksdb::Slice buf, rocksdb::Slice* key, 70 rocksdb::Slice* timestamp) { 71 if (buf.empty()) { 72 return false; 73 } 74 const char ts_size = buf[buf.size() - 1]; 75 if (ts_size >= buf.size()) { 76 return false; 77 } 78 *key = rocksdb::Slice(buf.data(), buf.size() - ts_size - 1); 79 *timestamp = rocksdb::Slice(key->data() + key->size(), ts_size); 80 return true; 81 } 82 83 // DecodeTimestamp an MVCC encoded timestamp. Returns true on success 84 // and false on any decoding error. 85 WARN_UNUSED_RESULT bool DecodeTimestamp(rocksdb::Slice* timestamp, int64_t* wall_time, 86 int32_t* logical); 87 WARN_UNUSED_RESULT bool DecodeTimestamp(rocksdb::Slice buf, 88 cockroach::util::hlc::Timestamp* timestamp); 89 90 // EmptyTimestamp returns whether ts represents an empty timestamp where both 91 // the wall_time and logical components are zero. 92 bool EmptyTimestamp(DBTimestamp ts); 93 94 // DecodeKey splits an MVCC key into a key slice and decoded 95 // timestamp. See also SplitKey if you want to do not need to decode 96 // the timestamp. Returns true on success and false on any decoding 97 // error. 98 WARN_UNUSED_RESULT bool DecodeKey(rocksdb::Slice buf, rocksdb::Slice* key, int64_t* wall_time, 99 int32_t* logical); 100 WARN_UNUSED_RESULT inline bool DecodeKey(rocksdb::Slice buf, rocksdb::Slice* key, DBTimestamp* ts) { 101 return DecodeKey(buf, key, &ts->wall_time, &ts->logical); 102 } 103 104 const int kLocalSuffixLength = 4; 105 106 // DecodeRangeIDKey parses a local range ID key into range ID, infix, 107 // suffix, and detail. 108 WARN_UNUSED_RESULT bool DecodeRangeIDKey(rocksdb::Slice buf, int64_t* range_id, 109 rocksdb::Slice* infix, rocksdb::Slice* suffix, 110 rocksdb::Slice* detail); 111 112 // KeyPrefix strips the timestamp from an MVCC encoded key, returning 113 // a slice that is still MVCC encoded. This is used by the prefix 114 // extractor used to build bloom filters on the prefix. 115 rocksdb::Slice KeyPrefix(const rocksdb::Slice& src); 116 117 // IsInt peeks at the type of the value encoded at the start of buf and checks 118 // if it is of type int. 119 WARN_UNUSED_RESULT bool IsInt(rocksdb::Slice* buf); 120 121 // StripTenantPrefix validates that the given key has a tenant prefix. On 122 // completion, buf holds the remainder of the key (with the prefix removed). 123 WARN_UNUSED_RESULT bool StripTenantPrefix(rocksdb::Slice* buf); 124 125 // DecodeTenantAndTablePrefix validates that the given key has a tenant and 126 // table prefix. On completion, buf holds the remainder of the key (with the 127 // prefix removed) and tbl stores the decoded descriptor ID of the table. 128 WARN_UNUSED_RESULT bool DecodeTenantAndTablePrefix(rocksdb::Slice* buf, uint64_t* tbl); 129 130 } // namespace cockroach