blob: 8d38023acdac2d1e64a15f97071dcf9a5338ce0f [file] [log] [blame]
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +00001// 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
jorlow@chromium.org4671a692011-03-30 18:35:40 +00005#include "leveldb/db.h"
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +00006
7#include "db/memtable.h"
8#include "db/write_batch_internal.h"
jorlow@chromium.org4671a692011-03-30 18:35:40 +00009#include "leveldb/env.h"
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +000010#include "util/logging.h"
11#include "util/testharness.h"
12
13namespace leveldb {
14
15static std::string PrintContents(WriteBatch* b) {
16 InternalKeyComparator cmp(BytewiseComparator());
dgrogan@chromium.orgda799092011-05-21 02:17:43 +000017 MemTable* mem = new MemTable(cmp);
18 mem->Ref();
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +000019 std::string state;
dgrogan@chromium.orgda799092011-05-21 02:17:43 +000020 Status s = WriteBatchInternal::InsertInto(b, mem);
Sanjay Ghemawatd79762e2012-03-08 16:23:21 -080021 int count = 0;
dgrogan@chromium.orgda799092011-05-21 02:17:43 +000022 Iterator* iter = mem->NewIterator();
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +000023 for (iter->SeekToFirst(); iter->Valid(); iter->Next()) {
24 ParsedInternalKey ikey;
25 ASSERT_TRUE(ParseInternalKey(iter->key(), &ikey));
26 switch (ikey.type) {
27 case kTypeValue:
28 state.append("Put(");
29 state.append(ikey.user_key.ToString());
30 state.append(", ");
31 state.append(iter->value().ToString());
32 state.append(")");
Sanjay Ghemawatd79762e2012-03-08 16:23:21 -080033 count++;
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +000034 break;
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +000035 case kTypeDeletion:
36 state.append("Delete(");
37 state.append(ikey.user_key.ToString());
38 state.append(")");
Sanjay Ghemawatd79762e2012-03-08 16:23:21 -080039 count++;
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +000040 break;
41 }
42 state.append("@");
43 state.append(NumberToString(ikey.sequence));
44 }
45 delete iter;
46 if (!s.ok()) {
47 state.append("ParseError()");
Sanjay Ghemawatd79762e2012-03-08 16:23:21 -080048 } else if (count != WriteBatchInternal::Count(b)) {
49 state.append("CountMismatch()");
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +000050 }
dgrogan@chromium.orgda799092011-05-21 02:17:43 +000051 mem->Unref();
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +000052 return state;
53}
54
55class WriteBatchTest { };
56
57TEST(WriteBatchTest, Empty) {
58 WriteBatch batch;
59 ASSERT_EQ("", PrintContents(&batch));
60 ASSERT_EQ(0, WriteBatchInternal::Count(&batch));
61}
62
63TEST(WriteBatchTest, Multiple) {
64 WriteBatch batch;
65 batch.Put(Slice("foo"), Slice("bar"));
66 batch.Delete(Slice("box"));
67 batch.Put(Slice("baz"), Slice("boo"));
68 WriteBatchInternal::SetSequence(&batch, 100);
69 ASSERT_EQ(100, WriteBatchInternal::Sequence(&batch));
70 ASSERT_EQ(3, WriteBatchInternal::Count(&batch));
71 ASSERT_EQ("Put(baz, boo)@102"
72 "Delete(box)@101"
73 "Put(foo, bar)@100",
74 PrintContents(&batch));
75}
76
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +000077TEST(WriteBatchTest, Corruption) {
78 WriteBatch batch;
79 batch.Put(Slice("foo"), Slice("bar"));
80 batch.Delete(Slice("box"));
81 WriteBatchInternal::SetSequence(&batch, 200);
82 Slice contents = WriteBatchInternal::Contents(&batch);
83 WriteBatchInternal::SetContents(&batch,
84 Slice(contents.data(),contents.size()-1));
85 ASSERT_EQ("Put(foo, bar)@200"
86 "ParseError()",
87 PrintContents(&batch));
88}
89
Sanjay Ghemawatd79762e2012-03-08 16:23:21 -080090TEST(WriteBatchTest, Append) {
91 WriteBatch b1, b2;
92 WriteBatchInternal::SetSequence(&b1, 200);
93 WriteBatchInternal::SetSequence(&b2, 300);
94 WriteBatchInternal::Append(&b1, &b2);
95 ASSERT_EQ("",
96 PrintContents(&b1));
97 b2.Put("a", "va");
98 WriteBatchInternal::Append(&b1, &b2);
99 ASSERT_EQ("Put(a, va)@200",
100 PrintContents(&b1));
101 b2.Clear();
102 b2.Put("b", "vb");
103 WriteBatchInternal::Append(&b1, &b2);
104 ASSERT_EQ("Put(a, va)@200"
105 "Put(b, vb)@201",
106 PrintContents(&b1));
107 b2.Delete("foo");
108 WriteBatchInternal::Append(&b1, &b2);
109 ASSERT_EQ("Put(a, va)@200"
110 "Put(b, vb)@202"
111 "Put(b, vb)@201"
112 "Delete(foo)@203",
113 PrintContents(&b1));
114}
115
costan69e2bd22017-05-23 17:29:44 -0700116TEST(WriteBatchTest, ApproximateSize) {
117 WriteBatch batch;
118 size_t empty_size = batch.ApproximateSize();
119
120 batch.Put(Slice("foo"), Slice("bar"));
121 size_t one_key_size = batch.ApproximateSize();
122 ASSERT_LT(empty_size, one_key_size);
123
124 batch.Put(Slice("baz"), Slice("boo"));
125 size_t two_keys_size = batch.ApproximateSize();
126 ASSERT_LT(one_key_size, two_keys_size);
127
128 batch.Delete(Slice("box"));
129 size_t post_delete_size = batch.ApproximateSize();
130 ASSERT_LT(two_keys_size, post_delete_size);
131}
132
Hans Wennborg36a5f8e2011-10-31 17:22:06 +0000133} // namespace leveldb
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +0000134
135int main(int argc, char** argv) {
136 return leveldb::test::RunAllTests();
137}