Skip to content

Commit

Permalink
Partition, from context.
Browse files Browse the repository at this point in the history
  • Loading branch information
John-LittleBearLabs committed Feb 15, 2024
1 parent e0549c4 commit 0bc85e0
Show file tree
Hide file tree
Showing 18 changed files with 80 additions and 44 deletions.
4 changes: 2 additions & 2 deletions component/inter_request_state.cc
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ auto Self::cache() -> std::shared_ptr<CacheRequestor>& {
}
return cache_;
}
auto Self::orchestrator() -> Orchestrator& {
auto Self::orchestrator() -> Partition& {
if (!orc_) {
auto rtor = gw::default_requestor(cache(), api());
orc_ = std::make_shared<Orchestrator>(rtor, api());
orc_ = api()->with(rtor).partition({});
}
return *orc_;
}
Expand Down
6 changes: 3 additions & 3 deletions component/inter_request_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

#include "ipfs_client/ctx/default_gateways.h"
#include "ipfs_client/ipns_names.h"
#include "ipfs_client/orchestrator.h"
#include "ipfs_client/partition.h"

#include "base/supports_user_data.h"
#include "services/network/network_context.h"
Expand All @@ -25,7 +25,7 @@ class COMPONENT_EXPORT(IPFS) InterRequestState
std::time_t last_discovery_ = 0;
std::shared_ptr<CacheRequestor> cache_;
base::FilePath const disk_path_;
std::shared_ptr<Orchestrator> orc_; // TODO - map of origin to Orchestrator
std::shared_ptr<Partition> orc_; // TODO - map of origin to Orchestrator
raw_ptr<network::mojom::NetworkContext> network_context_;

std::shared_ptr<CacheRequestor>& cache();
Expand All @@ -38,7 +38,7 @@ class COMPONENT_EXPORT(IPFS) InterRequestState
Scheduler& scheduler();
std::shared_ptr<ContextApi> api();
std::array<std::shared_ptr<CacheRequestor>,2> serialized_caches();
Orchestrator& orchestrator();
Partition& orchestrator();
void network_context(network::mojom::NetworkContext*);
network::mojom::NetworkContext* network_context() const;

Expand Down
1 change: 0 additions & 1 deletion library/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -241,4 +241,3 @@ if(GTest_FOUND AND IN_WORKSPACE)
else()
message(WARNING "Not running tests - no gtest available.")
endif()#GTest_FOUND

7 changes: 7 additions & 0 deletions library/include/ipfs_client/context_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "ctx/http_api.h"
#include "ctx/json_parser.h"
#include "dag_cbor_value.h"
#include "gw/requestor.h"
#include "http_request_description.h"
#include "ipns_cbor_entry.h"
#include "multi_hash.h"
Expand All @@ -27,6 +28,7 @@ class io_context;
}

