Git Product home page Git Product logo

Comments (13)

PragmaTwice avatar PragmaTwice commented on June 21, 2024 1

Yeah, and you don't need to parse anything for implementing such behavior.

so is this approach not desirable unstable...VasuDevrani:kvrocks:init_INFO

Yeah, it looks a bit ugly. We can have a more intuitive design for this feature.

from kvrocks.

PragmaTwice avatar PragmaTwice commented on June 21, 2024 1

Yeah, it looks a bit ugly. We can have a more intuitive design for this feature.

i feel confused about the more intuitive design is this close to that 292bdbe? otherwise its out of scope for me (at least for now, will have to wait for someone else's PR or if someone can guide me around the better approach).

kvrocks/src/server/server.cc

Lines 1211 to 1229 in 292bdbe

if (json_format) {
jsoncons::json persistence;
persistence["loading"] = is_loading_;
persistence["bgsave_in_progress"] = is_bgsave_in_progress_ ? 1 : 0;
persistence["last_bgsave_time"] = last_bgsave_timestamp_secs_ == -1 ? start_time_secs_ : last_bgsave_timestamp_secs_;
persistence["last_bgsave_status"] = last_bgsave_status_;
persistence["last_bgsave_time_sec"] = last_bgsave_duration_secs_;
json_output["persistence"] = persistence;
} else {
if (section_cnt++) string_stream << "\r\n";
string_stream << "# Persistence\r\n";
string_stream << "loading:" << is_loading_ << "\r\n";
string_stream << "bgsave_in_progress:" << (is_bgsave_in_progress_ ? 1 : 0) << "\r\n";
string_stream << "last_bgsave_time:"
<< (last_bgsave_timestamp_secs_ == -1 ? start_time_secs_ : last_bgsave_timestamp_secs_) <<
"\r\n";
string_stream << "last_bgsave_status:" << last_bgsave_status_ << "\r\n";
string_stream << "last_bgsave_time_sec:" << last_bgsave_duration_secs_ << "\r\n";

I hope you can notice that there is a lot of repetition in your code between if and else, we should avoid this copy-and-paste programming. For me, preventing such code from entering kvrocks codebase is my top priority.

In fact, we can see that the result returned by INFO is a format similar to

std::map<SectionNameTy, std::map<KeyTy, ValueTy>>
(where ValueTy = something like std::variant<int, std::string>)

So we can return this format and transform it into JSON or TEXT at the end.

from kvrocks.

PragmaTwice avatar PragmaTwice commented on June 21, 2024 1

In details, we can have such small functions, like:

auto GetPersistenceInfo() -> std::map<KeyTy, ValueTy>;
auto GetServerInfo() -> std::map<KeyTy, ValueTy>;
auto GetPWhateverInfo() -> std::map<KeyTy, ValueTy>;

And then at the function that can generate all info (not real C++ code):

std::map<..> func_map = {{"persistence", GetPersistenceInfo}, ...}

auto all_info = func_map.filter(selected_info_sections).map(x -> x.call())

if (JSON) generateJsonOutput(all_info)
else (TXT) generateTxtOutput(all_info)

from kvrocks.

VasuDevrani avatar VasuDevrani commented on June 21, 2024

do we want INFO command to return output in json fomat when INFO JSON (JSON specified in command) otherwise plain text?

from kvrocks.

PragmaTwice avatar PragmaTwice commented on June 21, 2024

I think for compatibility, it should be INFO [<section_name>] [FORMAT (TXT | JSON)].

from kvrocks.

VasuDevrani avatar VasuDevrani commented on June 21, 2024

im struggling with parsing the output to json, this line of code outputs string which looks like

"# Clients\r\nmaxclients:10000\r\nconnected_clients:1\r\nmonitor_clients:0\r\nblocked_clients:0\r\n"

using jsoncons::json::parse throws ser_error (unsupported format) and I should not directly append the string as is to the json.

should i create a custom parsing function to convert it something like?
{"clients":{"blocked_clients":"0\r","connected_clients":"1\r","maxclients":"10000\r","monitor_clients":"0\r"}}

from kvrocks.

PragmaTwice avatar PragmaTwice commented on June 21, 2024

TBH I cannot get your point. Why do you want to parse it as JSON? It's just not JSON.

from kvrocks.

VasuDevrani avatar VasuDevrani commented on June 21, 2024

TBH I cannot get your point. Why do you want to parse it as JSON? It's just not JSON.

Maybe i misunderstood, please confirm.

The issue wants the support of commands INFO [<section_name>] [FORMAT (TXT | JSON)]:

INFO: returns info in default plain text format.
INFO <section>: section info in plain text format.
INFO all FORMAT JSON: returns the entire info in JSON format.
INFO <section> FORMAT JSON: returns just section info in JSON.
INFO <section> FORMAT TXT: section info in plain text format.

from kvrocks.

PragmaTwice avatar PragmaTwice commented on June 21, 2024

Yeah, and you don't need to parse anything for implementing such behavior.

from kvrocks.

VasuDevrani avatar VasuDevrani commented on June 21, 2024

Yeah, and you don't need to parse anything for implementing such behavior.

so is this approach not desirable
unstable...VasuDevrani:kvrocks:init_INFO

from kvrocks.

VasuDevrani avatar VasuDevrani commented on June 21, 2024

Yeah, it looks a bit ugly. We can have a more intuitive design for this feature.

i feel confused about the more intuitive design
is this close to that 292bdbe?
otherwise its out of scope for me (at least for now, will have to wait for someone else's PR or if someone can guide me around the better approach).

from kvrocks.

VasuDevrani avatar VasuDevrani commented on June 21, 2024
auto GetPersistenceInfo() -> std::map<KeyTy, ValueTy>;
auto GetServerInfo() -> std::map<KeyTy, ValueTy>;
auto GetPWhateverInfo() -> std::map<KeyTy, ValueTy>;

we already does have some function like GetServerInfo that uses string stream and returns string, should i overwrite them to return std::map<KeyTy, ValueTy>

from kvrocks.

PragmaTwice avatar PragmaTwice commented on June 21, 2024
auto GetPersistenceInfo() -> std::map<KeyTy, ValueTy>;
auto GetServerInfo() -> std::map<KeyTy, ValueTy>;
auto GetPWhateverInfo() -> std::map<KeyTy, ValueTy>;

we already does have some function like GetServerInfo that uses string stream and returns string, should i overwrite them to return std::map<KeyTy, ValueTy>

Yeah you can update these methods.

from kvrocks.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.