ipfs-chromium
block_storage.h
1 #ifndef IPFS_BLOCKS_H_
2 #define IPFS_BLOCKS_H_
3 
4 #include "pb_dag.h"
5 #include "vocab/flat_mapset.h"
6 
7 #include <list>
8 #include <string>
9 #include <string_view>
10 
11 namespace libp2p::multi {
12 struct ContentIdentifier;
13 }
14 
15 namespace ipfs {
16 class DagListener;
17 class Client;
18 
19 class UnixFsPathResolver;
20 
26 class BlockStorage {
27  public:
28  BlockStorage();
29 
30  BlockStorage(BlockStorage const&) = delete;
31 
32  ~BlockStorage() noexcept;
33 
43  bool Store(std::string cid_str,
44  Cid const& cid,
45  std::string headers,
46  std::string const& body,
47  PbDag&& block);
48 
56  bool Store(std::string headers, std::string const& body, PbDag&& block);
57  bool Store(std::string const& cid, std::string headers, std::string body);
58  bool Store(std::string cid_str,
59  Cid const& cid,
60  std::string headers,
61  std::string body);
62  bool Store(Cid const& cid,
63  std::string headers,
64  std::string const& body,
65  PbDag&&);
67 
76  PbDag const* Get(std::string const& cid);
77 
85  std::string const* GetHeaders(std::string const& cid);
86 
91  void AddListening(UnixFsPathResolver*);
92 
96  void StopListening(UnixFsPathResolver*);
97 
104 
113  std::function<void(std::string, std::string, std::string)>;
114 
120 
121  private:
122  struct Record {
123  Record();
124  ~Record() noexcept;
125  std::time_t last_access = 0L;
126  std::string cid_str = {};
127  PbDag block = {};
128  std::string headers = {};
129  };
130  std::list<Record> records_ = std::list<Record>(0xFFUL);
131  using Iter = decltype(records_)::iterator;
132  flat_map<std::string, Record*> cid2record_;
133  flat_set<UnixFsPathResolver*> listening_;
134  bool checking_ = false;
135  std::vector<SerializedStorageHook> hooks_;
136 
137  Record const* GetInternal(std::string const&);
138  Record* FindFree(std::time_t);
139  Record* Allocate();
140  Record* StoreIdentity(std::string const&, Cid const&);
141 };
142 } // namespace ipfs
143 
144 #endif // IPFS_BLOCKS_H_
Immediate access to recently-accessed blocks.
Definition: block_storage.h:26
void AddListening(UnixFsPathResolver *)
Indicate that a particular path resolver is waiting on a CID to become available.
std::string const * GetHeaders(std::string const &cid)
Get HTTP headers associated with the block.
void AddStorageHook(SerializedStorageHook)
Register a callback that will be called when any new block goes into storage.
PbDag const * Get(std::string const &cid)
Get a block!
void CheckListening()
Normally called internally.
bool Store(std::string cid_str, Cid const &cid, std::string headers, std::string const &body, PbDag &&block)
Store a Block for later access.
std::function< void(std::string, std::string, std::string)> SerializedStorageHook
Type for callbacks about new blocks.
Definition: block_storage.h:113
void StopListening(UnixFsPathResolver *)
Indicate that a particular path resolver is no longer waiting.
Definition: cid.h:16
Something to which a CID may refer directly.
Definition: pb_dag.h:24
Definition: block_storage.h:11