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
This commit is contained in:
agiardullo 2015-08-06 17:59:05 -07:00
parent 257ee895f9
commit 16ea1c7d1c
9 changed files with 93 additions and 33 deletions

View File

@ -19,11 +19,11 @@
#include <string>
#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"

View File

@ -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));

View File

@ -17,11 +17,11 @@
#include <string>
#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"

23
db/snapshot_impl.cc Normal file
View File

@ -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

View File

@ -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"

View File

@ -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

View File

@ -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

1
src.mk
View File

@ -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 \