<commit_before>#include "../ldb.h"

//
//
//
bool replace(string& str, const string& from, const string& to) {
  size_t start_pos = str.find(from);
  if (start_pos == string::npos) return false;
  str.replace(start_pos, from.length(), to);
  return true;
}

//
//
//
void ldb::put_value(string key, string value) {
  leveldb::WriteOptions writeOptions;
  leveldb::Status status;
  status = db->Put(writeOptions, key, value);
  if (!status.ok()) {
    cerr << status.ToString() << endl;
  }
}

//
//
//
void ldb::get_value(string key) {
  if (key == "") return;

  string value;
  leveldb::Status status = db->Get(leveldb::ReadOptions(), key, &value);

  if (!status.ok()) {
    cerr << "Not Found: [" << COLOR_BLUE << key << COLOR_NONE << "]" << endl;
  } else {
    if (json > 0) {
      value = JSON(value, json);
    }

    cout << value << endl;
  }
}

//
//
//
void ldb::del_value(string key) {
  leveldb::WriteOptions writeOptions;
  leveldb::Status status;

  status = db->Delete(leveldb::WriteOptions(), key);
  if (!status.ok()) {
    cerr << status.ToString() << endl;
  }
}

//
//
//
void ldb::find(string exp, int type) {
  cout << "\r\n";
  regex e(exp);

  leveldb::Iterator* itr = db->NewIterator(leveldb::ReadOptions());

  for (itr->Seek(key_start); itr->Valid(); itr->Next()) {
    leveldb::Slice key = itr->key();
    leveldb::Slice value = itr->value();

    string sKey = key.ToString();

    if (sKey == key_end) break;

    string sValue = value.ToString();
    string subject = type == 0 ? sKey : sValue;
    string output;
    smatch m;

    regex_search(subject, m, e);

    if (m.length()) {

      string s = subject.substr(m.position(), m.length());
      replace(subject, s, hi_start + s + hi_end);

      if (type == 1) { // fancy truncate value output

        int padding = hi_start.length() + hi_end.length() + 20;
        int substart = m.position() - padding;
        int matchlen = m.length() + padding * 2;
        int subend = matchlen;

        if (substart <= 0) substart = 0;
        if (subend >= subject.length()) subend = subject.length();

        string partial = subject.substr(substart, matchlen);
        output = sKey + ": ..." + partial + "...\r\n";
      } else {
        output = subject + "\r\n";
      }
      cout << output << endl;
    }
  }

  cout << "\r\n";
  delete itr;
}

