blob: 4963579bd682287b1e2ba0a41d417c790715bfcd [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
5#include "include/db.h"
6
7#include "db/memtable.h"
8#include "db/write_batch_internal.h"
9#include "include/env.h"
10#include "util/logging.h"
11#include "util/testharness.h"
12
13namespace leveldb {
14
15static std::string PrintContents(WriteBatch* b) {
16 InternalKeyComparator cmp(BytewiseComparator());
17 MemTable mem(cmp);
18 std::string state;
19 Status s = WriteBatchInternal::InsertInto(b, &mem);
20 Iterator* iter = mem.NewIterator();
21 for (iter->SeekToFirst(); iter->Valid(); iter->Next()) {
22 ParsedInternalKey ikey;
23 ASSERT_TRUE(ParseInternalKey(iter->key(), &ikey));
24 switch (ikey.type) {
25 case kTypeValue:
26 state.append("Put(");
27 state.append(ikey.user_key.ToString());
28 state.append(", ");
29 state.append(iter->value().ToString());
30 state.append(")");
31 break;
32 case kTypeLargeValueRef:
33 state.append("PutRef(");
34 state.append(ikey.user_key.ToString());
35 state.append(", ");
36 state.append(iter->value().ToString());
37 state.append(")");
38 break;
39 case kTypeDeletion:
40 state.append("Delete(");
41 state.append(ikey.user_key.ToString());
42 state.append(")");
43 break;
44 }
45 state.append("@");
46 state.append(NumberToString(ikey.sequence));
47 }
48 delete iter;
49 if (!s.ok()) {
50 state.append("ParseError()");
51 }
52 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
77TEST(WriteBatchTest, PutIndirect) {
78 WriteBatch batch;
79 batch.Put(Slice("baz"), Slice("boo"));
80 LargeValueRef h;
81 for (int i = 0; i < LargeValueRef::ByteSize(); i++) {
82 h.data[i] = (i < 20) ? 'a' : 'b';
83 }
84 WriteBatchInternal::PutLargeValueRef(&batch, Slice("foo"), h);
85 WriteBatchInternal::SetSequence(&batch, 100);
86 ASSERT_EQ(100, WriteBatchInternal::Sequence(&batch));
87 ASSERT_EQ(2, WriteBatchInternal::Count(&batch));
88 ASSERT_EQ("Put(baz, boo)@100"
89 "PutRef(foo, aaaaaaaaaaaaaaaaaaaabbbbbbbbb)@101",
90 PrintContents(&batch));
91}
92
93TEST(WriteBatchTest, Corruption) {
94 WriteBatch batch;
95 batch.Put(Slice("foo"), Slice("bar"));
96 batch.Delete(Slice("box"));
97 WriteBatchInternal::SetSequence(&batch, 200);
98 Slice contents = WriteBatchInternal::Contents(&batch);
99 WriteBatchInternal::SetContents(&batch,
100 Slice(contents.data(),contents.size()-1));
101 ASSERT_EQ("Put(foo, bar)@200"
102 "ParseError()",
103 PrintContents(&batch));
104}
105
106}
107
108int main(int argc, char** argv) {
109 return leveldb::test::RunAllTests();
110}