BuildCache
data_store.hpp
1 //--------------------------------------------------------------------------------------------------
2 // Copyright (c) 2020 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_DATA_STORE_HPP_
21 #define BUILDCACHE_DATA_STORE_HPP_
22 
23 #include <base/time_utils.hpp>
24 
25 #include <string>
26 
27 namespace bcache {
36 class data_store_t {
37 public:
39  class item_t {
40  public:
43  item_t(const std::string& value) : m_value(value), m_is_valid(true) {
44  }
45 
47  item_t() {
48  }
49 
51  const std::string& value() const {
52  return m_value;
53  }
54 
57  bool is_valid() const {
58  return m_is_valid;
59  }
60 
61  private:
62  const std::string m_value;
63  const bool m_is_valid = false;
64  };
65 
68  data_store_t(const std::string& name);
69 
74  void store_item(const std::string& key, const std::string& value, const time::seconds_t timeout);
75 
80  item_t get_item(const std::string& key);
81 
84  void remove_item(const std::string& key);
85 
87  void clear();
88 
89 private:
90  std::string make_file_path(const std::string& key);
91  void perform_housekeeping();
92 
93  std::string m_root_dir;
94 };
95 } // namespace bcache
96 
97 #endif // BUILDCACHE_DATA_STORE_HPP_
bcache::data_store_t::item_t::item_t
item_t()
Construct an invalid (empty) data item.
Definition: data_store.hpp:47
bcache::data_store_t
Provide access to a local key/value store.
Definition: data_store.hpp:36
bcache::time::seconds_t
int64_t seconds_t
Time in seconds since the Unix epoch.
Definition: time_utils.hpp:31
bcache::data_store_t::item_t::item_t
item_t(const std::string &value)
Construct a valid data item.
Definition: data_store.hpp:43
bcache::data_store_t::get_item
item_t get_item(const std::string &key)
Get a data item.
Definition: data_store.cpp:107
bcache::data_store_t::remove_item
void remove_item(const std::string &key)
Remove a data item.
Definition: data_store.cpp:125
bcache::data_store_t::item_t::value
const std::string & value() const
Definition: data_store.hpp:51
bcache
The top level namespace.
Definition: compressor.cpp:34
bcache::data_store_t::item_t::is_valid
bool is_valid() const
Check if the item is valid.
Definition: data_store.hpp:57
bcache::data_store_t::clear
void clear()
Clear the data store.
Definition: data_store.cpp:129
bcache::data_store_t::store_item
void store_item(const std::string &key, const std::string &value, const time::seconds_t timeout)
Add or overwrite a data item.
Definition: data_store.cpp:85
bcache::data_store_t::item_t
A data item.
Definition: data_store.hpp:39
bcache::data_store_t::data_store_t
data_store_t(const std::string &name)
Construct a data store.
Definition: data_store.cpp:75