bool check_and_set()

in src/shell/commands/data_operations.cpp [724:877]


bool check_and_set(command_executor *e, shell_context *sc, arguments args)
{
    if (args.argc < 2)
        return false;

    std::string hash_key = sds_to_string(args.argv[1]);
    bool check_sort_key_provided = false;
    std::string check_sort_key;
    ::dsn::apps::cas_check_type::type check_type = ::dsn::apps::cas_check_type::CT_NO_CHECK;
    std::string check_type_name;
    bool check_operand_provided = false;
    std::string check_operand;
    bool set_sort_key_provided = false;
    std::string set_sort_key;
    bool set_value_provided = false;
    std::string set_value;
    pegasus::pegasus_client::check_and_set_options options;

    static struct option long_options[] = {{"check_sort_key", required_argument, 0, 'c'},
                                           {"check_type", required_argument, 0, 't'},
                                           {"check_operand", required_argument, 0, 'o'},
                                           {"set_sort_key", required_argument, 0, 's'},
                                           {"set_value", required_argument, 0, 'v'},
                                           {"set_value_ttl_seconds", required_argument, 0, 'l'},
                                           {"return_check_value", no_argument, 0, 'r'},
                                           {0, 0, 0, 0}};

    escape_sds_argv(args.argc, args.argv);
    optind = 0;
    while (true) {
        int option_index = 0;
        int c;
        c = getopt_long(args.argc, args.argv, "c:t:o:s:v:l:r", long_options, &option_index);
        if (c == -1)
            break;
        switch (c) {
        case 'c':
            check_sort_key_provided = true;
            check_sort_key = unescape_str(optarg);
            break;
        case 't':
            check_type = type_from_string(::dsn::apps::_cas_check_type_VALUES_TO_NAMES,
                                          std::string("ct_value_") + optarg,
                                          ::dsn::apps::cas_check_type::CT_NO_CHECK);
            if (check_type == ::dsn::apps::cas_check_type::CT_NO_CHECK) {
                fprintf(stderr, "ERROR: invalid check_type param\n");
                return false;
            }
            check_type_name = optarg;
            break;
        case 'o':
            check_operand_provided = true;
            check_operand = unescape_str(optarg);
            break;
        case 's':
            set_sort_key_provided = true;
            set_sort_key = unescape_str(optarg);
            break;
        case 'v':
            set_value_provided = true;
            set_value = unescape_str(optarg);
            break;
        case 'l':
            if (!dsn::buf2int32(optarg, options.set_value_ttl_seconds)) {
                fprintf(stderr, "ERROR: invalid set_value_ttl_seconds param\n");
                return false;
            }
            break;
        case 'r':
            options.return_check_value = true;
            break;
        default:
            return false;
        }
    }

    if (!check_sort_key_provided) {
        fprintf(stderr, "ERROR: check_sort_key not provided\n");
        return false;
    }
    if (check_type == ::dsn::apps::cas_check_type::CT_NO_CHECK) {
        fprintf(stderr, "ERROR: check_type not provided\n");
        return false;
    }
    if (!check_operand_provided && pegasus::cas_is_check_operand_needed(check_type)) {
        fprintf(stderr, "ERROR: check_operand not provided\n");
        return false;
    }
    if (!set_sort_key_provided) {
        fprintf(stderr, "ERROR: set_sort_key not provided\n");
        return false;
    }
    if (!set_value_provided) {
        fprintf(stderr, "ERROR: set_value not provided\n");
        return false;
    }

    fprintf(stderr, "hash_key: \"%s\"\n", pegasus::utils::c_escape_string(hash_key).c_str());
    fprintf(stderr,
            "check_sort_key: \"%s\"\n",
            pegasus::utils::c_escape_string(check_sort_key).c_str());
    fprintf(stderr, "check_type: %s\n", check_type_name.c_str());
    if (check_type >= ::dsn::apps::cas_check_type::CT_VALUE_MATCH_ANYWHERE) {
        fprintf(stderr,
                "check_operand: \"%s\"\n",
                pegasus::utils::c_escape_string(check_operand).c_str());
    }
    fprintf(
        stderr, "set_sort_key: \"%s\"\n", pegasus::utils::c_escape_string(set_sort_key).c_str());
    fprintf(stderr, "set_value: \"%s\"\n", pegasus::utils::c_escape_string(set_value).c_str());
    fprintf(stderr, "set_value_ttl_seconds: %d\n", options.set_value_ttl_seconds);
    fprintf(stderr, "return_check_value: %s\n", options.return_check_value ? "true" : "false");
    fprintf(stderr, "\n");

    pegasus::pegasus_client::check_and_set_results results;
    pegasus::pegasus_client::internal_info info;
    int ret = sc->pg_client->check_and_set(hash_key,
                                           check_sort_key,
                                           (pegasus::pegasus_client::cas_check_type)check_type,
                                           check_operand,
                                           set_sort_key,
                                           set_value,
                                           options,
                                           results,
                                           sc->timeout_ms,
                                           &info);
    if (ret != pegasus::PERR_OK) {
        fprintf(stderr, "ERROR: %s\n", sc->pg_client->get_error_string(ret));
    } else {
        if (results.set_succeed) {
            fprintf(stderr, "Set succeed.\n");
        } else {
            fprintf(stderr, "Set failed, because check not passed.\n");
        }
        if (results.check_value_returned) {
            fprintf(stderr, "\n");
            if (results.check_value_exist) {
                fprintf(
                    stderr,
                    "Check value: \"%s\"\n",
                    pegasus::utils::c_escape_string(results.check_value, sc->escape_all).c_str());
            } else {
                fprintf(stderr, "Check value not exist.\n");
            }
        }
    }

    fprintf(stderr, "\n");
    fprintf(stderr, "app_id          : %d\n", info.app_id);
    fprintf(stderr, "partition_index : %d\n", info.partition_index);
    fmt::fprintf(stderr, "decree          : %lld\n", info.decree);
    fprintf(stderr, "server          : %s\n", info.server.c_str());
    return true;
}