Skip to content

Commit 24b3279

Browse files
committed
modified: README.md
modified: db/write_batch.cc modified: include/leveldb/comparator.h modified: include/leveldb/options.h modified: include/leveldb/slice.h modified: include/leveldb/status.h modified: include/leveldb/write_batch.h modified: util/comparator.cc modified: util/status.cc
1 parent 9c85ab6 commit 24b3279

File tree

9 files changed

+142
-150
lines changed

9 files changed

+142
-150
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,3 +137,9 @@ return Status::NotFound("in-memory file skipped past end");
137137
#### 4. include/leveldb/comparator.h util/comparator.cc
138138

139139
对key排序时使用的比较方法。默认为升序。
140+
141+
#### 5. include/leveldb/write_batch.h db/write_batch.cc
142+
143+
对若干数目 key 的 write 操作(put/delete)封装成 WriteBatch
144+
145+

db/write_batch.cc

Lines changed: 94 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
1-
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
2-
// Use of this source code is governed by a BSD-style license that can be
3-
// found in the LICENSE file. See the AUTHORS file for names of contributors.
4-
//
5-
// WriteBatch::rep_ :=
6-
// sequence: fixed64
7-
// count: fixed32
8-
// data: record[count]
9-
// record :=
10-
// kTypeValue varstring varstring |
11-
// kTypeDeletion varstring
12-
// varstring :=
13-
// len: varint32
14-
// data: uint8[len]
1+
// WriteBatch唯一成员string rep_格式
2+
// sequence: 64bit
3+
// count: 32bit 有多少条记录需要处理
4+
// data: recode[count] 具体某条记录
5+
// recode :=
6+
// (kTypeValue 为记录类型,更新or删除,
7+
// 删除时只有key) )
8+
// kTypeValue varstring varstring. kTypeValue |
9+
// kTypeValue varstring(key)
10+
// varstring :=
11+
// len: varint32
12+
// data: uint8[len]
1513

1614
#include "leveldb/write_batch.h"
1715

@@ -23,125 +21,131 @@
2321

