Add ClearSnapshot()

Summary:
MyRocks needs the ability to clear a snapshot for Read Committed support

Test Plan: transaction_test

Reviewers: anthony

Reviewed By: anthony

Subscribers: dhruba

Differential Revision: https://reviews.facebook.net/D48861
main
Jay Edgar 9 years ago
parent f9ba79ecd6
commit 8f143e03fb
  1. 16
      include/rocksdb/utilities/transaction.h
  2. 5
      utilities/transactions/transaction_base.h
  3. 47
      utilities/transactions/transaction_test.cc

@ -83,10 +83,22 @@ class Transaction {
// Returns the Snapshot created by the last call to SetSnapshot().
//
// REQUIRED: The returned Snapshot is only valid up until the next time
// SetSnapshot()/SetSnapshotOnNextSavePoint() is called or the Transaction
// is deleted.
// SetSnapshot()/SetSnapshotOnNextSavePoint() is called, ClearSnapshot()
// is called, or the Transaction is deleted.
virtual const Snapshot* GetSnapshot() const = 0;
// Clears the current snapshot (i.e. no snapshot will be 'set')
//
// This removes any snapshot that currently exists or is set to be created
// on the next update operation (SetSnapshotOnNextOperation).
//
// Calling ClearSnapshot() has no effect on keys written before this function
// has been called.
//
// If a reference to a snapshot was retrieved via GetSnapshot(), it will no
// longer be valid and should be discarded after a call to ClearSnapshot().
virtual void ClearSnapshot() = 0;
// Write all batched keys to the db atomically.
//
// Returns OK on success.

@ -171,6 +171,11 @@ class TransactionBaseImpl : public Transaction {
void SetSnapshot() override;
void SetSnapshotOnNextOperation() override;
void ClearSnapshot() override {
snapshot_.reset();
snapshot_needed_ = false;
}
void DisableIndexing() override { indexing_enabled_ = false; }
void EnableIndexing() override { indexing_enabled_ = true; }

@ -2161,6 +2161,53 @@ TEST_F(TransactionTest, DeferSnapshotSavePointTest) {
delete txn1;
}
TEST_F(TransactionTest, ClearSnapshotTest) {
WriteOptions write_options;
ReadOptions read_options, snapshot_read_options;
string value;
Status s;
s = db->Put(write_options, "foo", "0");
ASSERT_OK(s);
Transaction* txn = db->BeginTransaction(write_options);
ASSERT_TRUE(txn);
s = db->Put(write_options, "foo", "1");
ASSERT_OK(s);
snapshot_read_options.snapshot = txn->GetSnapshot();
ASSERT_FALSE(snapshot_read_options.snapshot);
// No snapshot created yet
s = txn->Get(snapshot_read_options, "foo", &value);
ASSERT_EQ(value, "1");
txn->SetSnapshot();
snapshot_read_options.snapshot = txn->GetSnapshot();
ASSERT_TRUE(snapshot_read_options.snapshot);
s = db->Put(write_options, "foo", "2");
ASSERT_OK(s);
// Snapshot was created before change to '2'
s = txn->Get(snapshot_read_options, "foo", &value);
ASSERT_EQ(value, "1");
txn->ClearSnapshot();
snapshot_read_options.snapshot = txn->GetSnapshot();
ASSERT_FALSE(snapshot_read_options.snapshot);
// Snapshot has now been cleared
s = txn->Get(snapshot_read_options, "foo", &value);
ASSERT_EQ(value, "2");
s = txn->Commit();
ASSERT_OK(s);
delete txn;
}
} // namespace rocksdb
int main(int argc, char** argv) {

Loading…
Cancel
Save