diff --git a/include/opendht/indexation/pht.h b/include/opendht/indexation/pht.h index e6b37f8ebaa5161084e6a32a6bc18a3bd9a85cce..0d679414bfdf3fdeb97ecf041e2a16f282db6c5c 100644 --- a/include/opendht/indexation/pht.h +++ b/include/opendht/indexation/pht.h @@ -147,6 +147,7 @@ struct Prefix { 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); @@ -177,7 +178,9 @@ struct Prefix { Blob flags_ {}; Blob content_ {}; + private: + std::string blobToString(const Blob &bl) const { std::stringstream ss; @@ -392,32 +395,7 @@ private: std::shared_ptr<unsigned> max_common_prefix_len, int start = -1, bool all_values = false); - Prefix zcurve(const std::vector<Prefix>& all_prefix) const { - Prefix p; - if ( all_prefix.size() == 1 ) return all_prefix[0]; - - for ( size_t j = 0, bit = 0; j < all_prefix[0].content_.size(); j++) { - uint8_t mask = 0x80; - for ( int i = 0; i < 8; ) { - uint8_t content = 0; - uint8_t flags = 0; - for ( int k = 0 ; k < 8; k++, bit++ ) { - auto diff = k - i; - auto x = all_prefix[bit].content_[j] & mask; - auto y = all_prefix[bit].flags_[j] & mask; - content |= ( diff >= 0 ) ? x >> diff : x << std::abs(diff); - flags |= ( diff >= 0 ) ? y >> diff : y << std::abs(diff); - - if ( bit == all_prefix.size() - 1 ) { bit = -1; ++i; mask >>= 1; } - } - p.content_.push_back(content); - p.flags_.push_back(flags); - p.size_ += 8; - } - } - - return p; - } + Prefix zcurve(const std::vector<Prefix>& all_prefix) const; /** * Linearizes the key into a unidimensional key. A pht only takes @@ -459,7 +437,7 @@ private: size_t foundSplitLocation(Prefix compared, std::shared_ptr<std::vector<std::shared_ptr<IndexEntry>>> vals) { for ( size_t i = 0; i < compared.content_.size() * 8 - 1; i++ ) for ( auto const& v : *vals) - if ( Prefix(v->prefix).isActiveBit(i) != compared.isActiveBit(i) ) + if ( Prefix(v->prefix).isContentBitActive(i) != compared.isContentBitActive(i) ) return i + 1; return compared.content_.size() * 8 - 1; diff --git a/src/Makefile.am b/src/Makefile.am index 0a4b80e999b6e79a788abdc360a23bfd372c8268..74451a36d45775bedc0bd58b5f28a1669cf79b24 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -4,7 +4,7 @@ lib_LTLIBRARIES = libopendht.la AM_CPPFLAGS = -I../include/opendht libopendht_la_CXXFLAGS = @CXXFLAGS@ -libopendht_la_LDFLAGS = @LDFLAGS@ @GNUTLS_LIBS@ @nettle_LIBS@ -version-info 6:0:2 +libopendht_la_LDFLAGS = @LDFLAGS@ @GNUTLS_LIBS@ @nettle_LIBS@ libopendht_la_LIBADD = ./argon2/libargon2.la libopendht_la_SOURCES = \ diff --git a/src/indexation/pht.cpp b/src/indexation/pht.cpp index df80d8b42c3dcd155c7960823622e419a5077235..0fa380a8147f884accf08f6dec79ddff2f5fe70f 100644 --- a/src/indexation/pht.cpp +++ b/src/indexation/pht.cpp @@ -57,7 +57,7 @@ int Pht::Cache::lookup(const Prefix& p) { auto now = clock::now(), last_node_time = now; /* Before lookup remove the useless one [i.e. too old] */ - while ( leaves_.size() > 0 + while ( leaves_.size() > 0 and leaves_.begin()->first + NODE_EXPIRE_TIME < now ) { leaves_.erase(leaves_.begin()); @@ -215,8 +215,7 @@ void Pht::lookupStep(Prefix p, std::shared_ptr<int> lo, std::shared_ptr<int> hi, if (first_res->done) on_done(true); } - }, pht_filter); - + }, pht_filter); } else { on_done(true); } @@ -286,7 +285,7 @@ void Pht::insert(Prefix kp, IndexEntry entry, std::shared_ptr<int> lo, std::shar updateCanary(*p); checkPhtUpdate(*p, entry, time_p); cache_.insert(*p); - dht_->put(p->hash(), std::move(entry), done_cb /*, time_p */); + dht_->put(p->hash(), std::move(entry), done_cb , time_p); }; if ( not check_split or final_prefix->size_ == kp.size_ ) { @@ -302,46 +301,63 @@ void Pht::insert(Prefix kp, IndexEntry entry, std::shared_ptr<int> lo, std::shar ); } -Prefix linearize(Key k) const { +Prefix Pht::zcurve(const std::vector<Prefix>& all_prefix) const { + Prefix p; + + if ( all_prefix.size() == 1 ) + return all_prefix[0]; + + for ( size_t j = 0, bit = 0; j < all_prefix[0].content_.size(); j++) { + + uint8_t mask = 0x80; + for ( int i = 0; i < 8; ) { + + uint8_t flags = 0; + uint8_t content = 0; + + for ( int k = 0 ; k < 8; k++, bit++ ) { + + auto diff = k - i; + + auto x = all_prefix[bit].content_[j] & mask; + auto y = all_prefix[bit].flags_[j] & mask; + + content |= ( diff >= 0 ) ? x >> diff : x << std::abs(diff); + flags |= ( diff >= 0 ) ? y >> diff : y << std::abs(diff); + + if ( bit == all_prefix.size() - 1 ) { bit = -1; ++i; mask >>= 1; } + } + + p.content_.push_back(content); + p.flags_.push_back(flags); + p.size_ += 8; + } + } + + return p; +} + +Prefix Pht::linearize(Key k) const { if (not validKey(k)) { throw std::invalid_argument(INVALID_KEY); } + std::vector<Prefix> all_prefix; + all_prefix.reserve(k.size()); + auto max = std::max_element(keySpec_.begin(), keySpec_.end(), - [](const std::pair<string, size_t>& a, const std::pair<string, size_t>& b) { + [](const std::pair<std::string, size_t>& a, const std::pair<std::string, size_t>& b) { return a.second < b.second; - }); + })->second + 1; - for ( auto i = 0; i < k.size; i++ ) { - Prefix p = Blob {k.begin()->second.begin(), k.begin()->second.end()}; + for ( auto const& it : k ) { + Prefix p = Blob {it.second.begin(), it.second.end()}; p.addPaddingContent(max); p.updateFlags(); - all_prefix.push_back(p); + all_prefix.emplace_back(std::move(p)); } return zcurve(all_prefix); -/* - virtual Prefix linearize(Key k) const { - if (not validKey(k)) { throw std::invalid_argument(INVALID_KEY); } - - std::vector<Prefix> all_prefix; - auto max = std::max_element(keySpec_.begin(), keySpec_.end(), - [&](const std::pair<std::string, size_t>& a, const std::pair<std::string, size_t>& b) { - return a.second < b.second; - })->second + 1; - - for ( auto const& it : k ) { - Prefix p = Blob {it.second.begin(), it.second.end()}; - p.addPaddingContent(max); - p.updateFlags(); - all_prefix.push_back(p); - - auto bit_loc = p.size_ + 1; - for ( auto i = p.content_.size(); i < keySpec_.begin()->second + 1; i++ ) - p.content_.push_back(0); - - return zcurve(all_prefix); - };*/ -}; +} void Pht::getRealPrefix(std::shared_ptr<Prefix> p, IndexEntry entry, RealInsertCallback end_cb ) { @@ -428,9 +444,12 @@ 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--) + for(;loc != insert.size_ - 1; loc--) { + std::cerr << "loc " << full.getPrefix(loc).toString() << std::endl; updateCanary(full.getPrefix(loc)); + } end_cb(prefix_to_insert, entry); }