Skip to content

Commit ca5ff82

Browse files
完成对batch的拆解
1 parent ae7c2a5 commit ca5ff82

File tree

3 files changed

+15
-13
lines changed

3 files changed

+15
-13
lines changed

demo/batch_test/write_batch_demo.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ namespace WRITE_BATCH_DEMO {
108108

109109

110110
WriteBatch::WriteBatch() {
111-
//Clear();
111+
Clear();
112112
}
113113

114114
WriteBatch::~WriteBatch() = default;
@@ -155,6 +155,7 @@ namespace WRITE_BATCH_DEMO {
155155
}
156156

157157
// 2. 剔除序列化相关的无用数据
158+
input.remove_prefix(kHeader);
158159
leveldb::Slice key, value;
159160
int32_t found = 0;
160161

demo/batch_test/write_batch_internal_demo.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ uint32_t WriteBatchInternal::Count(const WriteBatch *batch) {
2121
void WriteBatchInternal::SetCount(WriteBatch *batch, uint32_t n) {
2222
// 前面8byte sequence number
2323
// 这里和上边的去比啊,这里使用[]取值是因为[]取出的非const 引用
24-
auto *const buffer = reinterpret_cast<uint8_t *>(batch->rep_[8]);
24+
auto *const buffer = reinterpret_cast<uint8_t *>(&batch->rep_[8]);
2525
// 将uint32_t类型数据每8个字节存储到一个chat里面
2626
buffer[0] = static_cast<uint8_t>(n);
2727
buffer[1] = static_cast<uint8_t>(n >> 8);

demo/batch_test/write_batch_main.cpp

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,34 +17,35 @@ class HandlerDemo : public WRITE_BATCH_DEMO::WriteBatch::Handler {
1717
}
1818

1919
void Delete(const leveldb::Slice &key) override {
20-
std::cout << "put : " << key.ToString() << std::endl;
20+
std::cout << "Delete : " << key.ToString() << std::endl;
2121

2222
}
2323
};
2424

2525

2626
int main(int argc, char **argv) {
2727

28-
29-
int x = 12;
30-
3128
WRITE_BATCH_DEMO::WriteBatch batch;
29+
WRITE_BATCH_DEMO::WriteBatch batch1;
3230
HandlerDemo handler;
3331

3432
//
3533
auto key = leveldb::Slice("xiaoming");
3634
auto value = leveldb::Slice("21");
3735
batch.Put(key, value);
38-
//batch.Put(leveldb::Slice("xiaohong"), leveldb::Slice("21"));
39-
//batch.Put(leveldb::Slice("wanger"), leveldb::Slice("21"));
40-
//batch.Put(leveldb::Slice("daxiong"), leveldb::Slice("21"));
41-
//
42-
//batch.Delete("xiaoming");
43-
//
44-
//batch.Iterate(&handler);
36+
batch.Put(leveldb::Slice("xiaohong"), leveldb::Slice("21"));
37+
batch.Put(leveldb::Slice("wanger"), leveldb::Slice("22"));
38+
batch.Put(leveldb::Slice("daxiong"), leveldb::Slice("24"));
39+
40+
batch1.Put(leveldb::Slice("Batch1ForTest"), leveldb::Slice("18"));
4541

42+
batch.Delete("xiaoming");
4643

44+
std::cout << batch.ApproximateSize() << std::endl;
45+
batch.Append(batch1);
46+
std::cout << batch.ApproximateSize() << std::endl;
4747

48+
std::cout << batch.Iterate(&handler).ToString() << std::endl;
4849

4950
return 0;
5051
}

0 commit comments

Comments
 (0)