A11 (C++ runtime)
Native C++ implementation of the A11 streaming action runtime
Loading...
Searching...
No Matches
node_map.h
Go to the documentation of this file.
1/*
2 * Copyright 2026 The A11 Authors
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
28#ifndef A11_NODES_NODE_MAP_H_
29#define A11_NODES_NODE_MAP_H_
30
31#include <cstddef>
32#include <functional>
33#include <memory>
34#include <string>
35#include <string_view>
36#include <vector>
37
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>
42
43#include "thread/boost_primitives.h"
44
45namespace a11::stores {
46class ChunkStore;
47} // namespace a11::stores
48
49namespace a11::nodes {
50
51class AsyncNode;
52
56 std::function<absl::StatusOr<std::shared_ptr<stores::ChunkStore>>(
57 std::string node_id)>;
58
66class NodeMap : public std::enable_shared_from_this<NodeMap> {
67 public:
74 static absl::StatusOr<std::shared_ptr<NodeMap>> Create(
75 ChunkStoreFactory factory = {});
76 ~NodeMap() = default;
77
83 // By value: on a miss the id is moved into the map, and callers hand it
84 // over rather than lending it.
85 // NOLINTNEXTLINE(performance-unnecessary-value-param)
91 absl::StatusOr<std::shared_ptr<AsyncNode>> Get(std::string node_id);
92
98 absl::StatusOr<std::shared_ptr<AsyncNode>> GetIfExists(
99 std::string_view node_id) const;
100
108 absl::StatusOr<std::shared_ptr<AsyncNode>> Discard(
109 std::string_view node_id,
110 const std::shared_ptr<AsyncNode>& expected = nullptr);
111
117 [[nodiscard]] bool Contains(std::string_view node_id) const;
118
123 [[nodiscard]] size_t Size() const;
124
133 [[nodiscard]] std::vector<std::string> Ids() const;
134
135 private:
136 explicit NodeMap(ChunkStoreFactory factory);
137
138 const ChunkStoreFactory factory_;
139 mutable thread::Mutex mu_;
140 absl::flat_hash_map<std::string, std::shared_ptr<AsyncNode>> nodes_
141 ABSL_GUARDED_BY(mu_);
142};
143
144} // namespace a11::nodes
145
146#endif // A11_NODES_NODE_MAP_H_
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
Definition action.h:69
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
Definition node_map.h:45