Browse Source

+cmd:print

master
Erik Zscheile 2 years ago
parent
commit
e1472c84bf
4 changed files with 35 additions and 1 deletions
  1. +4
    -0
      args.cxx
  2. +1
    -1
      args.hpp
  3. +28
    -0
      main.cxx
  4. +2
    -0
      maint.cxx

+ 4
- 0
args.cxx View File

@@ -73,6 +73,7 @@ bool args_t::parse(const int argc, char *argv[]) {
case 'r': cmd = RM ; break;
case 'v': cmd = MV ; break;
case 'm': cmd = MATCH; break;
case 'p': cmd = PRINT; break;
default:
fprintf(stderr, "zstreedb2: invalid command '%s'\n", argv[i]);
return false;
@@ -89,6 +90,7 @@ bool args_t::flag_use_ro() const noexcept {
case GET:
case LS:
case MATCH:
case PRINT:
return true;
default:
return false;
@@ -115,6 +117,8 @@ void help() {

puts("\nrecursive node commands:\n"
" l list\n"
" p print [ARG = BEFORE_KEY BEFORE_VALUE AFTER_VALUE]\n"
" --> each line: $1$key$2$value$3\n"
" r recursive remove\n"
" v move [ARGS = FROM TO]");



+ 1
- 1
args.hpp View File

@@ -12,7 +12,7 @@ namespace ztdb {

struct args_t final {
rocksdb::CompressionType compress;
enum { UNKNOWN, GET, SET, LS, MV, RM, MATCH } cmd;
enum { UNKNOWN, GET, SET, LS, MV, RM, MATCH, PRINT } cmd;
//std::string sel_ns;
std::string sel_node;
std::vector<std::string> cmd_args;


+ 28
- 0
main.cxx View File

@@ -43,6 +43,7 @@ int main(const int argc, char *argv[]) {
opts.allow_mmap_reads = true;
opts.allow_mmap_writes = true;
opts.compression = args.compress;
opts.bottommost_compression = args.compress;
opts.create_if_missing = true;
st = args.flag_use_ro()
? rocksdb::DB::OpenForReadOnly(opts, ".", &db)
@@ -90,6 +91,33 @@ int main(const int argc, char *argv[]) {
}
break;

AC(PRINT)
{
if(args.cmd_args.size() < 3)
errmsg(1, "invalid invocation to 'zstreedb2 p BFK BFV AFV'");

const bool sall = (args.sel_node == "*");
const string xpar = args.sel_node + '.';
const char *bfk = args.cmd_args[0].c_str();
const char *bfv = args.cmd_args[1].c_str();
const char *afv = args.cmd_args[2].c_str();
const auto it = db->NewIterator({});
if(sall) it->SeekToFirst();
else it->Seek(xpar);
for(; it->Valid(); it->Next()) {
auto tmp = it->key();
if(!sall) {
if(!tmp.starts_with(xpar)) break;
tmp.remove_prefix(xpar.size());
}
auto val = it->value();
printf("%s%.*s%s%.*s%s\n", bfk, static_cast<int>(tmp.size()), tmp.data(),
bfv, static_cast<int>(val.size()), val.data(), afv);
}
delete it;
}
break;

AC(MV)
if(!args.cmd_args.empty() && args.sel_node != args.cmd_args.front()) {
const auto it = db->NewIterator({});


+ 2
- 0
maint.cxx View File

@@ -50,6 +50,8 @@ int main(const int argc, char *argv[]) {
return 0;
}

opts.bottommost_compression = opts.compression;

std::unique_ptr<rocksdb::DB> db;
{
rocksdb::DB *dbptr;


Loading…
Cancel
Save