db_with_timestamp_test_util.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. #pragma once
  10. #include "db/db_test_util.h"
  11. #include "port/stack_trace.h"
  12. #include "test_util/testutil.h"
  13. namespace ROCKSDB_NAMESPACE {
  14. class DBBasicTestWithTimestampBase : public DBTestBase {
  15. public:
  16. explicit DBBasicTestWithTimestampBase(const std::string& dbname)
  17. : DBTestBase(dbname, /*env_do_fsync=*/true) {}
  18. protected:
  19. static std::string Key1(uint64_t k);
  20. static std::string KeyWithPrefix(std::string prefix, uint64_t k);
  21. static std::vector<Slice> ConvertStrToSlice(
  22. std::vector<std::string>& strings);
  23. class TestComparator : public Comparator {
  24. private:
  25. const Comparator* cmp_without_ts_;
  26. public:
  27. explicit TestComparator(size_t ts_sz)
  28. : Comparator(ts_sz), cmp_without_ts_(nullptr) {
  29. cmp_without_ts_ = BytewiseComparator();
  30. }
  31. const char* Name() const override { return "TestComparator"; }
  32. void FindShortSuccessor(std::string*) const override {}
  33. void FindShortestSeparator(std::string*, const Slice&) const override {}
  34. int Compare(const Slice& a, const Slice& b) const override {
  35. int r = CompareWithoutTimestamp(a, b);
  36. if (r != 0 || 0 == timestamp_size()) {
  37. return r;
  38. }
  39. return -CompareTimestamp(
  40. Slice(a.data() + a.size() - timestamp_size(), timestamp_size()),
  41. Slice(b.data() + b.size() - timestamp_size(), timestamp_size()));
  42. }
  43. using Comparator::CompareWithoutTimestamp;
  44. int CompareWithoutTimestamp(const Slice& a, bool a_has_ts, const Slice& b,
  45. bool b_has_ts) const override {
  46. if (a_has_ts) {
  47. assert(a.size() >= timestamp_size());
  48. }
  49. if (b_has_ts) {
  50. assert(b.size() >= timestamp_size());
  51. }
  52. Slice lhs = a_has_ts ? StripTimestampFromUserKey(a, timestamp_size()) : a;
  53. Slice rhs = b_has_ts ? StripTimestampFromUserKey(b, timestamp_size()) : b;
  54. return cmp_without_ts_->Compare(lhs, rhs);
  55. }
  56. int CompareTimestamp(const Slice& ts1, const Slice& ts2) const override {
  57. if (!ts1.data() && !ts2.data()) {
  58. return 0;
  59. } else if (ts1.data() && !ts2.data()) {
  60. return 1;
  61. } else if (!ts1.data() && ts2.data()) {
  62. return -1;
  63. }
  64. assert(ts1.size() == ts2.size());
  65. uint64_t low1 = 0;
  66. uint64_t low2 = 0;
  67. uint64_t high1 = 0;
  68. uint64_t high2 = 0;
  69. const size_t kSize = ts1.size();
  70. std::unique_ptr<char[]> ts1_buf(new char[kSize]);
  71. memcpy(ts1_buf.get(), ts1.data(), ts1.size());
  72. std::unique_ptr<char[]> ts2_buf(new char[kSize]);
  73. memcpy(ts2_buf.get(), ts2.data(), ts2.size());
  74. Slice ts1_copy = Slice(ts1_buf.get(), kSize);
  75. Slice ts2_copy = Slice(ts2_buf.get(), kSize);
  76. auto* ptr1 = const_cast<Slice*>(&ts1_copy);
  77. auto* ptr2 = const_cast<Slice*>(&ts2_copy);
  78. if (!GetFixed64(ptr1, &low1) || !GetFixed64(ptr1, &high1) ||
  79. !GetFixed64(ptr2, &low2) || !GetFixed64(ptr2, &high2)) {
  80. assert(false);
  81. }
  82. if (high1 < high2) {
  83. return -1;
  84. } else if (high1 > high2) {
  85. return 1;
  86. }
  87. if (low1 < low2) {
  88. return -1;
  89. } else if (low1 > low2) {
  90. return 1;
  91. }
  92. return 0;
  93. }
  94. };
  95. std::string Timestamp(uint64_t low, uint64_t high);
  96. void CheckIterUserEntry(const Iterator* it, const Slice& expected_key,
  97. ValueType expected_value_type,
  98. const Slice& expected_value,
  99. const Slice& expected_ts) const;
  100. void CheckIterEntry(const Iterator* it, const Slice& expected_ukey,
  101. SequenceNumber expected_seq, ValueType expected_val_type,
  102. const Slice& expected_value,
  103. const Slice& expected_ts) const;
  104. void CheckIterEntry(const Iterator* it, const Slice& expected_ukey,
  105. ValueType expected_val_type, const Slice& expected_value,
  106. const Slice& expected_ts) const;
  107. };
  108. } // namespace ROCKSDB_NAMESPACE