namespace ipfs {
class Partition;
class IpfsRequest;
class DagJsonValue;
class DagCborValue;
Expand All @@ -47,16 +49,19 @@ class ContextApi : public std::enable_shared_from_this<ContextApi> {
ContextApi();
virtual ~ContextApi() noexcept {}

std::shared_ptr<Partition> partition(std::string key);
ctx::HttpApi& http();
ctx::DnsTxtLookup& dns_txt();
ctx::GatewayConfig& gw_cfg();
ctx::JsonParser& json();
ctx::CborParser& cbor();
std::shared_ptr<gw::Requestor> requestor();
ContextApi& with(std::unique_ptr<ctx::HttpApi>);
ContextApi& with(std::unique_ptr<ctx::DnsTxtLookup>);
ContextApi& with(std::unique_ptr<ctx::GatewayConfig>);
ContextApi& with(std::unique_ptr<ctx::JsonParser>);
ContextApi& with(std::unique_ptr<ctx::CborParser>);
ContextApi& with(std::shared_ptr<gw::Requestor>);
ContextApi& with(SigningKeyType, std::unique_ptr<crypto::SignatureVerifier>);
ContextApi& with(MimeTypeDeduction);
ContextApi& with(UrlUnescaping);
Expand Down Expand Up @@ -98,6 +103,8 @@ class ContextApi : public std::enable_shared_from_this<ContextApi> {
std::unique_ptr<ctx::CborParser> cbor_parser_;
MimeTypeDeduction deduce_mime_type_;
UrlUnescaping unescape_;
std::shared_ptr<gw::Requestor> rtor_;
std::unordered_map<std::string, std::shared_ptr<Partition>> partitions_;
};

} // namespace ipfs
Expand Down
6 changes: 3 additions & 3 deletions library/include/ipfs_client/gw/gateway_request.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

namespace ipfs {
class IpfsRequest;
class Orchestrator;
class Partition;
namespace ipld {
class DagNode;
}
Expand All @@ -37,7 +37,7 @@ std::string_view name(Type);
constexpr std::size_t BLOCK_RESPONSE_BUFFER_SIZE = 2 * 1024 * 1024;

class GatewayRequest {
std::shared_ptr<Orchestrator> orchestrator_;
std::shared_ptr<Partition> orchestrator_;
std::vector<std::function<void(std::string_view)>> bytes_received_hooks;

void ParseNodes(std::string_view, ContextApi* api);
Expand All @@ -60,7 +60,7 @@ class GatewayRequest {
std::optional<std::size_t> max_response_size() const;
std::optional<HttpRequestDescription> describe_http(std::string_view) const;
std::string debug_string() const;
void orchestrator(std::shared_ptr<Orchestrator> const&);
void orchestrator(std::shared_ptr<Partition> const&);
bool cachable() const;

bool RespondSuccessfully(std::string_view,
Expand Down
6 changes: 3 additions & 3 deletions library/include/ipfs_client/opinionated_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include <ipfs_client/ctx/boost_beast_http.h>

#include <ipfs_client/context_api.h>
#include <ipfs_client/orchestrator.h>
#include <ipfs_client/partition.h>

#include <memory>

Expand All @@ -29,8 +29,8 @@ class HttpSession;

namespace ipfs {

std::pair<std::shared_ptr<ContextApi>, std::shared_ptr<Orchestrator>>
start_default(boost::asio::io_context& io);
// std::pair<std::shared_ptr<ContextApi>, std::shared_ptr<Partition>>
std::shared_ptr<ContextApi> start_default(boost::asio::io_context& io);

} // namespace ipfs

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,20 @@ namespace ipfs {

class ContextApi;

class Orchestrator : public std::enable_shared_from_this<Orchestrator> {
class Partition : public std::enable_shared_from_this<Partition> {
public:
using GatewayAccess =
std::function<void(std::shared_ptr<gw::GatewayRequest>)>;
using MimeDetection = std::function<
std::string(std::string, std::string_view, std::string const&)>;
explicit Orchestrator(std::shared_ptr<gw::Requestor> requestor,
std::shared_ptr<ContextApi> = {});
void build_response(std::shared_ptr<IpfsRequest>);
bool add_node(std::string key, ipld::NodePtr);
bool has_key(std::string const& k) const;
std::size_t Stored() const { return dags_.size(); }

explicit Partition(std::shared_ptr<gw::Requestor> requestor,
std::shared_ptr<ContextApi>);

private:
flat_map<std::string, ipld::NodePtr> dags_;
std::shared_ptr<ContextApi> api_;
Expand Down
23 changes: 23 additions & 0 deletions library/src/ipfs_client/context_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
#include "ipfs_client/ctx/null_dns_txt_lookup.h"
#include "ipfs_client/ctx/null_http_provider.h"
#include "ipfs_client/ctx/transitory_gateway_config.h"
#include "ipfs_client/gw/default_requestor.h"

#include "ipfs_client/partition.h"
#include "ipfs_client/web_util.h"

#include "log_macros.h"
Expand Down Expand Up @@ -67,6 +70,14 @@ std::string Self::UnescapeUrlComponent(std::string_view url_comp) {
return unescape_(url_comp);
}

auto Self::partition(std::string key) -> std::shared_ptr<Partition> {
auto& part = partitions_[key];
if (!part) {
LOG(INFO) << "New partition for key '" << key << "'.";
part = std::make_shared<Partition>(requestor(), shared_from_this());
}
return part;
}
auto Self::http() -> ctx::HttpApi& {
if (!http_api_) {
http_api_ = std::make_unique<ctx::NullHttpProvider>();
Expand Down Expand Up @@ -108,6 +119,14 @@ auto Self::cbor() -> ctx::CborParser& {
DCHECK(cbor_parser_);
return *cbor_parser_;
}
auto Self::requestor() -> std::shared_ptr<gw::Requestor> {
if (!rtor_) {
LOG(INFO) << "No gateway requestor provided. Will use default with no "
"caching or custom behavior.";
rtor_ = gw::default_requestor({}, shared_from_this());
}
return rtor_;
}
Self& Self::with(std::unique_ptr<ctx::JsonParser> p) {
json_parser_ = std::move(p);
return *this;
Expand Down Expand Up @@ -145,3 +164,7 @@ Self& Self::with(std::unique_ptr<ctx::GatewayConfig> p) {
gateway_config_ = std::move(p);
return *this;
}
Self& Self::with(std::shared_ptr<gw::Requestor> p) {
rtor_ = p;
return *this;
}
2 changes: 1 addition & 1 deletion library/src/ipfs_client/gw/dnslink_requestor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

#include <ipfs_client/context_api.h>
#include <ipfs_client/ipfs_request.h>
#include <ipfs_client/orchestrator.h>
#include <ipfs_client/partition.h>

#include "log_macros.h"

Expand Down
12 changes: 8 additions & 4 deletions library/src/ipfs_client/gw/dnslink_requestor_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#include <ipfs_client/gw/gateway_request.h>
#include <ipfs_client/gw/terminating_requestor.h>
#include <ipfs_client/orchestrator.h>
#include <ipfs_client/partition.h>

using T = ig::DnsLinkRequestor;

Expand All @@ -15,9 +15,13 @@ struct DnslinkRequestorTest : public testing::Test {
std::shared_ptr<ig::GatewayRequest> r_ =
ig::GatewayRequest::fromIpfsPath(i::SlashDelimited{"/ipns/not_a_cid"});
ig::Requestor& t() { return *t_; }
std::shared_ptr<i::Orchestrator> orc_ = std::make_shared<i::Orchestrator>(
std::make_shared<ig::TerminatingRequestor>());
DnslinkRequestorTest() { r_->orchestrator(orc_); }
std::shared_ptr<ig::Requestor> rtor_;
std::shared_ptr<i::Partition> orc_;
DnslinkRequestorTest() {
rtor_ = std::make_shared<ig::TerminatingRequestor>();
orc_ = api->with(rtor_).partition({});
r_->orchestrator(orc_);
}
};
} // namespace

Expand Down
4 changes: 2 additions & 2 deletions library/src/ipfs_client/gw/gateway_request.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include <ipfs_client/car.h>
#include <ipfs_client/ipfs_request.h>
#include <ipfs_client/ipns_record.h>
#include <ipfs_client/orchestrator.h>
#include <ipfs_client/partition.h>
#include <ipfs_client/pb_dag.h>
#include <ipfs_client/response.h>

Expand Down Expand Up @@ -364,7 +364,7 @@ bool Self::RespondSuccessfully(std::string_view bytes,
void Self::Hook(std::function<void(std::string_view)> f) {
bytes_received_hooks.push_back(f);
}
void Self::orchestrator(std::shared_ptr<Orchestrator> const& orc) {
void Self::orchestrator(std::shared_ptr<Partition> const& orc) {
orchestrator_ = orc;
}
bool Self::PartiallyRedundant() const {
Expand Down
6 changes: 3 additions & 3 deletions library/src/ipfs_client/gw/gateway_request_unittest.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <ipfs_client/gw/gateway_request.h>
#include <ipfs_client/orchestrator.h>
#include <ipfs_client/partition.h>

#include <mock_api.h>
#include <mock_requestor.h>
Expand Down Expand Up @@ -53,9 +53,9 @@ struct GatewayRequestTest : public testing::Test {
T t_;
std::shared_ptr<MockApi> api = std::make_shared<MockApi>();
std::shared_ptr<MockRequestor> rtor = std::make_shared<MockRequestor>();
std::shared_ptr<i::Orchestrator> orc =
std::make_shared<i::Orchestrator>(rtor, api);
std::shared_ptr<i::Partition> orc;
GatewayRequestTest() {
orc = api->with(rtor).partition({});
t_.orchestrator(orc);
t_.main_param = "main";
}
Expand Down
2 changes: 1 addition & 1 deletion library/src/ipfs_client/gw/inline_request_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include <ipfs_client/gw/gateway_request.h>
#include <ipfs_client/ipld/chunk.h>
#include <ipfs_client/orchestrator.h>
#include <ipfs_client/partition.h>

#include "log_macros.h"

Expand Down
10 changes: 5 additions & 5 deletions library/src/ipfs_client/gw/inline_request_handler_unittest.cc
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#include <ipfs_client/gw/inline_request_handler.h>

#include <gtest/gtest.h>
#include <mock_api.h>

#include <ipfs_client/gw/gateway_request.h>
#include <ipfs_client/gw/terminating_requestor.h>
#include <ipfs_client/orchestrator.h>
#include <ipfs_client/partition.h>

namespace i = ipfs;
namespace ig = i::gw;
Expand All @@ -15,9 +15,9 @@ using ipfs::Cid;

TEST(InlineRequestHanlder, bluesky) {
T t;

auto orc = std::make_shared<i::Orchestrator>(
std::make_shared<ig::TerminatingRequestor>());
auto api = std::make_shared<MockApi>();
auto rtor = std::make_shared<ig::TerminatingRequestor>();
auto orc = api->with(rtor).partition({});
auto r = std::make_shared<ig::GatewayRequest>();
r->type = ig::Type::Identity;
r->orchestrator(orc);
Expand Down
2 changes: 1 addition & 1 deletion library/src/ipfs_client/gw/requestor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <ipfs_client/ipld/dag_node.h>

#include <ipfs_client/ipfs_request.h>
#include <ipfs_client/orchestrator.h>
#include <ipfs_client/partition.h>
#include <ipfs_client/pb_dag.h>
#include <ipfs_client/response.h>

Expand Down
9 changes: 6 additions & 3 deletions library/src/ipfs_client/opinionated_context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
#if HAS_OPINIONATED_CONTEXT

auto ipfs::start_default(boost::asio::io_context& io)
-> std::pair<std::shared_ptr<ContextApi>, std::shared_ptr<Orchestrator>> {
-> std::shared_ptr<ContextApi> {
// -> std::pair<std::shared_ptr<ContextApi>, std::shared_ptr<Partition>> {
auto api = std::make_shared<ContextApi>();
using SKT = crypto::SigningKeyType;
api->with(std::make_unique<ctx::BoostBeastHttp>(io))
Expand All @@ -15,8 +16,10 @@ auto ipfs::start_default(boost::asio::io_context& io)
.with(SKT::Ed25519, std::make_unique<crypto::OpensslSignatureVerifier>(
EVP_PKEY_ED25519));
auto rtor = gw::default_requestor({}, api);
auto orc = std::make_shared<Orchestrator>(rtor, api);
return {api, orc};
api->with(rtor);
// auto orc = std::make_shared<Partition>(rtor, api);
// return {api, orc};
return api;
}

#endif // HAS_OPINIONATED_CONTEXT
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "ipfs_client/orchestrator.h"
#include "ipfs_client/partition.h"

#include <ipfs_client/context_api.h>
#include <ipfs_client/ipfs_request.h>
Expand All @@ -10,11 +10,10 @@

using namespace std::literals;

using Self = ipfs::Orchestrator;
using Self = ipfs::Partition;

Self::Orchestrator(std::shared_ptr<gw::Requestor> requestor,
std::shared_ptr<ContextApi> api)
// : gw_requestor_{ga}, api_{api}, requestor_{requestor} {
Self::Partition(std::shared_ptr<gw::Requestor> requestor,
std::shared_ptr<ContextApi> api)
: api_{api}, requestor_{requestor} {
DCHECK(requestor);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "ipfs_client/orchestrator.h"
#include "ipfs_client/partition.h"

#include <mock_cbor.h>

Expand Down Expand Up @@ -158,16 +158,16 @@ struct TestRequestor final : public ig::Requestor {
};
struct OrchestratingRealData : public ::testing::Test {
std::shared_ptr<MockApi> api_ = std::make_shared<MockApi>();
std::shared_ptr<i::Orchestrator> orc_;
std::shared_ptr<i::Partition> orc_;
OrchestratingRealData() {
i::log::SetLevel(i::log::Level::Off);
auto f = [](auto) {};
resp_.body_ = "No response received.";
resp_.status_ = 0;
resp_.mime_ = "uninit - no mime provided";
resp_.location_ = "uninit";
orc_ = std::make_shared<i::Orchestrator>(std::make_shared<TestRequestor>(),
api_);
api_->with(std::make_shared<TestRequestor>());
orc_ = api_->partition({});
}
i::Response resp_;
void dorequest(std::string_view ipfs_path) {
Expand Down

0 comments on commit 0bc85e0

Please sign in to comment.