Skip to content

Commit 96baea4

Browse files
committed
ldb dump_wal: add LOG_DATA, TIMED_PUT, PUT_BLOB_INDEX
Previously, LOG_DATA would be ignored and would print like an empty record, and TIMED_PUT and PUT_BLOB_INDEX would cause ldb to exit with an unimplemented error. Add a unit test to cover these cases.
1 parent 1cc1df8 commit 96baea4

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

tools/ldb_cmd.cc

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2942,6 +2942,20 @@ class InMemoryHandler : public WriteBatch::Handler {
29422942
return Status::OK();
29432943
}
29442944

2945+
Status TimedPutCF(uint32_t cf, const Slice& key, const Slice& value,
2946+
uint64_t write_time) override {
2947+
row_ << "TIMED_PUT(" << cf << ", " << write_time << ") : ";
2948+
commonPutMerge(cf, key, value);
2949+
return Status::OK();
2950+
}
2951+
2952+
Status PutBlobIndexCF(uint32_t cf, const Slice& key,
2953+
const Slice& value) override {
2954+
row_ << "PUT_BLOB_INDEX(" << cf << ") : ";
2955+
commonPutMerge(cf, key, value);
2956+
return Status::OK();
2957+
}
2958+
29452959
Status MergeCF(uint32_t cf, const Slice& key, const Slice& value) override {
29462960
row_ << "MERGE(" << cf << ") : ";
29472961
commonPutMerge(cf, key, value);
@@ -2973,6 +2987,11 @@ class InMemoryHandler : public WriteBatch::Handler {
29732987
return Status::OK();
29742988
}
29752989

2990+
void LogData(const Slice& blob) override {
2991+
row_ << "LOG_DATA : ";
2992+
row_ << LDBCommand::StringToHex(blob.ToString()) << " ";
2993+
}
2994+
29762995
Status MarkBeginPrepare(bool unprepare) override {
29772996
row_ << "BEGIN_PREPARE(";
29782997
row_ << (unprepare ? "true" : "false") << ") ";

tools/ldb_cmd_test.cc

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1255,6 +1255,66 @@ TEST_F(LdbCmdTest, CustomComparator) {
12551255
LDBCommandRunner::RunCommand(4, argv, opts, LDBOptions(), &cfds));
12561256
}
12571257

1258+
TEST_F(LdbCmdTest, DumpWal) {
1259+
Env* base_env = TryLoadCustomOrDefaultEnv();
1260+
std::unique_ptr<Env> env(NewMemEnv(base_env));
1261+
Options opts;
1262+
opts.env = env.get();
1263+
opts.create_if_missing = true;
1264+
1265+
std::string dbname = test::PerThreadDBPath(env.get(), "ldb_cmd_test");
1266+
1267+
DB* db = nullptr;
1268+
ASSERT_OK(DB::Open(opts, dbname, &db));
1269+
1270+
// PutLogData record
1271+
WriteBatch batch;
1272+
batch.PutLogData("xxx");
1273+
WriteOptions wopts;
1274+
ASSERT_OK(db->Write(wopts, &batch));
1275+
batch.Clear();
1276+
1277+
// PutBlobIndex record copied from db_blob_basic_test.cc
1278+
std::string blob_index;
1279+
constexpr uint64_t blob_file_number = 1000;
1280+
constexpr uint64_t offset = 1234;
1281+
constexpr uint64_t size = 5678;
1282+
BlobIndex::EncodeBlob(&blob_index, blob_file_number, offset, size,
1283+
kNoCompression);
1284+
constexpr uint64_t column_family_id = 0;
1285+
ASSERT_OK(WriteBatchInternal::PutBlobIndex(&batch, column_family_id,
1286+
"blob_index_key", blob_index));
1287+
ASSERT_OK(db->Write(wopts, &batch));
1288+
batch.Clear();
1289+
1290+
// TimedPut record
1291+
constexpr uint64_t write_unix_time = 1767123301; // 2025-12-30T19:35:01Z
1292+
batch.TimedPut(nullptr, "timed_put_key", "v", write_unix_time);
1293+
ASSERT_OK(db->Write(wopts, &batch));
1294+
batch.Clear();
1295+
1296+
ASSERT_OK(db->Close());
1297+
delete db;
1298+
1299+
string walfile_arg = string("--walfile=") + dbname;
1300+
const char* const argv[] = {"./ldb", "dump_wal", walfile_arg.c_str()};
1301+
static const size_t argc = sizeof(argv) / sizeof(*argv);
1302+
1303+
// capture cout while running the command
1304+
std::stringstream captured_cout;
1305+
std::streambuf* original_cout_buffer = std::cout.rdbuf(captured_cout.rdbuf());
1306+
int result =
1307+
LDBCommandRunner::RunCommand(argc, argv, opts, LDBOptions(), nullptr);
1308+
std::cout.rdbuf(original_cout_buffer);
1309+
1310+
// check the results of running dump_wal: log data must be included
1311+
string captured_output = captured_cout.str();
1312+
ASSERT_EQ(0, result) << "ldb output:\n\n" << captured_output;
1313+
ASSERT_NE(std::string::npos, captured_output.find("LOG_DATA : 0x787878"))
1314+
<< "ldb output:\n\n"
1315+
<< captured_output;
1316+
}
1317+
12581318
} // namespace ROCKSDB_NAMESPACE
12591319

12601320
int main(int argc, char** argv) {

0 commit comments

Comments
 (0)