#include "util/coding.h"
#include "util/compression.h"
#include "util/crc32c.h"
+#include "util/defer.h"
#include "util/mutexlock.h"
#include "util/stop_watch.h"
#include "util/string_util.h"
}
// If timestamp is used, we use read callback to ensure <key,t,s> is returned
// only if t <= read_opts.timestamp and s <= snapshot.
+ // HACK: temporarily overwrite input struct field but restore
+ SaveAndRestore<ReadCallback*> restore_callback(&get_impl_options.callback);
if (ts_sz > 0) {
assert(!get_impl_options
.callback); // timestamp with callback is not supported
// lambda passed to Defer, and you can return immediately on failure when necessary.
class Defer final {
public:
- Defer(std::function<void()>&& fn) : fn_(std::move(fn)) {}
+ explicit Defer(std::function<void()>&& fn) : fn_(std::move(fn)) {}
~Defer() { fn_(); }
// Disallow copy.
std::function<void()> fn_;
};
+// An RAII utility object that saves the current value of an object so that
+// it can be overwritten, and restores it to the saved value when the
+// SaveAndRestore object goes out of scope.
+template <typename T>
+class SaveAndRestore {
+ public:
+ // obj is non-null pointer to value to be saved and later restored.
+ explicit SaveAndRestore(T* obj) : obj_(obj), saved_(*obj) {}
+ ~SaveAndRestore() { *obj_ = std::move(saved_); }
+
+ // No copies
+ SaveAndRestore(const SaveAndRestore&) = delete;
+ SaveAndRestore& operator=(const SaveAndRestore&) = delete;
+
+ private:
+ T* const obj_;
+ T saved_;
+};
+
} // namespace ROCKSDB_NAMESPACE
ASSERT_EQ(4, v);
}
+TEST(SaveAndRestoreTest, BlockScope) {
+ int v = 1;
+ {
+ SaveAndRestore<int> sr(&v);
+ ASSERT_EQ(v, 1);
+ v = 2;
+ ASSERT_EQ(v, 2);
+ }
+ ASSERT_EQ(v, 1);
+}
+
} // namespace ROCKSDB_NAMESPACE
int main(int argc, char** argv) {