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;
}