Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add GetAllSnapshots functionality to SnapshotList (#1240) #1242

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
8 changes: 8 additions & 0 deletions db/db_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 4 additions & 0 deletions db/db_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <deque>
#include <set>
#include <string>
#include <vector>

#include "db/dbformat.h"
#include "db/log_writer.h"
Expand Down Expand Up @@ -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

Expand Down
11 changes: 11 additions & 0 deletions db/snapshot.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_;
Expand Down
30 changes: 30 additions & 0 deletions db/snapshot_test.cc
Original file line number Diff line number Diff line change
@@ -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);
}

}
3 changes: 3 additions & 0 deletions include/leveldb/db.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down