Commit a8f27723 authored by liuminghang's avatar liuminghang

find commandhandler by string_piece

parent 48e0402c
...@@ -90,7 +90,7 @@ int ConsumeCommand(RedisConnContext* ctx, ...@@ -90,7 +90,7 @@ int ConsumeCommand(RedisConnContext* ctx,
return -1; return -1;
} }
} else { } else {
RedisCommandHandler* ch = ctx->redis_service->FindCommandHandler(commands[0].as_string()); RedisCommandHandler* ch = ctx->redis_service->FindCommandHandler(commands[0]);
if (!ch) { if (!ch) {
char buf[64]; char buf[64];
snprintf(buf, sizeof(buf), "ERR unknown command `%s`", commands[0].as_string().c_str()); snprintf(buf, sizeof(buf), "ERR unknown command `%s`", commands[0].as_string().c_str());
......
...@@ -437,19 +437,21 @@ std::ostream& operator<<(std::ostream& os, const RedisResponse& response) { ...@@ -437,19 +437,21 @@ std::ostream& operator<<(std::ostream& os, const RedisResponse& response) {
} }
bool RedisService::AddCommandHandler(const std::string& name, RedisCommandHandler* handler) { bool RedisService::AddCommandHandler(const std::string& name, RedisCommandHandler* handler) {
std::string lcname = StringToLowerASCII(name); butil::StringPiece name_piece(name);
auto it = _command_map.find(lcname); auto it = _command_map.find(name_piece);
if (it != _command_map.end()) { if (it != _command_map.end()) {
LOG(ERROR) << "redis command name=" << name << " exist"; LOG(ERROR) << "redis command name=" << name << " exist";
return false; return false;
} }
_command_map[lcname] = handler;
_all_commands.push_back(name);
name_piece = _all_commands.back();
_command_map[name_piece] = handler;
return true; return true;
} }
RedisCommandHandler* RedisService::FindCommandHandler(const std::string& name) const { RedisCommandHandler* RedisService::FindCommandHandler(const butil::StringPiece& name) const {
std::string lcname = StringToLowerASCII(name); auto it = _command_map.find(name);
auto it = _command_map.find(lcname);
if (it != _command_map.end()) { if (it != _command_map.end()) {
return it->second; return it->second;
} }
......
...@@ -22,6 +22,7 @@ ...@@ -22,6 +22,7 @@
#include <google/protobuf/message.h> #include <google/protobuf/message.h>
#include <unordered_map> #include <unordered_map>
#include <memory> #include <memory>
#include <list>
#include "butil/iobuf.h" #include "butil/iobuf.h"
#include "butil/strings/string_piece.h" #include "butil/strings/string_piece.h"
#include "butil/arena.h" #include "butil/arena.h"
...@@ -224,11 +225,13 @@ public: ...@@ -224,11 +225,13 @@ public:
bool AddCommandHandler(const std::string& name, RedisCommandHandler* handler); bool AddCommandHandler(const std::string& name, RedisCommandHandler* handler);
// This function should not be touched by user and used by brpc deverloper only. // This function should not be touched by user and used by brpc deverloper only.
RedisCommandHandler* FindCommandHandler(const std::string& name) const; RedisCommandHandler* FindCommandHandler(const butil::StringPiece& name) const;
private: private:
typedef std::unordered_map<std::string, RedisCommandHandler*> CommandMap; typedef BUTIL_HASH_NAMESPACE::hash<butil::StringPiece> StringPieceHasher;
typedef std::unordered_map<butil::StringPiece, RedisCommandHandler*, StringPieceHasher> CommandMap;
CommandMap _command_map; CommandMap _command_map;
std::list<std::string> _all_commands;
}; };
enum RedisCommandHandlerResult { enum RedisCommandHandlerResult {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment