提交 7871c0f0 编写于 作者: L leipeng

async api: refactory: pass key value to callback & future

上级 e930462c
......@@ -2896,37 +2896,38 @@ DB::~DB() {}
void DB::GetAsync(const ReadOptions& ro, ColumnFamilyHandle* cfh,
std::string key, std::string* value,
std::function<void(const Status&)> cb) {
GetAsyncCallback cb) {
using namespace boost::fibers;
using std::move;
auto tls = getFiberTLS();
tls->update_fiber_count(ro.aio_concurrency);
tls->push([=,key=move(key),cb=move(cb)](){
cb(this->Get(ro, cfh, key, value));
tls->push([=,key=move(key),cb=move(cb)]()mutable{
auto s = this->Get(ro, cfh, key, value);
cb(move(s), move(key), value);
});
}
void DB::GetAsync(const ReadOptions& ro,
std::string key, std::string* value,
std::function<void(const Status&)> cb) {
GetAsyncCallback cb) {
using std::move;
GetAsync(ro, DefaultColumnFamily(), move(key), value, move(cb));
}
void DB::GetAsync(const ReadOptions& ro, ColumnFamilyHandle* cfh,
std::string key,
std::function<void(const Status&, std::string*)> cb) {
GetAsyncCallback cb) {
using namespace boost::fibers;
using std::move;
auto tls = getFiberTLS();
tls->update_fiber_count(ro.aio_concurrency);
tls->push([=,key=move(key),cb=move(cb)](){
tls->push([=,key=move(key),cb=move(cb)]()mutable{
std::string value;
Status s = this->Get(ro, cfh, key, &value);
cb(s, &value);
cb(move(s), move(key), &value);
});
}
void DB::GetAsync(const ReadOptions& ro,
std::string key,
std::function<void(const Status&, std::string*)> cb) {
GetAsyncCallback cb) {
using std::move;
GetAsync(ro, DefaultColumnFamily(), move(key), move(cb));
}
......@@ -2963,41 +2964,46 @@ struct DB_PromisePtr : boost::intrusive_ptr<DB_Promise<T> > {
: boost::intrusive_ptr<DB_Promise<T> >(new DB_Promise<T>(k)) {}
};
future<Status>
future<std::tuple<Status, std::string, std::string*> >
DB::GetFuture(const ReadOptions& ro, ColumnFamilyHandle* cfh,
std::string key, std::string* value) {
using namespace boost::fibers;
using std::move;
auto tls = getFiberTLS();
tls->update_fiber_count(ro.aio_concurrency);
DB_PromisePtr<Status> p(key);
future<Status> fu = p->pr.get_future();
DB_PromisePtr<std::tuple<Status, std::string, std::string*> > p(key);
auto fu = p->pr.get_future();
tls->push([this,ro,cfh,p,value](){
p->pr.set_value(this->Get(ro, cfh, p->key, value));
std::tuple<Status, std::string, std::string*> result;
std::get<0>(result) = this->Get(ro, cfh, p->key, value);
std::get<1>(result) = std::move(p->key);
std::get<2>(result) = value;
p->pr.set_value(std::move(result));
});
return fu;
}
future<Status>
future<std::tuple<Status, std::string, std::string*> >
DB::GetFuture(const ReadOptions& ro, std::string key, std::string* value) {
return GetFuture(ro, DefaultColumnFamily(), std::move(key), value);
}
future<std::pair<Status, std::string> >
future<std::tuple<Status, std::string, std::string> >
DB::GetFuture(const ReadOptions& ro, ColumnFamilyHandle* cfh, std::string key) {
using namespace boost::fibers;
using std::move;
auto tls = getFiberTLS();
tls->update_fiber_count(ro.aio_concurrency);
DB_PromisePtr<std::pair<Status, std::string> > p(key);
future<std::pair<Status, std::string> > fu = p->pr.get_future();
DB_PromisePtr<std::tuple<Status, std::string, std::string> > p(key);
auto fu = p->pr.get_future();
tls->push([this,ro,cfh,p](){
std::pair<Status, std::string> result;
result.first = this->Get(ro, cfh, p->key, &result.second);
std::tuple<Status, std::string, std::string> result;
std::get<0>(result) = this->Get(ro, cfh, p->key, &std::get<2>(result));
std::get<1>(result) = std::move(p->key);
p->pr.set_value(std::move(result));
});
return fu;
}
future<std::pair<Status, std::string> >
future<std::tuple<Status, std::string, std::string> >
DB::GetFuture(const ReadOptions& ro, std::string key) {
return GetFuture(ro, DefaultColumnFamily(), std::move(key));
}
......
......@@ -374,16 +374,17 @@ class DB {
return Get(options, DefaultColumnFamily(), key, value);
}
void GetAsync(const ReadOptions&, ColumnFamilyHandle*, std::string key, std::string* value, std::function<void(const Status&)>);
void GetAsync(const ReadOptions&, std::string key, std::string* value, std::function<void(const Status&)>);
void GetAsync(const ReadOptions&, ColumnFamilyHandle*, std::string key, std::function<void(const Status&, std::string*)>);
void GetAsync(const ReadOptions&, std::string key, std::function<void(const Status&, std::string*)>);
typedef std::function<void(Status&&, std::string&& key, std::string* value)> GetAsyncCallback;
void GetAsync(const ReadOptions&, ColumnFamilyHandle*, std::string key, std::string* value, GetAsyncCallback);
void GetAsync(const ReadOptions&, std::string key, std::string* value, GetAsyncCallback);
void GetAsync(const ReadOptions&, ColumnFamilyHandle*, std::string key, GetAsyncCallback);
void GetAsync(const ReadOptions&, std::string key, GetAsyncCallback);
int WaitAsync(int timeout_us);
int WaitAsync();
future<Status> GetFuture(const ReadOptions&, ColumnFamilyHandle*, std::string key, std::string* value);
future<Status> GetFuture(const ReadOptions&, std::string key, std::string* value);
future<std::pair<Status,std::string> > GetFuture(const ReadOptions&, ColumnFamilyHandle*, std::string key);
future<std::pair<Status,std::string> > GetFuture(const ReadOptions&, std::string key);
future<std::tuple<Status, std::string, std::string*> > GetFuture(const ReadOptions&, ColumnFamilyHandle*, std::string key, std::string* value);
future<std::tuple<Status, std::string, std::string*> > GetFuture(const ReadOptions&, std::string key, std::string* value);
future<std::tuple<Status, std::string, std::string > > GetFuture(const ReadOptions&, ColumnFamilyHandle*, std::string key);
future<std::tuple<Status, std::string, std::string > > GetFuture(const ReadOptions&, std::string key);
// If keys[i] does not exist in the database, then the i'th returned
// status will be one for which Status::IsNotFound() is true, and
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册