<commit_before>require 'json'

module Pakyow
  module UI
    # Manages mutations.
    #
    # This is the default registry in production systems and is required in
    # deployments with more than one app instance.
    #
    # @api private
    class RedisMutationRegistry
      include Singleton

      def initialize
      end

      def register(scope, mutation)
        Pakyow::Realtime.redis.sadd(key(scope: scope, socket_key: mutation[:socket_key]), mutation.to_json)
      end

      def mutations(scope)
        mutations = []

        keys(key(scope: scope)) do |key|
          Pakyow::Realtime.redis.smembers(key).each do |m|
            mutations << Hash.strhash(JSON.parse(m))
          end
        end

        mutations
      end

      def unregister(socket_key)
        keys(key(socket_key: socket_key)) do |key|
          Pakyow::Realtime.redis.del(key)
        end
      end

      private

      def key(scope: nil, socket_key: nil)
        if socket_key.nil?
          base = "*:"
        else
          base = "#{socket_key}:"
        end

        if scope.nil?
          "#{base}*"
        else
          "#{base}pui-mutation-#{scope}"
        end
      end

      def keys(match)
        cursor = 0

        loop do
          cursor, keys = Pakyow::Realtime.redis.scan(cursor, match: match)

          keys.each do |key|
            yield key
          end

          break if cursor == '0'
        end
      end
    end<commit_msg>Better key finding in redis mutation store<commit_after>require 'json'

module Pakyow
  module UI
    # Manages mutations.
    #
    # This is the default registry in production systems and is required in
    # deployments with more than one app instance.
    #
    # @api private
    class RedisMutationRegistry
      include Singleton

      def initialize
      end

      def register(scope, mutation)
        Pakyow::Realtime.redis.sadd(key(scope: scope, socket_key: mutation[:socket_key]), mutation.to_json)
      end

      def mutations(scope)
        mutations = []

        keys(key(scope: scope)) do |key|
          Pakyow::Realtime.redis.smembers(key).each do |m|
            mutations << Hash.strhash(JSON.parse(m))
          end
        end

        mutations
      end

      def unregister(socket_key)
        keys(key(socket_key: socket_key)) do |key|
          Pakyow::Realtime.redis.del(key)
        end
      end

      private

      def key(scope: nil, socket_key: nil)
        if socket_key.nil?
          base = "*:"
        else
          base = "#{socket_key}:"
        end

        if scope.nil?
          "#{base}*"
        else
          "#{base}pui-mutation-#{scope}"
        end
      end

      def keys(match)
        Pakyow::Realtime.redis.sscan_each(match).to_a
      end
    end
{'fraction_non_alphanumeric': 0.09284682080924855, 'fraction_numerical': 0.000722543352601156, 'mean_word_length': 1.7801204819277108, 'pattern_counts': {'":': 0, '<': 7, '<?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': '4553894', 'n_tokens_mistral': 861, 'n_tokens_neox': 830, 'n_words': 214}