Add GetAllSnapshots functionality to SnapshotList (#1240)

This commit is contained in:
Anushree Jha 2025-01-10 22:21:57 +05:30
parent 578eeb702e
commit 7e55292dff
6 changed files with 57 additions and 0 deletions

View File

@ -330,6 +330,7 @@ if(LEVELDB_BUILD_TESTS)
"db/autocompact_test.cc"
"db/corruption_test.cc"
"db/db_test.cc"
"db/snapshot_test.cc"
"db/dbformat_test.cc"
"db/filename_test.cc"
"db/log_test.cc"

View File

@ -1575,4 +1575,12 @@ Status DestroyDB(const std::string& dbname, const Options& options) {
return result;
}
std::vector<const Snapshot*> DBImpl::GetAllSnapshots() {
std::vector<const Snapshot*> snapshots;
mutex_.Lock();
snapshots = snapshots_.GetAllSnapshots(); // Call the SnapshotList method
mutex_.Unlock();
return snapshots;
}
} // namespace leveldb

View File

@ -9,6 +9,7 @@
#include <deque>
#include <set>
#include <string>
#include <vector>
#include "db/dbformat.h"
#include "db/log_writer.h"
@ -48,6 +49,9 @@ class DBImpl : public DB {
bool GetProperty(const Slice& property, std::string* value) override;
void GetApproximateSizes(const Range* range, int n, uint64_t* sizes) override;
void CompactRange(const Slice* begin, const Slice* end) override;
// Retrieve all active snapshots in the database
std::vector<const Snapshot*> GetAllSnapshots() override;
// Extra methods (for testing) that are not in the public DB interface

View File

@ -85,6 +85,17 @@ class SnapshotList {
delete snapshot;
}
// Retrieves all snapshots in the list.
//
// This method iterates through the linked list and collects all snapshots.
std::vector<const Snapshot*> GetAllSnapshots() const {
std::vector<const Snapshot*> snapshots;
for (const SnapshotImpl* s = head_.next_; s != &head_; s = s->next_) {
snapshots.push_back(static_cast<const Snapshot*>(s));
}
return snapshots;
}
private:
// Dummy head of doubly-linked list of snapshots
SnapshotImpl head_;

30
db/snapshot_test.cc Normal file
View File

@ -0,0 +1,30 @@
#include "db/db_impl.h"
#include "gtest/gtest.h"
namespace leveldb {
// Test to validate GetAllSnapshots retrieves all snapshots correctly.
TEST(SnapshotTest, GetAllSnapshots) {
SnapshotList snapshot_list;
// Create some snapshots
SnapshotImpl* s1 = snapshot_list.New(1);
SnapshotImpl* s2 = snapshot_list.New(2);
SnapshotImpl* s3 = snapshot_list.New(3);
// Use GetAllSnapshots to retrieve them
std::vector<const Snapshot*> snapshots = snapshot_list.GetAllSnapshots();
// Validate the results
ASSERT_EQ(snapshots.size(), 3);
EXPECT_EQ(static_cast<const SnapshotImpl*>(snapshots[0])->sequence_number(), 1);
EXPECT_EQ(static_cast<const SnapshotImpl*>(snapshots[1])->sequence_number(), 2);
EXPECT_EQ(static_cast<const SnapshotImpl*>(snapshots[2])->sequence_number(), 3);
// Clean up
snapshot_list.Delete(s1);
snapshot_list.Delete(s2);
snapshot_list.Delete(s3);
}
}

View File

@ -145,6 +145,9 @@ class LEVELDB_EXPORT DB {
// Therefore the following call will compact the entire database:
// db->CompactRange(nullptr, nullptr);
virtual void CompactRange(const Slice* begin, const Slice* end) = 0;
// Retrieve all active snapshots in the database.
virtual std::vector<const Snapshot*> GetAllSnapshots() = 0;
};
// Destroy the contents of the specified database.