Skip to content

Commit

Permalink
*: use buf2int/buf2bool in string_conv.h instead of which in pegasus_…
Browse files Browse the repository at this point in the history
…utils.h (apache#114)

Former-commit-id: fe0a6e4fc5daaf04e351845859419ad5860bd9ee [formerly f56bbda]
Former-commit-id: d46c3eb43b3dfabe5ab2b7293112cf28c4098e24
  • Loading branch information
qinzuoyan authored Jul 10, 2018
1 parent b580c1a commit 7894bce
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 86 deletions.
43 changes: 0 additions & 43 deletions src/base/pegasus_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,49 +28,6 @@ void addr2host(const ::dsn::rpc_address &addr, char *str, int len /* = 100*/)
}
}

bool buf2int(const char *buffer, int length, int &result)
{
if (length <= 0)
return false;
std::string s(buffer, length);
char *p = nullptr;
long long v = ::strtoll(s.data(), &p, 10);
if (*p != 0)
return false;
if (v < INT_MIN || v > INT_MAX) // out of range
return false;
result = (int)v;
return true;
}

bool buf2int64(const char *buffer, int length, int64_t &result)
{
if (length <= 0)
return false;
std::string s(buffer, length);
char *p = nullptr;
long long v = ::strtoll(s.data(), &p, 10);
if (*p != 0)
return false;
if ((v == LLONG_MIN || v == LLONG_MAX) && errno == ERANGE) // out of range
return false;
result = v;
return true;
}

bool buf2bool(const char *buffer, int length, bool &result)
{
if (length == 4 && strncmp(buffer, "true", 4) == 0) {
result = true;
return true;
}
if (length == 5 && strncmp(buffer, "false", 5) == 0) {
result = false;
return true;
}
return false;
}

