Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@
[submodule "deps/3rd/benchmark"]
path = deps/3rd/benchmark
url = https://github.com/google/benchmark
[submodule "deps/3rd/replxx"]
path = deps/3rd/replxx
url = https://github.com/AmokHuginnsson/replxx
10 changes: 9 additions & 1 deletion build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,15 @@ function do_init
mkdir -p build && \
cd build && \
${CMAKE_COMMAND_THIRD_PARTY} -DJSONCPP_WITH_TESTS=OFF -DJSONCPP_WITH_POST_BUILD_UNITTEST=OFF .. && \
${MAKE_COMMAND} && \
${MAKE_COMMAND} -j4 && \
${MAKE_COMMAND} install

# build replxx
cd ${TOPDIR}/deps/3rd/replxx && \
mkdir -p build && \
cd build && \
${CMAKE_COMMAND_THIRD_PARTY} .. -DCMAKE_BUILD_TYPE=Release -DREPLXX_BUILD_EXAMPLES=OFF -DREPLXX_BUILD_PACKAGE=OFF && \
${MAKE_COMMAND} -j4 && \
${MAKE_COMMAND} install

cd $current_dir
Expand Down
1 change: 1 addition & 0 deletions deps/3rd/replxx
Submodule replxx added at 1f149b
12 changes: 4 additions & 8 deletions src/obclient/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,10 @@ MESSAGE("Begin to build " obclient)
INCLUDE(readline)
MINIOB_FIND_READLINE()

IF (HAVE_READLINE)
TARGET_LINK_LIBRARIES(obclient ${READLINE_LIBRARY})
TARGET_INCLUDE_DIRECTORIES(obclient PRIVATE ${READLINE_INCLUDE_DIR})
ADD_DEFINITIONS(-DUSE_READLINE)
MESSAGE ("obclient use readline")
ELSE ()
MESSAGE ("readline is not found")
ENDIF()
find_package(Threads REQUIRED)
find_package(replxx REQUIRED)
TARGET_LINK_LIBRARIES(obclient replxx::replxx)
MESSAGE ("obclient use replxx")

FILE(GLOB_RECURSE ALL_SRC *.cpp)
FOREACH (F ${ALL_SRC})
Expand Down
85 changes: 46 additions & 39 deletions src/obclient/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,63 +29,59 @@ See the Mulan PSL v2 for more details. */

#include "common/defs.h"
#include "common/lang/string.h"

#ifdef USE_READLINE
#include "readline/history.h"
#include "readline/readline.h"
#endif
#include "replxx.hxx"

#define MAX_MEM_BUFFER_SIZE 8192
#define PORT_DEFAULT 6789

using namespace std;
using namespace common;

#ifdef USE_READLINE
const string HISTORY_FILE = string(getenv("HOME")) + "/.miniob.history";
time_t last_history_write_time = 0;
static replxx::Replxx rx;
const std::string REPLXX_HISTORY_FILE = "./.obclient.history";

