From 647de16f1cea688cc36e8d4a3dadd107cc95da2b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrien=20B=C3=A9raud?= <adrien.beraud@savoirfairelinux.com> Date: Fri, 16 Mar 2018 11:52:14 -0400 Subject: [PATCH] proxy client: implement getLocal(), getLocalById() --- include/opendht/dht_proxy_client.h | 9 ++----- src/dht_proxy_client.cpp | 16 ++++++++++++ src/op_cache.h | 39 ++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 7 deletions(-) diff --git a/include/opendht/dht_proxy_client.h b/include/opendht/dht_proxy_client.h index e3f82875..df3fe363 100644 --- a/include/opendht/dht_proxy_client.h +++ b/include/opendht/dht_proxy_client.h @@ -227,13 +227,8 @@ public: return types.getType(type_id); } - std::vector<Sp<Value>> getLocal(const InfoHash&, Value::Filter) const { - return {}; - } - - Sp<Value> getLocalById(const InfoHash&, Value::Id) const { - return {}; - } + std::vector<Sp<Value>> getLocal(const InfoHash& k, Value::Filter filter) const; + Sp<Value> getLocalById(const InfoHash& k, Value::Id id) const; /** * NOTE: The following methods will not be implemented because the diff --git a/src/dht_proxy_client.cpp b/src/dht_proxy_client.cpp index 8562bb39..9a5b2d05 100644 --- a/src/dht_proxy_client.cpp +++ b/src/dht_proxy_client.cpp @@ -84,6 +84,22 @@ DhtProxyClient::~DhtProxyClient() cancelAllListeners(); } +std::vector<Sp<Value>> +DhtProxyClient::getLocal(const InfoHash& k, Value::Filter filter) const { + auto s = listeners_.find(k); + if (s == listeners_.end()) + return {}; + return s->second.ops.get(filter); +} + +Sp<Value> +DhtProxyClient::getLocalById(const InfoHash& k, Value::Id id) const { + auto s = listeners_.find(k); + if (s == listeners_.end()) + return {}; + return s->second.ops.get(id); +} + void DhtProxyClient::cancelAllOperations() { diff --git a/src/op_cache.h b/src/op_cache.h index e20f8845..e2f26008 100644 --- a/src/op_cache.h +++ b/src/op_cache.h @@ -139,6 +139,23 @@ public: return listeners.empty(); } + std::vector<Sp<Value>> get(Value::Filter& filter) const { + std::vector<Sp<Value>> ret; + if (not filter) + ret.reserve(values.size()); + for (const auto& v : values) + if (not filter or filter(*v.second.data)) + ret.emplace_back(v.second.data); + return ret; + } + + Sp<Value> get(Value::Id id) const { + auto v = values.find(id); + if (v == values.end()) + return {}; + return v->second.data; + } + size_t searchToken; private: std::map<size_t, LocalListener> listeners; @@ -188,6 +205,28 @@ public: return false; } + std::vector<Sp<Value>> get(Value::Filter& filter) const { + if (ops.size() == 1) + return ops.begin()->second.get(filter); + std::map<Value::Id, Sp<Value>> c; + for (const auto& op : ops) { + for (const auto& v : op.second.get(filter)) + c.emplace(v->id, v); + } + std::vector<Sp<Value>> ret; + ret.reserve(c.size()); + for (auto& v : c) + ret.emplace_back(std::move(v.second)); + return ret; + } + + Sp<Value> get(Value::Id id) const { + for (const auto& op : ops) + if (auto v = op.second.get(id)) + return v; + return {}; + } + private: std::map<Sp<Query>, OpCache> ops; size_t nextToken_ {1}; -- GitLab