From 16ea1c7d1c121d9bdc2e841e02036e8ffd4bbb2b Mon Sep 17 00:00:00 2001 From: agiardullo Date: Thu, 6 Aug 2015 17:59:05 -0700 Subject: [PATCH] simple ManagedSnapshot wrapper Summary: Implemented this simple wrapper for something else I was working on. Seemed like it makes sense to expose it instead of burying it in some random code. Test Plan: added test Reviewers: rven, kradhakrishnan, sdong, yhchiang Reviewed By: sdong Subscribers: dhruba, leveldb Differential Revision: https://reviews.facebook.net/D43293 --- db/db_impl.h | 4 +-- db/db_test.cc | 32 +++++++++++---------- db/flush_job.h | 6 ++-- db/snapshot_impl.cc | 23 +++++++++++++++ db/{snapshot.h => snapshot_impl.h} | 0 db/write_batch.cc | 2 +- include/rocksdb/db.h | 13 +-------- include/rocksdb/snapshot.h | 45 ++++++++++++++++++++++++++++++ src.mk | 1 + 9 files changed, 93 insertions(+), 33 deletions(-) create mode 100644 db/snapshot_impl.cc rename db/{snapshot.h => snapshot_impl.h} (100%) create mode 100644 include/rocksdb/snapshot.h diff --git a/db/db_impl.h b/db/db_impl.h index 3dd95f8b2..d2f4b868d 100644 --- a/db/db_impl.h +++ b/db/db_impl.h @@ -19,11 +19,11 @@ #include #include "db/dbformat.h" -#include "db/log_writer.h" -#include "db/snapshot.h" #include "db/column_family.h" #include "db/compaction_job.h" #include "db/flush_job.h" +#include "db/log_writer.h" +#include "db/snapshot_impl.h" #include "db/version_edit.h" #include "db/wal_manager.h" #include "db/writebuffer.h" diff --git a/db/db_test.cc b/db/db_test.cc index 07a0b570e..f728314c2 100644 --- a/db/db_test.cc +++ b/db/db_test.cc @@ -39,11 +39,12 @@ #include "rocksdb/env.h" #include "rocksdb/experimental.h" #include "rocksdb/filter_policy.h" +#include "rocksdb/options.h" #include "rocksdb/perf_context.h" #include "rocksdb/slice.h" #include "rocksdb/slice_transform.h" +#include "rocksdb/snapshot.h" #include "rocksdb/table.h" -#include "rocksdb/options.h" #include "rocksdb/table_properties.h" #include "rocksdb/thread_status.h" #include "rocksdb/utilities/write_batch_with_index.h" @@ -3026,22 +3027,23 @@ TEST_F(DBTest, Snapshot) { Put(0, "foo", "0v3"); Put(1, "foo", "1v3"); - const Snapshot* s3 = db_->GetSnapshot(); - ASSERT_EQ(3U, GetNumSnapshots()); - ASSERT_EQ(time_snap1, GetTimeOldestSnapshots()); + { + ManagedSnapshot s3(db_); + ASSERT_EQ(3U, GetNumSnapshots()); + ASSERT_EQ(time_snap1, GetTimeOldestSnapshots()); - Put(0, "foo", "0v4"); - Put(1, "foo", "1v4"); - ASSERT_EQ("0v1", Get(0, "foo", s1)); - ASSERT_EQ("1v1", Get(1, "foo", s1)); - ASSERT_EQ("0v2", Get(0, "foo", s2)); - ASSERT_EQ("1v2", Get(1, "foo", s2)); - ASSERT_EQ("0v3", Get(0, "foo", s3)); - ASSERT_EQ("1v3", Get(1, "foo", s3)); - ASSERT_EQ("0v4", Get(0, "foo")); - ASSERT_EQ("1v4", Get(1, "foo")); + Put(0, "foo", "0v4"); + Put(1, "foo", "1v4"); + ASSERT_EQ("0v1", Get(0, "foo", s1)); + ASSERT_EQ("1v1", Get(1, "foo", s1)); + ASSERT_EQ("0v2", Get(0, "foo", s2)); + ASSERT_EQ("1v2", Get(1, "foo", s2)); + ASSERT_EQ("0v3", Get(0, "foo", s3.snapshot())); + ASSERT_EQ("1v3", Get(1, "foo", s3.snapshot())); + ASSERT_EQ("0v4", Get(0, "foo")); + ASSERT_EQ("1v4", Get(1, "foo")); + } - db_->ReleaseSnapshot(s3); ASSERT_EQ(2U, GetNumSnapshots()); ASSERT_EQ(time_snap1, GetTimeOldestSnapshots()); ASSERT_EQ("0v1", Get(0, "foo", s1)); diff --git a/db/flush_job.h b/db/flush_job.h index db2a49cda..180112584 100644 --- a/db/flush_job.h +++ b/db/flush_job.h @@ -17,11 +17,11 @@ #include #include "db/dbformat.h" -#include "db/log_writer.h" -#include "db/snapshot.h" #include "db/column_family.h" -#include "db/version_edit.h" +#include "db/log_writer.h" #include "db/memtable_list.h" +#include "db/snapshot_impl.h" +#include "db/version_edit.h" #include "port/port.h" #include "rocksdb/db.h" #include "rocksdb/env.h" diff --git a/db/snapshot_impl.cc b/db/snapshot_impl.cc new file mode 100644 index 000000000..1546d68f6 --- /dev/null +++ b/db/snapshot_impl.cc @@ -0,0 +1,23 @@ +// Copyright (c) 2015, Facebook, Inc. All rights reserved. +// This source code is licensed under the BSD-style license found in the +// LICENSE file in the root directory of this source tree. An additional grant +// of patent rights can be found in the PATENTS file in the same directory. + +#include "rocksdb/snapshot.h" + +#include "rocksdb/db.h" + +namespace rocksdb { + +ManagedSnapshot::ManagedSnapshot(DB* db) : db_(db), + snapshot_(db->GetSnapshot()) {} + +ManagedSnapshot::~ManagedSnapshot() { + if (snapshot_) { + db_->ReleaseSnapshot(snapshot_); + } +} + +const Snapshot* ManagedSnapshot::snapshot() { return snapshot_;} + +} // namespace rocksdb diff --git a/db/snapshot.h b/db/snapshot_impl.h similarity index 100% rename from db/snapshot.h rename to db/snapshot_impl.h diff --git a/db/write_batch.cc b/db/write_batch.cc index 44e1cd84d..28b2b6fc6 100644 --- a/db/write_batch.cc +++ b/db/write_batch.cc @@ -32,7 +32,7 @@ #include "db/db_impl.h" #include "db/column_family.h" #include "db/memtable.h" -#include "db/snapshot.h" +#include "db/snapshot_impl.h" #include "db/write_batch_internal.h" #include "util/coding.h" #include "util/statistics.h" diff --git a/include/rocksdb/db.h b/include/rocksdb/db.h index b3f028e6d..c5aca3af4 100644 --- a/include/rocksdb/db.h +++ b/include/rocksdb/db.h @@ -22,6 +22,7 @@ #include "rocksdb/types.h" #include "rocksdb/transaction_log.h" #include "rocksdb/listener.h" +#include "rocksdb/snapshot.h" #include "rocksdb/thread_status.h" #ifdef _WIN32 @@ -68,18 +69,6 @@ struct ColumnFamilyDescriptor { static const int kMajorVersion = __ROCKSDB_MAJOR__; static const int kMinorVersion = __ROCKSDB_MINOR__; -// Abstract handle to particular state of a DB. -// A Snapshot is an immutable object and can therefore be safely -// accessed from multiple threads without any external synchronization. -class Snapshot { - public: - // returns Snapshot's sequence number - virtual SequenceNumber GetSequenceNumber() const = 0; - - protected: - virtual ~Snapshot(); -}; - // A range of keys struct Range { Slice start; // Included in the range diff --git a/include/rocksdb/snapshot.h b/include/rocksdb/snapshot.h new file mode 100644 index 000000000..aad675b4b --- /dev/null +++ b/include/rocksdb/snapshot.h @@ -0,0 +1,45 @@ +// Copyright (c) 2015, Facebook, Inc. All rights reserved. +// This source code is licensed under the BSD-style license found in the +// LICENSE file in the root directory of this source tree. An additional grant +// of patent rights can be found in the PATENTS file in the same directory. + +#pragma once + +#include "rocksdb/types.h" + +namespace rocksdb { + +class DB; + +// Abstract handle to particular state of a DB. +// A Snapshot is an immutable object and can therefore be safely +// accessed from multiple threads without any external synchronization. +// +// To Create a Snapshot, call DB::GetSnapshot(). +// To Destroy a Snapshot, call DB::ReleaseSnapshot(snapshot). +class Snapshot { + public: + // returns Snapshot's sequence number + virtual SequenceNumber GetSequenceNumber() const = 0; + + protected: + virtual ~Snapshot(); +}; + +// Simple RAII wrapper class for Snapshot. +// Constructing this object will create a snapshot. Destructing will +// release the snapshot. +class ManagedSnapshot { + public: + explicit ManagedSnapshot(DB* db); + + ~ManagedSnapshot(); + + const Snapshot* snapshot(); + + private: + DB* db_; + const Snapshot* snapshot_; +}; + +} // namespace rocksdb diff --git a/src.mk b/src.mk index 860d7ab68..4e94c7d94 100644 --- a/src.mk +++ b/src.mk @@ -33,6 +33,7 @@ LIB_SOURCES = \ db/merge_operator.cc \ db/repair.cc \ db/slice.cc \ + db/snapshot_impl.cc \ db/table_cache.cc \ db/table_properties_collector.cc \ db/transaction_log_impl.cc \