db_inplace_update_test.cc 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
  2. // This source code is licensed under both the GPLv2 (found in the
  3. // COPYING file in the root directory) and Apache 2.0 License
  4. // (found in the LICENSE.Apache file in the root directory).
  5. //
  6. // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
  7. // Use of this source code is governed by a BSD-style license that can be
  8. // found in the LICENSE file. See the AUTHORS file for names of contributors.
  9. #include "db/db_test_util.h"
  10. #include "port/stack_trace.h"
  11. namespace ROCKSDB_NAMESPACE {
  12. class DBTestInPlaceUpdate : public DBTestBase {
  13. public:
  14. DBTestInPlaceUpdate() : DBTestBase("/db_inplace_update_test") {}
  15. };
  16. TEST_F(DBTestInPlaceUpdate, InPlaceUpdate) {
  17. do {
  18. Options options = CurrentOptions();
  19. options.create_if_missing = true;
  20. options.inplace_update_support = true;
  21. options.env = env_;
  22. options.write_buffer_size = 100000;
  23. options.allow_concurrent_memtable_write = false;
  24. Reopen(options);
  25. CreateAndReopenWithCF({"pikachu"}, options);
  26. // Update key with values of smaller size
  27. int numValues = 10;
  28. for (int i = numValues; i > 0; i--) {
  29. std::string value = DummyString(i, 'a');
  30. ASSERT_OK(Put(1, "key", value));
  31. ASSERT_EQ(value, Get(1, "key"));
  32. }
  33. // Only 1 instance for that key.
  34. validateNumberOfEntries(1, 1);
  35. } while (ChangeCompactOptions());
  36. }
  37. TEST_F(DBTestInPlaceUpdate, InPlaceUpdateLargeNewValue) {
  38. do {
  39. Options options = CurrentOptions();
  40. options.create_if_missing = true;
  41. options.inplace_update_support = true;
  42. options.env = env_;
  43. options.write_buffer_size = 100000;
  44. options.allow_concurrent_memtable_write = false;
  45. Reopen(options);
  46. CreateAndReopenWithCF({"pikachu"}, options);
  47. // Update key with values of larger size
  48. int numValues = 10;
  49. for (int i = 0; i < numValues; i++) {
  50. std::string value = DummyString(i, 'a');
  51. ASSERT_OK(Put(1, "key", value));
  52. ASSERT_EQ(value, Get(1, "key"));
  53. }
  54. // All 10 updates exist in the internal iterator
  55. validateNumberOfEntries(numValues, 1);
  56. } while (ChangeCompactOptions());
  57. }
  58. TEST_F(DBTestInPlaceUpdate, InPlaceUpdateCallbackSmallerSize) {
  59. do {
  60. Options options = CurrentOptions();
  61. options.create_if_missing = true;
  62. options.inplace_update_support = true;
  63. options.env = env_;
  64. options.write_buffer_size = 100000;
  65. options.inplace_callback =
  66. ROCKSDB_NAMESPACE::DBTestInPlaceUpdate::updateInPlaceSmallerSize;
  67. options.allow_concurrent_memtable_write = false;
  68. Reopen(options);
  69. CreateAndReopenWithCF({"pikachu"}, options);
  70. // Update key with values of smaller size
  71. int numValues = 10;
  72. ASSERT_OK(Put(1, "key", DummyString(numValues, 'a')));
  73. ASSERT_EQ(DummyString(numValues, 'c'), Get(1, "key"));
  74. for (int i = numValues; i > 0; i--) {
  75. ASSERT_OK(Put(1, "key", DummyString(i, 'a')));
  76. ASSERT_EQ(DummyString(i - 1, 'b'), Get(1, "key"));
  77. }
  78. // Only 1 instance for that key.
  79. validateNumberOfEntries(1, 1);
  80. } while (ChangeCompactOptions());
  81. }
  82. TEST_F(DBTestInPlaceUpdate, InPlaceUpdateCallbackSmallerVarintSize) {
  83. do {
  84. Options options = CurrentOptions();
  85. options.create_if_missing = true;
  86. options.inplace_update_support = true;
  87. options.env = env_;
  88. options.write_buffer_size = 100000;
  89. options.inplace_callback =
  90. ROCKSDB_NAMESPACE::DBTestInPlaceUpdate::updateInPlaceSmallerVarintSize;
  91. options.allow_concurrent_memtable_write = false;
  92. Reopen(options);
  93. CreateAndReopenWithCF({"pikachu"}, options);
  94. // Update key with values of smaller varint size
  95. int numValues = 265;
  96. ASSERT_OK(Put(1, "key", DummyString(numValues, 'a')));
  97. ASSERT_EQ(DummyString(numValues, 'c'), Get(1, "key"));
  98. for (int i = numValues; i > 0; i--) {
  99. ASSERT_OK(Put(1, "key", DummyString(i, 'a')));
  100. ASSERT_EQ(DummyString(1, 'b'), Get(1, "key"));
  101. }
  102. // Only 1 instance for that key.
  103. validateNumberOfEntries(1, 1);
  104. } while (ChangeCompactOptions());
  105. }
  106. TEST_F(DBTestInPlaceUpdate, InPlaceUpdateCallbackLargeNewValue) {
  107. do {
  108. Options options = CurrentOptions();
  109. options.create_if_missing = true;
  110. options.inplace_update_support = true;
  111. options.env = env_;
  112. options.write_buffer_size = 100000;
  113. options.inplace_callback =
  114. ROCKSDB_NAMESPACE::DBTestInPlaceUpdate::updateInPlaceLargerSize;
  115. options.allow_concurrent_memtable_write = false;
  116. Reopen(options);
  117. CreateAndReopenWithCF({"pikachu"}, options);
  118. // Update key with values of larger size
  119. int numValues = 10;
  120. for (int i = 0; i < numValues; i++) {
  121. ASSERT_OK(Put(1, "key", DummyString(i, 'a')));
  122. ASSERT_EQ(DummyString(i, 'c'), Get(1, "key"));
  123. }
  124. // No inplace updates. All updates are puts with new seq number
  125. // All 10 updates exist in the internal iterator
  126. validateNumberOfEntries(numValues, 1);
  127. } while (ChangeCompactOptions());
  128. }
  129. TEST_F(DBTestInPlaceUpdate, InPlaceUpdateCallbackNoAction) {
  130. do {
  131. Options options = CurrentOptions();
  132. options.create_if_missing = true;
  133. options.inplace_update_support = true;
  134. options.env = env_;
  135. options.write_buffer_size = 100000;
  136. options.inplace_callback =
  137. ROCKSDB_NAMESPACE::DBTestInPlaceUpdate::updateInPlaceNoAction;
  138. options.allow_concurrent_memtable_write = false;
  139. Reopen(options);
  140. CreateAndReopenWithCF({"pikachu"}, options);
  141. // Callback function requests no actions from db
  142. ASSERT_OK(Put(1, "key", DummyString(1, 'a')));
  143. ASSERT_EQ(Get(1, "key"), "NOT_FOUND");
  144. } while (ChangeCompactOptions());
  145. }
  146. } // namespace ROCKSDB_NAMESPACE
  147. int main(int argc, char** argv) {
  148. ROCKSDB_NAMESPACE::port::InstallStackTraceHandler();
  149. ::testing::InitGoogleTest(&argc, argv);
  150. return RUN_ALL_TESTS();
  151. }