db_inplace_update_test.cc 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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()
  15. : DBTestBase("db_inplace_update_test", /*env_do_fsync=*/true) {}
  16. };
  17. TEST_F(DBTestInPlaceUpdate, InPlaceUpdate) {
  18. do {
  19. Options options = CurrentOptions();
  20. options.create_if_missing = true;
  21. options.inplace_update_support = true;
  22. options.env = env_;
  23. options.write_buffer_size = 100000;
  24. options.allow_concurrent_memtable_write = false;
  25. Reopen(options);
  26. CreateAndReopenWithCF({"pikachu"}, options);
  27. // Update key with values of smaller size
  28. int numValues = 10;
  29. for (int i = numValues; i > 0; i--) {
  30. std::string value = DummyString(i, 'a');
  31. ASSERT_OK(Put(1, "key", value));
  32. ASSERT_EQ(value, Get(1, "key"));
  33. }
  34. // Only 1 instance for that key.
  35. validateNumberOfEntries(1, 1);
  36. } while (ChangeCompactOptions());
  37. }
  38. TEST_F(DBTestInPlaceUpdate, InPlaceUpdateLargeNewValue) {
  39. do {
  40. Options options = CurrentOptions();
  41. options.create_if_missing = true;
  42. options.inplace_update_support = true;
  43. options.env = env_;
  44. options.write_buffer_size = 100000;
  45. options.allow_concurrent_memtable_write = false;
  46. Reopen(options);
  47. CreateAndReopenWithCF({"pikachu"}, options);
  48. // Update key with values of larger size
  49. int numValues = 10;
  50. for (int i = 0; i < numValues; i++) {
  51. std::string value = DummyString(i, 'a');
  52. ASSERT_OK(Put(1, "key", value));
  53. ASSERT_EQ(value, Get(1, "key"));
  54. }
  55. // All 10 updates exist in the internal iterator
  56. validateNumberOfEntries(numValues, 1);
  57. } while (ChangeCompactOptions());
  58. }
  59. TEST_F(DBTestInPlaceUpdate, InPlaceUpdateEntitySmallerNewValue) {
  60. do {
  61. Options options = CurrentOptions();
  62. options.create_if_missing = true;
  63. options.inplace_update_support = true;
  64. options.env = env_;
  65. options.allow_concurrent_memtable_write = false;
  66. Reopen(options);
  67. CreateAndReopenWithCF({"pikachu"}, options);
  68. // Update key with values of smaller size
  69. constexpr int num_values = 10;
  70. for (int i = num_values; i > 0; --i) {
  71. constexpr char key[] = "key";
  72. const std::string value = DummyString(i, 'a');
  73. WideColumns wide_columns{{"attr", value}};
  74. ASSERT_OK(db_->PutEntity(WriteOptions(), handles_[1], key, wide_columns));
  75. // TODO: use Get to check entity once it's supported
  76. }
  77. // Only 1 instance for that key.
  78. validateNumberOfEntries(1, 1);
  79. } while (ChangeCompactOptions());
  80. }
  81. TEST_F(DBTestInPlaceUpdate, InPlaceUpdateEntityLargerNewValue) {
  82. do {
  83. Options options = CurrentOptions();
  84. options.create_if_missing = true;
  85. options.inplace_update_support = true;
  86. options.env = env_;
  87. options.allow_concurrent_memtable_write = false;
  88. Reopen(options);
  89. CreateAndReopenWithCF({"pikachu"}, options);
  90. // Update key with values of larger size
  91. constexpr int num_values = 10;
  92. for (int i = 0; i < num_values; ++i) {
  93. constexpr char key[] = "key";
  94. const std::string value = DummyString(i, 'a');
  95. WideColumns wide_columns{{"attr", value}};
  96. ASSERT_OK(db_->PutEntity(WriteOptions(), handles_[1], key, wide_columns));
  97. // TODO: use Get to check entity once it's supported
  98. }
  99. // All 10 updates exist in the internal iterator
  100. validateNumberOfEntries(num_values, 1);
  101. } while (ChangeCompactOptions());
  102. }
  103. TEST_F(DBTestInPlaceUpdate, InPlaceUpdateCallbackSmallerSize) {
  104. do {
  105. Options options = CurrentOptions();
  106. options.create_if_missing = true;
  107. options.inplace_update_support = true;
  108. options.env = env_;
  109. options.write_buffer_size = 100000;
  110. options.inplace_callback =
  111. ROCKSDB_NAMESPACE::DBTestInPlaceUpdate::updateInPlaceSmallerSize;
  112. options.allow_concurrent_memtable_write = false;
  113. Reopen(options);
  114. CreateAndReopenWithCF({"pikachu"}, options);
  115. // Update key with values of smaller size
  116. int numValues = 10;
  117. ASSERT_OK(Put(1, "key", DummyString(numValues, 'a')));
  118. ASSERT_EQ(DummyString(numValues, 'c'), Get(1, "key"));
  119. for (int i = numValues; i > 0; i--) {
  120. ASSERT_OK(Put(1, "key", DummyString(i, 'a')));
  121. ASSERT_EQ(DummyString(i - 1, 'b'), Get(1, "key"));
  122. }
  123. // Only 1 instance for that key.
  124. validateNumberOfEntries(1, 1);
  125. } while (ChangeCompactOptions());
  126. }
  127. TEST_F(DBTestInPlaceUpdate, InPlaceUpdateCallbackSmallerVarintSize) {
  128. do {
  129. Options options = CurrentOptions();
  130. options.create_if_missing = true;
  131. options.inplace_update_support = true;
  132. options.env = env_;
  133. options.write_buffer_size = 100000;
  134. options.inplace_callback =
  135. ROCKSDB_NAMESPACE::DBTestInPlaceUpdate::updateInPlaceSmallerVarintSize;
  136. options.allow_concurrent_memtable_write = false;
  137. Reopen(options);
  138. CreateAndReopenWithCF({"pikachu"}, options);
  139. // Update key with values of smaller varint size
  140. int numValues = 265;
  141. ASSERT_OK(Put(1, "key", DummyString(numValues, 'a')));
  142. ASSERT_EQ(DummyString(numValues, 'c'), Get(1, "key"));
  143. for (int i = numValues; i > 0; i--) {
  144. ASSERT_OK(Put(1, "key", DummyString(i, 'a')));
  145. ASSERT_EQ(DummyString(1, 'b'), Get(1, "key"));
  146. }
  147. // Only 1 instance for that key.
  148. validateNumberOfEntries(1, 1);
  149. } while (ChangeCompactOptions());
  150. }
  151. TEST_F(DBTestInPlaceUpdate, InPlaceUpdateCallbackLargeNewValue) {
  152. do {
  153. Options options = CurrentOptions();
  154. options.create_if_missing = true;
  155. options.inplace_update_support = true;
  156. options.env = env_;
  157. options.write_buffer_size = 100000;
  158. options.inplace_callback =
  159. ROCKSDB_NAMESPACE::DBTestInPlaceUpdate::updateInPlaceLargerSize;
  160. options.allow_concurrent_memtable_write = false;
  161. Reopen(options);
  162. CreateAndReopenWithCF({"pikachu"}, options);
  163. // Update key with values of larger size
  164. int numValues = 10;
  165. for (int i = 0; i < numValues; i++) {
  166. ASSERT_OK(Put(1, "key", DummyString(i, 'a')));
  167. ASSERT_EQ(DummyString(i, 'c'), Get(1, "key"));
  168. }
  169. // No inplace updates. All updates are puts with new seq number
  170. // All 10 updates exist in the internal iterator
  171. validateNumberOfEntries(numValues, 1);
  172. } while (ChangeCompactOptions());
  173. }
  174. TEST_F(DBTestInPlaceUpdate, InPlaceUpdateCallbackNoAction) {
  175. do {
  176. Options options = CurrentOptions();
  177. options.create_if_missing = true;
  178. options.inplace_update_support = true;
  179. options.env = env_;
  180. options.write_buffer_size = 100000;
  181. options.inplace_callback =
  182. ROCKSDB_NAMESPACE::DBTestInPlaceUpdate::updateInPlaceNoAction;
  183. options.allow_concurrent_memtable_write = false;
  184. Reopen(options);
  185. CreateAndReopenWithCF({"pikachu"}, options);
  186. // Callback function requests no actions from db
  187. ASSERT_OK(Put(1, "key", DummyString(1, 'a')));
  188. ASSERT_EQ(Get(1, "key"), "NOT_FOUND");
  189. } while (ChangeCompactOptions());
  190. }
  191. TEST_F(DBTestInPlaceUpdate, InPlaceUpdateAndSnapshot) {
  192. do {
  193. Options options = CurrentOptions();
  194. options.create_if_missing = true;
  195. options.inplace_update_support = true;
  196. options.env = env_;
  197. options.write_buffer_size = 100000;
  198. options.allow_concurrent_memtable_write = false;
  199. Reopen(options);
  200. CreateAndReopenWithCF({"pikachu"}, options);
  201. // Update key with values of smaller size, and
  202. // run GetSnapshot and ReleaseSnapshot
  203. int numValues = 2;
  204. for (int i = numValues; i > 0; i--) {
  205. const Snapshot* s = db_->GetSnapshot();
  206. ASSERT_EQ(nullptr, s);
  207. std::string value = DummyString(i, 'a');
  208. ASSERT_OK(Put(1, "key", value));
  209. ASSERT_EQ(value, Get(1, "key"));
  210. // release s (nullptr)
  211. db_->ReleaseSnapshot(s);
  212. }
  213. // Only 1 instance for that key.
  214. validateNumberOfEntries(1, 1);
  215. } while (ChangeCompactOptions());
  216. }
  217. } // namespace ROCKSDB_NAMESPACE
  218. int main(int argc, char** argv) {
  219. ROCKSDB_NAMESPACE::port::InstallStackTraceHandler();
  220. ::testing::InitGoogleTest(&argc, argv);
  221. return RUN_ALL_TESTS();
  222. }