size_t
c_escape_string(const char *src, size_t src_len, char *dest, size_t dest_len, bool always_escape)
{
Expand Down
7 changes: 0 additions & 7 deletions src/base/pegasus_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,6 @@ inline uint32_t epoch_now() { return time(nullptr) - epoch_begin; }
// extract "host" from rpc_address
void addr2host(const ::dsn::rpc_address &addr, char *str, int len);

// parse int from string
bool buf2int(const char *buffer, int length, int &result);
bool buf2int64(const char *buffer, int length, int64_t &result);

// parse bool from string, must be "true" or "false".
bool buf2bool(const char *buffer, int length, bool &result);

// three-way comparison, returns value:
// < 0 iff "a" < "b",
// == 0 iff "a" == "b",
Expand Down
2 changes: 1 addition & 1 deletion src/redis_protocol/proxy_lib/redis_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ void redis_parser::setex(message_entry &entry)
redis_simple_string result;
::dsn::blob &ttl_blob = redis_req.buffers[2].data;
int ttl_seconds;
if (!pegasus::utils::buf2int(ttl_blob.data(), ttl_blob.length(), ttl_seconds)) {
if (!dsn::buf2int32(ttl_blob, ttl_seconds)) {
result.is_error = true;
result.message = "ERR value is not an integer or out of range";
reply_message(entry, result);
Expand Down
4 changes: 3 additions & 1 deletion src/redis_protocol/proxy_ut/redis_proxy_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include <boost/lexical_cast.hpp>
#include <boost/asio.hpp>

#include <dsn/utility/string_conv.h>

#include <gtest/gtest.h>
#include <rrdb/rrdb.client.h>
#include <pegasus_utils.h>
Expand Down Expand Up @@ -312,7 +314,7 @@ TEST(proxy, utils)

for (int i = 0; int_buffers[i]; ++i) {
int result;
bool succeed = pegasus::utils::buf2int(int_buffers[i], strlen(int_buffers[i]), result);
bool succeed = dsn::buf2int32(int_buffers[i], result);

ASSERT_EQ(call_results[i].succeed, succeed);
if (result == true) {
Expand Down
4 changes: 3 additions & 1 deletion src/server/pegasus_write_service_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

#include "base/pegasus_key_schema.h"

#include <dsn/utility/string_conv.h>

namespace pegasus {
namespace server {

Expand Down Expand Up @@ -137,7 +139,7 @@ class pegasus_write_service::impl : public dsn::replication::replica_base
new_value = update.increment;
} else {
int64_t old_value_int;
if (!utils::buf2int64(old_value.data(), old_value.length(), old_value_int)) {
if (!dsn::buf2int64(old_value, old_value_int)) {
// invalid old value
derror_replica("incr failed: decree = {}, error = {}",
decree,
Expand Down
67 changes: 34 additions & 33 deletions src/shell/commands.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <rocksdb/db.h>
#include <rocksdb/sst_dump_tool.h>
#include <dsn/utility/filesystem.h>
#include <dsn/utility/string_conv.h>
#include <dsn/tool/cli/cli.client.h>
#include <dsn/dist/replication/replication_ddl_client.h>
#include <dsn/dist/replication/mutation_log_tool.h>
Expand Down Expand Up @@ -257,13 +258,13 @@ inline bool create_app(command_executor *e, shell_context *sc, arguments args)
break;
switch (c) {
case 'p':
if (!::pegasus::utils::buf2int(optarg, strlen(optarg), pc)) {
if (!dsn::buf2int32(optarg, pc)) {
fprintf(stderr, "parse %s as partition_count failed\n", optarg);
return false;
}
break;
case 'r':
if (!::pegasus::utils::buf2int(optarg, strlen(optarg), rc)) {
if (!dsn::buf2int32(optarg, rc)) {
fprintf(stderr, "parse %s as replica_count failed\n", optarg);
return false;
}
Expand Down Expand Up @@ -309,7 +310,7 @@ inline bool drop_app(command_executor *e, shell_context *sc, arguments args)
break;
switch (c) {
case 'r':
if (!::pegasus::utils::buf2int(optarg, strlen(optarg), reserve_seconds)) {
if (!dsn::buf2int32(optarg, reserve_seconds)) {
fprintf(stderr, "parse %s as reserve_seconds failed\n", optarg);
return false;
}
Expand All @@ -335,7 +336,7 @@ inline bool recall_app(command_executor *e, shell_context *sc, arguments args)

int id;
std::string new_name = "";
if (!::pegasus::utils::buf2int(args.argv[1], strlen(args.argv[1]), id)) {
if (!dsn::buf2int32(args.argv[1], id)) {
fprintf(stderr, "ERROR: parse %s as id failed\n", args.argv[1]);
return false;
}
Expand Down Expand Up @@ -590,7 +591,7 @@ inline bool process_timeout(command_executor *e, shell_context *sc, arguments ar
return true;
} else if (args.argc == 2) {
int timeout;
if (!::pegasus::utils::buf2int(args.argv[1], strlen(args.argv[1]), timeout)) {
if (!dsn::buf2int32(args.argv[1], timeout)) {
fprintf(stderr, "ERROR: parse %s as timeout failed\n", args.argv[1]);
return false;
}
Expand Down Expand Up @@ -776,13 +777,13 @@ inline bool multi_get_range(command_executor *e, shell_context *sc, arguments ar
break;
switch (c) {
case 'a':
if (!::pegasus::utils::buf2bool(optarg, strlen(optarg), options.start_inclusive)) {
if (!dsn::buf2bool(optarg, options.start_inclusive)) {
fprintf(stderr, "invalid start_inclusive param\n");
return false;
}
break;
case 'b':
if (!::pegasus::utils::buf2bool(optarg, strlen(optarg), options.stop_inclusive)) {
if (!dsn::buf2bool(optarg, options.stop_inclusive)) {
fprintf(stderr, "invalid stop_inclusive param\n");
return false;
}
Expand All @@ -798,7 +799,7 @@ inline bool multi_get_range(command_executor *e, shell_context *sc, arguments ar
options.sort_key_filter_pattern = unescape_str(optarg);
break;
case 'n':
if (!::pegasus::utils::buf2int(optarg, strlen(optarg), max_count)) {
if (!dsn::buf2int32(optarg, max_count)) {
fprintf(stderr, "parse %s as max_count failed\n", optarg);
return false;
}
Expand Down Expand Up @@ -966,7 +967,7 @@ inline bool set_value(command_executor *e, shell_context *sc, arguments args)
std::string value = sds_to_string(args.argv[3]);
int32_t ttl = 0;
if (args.argc == 5) {
if (!::pegasus::utils::buf2int(args.argv[4], strlen(args.argv[4]), ttl)) {
if (!dsn::buf2int32(args.argv[4], ttl)) {
fprintf(stderr, "ERROR: parse %s as ttl failed\n", args.argv[4]);
return false;
}
Expand Down Expand Up @@ -1114,13 +1115,13 @@ inline bool multi_del_range(command_executor *e, shell_context *sc, arguments ar
break;
switch (c) {
case 'a':
if (!::pegasus::utils::buf2bool(optarg, strlen(optarg), options.start_inclusive)) {
if (!dsn::buf2bool(optarg, options.start_inclusive)) {
fprintf(stderr, "invalid start_inclusive param\n");
return false;
}
break;
case 'b':
if (!::pegasus::utils::buf2bool(optarg, strlen(optarg), options.stop_inclusive)) {
if (!dsn::buf2bool(optarg, options.stop_inclusive)) {
fprintf(stderr, "invalid stop_inclusive param\n");
return false;
}
Expand Down Expand Up @@ -1283,7 +1284,7 @@ inline bool incr(command_executor *e, shell_context *sc, arguments args)
std::string sort_key = sds_to_string(args.argv[2]);
int64_t increment = 1;
if (args.argc == 4) {
if (!pegasus::utils::buf2int64(args.argv[3], strlen(args.argv[3]), increment)) {
if (!dsn::buf2int64(args.argv[3], increment)) {
fprintf(stderr, "ERROR: invalid increment param\n");
return false;
}
Expand Down Expand Up @@ -1382,19 +1383,19 @@ inline bool hash_scan(command_executor *e, shell_context *sc, arguments args)
detailed = true;
break;
case 'z':
if (!::pegasus::utils::buf2int(optarg, strlen(optarg), batch_size)) {
if (!dsn::buf2int32(optarg, batch_size)) {
fprintf(stderr, "parse %s as batch_size failed\n", optarg);
return false;
}
break;
case 'n':
if (!::pegasus::utils::buf2int(optarg, strlen(optarg), max_count)) {
if (!dsn::buf2int32(optarg, max_count)) {
fprintf(stderr, "parse %s as max_count failed\n", optarg);
return false;
}
break;
case 't':
if (!::pegasus::utils::buf2int(optarg, strlen(optarg), timeout_ms)) {
if (!dsn::buf2int32(optarg, timeout_ms)) {
fprintf(stderr, "parse %s as timeout_ms failed\n", optarg);
return false;
}
Expand All @@ -1407,13 +1408,13 @@ inline bool hash_scan(command_executor *e, shell_context *sc, arguments args)
}
break;
case 'a':
if (!::pegasus::utils::buf2bool(optarg, strlen(optarg), options.start_inclusive)) {
if (!dsn::buf2bool(optarg, options.start_inclusive)) {
fprintf(stderr, "invalid start_inclusive param\n");
return false;
}
break;
case 'b':
if (!::pegasus::utils::buf2bool(optarg, strlen(optarg), options.stop_inclusive)) {
if (!dsn::buf2bool(optarg, options.stop_inclusive)) {
fprintf(stderr, "invalid stop_inclusive param\n");
return false;
}
Expand Down Expand Up @@ -1565,19 +1566,19 @@ inline bool full_scan(command_executor *e, shell_context *sc, arguments args)
detailed = true;
break;
case 'z':
if (!::pegasus::utils::buf2int(optarg, strlen(optarg), batch_size)) {
if (!dsn::buf2int32(optarg, batch_size)) {
fprintf(stderr, "parse %s as batch_size failed\n", optarg);
return false;
}
break;
case 'n':
if (!::pegasus::utils::buf2int(optarg, strlen(optarg), max_count)) {
if (!dsn::buf2int32(optarg, max_count)) {
fprintf(stderr, "parse %s as max_count failed\n", optarg);
return false;
}
break;
case 'p':
if (!::pegasus::utils::buf2int(optarg, strlen(optarg), partition)) {
if (!dsn::buf2int32(optarg, partition)) {
fprintf(stderr, "parse %s as partition id failed\n", optarg);
return false;
}
Expand All @@ -1587,7 +1588,7 @@ inline bool full_scan(command_executor *e, shell_context *sc, arguments args)
}
break;
case 't':
if (!::pegasus::utils::buf2int(optarg, strlen(optarg), timeout_ms)) {
if (!dsn::buf2int32(optarg, timeout_ms)) {
fprintf(stderr, "parse %s as timeout_ms failed\n", optarg);
return false;
}
Expand Down Expand Up @@ -1764,19 +1765,19 @@ inline bool copy_data(command_executor *e, shell_context *sc, arguments args)
target_app_name = optarg;
break;
case 's':
if (!::pegasus::utils::buf2int(optarg, strlen(optarg), max_split_count)) {
if (!dsn::buf2int32(optarg, max_split_count)) {
fprintf(stderr, "parse %s as max_split_count failed\n", optarg);
return false;
}
break;
case 'b':
if (!::pegasus::utils::buf2int(optarg, strlen(optarg), max_batch_count)) {
if (!dsn::buf2int32(optarg, max_batch_count)) {
fprintf(stderr, "parse %s as max_batch_count failed\n", optarg);
return false;
}
break;
case 't':
if (!::pegasus::utils::buf2int(optarg, strlen(optarg), timeout_ms)) {
if (!dsn::buf2int32(optarg, timeout_ms)) {
fprintf(stderr, "parse %s as timeout_ms failed\n", optarg);
return false;
}
Expand Down Expand Up @@ -1951,19 +1952,19 @@ inline bool clear_data(command_executor *e, shell_context *sc, arguments args)
force = true;
break;
case 's':
if (!::pegasus::utils::buf2int(optarg, strlen(optarg), max_split_count)) {
if (!dsn::buf2int32(optarg, max_split_count)) {
fprintf(stderr, "parse %s as max_split_count failed\n", optarg);
return false;
}
break;
case 'b':
if (!::pegasus::utils::buf2int(optarg, strlen(optarg), max_batch_count)) {
if (!dsn::buf2int32(optarg, max_batch_count)) {
fprintf(stderr, "parse %s as max_batch_count failed\n", optarg);
return false;
}
break;
case 't':
if (!::pegasus::utils::buf2int(optarg, strlen(optarg), timeout_ms)) {
if (!dsn::buf2int32(optarg, timeout_ms)) {
fprintf(stderr, "parse %s as timeout_ms failed\n", optarg);
return false;
}
Expand Down Expand Up @@ -2113,19 +2114,19 @@ inline bool count_data(command_executor *e, shell_context *sc, arguments args)
break;
switch (c) {
case 's':
if (!::pegasus::utils::buf2int(optarg, strlen(optarg), max_split_count)) {
if (!dsn::buf2int32(optarg, max_split_count)) {
fprintf(stderr, "parse %s as max_split_count failed\n", optarg);
return false;
}
break;
case 'b':
if (!::pegasus::utils::buf2int(optarg, strlen(optarg), max_batch_count)) {
if (!dsn::buf2int32(optarg, max_batch_count)) {
fprintf(stderr, "parse %s as max_batch_count failed\n", optarg);
return false;
}
break;
case 't':
if (!::pegasus::utils::buf2int(optarg, strlen(optarg), timeout_ms)) {
if (!dsn::buf2int32(optarg, timeout_ms)) {
fprintf(stderr, "parse %s as timeout_ms failed\n", optarg);
return false;
}
Expand All @@ -2134,13 +2135,13 @@ inline bool count_data(command_executor *e, shell_context *sc, arguments args)
stat_size = true;
break;
case 'c':
if (!::pegasus::utils::buf2int(optarg, strlen(optarg), top_count)) {
if (!dsn::buf2int32(optarg, top_count)) {
fprintf(stderr, "parse %s as top_count failed\n", optarg);
return false;
}
break;
case 'r':
if (!::pegasus::utils::buf2int(optarg, strlen(optarg), run_seconds)) {
if (!dsn::buf2int32(optarg, run_seconds)) {
fprintf(stderr, "parse %s as run_seconds failed\n", optarg);
return false;
}
Expand Down Expand Up @@ -2660,7 +2661,7 @@ inline bool recover(command_executor *e, shell_context *sc, arguments args)
node_list_str = optarg;
break;
case 'w':
if (!::pegasus::utils::buf2int(optarg, strlen(optarg), wait_seconds)) {
if (!dsn::buf2int32(optarg, wait_seconds)) {
fprintf(stderr, "parse %s as wait_seconds failed\n", optarg);
return false;
}
Expand Down

0 comments on commit 7894bce

Please sign in to comment.