1179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org// Copyright (c) 2011 The LevelDB Authors. All rights reserved. 2179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org// Use of this source code is governed by a BSD-style license that can be 3179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org// found in the LICENSE file. See the AUTHORS file for names of contributors. 4179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org 5fbd97aa4c5325eace57d24b89845b9581bac9324jorlow@chromium.org#include "leveldb/db.h" 6179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org 7179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org#include "db/memtable.h" 8179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org#include "db/write_batch_internal.h" 9fbd97aa4c5325eace57d24b89845b9581bac9324jorlow@chromium.org#include "leveldb/env.h" 10179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org#include "util/logging.h" 11179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org#include "util/testharness.h" 12179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org 13179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.orgnamespace leveldb { 14179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org 15179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.orgstatic std::string PrintContents(WriteBatch* b) { 16179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org InternalKeyComparator cmp(BytewiseComparator()); 17a5b4129c0a8c01158cde2244a5811f15b9d45ec0dgrogan@chromium.org MemTable* mem = new MemTable(cmp); 18a5b4129c0a8c01158cde2244a5811f15b9d45ec0dgrogan@chromium.org mem->Ref(); 19179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org std::string state; 20a5b4129c0a8c01158cde2244a5811f15b9d45ec0dgrogan@chromium.org Status s = WriteBatchInternal::InsertInto(b, mem); 2113daa9f29c999ee40a257ee0775abee2c78a0ad9sanjay@google.com int count = 0; 22a5b4129c0a8c01158cde2244a5811f15b9d45ec0dgrogan@chromium.org Iterator* iter = mem->NewIterator(); 23179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org for (iter->SeekToFirst(); iter->Valid(); iter->Next()) { 24179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org ParsedInternalKey ikey; 25179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org ASSERT_TRUE(ParseInternalKey(iter->key(), &ikey)); 26179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org switch (ikey.type) { 27179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org case kTypeValue: 28179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org state.append("Put("); 29179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org state.append(ikey.user_key.ToString()); 30179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org state.append(", "); 31179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org state.append(iter->value().ToString()); 32179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org state.append(")"); 3313daa9f29c999ee40a257ee0775abee2c78a0ad9sanjay@google.com count++; 34179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org break; 35179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org case kTypeDeletion: 36179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org state.append("Delete("); 37179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org state.append(ikey.user_key.ToString()); 38179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org state.append(")"); 3913daa9f29c999ee40a257ee0775abee2c78a0ad9sanjay@google.com count++; 40179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org break; 41179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org } 42179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org state.append("@"); 43179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org state.append(NumberToString(ikey.sequence)); 44179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org } 45179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org delete iter; 46179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org if (!s.ok()) { 47179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org state.append("ParseError()"); 4813daa9f29c999ee40a257ee0775abee2c78a0ad9sanjay@google.com } else if (count != WriteBatchInternal::Count(b)) { 4913daa9f29c999ee40a257ee0775abee2c78a0ad9sanjay@google.com state.append("CountMismatch()"); 50179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org } 51a5b4129c0a8c01158cde2244a5811f15b9d45ec0dgrogan@chromium.org mem->Unref(); 52179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org return state; 53179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org} 54179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org 55179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.orgclass WriteBatchTest { }; 56179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org 57179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.orgTEST(WriteBatchTest, Empty) { 58179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org WriteBatch batch; 59179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org ASSERT_EQ("", PrintContents(&batch)); 60179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org ASSERT_EQ(0, WriteBatchInternal::Count(&batch)); 61179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org} 62179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org 63179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.orgTEST(WriteBatchTest, Multiple) { 64179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org WriteBatch batch; 65179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org batch.Put(Slice("foo"), Slice("bar")); 66179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org batch.Delete(Slice("box")); 67179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org batch.Put(Slice("baz"), Slice("boo")); 68179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org WriteBatchInternal::SetSequence(&batch, 100); 69179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org ASSERT_EQ(100, WriteBatchInternal::Sequence(&batch)); 70179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org ASSERT_EQ(3, WriteBatchInternal::Count(&batch)); 71179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org ASSERT_EQ("Put(baz, boo)@102" 72179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org "Delete(box)@101" 73179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org "Put(foo, bar)@100", 74179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org PrintContents(&batch)); 75179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org} 76179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org 77179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.orgTEST(WriteBatchTest, Corruption) { 78179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org WriteBatch batch; 79179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org batch.Put(Slice("foo"), Slice("bar")); 80179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org batch.Delete(Slice("box")); 81179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org WriteBatchInternal::SetSequence(&batch, 200); 82179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org Slice contents = WriteBatchInternal::Contents(&batch); 83179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org WriteBatchInternal::SetContents(&batch, 84179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org Slice(contents.data(),contents.size()-1)); 85179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org ASSERT_EQ("Put(foo, bar)@200" 86179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org "ParseError()", 87179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org PrintContents(&batch)); 88179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org} 89179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org 9013daa9f29c999ee40a257ee0775abee2c78a0ad9sanjay@google.comTEST(WriteBatchTest, Append) { 9113daa9f29c999ee40a257ee0775abee2c78a0ad9sanjay@google.com WriteBatch b1, b2; 9213daa9f29c999ee40a257ee0775abee2c78a0ad9sanjay@google.com WriteBatchInternal::SetSequence(&b1, 200); 9313daa9f29c999ee40a257ee0775abee2c78a0ad9sanjay@google.com WriteBatchInternal::SetSequence(&b2, 300); 9413daa9f29c999ee40a257ee0775abee2c78a0ad9sanjay@google.com WriteBatchInternal::Append(&b1, &b2); 9513daa9f29c999ee40a257ee0775abee2c78a0ad9sanjay@google.com ASSERT_EQ("", 9613daa9f29c999ee40a257ee0775abee2c78a0ad9sanjay@google.com PrintContents(&b1)); 9713daa9f29c999ee40a257ee0775abee2c78a0ad9sanjay@google.com b2.Put("a", "va"); 9813daa9f29c999ee40a257ee0775abee2c78a0ad9sanjay@google.com WriteBatchInternal::Append(&b1, &b2); 9913daa9f29c999ee40a257ee0775abee2c78a0ad9sanjay@google.com ASSERT_EQ("Put(a, va)@200", 10013daa9f29c999ee40a257ee0775abee2c78a0ad9sanjay@google.com PrintContents(&b1)); 10113daa9f29c999ee40a257ee0775abee2c78a0ad9sanjay@google.com b2.Clear(); 10213daa9f29c999ee40a257ee0775abee2c78a0ad9sanjay@google.com b2.Put("b", "vb"); 10313daa9f29c999ee40a257ee0775abee2c78a0ad9sanjay@google.com WriteBatchInternal::Append(&b1, &b2); 10413daa9f29c999ee40a257ee0775abee2c78a0ad9sanjay@google.com ASSERT_EQ("Put(a, va)@200" 10513daa9f29c999ee40a257ee0775abee2c78a0ad9sanjay@google.com "Put(b, vb)@201", 10613daa9f29c999ee40a257ee0775abee2c78a0ad9sanjay@google.com PrintContents(&b1)); 10713daa9f29c999ee40a257ee0775abee2c78a0ad9sanjay@google.com b2.Delete("foo"); 10813daa9f29c999ee40a257ee0775abee2c78a0ad9sanjay@google.com WriteBatchInternal::Append(&b1, &b2); 10913daa9f29c999ee40a257ee0775abee2c78a0ad9sanjay@google.com ASSERT_EQ("Put(a, va)@200" 11013daa9f29c999ee40a257ee0775abee2c78a0ad9sanjay@google.com "Put(b, vb)@202" 11113daa9f29c999ee40a257ee0775abee2c78a0ad9sanjay@google.com "Put(b, vb)@201" 11213daa9f29c999ee40a257ee0775abee2c78a0ad9sanjay@google.com "Delete(foo)@203", 11313daa9f29c999ee40a257ee0775abee2c78a0ad9sanjay@google.com PrintContents(&b1)); 11413daa9f29c999ee40a257ee0775abee2c78a0ad9sanjay@google.com} 11513daa9f29c999ee40a257ee0775abee2c78a0ad9sanjay@google.com 11645b9940be332834440bd5299419f396e38085ebehans@chromium.org} // namespace leveldb 117179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org 118179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.orgint main(int argc, char** argv) { 119179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org return leveldb::test::RunAllTests(); 120179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org} 121