Skip to content

[http_server] Simplify DRT node and table #132

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 60 additions & 38 deletions libraries/http_server/http_server/dynamic_routing_table.hh
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,41 @@
#include <string>
#include <string_view>
#include <unordered_map>
#include <unordered_set>

namespace li {

namespace internal {

template <typename V> struct drt_node {
/**
* A memory pool for drt_node objects that manages node allocation and lifetime
*/
template <typename T> struct drt_node_pool {
template <typename... Args> T* allocate(Args&&... args) noexcept {
auto new_node = std::make_unique<T>(std::forward<Args>(args)...);
T* ptr = new_node.get();
pool_.emplace_back(std::move(new_node));
return ptr;
}

std::vector<std::unique_ptr<T>> pool_;
};

drt_node() : v_{0, nullptr} {}
template <typename V> struct drt_node {
drt_node() noexcept : pool_(nullptr), v_{0, nullptr} {}
drt_node(drt_node_pool<drt_node>& pool) noexcept : pool_(pool), v_{0, nullptr} {}

struct iterator {
const drt_node<V>* ptr;
std::string_view first;
V second;

auto operator->() { return this; }
bool operator==(const iterator& b) const { return this->ptr == b.ptr; }
bool operator!=(const iterator& b) const { return this->ptr != b.ptr; }
auto operator->() noexcept { return this; }
bool operator==(const iterator& b) const noexcept { return this->ptr == b.ptr; }
bool operator!=(const iterator& b) const noexcept { return this->ptr != b.ptr; }
};

auto end() const { return iterator{nullptr, std::string_view(), V()}; }
auto end() const noexcept { return iterator{nullptr, std::string_view(), V()}; }

auto& find_or_create(std::string_view r, unsigned int c) {
if (c == r.size())
Expand All @@ -37,17 +52,10 @@ template <typename V> struct drt_node {
c++;
std::string_view k = r.substr(s, c - s);

auto it = children_.find(k);
if (it != children_.end())
return children_[k]->find_or_create(r, c);
else {
auto new_node = std::make_shared<drt_node>();
children_shared_pointers_.push_back(new_node);
children_.insert({k, new_node.get()});
return new_node->find_or_create(r, c);
if (children_.find(k) == children_.end()) {
children_[k] = pool_.allocate(pool_);
}

return v_;
return children_[k]->find_or_create(r, c);
}

template <typename F> void for_all_routes(F f, std::string prefix = "") const {
Expand All @@ -56,13 +64,13 @@ template <typename V> struct drt_node {
else {
if (prefix.size() && prefix.back() != '/')
prefix += '/';
for (auto pair : children_)
pair.second->for_all_routes(f, prefix + std::string(pair.first));
for (const auto& kv : children_)
kv.second->for_all_routes(f, prefix + std::string(kv.first));
}
}

// Find a route.
iterator find(const std::string_view& r, unsigned int c) const {
iterator find(const std::string_view& r, unsigned int c) const noexcept {
// We found the route r.
if ((c == r.size() and v_.handler != nullptr) or (children_.size() == 0))
return iterator{this, r, v_};
Expand Down Expand Up @@ -94,7 +102,7 @@ template <typename V> struct drt_node {

{
// if one child is a url param {{param_name}}, choose it
for (auto& kv : children_) {
for (const auto& kv : children_) {
auto name = kv.first;
if (name.size() > 4 and name[0] == '{' and name[1] == '{' and
name[name.size() - 2] == '}' and name[name.size() - 1] == '}')
Expand All @@ -106,32 +114,46 @@ template <typename V> struct drt_node {

V v_;
std::unordered_map<std::string_view, drt_node*> children_;
std::vector<std::shared_ptr<drt_node>> children_shared_pointers_;
drt_node_pool<drt_node>& pool_;
};

template <typename V> struct dynamic_routing_table_data {
dynamic_routing_table_data() noexcept : root(pool_) {}

std::unordered_set<std::string> paths;
drt_node<V> root;

private:
drt_node_pool<drt_node<V>> pool_;
};
} // namespace internal

/**
* A dynamic routing table that supports route registration and lookup.
*/
template <typename V> struct dynamic_routing_table {
dynamic_routing_table() noexcept
: data_(std::make_shared<internal::dynamic_routing_table_data<V>>()) {}
dynamic_routing_table(const dynamic_routing_table& other) noexcept : data_(other.data_) {}

// Find a route and return reference to a procedure.
auto& operator[](const std::string_view& r) {
strings.push_back(std::make_shared<std::string>(r));
std::string_view r2(*strings.back());
return root.find_or_create(r2, 0);
}
auto& operator[](const std::string& r) {
strings.push_back(std::make_shared<std::string>(r));
std::string_view r2(*strings.back());
return root.find_or_create(r2, 0);
dynamic_routing_table& operator=(const dynamic_routing_table& other) noexcept {
if (this != &other) {
data_ = other.data_;
}
return *this;
}

// Find a route and return an iterator.
auto find(const std::string_view& r) const { return root.find(r, 0); }

template <typename F> void for_all_routes(F f) const { root.for_all_routes(f); }
auto end() const { return root.end(); }
auto& operator[](const std::string_view& r) { return this->operator[](std::string(r)); }
auto& operator[](const std::string& s) {
auto [itr, is_inserted] = data_->paths.emplace(s);
return data_->root.find_or_create(*itr, 0);
}
auto find(const std::string_view& r) const noexcept { return data_->root.find(r, 0); }
template <typename F> void for_all_routes(F f) const { data_->root.for_all_routes(f); }
auto end() const noexcept { return data_->root.end(); }

std::vector<std::shared_ptr<std::string>> strings;
internal::drt_node<V> root;
private:
std::shared_ptr<internal::dynamic_routing_table_data<V>> data_;
};

} // namespace li
98 changes: 60 additions & 38 deletions single_headers/lithium.hh
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
#include <tuple>
#include <unistd.h>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <variant>
#include <vector>
Expand Down Expand Up @@ -6513,21 +6514,35 @@ namespace li {

namespace internal {

template <typename V> struct drt_node {
/**
* A memory pool for drt_node objects that manages node allocation and lifetime
*/
template <typename T> struct drt_node_pool {
template <typename... Args> T* allocate(Args&&... args) noexcept {
auto new_node = std::make_unique<T>(std::forward<Args>(args)...);
T* ptr = new_node.get();
pool_.emplace_back(std::move(new_node));
return ptr;
}

std::vector<std::unique_ptr<T>> pool_;
};

drt_node() : v_{0, nullptr} {}
template <typename V> struct drt_node {
drt_node() noexcept : pool_(nullptr), v_{0, nullptr} {}
drt_node(drt_node_pool<drt_node>& pool) noexcept : pool_(pool), v_{0, nullptr} {}

struct iterator {
const drt_node<V>* ptr;
std::string_view first;
V second;

auto operator->() { return this; }
bool operator==(const iterator& b) const { return this->ptr == b.ptr; }
bool operator!=(const iterator& b) const { return this->ptr != b.ptr; }
auto operator->() noexcept { return this; }
bool operator==(const iterator& b) const noexcept { return this->ptr == b.ptr; }
bool operator!=(const iterator& b) const noexcept { return this->ptr != b.ptr; }
};

auto end() const { return iterator{nullptr, std::string_view(), V()}; }
auto end() const noexcept { return iterator{nullptr, std::string_view(), V()}; }

auto& find_or_create(std::string_view r, unsigned int c) {
if (c == r.size())
Expand All @@ -6540,17 +6555,10 @@ template <typename V> struct drt_node {
c++;
std::string_view k = r.substr(s, c - s);

auto it = children_.find(k);
if (it != children_.end())
return children_[k]->find_or_create(r, c);
else {
auto new_node = std::make_shared<drt_node>();
children_shared_pointers_.push_back(new_node);
children_.insert({k, new_node.get()});
return new_node->find_or_create(r, c);
if (children_.find(k) == children_.end()) {
children_[k] = pool_.allocate(pool_);
}

return v_;
return children_[k]->find_or_create(r, c);
}

template <typename F> void for_all_routes(F f, std::string prefix = "") const {
Expand All @@ -6559,13 +6567,13 @@ template <typename V> struct drt_node {
else {
if (prefix.size() && prefix.back() != '/')
prefix += '/';
for (auto pair : children_)
pair.second->for_all_routes(f, prefix + std::string(pair.first));
for (const auto& kv : children_)
kv.second->for_all_routes(f, prefix + std::string(kv.first));
}
}

// Find a route.
iterator find(const std::string_view& r, unsigned int c) const {
iterator find(const std::string_view& r, unsigned int c) const noexcept {
// We found the route r.
if ((c == r.size() and v_.handler != nullptr) or (children_.size() == 0))
return iterator{this, r, v_};
Expand Down Expand Up @@ -6597,7 +6605,7 @@ template <typename V> struct drt_node {

{
// if one child is a url param {{param_name}}, choose it
for (auto& kv : children_) {
for (const auto& kv : children_) {
auto name = kv.first;
if (name.size() > 4 and name[0] == '{' and name[1] == '{' and
name[name.size() - 2] == '}' and name[name.size() - 1] == '}')
Expand All @@ -6609,32 +6617,46 @@ template <typename V> struct drt_node {

V v_;
std::unordered_map<std::string_view, drt_node*> children_;
std::vector<std::shared_ptr<drt_node>> children_shared_pointers_;
drt_node_pool<drt_node>& pool_;
};

template <typename V> struct dynamic_routing_table_data {
dynamic_routing_table_data() noexcept : root(pool_) {}

std::unordered_set<std::string> paths;
drt_node<V> root;

private:
drt_node_pool<drt_node<V>> pool_;
};
} // namespace internal

/**
* A dynamic routing table that supports route registration and lookup.
*/
template <typename V> struct dynamic_routing_table {
dynamic_routing_table() noexcept
: data_(std::make_shared<internal::dynamic_routing_table_data<V>>()) {}
dynamic_routing_table(const dynamic_routing_table& other) noexcept : data_(other.data_) {}

// Find a route and return reference to a procedure.
auto& operator[](const std::string_view& r) {
strings.push_back(std::make_shared<std::string>(r));
std::string_view r2(*strings.back());
return root.find_or_create(r2, 0);
}
auto& operator[](const std::string& r) {
strings.push_back(std::make_shared<std::string>(r));
std::string_view r2(*strings.back());
return root.find_or_create(r2, 0);
dynamic_routing_table& operator=(const dynamic_routing_table& other) noexcept {
if (this != &other) {
data_ = other.data_;
}
return *this;
}

// Find a route and return an iterator.
auto find(const std::string_view& r) const { return root.find(r, 0); }

template <typename F> void for_all_routes(F f) const { root.for_all_routes(f); }
auto end() const { return root.end(); }
auto& operator[](const std::string_view& r) { return this->operator[](std::string(r)); }
auto& operator[](const std::string& s) {
auto [itr, is_inserted] = data_->paths.emplace(s);
return data_->root.find_or_create(*itr, 0);
}
auto find(const std::string_view& r) const noexcept { return data_->root.find(r, 0); }
template <typename F> void for_all_routes(F f) const { data_->root.for_all_routes(f); }
auto end() const noexcept { return data_->root.end(); }

std::vector<std::shared_ptr<std::string>> strings;
internal::drt_node<V> root;
private:
std::shared_ptr<internal::dynamic_routing_table_data<V>> data_;
};

} // namespace li
Expand Down
Loading
Loading