<commit_before>require 'yaml'

module CouchbaseModelLogging

  class Logger

    attr_accessor :client, :prefix, :separator

    def initialize(client, prefix = nil, options = { })
      self.client    = client
      self.prefix    = prefix
      self.separator = options[:separator] || '[SEP]'
    end

    def key?(key)
      !get(key).nil?
    end

    def encode(hash)
      yaml = hash.to_yaml
      raise CouchbaseModelLogging::ReplacementError, "Found separator [#{separator}] in YAML string" if yaml.index(separator)
      yaml + separator
    end

    def add(key, hash = { })
      yaml     = encode hash
      pref_key = prefixed_key_for key
      begin
        client.append pref_key, yaml, :format => :plain
      rescue ::Couchbase::Error::NotStored
        begin
          client.add pref_key, yaml, :format => :plain
        rescue ::Couchbase::Error::KeyExists
          client.append pref_key, yaml, :format => :plain
        end
      end
    end

    def get(key)
      client.get(prefixed_key_for(key), :format => :plain)
    end

    def decode(str)
      return [] if str.nil? || str.empty?
      str.split(separator).map { |yaml| YAML.load yaml }
    end

    def all(key)
      decode get(key)
    end

    def delete(key)
      client.delete prefixed_key_for(key)
    end

    def prefixed_key_for(key)
      prefix.nil? ? key.to_s : "#{prefix}::#{key}"
    end

  end

end
<commit_msg>Guard for errors adding when we can append.<commit_after>require 'yaml'

module CouchbaseModelLogging

  class Logger

    attr_accessor :client, :prefix, :string_separator, :key_separator

    def initialize(client, prefix = nil, options = { })
      self.client    = client
      self.prefix    = prefix
      self.key_separator = ":"
      self.string_separator = options[:string_separator] || '[SEP]'
    end

    def key?(key)
      !get(key).nil?
    end

    def encode(hash)
      yaml = hash.to_yaml
      raise CouchbaseModelLogging::ReplacementError, "Found separator [#{string_separator}] in YAML string" if yaml.index(string_separator)
      yaml + string_separator
    end

    def add(key, hash = { })
      yaml     = encode hash
      pref_key = prefixed_key_for key
      begin
        client.append pref_key, yaml, :format => :plain
      rescue ::Couchbase::Error::NotStored
        begin
          client.add pref_key, yaml, :format => :plain
        rescue ::Couchbase::Error::KeyExists
          client.append pref_key, yaml, :format => :plain
        end
      end
    end

    def get(key)
      client.get(prefixed_key_for(key), :format => :plain)
    end

    def decode(str)
      return [] if str.nil? || str.empty?
      str.split(string_separator).map { |yaml| YAML.load yaml }
    end

    def all(key)
      decode get(key)
    end

    def delete(key)
      client.delete prefixed_key_for(key)
    end

    def prefixed_key_for(key)
      prefix.nil? ? key.to_s : "#{prefix}#{key_separator}#{key}"
    end

  end

end

{'fraction_non_alphanumeric': 0.10897435897435898, 'fraction_numerical': 0.0, 'mean_word_length': 2.4557109557109555, 'pattern_counts': {'":': 1, '<': 3, '<?xml version=': 0, '>': 11, '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': '2255504', 'n_tokens_mistral': 988, 'n_tokens_neox': 937, 'n_words': 233}