diff --git a/include/opendht/indexation/pht.h b/include/opendht/indexation/pht.h index 0d679414bfdf3fdeb97ecf041e2a16f282db6c5c..f2ce26b3beaf2a2f44053671af02d4d2046e9721 100644 --- a/include/opendht/indexation/pht.h +++ b/include/opendht/indexation/pht.h @@ -35,8 +35,9 @@ struct Prefix { size_(std::min(first, p.content_.size()*8)), content_(Blob(p.content_.begin(), p.content_.begin()+size_/8)) { + auto rem = size_ % 8; - if ( not flags_.empty() ) { + if ( not p.flags_.empty() ) { flags_ = Blob(p.flags_.begin(), p.flags_.begin()+size_/8); if (rem) flags_.push_back(p.flags_[size_/8] & (0xFF << (8 - rem))); @@ -51,6 +52,7 @@ struct Prefix { throw std::out_of_range("len larger than prefix size."); if (len < 0) len += size_; + return Prefix(*this, len); } @@ -77,9 +79,11 @@ struct Prefix { * @return The prefix of this sibling. */ Prefix getSibling() const { - if ( not size_ ) - return Prefix(*this); - return swapBit(size_); + Prefix copy = *this; + if ( size_ ) + copy.swapContentBit(size_ - 1); + + return copy; } InfoHash hash() const { @@ -137,6 +141,10 @@ struct Prefix { return 8 * i + j; } + void swapContentBit(size_t bit) { + swapBit(content_, bit); + } + /** * This method swap the bit a the position 'bit' * @@ -144,11 +152,6 @@ struct Prefix { * @return The prefix with the bit at position 'bit' swapped * @throw out_of_range Throw out of range if bit does not exist */ - Prefix swapBit(size_t bit) const { - if ( bit >= content_.size() * 8 ) - throw std::out_of_range("bit larger than prefix size."); - } - void swapFlagBit(size_t bit) { swapBit(flags_, bit); } @@ -183,9 +186,8 @@ private: std::string blobToString(const Blob &bl) const { std::stringstream ss; - - auto bn = size_ % 8; - auto n = size_ / 8; + auto bn = bl.size() % 8; + auto n = bl.size() / 8; for (size_t i = 0; i < bl.size(); i++) ss << std::bitset<8>(bl[i]) << " "; @@ -206,7 +208,7 @@ private: } bool isActiveBit(const Blob &b, size_t pos) const { - if ( pos >= size_ ) + if ( pos >= content_.size() * 8 ) throw std::out_of_range("Can't detect active bit at pos, pos larger than prefix size or empty prefix"); return ((b[pos / 8] >> (7 - (pos % 8)) ) & 1) == 1; diff --git a/src/indexation/pht.cpp b/src/indexation/pht.cpp index 0fa380a8147f884accf08f6dec79ddff2f5fe70f..59a77ca8537fd0e6f7448625a1c3196e7b92d605 100644 --- a/src/indexation/pht.cpp +++ b/src/indexation/pht.cpp @@ -106,6 +106,7 @@ void Pht::lookupStep(Prefix p, std::shared_ptr<int> lo, std::shared_ptr<int> hi, /* start could be under 0 but after the compare it to 0 it always will be unsigned, so we can cast it*/ auto mid = (start >= 0) ? (unsigned) start : (*lo + *hi)/2; + auto first_res = std::make_shared<node_lookup_result>(); auto second_res = std::make_shared<node_lookup_result>(); @@ -117,6 +118,8 @@ void Pht::lookupStep(Prefix p, std::shared_ptr<int> lo, std::shared_ptr<int> hi, } else if (is_leaf or *lo > *hi) { // leaf node + Prefix to_insert = p.getPrefix(mid); + cache_.insert(to_insert); if (cb) { if (vals->size() == 0 and max_common_prefix_len and mid > 0) { @@ -126,7 +129,7 @@ void Pht::lookupStep(Prefix p, std::shared_ptr<int> lo, std::shared_ptr<int> hi, lookupStep(p_, lo, hi, vals, cb, done_cb, max_common_prefix_len, -1, all_values); } - cb(*vals, p.getPrefix(mid)); + cb(*vals, to_insert); } if (done_cb) @@ -291,10 +294,12 @@ void Pht::insert(Prefix kp, IndexEntry entry, std::shared_ptr<int> lo, std::shar if ( not check_split or final_prefix->size_ == kp.size_ ) { real_insert(final_prefix, std::move(entry)); } else { - if ( vals->size() < MAX_NODE_ENTRY_COUNT ) + if ( vals->size() < MAX_NODE_ENTRY_COUNT ) { getRealPrefix(final_prefix, std::move(entry), real_insert); - else + } + else { split(*final_prefix, vals, entry, real_insert); + } } } }, nullptr, cache_.lookup(kp), true @@ -444,10 +449,8 @@ void Pht::split(Prefix insert, std::shared_ptr<std::vector<std::shared_ptr<Index auto loc = foundSplitLocation(full, vals); auto prefix_to_insert = std::make_shared<Prefix>(full.getPrefix(loc)); - std::cerr << " Split loc " << loc << " full" << full.toString() << " size " << full.size_ << std::endl; for(;loc != insert.size_ - 1; loc--) { - std::cerr << "loc " << full.getPrefix(loc).toString() << std::endl; updateCanary(full.getPrefix(loc)); } diff --git a/tools/dhtnode.cpp b/tools/dhtnode.cpp index c3a59ade28570f71e0a33fd9fe709762776926b4..85e09f846837289d1a43ebaf3fd03c22ed8584f6 100644 --- a/tools/dhtnode.cpp +++ b/tools/dhtnode.cpp @@ -269,7 +269,7 @@ void cmd_loop(std::shared_ptr<DhtRunner>& dht, std::map<std::string, indexation: if (vals.empty()) return; std::cout << "Pht::lookup: found entries!" << std::endl - << " prefix: \"" << p.toString() << "\"" << std::endl + << p.toString() << std::endl << " hash: " << p.hash() << std::endl; std::cout << " entries:" << std::endl; for (auto v : vals)