小组成员: 曹可心-10223903406 朴祉燕-10224602413
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

110 lines
3.2 KiB

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