#ifndef COMPONENTS_SQLITE_PROTO_KEY_VALUE_DATA_H_
#define COMPONENTS_SQLITE_PROTO_KEY_VALUE_DATA_H_
#include <algorithm>
#include <map>
#include <memory>
#include <optional>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/functional/callback_helpers.h"
#include "base/location.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/ref_counted.h"
#include "base/sequence_checker.h"
#include "base/time/time.h"
#include "base/timer/timer.h"
#include "components/sqlite_proto/key_value_table.h"
#include "components/sqlite_proto/table_manager.h"
namespace sqlite_proto {
namespace internal {
template <typename T>
struct FakeCompare { … };
}
template <typename T, typename Compare = internal::FakeCompare<T>>
class KeyValueData {
public:
KeyValueData(scoped_refptr<TableManager> manager,
KeyValueTable<T>* backend,
std::optional<size_t> max_num_entries,
base::TimeDelta flush_delay);
KeyValueData(const KeyValueData&) = delete;
KeyValueData& operator=(const KeyValueData&) = delete;
void InitializeOnDBSequence();
bool TryGetData(const std::string& key, T* data) const;
const std::map<std::string, T>& GetAllCached() { … }
void UpdateData(const std::string& key, const T& data);
void DeleteData(const std::vector<std::string>& keys);
void DeleteAllData();
void FlushDataToDisk();
void FlushDataToDisk(base::OnceClosure on_done);
private:
struct EntryCompare : private Compare {
bool operator()(const std::pair<std::string, T>& lhs,
const std::pair<std::string, T>& rhs) {
return Compare::operator()(lhs.second, rhs.second);
}
};
enum class DeferredOperation { kUpdate, kDelete };
scoped_refptr<TableManager> manager_;
base::WeakPtr<KeyValueTable<T>> backend_table_;
std::unique_ptr<std::map<std::string, T>> data_cache_;
std::unordered_map<std::string, DeferredOperation> deferred_updates_;
base::RepeatingTimer flush_timer_;
const base::TimeDelta flush_delay_;
const std::optional<size_t> max_num_entries_;
EntryCompare entry_compare_;
SEQUENCE_CHECKER(sequence_checker_);
};
template <typename T, typename Compare>
KeyValueData<T, Compare>::KeyValueData(scoped_refptr<TableManager> manager,
KeyValueTable<T>* backend,
std::optional<size_t> max_num_entries,
base::TimeDelta flush_delay)
: … { … }
template <typename T, typename Compare>
void KeyValueData<T, Compare>::InitializeOnDBSequence() { … }
template <typename T, typename Compare>
bool KeyValueData<T, Compare>::TryGetData(const std::string& key,
T* data) const { … }
template <typename T, typename Compare>
void KeyValueData<T, Compare>::UpdateData(const std::string& key,
const T& data) { … }
template <typename T, typename Compare>
void KeyValueData<T, Compare>::DeleteData(
const std::vector<std::string>& keys) { … }
template <typename T, typename Compare>
void KeyValueData<T, Compare>::DeleteAllData() { … }
template <typename T, typename Compare>
void KeyValueData<T, Compare>::FlushDataToDisk() { … }
template <typename T, typename Compare>
void KeyValueData<T, Compare>::FlushDataToDisk(base::OnceClosure on_done) { … }
}
#endif