add OptionType kInt32T and kInt64T

Summary: Pull Request resolved: https://github.com/facebook/rocksdb/pull/5061

Differential Revision: D14418581

Pulled By: miasantreble

fbshipit-source-id: be7f90e16586666ddd0cce36971e403782ab0892
This commit is contained in:
Zhongyi Xie 2019-03-12 13:46:12 -07:00 committed by Facebook Github Bot
parent 5a5c0492db
commit fdc72a5c5d
8 changed files with 87 additions and 0 deletions

View File

@ -454,6 +454,12 @@ bool ParseOptionHelper(char* opt_address, const OptionType& opt_type,
case OptionType::kInt: case OptionType::kInt:
*reinterpret_cast<int*>(opt_address) = ParseInt(value); *reinterpret_cast<int*>(opt_address) = ParseInt(value);
break; break;
case OptionType::kInt32T:
*reinterpret_cast<int32_t*>(opt_address) = ParseInt32(value);
break;
case OptionType::kInt64T:
PutUnaligned(reinterpret_cast<int64_t*>(opt_address), ParseInt64(value));
break;
case OptionType::kVectorInt: case OptionType::kVectorInt:
*reinterpret_cast<std::vector<int>*>(opt_address) = ParseVectorInt(value); *reinterpret_cast<std::vector<int>*>(opt_address) = ParseVectorInt(value);
break; break;
@ -563,6 +569,16 @@ bool SerializeSingleOptionHelper(const char* opt_address,
case OptionType::kInt: case OptionType::kInt:
*value = ToString(*(reinterpret_cast<const int*>(opt_address))); *value = ToString(*(reinterpret_cast<const int*>(opt_address)));
break; break;
case OptionType::kInt32T:
*value = ToString(*(reinterpret_cast<const int32_t*>(opt_address)));
break;
case OptionType::kInt64T:
{
int64_t v;
GetUnaligned(reinterpret_cast<const int64_t*>(opt_address), &v);
*value = ToString(v);
}
break;
case OptionType::kVectorInt: case OptionType::kVectorInt:
return SerializeIntVector( return SerializeIntVector(
*reinterpret_cast<const std::vector<int>*>(opt_address), value); *reinterpret_cast<const std::vector<int>*>(opt_address), value);

View File

@ -47,6 +47,8 @@ Status GetTableFactoryFromMap(
enum class OptionType { enum class OptionType {
kBoolean, kBoolean,
kInt, kInt,
kInt32T,
kInt64T,
kVectorInt, kVectorInt,
kUInt, kUInt,
kUInt32T, kUInt32T,

View File

@ -500,6 +500,16 @@ bool AreEqualOptions(
case OptionType::kInt: case OptionType::kInt:
return (*reinterpret_cast<const int*>(offset1) == return (*reinterpret_cast<const int*>(offset1) ==
*reinterpret_cast<const int*>(offset2)); *reinterpret_cast<const int*>(offset2));
case OptionType::kInt32T:
return (*reinterpret_cast<const int32_t*>(offset1) ==
*reinterpret_cast<const int32_t*>(offset2));
case OptionType::kInt64T:
{
int64_t v1, v2;
GetUnaligned(reinterpret_cast<const int64_t*>(offset1), &v1);
GetUnaligned(reinterpret_cast<const int64_t*>(offset2), &v2);
return (v1 == v2);
}
case OptionType::kVectorInt: case OptionType::kVectorInt:
return (*reinterpret_cast<const std::vector<int>*>(offset1) == return (*reinterpret_cast<const std::vector<int>*>(offset1) ==
*reinterpret_cast<const std::vector<int>*>(offset2)); *reinterpret_cast<const std::vector<int>*>(offset2));

View File

@ -21,6 +21,7 @@
#include "options/options_helper.h" #include "options/options_helper.h"
#include "options/options_parser.h" #include "options/options_parser.h"
#include "options/options_sanity_check.h" #include "options/options_sanity_check.h"
#include "port/port.h"
#include "rocksdb/cache.h" #include "rocksdb/cache.h"
#include "rocksdb/convenience.h" #include "rocksdb/convenience.h"
#include "rocksdb/memtablerep.h" #include "rocksdb/memtablerep.h"
@ -1813,6 +1814,18 @@ bool IsEscapedString(const std::string& str) {
} }
} // namespace } // namespace
TEST_F(OptionsParserTest, IntegerParsing) {
ASSERT_EQ(ParseUint64("18446744073709551615"), 18446744073709551615U);
ASSERT_EQ(ParseUint32("4294967295"), 4294967295U);
ASSERT_EQ(ParseSizeT("18446744073709551615"), 18446744073709551615U);
ASSERT_EQ(ParseInt64("9223372036854775807"), 9223372036854775807U);
ASSERT_EQ(ParseInt64("-9223372036854775808"), port::kMinInt64);
ASSERT_EQ(ParseInt32("2147483647"), 2147483647U);
ASSERT_EQ(ParseInt32("-2147483648"), port::kMinInt32);
ASSERT_EQ(ParseInt("-32767"), -32767);
ASSERT_EQ(ParseDouble("-1.234567"), -1.234567);
}
TEST_F(OptionsParserTest, EscapeOptionString) { TEST_F(OptionsParserTest, EscapeOptionString) {
ASSERT_EQ(UnescapeOptionString( ASSERT_EQ(UnescapeOptionString(
"This is a test string with \\# \\: and \\\\ escape chars."), "This is a test string with \\# \\: and \\\\ escape chars."),

View File

@ -87,8 +87,10 @@ namespace port {
// For use at db/file_indexer.h kLevelMaxIndex // For use at db/file_indexer.h kLevelMaxIndex
const uint32_t kMaxUint32 = std::numeric_limits<uint32_t>::max(); const uint32_t kMaxUint32 = std::numeric_limits<uint32_t>::max();
const int kMaxInt32 = std::numeric_limits<int32_t>::max(); const int kMaxInt32 = std::numeric_limits<int32_t>::max();
const int kMinInt32 = std::numeric_limits<int32_t>::min();
const uint64_t kMaxUint64 = std::numeric_limits<uint64_t>::max(); const uint64_t kMaxUint64 = std::numeric_limits<uint64_t>::max();
const int64_t kMaxInt64 = std::numeric_limits<int64_t>::max(); const int64_t kMaxInt64 = std::numeric_limits<int64_t>::max();
const int64_t kMinInt64 = std::numeric_limits<int64_t>::min();
const size_t kMaxSizet = std::numeric_limits<size_t>::max(); const size_t kMaxSizet = std::numeric_limits<size_t>::max();
static const bool kLittleEndian = PLATFORM_IS_LITTLE_ENDIAN; static const bool kLittleEndian = PLATFORM_IS_LITTLE_ENDIAN;

View File

@ -93,7 +93,9 @@ namespace port {
// For use at db/file_indexer.h kLevelMaxIndex // For use at db/file_indexer.h kLevelMaxIndex
const uint32_t kMaxUint32 = UINT32_MAX; const uint32_t kMaxUint32 = UINT32_MAX;
const int kMaxInt32 = INT32_MAX; const int kMaxInt32 = INT32_MAX;
const int kMinInt32 = INT32_MIN;
const int64_t kMaxInt64 = INT64_MAX; const int64_t kMaxInt64 = INT64_MAX;
const int64_t kMinInt64 = INT64_MIN;
const uint64_t kMaxUint64 = UINT64_MAX; const uint64_t kMaxUint64 = UINT64_MAX;
#ifdef _WIN64 #ifdef _WIN64
@ -109,8 +111,10 @@ const size_t kMaxSizet = UINT_MAX;
// For use at db/file_indexer.h kLevelMaxIndex // For use at db/file_indexer.h kLevelMaxIndex
const uint32_t kMaxUint32 = std::numeric_limits<uint32_t>::max(); const uint32_t kMaxUint32 = std::numeric_limits<uint32_t>::max();
const int kMaxInt32 = std::numeric_limits<int>::max(); const int kMaxInt32 = std::numeric_limits<int>::max();
const int kMinInt32 = std::numeric_limits<int>::min();
const uint64_t kMaxUint64 = std::numeric_limits<uint64_t>::max(); const uint64_t kMaxUint64 = std::numeric_limits<uint64_t>::max();
const int64_t kMaxInt64 = std::numeric_limits<int64_t>::max(); const int64_t kMaxInt64 = std::numeric_limits<int64_t>::max();
const int64_t kMinInt64 = std::numeric_limits<int64_t>::min();
const size_t kMaxSizet = std::numeric_limits<size_t>::max(); const size_t kMaxSizet = std::numeric_limits<size_t>::max();

View File

@ -21,6 +21,7 @@
#include <utility> #include <utility>
#include <vector> #include <vector>
#include "rocksdb/env.h" #include "rocksdb/env.h"
#include "port/port.h"
#include "rocksdb/slice.h" #include "rocksdb/slice.h"
namespace rocksdb { namespace rocksdb {
@ -276,6 +277,15 @@ uint32_t ParseUint32(const std::string& value) {
} }
} }
int32_t ParseInt32(const std::string& value) {
int64_t num = ParseInt64(value);
if (num <= port::kMaxInt32 && num >= port::kMinInt32) {
return static_cast<int32_t>(num);
} else {
throw std::out_of_range(value);
}
}
#endif #endif
uint64_t ParseUint64(const std::string& value) { uint64_t ParseUint64(const std::string& value) {
@ -303,6 +313,31 @@ uint64_t ParseUint64(const std::string& value) {
return num; return num;
} }
int64_t ParseInt64(const std::string& value) {
size_t endchar;
#ifndef CYGWIN
int64_t num = std::stoll(value.c_str(), &endchar);
#else
char* endptr;
int64_t num = std::strtoll(value.c_str(), &endptr, 0);
endchar = endptr - value.c_str();
#endif
if (endchar < value.length()) {
char c = value[endchar];
if (c == 'k' || c == 'K')
num <<= 10LL;
else if (c == 'm' || c == 'M')
num <<= 20LL;
else if (c == 'g' || c == 'G')
num <<= 30LL;
else if (c == 't' || c == 'T')
num <<= 40LL;
}
return num;
}
int ParseInt(const std::string& value) { int ParseInt(const std::string& value) {
size_t endchar; size_t endchar;
#ifndef CYGWIN #ifndef CYGWIN

View File

@ -109,12 +109,17 @@ std::string trim(const std::string& str);
bool ParseBoolean(const std::string& type, const std::string& value); bool ParseBoolean(const std::string& type, const std::string& value);
uint32_t ParseUint32(const std::string& value); uint32_t ParseUint32(const std::string& value);
int32_t ParseInt32(const std::string& value);
#endif #endif
uint64_t ParseUint64(const std::string& value); uint64_t ParseUint64(const std::string& value);
int ParseInt(const std::string& value); int ParseInt(const std::string& value);
int64_t ParseInt64(const std::string& value);
double ParseDouble(const std::string& value); double ParseDouble(const std::string& value);
size_t ParseSizeT(const std::string& value); size_t ParseSizeT(const std::string& value);