BuildCache
local_cache.hpp
1 //--------------------------------------------------------------------------------------------------
2 // Copyright (c) 2019 Marcus Geelnard
3 //
4 // This software is provided 'as-is', without any express or implied warranty. In no event will the
5 // authors be held liable for any damages arising from the use of this software.
6 //
7 // Permission is granted to anyone to use this software for any purpose, including commercial
8 // applications, and to alter it and redistribute it freely, subject to the following restrictions:
9 //
10 // 1. The origin of this software must not be misrepresented; you must not claim that you wrote
11 // the original software. If you use this software in a product, an acknowledgment in the
12 // product documentation would be appreciated but is not required.
13 //
14 // 2. Altered source versions must be plainly marked as such, and must not be misrepresented as
15 // being the original software.
16 //
17 // 3. This notice may not be removed or altered from any source distribution.
18 //--------------------------------------------------------------------------------------------------
19 
20 #ifndef BUILDCACHE_LOCAL_CACHE_HPP_
21 #define BUILDCACHE_LOCAL_CACHE_HPP_
22 
23 #include <base/file_lock.hpp>
24 #include <base/file_utils.hpp>
25 #include <cache/cache_entry.hpp>
26 #include <cache/cache_stats.hpp>
27 #include <cache/direct_mode_manifest.hpp>
28 #include <cache/expected_file.hpp>
29 
30 #include <map>
31 #include <string>
32 #include <utility>
33 
34 namespace bcache {
35 
37 public:
40  local_cache_t();
41 
44 
46  void clear();
47 
49  void show_stats();
50 
52  void zero_stats();
53 
57  void add_direct(const std::string& direct_hash, const direct_mode_manifest_t& manifest);
58 
62  direct_mode_manifest_t lookup_direct(const std::string& direct_hash);
63 
70  void add(const std::string& hash,
71  const cache_entry_t& entry,
72  const std::map<std::string, expected_file_t>& expected_files,
73  const bool allow_hard_links);
74 
79  std::pair<cache_entry_t, file::file_lock_t> lookup(const std::string& hash);
80 
87  void get_file(const std::string& hash,
88  const std::string& source_id,
89  const std::string& target_path,
90  const bool is_compressed,
91  const bool allow_hard_links);
92 
99  bool update_stats(const std::string& hash, const cache_stats_t& delta) const noexcept;
100 
101 private:
102  std::string hash_to_cache_entry_path(const std::string& hash) const;
103  std::string get_cache_files_folder() const;
104 
105  void perform_housekeeping();
106 };
107 
108 } // namespace bcache
109 
110 #endif // BUILDCACHE_LOCAL_CACHE_HPP_
bcache::local_cache_t::show_stats
void show_stats()
Show cache statistics (print to standard out).
Definition: local_cache.cpp:244
bcache::local_cache_t::clear
void clear()
Clear all entries in the cache.
Definition: local_cache.cpp:213
bcache::local_cache_t::lookup
std::pair< cache_entry_t, file::file_lock_t > lookup(const std::string &hash)
Check if an entry exists in the cache.
Definition: local_cache.cpp:427
bcache
The top level namespace.
Definition: compressor.cpp:34
bcache::local_cache_t::lookup_direct
direct_mode_manifest_t lookup_direct(const std::string &direct_hash)
Check if a direct mode entry exists in the cache.
Definition: local_cache.cpp:341
bcache::direct_mode_manifest_t
A direct mode cache manifest.
Definition: direct_mode_manifest.hpp:28
bcache::local_cache_t::get_file
void get_file(const std::string &hash, const std::string &source_id, const std::string &target_path, const bool is_compressed, const bool allow_hard_links)
Copy a cached file to the local file system.
Definition: local_cache.cpp:489
bcache::local_cache_t::local_cache_t
local_cache_t()
Initialize the cache object.
Definition: local_cache.cpp:195
bcache::local_cache_t::~local_cache_t
~local_cache_t()
De-initialzie the cache object.
Definition: local_cache.cpp:199
bcache::local_cache_t::update_stats
bool update_stats(const std::string &hash, const cache_stats_t &delta) const noexcept
Update statistics associated with the given entry.
Definition: local_cache.cpp:458
bcache::cache_stats_t
Definition: cache_stats.hpp:29
bcache::local_cache_t
Definition: local_cache.hpp:36
bcache::local_cache_t::add
void add(const std::string &hash, const cache_entry_t &entry, const std::map< std::string, expected_file_t > &expected_files, const bool allow_hard_links)
Add a set of files to the cache.
Definition: local_cache.cpp:381
bcache::local_cache_t::add_direct
void add_direct(const std::string &direct_hash, const direct_mode_manifest_t &manifest)
Add a new direct mode entry to the cache.
Definition: local_cache.cpp:308
bcache::cache_entry_t
Meta data for a single cache entry.
Definition: cache_entry.hpp:28
bcache::local_cache_t::zero_stats
void zero_stats()
Clear the cache statistics.
Definition: local_cache.cpp:290