Avoid type unique_ptr in LogWriterNumber::writer for Windows build break

Summary:
Visual Studio complains about deque<LogWriterNumber> because LogWriterNumber is non-copyable for its unique_ptr member writer. Move away from it, and do explit free.
It is less safe but I can't think of a better way to unblock it.

Test Plan: valgrind check test

Reviewers: anthony, IslamAbdelRahman, kolmike, rven, yhchiang

Reviewed By: yhchiang

Subscribers: leveldb, dhruba

Differential Revision: https://reviews.facebook.net/D43647
This commit is contained in:
sdong 2015-08-05 20:51:27 -07:00
parent d7314ba759
commit 6a4aaadcd7
2 changed files with 26 additions and 11 deletions

View File

@ -369,6 +369,9 @@ DBImpl::~DBImpl() {
for (auto l : logs_to_free_) { for (auto l : logs_to_free_) {
delete l; delete l;
} }
for (auto& log : logs_) {
log.ClearWriter();
}
logs_.clear(); logs_.clear();
// versions need to be destroyed before table_cache since it can hold // versions need to be destroyed before table_cache since it can hold
@ -557,7 +560,7 @@ void DBImpl::FindObsoleteFiles(JobContext* job_context, bool force,
// logs_ could have changed while we were waiting. // logs_ could have changed while we were waiting.
continue; continue;
} }
logs_to_free_.push_back(log.writer.release()); logs_to_free_.push_back(log.ReleaseWriter());
logs_.pop_front(); logs_.pop_front();
} }
// Current log cannot be obsolete. // Current log cannot be obsolete.
@ -1978,7 +1981,7 @@ Status DBImpl::SyncWAL() {
auto& log = *it; auto& log = *it;
assert(!log.getting_synced); assert(!log.getting_synced);
log.getting_synced = true; log.getting_synced = true;
logs_to_sync.push_back(log.writer.get()); logs_to_sync.push_back(log.writer);
} }
need_log_dir_sync = !log_dir_synced_; need_log_dir_sync = !log_dir_synced_;
@ -2015,7 +2018,7 @@ void DBImpl::MarkLogsSynced(
auto& log = *it; auto& log = *it;
assert(log.getting_synced); assert(log.getting_synced);
if (status.ok() && logs_.size() > 1) { if (status.ok() && logs_.size() > 1) {
logs_to_free_.push_back(log.writer.release()); logs_to_free_.push_back(log.ReleaseWriter());
logs_.erase(it++); logs_.erase(it++);
} else { } else {
log.getting_synced = false; log.getting_synced = false;
@ -3805,7 +3808,7 @@ Status DBImpl::SwitchMemtable(ColumnFamilyData* cfd, WriteContext* context) {
assert(new_log != nullptr); assert(new_log != nullptr);
log_empty_ = true; log_empty_ = true;
log_dir_synced_ = false; log_dir_synced_ = false;
logs_.emplace_back(logfile_number_, std::unique_ptr<log::Writer>(new_log)); logs_.emplace_back(logfile_number_, new_log);
alive_log_files_.push_back(LogFileNumberSize(logfile_number_)); alive_log_files_.push_back(LogFileNumberSize(logfile_number_));
for (auto loop_cfd : *versions_->GetColumnFamilySet()) { for (auto loop_cfd : *versions_->GetColumnFamilySet()) {
// all this is just optimization to delete logs that // all this is just optimization to delete logs that
@ -4338,10 +4341,8 @@ Status DB::Open(const DBOptions& db_options, const std::string& dbname,
impl->logfile_number_ = new_log_number; impl->logfile_number_ = new_log_number;
unique_ptr<WritableFileWriter> file_writer( unique_ptr<WritableFileWriter> file_writer(
new WritableFileWriter(std::move(lfile), opt_env_options)); new WritableFileWriter(std::move(lfile), opt_env_options));
impl->logs_.emplace_back( impl->logs_.emplace_back(new_log_number,
new_log_number, new log::Writer(std::move(file_writer)));
std::unique_ptr<log::Writer>(
new log::Writer(std::move(file_writer))));
// set column family handles // set column family handles
for (auto cf : column_families) { for (auto cf : column_families) {

View File

@ -532,10 +532,24 @@ class DBImpl : public DB {
bool getting_flushed = false; bool getting_flushed = false;
}; };
struct LogWriterNumber { struct LogWriterNumber {
LogWriterNumber(uint64_t _number, std::unique_ptr<log::Writer> _writer) // pass ownership of _writer
: number(_number), writer(std::move(_writer)) {} LogWriterNumber(uint64_t _number, log::Writer* _writer)
: number(_number), writer(_writer) {}
log::Writer* ReleaseWriter() {
auto* w = writer;
writer = nullptr;
return w;
}
void ClearWriter() {
delete writer;
writer = nullptr;
}
uint64_t number; uint64_t number;
std::unique_ptr<log::Writer> writer; // Visual Studio doesn't support deque's member to be noncopyable because
// of a unique_ptr as a member.
log::Writer* writer; // own
// true for some prefix of logs_ // true for some prefix of logs_
bool getting_synced = false; bool getting_synced = false;
}; };