<commit_before>/*
 * "Copyright [2016] qihoo"
 */
#include <string>
#include <vector>
#include <iostream>
#include <algorithm>

#include "libzp/include/zp_cluster.h"
#include "linenoise/linenoise.h"
#include "zp_manager_help.h"

void SplitByBlank(const std::string& old_line,
    std::vector<std::string>& line_args) {
  std::string line = old_line;
  line += " ";
  std::string unparse = line;
  std::string::size_type pos_start;
  std::string::size_type pos_end;
  pos_start = unparse.find_first_not_of(" ");
  while (pos_start != std::string::npos) {
    pos_end = unparse.find_first_of(" ", pos_start);
    line_args.push_back(unparse.substr(pos_start, pos_end - pos_start));
    unparse = unparse.substr(pos_end);
    pos_start = unparse.find_first_not_of(" ");
  }
}

typedef struct {
  std::string name;
  std::string params;
  int params_num;
  std::string info;
} CommandEntry;


static std::vector<CommandEntry> commandEntries;
static int helpEntriesLen;

static void cliInitHelp(void) {
  int commands_num = sizeof(commandHelp)/sizeof(struct CommandHelp);
  int i, len, pos = 0;
  CommandEntry tmp;
  for (int i = 0; i < commands_num; i++) {
    tmp.name = std::string(commandHelp[i].name);
    tmp.params = std::string(commandHelp[i].params);
    tmp.params_num = commandHelp[i].params_num;
    tmp.info = std::string(commandHelp[i].summary);
    commandEntries.push_back(tmp);
  }
}

// completion example
void completion(const char *buf, linenoiseCompletions *lc) {
  size_t start_pos = 0;
  size_t match_len = 0;
  std::string tmp;

  for (size_t i = 0; i < commandEntries.size(); i++) {
    match_len = strlen(buf);
    if (strncasecmp(buf, commandEntries[i].name.c_str(), match_len) == 0) {
      tmp = std::string();
      tmp = commandEntries[i].name;
      linenoiseAddCompletion(lc, tmp.c_str());
    }
  }
}

// hints example
char *hints(const char *buf, int *color, int *bold) {
  std::string buf_str = std::string(buf);
  std::vector<std::string> buf_args;
  SplitByBlank(buf_str, buf_args);
  size_t buf_len = strlen(buf);
  if (buf_len == 0) {
    return NULL;
  }
  int endspace = buf_len && isspace(buf[buf_len-1]);
  for (size_t i = 0; i < commandEntries.size(); i++) {
    size_t match_len = std::max(strlen(commandEntries[i].name.c_str()),
        strlen(buf_args[0].c_str()));
    if (strncasecmp(buf_args[0].c_str(),
          commandEntries[i].name.c_str(), match_len) == 0) {
      *color = 90;
      *bold = 0;
      char* hint = const_cast<char *>(commandEntries[i].params.c_str());
      int to_move = buf_args.size() - 1;
      while (strlen(hint) && to_move > 0) {
        if (hint[0] == ' ') {
          to_move--;
        }
        hint = hint + 1;
      }
      if (!endspace) {
        std::string new_hint = std::string(" ") + std::string(hint);
        hint = const_cast<char *>(new_hint.c_str());
      }
      return strlen(hint)? hint:NULL;
    }
  }
  return NULL;
}


