in_memory_stats_history.cc 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. #include "monitoring/in_memory_stats_history.h"
  9. #include "db/db_impl/db_impl.h"
  10. namespace ROCKSDB_NAMESPACE {
  11. InMemoryStatsHistoryIterator::~InMemoryStatsHistoryIterator() {}
  12. bool InMemoryStatsHistoryIterator::Valid() const { return valid_; }
  13. Status InMemoryStatsHistoryIterator::status() const { return status_; }
  14. // Because of garbage collection, the next stats snapshot may or may not be
  15. // right after the current one. When reading from DBImpl::stats_history_, this
  16. // call will be protected by DB Mutex so it will not return partial or
  17. // corrupted results.
  18. void InMemoryStatsHistoryIterator::Next() {
  19. // increment start_time by 1 to avoid infinite loop
  20. AdvanceIteratorByTime(GetStatsTime() + 1, end_time_);
  21. }
  22. uint64_t InMemoryStatsHistoryIterator::GetStatsTime() const { return time_; }
  23. const std::map<std::string, uint64_t>&
  24. InMemoryStatsHistoryIterator::GetStatsMap() const {
  25. return stats_map_;
  26. }
  27. // advance the iterator to the next time between [start_time, end_time)
  28. // if success, update time_ and stats_map_ with new_time and stats_map
  29. void InMemoryStatsHistoryIterator::AdvanceIteratorByTime(uint64_t start_time,
  30. uint64_t end_time) {
  31. // try to find next entry in stats_history_ map
  32. if (db_impl_ != nullptr) {
  33. valid_ =
  34. db_impl_->FindStatsByTime(start_time, end_time, &time_, &stats_map_);
  35. } else {
  36. valid_ = false;
  37. }
  38. }
  39. } // namespace ROCKSDB_NAMESPACE