snapshot_checker.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. #pragma once
  6. #include "rocksdb/types.h"
  7. namespace ROCKSDB_NAMESPACE {
  8. enum class SnapshotCheckerResult : int {
  9. kInSnapshot = 0,
  10. kNotInSnapshot = 1,
  11. // In case snapshot is released and the checker has no clue whether
  12. // the given sequence is visible to the snapshot.
  13. kSnapshotReleased = 2,
  14. };
  15. // Callback class that control GC of duplicate keys in flush/compaction.
  16. class SnapshotChecker {
  17. public:
  18. virtual ~SnapshotChecker() {}
  19. virtual SnapshotCheckerResult CheckInSnapshot(
  20. SequenceNumber sequence, SequenceNumber snapshot_sequence) const = 0;
  21. };
  22. class DisableGCSnapshotChecker : public SnapshotChecker {
  23. public:
  24. virtual ~DisableGCSnapshotChecker() {}
  25. virtual SnapshotCheckerResult CheckInSnapshot(
  26. SequenceNumber /*sequence*/,
  27. SequenceNumber /*snapshot_sequence*/) const override {
  28. // By returning kNotInSnapshot, we prevent all the values from being GCed
  29. return SnapshotCheckerResult::kNotInSnapshot;
  30. }
  31. static DisableGCSnapshotChecker* Instance() { return &instance_; }
  32. protected:
  33. static DisableGCSnapshotChecker instance_;
  34. explicit DisableGCSnapshotChecker() {}
  35. };
  36. class WritePreparedTxnDB;
  37. // Callback class created by WritePreparedTxnDB to check if a key
  38. // is visible by a snapshot.
  39. class WritePreparedSnapshotChecker : public SnapshotChecker {
  40. public:
  41. explicit WritePreparedSnapshotChecker(WritePreparedTxnDB* txn_db);
  42. virtual ~WritePreparedSnapshotChecker() {}
  43. virtual SnapshotCheckerResult CheckInSnapshot(
  44. SequenceNumber sequence, SequenceNumber snapshot_sequence) const override;
  45. private:
  46. #ifndef ROCKSDB_LITE
  47. const WritePreparedTxnDB* const txn_db_;
  48. #endif // !ROCKSDB_LITE
  49. };
  50. } // namespace ROCKSDB_NAMESPACE