Add JSON dump format

This commit is contained in:
Vitaliy Filippov
2024-03-16 13:24:36 +03:00
parent 018e89f867
commit c289584469
3 changed files with 42 additions and 17 deletions
+27 -9
View File
@@ -201,6 +201,15 @@ void kv_cli_t::next_cmd()
} }
} }
struct kv_cli_list_t
{
kv_dbw_t *db = NULL;
void *handle = NULL;
int format = 0;
int n = 0;
std::function<void()> cb;
};
void kv_cli_t::handle_cmd(const std::string & cmd, std::function<void()> cb) void kv_cli_t::handle_cmd(const std::string & cmd, std::function<void()> cb)
{ {
if (cmd == "") if (cmd == "")
@@ -327,9 +336,12 @@ void kv_cli_t::handle_cmd(const std::string & cmd, std::function<void()> cb)
}); });
} }
} }
else if (opname == "list" || opname == "dump") else if (opname == "list" || opname == "dump" || opname == "dumpjson")
{ {
bool dump = opname == "dump"; kv_cli_list_t *lst = new kv_cli_list_t;
lst->db = db;
lst->format = opname == "dump" ? 1 : (opname == "dumpjson" ? 2 : 0);
lst->cb = std::move(cb);
std::string start, end; std::string start, end;
if (pos != std::string::npos) if (pos != std::string::npos)
{ {
@@ -344,8 +356,8 @@ void kv_cli_t::handle_cmd(const std::string & cmd, std::function<void()> cb)
start = trim(cmd.substr(pos+1)); start = trim(cmd.substr(pos+1));
} }
} }
void *handle = db->list_start(start); lst->handle = db->list_start(start);
db->list_next(handle, [=](int res, const std::string & key, const std::string & value) db->list_next(lst->handle, [lst](int res, const std::string & key, const std::string & value)
{ {
if (res < 0) if (res < 0)
{ {
@@ -353,16 +365,22 @@ void kv_cli_t::handle_cmd(const std::string & cmd, std::function<void()> cb)
{ {
fprintf(stderr, "Error: %s (code %d)\n", strerror(-res), res); fprintf(stderr, "Error: %s (code %d)\n", strerror(-res), res);
} }
db->list_close(handle); if (lst->format == 2)
cb(); printf("\n}\n");
lst->db->list_close(lst->handle);
lst->cb();
delete lst;
} }
else else
{ {
if (dump) if (lst->format == 2)
printf(lst->n ? ",\n %s: %s" : "{\n %s: %s", addslashes(key).c_str(), addslashes(value).c_str());
else if (lst->format == 1)
printf("set %s %s\n", auto_addslashes(key).c_str(), value.c_str()); printf("set %s %s\n", auto_addslashes(key).c_str(), value.c_str());
else else
printf("%s = %s\n", key.c_str(), value.c_str()); printf("%s = %s\n", key.c_str(), value.c_str());
db->list_next(handle, NULL); lst->n++;
lst->db->list_next(lst->handle, NULL);
} }
}); });
} }
@@ -386,7 +404,7 @@ void kv_cli_t::handle_cmd(const std::string & cmd, std::function<void()> cb)
"open <pool_id> <inode_id> [block_size]\n" "open <pool_id> <inode_id> [block_size]\n"
"config <property> <value>\n" "config <property> <value>\n"
"get <key>\nset <key> <value>\ndel <key>\n" "get <key>\nset <key> <value>\ndel <key>\n"
"list [<start> [end]]\ndump [<start> [end]]\n" "list [<start> [end]]\ndump [<start> [end]]\ndumpjson [<start> [end]]\n"
"close\nquit\n", opname.c_str() "close\nquit\n", opname.c_str()
); );
cb(); cb();
+13 -7
View File
@@ -412,19 +412,25 @@ std::string scan_escaped(const std::string & cmd, size_t & pos)
return key; return key;
} }
std::string auto_addslashes(const std::string & str) std::string auto_addslashes(const std::string & str, const char *toescape)
{ {
auto pos = str.find_first_of("\\\""); auto pos = str.find_first_of(toescape);
if (pos == std::string::npos) if (pos == std::string::npos)
return str; return str;
std::string res = "\""+str.substr(0, pos)+"\\"+str[pos]; return addslashes(str, toescape);
while (pos < str.size()-1) }
std::string addslashes(const std::string & str, const char *toescape)
{
std::string res = "\"";
auto pos = 0;
while (pos < str.size())
{ {
auto pos2 = str.find_first_of("\\\"", pos+1); auto pos2 = str.find_first_of(toescape, pos);
if (pos2 == std::string::npos) if (pos2 == std::string::npos)
return res + str.substr(pos+1) + "\""; return res + str.substr(pos) + "\"";
res += str.substr(pos, pos2-pos)+"\\"+str[pos2]; res += str.substr(pos, pos2-pos)+"\\"+str[pos2];
pos = pos2; pos = pos2+1;
} }
return res+"\""; return res+"\"";
} }
+2 -1
View File
@@ -24,5 +24,6 @@ size_t utf8_length(const std::string & s);
size_t utf8_length(const char *s); size_t utf8_length(const char *s);
std::vector<std::string> explode(const std::string & sep, const std::string & value, bool trim); std::vector<std::string> explode(const std::string & sep, const std::string & value, bool trim);
std::string scan_escaped(const std::string & cmd, size_t & pos); std::string scan_escaped(const std::string & cmd, size_t & pos);
std::string auto_addslashes(const std::string & str); std::string auto_addslashes(const std::string & str, const char *toescape = "\\\"");
std::string addslashes(const std::string & str, const char *toescape = "\\\"");
std::string realpath_str(std::string path, bool nofail = true); std::string realpath_str(std::string path, bool nofail = true);