char *my_readline(const char *prompt)
{
int size = history_length;
if (size == 0) {
read_history(HISTORY_FILE.c_str());
static bool is_first_call = true;
if (is_first_call) {
rx.history_load(REPLXX_HISTORY_FILE);
rx.install_window_change_handler();
is_first_call = false;
}

FILE *fp = fopen(HISTORY_FILE.c_str(), "a");
if (fp != nullptr) {
fclose(fp);
}
char const *cinput = nullptr;

try {
cinput = rx.input(prompt);
} catch (std::exception const &e) {
fprintf(stderr, "replxx input error: %s\n", e.what());
return nullptr;
}

char *line = readline(prompt);
if (line != nullptr && line[0] != 0) {
add_history(line);
if (time(NULL) - last_history_write_time > 5) {
write_history(HISTORY_FILE.c_str());
if (cinput == nullptr) {
return nullptr;
}

bool is_valid_input = false;
for (auto c = cinput; *c != '\0'; ++c) {
if (!isspace(*c)) {
is_valid_input = true;
break;
}
// append_history doesn't work on some readlines
// append_history(1, HISTORY_FILE.c_str());
}
return line;
}
#else // USE_READLINE
char *my_readline(const char *prompt)
{
char *buffer = (char *)malloc(MAX_MEM_BUFFER_SIZE);
if (nullptr == buffer) {
fprintf(stderr, "failed to alloc line buffer");
return nullptr;

if (is_valid_input) {
rx.history_add(cinput);
}
fprintf(stdout, "%s", prompt);
char *s = fgets(buffer, MAX_MEM_BUFFER_SIZE, stdin);
if (nullptr == s) {
fprintf(stderr, "failed to read message from console");
free(buffer);

char *line = strdup(cinput);
if (line == nullptr) {
fprintf(stderr, "Failed to dup input string from replxx\n");
return nullptr;
}
return buffer;

return line;
}
#endif // USE_READLINE

/* this function config a exit-cmd list, strncasecmp func truncate the command from terminal according to the number,
'strncasecmp("exit", cmd, 4)' means that obclient read command string from terminal, truncate it to 4 chars from
Expand Down Expand Up @@ -188,7 +184,9 @@ int main(int argc, char *argv[])

char send_buf[MAX_MEM_BUFFER_SIZE];

char *input_command = nullptr;
char *input_command = nullptr;
static time_t previous_history_save_time = 0;

while ((input_command = my_readline(prompt_str)) != nullptr) {
if (common::is_blank(input_command)) {
free(input_command);
Expand All @@ -199,9 +197,15 @@ int main(int argc, char *argv[])
if (is_exit_command(input_command)) {
free(input_command);
input_command = nullptr;
rx.history_save(REPLXX_HISTORY_FILE);
break;
}

if (time(NULL) - previous_history_save_time > 5) {
rx.history_save(REPLXX_HISTORY_FILE);
previous_history_save_time = time(NULL);
}

if ((send_bytes = write(sockfd, input_command, strlen(input_command) + 1)) == -1) { // TODO writen
fprintf(stderr, "send error: %d:%s \n", errno, strerror(errno));
exit(1);
Expand Down Expand Up @@ -243,5 +247,8 @@ int main(int argc, char *argv[])
}
close(sockfd);

rx.history_save(REPLXX_HISTORY_FILE);
printf("Command history saved to: %s\n", REPLXX_HISTORY_FILE.c_str());

return 0;
}
13 changes: 5 additions & 8 deletions src/oblsm/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,11 @@ ADD_EXECUTABLE(oblsm_cli ${CLIUTIL_SOURCES} client/ob_lsm_client.cpp)
INCLUDE(readline)
MINIOB_FIND_READLINE()

IF (HAVE_READLINE)
TARGET_LINK_LIBRARIES(oblsm_cli ${READLINE_LIBRARY})
TARGET_INCLUDE_DIRECTORIES(oblsm_cli PRIVATE ${READLINE_INCLUDE_DIR})
ADD_DEFINITIONS(-DUSE_READLINE)
MESSAGE ("oblsm_cli use readline")
ELSE ()
MESSAGE ("readline is not found")
ENDIF()
find_package(Threads REQUIRED)
find_package(replxx REQUIRED)
TARGET_LINK_LIBRARIES(oblsm_cli replxx::replxx)
MESSAGE ("oblsm_cli use replxx")

TARGET_LINK_LIBRARIES(oblsm_cli oblsm)


Expand Down
30 changes: 30 additions & 0 deletions src/oblsm/client/cliutil/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,40 @@ See the Mulan PSL v2 for more details. */
// Created by Ping Xu(haibarapink@gmail.com)
//
#include "oblsm/client/cliutil/parser.h"
#include "common/lang/string.h"
#include "common/log/log.h"
#include "common/sys/rc.h"

namespace oceanbase {
std::string my_readline(const std::string &prompt)
{
static bool is_first_call = true;
if (is_first_call) {
rx.history_load(REPLXX_HISTORY_FILE);
rx.install_window_change_handler();
is_first_call = false;
}

char const *cinput = nullptr;

try {
cinput = rx.input(prompt.c_str());
} catch (std::exception const &e) {
std::cerr << "replxx input error: " << e.what() << std::endl;
return "";
}

bool is_valid_input = !common::is_blank(cinput);

std::string result = cinput;

if (is_valid_input) {
rx.history_add(result);
}

return result;
}

void ObLsmCliCmdTokenizer::skip_blank_space()
{
while (!out_of_range() && std::isspace(command_[p_])) {
Expand Down
49 changes: 4 additions & 45 deletions src/oblsm/client/cliutil/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,56 +23,15 @@ See the Mulan PSL v2 for more details. */
#include "common/lang/string.h"
#include "common/lang/string_view.h"
#include "oblsm/client/cliutil/defs.h"

#ifdef USE_READLINE
#include "readline/history.h"
#include "readline/readline.h"
#endif
#include "replxx.hxx"

#define MAX_MEM_BUFFER_SIZE 8192
namespace oceanbase {

#ifdef USE_READLINE
inline const string HISTORY_FILE = string(getenv("HOME")) + "/.oblsm_cli.history";
inline time_t last_history_write_time = 0;

inline std::string my_readline(const string &prompt)
{
int size = history_length;
if (size == 0) {
read_history(HISTORY_FILE.c_str());

std::ofstream historyFile(HISTORY_FILE, std::ios::app);
if (historyFile.is_open()) {
historyFile.close();
}
}

char *line = readline(prompt.c_str());
std::string result;
if (line != nullptr && line[0] != 0) {
result = line;
add_history(line);
static replxx::Replxx rx;
inline const string REPLXX_HISTORY_FILE = "./.oblsm_cli.history";

if (std::time(nullptr) - last_history_write_time > 5) {
write_history(HISTORY_FILE.c_str());
}
}
free(line);
return result;
}
#else // USE_READLINE
inline std::string my_readline(const std::string &prompt)
{
std::cout << prompt;
std::string buffer;
if (std::getline(std::cin, buffer)) {
return buffer;
} else {
return "";
}
}
#endif // USE_READLINE
std::string my_readline(const string &prompt);

enum class TokenType
{
Expand Down
15 changes: 14 additions & 1 deletion src/oblsm/client/ob_lsm_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,19 @@ int main(int, char **)
{
print_sys_msg(startup_tips);
print_sys_msg("Enter the help command to view the usage of oblsm_cli");

static time_t previous_history_save_time = 0;

for (; !quit;) {
string command = my_readline(prompt);
string command = my_readline(prompt);

if (!command.empty()) {
if (time(nullptr) - previous_history_save_time > 5) {
rx.history_save(REPLXX_HISTORY_FILE);
previous_history_save_time = time(nullptr);
}
}

ObLsmCliCmdParser parser;
auto &&result = parser.result;
RC rc = parser.parse(command);
Expand Down Expand Up @@ -182,6 +193,8 @@ int main(int, char **)
delete lsm;
lsm = nullptr;
}
rx.history_save(REPLXX_HISTORY_FILE);
print_sys_msg("Command history saved to " + string(REPLXX_HISTORY_FILE));
print_sys_msg("bye.");
quit = true;
break;
Expand Down
15 changes: 5 additions & 10 deletions src/observer/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,11 @@ ADD_EXECUTABLE(observer ${MAIN_SRC})
TARGET_LINK_LIBRARIES(observer observer_static)

ADD_LIBRARY(observer_static STATIC ${LIB_SRC})
INCLUDE (readline)
MINIOB_FIND_READLINE()
IF (HAVE_READLINE)
TARGET_LINK_LIBRARIES(observer_static ${READLINE_LIBRARY})
TARGET_INCLUDE_DIRECTORIES(observer_static PRIVATE ${READLINE_INCLUDE_DIR})
ADD_DEFINITIONS(-DUSE_READLINE)
MESSAGE ("observer_static use readline ${READLINE_INCLUDE_DIR}")
ELSE ()
MESSAGE ("readline is not found")
ENDIF()

find_package(Threads REQUIRED)
find_package(replxx REQUIRED)
TARGET_LINK_LIBRARIES(observer_static replxx::replxx)
MESSAGE ("observer_static use replxx")

SET_TARGET_PROPERTIES(observer_static PROPERTIES OUTPUT_NAME observer)
TARGET_LINK_LIBRARIES(observer_static ${LIBRARIES} oblsm)
Expand Down
Loading