Test using yyjson in pkv.

This commit is contained in:
zhengshuxin 2023-08-05 22:05:38 +08:00
parent ccefbdd2b6
commit 9a4b22c1f9
17 changed files with 126 additions and 10 deletions

View File

@ -48,8 +48,9 @@ set(src_paths
${base_path} ${base_path}
${base_path}/action ${base_path}/action
${base_path}/dao ${base_path}/dao
${base_path}/dao/rocksdb ${base_path}/db
${base_path}/dao/wt ${base_path}/db/rocksdb
${base_path}/db/wt
${base_path}/proto ${base_path}/proto
) )
@ -93,7 +94,7 @@ endif()
set(acl_all ${home_path}/lib_fiber/lib/libfiber_cpp.a ${home_path}/libacl_all.a) set(acl_all ${home_path}/lib_fiber/lib/libfiber_cpp.a ${home_path}/libacl_all.a)
set(fiber ${home_path}/lib_fiber/lib/libfiber.a) set(fiber ${home_path}/lib_fiber/lib/libfiber.a)
set(lib_all ${acl_all}) set(lib_all ${acl_all} -lyyjson)
if(${HAS_ROCKSDB} MATCHES "YES") if(${HAS_ROCKSDB} MATCHES "YES")
find_library(rocksdb NAMES rocksdb PATHS /usr/lib /usr/local/lib) find_library(rocksdb NAMES rocksdb PATHS /usr/lib /usr/local/lib)

View File

@ -7,10 +7,10 @@ else
BUILD_ARGS = -DBUILD_WITH_C11=NO BUILD_ARGS = -DBUILD_WITH_C11=NO
endif endif
BUILD_ARGS += -DCMAKE_VERBOSE_MAKEFILE=ON #BUILD_ARGS += -DCMAKE_VERBOSE_MAKEFILE=ON
#BUILD_ARGS += -DHAS_ROCKSDB=YES #BUILD_ARGS += -DHAS_ROCKSDB=YES
#BUILD_ARGS += -DHAS_WT=YES #BUILD_ARGS += -DHAS_WT=YES
BUILD_ARGS += -DHAS_JEMALLOC=YES #BUILD_ARGS += -DHAS_JEMALLOC=YES
all: all:
@(mkdir -p build; cd build; cmake ${BUILD_ARGS} ..; make -j 4) @(mkdir -p build; cd build; cmake ${BUILD_ARGS} ..; make -j 4)

View File

