Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix format #5

Merged
merged 1 commit into from
Jun 23, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 83 additions & 34 deletions dbms/src/Storages/Transaction/TiKVRecordFormat.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,17 +83,35 @@ inline TiKVKey encodeAsTiKVKey(const String & ori_str)
return TiKVKey(ss.releaseStr());
}

inline UInt64 encodeUInt64(const UInt64 x) { return toBigEndian(x); }
inline UInt64 encodeUInt64(const UInt64 x)
{
return toBigEndian(x);
}

inline UInt64 encodeInt64(const Int64 x) { return encodeUInt64(static_cast<UInt64>(x) ^ SIGN_MASK); }
inline UInt64 encodeInt64(const Int64 x)
{
return encodeUInt64(static_cast<UInt64>(x) ^ SIGN_MASK);
}

inline UInt64 encodeUInt64Desc(const UInt64 x) { return encodeUInt64(~x); }
inline UInt64 encodeUInt64Desc(const UInt64 x)
{
return encodeUInt64(~x);
}

inline UInt64 decodeUInt64(const UInt64 x) { return toBigEndian(x); }
inline UInt64 decodeUInt64(const UInt64 x)
{
return toBigEndian(x);
}

inline UInt64 decodeUInt64Desc(const UInt64 x) { return ~decodeUInt64(x); }
inline UInt64 decodeUInt64Desc(const UInt64 x)
{
return ~decodeUInt64(x);
}

inline Int64 decodeInt64(const UInt64 x) { return static_cast<Int64>(decodeUInt64(x) ^ SIGN_MASK); }
inline Int64 decodeInt64(const UInt64 x)
{
return static_cast<Int64>(decodeUInt64(x) ^ SIGN_MASK);
}

inline void encodeInt64(const Int64 x, WriteBuffer & ss)
{
Expand Down Expand Up @@ -125,7 +143,10 @@ inline DecodedTiKVKey genRawKey(const TableID tableId, const HandleID handleId)
return key;
}

inline TiKVKey genKey(const TableID tableId, const HandleID handleId) { return encodeAsTiKVKey(genRawKey(tableId, handleId)); }
inline TiKVKey genKey(const TableID tableId, const HandleID handleId)
{
return encodeAsTiKVKey(genRawKey(tableId, handleId));
}

inline TiKVKey genKey(const TiDB::TableInfo & table_info, std::vector<Field> keys)
{
Expand Down Expand Up @@ -183,29 +204,50 @@ inline std::tuple<DecodedTiKVKey, size_t> decodeTiKVKeyFull(const TiKVKey & key)
}
}

inline DecodedTiKVKey decodeTiKVKey(const TiKVKey & key) { return std::get<0>(decodeTiKVKeyFull(key)); }
inline DecodedTiKVKey decodeTiKVKey(const TiKVKey & key)
{
return std::get<0>(decodeTiKVKeyFull(key));
}

inline Timestamp getTs(const TiKVKey & key) { return decodeUInt64Desc(read<UInt64>(key.data() + key.dataSize() - 8)); }
inline Timestamp getTs(const TiKVKey & key)
{
return decodeUInt64Desc(read<UInt64>(key.data() + key.dataSize() - 8));
}

inline TableID getTableId(const DecodedTiKVKey & key) { return decodeInt64(read<UInt64>(key.data() + 1)); }
inline TableID getTableId(const DecodedTiKVKey & key)
{
return decodeInt64(read<UInt64>(key.data() + 1));
}

inline HandleID getHandle(const DecodedTiKVKey & key) { return decodeInt64(read<UInt64>(key.data() + RAW_KEY_NO_HANDLE_SIZE)); }
inline HandleID getHandle(const DecodedTiKVKey & key)
{
return decodeInt64(read<UInt64>(key.data() + RAW_KEY_NO_HANDLE_SIZE));
}

inline RawTiDBPK getRawTiDBPK(const DecodedTiKVKey & key)
{
return std::make_shared<const std::string>(key.begin() + RAW_KEY_NO_HANDLE_SIZE, key.end());
}

inline TableID getTableId(const TiKVKey & key) { return getTableId(decodeTiKVKey(key)); }
inline TableID getTableId(const TiKVKey & key)
{
return getTableId(decodeTiKVKey(key));
}

