Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

[http_server] Simplify DRT node and table #132

Draft
wants to merge 4 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
86 changes: 60 additions & 26 deletions libraries/http_server/http_server/dynamic_routing_table.hh
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,27 @@
#include <string>
#include <string_view>
#include <unordered_map>
#include <unordered_set>

namespace li {

namespace internal {

template <typename V> struct drt_node {
// A simple memory pool for drt_node objects
template <typename T> struct drt_node_pool {
template <typename... Args> T* allocate(Args&&... args) {
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() : pool_(nullptr), v_{0, nullptr} {}
drt_node(drt_node_pool<drt_node>& pool) : pool_(pool), v_{0, nullptr} {}

struct iterator {
const drt_node<V>* ptr;
Expand All @@ -37,17 +50,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,8 +62,8 @@ 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));
}
}

Expand Down Expand Up @@ -94,7 +100,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,22 +112,22 @@ 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_;
};
} // namespace internal

template <typename V> struct dynamic_routing_table {
template <typename V> struct dynamic_routing_table_impl {
dynamic_routing_table_impl() : root(pool) {}

// 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());
auto [itr, is_inserted] = strings.emplace(std::string(r));
std::string_view r2(*itr);
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);
auto& operator[](const std::string& s) {
auto [itr, is_inserted] = strings.emplace(s);
std::string_view r(*itr);
return root.find_or_create(r, 0);
}

// Find a route and return an iterator.
Expand All @@ -130,8 +136,36 @@ template <typename V> struct dynamic_routing_table {
template <typename F> void for_all_routes(F f) const { root.for_all_routes(f); }
auto end() const { return root.end(); }

std::vector<std::shared_ptr<std::string>> strings;
internal::drt_node<V> root;
std::unordered_set<std::string> strings;
drt_node_pool<drt_node<V>> pool;
drt_node<V> root;
};
} // namespace internal

template <typename V> struct dynamic_routing_table {
dynamic_routing_table() : impl_(std::make_shared<internal::dynamic_routing_table_impl<V>>()) {}
dynamic_routing_table(const dynamic_routing_table& other) : impl_(other.impl_) {}

// Assignment operator
dynamic_routing_table& operator=(const dynamic_routing_table& other) {
if (this != &other) {
impl_ = other.impl_;
}
return *this;
}

// Find a route and return reference to a procedure.
auto& operator[](const std::string_view& r) { return impl_->operator[](r); }
auto& operator[](const std::string& s) { return impl_->operator[](s); }

// Find a route and return an iterator.
auto find(const std::string_view& r) const { return impl_->find(r); }

template <typename F> void for_all_routes(F f) const { impl_->for_all_routes(f); }
auto end() const { return impl_->end(); }

private:
std::shared_ptr<internal::dynamic_routing_table_impl<V>> impl_;
};

} // namespace li
1 change: 1 addition & 0 deletions libraries/sql/sql/sql_database.hh
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <deque>
#include <functional>
#include <unordered_map>

namespace li {
Expand Down
86 changes: 60 additions & 26 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 @@ -6510,9 +6511,21 @@ namespace li {

namespace internal {

template <typename V> struct drt_node {
// A simple memory pool for drt_node objects
template <typename T> struct drt_node_pool {
template <typename... Args> T* allocate(Args&&... args) {
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;
}

drt_node() : v_{0, nullptr} {}
std::vector<std::unique_ptr<T>> pool_;
};

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

struct iterator {
const drt_node<V>* ptr;
Expand All @@ -6537,17 +6550,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 @@ -6556,8 +6562,8 @@ 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));
}
}

Expand Down Expand Up @@ -6594,7 +6600,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 @@ -6606,22 +6612,22 @@ 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_;
};
} // namespace internal

template <typename V> struct dynamic_routing_table {
template <typename V> struct dynamic_routing_table_impl {
dynamic_routing_table_impl() : root(pool) {}

// 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());
auto [itr, is_inserted] = strings.emplace(std::string(r));
std::string_view r2(*itr);
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);
auto& operator[](const std::string& s) {
auto [itr, is_inserted] = strings.emplace(s);
std::string_view r(*itr);
return root.find_or_create(r, 0);
}

// Find a route and return an iterator.
Expand All @@ -6630,8 +6636,36 @@ template <typename V> struct dynamic_routing_table {
template <typename F> void for_all_routes(F f) const { root.for_all_routes(f); }
auto end() const { return root.end(); }

std::vector<std::shared_ptr<std::string>> strings;
internal::drt_node<V> root;
std::unordered_set<std::string> strings;
drt_node_pool<drt_node<V>> pool;
drt_node<V> root;
};
} // namespace internal

template <typename V> struct dynamic_routing_table {
dynamic_routing_table() : impl_(std::make_shared<internal::dynamic_routing_table_impl<V>>()) {}
dynamic_routing_table(const dynamic_routing_table& other) : impl_(other.impl_) {}

// Assignment operator
dynamic_routing_table& operator=(const dynamic_routing_table& other) {
if (this != &other) {
impl_ = other.impl_;
}
return *this;
}

// Find a route and return reference to a procedure.
auto& operator[](const std::string_view& r) { return impl_->operator[](r); }
auto& operator[](const std::string& s) { return impl_->operator[](s); }

// Find a route and return an iterator.
auto find(const std::string_view& r) const { return impl_->find(r); }

template <typename F> void for_all_routes(F f) const { impl_->for_all_routes(f); }
auto end() const { return impl_->end(); }

private:
std::shared_ptr<internal::dynamic_routing_table_impl<V>> impl_;
};

} // namespace li
Expand Down
Loading
Loading