<commit_before># encoding: utf-8

class Myaso::TokyoCabinet::Rules < Myaso::Adapter
  include TokyoCabinet

  def find id
    rules.get(id)
  end

  def set id, rule
    rules.put(id, rule)
  end

  def delete id
    rules.delete(id)
  end

  def find_rule rule_set_id, suffix, prefix = ''
    rule_id = TDBQRY.new(rules).tap do |q|
      q.addcond('rule_set_id', TDBQRY::QCNUMEQ, rule_set_id)

      if suffix && !suffix.empty?
        q.addcond('suffix', TDBQRY::QCSTREQ, suffix)
      else
        q.addcond('suffix', TDBQRY::QCSTRRX | TDBQRY::QCNEGATE, '')
      end

      if prefix && !prefix.empty?
        q.addcond('prefix', TDBQRY::QCSTREQ, prefix)
      else
        q.addcond('prefix', TDBQRY::QCSTRRX | TDBQRY::QCNEGATE, '')
      end

      q.setlimit(1, 0)
    end.search.first

    rule_id && find(rule_id)
  end

  def has_suffix? suffix
    TDBQRY.new(rules).tap do |q|
      if suffix && !suffix.empty?
        q.addcond('suffix', TDBQRY::QCSTREQ, suffix)
      else
        q.addcond('suffix', TDBQRY::QCSTRRX | TDBQRY::QCNEGATE, '')
      end

      q.setlimit(1, 0)
    end.search.any?
  end

  def select_by_rule_set rule_set_id
    TDBQRY.new(rules).tap do |q|
      q.addcond('rule_set_id', TDBQRY::QCNUMEQ, rule_set_id)
    end.search.map { |id| find(id) }
  end

  def select_by_prefix prefix, rule_set_id = nil
    TDBQRY.new(rules).tap do |q|
      q.addcond('prefix', TDBQRY::QCSTREQ, prefix.empty? ? nil : prefix)
      if rule_set_id
        q.addcond('rule_set_id', TDBQRY::QCNUMEQ, rule_set_id)
      end
    end.search.map { |id| find(id) }
  end

  def select_by_suffix suffix, rule_set_id = nil
    TDBQRY.new(rules).tap do |q|
      q.addcond('suffix', TDBQRY::QCSTREQ, suffix.empty? ? nil : suffix)
      if rule_set_id
        q.addcond('rule_set_id', TDBQRY::QCNUMEQ, rule_set_id)
      end
    end.search.map { |id| find(id) }
  end

  protected
    def rules<commit_msg>Tender suffix/prefix handling<commit_after># encoding: utf-8

class Myaso::TokyoCabinet::Rules < Myaso::Adapter
  include TokyoCabinet

  def find id
    rules.get(id)
  end

  def set id, rule
    rules.put(id, rule)
  end

  def delete id
    rules.delete(id)
  end

  def find_rule rule_set_id, suffix, prefix = ''
    rule_id = TDBQRY.new(rules).tap do |q|
      q.addcond('rule_set_id', TDBQRY::QCNUMEQ, rule_set_id)

      if suffix && !suffix.empty?
        q.addcond('suffix', TDBQRY::QCSTREQ, suffix)
      else
        q.addcond('suffix', TDBQRY::QCSTRRX | TDBQRY::QCNEGATE, '')
      end

      if prefix && !prefix.empty?
        q.addcond('prefix', TDBQRY::QCSTREQ, prefix)
      else
        q.addcond('prefix', TDBQRY::QCSTRRX | TDBQRY::QCNEGATE, '')
      end

      q.setlimit(1, 0)
    end.search.first

    rule_id && find(rule_id)
  end

  def has_suffix? suffix
    TDBQRY.new(rules).tap do |q|
      if suffix && !suffix.empty?
        q.addcond('suffix', TDBQRY::QCSTREQ, suffix)
      else
        q.addcond('suffix', TDBQRY::QCSTRRX | TDBQRY::QCNEGATE, '')
      end

      q.setlimit(1, 0)
    end.search.any?
  end

  def select_by_rule_set rule_set_id
    TDBQRY.new(rules).tap do |q|
      q.addcond('rule_set_id', TDBQRY::QCNUMEQ, rule_set_id)
    end.search.map { |id| find(id) }
  end

  def select_by_prefix prefix, rule_set_id = nil
    TDBQRY.new(rules).tap do |q|
      if prefix && !prefix.empty?
        q.addcond('prefix', TDBQRY::QCSTREQ, prefix)
      else
        q.addcond('prefix', TDBQRY::QCSTRRX | TDBQRY::QCNEGATE, '')
      end

      if rule_set_id
        q.addcond('rule_set_id', TDBQRY::QCNUMEQ, rule_set_id)
      end
    end.search.map { |id| find(id) }
  end

  def select_by_suffix suffix, rule_set_id = nil
    TDBQRY.new(rules).tap do |q|
      if suffix && !suffix.empty?
        q.addcond('suffix', TDBQRY::QCSTREQ, suffix)
      else
        q.addcond('suffix', TDBQRY::QCSTRRX | TDBQRY::QCNEGATE, '')
      end

      if rule_set_id
        q.addcond('rule_set_id', TDBQRY::QCNUMEQ, rule_set_id)
      end
    end.search.map { |id| find(id) }
  end

  protected
    def rules
{'fraction_non_alphanumeric': 0.13209876543209875, 'fraction_numerical': 0.0024691358024691358, 'mean_word_length': 2.723345588235294, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, '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': '950114', 'n_tokens_mistral': 1738, 'n_tokens_neox': 1595, 'n_words': 309}