ipfs-chromium
dag_node.h
1 #ifndef IPFS_DAG_NODE_H_
2 #define IPFS_DAG_NODE_H_
3 
4 #include "link.h"
5 #include "resolution_state.h"
6 
7 #include <ipfs_client/gw/gateway_request.h>
8 #include <ipfs_client/ipld/block_source.h>
9 
10 #include <ipfs_client/cid.h>
11 #include <ipfs_client/response.h>
12 #include <vocab/slash_delimited.h>
13 
14 #include <cstdint>
15 
16 #include <functional>
17 #include <memory>
18 #include <string>
19 #include <string_view>
20 #include <variant>
21 #include <vector>
22 
23 namespace ipfs {
24 class PbDag;
25 class Client;
26 struct ValidatedIpns;
27 } // namespace ipfs
28 namespace libp2p::multi {
29 struct ContentIdentifier;
30 }
31 namespace ipfs::ipld {
32 
33 using NodePtr = std::shared_ptr<DagNode>;
34 class DirShard;
35 class DnsLinkName;
36 class IpnsName;
37 
39  MoreDataNeeded(std::string one) : ipfs_abs_paths_{{one}} {}
40  template <class Range>
41  MoreDataNeeded(Range const& many)
42  : ipfs_abs_paths_(many.begin(), many.end()) {}
43  std::vector<std::string> ipfs_abs_paths_;
44  bool insist_on_car = false;
45 };
46 enum class ProvenAbsent {};
47 struct PathChange {
48  std::string new_path;
49 };
50 
51 using ResolveResult =
52  std::variant<MoreDataNeeded, Response, ProvenAbsent, PathChange>;
56 class DagNode : public std::enable_shared_from_this<DagNode> {
57  Link* FindChild(std::string_view);
58  BlockSource source_;
59 
60  virtual ResolveResult resolve(ResolutionState& params) = 0;
61 
62  protected:
63  std::vector<std::pair<std::string, Link>> links_;
64  std::shared_ptr<Client> api_;
65 
68  ResolveResult CallChild(ResolutionState&);
69 
71  ResolveResult CallChild(ResolutionState&,
72  std::function<NodePtr(std::string_view)> gen_child);
73 
76  ResolveResult CallChild(ResolutionState&, std::string_view link_key);
77 
79  ResolveResult CallChild(ResolutionState&,
80  std::string_view link_key,
81  std::string_view block_key);
82 
83  public:
84  // ResolveResult resolve(SlashDelimited initial_path, BlockLookup);
85  ResolveResult Resolve(ResolutionState& params);
86 
87  static NodePtr fromBytes(std::shared_ptr<Client> const& api,
88  Cid const&,
89  ByteView bytes);
90  static NodePtr fromBytes(std::shared_ptr<Client> const& api,
91  Cid const&,
92  std::string_view bytes);
93  static NodePtr fromBlock(PbDag const&);
94 
95  virtual ~DagNode() noexcept {}
96 
97  virtual NodePtr rooted();
98  virtual NodePtr deroot();
99 
100  // Wish I had access to dynamic_cast
101  virtual DnsLinkName const* as_dnslink() const { return nullptr; }
102  virtual DirShard* as_hamt() { return nullptr; }
103  virtual IpnsName const* as_ipns() const { return nullptr; }
104 
105  virtual bool expired() const;
106  virtual bool PreferOver(DagNode const& another) const;
107 
108  void set_api(std::shared_ptr<Client>);
109  void source(BlockSource src) { source_ = src; }
110 };
111 } // namespace ipfs::ipld
112 
113 std::ostream& operator<<(std::ostream&, ipfs::ipld::PathChange const&);
114 
115 #endif // IPFS_DAG_NODE_H_
Definition: cid.h:16
Something to which a CID may refer directly.
Definition: pb_dag.h:24
A block, an IPNS record, etc.
Definition: dag_node.h:56
std::shared_ptr< Client > api_
Definition: dag_node.h:64
ResolveResult CallChild(ResolutionState &)
As before, but it might be possible to create on the fly if not known.
Definition: dag_node.cc:171
Definition: dnslink_name.h:9
Definition: resolution_state.h:21
Definition: block_storage.h:11
Definition: block_source.h:12
Definition: dag_node.h:38
Definition: dag_node.h:47