天天看點

leveldb kv資料庫可視化操作小工具(增删查改)

 leveldb産生的檔案無法直覺檢視,寫了以下簡單的工具使用指令行對leveldb進行直覺的檢視,包含簡單的功能:增删查改,具體用法可看--help

#include "leveldb/db.h"

#include <iostream>
#include <unistd.h>
#include <getopt.h>
#include <string>

using namespace std;
using namespace leveldb;

enum EDB_OPT
{
  PUT = 0,
  GET,
  DEL,
  ITE
};

void printUsage()
{

  printf("【usage】:\n");

  printf("-n --dbname <dbname> 資料庫名稱\n");
  printf("-p --put 插入\n");
  printf("-g --get 查詢\n");
  printf("-d --delete 删除\n");
  printf("-i --iterator 周遊所有kv\n");
  printf("-k --key <key>\n");
  printf("-v --value <value>\n");
  printf("-h --help \n");
}

void db_put(string dbName, string key, string value)
{
  leveldb::DB *db;
  leveldb::Options options;
  options.create_if_missing = true;
  leveldb::Status status = leveldb::DB::Open(options, dbName, &db);
  if (!status.ok())
  {
    cout << "打開資料失敗:dbName:" << dbName << endl;
    return;
  }
  status = db->Put(WriteOptions(), key, value);

  if (!status.ok())
  {
    cout << "put操作失敗" << endl;
  }
  else
  {
    cout << "put操作成功" << endl;
  }
  delete db;
}

void db_get(string dbName, string key)
{
  leveldb::DB *db;
  leveldb::Options options;
  options.create_if_missing = true;
  leveldb::Status status = leveldb::DB::Open(options, dbName, &db);
  if (!status.ok())
  {
    cout << "打開資料失敗:dbName:" << dbName << endl;
    return;
  }
  string res;
  status = db->Get(ReadOptions(), dbName, &res);

  if (!status.ok())
  {
    cout << "get操作失敗" << endl;
  }
  else
  {
    cout << "get操作成功,查詢結果:" << res << endl;
  }
  delete db;
}

void db_del(string dbName, string key)
{
  leveldb::DB *db;
  leveldb::Options options;
  options.create_if_missing = true;
  leveldb::Status status = leveldb::DB::Open(options, dbName, &db);
  if (!status.ok())
  {
    cout << "打開資料失敗:dbName:" << dbName << endl;
    return;
  }
  status = db->Delete(WriteOptions(), key);

  if (!status.ok())
  {
    cout << "del操作失敗" << endl;
  }
  else
  {
    cout << "del操作成功" << endl;
  }
  delete db;
}

void db_iterator(string dbName)
{
  leveldb::DB *db;
  leveldb::Options options;
  leveldb::Status status = leveldb::DB::Open(options, dbName, &db);
  if (!status.ok())
  {
    cout << "打開資料失敗:dbName:" << dbName << endl;
    return;
  }
  leveldb::Iterator *it = db->NewIterator(leveldb::ReadOptions());
  for (it->SeekToFirst(); it->Valid(); it->Next())
  {
    cout << "查找到kv對! " << it->key().ToString() << ":" << it->value().ToString() << endl;
  }
}

int main(int argc, char **argv)
{
  int opt;
  int digit_optind = 0;
  int option_index = 0;
  char *strings = "a::b:c:d";
  static struct option long_options[] = {
      {"dbname", required_argument, NULL, 'n'},
      {"put", no_argument, NULL, 'p'},
      {"get", no_argument, NULL, 'g'},
      {"delete", no_argument, NULL, 'd'},
      {"iterator", no_argument, NULL, 'i'},
      {"key", required_argument, NULL, 'k'},
      {"value", required_argument, NULL, 'v'},
      {"help", no_argument, NULL, 'h'},
      {NULL, 0, NULL, 0},
  };

  string dbName;
  EDB_OPT opcode;
  string key;
  string value;

  while ((opt = getopt_long_only(argc, argv, strings, long_options, &option_index)) != -1)
  {
    if (opt == 'n')
    {
      dbName = optarg;
    }
    if (opt == 'k')
    {
      key = optarg;
    }
    if (opt == 'v')
    {
      value = optarg;
    }
    if (opt == 'g')
    {
      opcode = GET;
    }
    if (opt == 'p')
    {
      opcode = PUT;
    }
    if (opt == 'd')
    {
      opcode = DEL;
    }
    if (opt == 'i')
    {
      opcode = ITE;
    }

    if (opt == 'h')
    {
      printUsage();
      exit(0);
    }
  }

  cout << "dbName:" << dbName << endl;
  cout << "opcode(0-PUT,1-GET,2-DEL,3-ITE):" << opcode << endl;
  cout << "key:" << key << endl;
  cout << "value:" << value << endl;

  if (opcode == PUT)
  {
    db_put(dbName, key, value);
  }
  else if (opcode == GET)
  {
    db_get(dbName, key);
  }
  else if (opcode == DEL)
  {
    db_del(dbName, key);
  }
  else if (opcode == ITE)
  {
    db_iterator(dbName);
  }
  else
  {
    printUsage();
  }

  return 0;
}