2422
namespace leveldb {
2523

26-
// WriteBatch header has an 8-byte sequence number followed by a 4-byte count.
27-
static const size_t kHeader = 12;
24+
// sequence(8-byte) and count(4-byte)
25+
static const size_t kHeader = 12;
2826

2927
WriteBatch::WriteBatch() {
30-
Clear();
28+
Clear();
3129
}
3230

33-
WriteBatch::~WriteBatch() { }
31+
WriteBatch::~WriteBatch() {}
3432

35-
WriteBatch::Handler::~Handler() { }
33+
WriteBatch::Handler::~Handler(){}
3634

3735
void WriteBatch::Clear() {
38-
rep_.clear();
39-
rep_.resize(kHeader);
36+
rep_.clear();
37+
rep_.resize(kHeader);
4038
}
4139

4240
Status WriteBatch::Iterate(Handler* handler) const {
43-
Slice input(rep_);
44-
if (input.size() < kHeader) {
45-
return Status::Corruption("malformed WriteBatch (too small)");
46-
}
47-
48-
input.remove_prefix(kHeader);
49-
Slice key, value;
50-
int found = 0;
51-
while (!input.empty()) {
52-
found++;
53-
char tag = input[0];
54-
input.remove_prefix(1);
55-
switch (tag) {
56-
case kTypeValue:
57-
if (GetLengthPrefixedSlice(&input, &key) &&
58-
GetLengthPrefixedSlice(&input, &value)) {
59-
handler->Put(key, value);
60-
} else {
61-
return Status::Corruption("bad WriteBatch Put");
62-
}
63-
break;
64-
case kTypeDeletion:
65-
if (GetLengthPrefixedSlice(&input, &key)) {
66-
handler->Delete(key);
67-
} else {
68-
return Status::Corruption("bad WriteBatch Delete");
41+
Slice input(rep_);
42+
if (input.size() < kHeader) {
43+
return Status::Corruption("malformed WriteBatch(too small)");
44+
}
45+
46+
// 移除req_ 的seq num和count
47+
input.remove_prefix(kHeader);
48+
Slice key, value;
49+
int found = 0;
50+
while (!input.empty()) {
51+
found++;
52+
char tag = input[0];
53+
input.remove_prefix(1);
54+
switch (tag) {
55+
case kTypeValue:
56+
if (GetLengthPrefixedSlice(&input, &key) && //get Key
57+
GetLengthPrefixedSlice(&input, &value)) { //get value
58+
handler->Put(key, value);
59+
} else {
60+
return Status::Corruption("bad WriteBatch Put");
61+
}
62+
break;
63+
64+
case kTypeDeletion:
65+
if (GetLengthPrefixedSlice(&input, &key)) {
66+
handler->Delete(key);
67+
} else {
68+
return Status::Corruption("bad WriteBatch Delete");
69+
}
70+
break;
71+
default:
72+
return Status::Corruption("unknown WriteBatch tag");
6973
}
70-
break;
71-
default:
72-
return Status::Corruption("unknown WriteBatch tag");
74+
} // while
75+
76+
if (found != WriteBatchInternal::Count(this)) {
77+
return Status::Corruption("WriteBatch has wrong count");
78+
} else {
79+
return Status::OK();
7380
}
74-
}
75-
if (found != WriteBatchInternal::Count(this)) {
76-
return Status::Corruption("WriteBatch has wrong count");
77-
} else {
78-
return Status::OK();
79-
}
8081
}
8182

8283
int WriteBatchInternal::Count(const WriteBatch* b) {
83-
return DecodeFixed32(b->rep_.data() + 8);
84+
return DecodeFixed32(b->rep_.data() + 8);
8485
}
8586

87+
// 设置读取记录数
8688
void WriteBatchInternal::SetCount(WriteBatch* b, int n) {
87-
EncodeFixed32(&b->rep_[8], n);
89+
EncodeFixed32(&b->rep_[8], n);
8890
}
8991

9092
SequenceNumber WriteBatchInternal::Sequence(const WriteBatch* b) {
91-
return SequenceNumber(DecodeFixed64(b->rep_.data()));
93+
return SequenceNumber(DecodeFixed64(b->rep_.data()));
9294
}
9395

9496
void WriteBatchInternal::SetSequence(WriteBatch* b, SequenceNumber seq) {
9597
EncodeFixed64(&b->rep_[0], seq);
9698
}
9799

98100
void WriteBatch::Put(const Slice& key, const Slice& value) {
99-
WriteBatchInternal::SetCount(this, WriteBatchInternal::Count(this) + 1);
100-
rep_.push_back(static_cast<char>(kTypeValue));
101-
PutLengthPrefixedSlice(&rep_, key);
102-
PutLengthPrefixedSlice(&rep_, value);
101+
WriteBatchInternal::SetCount(this, WriteBatchInternal::Count(this) + 1);
102+
rep_.push_back(static_cast<char>(kTypeValue));
103+
104+
// 将key append到rep_后面
105+
PutLengthPrefixedSlice(&rep_, key);
106+
PutLengthPrefixedSlice(&rep_, value);
103107
}
104108

105109
void WriteBatch::Delete(const Slice& key) {
106-
WriteBatchInternal::SetCount(this, WriteBatchInternal::Count(this) + 1);
107-
rep_.push_back(static_cast<char>(kTypeDeletion));
108-
PutLengthPrefixedSlice(&rep_, key);
110+
WriteBatchInternal::SetCount(this, WriteBatchInternal::Count(this) + 1);
111+
rep_.push_back(static_cast<char>(kTypeDeletion));
112+
PutLengthPrefixedSlice(&rep_, key);
109113
}
110114

111115
namespace {
112116
class MemTableInserter : public WriteBatch::Handler {
113-
public:
114-
SequenceNumber sequence_;
115-
MemTable* mem_;
116-
117-
virtual void Put(const Slice& key, const Slice& value) {
118-
mem_->Add(sequence_, kTypeValue, key, value);
119-
sequence_++;
120-
}
121-
virtual void Delete(const Slice& key) {
122-
mem_->Add(sequence_, kTypeDeletion, key, Slice());
123-
sequence_++;
124-
}
117+
public:
118+
SequenceNumber sequence_;
119+
MemTable* mem_;
120+
121+
virtual void Put(const Slice& key, const Slice& value) {
122+
mem_->Add(sequence_, kTypeValue, key ,value);
123+
sequence_++;
124+
}
125+
virtual void Delete(const Slice& key) {
126+
mem_->Add(sequence_, kTypeDeletion, key, Slice());
127+
sequence_++;
128+
}
125129
};
126-
} // namespace
130+
}
127131

128132
Status WriteBatchInternal::InsertInto(const WriteBatch* b,
129-
MemTable* memtable) {
130-
MemTableInserter inserter;
131-
inserter.sequence_ = WriteBatchInternal::Sequence(b);
132-
inserter.mem_ = memtable;
133-
return b->Iterate(&inserter);
133+
MemTable* memtable) {
134+
MemTableInserter inserter;
135+
inserter.sequence_ = WriteBatchInternal::Sequence(b);
136+
inserter.mem_ = memtable;
137+
return b->Iterate(&inserter);
134138
}
135139

136140
void WriteBatchInternal::SetContents(WriteBatch* b, const Slice& contents) {
137-
assert(contents.size() >= kHeader);
138-
b->rep_.assign(contents.data(), contents.size());
141+
assert(contents.size() > kHeader);
142+
b->rep_.assign(contents.data(), contents.size());
139143
}
140144

141145
void WriteBatchInternal::Append(WriteBatch* dst, const WriteBatch* src) {
142-
SetCount(dst, Count(dst) + Count(src));
143-
assert(src->rep_.size() >= kHeader);
144-
dst->rep_.append(src->rep_.data() + kHeader, src->rep_.size() - kHeader);
146+
SetCount(dst, Count(dst) + Count(src));
147+
assert(src->rep_.size() > kHeader);
148+
dst->rep_.append(src->rep_.data() + kHeader, src->rep_.size() - kHeader);
145149
}
146150

147-
} // namespace leveldb
151+
} // namespace leveldb

