From 4c696ed0018800b62e2448a4ead438255140fc25 Mon Sep 17 00:00:00 2001 From: amayank Date: Fri, 15 Feb 2013 15:28:24 -0800 Subject: [PATCH] Fix for the weird behaviour encountered by ldb Get where it could read only the second-latest value Summary: flush_on_destroy has a default value of false and the memtable is flushed in the dbimpl-destructor only when that is set to true. Because we want the memtable to be flushed everytime that the destructor is called(db is closed) and the cases where we work with the memtable only are very less it is a good idea to give this a default value of true. Thus the put from ldb wil have its data flushed to disk in the destructor and the next Get will be able to read it when opened with OpenForReadOnly. The reason that ldb could read the latest value when the db was opened in the normal Open mode is that the Get from normal Open first reads the memtable and directly finds the latest value written there and the Get from OpenForReadOnly doesn't have access to the memtable (which is correct because all its Put/Modify) are disabled Test Plan: make all; ldb put and get and scans Reviewers: dhruba, heyongqiang, sheki Reviewed By: heyongqiang CC: kosievdmerwe, zshao, dilipj, kailiu Differential Revision: https://reviews.facebook.net/D8631 --- db/db_impl.cc | 2 +- db/db_impl_readonly.cc | 4 +++- util/ldb_cmd.h | 4 +--- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/db/db_impl.cc b/db/db_impl.cc index a543534c71..846122f60f 100644 --- a/db/db_impl.cc +++ b/db/db_impl.cc @@ -160,7 +160,7 @@ DBImpl::DBImpl(const Options& options, const std::string& dbname) stall_level0_num_files_(0), stall_leveln_slowdown_(0), started_at_(options.env->NowMicros()), - flush_on_destroy_(false), + flush_on_destroy_(true), delayed_writes_(0) { mem_->Ref(); diff --git a/db/db_impl_readonly.cc b/db/db_impl_readonly.cc index 72889215de..74809a4e70 100644 --- a/db/db_impl_readonly.cc +++ b/db/db_impl_readonly.cc @@ -65,7 +65,9 @@ Iterator* DBImplReadOnly::NewIterator(const ReadOptions& options) { NewMergingIterator(&internal_comparator_, &list[0], list.size()); return NewDBIterator( &dbname_, env_, user_comparator(), internal_iter, - reinterpret_cast(options.snapshot)->number_); + (options.snapshot != NULL + ? reinterpret_cast(options.snapshot)->number_ + : versions_->LastSequence())); } diff --git a/util/ldb_cmd.h b/util/ldb_cmd.h index aac9905afc..63d22f9139 100644 --- a/util/ldb_cmd.h +++ b/util/ldb_cmd.h @@ -190,9 +190,7 @@ protected: // Open the DB. leveldb::Status st; if (is_read_only_) { - //st = leveldb::DB::OpenForReadOnly(opt, db_path_, &db_); - // Could not get this to work - st = leveldb::DB::Open(opt, db_path_, &db_); + st = leveldb::DB::OpenForReadOnly(opt, db_path_, &db_); } else { st = leveldb::DB::Open(opt, db_path_, &db_); }