pegasus_client_impl.cpp 52.4 KB
Newer Older
Q
qinzuoyan 已提交
1 2 3 4 5 6 7 8 9
// Copyright (c) 2017, Xiaomi, Inc.  All rights reserved.
// This source code is licensed under the Apache License Version 2.0, which
// can be found in the LICENSE file in the root directory of this source tree.

#include <cctype>
#include <algorithm>
#include <string>
#include <stdint.h>

10
#include <dsn/tool-api/auto_codes.h>
11
#include <dsn/tool-api/group_address.h>
Q
qinzuoyan 已提交
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
#include <dsn/cpp/serialization_helper/dsn.layer2_types.h>
#include <rrdb/rrdb.code.definition.h>
#include <pegasus/error.h>
#include "pegasus_client_impl.h"

using namespace ::dsn;

namespace pegasus {
namespace client {

#define ROCSKDB_ERROR_START -1000

std::unordered_map<int, std::string> pegasus_client_impl::_client_error_to_string;
std::unordered_map<int, int> pegasus_client_impl::_server_error_to_client;

pegasus_client_impl::pegasus_client_impl(const char *cluster_name, const char *app_name)
    : _cluster_name(cluster_name), _app_name(app_name)
{
    _server_uri = "dsn://" + _cluster_name + "/" + _app_name;
31 32
    _server_uri_address.assign_uri(_server_uri.c_str());
    _client = new ::dsn::apps::rrdb_client(_server_uri_address);
Q
qinzuoyan 已提交
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52

    std::string section = "uri-resolver.dsn://" + _cluster_name;
    std::string server_list = dsn_config_get_value_string(section.c_str(), "arguments", "", "");
    std::vector<std::string> lv;
    ::dsn::utils::split_args(server_list.c_str(), lv, ',');
    std::vector<dsn::rpc_address> meta_servers;
    for (auto &s : lv) {
        ::dsn::rpc_address addr;
        if (!addr.from_string_ipv4(s.c_str())) {
            dassert(false,
                    "invalid address '%s' specified in config [%s].arguments",
                    s.c_str(),
                    section.c_str());
        }
        meta_servers.push_back(addr);
    }
    dassert(meta_servers.size() > 0,
            "no meta server specified in config [%s].arguments",
            section.c_str());

53
    _meta_server.assign_group("meta-servers");
Q
qinzuoyan 已提交
54
    for (auto &ms : meta_servers) {
55
        _meta_server.group_address()->add(ms);
Q
qinzuoyan 已提交
56 57 58
    }
}

59
pegasus_client_impl::~pegasus_client_impl() { delete _client; }
Q
qinzuoyan 已提交
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111

const char *pegasus_client_impl::get_cluster_name() const { return _cluster_name.c_str(); }

const char *pegasus_client_impl::get_app_name() const { return _app_name.c_str(); }

int pegasus_client_impl::set(const std::string &hash_key,
                             const std::string &sort_key,
                             const std::string &value,
                             int timeout_milliseconds,
                             int ttl_seconds,
                             internal_info *info)
{
    ::dsn::utils::notify_event op_completed;
    int ret = -1;
    auto callback = [&](int err, internal_info &&_info) {
        ret = err;
        if (info != nullptr)
            (*info) = std::move(_info);
        op_completed.notify();
    };
    async_set(hash_key, sort_key, value, std::move(callback), timeout_milliseconds, ttl_seconds);
    op_completed.wait();
    return ret;
}

void pegasus_client_impl::async_set(const std::string &hash_key,
                                    const std::string &sort_key,
                                    const std::string &value,
                                    async_set_callback_t &&callback,
                                    int timeout_milliseconds,
                                    int ttl_seconds)
{
    // check params
    if (hash_key.size() >= UINT16_MAX) {
        derror("invalid hash key: hash key length should be less than UINT16_MAX, but %d",
               (int)hash_key.size());
        if (callback != nullptr)
            callback(PERR_INVALID_HASH_KEY, internal_info());
        return;
    }
    ::dsn::apps::update_request req;
    pegasus_generate_key(req.key, hash_key, sort_key);
    req.value.assign(value.c_str(), 0, value.size());
    if (ttl_seconds == 0)
        req.expire_ts_seconds = 0;
    else
        req.expire_ts_seconds = ttl_seconds + utils::epoch_now();

    auto partition_hash = pegasus_key_hash(req.key);

    // wrap the user defined callback function, generate a new callback function.
    auto new_callback = [user_callback = std::move(callback)](
W
Wu Tao 已提交
112
        ::dsn::error_code err, dsn::message_ex * req, dsn::message_ex * resp)
Q
qinzuoyan 已提交
113 114 115 116 117 118 119 120 121 122 123 124 125 126
    {
        if (user_callback == nullptr) {
            return;
        }
        internal_info info;
        ::dsn::apps::update_response response;
        if (err == ::dsn::ERR_OK) {
            ::dsn::unmarshall(resp, response);
            info.app_id = response.app_id;
            info.partition_index = response.partition_index;
            info.decree = response.decree;
            info.server = response.server;
        }
        auto ret = get_client_error(
127
            (err == ::dsn::ERR_OK) ? get_rocksdb_server_error(response.error) : int(err));
Q
qinzuoyan 已提交
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
        user_callback(ret, std::move(info));
    };
    _client->put(req,
                 std::move(new_callback),
                 std::chrono::milliseconds(timeout_milliseconds),
                 0,
                 partition_hash);
}

int pegasus_client_impl::multi_set(const std::string &hash_key,
                                   const std::map<std::string, std::string> &kvs,
                                   int timeout_milliseconds,
                                   int ttl_seconds,
                                   internal_info *info)
{
    ::dsn::utils::notify_event op_completed;
    int ret = -1;
    auto callback = [&](int err, internal_info &&_info) {
        ret = err;
        if (info != nullptr)
            (*info) = std::move(_info);
        op_completed.notify();
    };
    async_multi_set(hash_key, kvs, std::move(callback), timeout_milliseconds, ttl_seconds);
    op_completed.wait();
    return ret;
}

void pegasus_client_impl::async_multi_set(const std::string &hash_key,
                                          const std::map<std::string, std::string> &kvs,
                                          async_multi_set_callback_t &&callback,
                                          int timeout_milliseconds,
                                          int ttl_seconds)
{
    // check params
    if (hash_key.size() == 0) {
        derror("invalid hash key: hash key should not be empty for multi_set");
        if (callback != nullptr)
            callback(PERR_INVALID_HASH_KEY, internal_info());
        return;
    }
    if (hash_key.size() >= UINT16_MAX) {
        derror("invalid hash key: hash key length should be less than UINT16_MAX, but %d",
               (int)hash_key.size());
        if (callback != nullptr)
            callback(PERR_INVALID_HASH_KEY, internal_info());
        return;
    }
    if (kvs.empty()) {
        derror("invalid kvs: kvs should not be empty");
        if (callback != nullptr)
            callback(PERR_INVALID_VALUE, internal_info());
        return;
    }

    ::dsn::apps::multi_put_request req;
    req.hash_key = ::dsn::blob(hash_key.data(), 0, hash_key.size());
    for (auto &kv : kvs) {
        ::dsn::apps::key_value kv_blob;
        kv_blob.key = ::dsn::blob(kv.first.data(), 0, kv.first.size());
        kv_blob.value = ::dsn::blob(kv.second.data(), 0, kv.second.size());
        req.kvs.emplace_back(std::move(kv_blob));
    }
    if (ttl_seconds == 0)
        req.expire_ts_seconds = 0;
    else
        req.expire_ts_seconds = ttl_seconds + utils::epoch_now();

    ::dsn::blob tmp_key;
    pegasus_generate_key(tmp_key, req.hash_key, ::dsn::blob());
    auto partition_hash = pegasus_key_hash(tmp_key);
    // wrap the user-defined-callback-function, generate a new callback function.
    auto new_callback = [user_callback = std::move(callback)](
W
Wu Tao 已提交
201
        ::dsn::error_code err, dsn::message_ex * req, dsn::message_ex * resp)
Q
qinzuoyan 已提交
202 203 204 205 206 207 208 209 210 211 212 213 214 215
    {
        if (user_callback == nullptr) {
            return;
        }
        internal_info info;
        ::dsn::apps::update_response response;
        if (err == ::dsn::ERR_OK) {
            ::dsn::unmarshall(resp, response);
            info.app_id = response.app_id;
            info.partition_index = response.partition_index;
            info.decree = response.decree;
            info.server = response.server;
        }
        auto ret = get_client_error(
216
            (err == ::dsn::ERR_OK) ? get_rocksdb_server_error(response.error) : int(err));
Q
qinzuoyan 已提交
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
        user_callback(ret, std::move(info));
    };
    _client->multi_put(req,
                       std::move(new_callback),
                       std::chrono::milliseconds(timeout_milliseconds),
                       0,
                       partition_hash);
}

int pegasus_client_impl::get(const std::string &hash_key,
                             const std::string &sort_key,
                             std::string &value,
                             int timeout_milliseconds,
                             internal_info *info)
{
    ::dsn::utils::notify_event op_completed;
    int ret = -1;
    auto callback = [&](int err, std::string &&str, internal_info &&_info) {
        ret = err;
        value = std::move(str);
        if (info != nullptr)
            (*info) = std::move(_info);
        op_completed.notify();
    };
    async_get(hash_key, sort_key, std::move(callback), timeout_milliseconds);
    op_completed.wait();
    return ret;
}

void pegasus_client_impl::async_get(const std::string &hash_key,
                                    const std::string &sort_key,
                                    async_get_callback_t &&callback,
                                    int timeout_milliseconds)
{
    // check params
    if (hash_key.size() >= UINT16_MAX) {
        derror("invalid hash key: hash key length should be less than UINT16_MAX, but %d",
               (int)hash_key.size());
        if (callback != nullptr)
            callback(PERR_INVALID_HASH_KEY, std::string(), internal_info());
        return;
    }
    ::dsn::blob req;
    pegasus_generate_key(req, hash_key, sort_key);
    auto partition_hash = pegasus_key_hash(req);
    auto new_callback = [user_callback = std::move(callback)](
W
Wu Tao 已提交
263
        ::dsn::error_code err, dsn::message_ex * req, dsn::message_ex * resp)
Q
qinzuoyan 已提交
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
    {
        if (user_callback == nullptr) {
            return;
        }
        std::string value;
        internal_info info;
        dsn::apps::read_response response;
        if (err == ::dsn::ERR_OK) {
            ::dsn::unmarshall(resp, response);
            if (response.error == 0) {
                value.assign(response.value.data(), response.value.length());
            }
            info.app_id = response.app_id;
            info.partition_index = response.partition_index;
            info.server = response.server;
        }
        int ret =
281
            get_client_error(err == ERR_OK ? get_rocksdb_server_error(response.error) : int(err));
Q
qinzuoyan 已提交
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
        user_callback(ret, std::move(value), std::move(info));
    };
    _client->get(req,
                 std::move(new_callback),
                 std::chrono::milliseconds(timeout_milliseconds),
                 0,
                 partition_hash);
}

int pegasus_client_impl::multi_get(const std::string &hash_key,
                                   const std::set<std::string> &sort_keys,
                                   std::map<std::string, std::string> &values,
                                   int max_fetch_count,
                                   int max_fetch_size,
                                   int timeout_milliseconds,
                                   internal_info *info)
{
    ::dsn::utils::notify_event op_completed;
    int ret = -1;
    auto callback =
        [&](int err, std::map<std::string, std::string> &&_values, internal_info &&_info) {
            ret = err;
            if (info != nullptr)
                (*info) = std::move(_info);
            values = std::move(_values);
            op_completed.notify();
        };
    async_multi_get(hash_key,
                    sort_keys,
                    std::move(callback),
                    max_fetch_count,
                    max_fetch_size,
                    timeout_milliseconds);
    op_completed.wait();
    return ret;
}

void pegasus_client_impl::async_multi_get(const std::string &hash_key,
                                          const std::set<std::string> &sort_keys,
                                          async_multi_get_callback_t &&callback,
                                          int max_fetch_count,
                                          int max_fetch_size,
                                          int timeout_milliseconds)
{
    // check params
    if (hash_key.size() == 0) {
328
        derror("invalid hash key: hash key should not be empty");
Q
qinzuoyan 已提交
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
        if (callback != nullptr)
            callback(PERR_INVALID_HASH_KEY, std::map<std::string, std::string>(), internal_info());
        return;
    }
    if (hash_key.size() >= UINT16_MAX) {
        derror("invalid hash key: hash key length should be less than UINT16_MAX, but %d",
               (int)hash_key.size());
        if (callback != nullptr)
            callback(PERR_INVALID_HASH_KEY, std::map<std::string, std::string>(), internal_info());
        return;
    }

    ::dsn::apps::multi_get_request req;
    req.hash_key = ::dsn::blob(hash_key.data(), 0, hash_key.size());
    req.max_kv_count = max_fetch_count;
    req.max_kv_size = max_fetch_size;
345 346
    req.start_inclusive = true;
    req.stop_inclusive = false;
Q
qinzuoyan 已提交
347 348 349
    for (auto &sort_key : sort_keys) {
        req.sort_keys.emplace_back(sort_key.data(), 0, sort_key.size());
    }
350 351 352 353
    ::dsn::blob tmp_key;
    pegasus_generate_key(tmp_key, req.hash_key, ::dsn::blob());
    auto partition_hash = pegasus_key_hash(tmp_key);
    auto new_callback = [user_callback = std::move(callback)](
W
Wu Tao 已提交
354
        ::dsn::error_code err, dsn::message_ex * req, dsn::message_ex * resp)
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
    {
        if (user_callback == nullptr) {
            return;
        }
        std::map<std::string, std::string> values;
        internal_info info;
        ::dsn::apps::multi_get_response response;
        if (err == ::dsn::ERR_OK) {
            ::unmarshall(resp, response);
            info.app_id = response.app_id;
            info.partition_index = response.partition_index;
            info.server = response.server;
            for (auto &kv : response.kvs)
                values.emplace(std::string(kv.key.data(), kv.key.length()),
                               std::string(kv.value.data(), kv.value.length()));
        }
        int ret =
Q
qinzuoyan 已提交
372
            get_client_error(err == ERR_OK ? get_rocksdb_server_error(response.error) : int(err));
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
        user_callback(ret, std::move(values), std::move(info));
    };
    _client->multi_get(req,
                       std::move(new_callback),
                       std::chrono::milliseconds(timeout_milliseconds),
                       0,
                       partition_hash);
}

int pegasus_client_impl::multi_get(const std::string &hash_key,
                                   const std::string &start_sortkey,
                                   const std::string &stop_sortkey,
                                   const multi_get_options &options,
                                   std::map<std::string, std::string> &values,
                                   int max_fetch_count,
                                   int max_fetch_size,
                                   int timeout_milliseconds,
                                   internal_info *info)
{
    ::dsn::utils::notify_event op_completed;
    int ret = -1;
    auto callback =
        [&](int err, std::map<std::string, std::string> &&_values, internal_info &&_info) {
            ret = err;
            if (info != nullptr)
                (*info) = std::move(_info);
            values = std::move(_values);
            op_completed.notify();
        };
    async_multi_get(hash_key,
                    start_sortkey,
                    stop_sortkey,
                    options,
                    std::move(callback),
                    max_fetch_count,
                    max_fetch_size,
                    timeout_milliseconds);
    op_completed.wait();
    return ret;
}

void pegasus_client_impl::async_multi_get(const std::string &hash_key,
                                          const std::string &start_sortkey,
                                          const std::string &stop_sortkey,
                                          const multi_get_options &options,
                                          async_multi_get_callback_t &&callback,
                                          int max_fetch_count,
                                          int max_fetch_size,
                                          int timeout_milliseconds)
{
    // check params
    if (hash_key.size() == 0) {
        derror("invalid hash key: hash key should not be empty");
        if (callback != nullptr)
            callback(PERR_INVALID_HASH_KEY, std::map<std::string, std::string>(), internal_info());
        return;
    }
    if (hash_key.size() >= UINT16_MAX) {
        derror("invalid hash key: hash key length should be less than UINT16_MAX, but %d",
               (int)hash_key.size());
        if (callback != nullptr)
            callback(PERR_INVALID_HASH_KEY, std::map<std::string, std::string>(), internal_info());
        return;
    }

    ::dsn::apps::multi_get_request req;
    req.hash_key = ::dsn::blob(hash_key.data(), 0, hash_key.size());
    req.start_sortkey = ::dsn::blob(start_sortkey.data(), 0, start_sortkey.size());
    req.stop_sortkey = ::dsn::blob(stop_sortkey.data(), 0, stop_sortkey.size());
    req.start_inclusive = options.start_inclusive;
    req.stop_inclusive = options.stop_inclusive;
    req.max_kv_count = max_fetch_count;
    req.max_kv_size = max_fetch_size;
    req.no_value = options.no_value;
447
    req.reverse = options.reverse;
448 449 450
    req.sort_key_filter_type = (dsn::apps::filter_type::type)options.sort_key_filter_type;
    req.sort_key_filter_pattern = ::dsn::blob(
        options.sort_key_filter_pattern.data(), 0, options.sort_key_filter_pattern.size());
Q
qinzuoyan 已提交
451 452 453 454
    ::dsn::blob tmp_key;
    pegasus_generate_key(tmp_key, req.hash_key, ::dsn::blob());
    auto partition_hash = pegasus_key_hash(tmp_key);
    auto new_callback = [user_callback = std::move(callback)](
W
Wu Tao 已提交
455
        ::dsn::error_code err, dsn::message_ex * req, dsn::message_ex * resp)
Q
qinzuoyan 已提交
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
    {
        if (user_callback == nullptr) {
            return;
        }
        std::map<std::string, std::string> values;
        internal_info info;
        ::dsn::apps::multi_get_response response;
        if (err == ::dsn::ERR_OK) {
            ::unmarshall(resp, response);
            info.app_id = response.app_id;
            info.partition_index = response.partition_index;
            info.server = response.server;
            for (auto &kv : response.kvs)
                values.emplace(std::string(kv.key.data(), kv.key.length()),
                               std::string(kv.value.data(), kv.value.length()));
        }
        int ret =
473
            get_client_error(err == ERR_OK ? get_rocksdb_server_error(response.error) : int(err));
Q
qinzuoyan 已提交
474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534
        user_callback(ret, std::move(values), std::move(info));
    };
    _client->multi_get(req,
                       std::move(new_callback),
                       std::chrono::milliseconds(timeout_milliseconds),
                       0,
                       partition_hash);
}

int pegasus_client_impl::multi_get_sortkeys(const std::string &hash_key,
                                            std::set<std::string> &sort_keys,
                                            int max_fetch_count,
                                            int max_fetch_size,
                                            int timeout_milliseconds,
                                            internal_info *info)
{
    ::dsn::utils::notify_event op_completed;
    int ret = -1;
    auto callback = [&](int err, std::set<std::string> &&_sort_keys, internal_info &&_info) {
        ret = err;
        if (info != nullptr)
            (*info) = std::move(_info);
        sort_keys = std::move(_sort_keys);
        op_completed.notify();
    };
    async_multi_get_sortkeys(
        hash_key, std::move(callback), max_fetch_count, max_fetch_size, timeout_milliseconds);
    op_completed.wait();
    return ret;
}

void pegasus_client_impl::async_multi_get_sortkeys(const std::string &hash_key,
                                                   async_multi_get_sortkeys_callback_t &&callback,
                                                   int max_fetch_count,
                                                   int max_fetch_size,
                                                   int timeout_milliseconds)
{
    // check params
    if (hash_key.size() == 0) {
        derror("invalid hash key: hash key should not be empty for multi_get_sortkeys");
        if (callback != nullptr)
            callback(PERR_INVALID_HASH_KEY, std::set<std::string>(), internal_info());
        return;
    }
    if (hash_key.size() >= UINT16_MAX) {
        derror("invalid hash key: hash key length should be less than UINT16_MAX, but %d",
               (int)hash_key.size());
        if (callback != nullptr)
            callback(PERR_INVALID_HASH_KEY, std::set<std::string>(), internal_info());
        return;
    }

    ::dsn::apps::multi_get_request req;
    req.hash_key = ::dsn::blob(hash_key.data(), 0, hash_key.size());
    req.max_kv_count = max_fetch_count;
    req.max_kv_size = max_fetch_size;
    req.no_value = true;
    ::dsn::blob tmp_key;
    pegasus_generate_key(tmp_key, req.hash_key, ::dsn::blob());
    auto partition_hash = pegasus_key_hash(tmp_key);
    auto new_callback = [user_callback = std::move(callback)](
W
Wu Tao 已提交
535
        ::dsn::error_code err, dsn::message_ex * req, dsn::message_ex * resp)
Q
qinzuoyan 已提交
536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551
    {
        if (user_callback == nullptr) {
            return;
        }
        std::set<std::string> sort_keys;
        internal_info info;
        ::dsn::apps::multi_get_response response;
        if (err == ::dsn::ERR_OK) {
            ::unmarshall(resp, response);
            info.app_id = response.app_id;
            info.partition_index = response.partition_index;
            info.server = response.server;
            for (auto &kv : response.kvs)
                sort_keys.insert(std::string(kv.key.data(), kv.key.length()));
        }
        int ret =
552
            get_client_error(err == ERR_OK ? get_rocksdb_server_error(response.error) : int(err));
Q
qinzuoyan 已提交
553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609
        user_callback(ret, std::move(sort_keys), std::move(info));
    };
    _client->multi_get(req,
                       std::move(new_callback),
                       std::chrono::milliseconds(timeout_milliseconds),
                       0,
                       partition_hash);
}

int pegasus_client_impl::exist(const std::string &hash_key,
                               const std::string &sort_key,
                               int timeout_milliseconds,
                               internal_info *info)
{
    int ttl_seconds;
    return ttl(hash_key, sort_key, ttl_seconds, timeout_milliseconds, info);
}

int pegasus_client_impl::sortkey_count(const std::string &hash_key,
                                       int64_t &count,
                                       int timeout_milliseconds,
                                       internal_info *info)
{
    // check params
    if (hash_key.size() == 0) {
        derror("invalid hash key: hash key should not be empty for sortkey_count");
        return PERR_INVALID_HASH_KEY;
    }
    if (hash_key.size() >= UINT16_MAX) {
        derror("invalid hash key: hash key length should be less than UINT16_MAX, but %d",
               (int)hash_key.size());
        return PERR_INVALID_HASH_KEY;
    }

    ::dsn::blob tmp_key;
    pegasus_generate_key(tmp_key, hash_key, std::string());
    auto partition_hash = pegasus_key_hash(tmp_key);
    auto pr = _client->sortkey_count_sync(::dsn::blob(hash_key.data(), 0, hash_key.length()),
                                          std::chrono::milliseconds(timeout_milliseconds),
                                          0,
                                          partition_hash);
    if (pr.first == ERR_OK && pr.second.error == 0) {
        count = pr.second.count;
    }
    if (info != nullptr) {
        if (pr.first == ERR_OK) {
            info->app_id = pr.second.app_id;
            info->partition_index = pr.second.partition_index;
            info->decree = -1;
            info->server = pr.second.server;
        } else {
            info->app_id = -1;
            info->partition_index = -1;
            info->decree = -1;
        }
    }
    return get_client_error(pr.first == ERR_OK ? get_rocksdb_server_error(pr.second.error)
610
                                               : int(pr.first));
Q
qinzuoyan 已提交
611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649
}

int pegasus_client_impl::del(const std::string &hash_key,
                             const std::string &sort_key,
                             int timeout_milliseconds,
                             internal_info *info)
{
    ::dsn::utils::notify_event op_completed;
    int ret = -1;
    auto callback = [&](int err, internal_info &&_info) {
        ret = err;
        if (info != nullptr)
            (*info) = std::move(_info);
        op_completed.notify();
    };
    async_del(hash_key, sort_key, std::move(callback), timeout_milliseconds);
    op_completed.wait();
    return ret;
}

void pegasus_client_impl::async_del(const std::string &hash_key,
                                    const std::string &sort_key,
                                    async_del_callback_t &&callback,
                                    int timeout_milliseconds)
{
    // check params
    if (hash_key.size() >= UINT16_MAX) {
        derror("invalid hash key: hash key length should be less than UINT16_MAX, but %d",
               (int)hash_key.size());
        if (callback != nullptr)
            callback(PERR_INVALID_HASH_KEY, internal_info());
        return;
    }

    ::dsn::blob req;
    pegasus_generate_key(req, hash_key, sort_key);
    auto partition_hash = pegasus_key_hash(req);

    auto new_callback = [user_callback = std::move(callback)](
W
Wu Tao 已提交
650
        ::dsn::error_code err, dsn::message_ex * req, dsn::message_ex * resp)
Q
qinzuoyan 已提交
651 652 653 654 655 656 657 658 659 660 661 662 663 664
    {
        if (user_callback == nullptr) {
            return;
        }
        ::dsn::apps::update_response response;
        internal_info info;
        if (err == ::dsn::ERR_OK) {
            ::dsn::unmarshall(resp, response);
            info.app_id = response.app_id;
            info.partition_index = response.partition_index;
            info.decree = response.decree;
            info.server = response.server;
        }
        int ret =
665
            get_client_error(err == ERR_OK ? get_rocksdb_server_error(response.error) : int(err));
Q
qinzuoyan 已提交
666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731
        user_callback(ret, std::move(info));
    };
    _client->remove(req,
                    std::move(new_callback),
                    std::chrono::milliseconds(timeout_milliseconds),
                    0,
                    partition_hash);
}

int pegasus_client_impl::multi_del(const std::string &hash_key,
                                   const std::set<std::string> &sort_keys,
                                   int64_t &deleted_count,
                                   int timeout_milliseconds,
                                   internal_info *info)
{
    ::dsn::utils::notify_event op_completed;
    int ret = -1;
    auto callback = [&](int err, int64_t _deleted_count, internal_info &&_info) {
        ret = err;
        deleted_count = _deleted_count;
        if (info != nullptr)
            (*info) = std::move(_info);
        op_completed.notify();
    };
    async_multi_del(hash_key, sort_keys, std::move(callback), timeout_milliseconds);
    op_completed.wait();
    return ret;
}

void pegasus_client_impl::async_multi_del(const std::string &hash_key,
                                          const std::set<std::string> &sort_keys,
                                          async_multi_del_callback_t &&callback,
                                          int timeout_milliseconds)
{
    // check params
    if (hash_key.size() == 0) {
        derror("invalid hash key: hash key should not be empty for multi_del");
        if (callback != nullptr)
            callback(PERR_INVALID_HASH_KEY, 0, internal_info());
        return;
    }
    if (hash_key.size() >= UINT16_MAX) {
        derror("invalid hash key: hash key length should be less than UINT16_MAX, but %d",
               (int)hash_key.size());
        if (callback != nullptr)
            callback(PERR_INVALID_HASH_KEY, 0, internal_info());
        return;
    }
    if (sort_keys.empty()) {
        derror("invalid sort keys: should not be empty");
        if (callback != nullptr)
            callback(PERR_INVALID_VALUE, 0, internal_info());
        return;
    }

    ::dsn::apps::multi_remove_request req;
    req.hash_key = ::dsn::blob(hash_key.data(), 0, hash_key.size());
    for (auto &sort_key : sort_keys) {
        req.sort_keys.emplace_back(sort_key.data(), 0, sort_key.size());
    }

    ::dsn::blob tmp_key;
    pegasus_generate_key(tmp_key, req.hash_key, ::dsn::blob());
    auto partition_hash = pegasus_key_hash(tmp_key);

    auto new_callback = [user_callback = std::move(callback)](
W
Wu Tao 已提交
732
        ::dsn::error_code err, dsn::message_ex * req, dsn::message_ex * resp)
Q
qinzuoyan 已提交
733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748
    {
        if (user_callback == nullptr) {
            return;
        }
        ::dsn::apps::multi_remove_response response;
        internal_info info;
        int64_t deleted_count = 0;
        if (err == ::dsn::ERR_OK) {
            ::dsn::unmarshall(resp, response);
            info.app_id = response.app_id;
            info.partition_index = response.partition_index;
            info.decree = response.decree;
            info.server = response.server;
            deleted_count = response.count;
        }
        int ret =
749
            get_client_error(err == ERR_OK ? get_rocksdb_server_error(response.error) : int(err));
Q
qinzuoyan 已提交
750 751 752 753 754 755 756 757 758
        user_callback(ret, deleted_count, std::move(info));
    };
    _client->multi_remove(req,
                          std::move(new_callback),
                          std::chrono::milliseconds(timeout_milliseconds),
                          0,
                          partition_hash);
}

Q
QinZuoyan 已提交
759 760 761 762 763
int pegasus_client_impl::incr(const std::string &hash_key,
                              const std::string &sort_key,
                              int64_t increment,
                              int64_t &new_value,
                              int timeout_milliseconds,
764
                              int ttl_seconds,
Q
QinZuoyan 已提交
765 766 767 768 769 770 771 772 773 774 775
                              internal_info *info)
{
    ::dsn::utils::notify_event op_completed;
    int ret = -1;
    auto callback = [&](int _err, int64_t _new_value, internal_info &&_info) {
        ret = _err;
        new_value = _new_value;
        if (info != nullptr)
            (*info) = std::move(_info);
        op_completed.notify();
    };
776 777
    async_incr(
        hash_key, sort_key, increment, std::move(callback), timeout_milliseconds, ttl_seconds);
Q
QinZuoyan 已提交
778 779 780 781 782 783 784 785
    op_completed.wait();
    return ret;
}

void pegasus_client_impl::async_incr(const std::string &hash_key,
                                     const std::string &sort_key,
                                     int64_t increment,
                                     async_incr_callback_t &&callback,
786 787
                                     int timeout_milliseconds,
                                     int ttl_seconds)
Q
QinZuoyan 已提交
788 789 790 791 792 793 794 795 796
{
    // check params
    if (hash_key.size() >= UINT16_MAX) {
        derror("invalid hash key: hash key length should be less than UINT16_MAX, but %d",
               (int)hash_key.size());
        if (callback != nullptr)
            callback(PERR_INVALID_HASH_KEY, 0, internal_info());
        return;
    }
797 798 799 800 801 802
    if (ttl_seconds < -1) {
        derror("invalid ttl seconds: should be no less than -1, but %d", ttl_seconds);
        if (callback != nullptr)
            callback(PERR_INVALID_ARGUMENT, 0, internal_info());
        return;
    }
Q
QinZuoyan 已提交
803 804 805 806

    ::dsn::apps::incr_request req;
    pegasus_generate_key(req.key, hash_key, sort_key);
    req.increment = increment;
807 808 809 810
    if (ttl_seconds <= 0)
        req.expire_ts_seconds = ttl_seconds;
    else
        req.expire_ts_seconds = ttl_seconds + utils::epoch_now();
Q
QinZuoyan 已提交
811 812 813
    auto partition_hash = pegasus_key_hash(req.key);

    auto new_callback = [user_callback = std::move(callback)](
814
        ::dsn::error_code err, dsn::message_ex * req, dsn::message_ex * resp)
Q
QinZuoyan 已提交
815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838
    {
        if (user_callback == nullptr) {
            return;
        }
        ::dsn::apps::incr_response response;
        internal_info info;
        if (err == ::dsn::ERR_OK) {
            ::dsn::unmarshall(resp, response);
            info.app_id = response.app_id;
            info.partition_index = response.partition_index;
            info.decree = response.decree;
            info.server = response.server;
        }
        int ret =
            get_client_error(err == ERR_OK ? get_rocksdb_server_error(response.error) : int(err));
        user_callback(ret, response.new_value, std::move(info));
    };
    _client->incr(req,
                  std::move(new_callback),
                  std::chrono::milliseconds(timeout_milliseconds),
                  0,
                  partition_hash);
}

Q
QinZuoyan 已提交
839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890
int pegasus_client_impl::check_and_set(const std::string &hash_key,
                                       const std::string &check_sort_key,
                                       cas_check_type check_type,
                                       const std::string &check_operand,
                                       const std::string &set_sort_key,
                                       const std::string &set_value,
                                       const check_and_set_options &options,
                                       check_and_set_results &results,
                                       int timeout_milliseconds,
                                       internal_info *info)
{
    ::dsn::utils::notify_event op_completed;
    int ret = -1;
    auto callback = [&](int _err, check_and_set_results &&_results, internal_info &&_info) {
        ret = _err;
        results = std::move(_results);
        if (info != nullptr)
            (*info) = std::move(_info);
        op_completed.notify();
    };
    async_check_and_set(hash_key,
                        check_sort_key,
                        check_type,
                        check_operand,
                        set_sort_key,
                        set_value,
                        options,
                        std::move(callback),
                        timeout_milliseconds);
    op_completed.wait();
    return ret;
}

void pegasus_client_impl::async_check_and_set(const std::string &hash_key,
                                              const std::string &check_sort_key,
                                              cas_check_type check_type,
                                              const std::string &check_operand,
                                              const std::string &set_sort_key,
                                              const std::string &set_value,
                                              const check_and_set_options &options,
                                              async_check_and_set_callback_t &&callback,
                                              int timeout_milliseconds)
{
    // check params
    if (hash_key.size() >= UINT16_MAX) {
        derror("invalid hash key: hash key length should be less than UINT16_MAX, but %d",
               (int)hash_key.size());
        if (callback != nullptr)
            callback(PERR_INVALID_HASH_KEY, check_and_set_results(), internal_info());
        return;
    }

891 892 893 894 895 896 897 898
    if (dsn::apps::_cas_check_type_VALUES_TO_NAMES.find(check_type) ==
        dsn::apps::_cas_check_type_VALUES_TO_NAMES.end()) {
        derror("invalid check type: %d", (int)check_type);
        if (callback != nullptr)
            callback(PERR_INVALID_ARGUMENT, check_and_set_results(), internal_info());
        return;
    }

Q
QinZuoyan 已提交
899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918
    ::dsn::apps::check_and_set_request req;
    req.hash_key.assign(hash_key.c_str(), 0, hash_key.size());
    req.check_sort_key.assign(check_sort_key.c_str(), 0, check_sort_key.size());
    req.check_type = (dsn::apps::cas_check_type::type)check_type;
    req.check_operand.assign(check_operand.c_str(), 0, check_operand.size());
    if (check_sort_key != set_sort_key) {
        req.set_diff_sort_key = true;
        req.set_sort_key.assign(set_sort_key.c_str(), 0, set_sort_key.size());
    }
    req.set_value.assign(set_value.c_str(), 0, set_value.size());
    if (options.set_value_ttl_seconds == 0)
        req.set_expire_ts_seconds = 0;
    else
        req.set_expire_ts_seconds = options.set_value_ttl_seconds + utils::epoch_now();
    req.return_check_value = options.return_check_value;

    ::dsn::blob tmp_key;
    pegasus_generate_key(tmp_key, req.hash_key, ::dsn::blob());
    auto partition_hash = pegasus_key_hash(tmp_key);
    auto new_callback = [user_callback = std::move(callback)](
W
Wu Tao 已提交
919
        ::dsn::error_code err, dsn::message_ex * req, dsn::message_ex * resp)
Q
QinZuoyan 已提交
920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960
    {
        if (user_callback == nullptr) {
            return;
        }
        check_and_set_results results;
        internal_info info;
        ::dsn::apps::check_and_set_response response;
        if (err == ::dsn::ERR_OK) {
            ::dsn::unmarshall(resp, response);
            if (response.error == 0) {
                results.set_succeed = true;
            } else if (response.error == 13) { // kTryAgain
                results.set_succeed = false;
                response.error = 0;
            } else {
                results.set_succeed = false;
            }
            if (response.check_value_returned) {
                results.check_value_returned = true;
                if (response.check_value_exist) {
                    results.check_value_exist = true;
                    results.check_value.assign(response.check_value.data(),
                                               response.check_value.length());
                }
            }
            info.app_id = response.app_id;
            info.partition_index = response.partition_index;
            info.decree = response.decree;
            info.server = response.server;
        }
        int ret =
            get_client_error(err == ERR_OK ? get_rocksdb_server_error(response.error) : int(err));
        user_callback(ret, std::move(results), std::move(info));
    };
    _client->check_and_set(req,
                           std::move(new_callback),
                           std::chrono::milliseconds(timeout_milliseconds),
                           0,
                           partition_hash);
}

961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028
int pegasus_client_impl::check_and_mutate(const std::string &hash_key,
                                          const std::string &check_sort_key,
                                          cas_check_type check_type,
                                          const std::string &check_operand,
                                          const mutations &mutations,
                                          const check_and_mutate_options &options,
                                          check_and_mutate_results &results,
                                          int timeout_milliseconds,
                                          internal_info *info)
{
    ::dsn::utils::notify_event op_completed;
    int ret = -1;
    auto callback = [&](int _err, check_and_mutate_results &&_results, internal_info &&_info) {
        ret = _err;
        results = std::move(_results);
        if (info != nullptr)
            (*info) = std::move(_info);
        op_completed.notify();
    };
    async_check_and_mutate(hash_key,
                           check_sort_key,
                           check_type,
                           check_operand,
                           mutations,
                           options,
                           std::move(callback),
                           timeout_milliseconds);
    op_completed.wait();
    return ret;
}

void pegasus_client_impl::async_check_and_mutate(const std::string &hash_key,
                                                 const std::string &check_sort_key,
                                                 cas_check_type check_type,
                                                 const std::string &check_operand,
                                                 const mutations &mutations,
                                                 const check_and_mutate_options &options,
                                                 async_check_and_mutate_callback_t &&callback,
                                                 int timeout_milliseconds)
{
    // check params
    if (hash_key.size() >= UINT16_MAX) {
        derror("invalid hash key: hash key length should be less than UINT16_MAX, but %d",
               (int)hash_key.size());
        if (callback != nullptr)
            callback(PERR_INVALID_HASH_KEY, check_and_mutate_results(), internal_info());
        return;
    }

    if (dsn::apps::_cas_check_type_VALUES_TO_NAMES.find(check_type) ==
        dsn::apps::_cas_check_type_VALUES_TO_NAMES.end()) {
        derror("invalid check type: %d", (int)check_type);
        if (callback != nullptr)
            callback(PERR_INVALID_ARGUMENT, check_and_mutate_results(), internal_info());
        return;
    }
    if (mutations.is_empty()) {
        derror("invalid mutations: mutations should not be empty.");
        if (callback != nullptr)
            callback(PERR_INVALID_ARGUMENT, check_and_mutate_results(), internal_info());
        return;
    }

    ::dsn::apps::check_and_mutate_request req;
    req.hash_key.assign(hash_key.c_str(), 0, hash_key.size());
    req.check_sort_key.assign(check_sort_key.c_str(), 0, check_sort_key.size());
    req.check_type = (dsn::apps::cas_check_type::type)check_type;
    req.check_operand.assign(check_operand.c_str(), 0, check_operand.size());
1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042

    std::vector<mutate> mutate_list;
    mutations.get_mutations(mutate_list);
    req.mutate_list.resize(mutate_list.size());
    for (int i = 0; i < mutate_list.size(); ++i) {
        auto &mu = mutate_list[i];
        req.mutate_list[i].operation = (dsn::apps::mutate_operation::type)mu.operation;
        req.mutate_list[i].sort_key = blob::create_from_bytes(std::move(mu.sort_key));

        if (mu.operation == mutate::mutate_operation::MO_PUT) {
            req.mutate_list[i].value = blob::create_from_bytes(std::move(mu.value));
            req.mutate_list[i].set_expire_ts_seconds = mu.set_expire_ts_seconds;
        }
    }
W
Wu Tao 已提交
1043

1044 1045 1046 1047 1048 1049
    req.return_check_value = options.return_check_value;

    ::dsn::blob tmp_key;
    pegasus_generate_key(tmp_key, req.hash_key, ::dsn::blob());
    auto partition_hash = pegasus_key_hash(tmp_key);
    auto new_callback = [user_callback = std::move(callback)](
W
Wu Tao 已提交
1050
        ::dsn::error_code err, dsn::message_ex * req, dsn::message_ex * resp)
1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091
    {
        if (user_callback == nullptr) {
            return;
        }
        check_and_mutate_results results;
        internal_info info;
        ::dsn::apps::check_and_mutate_response response;
        if (err == ::dsn::ERR_OK) {
            ::dsn::unmarshall(resp, response);
            if (response.error == 0) {
                results.mutate_succeed = true;
            } else if (response.error == 13) { // kTryAgain
                results.mutate_succeed = false;
                response.error = 0;
            } else {
                results.mutate_succeed = false;
            }
            if (response.check_value_returned) {
                results.check_value_returned = true;
                if (response.check_value_exist) {
                    results.check_value_exist = true;
                    results.check_value.assign(response.check_value.data(),
                                               response.check_value.length());
                }
            }
            info.app_id = response.app_id;
            info.partition_index = response.partition_index;
            info.decree = response.decree;
            info.server = response.server;
        }
        int ret =
            get_client_error(err == ERR_OK ? get_rocksdb_server_error(response.error) : int(err));
        user_callback(ret, std::move(results), std::move(info));
    };
    _client->check_and_mutate(req,
                              std::move(new_callback),
                              std::chrono::milliseconds(timeout_milliseconds),
                              0,
                              partition_hash);
}

Q
qinzuoyan 已提交
1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125
int pegasus_client_impl::ttl(const std::string &hash_key,
                             const std::string &sort_key,
                             int &ttl_seconds,
                             int timeout_milliseconds,
                             internal_info *info)
{
    // check params
    if (hash_key.size() >= UINT16_MAX) {
        derror("invalid hash key: hash key length should be less than UINT16_MAX, but %d",
               (int)hash_key.size());
        return PERR_INVALID_HASH_KEY;
    }

    ::dsn::blob req;
    pegasus_generate_key(req, hash_key, sort_key);
    auto partition_hash = pegasus_key_hash(req);
    auto pr =
        _client->ttl_sync(req, std::chrono::milliseconds(timeout_milliseconds), 0, partition_hash);
    if (pr.first == ERR_OK && pr.second.error == 0) {
        ttl_seconds = pr.second.ttl_seconds;
    }
    if (info != nullptr) {
        if (pr.first == ERR_OK) {
            info->app_id = pr.second.app_id;
            info->partition_index = pr.second.partition_index;
            info->decree = -1;
            info->server = pr.second.server;
        } else {
            info->app_id = -1;
            info->partition_index = -1;
            info->decree = -1;
        }
    }
    return get_client_error(pr.first == ERR_OK ? get_rocksdb_server_error(pr.second.error)
1126
                                               : int(pr.first));
Q
qinzuoyan 已提交
1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161
}

void pegasus_client_impl::async_get_scanner(const std::string &hash_key,
                                            const std::string &start_sortkey,
                                            const std::string &stop_sortkey,
                                            const scan_options &options,
                                            async_get_scanner_callback_t &&callback)
{
    if (callback) {
        pegasus_scanner *scanner;
        int ret = get_scanner(hash_key, start_sortkey, stop_sortkey, options, scanner);
        callback(ret, scanner);
    }
}

int pegasus_client_impl::get_scanner(const std::string &hash_key,
                                     const std::string &start_sort_key,
                                     const std::string &stop_sort_key,
                                     const scan_options &options,
                                     pegasus_scanner *&scanner)
{
    // check params
    if (hash_key.size() >= UINT16_MAX) {
        derror("invalid hash key: hash key length should be less than UINT16_MAX, but %d",
               (int)hash_key.size());
        return PERR_INVALID_HASH_KEY;
    }
    if (hash_key.empty()) {
        derror("invalid hash key: hash key cannot be empty when scan");
        return PERR_INVALID_HASH_KEY;
    }

    ::dsn::blob start;
    ::dsn::blob stop;
    scan_options o(options);
1162 1163

    // generate key range by start_sort_key and stop_sort_key
Q
qinzuoyan 已提交
1164 1165 1166 1167 1168 1169 1170 1171
    pegasus_generate_key(start, hash_key, start_sort_key);
    if (stop_sort_key.empty()) {
        pegasus_generate_next_blob(stop, hash_key);
        o.stop_inclusive = false;
    } else {
        pegasus_generate_key(stop, hash_key, stop_sort_key);
    }

1172 1173 1174 1175 1176 1177 1178
    // limit key range by prefix filter
    if (o.sort_key_filter_type == filter_type::FT_MATCH_PREFIX &&
        o.sort_key_filter_pattern.length() > 0) {
        ::dsn::blob prefix_start, prefix_stop;
        pegasus_generate_key(prefix_start, hash_key, o.sort_key_filter_pattern);
        pegasus_generate_next_blob(prefix_stop, hash_key, o.sort_key_filter_pattern);

Q
QinZuoyan 已提交
1179
        if (::dsn::string_view(prefix_start).compare(start) > 0) {
1180 1181 1182 1183
            start = std::move(prefix_start);
            o.start_inclusive = true;
        }

Q
QinZuoyan 已提交
1184
        if (::dsn::string_view(prefix_stop).compare(stop) <= 0) {
1185 1186 1187 1188 1189 1190
            stop = std::move(prefix_stop);
            o.stop_inclusive = false;
        }
    }

    // check if range is empty
Q
qinzuoyan 已提交
1191
    std::vector<uint64_t> v;
Q
QinZuoyan 已提交
1192
    int c = ::dsn::string_view(start).compare(stop);
1193
    if (c < 0 || (c == 0 && o.start_inclusive && o.stop_inclusive)) {
Q
qinzuoyan 已提交
1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220
        v.push_back(pegasus_key_hash(start));
    }
    scanner = new pegasus_scanner_impl(_client, std::move(v), o, start, stop);

    return PERR_OK;
}

DEFINE_TASK_CODE_RPC(RPC_CM_QUERY_PARTITION_CONFIG_BY_INDEX,
                     TASK_PRIORITY_COMMON,
                     ::dsn::THREAD_POOL_DEFAULT)
void pegasus_client_impl::async_get_unordered_scanners(
    int max_split_count,
    const scan_options &options,
    async_get_unordered_scanners_callback_t &&callback)
{
    if (!callback) {
        return;
    }

    // check params
    if (max_split_count <= 0) {
        derror("invalid max_split_count: which should be greater than 0, but %d", max_split_count);
        callback(PERR_INVALID_SPLIT_COUNT, std::vector<pegasus_scanner *>());
        return;
    }

    auto new_callback = [ user_callback = std::move(callback), max_split_count, options, this ](
W
Wu Tao 已提交
1221
        ::dsn::error_code err, dsn::message_ex * req, dsn::message_ex * resp)
Q
qinzuoyan 已提交
1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239
    {
        std::vector<pegasus_scanner *> scanners;
        configuration_query_by_index_response response;
        if (err == ERR_OK) {
            ::dsn::unmarshall(resp, response);
            if (response.err == ERR_OK) {
                unsigned int count = response.partition_count;
                int split = count < max_split_count ? count : max_split_count;
                scanners.resize(split);

                int size = count / split;
                int more = count - size * split;

                for (int i = 0; i < split; i++) {
                    int s = size + (i < more);
                    std::vector<uint64_t> hash(s);
                    for (int j = 0; j < s; j++)
                        hash[j] = --count;
1240
                    scanners[i] = new pegasus_scanner_impl(_client, std::move(hash), options);
Q
qinzuoyan 已提交
1241 1242 1243
                }
            }
        }
1244
        int ret = get_client_error(err == ERR_OK ? int(response.err) : int(err));
Q
qinzuoyan 已提交
1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298
        user_callback(ret, std::move(scanners));
    };

    configuration_query_by_index_request req;
    req.app_name = _app_name;
    ::dsn::rpc::call(_meta_server,
                     RPC_CM_QUERY_PARTITION_CONFIG_BY_INDEX,
                     req,
                     nullptr,
                     new_callback,
                     std::chrono::milliseconds(options.timeout_ms),
                     0,
                     0);
}

int pegasus_client_impl::get_unordered_scanners(int max_split_count,
                                                const scan_options &options,
                                                std::vector<pegasus_scanner *> &scanners)
{
    ::dsn::utils::notify_event op_completed;
    int ret = -1;
    auto callback = [&](int err, std::vector<pegasus_scanner *> &&ss) {
        ret = err;
        scanners = std::move(ss);
        op_completed.notify();
    };
    async_get_unordered_scanners(max_split_count, options, std::move(callback));
    op_completed.wait();
    return ret;
}

const char *pegasus_client_impl::get_error_string(int error_code) const
{
    auto it = _client_error_to_string.find(error_code);
    dassert(
        it != _client_error_to_string.end(), "client error %d have no error string", error_code);
    return it->second.c_str();
}

/*static*/ void pegasus_client_impl::init_error()
{
    _client_error_to_string.clear();
#define PEGASUS_ERR_CODE(x, y, z) _client_error_to_string[y] = z
#include <pegasus/error_def.h>
#undef PEGASUS_ERR_CODE

    _server_error_to_client.clear();
    _server_error_to_client[::dsn::ERR_OK] = PERR_OK;
    _server_error_to_client[::dsn::ERR_TIMEOUT] = PERR_TIMEOUT;
    _server_error_to_client[::dsn::ERR_FILE_OPERATION_FAILED] = PERR_SERVER_INTERNAL_ERROR;
    _server_error_to_client[::dsn::ERR_INVALID_STATE] = PERR_SERVER_CHANGED;
    _server_error_to_client[::dsn::ERR_OBJECT_NOT_FOUND] = PERR_OBJECT_NOT_FOUND;
    _server_error_to_client[::dsn::ERR_NETWORK_FAILURE] = PERR_NETWORK_FAILURE;
    _server_error_to_client[::dsn::ERR_HANDLER_NOT_FOUND] = PERR_HANDLER_NOT_FOUND;
Q
QinZuoyan 已提交
1299
    _server_error_to_client[::dsn::ERR_OPERATION_DISABLED] = PERR_OPERATION_DISABLED;
1300
    _server_error_to_client[::dsn::ERR_NOT_ENOUGH_MEMBER] = PERR_NOT_ENOUGH_MEMBER;
Q
qinzuoyan 已提交
1301 1302 1303

    _server_error_to_client[::dsn::ERR_APP_NOT_EXIST] = PERR_APP_NOT_EXIST;
    _server_error_to_client[::dsn::ERR_APP_EXIST] = PERR_APP_EXIST;
1304
    _server_error_to_client[::dsn::ERR_BUSY] = PERR_APP_BUSY;
Q
qinzuoyan 已提交
1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326

    // rocksdb error;
    for (int i = 1001; i < 1013; i++) {
        _server_error_to_client[-i] = -i;
    }
}

/*static*/ int pegasus_client_impl::get_client_error(int server_error)
{
    auto it = _server_error_to_client.find(server_error);
    if (it != _server_error_to_client.end())
        return it->second;
    derror("can't find corresponding client error definition, server error:[%d:%s]",
           server_error,
           ::dsn::error_code(server_error).to_string());
    return PERR_UNKNOWN;
}

/*static*/ int pegasus_client_impl::get_rocksdb_server_error(int rocskdb_error)
{
    return (rocskdb_error == 0) ? 0 : ROCSKDB_ERROR_START - rocskdb_error;
}
1327 1328
} // namespace client
} // namespace pegasus