void StartRepl(libzp::Cluster* cluster) {
  char *line;
  linenoiseSetMultiLine(1);
  linenoiseSetCompletionCallback(completion);
  linenoiseSetHintsCallback(hints);
  linenoiseHistoryLoad("history.txt"); /* Load the history at startup */

  libzp::Status s;
  while ((line = linenoise("zp >> ")) != NULL) {
    linenoiseHistoryAdd(line); /* Add to the history. */
    linenoiseHistorySave("history.txt"); /* Save the history on disk. */
    /* Do something with the string. */
    std::string info = line;
    std::vector<std::string> line_args;
    SplitByBlank(info, line_args);

    if (!strncasecmp(line, "CREATE ", 7)) {
      std::string table_name = line_args[1];
      int partition_num = atoi(line_args[2].c_str());
      s = cluster->CreateTable(table_name, partition_num);
      std::cout << s.ToString() << std::endl;
      std::cout << "repull table "<< table_name << std::endl;
      s = cluster->Pull(table_name);

    } else if (!strncasecmp(line, "PULL ", 5)) {
      if (line_args.size() != 2) {
        std::cout << "arg num wrong" << std::endl;
        continue;
      }
      std::string table_name = line_args[1];
      s = cluster->Pull(table_name);
      std::cout << s.ToString() << std::endl;
      std::cout << "current table info:" << std::endl;
      cluster->DebugDumpPartition(table_name);

    } else if (!strncasecmp(line, "DUMP ", 5)) {
      if (line_args.size() != 2) {
        std::cout << "arg num wrong" << std::endl;
        continue;
      }
      std::string table_name = line_args[1];
      cluster->DebugDumpPartition(table_name);

    } else if (!strncasecmp(line, "LOCATE ", 5)) {
      if (line_args.size() != 3) {
        std::cout << "arg num wrong" << std::endl;
        continue;
      }
      int partition_id = cluster->LocateKey(line_args[1], line_args[2]);
      if (partition_id >= 0) {
        cluster->DebugDumpPartition(line_args[1], partition_id);
      } else {
        std::cout << "doe not exist in local table" << std::endl;
      }

    } else if (!strncasecmp(line, "SET ", 4)) {
      if (line_args.size() != 4
          && line_args.size() != 5) {
        std::cout << "arg num wrong" << std::endl;
        continue;
      }
      std::string table_name = line_args[1];
      std::string key = line_args[2];
      std::string value = line_args[3];
      int ttl = -1;
      if (line_args.size() == 5) {
        char * end;
        int ttl = std::strtol(line_args[4].c_str(), &end, 10);
        if (*end != 0) {
          std::cout << "ttl must be a integer" << std::endl;
          continue;
        }
      }
      s = cluster->Set(table_name, key, value, ttl);
      std::cout << s.ToString() << std::endl;

    } else if (!strncasecmp(line, "SETMASTER ", 10)) {
      if (line_args.size() != 5) {<commit_msg>use NULL instead of unused pointer<commit_after>/*
 * "Copyright [2016] qihoo"
 */
#include <string>
#include <vector>
#include <iostream>
#include <algorithm>

#include "libzp/include/zp_cluster.h"
#include "linenoise/linenoise.h"
#include "zp_manager_help.h"

void SplitByBlank(const std::string& old_line,
    std::vector<std::string>& line_args) {
  std::string line = old_line;
  line += " ";
  std::string unparse = line;
  std::string::size_type pos_start;
  std::string::size_type pos_end;
  pos_start = unparse.find_first_not_of(" ");
  while (pos_start != std::string::npos) {
    pos_end = unparse.find_first_of(" ", pos_start);
    line_args.push_back(unparse.substr(pos_start, pos_end - pos_start));
    unparse = unparse.substr(pos_end);
    pos_start = unparse.find_first_not_of(" ");
  }
}

typedef struct {
  std::string name;
  std::string params;
  int params_num;
  std::string info;
} CommandEntry;


static std::vector<CommandEntry> commandEntries;
static int helpEntriesLen;

static void cliInitHelp(void) {
  int commands_num = sizeof(commandHelp)/sizeof(struct CommandHelp);
  int i, len, pos = 0;
  CommandEntry tmp;
  for (int i = 0; i < commands_num; i++) {
    tmp.name = std::string(commandHelp[i].name);
    tmp.params = std::string(commandHelp[i].params);
    tmp.params_num = commandHelp[i].params_num;
    tmp.info = std::string(commandHelp[i].summary);
    commandEntries.push_back(tmp);
  }
}

// completion example
void completion(const char *buf, linenoiseCompletions *lc) {
  size_t start_pos = 0;
  size_t match_len = 0;
  std::string tmp;

  for (size_t i = 0; i < commandEntries.size(); i++) {
    match_len = strlen(buf);
    if (strncasecmp(buf, commandEntries[i].name.c_str(), match_len) == 0) {
      tmp = std::string();
      tmp = commandEntries[i].name;
      linenoiseAddCompletion(lc, tmp.c_str());
    }
  }
}

// hints example
char *hints(const char *buf, int *color, int *bold) {
  std::string buf_str = std::string(buf);
  std::vector<std::string> buf_args;
  SplitByBlank(buf_str, buf_args);
  size_t buf_len = strlen(buf);
  if (buf_len == 0) {
    return NULL;
  }
  int endspace = buf_len && isspace(buf[buf_len-1]);
  for (size_t i = 0; i < commandEntries.size(); i++) {
    size_t match_len = std::max(strlen(commandEntries[i].name.c_str()),
        strlen(buf_args[0].c_str()));
    if (strncasecmp(buf_args[0].c_str(),
          commandEntries[i].name.c_str(), match_len) == 0) {
      *color = 90;
      *bold = 0;
      char* hint = const_cast<char *>(commandEntries[i].params.c_str());
      int to_move = buf_args.size() - 1;
      while (strlen(hint) && to_move > 0) {
        if (hint[0] == ' ') {
          to_move--;
        }
        hint = hint + 1;
      }
      if (!endspace) {
        std::string new_hint = std::string(" ") + std::string(hint);
        hint = const_cast<char *>(new_hint.c_str());
      }
      return strlen(hint)? hint:NULL;
    }
  }
  return NULL;
}


void StartRepl(libzp::Cluster* cluster) {
  char *line;
  linenoiseSetMultiLine(1);
  linenoiseSetCompletionCallback(completion);
  linenoiseSetHintsCallback(hints);
  linenoiseHistoryLoad("history.txt"); /* Load the history at startup */

  libzp::Status s;
  while ((line = linenoise("zp >> ")) != NULL) {
    linenoiseHistoryAdd(line); /* Add to the history. */
    linenoiseHistorySave("history.txt"); /* Save the history on disk. */
    /* Do something with the string. */
    std::string info = line;
    std::vector<std::string> line_args;
    SplitByBlank(info, line_args);

    if (!strncasecmp(line, "CREATE ", 7)) {
      std::string table_name = line_args[1];
      int partition_num = atoi(line_args[2].c_str());
      s = cluster->CreateTable(table_name, partition_num);
      std::cout << s.ToString() << std::endl;
      std::cout << "repull table "<< table_name << std::endl;
      s = cluster->Pull(table_name);

    } else if (!strncasecmp(line, "PULL ", 5)) {
      if (line_args.size() != 2) {
        std::cout << "arg num wrong" << std::endl;
        continue;
      }
      std::string table_name = line_args[1];
      s = cluster->Pull(table_name);
      std::cout << s.ToString() << std::endl;
      std::cout << "current table info:" << std::endl;
      cluster->DebugDumpPartition(table_name);

    } else if (!strncasecmp(line, "DUMP ", 5)) {
      if (line_args.size() != 2) {
        std::cout << "arg num wrong" << std::endl;
        continue;
      }
      std::string table_name = line_args[1];
      cluster->DebugDumpPartition(table_name);

    } else if (!strncasecmp(line, "LOCATE ", 5)) {
      if (line_args.size() != 3) {
        std::cout << "arg num wrong" << std::endl;
        continue;
      }
      int partition_id = cluster->LocateKey(line_args[1], line_args[2]);
      if (partition_id >= 0) {
        cluster->DebugDumpPartition(line_args[1], partition_id);
      } else {
        std::cout << "doe not exist in local table" << std::endl;
      }

    } else if (!strncasecmp(line, "SET ", 4)) {
      if (line_args.size() != 4
          && line_args.size() != 5) {
        std::cout << "arg num wrong" << std::endl;
        continue;
      }
      std::string table_name = line_args[1];
      std::string key = line_args[2];
      std::string value = line_args[3];
      int ttl = -1;
      if (line_args.size() == 5) {
        int ttl = std::strtol(line_args[4].c_str(), NULL, 10);
        if (*end != 0) {
          std::cout << "ttl must be a integer" << std::endl;
          continue;
        }
      }
      s = cluster->Set(table_name, key, value, ttl);
      std::cout << s.ToString() << std::endl;

    } else if (!strncasecmp(line, "SETMASTER ", 10)) {
      if (line_args.size() != 5) {
{'fraction_non_alphanumeric': 0.1655408369155189, 'fraction_numerical': 0.009474515308360382, 'mean_word_length': 3.105149441843716, 'pattern_counts': {'":': 0, '<': 121, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3179788', 'n_tokens_mistral': 4081, 'n_tokens_neox': 3972, 'n_words': 849}