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

[FLASH-298] Import DecodedTiKVKey and TableRowIDMinMax #218

Merged
merged 2 commits into from
Sep 4, 2019
Merged
Changes from 1 commit
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
Next Next commit
FLASH-298
import DecodedTiKVKey
import TableRowIDMinMax to optimize min/max key for table id
solotzg committed Sep 3, 2019
commit 7b862050a8dd1d8e8d18d13014b81db0f41c2f1b
1 change: 1 addition & 0 deletions dbms/src/Debug/DBGInvoker.cpp
Original file line number Diff line number Diff line change
@@ -64,6 +64,7 @@ DBGInvoker::DBGInvoker()
regFunc("try_flush_region", dbgFuncTryFlushRegion);

regFunc("dump_all_region", dbgFuncDumpAllRegion);
regFunc("dump_all_mock_region", dbgFuncDumpAllMockRegion);

regFunc("enable_schema_sync_service", dbgFuncEnableSchemaSyncService);
regFunc("refresh_schemas", dbgFuncRefreshSchemas);
60 changes: 38 additions & 22 deletions dbms/src/Debug/dbgFuncRegion.cpp
Original file line number Diff line number Diff line change
@@ -143,13 +143,8 @@ void dbgFuncRegionSnapshotWithData(Context & context, const ASTs & args, DBGInvo
TiKVValue value = RecordKVFormat::EncodeRow(table->table_info, fields);
UInt64 commit_ts = tso;
UInt64 prewrite_ts = tso;
TiKVValue commit_value;

if (del)
commit_value = RecordKVFormat::encodeWriteCfValue(Region::DelFlag, prewrite_ts);
else
commit_value = RecordKVFormat::encodeWriteCfValue(Region::PutFlag, prewrite_ts, value);

TiKVValue commit_value = del ? RecordKVFormat::encodeWriteCfValue(Region::DelFlag, prewrite_ts)
: RecordKVFormat::encodeWriteCfValue(Region::PutFlag, prewrite_ts, value);
TiKVKey commit_key = RecordKVFormat::appendTs(key, commit_ts);

region->insert(Region::write_cf_name, std::move(commit_key), std::move(commit_value));
@@ -225,7 +220,7 @@ std::string getRegionKeyString(const TiKVRange::Handle s, const TiKVKey & k)
{
if (s.type != TiKVHandle::HandleIDType::NORMAL)
{
String raw_key = k.empty() ? "" : RecordKVFormat::decodeTiKVKey(k);
auto raw_key = k.empty() ? "" : RecordKVFormat::decodeTiKVKey(k);
bool is_record = RecordKVFormat::isRecord(raw_key);
std::stringstream ss;
if (is_record)
@@ -273,20 +268,10 @@ std::string getEndKeyString(TableID table_id, const TiKVKey & end_key)
}
}

void dbgFuncDumpAllRegion(Context & context, const ASTs & args, DBGInvoker::Printer output)
void dbgFuncDumpAllRegion(Context & context, TableID table_id, bool ignore_none, bool dump_status, DBGInvoker::Printer & output)
{
if (args.size() < 1)
throw Exception("Args not matched, should be: table_id", ErrorCodes::BAD_ARGUMENTS);

auto & tmt = context.getTMTContext();
TableID table_id = (TableID)safeGet<UInt64>(typeid_cast<const ASTLiteral &>(*args[0]).value);

bool ignore_none = false;
if (args.size() > 1)
ignore_none = (std::string(typeid_cast<const ASTIdentifier &>(*args[1]).name) == "true");

size_t size = 0;
tmt.getKVStore()->traverseRegions([&](const RegionID region_id, const RegionPtr & region) {
context.getTMTContext().getKVStore()->traverseRegions([&](const RegionID region_id, const RegionPtr & region) {
std::ignore = region_id;
auto range = region->getHandleRangeByTable(table_id);
size += 1;
@@ -295,15 +280,46 @@ void dbgFuncDumpAllRegion(Context & context, const ASTs & args, DBGInvoker::Prin
if (range.first >= range.second && ignore_none)
return;

ss << "table #" << table_id << " " << region->toString();
ss << region->toString(dump_status);
if (range.first >= range.second)
ss << " [none], ";
else
ss << " ranges: [" << range.first.toString() << ", " << range.second.toString() << "), ";
ss << region->dataInfo();
if (auto s = region->dataInfo(); s.size() > 2)
ss << ", " << s;
output(ss.str());
});
output("total size: " + toString(size));
}

void dbgFuncDumpAllRegion(Context & context, const ASTs & args, DBGInvoker::Printer output)
{
if (args.size() < 1)
throw Exception("Args not matched, should be: table_id", ErrorCodes::BAD_ARGUMENTS);

TableID table_id = (TableID)safeGet<UInt64>(typeid_cast<const ASTLiteral &>(*args[0]).value);

bool ignore_none = false;
if (args.size() > 1)
ignore_none = (std::string(typeid_cast<const ASTIdentifier &>(*args[1]).name) == "true");

bool dump_status = true;
if (args.size() > 2)
dump_status = (std::string(typeid_cast<const ASTIdentifier &>(*args[2]).name) == "true");

output("table #" + toString(table_id));
dbgFuncDumpAllRegion(context, table_id, ignore_none, dump_status, output);
}

void dbgFuncDumpAllMockRegion(Context & context, const ASTs & args, DBGInvoker::Printer output)
{
const String & database_name = typeid_cast<const ASTIdentifier &>(*args[0]).name;
const String & table_name = typeid_cast<const ASTIdentifier &>(*args[1]).name;

auto table = MockTiDB::instance().getTableByName(database_name, table_name);
auto table_id = table->id();

dbgFuncDumpAllRegion(context, table_id, false, false, output);
}

} // namespace DB
5 changes: 5 additions & 0 deletions dbms/src/Debug/dbgFuncRegion.h
Original file line number Diff line number Diff line change
@@ -30,6 +30,11 @@ void dbgFuncRegionSnapshotWithData(Context & context, const ASTs & args, DBGInvo
// ./storage-client.sh "DBGInvoke dump_all_region(table_id)"
void dbgFuncDumpAllRegion(Context & context, const ASTs & args, DBGInvoker::Printer output);

// Dump all region ranges for specific table
// Usage:
// ./storage-client.sh "DBGInvoke dump_all_mock_region(table_id)"
void dbgFuncDumpAllMockRegion(Context & context, const ASTs & args, DBGInvoker::Printer output);

// Try flush regions
// Usage:
// ./storage-client.sh "DBGInvoke try_flush()"
6 changes: 3 additions & 3 deletions dbms/src/Storages/Transaction/Region.cpp
Original file line number Diff line number Diff line change
@@ -42,7 +42,7 @@ TableID Region::insert(const std::string & cf, TiKVKey key, TiKVValue value)

TableID Region::doInsert(const std::string & cf, TiKVKey && key, TiKVValue && value)
{
std::string raw_key = RecordKVFormat::decodeTiKVKey(key);
auto raw_key = RecordKVFormat::decodeTiKVKey(key);
auto table_id = checkRecordAndValidTable(raw_key);
if (table_id == InvalidTableID)
return InvalidTableID;
@@ -59,7 +59,7 @@ TableID Region::remove(const std::string & cf, const TiKVKey & key)

TableID Region::doRemove(const std::string & cf, const TiKVKey & key)
{
std::string raw_key = RecordKVFormat::decodeTiKVKey(key);
auto raw_key = RecordKVFormat::decodeTiKVKey(key);
auto table_id = checkRecordAndValidTable(raw_key);
if (table_id == InvalidTableID)
return InvalidTableID;
@@ -585,7 +585,7 @@ void Region::compareAndCompleteSnapshot(HandleMap & handle_map, const TableID ta
if (ori_ts >= safe_point)
throw Exception("[Region::compareAndCompleteSnapshot] original ts >= gc safe point", ErrorCodes::LOGICAL_ERROR);

std::string raw_key = RecordKVFormat::genRawKey(table_id, handle);
auto raw_key = RecordKVFormat::genRawKey(table_id, handle);
TiKVKey key = RecordKVFormat::encodeAsTiKVKey(raw_key);
TiKVKey commit_key = RecordKVFormat::appendTs(key, ori_ts);
TiKVValue value = RecordKVFormat::encodeWriteCfValue(DelFlag, 0);
8 changes: 6 additions & 2 deletions dbms/src/Storages/Transaction/RegionCFDataBase.cpp
Original file line number Diff line number Diff line change
@@ -4,6 +4,10 @@
namespace DB
{

const std::string RegionWriteCFDataTrait::name = "write";
const std::string RegionDefaultCFDataTrait::name = "default";
const std::string RegionLockCFDataTrait::name = "lock";

template <typename Trait>
const TiKVKey & RegionCFDataBase<Trait>::getTiKVKey(const Value & val)
{
@@ -25,12 +29,12 @@ const TiKVValue & RegionCFDataBase<Trait>::getTiKVValue(const Value & val)
template <typename Trait>
TableID RegionCFDataBase<Trait>::insert(TiKVKey && key, TiKVValue && value)
{
const String & raw_key = RecordKVFormat::decodeTiKVKey(key);
const auto & raw_key = RecordKVFormat::decodeTiKVKey(key);
return insert(std::move(key), std::move(value), raw_key);
}

template <typename Trait>
TableID RegionCFDataBase<Trait>::insert(TiKVKey && key, TiKVValue && value, const String & raw_key)
TableID RegionCFDataBase<Trait>::insert(TiKVKey && key, TiKVValue && value, const DecodedTiKVKey & raw_key)
{
Pair kv_pair = Trait::genKVPair(std::move(key), raw_key, std::move(value));
if (shouldIgnoreInsert(kv_pair.second))
2 changes: 1 addition & 1 deletion dbms/src/Storages/Transaction/RegionCFDataBase.h
Original file line number Diff line number Diff line change
@@ -37,7 +37,7 @@ struct RegionCFDataBase

TableID insert(TiKVKey && key, TiKVValue && value);
TableID insert(const TableID table_id, std::pair<Key, Value> && kv_pair);
TableID insert(TiKVKey && key, TiKVValue && value, const String & raw_key);
TableID insert(TiKVKey && key, TiKVValue && value, const DecodedTiKVKey & raw_key);

static size_t calcTiKVKeyValueSize(const Value & value);

9 changes: 6 additions & 3 deletions dbms/src/Storages/Transaction/RegionCFDataTrait.h
Original file line number Diff line number Diff line change
@@ -19,12 +19,13 @@ struct CFKeyHasher

struct RegionWriteCFDataTrait
{
static const std::string name;
Copy link
Contributor

@innerr innerr Sep 3, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If my memories are not wrong, we could define static const std::string name = 'foobar' in .h, just work for static vars.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've test and this didn't work. std::string need constructor.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

name is a static variables of struct RegionWriteCFDataTrait, if we want to access it out of this struct, we must initialize it out of RegionWriteCFDataTrait.

using DecodedWriteCFValue = RecordKVFormat::DecodedWriteCFValue;
using Key = std::pair<HandleID, Timestamp>;
using Value = std::tuple<std::shared_ptr<const TiKVKey>, std::shared_ptr<const TiKVValue>, DecodedWriteCFValue>;
using Map = std::map<Key, Value>;

static Map::value_type genKVPair(TiKVKey && key, const String & raw_key, TiKVValue && value)
static Map::value_type genKVPair(TiKVKey && key, const DecodedTiKVKey & raw_key, TiKVValue && value)
{
HandleID handle_id = RecordKVFormat::getHandle(raw_key);
Timestamp ts = RecordKVFormat::getTs(key);
@@ -42,11 +43,12 @@ struct RegionWriteCFDataTrait

struct RegionDefaultCFDataTrait
{
static const std::string name;
using Key = std::pair<HandleID, Timestamp>;
using Value = std::tuple<std::shared_ptr<const TiKVKey>, std::shared_ptr<const TiKVValue>>;
using Map = std::map<Key, Value>;

static Map::value_type genKVPair(TiKVKey && key, const String & raw_key, TiKVValue && value)
static Map::value_type genKVPair(TiKVKey && key, const DecodedTiKVKey & raw_key, TiKVValue && value)
{
HandleID handle_id = RecordKVFormat::getHandle(raw_key);
Timestamp ts = RecordKVFormat::getTs(key);
@@ -57,12 +59,13 @@ struct RegionDefaultCFDataTrait

struct RegionLockCFDataTrait
{
static const std::string name;
using DecodedLockCFValue = RecordKVFormat::DecodedLockCFValue;
using Key = HandleID;
using Value = std::tuple<std::shared_ptr<const TiKVKey>, std::shared_ptr<const TiKVValue>, DecodedLockCFValue>;
using Map = std::unordered_map<Key, Value>;

static Map::value_type genKVPair(TiKVKey && key, const String & raw_key, TiKVValue && value)
static Map::value_type genKVPair(TiKVKey && key, const DecodedTiKVKey & raw_key, TiKVValue && value)
{
HandleID handle_id = RecordKVFormat::getHandle(raw_key);
auto decoded_val = RecordKVFormat::decodeLockCfValue(value);
8 changes: 4 additions & 4 deletions dbms/src/Storages/Transaction/RegionData.cpp
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@
namespace DB
{

TableID RegionData::insert(ColumnFamilyType cf, TiKVKey && key, const String & raw_key, TiKVValue && value)
TableID RegionData::insert(ColumnFamilyType cf, TiKVKey && key, const DecodedTiKVKey & raw_key, TiKVValue && value)
{
switch (cf)
{
@@ -33,20 +33,20 @@ TableID RegionData::insert(ColumnFamilyType cf, TiKVKey && key, const String & r
}
}

void RegionData::removeLockCF(const TableID & table_id, const String & raw_key)
void RegionData::removeLockCF(const TableID & table_id, const DecodedTiKVKey & raw_key)
{
HandleID handle_id = RecordKVFormat::getHandle(raw_key);
lock_cf.remove(table_id, handle_id);
}

void RegionData::removeDefaultCF(const TableID & table_id, const TiKVKey & key, const String & raw_key)
void RegionData::removeDefaultCF(const TableID & table_id, const TiKVKey & key, const DecodedTiKVKey & raw_key)
{
HandleID handle_id = RecordKVFormat::getHandle(raw_key);
Timestamp ts = RecordKVFormat::getTs(key);
cf_data_size -= default_cf.remove(table_id, RegionDefaultCFData::Key{handle_id, ts}, true);
}

void RegionData::removeWriteCF(const TableID & table_id, const TiKVKey & key, const String & raw_key)
void RegionData::removeWriteCF(const TableID & table_id, const TiKVKey & key, const DecodedTiKVKey & raw_key)
{
HandleID handle_id = RecordKVFormat::getHandle(raw_key);
Timestamp ts = RecordKVFormat::getTs(key);
8 changes: 4 additions & 4 deletions dbms/src/Storages/Transaction/RegionData.h
Original file line number Diff line number Diff line change
@@ -25,11 +25,11 @@ class RegionData
using WriteCFIter = RegionWriteCFData::Map::iterator;
using ConstWriteCFIter = RegionWriteCFData::Map::const_iterator;

TableID insert(ColumnFamilyType cf, TiKVKey && key, const String & raw_key, TiKVValue && value);
TableID insert(ColumnFamilyType cf, TiKVKey && key, const DecodedTiKVKey & raw_key, TiKVValue && value);

void removeLockCF(const TableID & table_id, const String & raw_key);
void removeDefaultCF(const TableID & table_id, const TiKVKey & key, const String & raw_key);
void removeWriteCF(const TableID & table_id, const TiKVKey & key, const String & raw_key);
void removeLockCF(const TableID & table_id, const DecodedTiKVKey & raw_key);
void removeDefaultCF(const TableID & table_id, const TiKVKey & key, const DecodedTiKVKey & raw_key);
void removeWriteCF(const TableID & table_id, const TiKVKey & key, const DecodedTiKVKey & raw_key);

WriteCFIter removeDataByWriteIt(const TableID & table_id, const WriteCFIter & write_it);

2 changes: 1 addition & 1 deletion dbms/src/Storages/Transaction/RegionHelper.hpp
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@ namespace DB
{

/// Ignoring all keys other than records.
inline TableID checkRecordAndValidTable(const std::string & raw_key)
inline TableID checkRecordAndValidTable(const DecodedTiKVKey & raw_key)
{
// Ignoring all keys other than records.
if (!RecordKVFormat::isRecord(raw_key))
18 changes: 18 additions & 0 deletions dbms/src/Storages/Transaction/TableRowIDMinMax.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include <Storages/Transaction/TableRowIDMinMax.h>

namespace DB
{

std::unordered_map<TableID, TableRowIDMinMax> TableRowIDMinMax::data;
std::mutex TableRowIDMinMax::mutex;

const TableRowIDMinMax & TableRowIDMinMax::getMinMax(const TableID table_id)
{
std::lock_guard<std::mutex> lock(mutex);

if (auto it = data.find(table_id); it != data.end())
return it->second;
return data.try_emplace(table_id, table_id).first->second;
}

} // namespace DB
31 changes: 31 additions & 0 deletions dbms/src/Storages/Transaction/TableRowIDMinMax.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#pragma once

#include <Storages/Transaction/TiKVRecordFormat.h>

namespace DB
{

/// TableRowIDMinMax is used to store the min/max key for specific table.
struct TableRowIDMinMax
{
TableRowIDMinMax(const TableID table_id)
: handle_min(RecordKVFormat::genRawKey(table_id, std::numeric_limits<HandleID>::min())),
handle_max(RecordKVFormat::genRawKey(table_id, std::numeric_limits<HandleID>::max()))
{}

/// Make this struct can't be copied or moved.
TableRowIDMinMax(const TableRowIDMinMax &) = delete;
TableRowIDMinMax(TableRowIDMinMax &&) = delete;

const DecodedTiKVKey handle_min;
const DecodedTiKVKey handle_max;

/// It's a long lived object, so just return const ref directly.
static const TableRowIDMinMax & getMinMax(const TableID table_id);

private:
static std::unordered_map<TableID, TableRowIDMinMax> data;
static std::mutex mutex;
};

} // namespace DB
22 changes: 14 additions & 8 deletions dbms/src/Storages/Transaction/TiKVKeyValue.h
Original file line number Diff line number Diff line change
@@ -19,22 +19,18 @@ struct StringObject : std::string
};

StringObject() = default;
explicit StringObject(Base && str_) : Base(std::move(str_)) {}
StringObject(Base && str_) : Base(std::move(str_)) {}
StringObject(StringObject && obj) : Base((Base &&) obj) {}
StringObject(const char * str, const size_t len) : Base(str, len) {}

StringObject(const char * str) : Base(str) {}
static StringObject copyFrom(const Base & str) { return StringObject(str); }

StringObject & operator=(const StringObject & a)
StringObject & operator=(const StringObject & a) = delete;
StringObject & operator=(StringObject && a)
{
if (this == &a)
return *this;

(Base &)* this = (const Base &)a;
return *this;
}
StringObject & operator=(StringObject && a)
{
(Base &)* this = (Base &&) a;
return *this;
}
@@ -79,4 +75,14 @@ using TiKVKey = StringObject<true>;
using TiKVValue = StringObject<false>;
using TiKVKeyValue = std::pair<TiKVKey, TiKVValue>;

struct DecodedTiKVKey : std::string
{
using Base = std::string;
DecodedTiKVKey(Base && str_) : Base(std::move(str_)) {}
DecodedTiKVKey() = delete;
DecodedTiKVKey(const char * str) : Base(str) {}
};

static_assert(sizeof(DecodedTiKVKey) == sizeof(std::string));

} // namespace DB
51 changes: 25 additions & 26 deletions dbms/src/Storages/Transaction/TiKVRange.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <Storages/Transaction/TableRowIDMinMax.h>
#include <Storages/Transaction/TiKVHandle.h>
#include <Storages/Transaction/TiKVKeyValue.h>
#include <Storages/Transaction/TiKVRecordFormat.h>
@@ -13,40 +14,21 @@ namespace TiKVRange

using Handle = TiKVHandle::Handle<HandleID>;

template <bool start, bool decoded = false, typename KeyType = TiKVKey>
inline Handle getRangeHandle(const KeyType & tikv_key, const TableID table_id)
template <bool start>
inline Handle getRangeHandle(const DecodedTiKVKey & key, const TableID table_id)
{
if constexpr (decoded)
static_assert(std::is_same_v<KeyType, std::string>);
else
static_assert(std::is_same_v<KeyType, TiKVKey>);

constexpr HandleID min = std::numeric_limits<HandleID>::min();
constexpr HandleID max = std::numeric_limits<HandleID>::max();

if (tikv_key.empty())
if (key.empty())
{
if constexpr (start)
return Handle::normal_min;
else
return Handle::max;
}

const std::string * raw_key_ptr = nullptr;
std::string decoded_raw_key;
if constexpr (decoded)
raw_key_ptr = &tikv_key;
else
{
decoded_raw_key = RecordKVFormat::decodeTiKVKey(tikv_key);
raw_key_ptr = &decoded_raw_key;
}

const std::string & key = *raw_key_ptr;

if (key <= RecordKVFormat::genRawKey(table_id, min))
const auto & table_min_max = TableRowIDMinMax::getMinMax(table_id);
if (key <= table_min_max.handle_min)
return Handle::normal_min;
if (key > RecordKVFormat::genRawKey(table_id, max))
if (key > table_min_max.handle_max)
return Handle::max;

if (likely(key.size() == RecordKVFormat::RAW_KEY_SIZE))
@@ -73,8 +55,25 @@ inline Handle getRangeHandle(const KeyType & tikv_key, const TableID table_id)
}
}

inline HandleRange<HandleID> getHandleRangeByTable(const TiKVKey & start_key, const TiKVKey & end_key, TableID table_id)
template <bool start>
inline Handle getRangeHandle(const TiKVKey & tikv_key, const TableID table_id)
{
if (tikv_key.empty())
{
if constexpr (start)
return Handle::normal_min;
else
return Handle::max;
}

return getRangeHandle<start>(RecordKVFormat::decodeTiKVKey(tikv_key), table_id);
}

template <typename KeyType>
inline HandleRange<HandleID> getHandleRangeByTable(const KeyType & start_key, const KeyType & end_key, const TableID table_id)
{
static_assert(std::is_same_v<KeyType, DecodedTiKVKey> || std::is_same_v<KeyType, TiKVKey>);

auto start_handle = getRangeHandle<true>(start_key, table_id);
auto end_handle = getRangeHandle<false>(end_key, table_id);

16 changes: 8 additions & 8 deletions dbms/src/Storages/Transaction/TiKVRecordFormat.h
Original file line number Diff line number Diff line change
@@ -78,16 +78,16 @@ inline T read(const char * s)
return *(reinterpret_cast<const T *>(s));
}

inline String genRawKey(const TableID tableId, const HandleID handleId)
inline DecodedTiKVKey genRawKey(const TableID tableId, const HandleID handleId)
{
String key(RecordKVFormat::RAW_KEY_SIZE, 0);
std::string key(RecordKVFormat::RAW_KEY_SIZE, 0);
memcpy(key.data(), &RecordKVFormat::TABLE_PREFIX, 1);
auto big_endian_table_id = encodeInt64(tableId);
memcpy(key.data() + 1, reinterpret_cast<const char *>(&big_endian_table_id), 8);
memcpy(key.data() + 1 + 8, RecordKVFormat::RECORD_PREFIX_SEP, 2);
auto big_endian_handle_id = encodeInt64(handleId);
memcpy(key.data() + RAW_KEY_NO_HANDLE_SIZE, reinterpret_cast<const char *>(&big_endian_handle_id), 8);
return key;
return std::move(key);
}

inline TiKVKey genKey(const TableID tableId, const HandleID handleId) { return encodeAsTiKVKey(genRawKey(tableId, handleId)); }
@@ -98,7 +98,7 @@ inline bool checkKeyPaddingValid(const char * ptr, const UInt8 pad_size)
return p == 0;
}

inline std::tuple<std::string, size_t> decodeTiKVKeyFull(const TiKVKey & key)
inline std::tuple<DecodedTiKVKey, size_t> decodeTiKVKeyFull(const TiKVKey & key)
{
const size_t chunk_len = ENC_GROUP_SIZE + 1;
std::string res;
@@ -126,19 +126,19 @@ inline std::tuple<std::string, size_t> decodeTiKVKeyFull(const TiKVKey & key)
}
}

inline String 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 TableID getTableId(const String & 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 String & 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 TableID getTableId(const TiKVKey & key) { return getTableId(decodeTiKVKey(key)); }

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

inline bool isRecord(const String & raw_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;
}
1 change: 1 addition & 0 deletions dbms/src/Storages/Transaction/tests/kvstore.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <ext/scope_guard.h>

#include <Storages/Transaction/KVStore.h>
#include <Storages/Transaction/RaftCommandResult.h>
#include <Storages/Transaction/applySnapshot.h>
#include <Storages/Transaction/Region.h>
#include <Storages/Transaction/TiKVRecordFormat.h>
26 changes: 13 additions & 13 deletions dbms/src/Storages/Transaction/tests/region_persister.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <Storages/Transaction/Region.h>
#include <Storages/Transaction/TiKVRecordFormat.h>
#include <Storages/Transaction/RegionManager.h>
#include <Storages/Transaction/TiKVRecordFormat.h>
#include <ext/scope_guard.h>

#include "region_helper.h"
@@ -64,9 +64,9 @@ int main(int, char **)
{
auto region = std::make_shared<Region>(createRegionMeta(100));
TiKVKey key = RecordKVFormat::genKey(100, 323, 9983);
region->insert("default", key, TiKVValue("value1"));
region->insert("write", key, RecordKVFormat::encodeWriteCfValue('P', 0));
region->insert("lock", key, RecordKVFormat::encodeLockCfValue('P', "", 0, 0));
region->insert("default", TiKVKey::copyFrom(key), TiKVValue("value1"));
region->insert("write", TiKVKey::copyFrom(key), RecordKVFormat::encodeWriteCfValue('P', 0));
region->insert("lock", TiKVKey::copyFrom(key), RecordKVFormat::encodeLockCfValue('P', "", 0, 0));

auto path = dir_path + "region.test";
WriteBufferFromFile write_buf(path, DBMS_DEFAULT_BUFFER_SIZE, O_WRONLY | O_CREAT);
@@ -125,9 +125,9 @@ int main(int, char **)
}

TiKVKey key = RecordKVFormat::genKey(100, 323, 9983);
region->insert("default", key, TiKVValue("value1"));
region->insert("write", key, RecordKVFormat::encodeWriteCfValue('P', 0));
region->insert("lock", key, RecordKVFormat::encodeLockCfValue('P', "", 0, 0));
region->insert("default", TiKVKey::copyFrom(key), TiKVValue("value1"));
region->insert("write", TiKVKey::copyFrom(key), RecordKVFormat::encodeWriteCfValue('P', 0));
region->insert("lock", TiKVKey::copyFrom(key), RecordKVFormat::encodeLockCfValue('P', "", 0, 0));

auto path = dir_path + "region_state.test";
WriteBufferFromFile write_buf(path, DBMS_DEFAULT_BUFFER_SIZE, O_WRONLY | O_CREAT);
@@ -166,9 +166,9 @@ int main(int, char **)
{
auto region = std::make_shared<Region>(createRegionMeta(i));
TiKVKey key = RecordKVFormat::genKey(100, i, diff++);
region->insert("default", key, TiKVValue("value1"));
region->insert("write", key, RecordKVFormat::encodeWriteCfValue('P', 0));
region->insert("lock", key, RecordKVFormat::encodeLockCfValue('P', "", 0, 0));
region->insert("default", TiKVKey::copyFrom(key), TiKVValue("value1"));
region->insert("write", TiKVKey::copyFrom(key), RecordKVFormat::encodeWriteCfValue('P', 0));
region->insert("lock", TiKVKey::copyFrom(key), RecordKVFormat::encodeLockCfValue('P', "", 0, 0));

persister.persist(*region);

@@ -216,9 +216,9 @@ int main(int, char **)
{
auto region = std::make_shared<Region>(createRegionMeta(i));
TiKVKey key = RecordKVFormat::genKey(100, i, diff++);
region->insert("default", key, TiKVValue("value1"));
region->insert("write", key, RecordKVFormat::encodeWriteCfValue('P', 0));
region->insert("lock", key, RecordKVFormat::encodeLockCfValue('P', "", 0, 0));
region->insert("default", TiKVKey::copyFrom(key), TiKVValue("value1"));
region->insert("write", TiKVKey::copyFrom(key), RecordKVFormat::encodeWriteCfValue('P', 0));
region->insert("lock", TiKVKey::copyFrom(key), RecordKVFormat::encodeLockCfValue('P', "", 0, 0));

persister.persist(*region);