inline HandleID getHandle(const TiKVKey & key) { return getHandle(decodeTiKVKey(key)); }
inline HandleID getHandle(const TiKVKey & key)
{
return getHandle(decodeTiKVKey(key));
}

inline bool isRecord(const DecodedTiKVKey & raw_key)
{
return raw_key.size() >= RAW_KEY_SIZE && raw_key[0] == TABLE_PREFIX && memcmp(raw_key.data() + 9, RECORD_PREFIX_SEP, 2) == 0;
}

inline TiKVKey truncateTs(const TiKVKey & key) { return TiKVKey(String(key.data(), key.dataSize() - sizeof(Timestamp))); }
inline TiKVKey truncateTs(const TiKVKey & key)
{
return TiKVKey(String(key.data(), key.dataSize() - sizeof(Timestamp)));
}

inline TiKVKey appendTs(const TiKVKey & key, Timestamp ts)
{
Expand All @@ -222,7 +264,12 @@ inline TiKVKey genKey(TableID tableId, HandleID handleId, Timestamp ts)
}

inline TiKVValue encodeLockCfValue(
UInt8 lock_type, const String & primary, Timestamp ts, UInt64 ttl, const String * short_value = nullptr, Timestamp min_commit_ts = 0)
UInt8 lock_type,
const String & primary,
Timestamp ts,
UInt64 ttl,
const String * short_value = nullptr,
Timestamp min_commit_ts = 0)
{
WriteBufferFromOwnString res;
res.write(lock_type);
Expand Down Expand Up @@ -282,7 +329,10 @@ inline R readVarInt(const char *& data, size_t & len)
return res;
}

inline UInt64 readVarUInt(const char *& data, size_t & len) { return readVarInt<UInt64>(data, len); }
inline UInt64 readVarUInt(const char *& data, size_t & len)
{
return readVarInt<UInt64>(data, len);
}

inline UInt8 readUInt8(const char *& data, size_t & len)
{
Expand Down Expand Up @@ -354,30 +404,29 @@ inline DecodedWriteCFValue decodeWriteCfValue(const TiKVValue & value)
auto flag = RecordKVFormat::readUInt8(data, len);
switch (flag)
{
case RecordKVFormat::SHORT_VALUE_PREFIX:
{
size_t slen = RecordKVFormat::readUInt8(data, len);
if (slen > len)
throw Exception("content len not equal to short value len", ErrorCodes::LOGICAL_ERROR);
short_value = RecordKVFormat::readRawString<std::string_view>(data, len, slen);
break;
}
case RecordKVFormat::FLAG_OVERLAPPED_ROLLBACK:
// ignore
break;
case RecordKVFormat::GC_FENCE_PREFIX:
/**
case RecordKVFormat::SHORT_VALUE_PREFIX:
{
size_t slen = RecordKVFormat::readUInt8(data, len);
if (slen > len)
throw Exception("content len not equal to short value len", ErrorCodes::LOGICAL_ERROR);
short_value = RecordKVFormat::readRawString<std::string_view>(data, len, slen);
break;
}
case RecordKVFormat::FLAG_OVERLAPPED_ROLLBACK:
// ignore
break;
case RecordKVFormat::GC_FENCE_PREFIX:
/**
* according to https://github.com/tikv/tikv/pull/9207, when meet `GC fence` flag, it is definitely a
* rewriting record and there must be a complete row written to tikv, just ignore it in tiflash.
*/
return std::nullopt;
default:
throw Exception("invalid flag " + std::to_string(flag) + " in write cf", ErrorCodes::LOGICAL_ERROR);
return std::nullopt;
default:
throw Exception("invalid flag " + std::to_string(flag) + " in write cf", ErrorCodes::LOGICAL_ERROR);
}
}

return InnerDecodedWriteCFValue{write_type, prewrite_ts,
short_value.empty() ? nullptr : std::make_shared<const TiKVValue>(short_value.data(), short_value.length())};
return InnerDecodedWriteCFValue{write_type, prewrite_ts, short_value.empty() ? nullptr : std::make_shared<const TiKVValue>(short_value.data(), short_value.length())};
}

inline TiKVValue encodeWriteCfValue(UInt8 write_type, Timestamp ts, std::string_view short_value = {}, bool gc_fence = false)
Expand Down