Changed the CompactionJobStats::output_key_prefix type from char[] to string.

Summary:
Keys in RocksDB can be arbitrary byte strings.  However, in the current
CompactionJobStats, smallest_output_key_prefix and largest_output_key_prefix
are of type char[] without having a length, which is insufficient to handle
non-null terminated strings.

This patch change their type to std::string.

Test Plan: compaction_job_stats_test

Reviewers: igor, rven, IslamAbdelRahman, kradhakrishnan, sdong

Reviewed By: sdong

Subscribers: dhruba, leveldb

Differential Revision: https://reviews.facebook.net/D39537
main
Yueh-Hsuan Chiang 10 years ago
parent 0b3172d071
commit bb808eaddb
  1. 21
      db/compaction_job.cc
  2. 21
      db/compaction_job_stats_test.cc
  3. 7
      include/rocksdb/compaction_job_stats.h

@ -1252,11 +1252,10 @@ void CompactionJob::CleanupCompaction(const Status& status) {
#ifndef ROCKSDB_LITE #ifndef ROCKSDB_LITE
namespace { namespace {
void CopyPrefix( void CopyPrefix(
char* dst, size_t dst_length, const Slice& src) { const Slice& src, size_t prefix_length, std::string* dst) {
assert(dst_length > 0); assert(prefix_length > 0);
size_t length = src.size() > dst_length - 1 ? dst_length - 1 : src.size(); size_t length = src.size() > prefix_length ? prefix_length : src.size();
memcpy(dst, src.data(), length); dst->assign(src.data(), length);
dst[length] = 0;
} }
} // namespace } // namespace
@ -1285,13 +1284,13 @@ void CompactionJob::UpdateCompactionJobStats(
if (compact_->outputs.size() > 0U) { if (compact_->outputs.size() > 0U) {
CopyPrefix( CopyPrefix(
compaction_job_stats_->smallest_output_key_prefix, compact_->outputs[0].smallest.user_key(),
sizeof(compaction_job_stats_->smallest_output_key_prefix), CompactionJobStats::kMaxPrefixLength,
compact_->outputs[0].smallest.user_key().ToString()); &compaction_job_stats_->smallest_output_key_prefix);
CopyPrefix( CopyPrefix(
compaction_job_stats_->largest_output_key_prefix, compact_->current_output()->largest.user_key(),
sizeof(compaction_job_stats_->largest_output_key_prefix), CompactionJobStats::kMaxPrefixLength,
compact_->current_output()->largest.user_key().ToString()); &compaction_job_stats_->largest_output_key_prefix);
} }
} }
#endif // !ROCKSDB_LITE #endif // !ROCKSDB_LITE

@ -457,11 +457,10 @@ uint64_t EstimatedFileSize(
namespace { namespace {
void CopyPrefix( void CopyPrefix(
char* dst, size_t dst_length, const Slice& src) { const Slice& src, size_t prefix_length, std::string* dst) {
assert(dst_length > 0); assert(prefix_length > 0);
size_t length = src.size() > dst_length - 1 ? dst_length - 1 : src.size(); size_t length = src.size() > prefix_length ? prefix_length : src.size();
memcpy(dst, src.data(), length); dst->assign(src.data(), length);
dst[length] = 0;
} }
} // namespace } // namespace
@ -499,12 +498,12 @@ CompactionJobStats NewManualCompactionJobStats(
stats.num_records_replaced = num_records_replaced; stats.num_records_replaced = num_records_replaced;
CopyPrefix(stats.smallest_output_key_prefix, CopyPrefix(smallest_key,
sizeof(stats.smallest_output_key_prefix), CompactionJobStats::kMaxPrefixLength,
smallest_key); &stats.smallest_output_key_prefix);
CopyPrefix(stats.largest_output_key_prefix, CopyPrefix(largest_key,
sizeof(stats.largest_output_key_prefix), CompactionJobStats::kMaxPrefixLength,
largest_key); &stats.largest_output_key_prefix);
return stats; return stats;
} }

@ -6,6 +6,7 @@
#pragma once #pragma once
#include <stddef.h> #include <stddef.h>
#include <stdint.h> #include <stdint.h>
#include <string>
namespace rocksdb { namespace rocksdb {
struct CompactionJobStats { struct CompactionJobStats {
@ -45,7 +46,9 @@ struct CompactionJobStats {
// 0-terminated strings storing the first 8 bytes of the smallest and // 0-terminated strings storing the first 8 bytes of the smallest and
// largest key in the output. // largest key in the output.
char smallest_output_key_prefix[9]; static const size_t kMaxPrefixLength = 8;
char largest_output_key_prefix[9];
std::string smallest_output_key_prefix;
std::string largest_output_key_prefix;
}; };
} // namespace rocksdb } // namespace rocksdb

Loading…
Cancel
Save