Skip to content
Snippets Groups Projects
Commit 647de16f authored by Adrien Béraud's avatar Adrien Béraud
Browse files

proxy client: implement getLocal(), getLocalById()

parent 2f4acd26
Branches
Tags
No related merge requests found
......@@ -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
......
......@@ -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()
{
......
......@@ -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};
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment