Skip to content

Commit

Permalink
优化redis格式化
Browse files Browse the repository at this point in the history
  • Loading branch information
xiyoo0812 committed Jun 4, 2024
1 parent 7135d62 commit 4f79b87
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 13 deletions.
6 changes: 6 additions & 0 deletions extend/lcodec/lcodec.lmak
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,15 @@ TARGET_NAME = "lcodec"
----工程类型: static/dynamic/exe
PROJECT_TYPE = "dynamic"

--需要定义的选项
DEFINES = {
"FMT_HEADER_ONLY"
}

--需要的include目录
INCLUDES = {
"../lua/lua",
"../fmt/include",
"../luakit/include"
}

Expand Down
2 changes: 2 additions & 0 deletions extend/lcodec/lcodec.mak
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@ STDCPP = -std=c++17

#需要的include目录
MYCFLAGS += -I../lua/lua
MYCFLAGS += -I../fmt/include
MYCFLAGS += -I../luakit/include

#需要定义的选项
MYCFLAGS += -DFMT_HEADER_ONLY

#LDFLAGS
LDFLAGS =
Expand Down
4 changes: 2 additions & 2 deletions extend/lcodec/lcodec.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Develop|x64'">
<ClCompile>
<Optimization>Disabled</Optimization>
<AdditionalIncludeDirectories>..\lua\lua;..\luakit\include;$(SolutionDir)extend\mimalloc\mimalloc\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<PreprocessorDefinitions>WIN32;NDEBUG;_WINDOWS;_CRT_SECURE_NO_WARNINGS;LUA_BUILD_AS_DLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<AdditionalIncludeDirectories>..\lua\lua;..\fmt\include;..\luakit\include;$(SolutionDir)extend\mimalloc\mimalloc\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<PreprocessorDefinitions>WIN32;NDEBUG;_WINDOWS;_CRT_SECURE_NO_WARNINGS;FMT_HEADER_ONLY;LUA_BUILD_AS_DLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<BasicRuntimeChecks>Default</BasicRuntimeChecks>
<RuntimeLibrary>MultiThreadedDLL</RuntimeLibrary>
<PrecompiledHeader></PrecompiledHeader>
Expand Down
17 changes: 6 additions & 11 deletions extend/lcodec/src/redis.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
#endif

#include "lua_kit.h"
#include "fmt/core.h"

using namespace std;
using namespace luakit;

namespace lcodec {
inline size_t CRLF_LEN = 2;
inline size_t HEAD_SIZE = 24;
inline const char* RDS_CRLF = "\r\n";

class rdscodec : public codec_base {
Expand All @@ -27,8 +27,7 @@ namespace lcodec {
m_buf->clean();
int n = lua_gettop(L);
uint32_t session_id = lua_tointeger(L, index++);
char* head = (char*)m_buf->peek_space(HEAD_SIZE);
m_buf->pop_space(sprintf(head, "*%d\r\n", n - index + 1));
m_buf->write(fmt::format("*{}\r\n", n - index + 1));
for (int i = index; i <= n; ++i) {
encode_bulk_string(L, i);
}
Expand Down Expand Up @@ -160,28 +159,24 @@ namespace lcodec {

void number_encode(double value) {
auto svalue = std::to_string(value);
char* head = (char*)m_buf->peek_space(HEAD_SIZE + svalue.size());
m_buf->pop_space(sprintf(head, "$%zd\r\n%s\r\n", svalue.size(), svalue.c_str()));
m_buf->write(fmt::format("${}\r\n{}\r\n", svalue.size(), svalue.c_str()));
}

void integer_encode(int64_t integer) {
auto svalue = std::to_string(integer);
char* head = (char*)m_buf->peek_space(HEAD_SIZE + svalue.size());
m_buf->pop_space(sprintf(head, "$%zd\r\n%s\r\n", svalue.size(), svalue.c_str()));
m_buf->write(fmt::format("${}\r\n{}\r\n", svalue.size(), svalue.c_str()));
}

void string_encode(lua_State* L, int idx) {
size_t len;
const char* data = lua_tolstring(L, idx, &len);
char* head = (char*)m_buf->peek_space(HEAD_SIZE + len);
m_buf->pop_space(sprintf(head, "$%zd\r\n%s\r\n", len, data));
m_buf->write(fmt::format("${}\r\n{}\r\n", len, string_view(data, len)));
}

void table_encode(lua_State* L, int idx) {
size_t len;
char* body = (char*)m_jcodec->encode(L, idx, &len);
char* head = (char*)m_buf->peek_space(HEAD_SIZE + len);
m_buf->pop_space(sprintf(head, "$%zd\r\n[js]%s\r\n", len + 4, body));
m_buf->write(fmt::format("${}\r\n[js]{}\r\n", len + 4, string_view(body, len)));
}

void encode_bulk_string(lua_State* L, int idx) {
Expand Down

0 comments on commit 4f79b87

Please sign in to comment.