28#ifndef A11_NODES_NODE_MAP_H_
29#define A11_NODES_NODE_MAP_H_
38#include <absl/base/thread_annotations.h>
39#include <absl/container/flat_hash_map.h>
40#include <absl/status/status.h>
41#include <absl/status/statusor.h>
43#include "thread/boost_primitives.h"
56 std::function<absl::StatusOr<std::shared_ptr<stores::ChunkStore>>(
57 std::string node_id)>;
66class NodeMap :
public std::enable_shared_from_this<NodeMap> {
74 static absl::StatusOr<std::shared_ptr<NodeMap>>
Create(
91 absl::StatusOr<std::shared_ptr<AsyncNode>>
Get(std::string node_id);
98 absl::StatusOr<std::shared_ptr<AsyncNode>>
GetIfExists(
99 std::string_view node_id)
const;
108 absl::StatusOr<std::shared_ptr<AsyncNode>>
Discard(
109 std::string_view node_id,
110 const std::shared_ptr<AsyncNode>& expected =
nullptr);
117 [[nodiscard]]
bool Contains(std::string_view node_id)
const;
123 [[nodiscard]]
size_t Size()
const;
133 [[nodiscard]] std::vector<std::string>
Ids()
const;
139 mutable thread::Mutex mu_;
140 absl::flat_hash_map<std::string, std::shared_ptr<AsyncNode>> nodes_
141 ABSL_GUARDED_BY(mu_);
A thread-safe registry of AsyncNodes keyed by id.
Definition node_map.h:66
absl::StatusOr< std::shared_ptr< AsyncNode > > GetIfExists(std::string_view node_id) const
Return the node for an id without creating it.
Definition node_map.cc:97
absl::StatusOr< std::shared_ptr< AsyncNode > > Discard(std::string_view node_id, const std::shared_ptr< AsyncNode > &expected=nullptr)
Remove the node for an id.
Definition node_map.cc:108
static absl::StatusOr< std::shared_ptr< NodeMap > > Create(ChunkStoreFactory factory={})
Create a node map.
Definition node_map.cc:43
absl::StatusOr< std::shared_ptr< AsyncNode > > Get(std::string node_id)
Return the node for an id, creating it if it does not exist.
Definition node_map.cc:64
bool Contains(std::string_view node_id) const
Report whether a node with the given id exists.
Definition node_map.cc:122
std::vector< std::string > Ids() const
Return the id of every node the map holds, sorted.
Definition node_map.cc:132
size_t Size() const
Return the number of nodes currently in the map.
Definition node_map.cc:127
Abstract, pluggable backing store for the data of a node: an ordered, appendable log of fragments.
Definition chunk_store.h:67
std::function< absl::StatusOr< std::shared_ptr< stores::ChunkStore > >(std::string node_id)> ChunkStoreFactory
Callable invoked to construct the backing chunk store for a new node from its id.
Definition node_map.h:57