CernVM-FS  2.9.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
catalog_counters.h
Go to the documentation of this file.
1 
5 #ifndef CVMFS_CATALOG_COUNTERS_H_
6 #define CVMFS_CATALOG_COUNTERS_H_
7 
8 #include <gtest/gtest_prod.h>
9 #include <stdint.h>
10 
11 #include <map>
12 #include <string>
13 
14 namespace swissknife {
15 class CommandCheck;
16 }
17 
18 namespace catalog {
19 
20 class DirectoryEntry;
21 class CatalogDatabase;
22 
23 struct LegacyMode {
24  enum Type { // TODO(rmeusel): C++11 typed enum
30  };
31 };
32 
33 // FieldT is either int64_t (DeltaCounters) or uint64_t (Counters)
34 template<typename FieldT>
37  FRIEND_TEST(T_CatalogCounters, FieldsCombinations);
38  FRIEND_TEST(T_CatalogCounters, FieldsMap);
39 
40  protected:
41  typedef std::map<std::string, const FieldT*> FieldsMap;
42  struct Fields {
44  : regular_files(0)
45  , symlinks(0)
46  , specials(0)
47  , directories(0)
48  , nested_catalogs(0)
49  , chunked_files(0)
50  , file_chunks(0)
51  , file_size(0)
53  , xattrs(0)
54  , externals(0)
55  , external_file_size(0) { }
56 
57  // typname U is another TreeCountersBase (eg: add DeltaCounters to Counters)
58  template<typename U>
59  void Add(const U &other) {
60  Combine<U, 1>(other);
61  }
62 
63  template<typename U>
64  void Subtract(const U &other) {
65  Combine<U, -1>(other);
66  }
67 
68  template<typename U, int factor>
69  void Combine(const U &other) {
70  regular_files += factor * other.regular_files;
71  symlinks += factor * other.symlinks;
72  specials += factor * other.specials;
73  directories += factor * other.directories;
74  nested_catalogs += factor * other.nested_catalogs;
75  chunked_files += factor * other.chunked_files;
76  file_chunks += factor * other.file_chunks;
77  file_size += factor * other.file_size;
78  chunked_file_size += factor * other.chunked_file_size;
79  xattrs += factor * other.xattrs;
80  externals += factor * other.externals;
81  external_file_size += factor * other.external_file_size;
82  }
83 
84  void FillFieldsMap(const std::string &prefix, FieldsMap *map) const {
85  (*map)[prefix + "regular"] = &regular_files;
86  (*map)[prefix + "symlink"] = &symlinks;
87  (*map)[prefix + "special"] = &specials;
88  (*map)[prefix + "dir"] = &directories;
89  (*map)[prefix + "nested"] = &nested_catalogs;
90  (*map)[prefix + "chunked"] = &chunked_files;
91  (*map)[prefix + "chunks"] = &file_chunks;
92  (*map)[prefix + "file_size"] = &file_size;
93  (*map)[prefix + "chunked_size"] = &chunked_file_size;
94  (*map)[prefix + "xattr"] = &xattrs;
95  (*map)[prefix + "external"] = &externals;
96  (*map)[prefix + "external_file_size"] = &external_file_size;
97  }
98 
99  FieldT regular_files;
100  FieldT symlinks;
101  FieldT specials;
102  FieldT directories;
105  FieldT file_chunks;
106  FieldT file_size;
108  FieldT xattrs;
109  FieldT externals;
111  };
112 
113  public:
114  FieldT Get(const std::string &key) const;
115  bool ReadFromDatabase(const CatalogDatabase &database,
116  const LegacyMode::Type legacy = LegacyMode::kNoLegacy);
117  bool WriteToDatabase(const CatalogDatabase &database) const;
118  bool InsertIntoDatabase(const CatalogDatabase &database) const;
119 
120  void SetZero();
121 
122  std::map<std::string, FieldT> GetValues() const;
123  std::string GetCsvMap() const;
124 
125  protected:
126  FieldsMap GetFieldsMap() const;
127 
128  public:
129  Fields self;
130  Fields subtree;
131 };
132 
133 
134 typedef int64_t DeltaCounters_t;
135 class DeltaCounters : public TreeCountersBase<DeltaCounters_t> {
136  friend class Counters;
137 
138  public:
139  void PopulateToParent(DeltaCounters *parent) const;
140  void RemoveFromSubtree(const DeltaCounters &child);
141  void Increment(const DirectoryEntry &dirent) { ApplyDelta(dirent, 1); }
142  void Decrement(const DirectoryEntry &dirent) { ApplyDelta(dirent, -1); }
143 
144  private:
145  void ApplyDelta(const DirectoryEntry &dirent, const int delta);
146 };
147 
148 
149 typedef uint64_t Counters_t;
150 class Counters : public TreeCountersBase<Counters_t> {
151  public:
152  static DeltaCounters Diff(const Counters &from, const Counters &to);
153 
154  void ApplyDelta(const DeltaCounters &delta);
155  void AddAsSubtree(DeltaCounters *delta) const;
156  void MergeIntoParent(DeltaCounters *parent_delta) const;
157  Counters_t GetSelfEntries() const;
159  Counters_t GetAllEntries() const;
160 };
161 
162 } // namespace catalog
163 
164 #include "catalog_counters_impl.h"
165 
166 #endif // CVMFS_CATALOG_COUNTERS_H_
bool InsertIntoDatabase(const CatalogDatabase &database) const
void ApplyDelta(const DeltaCounters &delta)
static DeltaCounters Diff(const Counters &from, const Counters &to)
Counters_t GetSelfEntries() const
std::map< std::string, const DeltaCounters_t * > FieldsMap
void Increment(const DirectoryEntry &dirent)
void AddAsSubtree(DeltaCounters *delta) const
bool WriteToDatabase(const CatalogDatabase &database) const
std::map< std::string, FieldT > GetValues() const
void FillFieldsMap(const std::string &prefix, FieldsMap *map) const
uint64_t Counters_t
void PopulateToParent(DeltaCounters *parent) const
void Decrement(const DirectoryEntry &dirent)
Counters_t GetSubtreeEntries() const
bool ReadFromDatabase(const CatalogDatabase &database, const LegacyMode::Type legacy=LegacyMode::kNoLegacy)
void ApplyDelta(const DirectoryEntry &dirent, const int delta)
int64_t DeltaCounters_t
FRIEND_TEST(T_CatalogCounters, FieldsCombinations)
FieldT Get(const std::string &key) const
std::string GetCsvMap() const
Counters_t GetAllEntries() const
void MergeIntoParent(DeltaCounters *parent_delta) const
void RemoveFromSubtree(const DeltaCounters &child)