Skip to content

Commit

Permalink
adding beezcli and ttl support #407
Browse files Browse the repository at this point in the history
beezcli is an interactive tool wrapping ldb

This commit adds some enhancement to ldb

Resolves: #407
  • Loading branch information
Or Friedmann committed Apr 4, 2023
1 parent e1955f3 commit 384f7f4
Show file tree
Hide file tree
Showing 10 changed files with 342 additions and 52 deletions.
10 changes: 5 additions & 5 deletions include/rocksdb/ldb_tool.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ struct LDBOptions {
// Default: Slice::ToString()
std::shared_ptr<SliceFormatter> key_formatter;

std::string print_help_header = "ldb - RocksDB Tool";
std::string print_help_header = "beezcli - Speedb Tool";
};

class LDBTool {
public:
void Run(
int argc, char** argv, Options db_options = Options(),
const LDBOptions& ldb_options = LDBOptions(),
const std::vector<ColumnFamilyDescriptor>* column_families = nullptr);
void Run(int argc, char** argv, Options db_options = Options(),
const LDBOptions& ldb_options = LDBOptions(),
const std::vector<ColumnFamilyDescriptor>* column_families = nullptr,
bool exit_with_retcode = true);
};

} // namespace ROCKSDB_NAMESPACE
Expand Down
7 changes: 7 additions & 0 deletions include/rocksdb/utilities/ldb_cmd.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ class LDBCommand {
static const std::string ARG_PREPOPULATE_BLOB_CACHE;
static const std::string ARG_DECODE_BLOB_INDEX;
static const std::string ARG_DUMP_UNCOMPRESSED_BLOBS;
static const std::string ARG_INTERACTIVE;

struct ParsedParams {
std::string cmd;
Expand Down Expand Up @@ -191,6 +192,9 @@ class LDBCommand {

bool create_if_missing_;

// If true will not print values for dump, idump, scan
bool is_no_value_;

/**
* Map of options passed on the command-line.
*/
Expand All @@ -207,6 +211,9 @@ class LDBCommand {
/** Shared pointer to underlying environment if applicable **/
std::shared_ptr<Env> env_guard_;

/** ttl value for dbwithttl::open **/
int32_t ttl_;

bool ParseKeyValue(const std::string& line, std::string* key,
std::string* value, bool is_key_hex, bool is_value_hex);

Expand Down
2 changes: 2 additions & 0 deletions monitoring/histogram.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ class Histogram {
virtual const char* Name() const = 0;
virtual uint64_t min() const = 0;
virtual uint64_t max() const = 0;
virtual uint64_t sum() const = 0;
virtual uint64_t num() const = 0;
virtual double Median() const = 0;
virtual double Percentile(double p) const = 0;
Expand All @@ -132,6 +133,7 @@ class HistogramImpl : public Histogram {
virtual uint64_t min() const override { return stats_.min(); }
virtual uint64_t max() const override { return stats_.max(); }
virtual uint64_t num() const override { return stats_.num(); }
virtual uint64_t sum() const override { return stats_.sum(); }
virtual double Median() const override;
virtual double Percentile(double p) const override;
virtual double Average() const override;
Expand Down
1 change: 1 addition & 0 deletions monitoring/histogram_windowing.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class HistogramWindowingImpl : public Histogram
virtual uint64_t min() const override { return stats_.min(); }
virtual uint64_t max() const override { return stats_.max(); }
virtual uint64_t num() const override { return stats_.num(); }
virtual uint64_t sum() const override { return stats_.sum(); }
virtual double Median() const override;
virtual double Percentile(double p) const override;
virtual double Average() const override;
Expand Down
5 changes: 3 additions & 2 deletions tools/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
set(CORE_TOOLS
sst_dump.cc
ldb.cc)
ldb.cc
beezcli.cc)
foreach(src ${CORE_TOOLS})
get_filename_component(exename ${src} NAME_WE)
add_executable(${exename}${ARTIFACT_SUFFIX}
${src})
target_link_libraries(${exename}${ARTIFACT_SUFFIX} ${ROCKSDB_LIB})
target_link_libraries(${exename}${ARTIFACT_SUFFIX} ${ROCKSDB_LIB} readline)
list(APPEND core_tool_deps ${exename})
endforeach()

Expand Down
119 changes: 119 additions & 0 deletions tools/beezcli.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
// Copyright (C) 2023 Speedb Ltd. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <getopt.h>
#include <readline/history.h>
#include <readline/readline.h>
#include <signal.h>

#include <iostream>
#include <iterator>
#include <sstream>

#include "rocksdb/ldb_tool.h"

void SignalHandler(int sigint) {
std::cout << std::endl << "Ciao" << std::endl;
exit(0);
}
void ToArgv(std::string const& input, std::vector<std::string>& temp) {
std::istringstream buffer(input);
std::copy(std::istream_iterator<std::string>(buffer),
std::istream_iterator<std::string>(), std::back_inserter(temp));
}
int main(int argc, char** argv) {
signal(SIGINT, &SignalHandler);
ROCKSDB_NAMESPACE::LDBTool tool;
std::string prompt = "beezcli> ";
const char* const short_opts = "dis\0";
const option long_opts[] = {{"db", required_argument, 0, 'd'},
{"interactive", no_argument, nullptr, 'i'},
{"secondary_path", required_argument, 0, 's'},
{0, 0, 0, 0}};
int opt;
std::string db_path = "";
std::string secondary_path = "";
bool i = false;
bool d = false;
bool s [[maybe_unused]] = false;
opterr = 0;
opt = getopt_long(argc, argv, short_opts, long_opts, nullptr);
while (opt != -1) {
switch (opt) {
case 'd':
db_path = std::string(optarg);
std::cout << db_path << std::endl;
d = true;
break;
case 'i':
i = true;
break;
case 's':
secondary_path = std::string(optarg);
s = true;
break;
}
opt = getopt_long(argc, argv, short_opts, long_opts, nullptr);
}
char* line;
if (i && !d) {
std::cerr << "interactive flag provided without --db" << std::endl;
return EINVAL;
}
while (i && d && (line = readline(prompt.c_str())) && line) {
if (line[0] != '\0') add_history(line);
std::string input(line);
free(line);
line = nullptr;
if (input == "help") {
char** help = new char*[2];
help[0] = argv[0];
help[1] = const_cast<char*>("--help");
tool.Run(2, help, ROCKSDB_NAMESPACE::Options(),
ROCKSDB_NAMESPACE::LDBOptions(), nullptr, false);
continue;
}
if (input == "quit" || input == "exit") {
SignalHandler(0);
}
if (!input.empty()) {
if (!s) {
std::vector<std::string> vec;
ToArgv(std::string(argv[0]) + " " + input + " --db=" + db_path, vec);
std::vector<char*> cstrings{};
for (const auto& string : vec) {
cstrings.push_back(const_cast<char*>(string.c_str()));
}
tool.Run(cstrings.size(), cstrings.data(), ROCKSDB_NAMESPACE::Options(),
ROCKSDB_NAMESPACE::LDBOptions(), nullptr, false);
} else {
std::vector<std::string> vec;
ToArgv(std::string(argv[0]) + " " + input + " --db=" + db_path +
" --secondary_path=" + secondary_path,
vec);
std::vector<char*> cstrings{};
for (const auto& string : vec) {
cstrings.push_back(const_cast<char*>(string.c_str()));
}
tool.Run(cstrings.size(), cstrings.data(), ROCKSDB_NAMESPACE::Options(),
ROCKSDB_NAMESPACE::LDBOptions(), nullptr, false);
}
}
}
if (line == nullptr && i && d) {
SignalHandler(0);
}
tool.Run(argc, argv);
return 0;
}
Loading

0 comments on commit 384f7f4

Please sign in to comment.