From 9ad21d5c5db2ec0028231c0a2e1c4b12c362d587 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Adrien=20B=C3=A9raud?= <adrien.beraud@savoirfairelinux.com>
Date: Wed, 18 May 2016 01:23:18 -0400
Subject: [PATCH] dht: remove newSearch()

---
 include/opendht/dht.h |  1 -
 src/dht.cpp           | 47 +++++++++++++------------------------------
 2 files changed, 14 insertions(+), 34 deletions(-)

diff --git a/include/opendht/dht.h b/include/opendht/dht.h
index 84c23b39..36b52612 100644
--- a/include/opendht/dht.h
+++ b/include/opendht/dht.h
@@ -456,7 +456,6 @@ private:
     void announce(const InfoHash& id, sa_family_t af, std::shared_ptr<Value> value, DoneCallback callback, time_point created=time_point::max());
     size_t listenTo(const InfoHash& id, sa_family_t af, GetCallback cb, Value::Filter f = Value::AllFilter());
 
-    std::shared_ptr<Search> newSearch(InfoHash id, sa_family_t af);
     void bootstrapSearch(Search& sr);
     Search *findSearch(unsigned short tid, sa_family_t af);
     void expireSearches();
diff --git a/src/dht.cpp b/src/dht.cpp
index 38b3c803..513f3b53 100644
--- a/src/dht.cpp
+++ b/src/dht.cpp
@@ -894,36 +894,6 @@ Dht::searchStep(std::shared_ptr<Search> sr)
         scheduler.edit(sr->nextSearchStep, sr->getNextStepTime(types, now));
 }
 
-
-std::shared_ptr<Dht::Search>
-Dht::newSearch(InfoHash id, sa_family_t af)
-{
-    auto& srs = af == AF_INET ? searches4 : searches6;
-    const auto& o = std::min_element(srs.begin(), srs.end(),
-        [](std::pair<const InfoHash, std::shared_ptr<Search>>& lsr, std::pair<const InfoHash, std::shared_ptr<Search>>& rsr) {
-            return lsr.second->done && rsr.second->step_time > lsr.second->step_time;
-        });
-    auto oldest = o != srs.end() ? o->second : nullptr;
-
-    /* The oldest slot is expired. */
-    if (oldest && oldest->announce.empty() && oldest->listeners.empty()
-            && oldest->step_time < scheduler.time() - SEARCH_EXPIRE_TIME)
-    {
-        DHT_LOG.WARN("Reusing expired search %s", oldest->id.toString().c_str());
-        return oldest;
-    }
-
-    /* Allocate a new slot. */
-    if (searches4.size() + searches6.size() < MAX_SEARCHES) {
-        auto sr = std::make_shared<Search>();
-        srs[id] = sr;
-        return sr;
-    }
-
-    /* Oh, well, never mind.  Reuse the oldest slot. */
-    return oldest;
-}
-
 /* Insert the contents of a bucket into a search structure. */
 unsigned
 Dht::Search::insertBucket(const Bucket& b, time_point now)
@@ -1190,9 +1160,20 @@ Dht::search(const InfoHash& id, sa_family_t af, GetCallback callback, DoneCallba
         sr->done = false;
         sr->expired = false;
     } else {
-        sr = newSearch(id, af);
-        if (not sr)
-            return {};
+        if (searches4.size() + searches6.size() < MAX_SEARCHES) {
+            sr = std::make_shared<Search>();
+            srs.emplace(id, sr);
+        } else {
+            for (auto it = srs.begin(); it!=srs.end();) {
+                auto& s = *it->second;
+                if ((s.done or s.expired) and s.announce.empty() and s.listeners.empty()) {
+                    sr = it->second;
+                    break;
+                }
+            }
+            if (not sr)
+                throw DhtException("Can't create search");
+        }
         sr->af = af;
         sr->tid = search_id++;
         sr->step_time = TIME_INVALID;
-- 
GitLab