Feathercoin  0.5.0
P2P Digital Currency
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
write_batch.cc
Go to the documentation of this file.
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]
15 
16 #include "leveldb/write_batch.h"
17 
18 #include "leveldb/db.h"
19 #include "db/dbformat.h"
20 #include "db/memtable.h"
22 #include "util/coding.h"
23 
24 namespace leveldb {
25 
26 // WriteBatch header has an 8-byte sequence number followed by a 4-byte count.
27 static const size_t kHeader = 12;
28 
30  Clear();
31 }
32 
34 
36 
38  rep_.clear();
39  rep_.resize(kHeader);
40 }
41 
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");
69  }
70  break;
71  default:
72  return Status::Corruption("unknown WriteBatch tag");
73  }
74  }
75  if (found != WriteBatchInternal::Count(this)) {
76  return Status::Corruption("WriteBatch has wrong count");
77  } else {
78  return Status::OK();
79  }
80 }
81 
83  return DecodeFixed32(b->rep_.data() + 8);
84 }
85 
87  EncodeFixed32(&b->rep_[8], n);
88 }
89 
91  return SequenceNumber(DecodeFixed64(b->rep_.data()));
92 }
93 
95  EncodeFixed64(&b->rep_[0], seq);
96 }
97 
98 void WriteBatch::Put(const Slice& key, const Slice& value) {
100  rep_.push_back(static_cast<char>(kTypeValue));
102  PutLengthPrefixedSlice(&rep_, value);
103 }
104 
105 void WriteBatch::Delete(const Slice& key) {
107  rep_.push_back(static_cast<char>(kTypeDeletion));
109 }
110 
111 namespace {
112 class MemTableInserter : public WriteBatch::Handler {
113  public:
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  }
125 };
126 } // namespace
127 
129  MemTable* memtable) {
130  MemTableInserter inserter;
131  inserter.sequence_ = WriteBatchInternal::Sequence(b);
132  inserter.mem_ = memtable;
133  return b->Iterate(&inserter);
134 }
135 
137  assert(contents.size() >= kHeader);
138  b->rep_.assign(contents.data(), contents.size());
139 }
140 
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);
145 }
146 
147 } // namespace leveldb
std::string * value
Definition: version_set.cc:270
virtual void Put(const Slice &key, const Slice &value)=0
static void SetContents(WriteBatch *batch, const Slice &contents)
Definition: write_batch.cc:136
uint64_t DecodeFixed64(const char *ptr)
Definition: coding.h:72
const char * data() const
Definition: slice.h:40
virtual void Delete(const Slice &key)=0
static Status Corruption(const Slice &msg, const Slice &msg2=Slice())
Definition: status.h:38
void EncodeFixed32(char *buf, uint32_t value)
Definition: coding.cc:9
static void SetSequence(WriteBatch *batch, SequenceNumber seq)
Definition: write_batch.cc:94
uint32_t DecodeFixed32(const char *ptr)
Definition: coding.h:58
void Delete(const Slice &key)
Definition: write_batch.cc:105
static Status OK()
Definition: status.h:32
bool empty() const
Definition: slice.h:46
uint64_t SequenceNumber
Definition: dbformat.h:63
void remove_prefix(size_t n)
Definition: slice.h:59
void EncodeFixed64(char *buf, uint64_t value)
Definition: coding.cc:20
MemTable * mem_
Definition: write_batch.cc:115
size_t size() const
Definition: slice.h:43
void PutLengthPrefixedSlice(std::string *dst, const Slice &value)
Definition: coding.cc:98
static void Append(WriteBatch *dst, const WriteBatch *src)
Definition: write_batch.cc:141
Status Iterate(Handler *handler) const
Definition: write_batch.cc:42
static Status InsertInto(const WriteBatch *batch, MemTable *memtable)
Definition: write_batch.cc:128
static SequenceNumber Sequence(const WriteBatch *batch)
Definition: write_batch.cc:90
static int Count(const WriteBatch *batch)
Definition: write_batch.cc:82
const char * GetLengthPrefixedSlice(const char *p, const char *limit, Slice *result)
Definition: coding.cc:172
static void SetCount(WriteBatch *batch, int n)
Definition: write_batch.cc:86
void Put(const Slice &key, const Slice &value)
Definition: write_batch.cc:98
std::string rep_
Definition: write_batch.h:57
SequenceNumber sequence_
Definition: write_batch.cc:114