CernVM-FS  2.13.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
lru_md.h
Go to the documentation of this file.
1 
7 #ifndef CVMFS_LRU_MD_H_
8 #define CVMFS_LRU_MD_H_
9 
10 #ifndef __STDC_FORMAT_MACROS
11 #define __STDC_FORMAT_MACROS
12 #endif
13 
14 #include <stdint.h>
15 
16 #include "crypto/hash.h"
17 #include "directory_entry.h"
18 #include "duplex_fuse.h"
19 #include "lru.h"
20 #include "shortstring.h"
21 #include "util/atomic.h"
22 #include "util/logging.h"
23 #include "util/murmur.hxx"
24 
25 
26 namespace lru {
27 
28 // Hash functions
29 static inline uint32_t hasher_md5(const shash::Md5 &key) {
30  // Don't start with the first bytes, because == is using them as well
31  return (uint32_t) * (reinterpret_cast<const uint32_t *>(key.digest) + 1);
32 }
33 
34 static inline uint32_t hasher_inode(const fuse_ino_t &inode) {
35  return MurmurHash2(&inode, sizeof(inode), 0x07387a4f);
36 }
37 // uint32_t hasher_md5(const shash::Md5 &key);
38 // uint32_t hasher_inode(const fuse_ino_t &inode);
39 
40 
41 class InodeCache : public LruCache<fuse_ino_t, catalog::DirectoryEntry> {
42  public:
43  explicit InodeCache(unsigned int cache_size, perf::Statistics *statistics)
44  : LruCache<fuse_ino_t, catalog::DirectoryEntry>(
45  cache_size, fuse_ino_t(-1), hasher_inode,
46  perf::StatisticsTemplate("inode_cache", statistics)) { }
47 
48  bool Insert(const fuse_ino_t &inode, const catalog::DirectoryEntry &dirent) {
49  LogCvmfs(kLogLru, kLogDebug, "insert inode --> dirent: %lu -> '%s'", inode,
50  dirent.name().c_str());
52  inode, dirent);
53  return result;
54  }
55 
56  bool Lookup(const fuse_ino_t &inode, catalog::DirectoryEntry *dirent,
57  bool update_lru = true) {
59  inode, dirent);
60  LogCvmfs(kLogLru, kLogDebug, "lookup inode --> dirent: %lu (%s)", inode,
61  result ? "hit" : "miss");
62  return result;
63  }
64 
65  void Drop() {
66  LogCvmfs(kLogLru, kLogDebug, "dropping inode cache");
68  }
69 }; // InodeCache
70 
71 
72 class PathCache : public LruCache<fuse_ino_t, PathString> {
73  public:
74  explicit PathCache(unsigned int cache_size, perf::Statistics *statistics)
75  : LruCache<fuse_ino_t, PathString>(
76  cache_size, fuse_ino_t(-1), hasher_inode,
77  perf::StatisticsTemplate("path_cache", statistics)) { }
78 
79  bool Insert(const fuse_ino_t &inode, const PathString &path) {
80  LogCvmfs(kLogLru, kLogDebug, "insert inode --> path %lu -> '%s'", inode,
81  path.c_str());
82  const bool result = LruCache<fuse_ino_t, PathString>::Insert(inode, path);
83  return result;
84  }
85 
86  bool Lookup(const fuse_ino_t &inode, PathString *path,
87  bool update_lru = true) {
88  const bool found = LruCache<fuse_ino_t, PathString>::Lookup(inode, path);
89  LogCvmfs(kLogLru, kLogDebug, "lookup inode --> path: %lu (%s)", inode,
90  found ? "hit" : "miss");
91  return found;
92  }
93 
94  void Drop() {
95  LogCvmfs(kLogLru, kLogDebug, "dropping path cache");
97  }
98 }; // PathCache
99 
100 
101 class Md5PathCache : public LruCache<shash::Md5, catalog::DirectoryEntry> {
102  public:
103  explicit Md5PathCache(unsigned int cache_size, perf::Statistics *statistics)
104  : LruCache<shash::Md5, catalog::DirectoryEntry>(
105  cache_size, shash::Md5(shash::AsciiPtr("!")), hasher_md5,
106  perf::StatisticsTemplate("md5_path_cache", statistics)) {
108  }
109 
110  bool Insert(const shash::Md5 &hash, const catalog::DirectoryEntry &dirent) {
111  LogCvmfs(kLogLru, kLogDebug, "insert md5 --> dirent: %s -> '%s'",
112  hash.ToString().c_str(), dirent.name().c_str());
114  hash, dirent);
115  return result;
116  }
117 
118  bool InsertNegative(const shash::Md5 &hash) {
119  const bool result = Insert(hash, dirent_negative_);
120  if (result)
122  return result;
123  }
124 
125  bool Lookup(const shash::Md5 &hash, catalog::DirectoryEntry *dirent,
126  bool update_lru = true) {
128  hash, dirent);
129  LogCvmfs(kLogLru, kLogDebug, "lookup md5 --> dirent: %s (%s)",
130  hash.ToString().c_str(), result ? "hit" : "miss");
131  return result;
132  }
133 
134  bool Forget(const shash::Md5 &hash) {
135  LogCvmfs(kLogLru, kLogDebug, "forget md5: %s", hash.ToString().c_str());
137  }
138 
139  void Drop() {
140  LogCvmfs(kLogLru, kLogDebug, "dropping md5path cache");
142  }
143 
144  private:
146 }; // Md5PathCache
147 
148 } // namespace lru
149 
150 #endif // CVMFS_LRU_MD_H_
bool InsertNegative(const shash::Md5 &hash)
Definition: lru_md.h:118
bool Forget(const shash::Md5 &hash)
Definition: lru_md.h:134
bool Insert(const fuse_ino_t &inode, const catalog::DirectoryEntry &dirent)
Definition: lru_md.h:48
std::string ToString(const bool with_suffix=false) const
Definition: hash.h:241
virtual bool Insert(const Key &key, const Value &value)
Definition: lru.h:562
bool Lookup(const shash::Md5 &hash, catalog::DirectoryEntry *dirent, bool update_lru=true)
Definition: lru_md.h:125
bool Lookup(const fuse_ino_t &inode, catalog::DirectoryEntry *dirent, bool update_lru=true)
Definition: lru_md.h:56
static uint32_t hasher_md5(const shash::Md5 &key)
Definition: lru_md.h:29
unsigned char digest[digest_size_]
Definition: hash.h:121
bool Lookup(const fuse_ino_t &inode, PathString *path, bool update_lru=true)
Definition: lru_md.h:86
NameString name() const
perf::Counter * n_insert_negative
Definition: lru.h:68
catalog::DirectoryEntry dirent_negative_
Definition: lru_md.h:145
void Drop()
Definition: lru_md.h:65
void Inc(class Counter *counter)
Definition: statistics.h:50
void Drop()
Definition: lru_md.h:94
static uint32_t hasher_inode(const fuse_ino_t &inode)
Definition: lru_md.h:34
Md5PathCache(unsigned int cache_size, perf::Statistics *statistics)
Definition: lru_md.h:103
virtual bool Lookup(const Key &key, Value *value, bool update_lru=true)
Definition: lru.h:646
virtual bool Forget(const Key &key)
Definition: lru.h:675
PathCache(unsigned int cache_size, perf::Statistics *statistics)
Definition: lru_md.h:74
virtual void Drop()
Definition: lru.h:703
bool Insert(const fuse_ino_t &inode, const PathString &path)
Definition: lru_md.h:79
InodeCache(unsigned int cache_size, perf::Statistics *statistics)
Definition: lru_md.h:43
const char * c_str() const
Definition: shortstring.h:143
bool Insert(const shash::Md5 &hash, const catalog::DirectoryEntry &dirent)
Definition: lru_md.h:110
uint32_t MurmurHash2(const void *key, int len, uint32_t seed)
Definition: murmur.hxx:23
CVMFS_EXPORT void LogCvmfs(const LogSource source, const int mask, const char *format,...)
Definition: logging.cc:545