mirror of
https://github.com/savoirfairelinux/opendht.git
synced 2026-11-08 12:28:42 +00:00
Fix print of the lookup output
This commit is contained in:
3 files changed
+24
-19
No files matched your search
@@ -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;
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -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)
|
||||
|
||||
Reference in new issue
Block a user