bool LuaTable::hget()

in common/luatable.cpp [78:126]


bool LuaTable::hget(const vector<string> &luaKeys, const string &field, string &value)
{
    if (m_lua.empty() || luaKeys.empty() || field.empty())
    {
        value.clear();
        return false;
    }

    // Assembly redis command args into a string vector
    vector<string> args;
    args.emplace_back("EVALSHA");
    args.emplace_back(loadRedisScript(m_db.get(), m_lua));
    args.emplace_back(to_string(luaKeys.size()));
    for (const auto& k: luaKeys)
    {
        args.emplace_back(k);
    }

    // ARGV[1..4]: counter db, gearbox counter db, table name, separator
    args.emplace_back(to_string(COUNTERS_DB));
    args.emplace_back(to_string(GB_COUNTERS_DB));
    args.emplace_back(getTableName());
    args.emplace_back(getTableNameSeparator());
    args.emplace_back("HGET");  // ARGV[5] get one field
    args.emplace_back(field);    // ARGV[6] field name
    for (const auto& v: m_luaArgv) // ARGV[7...] extra user-defined
    {
        args.emplace_back(v);
    }

    // Invoke redis command
    RedisCommand command;
    command.format(args);
    RedisReply r(m_db.get(), command);
    redisReply *reply = r.getContext();

    if (reply->type == REDIS_REPLY_NIL)
    {
        value.clear();
        return false;
    }

    if (reply->type != REDIS_REPLY_STRING)
        throw system_error(make_error_code(errc::io_error),
                "Got unexpected reply type");

    value = reply->str;
    return true;
}