A11 (C++ runtime)
Native C++ implementation of the A11 action and streaming runtime
Loading...
Searching...
No Matches
node_map.h
Go to the documentation of this file.
1// Copyright 2026 The A11 Authors.
2
14#ifndef A11_NODES_NODE_MAP_H_
15#define A11_NODES_NODE_MAP_H_
16
17#include <cstddef>
18#include <functional>
19#include <memory>
20#include <string>
21#include <string_view>
22
23#include <absl/base/thread_annotations.h>
24#include <absl/container/flat_hash_map.h>
25#include <absl/status/status.h>
26#include <absl/status/statusor.h>
27
28#include "thread/boost_primitives.h"
29
30namespace a11::stores {
31class ChunkStore;
32} // namespace a11::stores
33
34namespace a11::nodes {
35
36class AsyncNode;
37
41 std::function<absl::StatusOr<std::shared_ptr<stores::ChunkStore>>(
42 std::string node_id)>;
43
51class NodeMap : public std::enable_shared_from_this<NodeMap> {
52 public:
59 static absl::StatusOr<std::shared_ptr<NodeMap>> Create(
61 ~NodeMap() = default;
62
68 absl::StatusOr<std::shared_ptr<AsyncNode>> Get(std::string node_id);
69
75 absl::StatusOr<std::shared_ptr<AsyncNode>> GetIfExists(
76 std::string_view node_id) const;
77
85 absl::StatusOr<std::shared_ptr<AsyncNode>> Discard(
86 std::string_view node_id,
87 const std::shared_ptr<AsyncNode>& expected = nullptr);
88
94 [[nodiscard]] bool Contains(std::string_view node_id) const;
95
100 [[nodiscard]] size_t Size() const;
101
102 private:
103 explicit NodeMap(ChunkStoreFactory factory) : factory_(std::move(factory)) {}
104
105 const ChunkStoreFactory factory_;
106 mutable thread::Mutex mu_;
107 absl::flat_hash_map<std::string, std::shared_ptr<AsyncNode>> nodes_
108 ABSL_GUARDED_BY(mu_);
109};
110
111} // namespace a11::nodes
112
113#endif // A11_NODES_NODE_MAP_H_
A thread-safe registry of AsyncNodes keyed by id.
Definition node_map.h:51
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:82
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:94
static absl::StatusOr< std::shared_ptr< NodeMap > > Create(ChunkStoreFactory factory={})
Create a node map.
Definition node_map.cc:22
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:43
bool Contains(std::string_view node_id) const
Report whether a node with the given id exists.
Definition node_map.cc:110
size_t Size() const
Return the number of nodes currently in the map.
Definition node_map.cc:115
Abstract, pluggable backing store for the data of a node: an ordered, appendable log of fragments.
Definition chunk_store.h:53
Definition action.h:50
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:42
Definition node_map.h:30
Future< T > SubmitWithCancellationHook(absl::AnyInvocable< absl::StatusOr< T >() && > work, std::function< void()> cancellation_hook, thread::TreeOptions tree_options)
Run work on A11's fiber pool with application-specific cancellation.
Definition executor.h:30