persistent_stats_history.h 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
  6. // Use of this source code is governed by a BSD-style license that can be
  7. // found in the LICENSE file. See the AUTHORS file for names of contributors.
  8. #pragma once
  9. #include "db/db_impl/db_impl.h"
  10. #include "rocksdb/stats_history.h"
  11. namespace ROCKSDB_NAMESPACE {
  12. extern const std::string kFormatVersionKeyString;
  13. extern const std::string kCompatibleVersionKeyString;
  14. extern const uint64_t kStatsCFCurrentFormatVersion;
  15. extern const uint64_t kStatsCFCompatibleFormatVersion;
  16. enum StatsVersionKeyType : uint32_t {
  17. kFormatVersion = 1,
  18. kCompatibleVersion = 2,
  19. kKeyTypeMax = 3
  20. };
  21. // Read the version number from persitent stats cf depending on type provided
  22. // stores the version number in `*version_number`
  23. // returns Status::OK() on success, or other status code on failure
  24. Status DecodePersistentStatsVersionNumber(DBImpl* db, StatsVersionKeyType type,
  25. uint64_t* version_number);
  26. // Encode timestamp and stats key into buf
  27. // Format: timestamp(10 digit) + '#' + key
  28. // Total length of encoded key will be capped at 100 bytes
  29. int EncodePersistentStatsKey(uint64_t timestamp, const std::string& key,
  30. int size, char* buf);
  31. void OptimizeForPersistentStats(ColumnFamilyOptions* cfo);
  32. class PersistentStatsHistoryIterator final : public StatsHistoryIterator {
  33. public:
  34. PersistentStatsHistoryIterator(uint64_t start_time, uint64_t end_time,
  35. DBImpl* db_impl)
  36. : time_(0),
  37. start_time_(start_time),
  38. end_time_(end_time),
  39. valid_(true),
  40. db_impl_(db_impl) {
  41. AdvanceIteratorByTime(start_time_, end_time_);
  42. }
  43. ~PersistentStatsHistoryIterator() override;
  44. bool Valid() const override;
  45. Status status() const override;
  46. void Next() override;
  47. uint64_t GetStatsTime() const override;
  48. const std::map<std::string, uint64_t>& GetStatsMap() const override;
  49. private:
  50. // advance the iterator to the next stats history record with timestamp
  51. // between [start_time, end_time)
  52. void AdvanceIteratorByTime(uint64_t start_time, uint64_t end_time);
  53. // No copying allowed
  54. PersistentStatsHistoryIterator(const PersistentStatsHistoryIterator&) =
  55. delete;
  56. void operator=(const PersistentStatsHistoryIterator&) = delete;
  57. PersistentStatsHistoryIterator(PersistentStatsHistoryIterator&&) = delete;
  58. PersistentStatsHistoryIterator& operator=(PersistentStatsHistoryIterator&&) =
  59. delete;
  60. uint64_t time_;
  61. uint64_t start_time_;
  62. uint64_t end_time_;
  63. std::map<std::string, uint64_t> stats_map_;
  64. Status status_;
  65. bool valid_;
  66. DBImpl* db_impl_;
  67. };
  68. } // namespace ROCKSDB_NAMESPACE