Options helper supports k, m, g, and t unit suffixes

Summary:
Add unit support in options helper so we can specify, e.g., 10m for
10 megabytes.

Test Plan: Updated options_test

Reviewers: sdong, igor, ljin

Reviewed By: ljin

Subscribers: dhruba

Differential Revision: https://reviews.facebook.net/D28977
This commit is contained in:
Jonah Cohen 2014-11-17 13:47:51 -08:00
parent c46c2be8d6
commit 517c28994d
2 changed files with 64 additions and 6 deletions

View file

@ -43,20 +43,56 @@ bool ParseBoolean(const std::string& type, const std::string& value) {
throw type; throw type;
} }
} }
int ParseInt(const std::string& value) { return std::stoi(value); }
uint32_t ParseUint32(const std::string& value) {
return static_cast<uint32_t>(std::stoul(value));
}
uint64_t ParseUint64(const std::string& value) { uint64_t ParseUint64(const std::string& value) {
return std::stoull(value); size_t endchar;
uint64_t num = std::stoull(value.c_str(), &endchar);
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;
} }
size_t ParseSizeT(const std::string& value) { size_t ParseSizeT(const std::string& value) {
return static_cast<size_t>(ParseUint64(value)); return static_cast<size_t>(ParseUint64(value));
} }
uint32_t ParseUint32(const std::string& value) {
uint64_t num = ParseUint64(value);
if ((num >> 32LL) == 0) {
return static_cast<uint32_t>(num);
} else {
throw std::out_of_range(value);
}
}
int ParseInt(const std::string& value) {
size_t endchar;
int num = std::stoi(value.c_str(), &endchar);
if (endchar < value.length()) {
char c = value[endchar];
if (c == 'k' || c == 'K')
num <<= 10;
else if (c == 'm' || c == 'M')
num <<= 20;
else if (c == 'g' || c == 'G')
num <<= 30;
}
return num;
}
double ParseDouble(const std::string& value) { double ParseDouble(const std::string& value) {
return std::stod(value); return std::stod(value);
} }

View file

@ -298,6 +298,28 @@ TEST(OptionsTest, GetOptionsFromStringTest) {
// Missing option name // Missing option name
ASSERT_TRUE(!GetColumnFamilyOptionsFromString(base_cf_opt, ASSERT_TRUE(!GetColumnFamilyOptionsFromString(base_cf_opt,
"write_buffer_size=13; =100;", &new_cf_opt)); "write_buffer_size=13; =100;", &new_cf_opt));
// Units (k)
ASSERT_TRUE(GetColumnFamilyOptionsFromString(base_cf_opt,
"memtable_prefix_bloom_bits=14k;max_write_buffer_number=-15K",
&new_cf_opt));
ASSERT_EQ(new_cf_opt.memtable_prefix_bloom_bits, 14*1024);
ASSERT_EQ(new_cf_opt.max_write_buffer_number, -15*1024);
// Units (m)
ASSERT_TRUE(GetColumnFamilyOptionsFromString(base_cf_opt,
"max_write_buffer_number=16m;inplace_update_num_locks=17M",
&new_cf_opt));
ASSERT_EQ(new_cf_opt.max_write_buffer_number, 16*1024*1024);
ASSERT_EQ(new_cf_opt.inplace_update_num_locks, 17*1024*1024);
// Units (g)
ASSERT_TRUE(GetColumnFamilyOptionsFromString(base_cf_opt,
"write_buffer_size=18g;arena_block_size=19G", &new_cf_opt));
ASSERT_EQ(new_cf_opt.write_buffer_size, 18*1024LL*1024LL*1024LL);
ASSERT_EQ(new_cf_opt.arena_block_size, 19*1024LL*1024LL*1024LL);
// Units (t)
ASSERT_TRUE(GetColumnFamilyOptionsFromString(base_cf_opt,
"write_buffer_size=20t;arena_block_size=21T", &new_cf_opt));
ASSERT_EQ(new_cf_opt.write_buffer_size, 20*1024LL*1024LL*1024LL*1024LL);
ASSERT_EQ(new_cf_opt.arena_block_size, 21*1024LL*1024LL*1024LL*1024LL);
} }
} // namespace rocksdb } // namespace rocksdb