@ -6,7 +6,7 @@
#include <vector> #include <vector>
#include "proto/redis_coder.h" #include "proto/redis_coder.h"
#include "dao/db.h" #include "db/db.h"
namespace pkv { namespace pkv {

View File

@ -0,0 +1,11 @@
#pragma once
namespace pkv {
class dao_base {
public:
dao_base() = default;
virtual ~dao_base() = default;
};
} // namespace pkv

View File

@ -0,0 +1,33 @@
#include "stdafx.h"
#include "string_dao.h"
namespace pkv {
string_dao::string_dao() : data_(nullptr) {}
string_dao::~string_dao() {}
void string_dao::set_string(const char* data) {
data_ = data;
}
bool string_dao::to_string(std::string& out) {
yyjson_mut_doc* doc = yyjson_mut_doc_new(NULL);
yyjson_mut_val* root = yyjson_mut_obj(doc);
yyjson_mut_doc_set_root(doc, root);
yyjson_mut_obj_add_str(doc, root, "type", "string");
yyjson_mut_obj_add_int(doc, root, "expire", -1);
yyjson_mut_obj_add_str(doc, root, "data", data_);
const char* data = yyjson_mut_write(doc, 0, NULL);
if (data == nullptr) {
yyjson_mut_doc_free(doc);
return false;
}
out.append(data);
yyjson_mut_doc_free(doc);
return true;
}
} // namespace pkv

View File

@ -0,0 +1,20 @@
#pragma once
#include <yyjson.h>
#include "dao_base.h"
namespace pkv {
class string_dao : public dao_base {
public:
string_dao();
~string_dao() override;
void set_string(const char* data);
bool to_string(std::string& out);
private:
const char* data_;
};
} // namespace pkv

View File

@ -1,6 +1,6 @@
#pragma once #pragma once
#include "dao/db.h" #include "db/db.h"
class master_service : public acl::master_fiber { class master_service : public acl::master_fiber {
public: public:

View File

@ -5,6 +5,7 @@
#include "stdafx.h" #include "stdafx.h"
#include "redis_ocache.h" #include "redis_ocache.h"
#include "redis_coder.h" #include "redis_coder.h"
#include "dao/string_dao.h"
namespace pkv { namespace pkv {
@ -223,6 +224,7 @@ bool test_redis_build() {
} }
size_t redis_build_bench(size_t max) { size_t redis_build_bench(size_t max) {
#if 0
redis_ocache cache; redis_ocache cache;
redis_coder builder(cache); redis_coder builder(cache);
size_t i = 0; size_t i = 0;
@ -233,13 +235,28 @@ size_t redis_build_bench(size_t max) {
.create_child().set_string("string", true) .create_child().set_string("string", true)
.create_child().set_number(-1); .create_child().set_number(-1);
builder.create_object().set_status("hello world!"); builder.create_object().set_status("hello world!");
// builder.create_object().set_status("hello world!"); builder.create_object().set_status("hello world!");
// builder.create_object().set_number(-1); builder.create_object().set_number(-1);
// builder.create_object().set_number(-1); builder.create_object().set_number(-1);
builder.to_string(buff); builder.to_string(buff);
builder.clear(); builder.clear();
} }
#else
size_t i = 0;
string_dao dao;
for (; i < max; i++) {
std::string buff;
dao.set_string("hello world");
if (!dao.to_string(buff)) {
printf("to_string error\r\n");
break;
}
if (i == 0) {
printf("%s\r\n", buff.c_str());
}
}
#endif
return i; return i;
} }

View File

@ -386,8 +386,16 @@ bool redis_object::to_string(std::string& out) const {
#define CRLF "\r\n" #define CRLF "\r\n"
#endif #endif
#define USE_APPEND
if (!EMPTY(objs_)) { if (!EMPTY(objs_)) {
#ifdef USE_APPEND
out.append("*").append(std::to_string(objs_.size())).append(CRLF); out.append("*").append(std::to_string(objs_.size())).append(CRLF);
#else
out += "*";
out += std::to_string(objs_.size());
out += CRLF;
#endif
for (const auto& obj : objs_) { for (const auto& obj : objs_) {
if (!obj->to_string(out)) { if (!obj->to_string(out)) {
@ -400,17 +408,43 @@ bool redis_object::to_string(std::string& out) const {
switch (type_) { switch (type_) {
case REDIS_OBJ_STATUS: case REDIS_OBJ_STATUS:
#ifdef USE_APPEND
out.append("+").append(buf_.c_str(), buf_.size()).append(CRLF); out.append("+").append(buf_.c_str(), buf_.size()).append(CRLF);
#else
out += "+";
out.append(buf_.c_str(), buf_.size());
out += CRLF;
#endif
break; break;
case REDIS_OBJ_ERROR: case REDIS_OBJ_ERROR:
#ifdef USE_APPEND
out.append("-").append(buf_.c_str(), buf_.size()).append(CRLF); out.append("-").append(buf_.c_str(), buf_.size()).append(CRLF);
#else
out += "-";
out.append(buf_.c_str(), buf_.size());
out += CRLF;
#endif
break; break;
case REDIS_OBJ_INTEGER: case REDIS_OBJ_INTEGER:
#ifdef USE_APPEND
out.append(":").append(buf_.c_str(), buf_.size()).append(CRLF); out.append(":").append(buf_.c_str(), buf_.size()).append(CRLF);
#else
out += ":";
out.append(buf_.c_str(), buf_.size());
out += CRLF;
#endif
break; break;
case REDIS_OBJ_STRING: case REDIS_OBJ_STRING:
#ifdef USE_APPEND
out.append("$").append(std::to_string(buf_.size())).append(CRLF) out.append("$").append(std::to_string(buf_.size())).append(CRLF)
.append(buf_.c_str(), buf_.size()).append(CRLF); .append(buf_.c_str(), buf_.size()).append(CRLF);
#else
out += "$";
out += std::to_string(buf_.size());
out += CRLF;
out.append(buf_.c_str(), buf_.size());
out += CRLF;
#endif
break; break;
//case acl::REDIS_RESULT_ARRAY: //case acl::REDIS_RESULT_ARRAY:
// break; // break;