Skip to content
Snippets Groups Projects
Unverified Commit e2797284 authored by kaldoran's avatar kaldoran
Browse files

Fix print of the lookup output

parent a6b1b319
No related branches found
No related tags found
No related merge requests found
...@@ -35,8 +35,9 @@ struct Prefix { ...@@ -35,8 +35,9 @@ struct Prefix {
size_(std::min(first, p.content_.size()*8)), size_(std::min(first, p.content_.size()*8)),
content_(Blob(p.content_.begin(), p.content_.begin()+size_/8)) content_(Blob(p.content_.begin(), p.content_.begin()+size_/8))
{ {
auto rem = 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); flags_ = Blob(p.flags_.begin(), p.flags_.begin()+size_/8);
if (rem) if (rem)
flags_.push_back(p.flags_[size_/8] & (0xFF << (8 - rem))); flags_.push_back(p.flags_[size_/8] & (0xFF << (8 - rem)));
...@@ -51,6 +52,7 @@ struct Prefix { ...@@ -51,6 +52,7 @@ struct Prefix {
throw std::out_of_range("len larger than prefix size."); throw std::out_of_range("len larger than prefix size.");
if (len < 0) if (len < 0)
len += size_; len += size_;
return Prefix(*this, len); return Prefix(*this, len);
} }
...@@ -77,9 +79,11 @@ struct Prefix { ...@@ -77,9 +79,11 @@ struct Prefix {
* @return The prefix of this sibling. * @return The prefix of this sibling.
*/ */
Prefix getSibling() const { Prefix getSibling() const {
if ( not size_ ) Prefix copy = *this;
return Prefix(*this); if ( size_ )
return swapBit(size_); copy.swapContentBit(size_ - 1);
return copy;
} }
InfoHash hash() const { InfoHash hash() const {
...@@ -137,6 +141,10 @@ struct Prefix { ...@@ -137,6 +141,10 @@ struct Prefix {
return 8 * i + j; return 8 * i + j;
} }
void swapContentBit(size_t bit) {
swapBit(content_, bit);
}
/** /**
* This method swap the bit a the position 'bit' * This method swap the bit a the position 'bit'
* *
...@@ -144,11 +152,6 @@ struct Prefix { ...@@ -144,11 +152,6 @@ struct Prefix {
* @return The prefix with the bit at position 'bit' swapped * @return The prefix with the bit at position 'bit' swapped
* @throw out_of_range Throw out of range if bit does not exist * @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) { void swapFlagBit(size_t bit) {
swapBit(flags_, bit); swapBit(flags_, bit);
} }
...@@ -183,9 +186,8 @@ private: ...@@ -183,9 +186,8 @@ private:
std::string blobToString(const Blob &bl) const { std::string blobToString(const Blob &bl) const {
std::stringstream ss; std::stringstream ss;
auto bn = bl.size() % 8;
auto bn = size_ % 8; auto n = bl.size() / 8;
auto n = size_ / 8;
for (size_t i = 0; i < bl.size(); i++) for (size_t i = 0; i < bl.size(); i++)
ss << std::bitset<8>(bl[i]) << " "; ss << std::bitset<8>(bl[i]) << " ";
...@@ -206,7 +208,7 @@ private: ...@@ -206,7 +208,7 @@ private:
} }
bool isActiveBit(const Blob &b, size_t pos) const { 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"); 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; 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, ...@@ -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*/ /* 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 mid = (start >= 0) ? (unsigned) start : (*lo + *hi)/2;
auto first_res = std::make_shared<node_lookup_result>(); auto first_res = std::make_shared<node_lookup_result>();
auto second_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, ...@@ -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) { else if (is_leaf or *lo > *hi) {
// leaf node // leaf node
Prefix to_insert = p.getPrefix(mid);
cache_.insert(to_insert);
if (cb) { if (cb) {
if (vals->size() == 0 and max_common_prefix_len and mid > 0) { 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, ...@@ -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); 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) if (done_cb)
...@@ -291,12 +294,14 @@ void Pht::insert(Prefix kp, IndexEntry entry, std::shared_ptr<int> lo, std::shar ...@@ -291,12 +294,14 @@ void Pht::insert(Prefix kp, IndexEntry entry, std::shared_ptr<int> lo, std::shar
if ( not check_split or final_prefix->size_ == kp.size_ ) { if ( not check_split or final_prefix->size_ == kp.size_ ) {
real_insert(final_prefix, std::move(entry)); real_insert(final_prefix, std::move(entry));
} else { } else {
if ( vals->size() < MAX_NODE_ENTRY_COUNT ) if ( vals->size() < MAX_NODE_ENTRY_COUNT ) {
getRealPrefix(final_prefix, std::move(entry), real_insert); getRealPrefix(final_prefix, std::move(entry), real_insert);
else }
else {
split(*final_prefix, vals, entry, real_insert); split(*final_prefix, vals, entry, real_insert);
} }
} }
}
}, nullptr, cache_.lookup(kp), true }, nullptr, cache_.lookup(kp), true
); );
} }
...@@ -444,10 +449,8 @@ void Pht::split(Prefix insert, std::shared_ptr<std::vector<std::shared_ptr<Index ...@@ -444,10 +449,8 @@ void Pht::split(Prefix insert, std::shared_ptr<std::vector<std::shared_ptr<Index
auto loc = foundSplitLocation(full, vals); auto loc = foundSplitLocation(full, vals);
auto prefix_to_insert = std::make_shared<Prefix>(full.getPrefix(loc)); 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--) { for(;loc != insert.size_ - 1; loc--) {
std::cerr << "loc " << full.getPrefix(loc).toString() << std::endl;
updateCanary(full.getPrefix(loc)); updateCanary(full.getPrefix(loc));
} }
......
...@@ -269,7 +269,7 @@ void cmd_loop(std::shared_ptr<DhtRunner>& dht, std::map<std::string, indexation: ...@@ -269,7 +269,7 @@ void cmd_loop(std::shared_ptr<DhtRunner>& dht, std::map<std::string, indexation:
if (vals.empty()) if (vals.empty())
return; return;
std::cout << "Pht::lookup: found entries!" << std::endl std::cout << "Pht::lookup: found entries!" << std::endl
<< " prefix: \"" << p.toString() << "\"" << std::endl << p.toString() << std::endl
<< " hash: " << p.hash() << std::endl; << " hash: " << p.hash() << std::endl;
std::cout << " entries:" << std::endl; std::cout << " entries:" << std::endl;
for (auto v : vals) for (auto v : vals)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment