From 07ee34d3e8de2ca58efc93e1452f587f2c92d1d9 Mon Sep 17 00:00:00 2001 From: Vitaly _Vi Shukela Date: Tue, 23 Jan 2018 23:37:56 +0300 Subject: [PATCH] Add get_bin command to retrieve binary data without trailing \n I'm not sure if this is a correct implementation. Related to #52. --- ldb.cc | 6 +++++- ldb.h | 1 + lib/commands.cc | 13 +++++++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/ldb.cc b/ldb.cc index 1e28242..e2d0d5b 100644 --- a/ldb.cc +++ b/ldb.cc @@ -52,7 +52,7 @@ R"(ldb Usage: ldb [--create] [--error] [--size] [--nocompress] - ldb (del|get) + ldb (del|get|getbin) ldb put [--nocompress] ldb keys [--limit=] [--lower=] [--upper=] ldb (-h | --help) @@ -106,6 +106,7 @@ int main(int argc, const char** argv) if (args["del"].asBool() || args["put"].asBool() || args["get"].asBool() || + args["getbin"].asBool() || args["keys"].asBool() || args["--size"].asBool()) { interactive = false; @@ -145,6 +146,9 @@ int main(int argc, const char** argv) if (args["get"] && args["get"].asBool()) { ldb::get_value(args[""].asString()); } + else if (args["getbin"] && args["getbin"].asBool()) { + ldb::get_value_bin(args[""].asString()); + } else if (args["put"] && args["put"].asBool()) { string key = args[""].asString(); diff --git a/ldb.h b/ldb.h index 7573a37..c662d80 100644 --- a/ldb.h +++ b/ldb.h @@ -66,6 +66,7 @@ namespace ldb { void auto_completion(const char *buf, linenoiseCompletions *lc); void put_value(string key, string value); void get_value(string key); + void get_value_bin(string key); void del_value(string key); void get_size(); void range(string prefix, bool surpress_output); diff --git a/lib/commands.cc b/lib/commands.cc index 2a06731..fe7bdb0 100644 --- a/lib/commands.cc +++ b/lib/commands.cc @@ -42,6 +42,19 @@ void ldb::get_value(string key) { } } +void ldb::get_value_bin(string key) { + if (key == "") return; + + string value; + leveldb::Status status = db->Get(leveldb::ReadOptions(), key, &value); + + if (!status.ok()) { + cerr << "Not Found: [" << COLOR_BLUE << key << COLOR_NONE << "]" << endl; + } else { + cout << value; + } +} + // // //