include/leveldb/comparator.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,16 @@ class Comparator {
1111
public:
1212
virtual ~Comparator();
1313

14-
virtual int Compare(const Slice& a, const SLice& b) const = 0;
14+
virtual int Compare(const Slice& a, const Slice& b) const = 0;
1515

1616
// The name of comparator.
1717
virtual const char* Name() const = 0;
1818

1919
virtual void FindShortestSeparator(
20-
std::string* start;
20+
std::string* start,
2121
const Slice& limit) const = 0;
2222

23-
virtual void FindShortSuccessor(str::string* key) const = 0;
23+
virtual void FindShortSuccessor(std::string* key) const = 0;
2424
};
2525

2626
extern const Comparator* BytewiseComparator();

include/leveldb/options.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ struct Options {
3535

3636
// If true, an error is raised if the database already exits.
3737
// Default: false
38-
bool error_if_exits;
38+
bool error_if_exists;
3939

4040
// If true, the implementation will do aggressive checking of the
4141
// data it is processing and will stop early if it detects any
@@ -50,7 +50,7 @@ struct Options {
5050
Logger* info_log;
5151

5252
// default: 4MB
53-
size_t wirte_buffer_size;
53+
size_t write_buffer_size;
5454

5555
// Number of open files that can used by DB. You may need to
5656
// increase this if your databases has a large working set (budget

include/leveldb/slice.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class Slice {
3939
void clear() { data_ = ""; size_ = 0; }
4040

4141
// drop the first "n" bytes from slices
42-
void remove_prifix(size_t n){
42+
void remove_prefix(size_t n){
4343
assert( n <= size() );
4444
data_ += n;
4545
size_ -= n;

include/leveldb/status.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ class Status {
2727
// 拷贝构造函数和重装赋值运算符
2828
Status(const Status& s);
2929
void operator=(const Status& s);
30-
3130
// return a success status.
3231
static Status OK() { return Status(); }
3332

@@ -44,15 +43,15 @@ class Status {
4443
static Status NotSupported(const Slice& msg, const Slice& msg2 = Slice()) {
4544
return Status(kNotSupported, msg, msg2);
4645
}
47-
static Status InvaildArgument(const Slice& msg, const Slice& msg2 = Slice()) {
46+
static Status InvalidArgument(const Slice& msg, const Slice& msg2 = Slice()) {
4847
return Status(kInvalidArgument, msg, msg2);
4948
}
5049
static Status IOError(const Slice& msg, const Slice& msg2 = Slice()) {
5150
return Status(kIOError, msg, msg2);
5251
}
5352

5453
// returns true if the status indicates success.
55-
bool ok() const { return (state == NULL); }
54+
bool ok() const { return (state_ == NULL); }
5655

5756
// returns true if the status indicates a NotFound error.
5857
bool IsNotFound() const { return code() == kNotFound; }
@@ -99,6 +98,11 @@ class Status {
9998
};
10099

101100
inline Status::Status(const Status& s) {
101+
state_ = (s.state_ == NULL) ? NULL : CopyState(s.state_);
102+
}
103+
104+
105+
inline void Status::operator=(const Status& s) {
102106
if (state_ != s.state_) {
103107
delete[] state_;
104108
state_ = (s.state_ == NULL) ? NULL : CopyState(s.state_);

0 commit comments

Comments
 (0)