From 5a239cddf7eeb4f2aaa9b68f173caebae42dae0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20D=C3=A9saulniers?= <rostydela@gmail.com> Date: Mon, 18 Jan 2016 16:15:41 -0500 Subject: [PATCH] pht: fix insert always to root bug This makes the pht node recover all indexed data on a DHT node (maximum MAX_NODE_ENTRY_COUNT per node) so that the pht node can be aware if it has to split its data in the leaf. --- include/opendht/indexation/pht.h | 8 ++++++-- src/indexation/pht.cpp | 19 ++++++++++--------- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/include/opendht/indexation/pht.h b/include/opendht/indexation/pht.h index ad405a3c..910e97e2 100644 --- a/include/opendht/indexation/pht.h +++ b/include/opendht/indexation/pht.h @@ -131,7 +131,11 @@ struct IndexEntry : public dht::Value::Serializable<IndexEntry> { class Pht { static constexpr const char* INDEX_PREFIX = "index.pht."; - static constexpr const size_t MAX_NODE_ENTRY_COUNT {128}; + + /* This is the maximum number of entries per node. This parameter is + * critical and influences the traffic alot during a lookup operation. + */ + static constexpr const size_t MAX_NODE_ENTRY_COUNT {16}; public: using Key = std::map<std::string, Prefix>; @@ -180,7 +184,7 @@ private: void lookupStep(Prefix k, std::shared_ptr<int> lo, std::shared_ptr<int> hi, std::shared_ptr<std::vector<std::shared_ptr<Value>>> vals, LookupCallback cb, Dht::DoneCallbackSimple done_cb, - std::shared_ptr<unsigned> max_common_prefix_len); + std::shared_ptr<unsigned> max_common_prefix_len, bool all_values = false); /** * Updates the canary token on the node responsible for the specified diff --git a/src/indexation/pht.cpp b/src/indexation/pht.cpp index f12abed9..8fc45138 100644 --- a/src/indexation/pht.cpp +++ b/src/indexation/pht.cpp @@ -9,7 +9,7 @@ const ValueType IndexEntry::TYPE = ValueType::USER_DATA; void Pht::lookupStep(Prefix p, std::shared_ptr<int> lo, std::shared_ptr<int> hi, std::shared_ptr<std::vector<std::shared_ptr<Value>>> vals, LookupCallback cb, Dht::DoneCallbackSimple done_cb, - std::shared_ptr<unsigned> max_common_prefix_len) + std::shared_ptr<unsigned> max_common_prefix_len, bool all_values) { struct node_lookup_result { bool done {false}; @@ -19,7 +19,7 @@ void Pht::lookupStep(Prefix p, std::shared_ptr<int> lo, std::shared_ptr<int> hi, auto mid = (*lo + *hi)/2; auto first_res = std::make_shared<node_lookup_result>(); auto second_res = std::make_shared<node_lookup_result>(); - auto on_done = [=](bool ok){ + auto on_done = [=](bool ok) { bool is_leaf = first_res->is_pht and not second_res->is_pht; if (not ok) { if (done_cb) @@ -27,20 +27,21 @@ void Pht::lookupStep(Prefix p, std::shared_ptr<int> lo, std::shared_ptr<int> hi, } else if (is_leaf or *lo > *hi) { // leaf node - if (cb) + if (cb) { if (vals->size() == 0 and max_common_prefix_len and mid > 0) { auto p_ = (p.getPrefix(mid)).getSibling().getFullSize(); *lo = mid; *hi = p_.size_; - lookupStep(p_, lo, hi, vals, cb, done_cb, max_common_prefix_len); + lookupStep(p_, lo, hi, vals, cb, done_cb, max_common_prefix_len, all_values); } cb(*vals, p.getPrefix(mid)); + } if (done_cb) done_cb(true); } else if (first_res->is_pht) { // internal node *lo = mid+1; - lookupStep(p, lo, hi, vals, cb, done_cb, max_common_prefix_len); + lookupStep(p, lo, hi, vals, cb, done_cb, max_common_prefix_len, all_values); } else { // first get failed before second. if (done_cb) @@ -77,7 +78,7 @@ void Pht::lookupStep(Prefix p, std::shared_ptr<int> lo, std::shared_ptr<int> hi, } } } - else if (entry.prefix == p.content_) + else if (all_values or entry.prefix == p.content_) add_value(false); } return true; @@ -95,7 +96,7 @@ void Pht::lookupStep(Prefix p, std::shared_ptr<int> lo, std::shared_ptr<int> hi, if (not first_res->is_pht) { // Not a PHT node. *hi = mid-1; - lookupStep(p, lo, hi, vals, cb, done_cb, max_common_prefix_len); + lookupStep(p, lo, hi, vals, cb, done_cb, max_common_prefix_len, all_values); } else { first_res->done = true; if (second_res->done) @@ -172,7 +173,7 @@ void Pht::insert(Key k, Value v, Dht::DoneCallbackSimple done_cb) { if (done_cb) done_cb(false); } else { - if (vals->size() > MAX_NODE_ENTRY_COUNT) + if (vals->size() >= MAX_NODE_ENTRY_COUNT) *final_prefix = kp.getPrefix(final_prefix->size_+1); IndexEntry entry; @@ -183,7 +184,7 @@ void Pht::insert(Key k, Value v, Dht::DoneCallbackSimple done_cb) { updateCanary(*final_prefix); dht_->put(final_prefix->hash(), std::move(entry), done_cb); } - }, nullptr + }, nullptr, true ); } -- GitLab