<commit_before>module Berkshelf::API
  class CacheBuilder
    module Worker
      class Base
        INTERVAL = 5

        include Celluloid
        include Berkshelf::API::Logging
        include Berkshelf::API::Mixin::Services

        attr_reader :options

        def initialize(options = {})
          @options = options
        end

        # @abstract
        #
        # @return [#to_s]
        def archive_name
          raise RuntimeError, "must be implemented"
        end

        # @abstract
        #
        # @param [RemoteCookbook] remote
        #
        # @return [Ridley::Chef::Cookbook::Metadata]
        def metadata(remote)
          raise RuntimeError, "must be implemented"
        end

        # @abstract
        #
        # @return [Array<RemoteCookbook>]
        #  The list of cookbooks this builder can find
        def cookbooks
          raise RuntimeError, "must be implemented"
        end

        def build
          loop do
            update_cache if stale?

            sleep INTERVAL
            clear_diff
          end
        end

        # @return [Array<RemoteCookbook>]
        def diff
          @diff ||= cache_manager.diff(cookbooks)
        end

        def update_cache
          created_cookbooks, deleted_cookbooks = diff
          created_cookbooks.collect { |remote| cache_manager.future(:add, remote.name, remote.version, metadata(remote)) }.map(&:value)
          deleted_cookbooks.each { |remote| cache_manager.remove(remote.name, remote.version) }
        end

        def stale?
          created_cookbooks, deleted_cookbooks = diff
          created_cookbooks.any? || deleted_cookbooks.any?
        end

        private

          def clear_diff<commit_msg>only build if we're not building<commit_after>module Berkshelf::API
  class CacheBuilder
    module Worker
      class Base
        INTERVAL = 5

        include Celluloid
        include Berkshelf::API::Logging
        include Berkshelf::API::Mixin::Services

        attr_reader :options

        def initialize(options = {})
          @options = options
        end

        # @abstract
        #
        # @return [#to_s]
        def archive_name
          raise RuntimeError, "must be implemented"
        end

        # @abstract
        #
        # @param [RemoteCookbook] remote
        #
        # @return [Ridley::Chef::Cookbook::Metadata]
        def metadata(remote)
          raise RuntimeError, "must be implemented"
        end

        # @abstract
        #
        # @return [Array<RemoteCookbook>]
        #  The list of cookbooks this builder can find
        def cookbooks
          raise RuntimeError, "must be implemented"
        end

        def build
          return if @building

          loop do
            @building = true
            update_cache if stale?

            sleep INTERVAL
            clear_diff
          end
        end

        # @return [Array<RemoteCookbook>]
        def diff
          @diff ||= cache_manager.diff(cookbooks)
        end

        def update_cache
          created_cookbooks, deleted_cookbooks = diff
          created_cookbooks.collect { |remote| cache_manager.future(:add, remote.name, remote.version, metadata(remote)) }.map(&:value)
          deleted_cookbooks.each { |remote| cache_manager.remove(remote.name, remote.version) }
        end

        def stale?
          created_cookbooks, deleted_cookbooks = diff
          created_cookbooks.any? || deleted_cookbooks.any?
        end

        private

          def clear_diff
{'fraction_non_alphanumeric': 0.07388462631429384, 'fraction_numerical': 0.0005683432793407218, 'mean_word_length': 1.7564604541895066, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '2637694', 'n_tokens_mistral': 957, 'n_tokens_neox': 902, 'n_words': 237}