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