//
//
//
void ldb::range(string prefix, bool surpress_output) {
  struct winsize term;
  ioctl(0, TIOCGWINSZ, &term);

  key_cache.clear();

  int count = 0;
  int maxWidth = 0;
  int maxColumns = 0;
  int padding = 2;
  bool initLine = false;

  leveldb::Iterator* itr = db->NewIterator(leveldb::ReadOptions());

  for (itr->Seek(key_start); itr->Valid(); itr->Next()) {
    count++;
    string sKey = itr->key().ToString();

    if (key_end != "") {
      size_t start_pos = sKey.find(key_end);
      if (start_pos != string::npos) break;
    }

    int len = sKey.length();
    if (len > maxWidth) {
      maxWidth = len;
    }
  }

  if (count == 0) return;

  count = 0;

  maxWidth += padding;
  maxColumns = term.ws_col / maxWidth;
  int colSize = maxWidth + hi_start.length() + hi_end.length();

  for (itr->Seek(key_start); itr->Valid(); itr->Next()) {

    if (!initLine) {
      cout << "\r";
      initLine = true;
    }

    string sKey = itr->key().ToString();

    if (key_end != "") {<commit_msg>handle narrow terminals<commit_after>#include "../ldb.h"

//
//
//
bool replace(string& str, const string& from, const string& to) {
  size_t start_pos = str.find(from);
  if (start_pos == string::npos) return false;
  str.replace(start_pos, from.length(), to);
  return true;
}

//
//
//
void ldb::put_value(string key, string value) {
  leveldb::WriteOptions writeOptions;
  leveldb::Status status;
  status = db->Put(writeOptions, key, value);
  if (!status.ok()) {
    cerr << status.ToString() << endl;
  }
}

//
//
//
void ldb::get_value(string key) {
  if (key == "") return;

  string value;
  leveldb::Status status = db->Get(leveldb::ReadOptions(), key, &value);

  if (!status.ok()) {
    cerr << "Not Found: [" << COLOR_BLUE << key << COLOR_NONE << "]" << endl;
  } else {
    if (json > 0) {
      value = JSON(value, json);
    }

    cout << value << endl;
  }
}

//
//
//
void ldb::del_value(string key) {
  leveldb::WriteOptions writeOptions;
  leveldb::Status status;

  status = db->Delete(leveldb::WriteOptions(), key);
  if (!status.ok()) {
    cerr << status.ToString() << endl;
  }
}

//
//
//
void ldb::find(string exp, int type) {
  cout << "\r\n";
  regex e(exp);

  leveldb::Iterator* itr = db->NewIterator(leveldb::ReadOptions());

  for (itr->Seek(key_start); itr->Valid(); itr->Next()) {
    leveldb::Slice key = itr->key();
    leveldb::Slice value = itr->value();

    string sKey = key.ToString();

    if (sKey == key_end) break;

    string sValue = value.ToString();
    string subject = type == 0 ? sKey : sValue;
    string output;
    smatch m;

    regex_search(subject, m, e);

    if (m.length()) {

      string s = subject.substr(m.position(), m.length());
      replace(subject, s, hi_start + s + hi_end);

      if (type == 1) { // fancy truncate value output

        int padding = hi_start.length() + hi_end.length() + 20;
        int substart = m.position() - padding;
        int matchlen = m.length() + padding * 2;
        int subend = matchlen;

        if (substart <= 0) substart = 0;
        if (subend >= subject.length()) subend = subject.length();

        string partial = subject.substr(substart, matchlen);
        output = sKey + ": ..." + partial + "...\r\n";
      } else {
        output = subject + "\r\n";
      }
      cout << output << endl;
    }
  }

  cout << "\r\n";
  delete itr;
}

//
//
//
void ldb::range(string prefix, bool surpress_output) {
  struct winsize term;
  ioctl(0, TIOCGWINSZ, &term);

  key_cache.clear();

  int count = 0;
  int maxWidth = 0;
  int maxColumns = 0;
  int padding = 2;
  bool initLine = false;

  leveldb::Iterator* itr = db->NewIterator(leveldb::ReadOptions());

  for (itr->Seek(key_start); itr->Valid(); itr->Next()) {
    count++;
    string sKey = itr->key().ToString();

    if (key_end != "") {
      size_t start_pos = sKey.find(key_end);
      if (start_pos != string::npos) break;
    }

    int len = sKey.length();
    if (len > maxWidth) {
      maxWidth = len;
    }
  }

  if (count == 0) return;

  count = 0;

  maxWidth += padding;
  maxWidth = std::min(maxWidth, (int) term.ws_col);
  maxColumns = term.ws_col / maxWidth;
  int colSize = maxWidth + hi_start.length() + hi_end.length();

  for (itr->Seek(key_start); itr->Valid(); itr->Next()) {

    if (!initLine) {
      cout << "\r";
      initLine = true;
    }

    string sKey = itr->key().ToString();

    if (key_end != "") {
{'fraction_non_alphanumeric': 0.18319029298609055, 'fraction_numerical': 0.004439183190292986, 'mean_word_length': 2.6634146341463416, 'pattern_counts': {'":': 2, '<': 73, '<?xml version=': 0, '>': 45, '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': '2433273', 'n_tokens_mistral': 2459, 'n_tokens_neox': 2367, 'n_words': 574}