-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbitcask-cli.cpp
107 lines (97 loc) · 3.16 KB
/
bitcask-cli.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <iostream>
#include <sstream>
#include "include/bitcask_handle.hpp"
int main(int argc, const char *argv[])
{
// Handle command line arguments
if (argc < 2)
{
std::cerr << "Usage: " << argv[0] << " <path_to_dir> [args]" << std::endl;
return EXIT_FAILURE;
}
// Print welcome message
// std::cout << "Welcome to \033[31;1mBitcask CLI\033[0m!" << '\n';
// std::cout << "Type 'help' for a list of commands." << '\n';
// std::cout << "Type 'quit' to exit." << '\n';
// Create a BitcaskHandle object
std::string db_path = argv[1];
bitcask::BitcaskHandle handle = bitcask::BitcaskHandle(db_path);
// Main loop
while (true)
{
std::string input;
std::cout << db_path << "\033[1m> \033[0m"; // Prompt
std::getline(std::cin, input);
std::istringstream iss(input);
// lowercase the input the command: first word of the input
std::string command;
iss >> command;
std::transform(command.begin(), command.end(), command.begin(), ::tolower);
if (command == "quit" || command == "q")
{
break;
}
else if (command == "help")
{
handle.help();
}
else
{
// Parse the input
std::string key;
std::string value;
if (command == "get" || command == "g")
{
iss >> key;
absl::StatusOr<std::string> result = handle.get(key);
if (result.ok())
{
std::cout << result.value() << '\n';
}
else
{
std::cerr << "Error: " << result.status().message() << std::endl;
}
}
else if (command == "set" || command == "s")
{
iss >> key;
iss >> value;
// std::cout << "key=" << key;
// std::cout << ", value=" << value << '\n';
absl::Status status = handle.set(key, value);
if (!status.ok())
{
std::cerr << "Error: " << status.message() << std::endl;
}
else
{
std::cout << "OK" << '\n';
}
}
else if (command == "del" || command == "d")
{
iss >> key;
// std::cout << "key=" << key << '\n';
absl::Status status = handle.del(key);
if (!status.ok())
{
std::cerr << "Error: " << status.message() << std::endl;
}
}
else if (command == "list" || command == "l" || command == "keys" || command == "k")
{
absl::Status status = handle.list();
if (!status.ok())
{
std::cerr << "Error: " << status.message() << std::endl;
}
}
else
{
std::cerr << "Unrecognized command: " << input << std::endl;
}
}
}
return EXIT_SUCCESS;
}