<commit_before># TODO: reinsert?
class Cassandra::Flow::Action::MatchTime < Cassandra::Flow::Action
  attr_reader :mapper, :action, :catalog, :flow,
    :source_time_field, :matched_time_field

  def initialize(mapper, source_time_field=nil, &action)
    @mapper = mapper
    @action = action
    @source_time_field = source_time_field
  end

  def setup!(flow)
    @flow    = flow
    @catalog = build_catalog

    @source_time_field ||= flow.source.config.subkey.first
    @matched_time_field = mapper.config.subkey.first

    mapper.config.dsl.after_insert do |match|
      key          = select(:key, match)
      matched_time = match[matched_time_field]

      lock key do
        # -0.001 instead of (slice: :before) because of uuid
        records = catalog.get key, start: { source_time: matched_time - 0.001 }
        records.select! do |it|
          not it[:matched_time] or
          it[:matched_time] <= match[matched_time_field]
        end

        records.each do |record|
          next_actions.propagate :remove, record[:action_result]
          record[:action_result] = action.call record[:action_data].dup, match
          next_actions.propagate :insert, record[:action_result]

          record[:matched_time] = matched_time
          catalog.insert record
        end
      end
    end

    mapper.config.dsl.after_remove do |match|
      key          = select(:key, match)
      matched_time = match[matched_time_field]

      lock key do
        records = catalog.get key, start: { source_time: matched_time }
        records.select! {|it| it[:matched_time] == match[matched_time_field] }

        records.each do |record|
          catalog.remove record
          next_actions.propagate :remove, record[:action_result]
          result = match_time :insert, key, record[:action_data]
          next_actions.propagate :insert, result
        end
      end
    end
  end

  def propagate(type, data)
    key = select(:key, data)

    lock key do
      match_time type, key, data
    end
  end

  private

  def match_time(type, key, data)
    source_time = data[source_time_field]
    matched = mapper.one key,
                         reversed: true,
                         start: { matched_time_field => source_time, slice: :after }

    matched_time = matched[matched_time_field] if matched
    result       = action.call data.dup, matched
    subkey       = { source_time: source_time, matched_time: matched_time }
<commit_msg>Raise error if time is missing in match_time action<commit_after># TODO: reinsert?
class Cassandra::Flow::Action::MatchTime < Cassandra::Flow::Action
  attr_reader :mapper, :action, :catalog, :flow,
    :source_time_field, :matched_time_field

  def initialize(mapper, source_time_field=nil, &action)
    @mapper = mapper
    @action = action
    @source_time_field = source_time_field
  end

  def setup!(flow)
    @flow    = flow
    @catalog = build_catalog

    @source_time_field ||= flow.source.config.subkey.first
    @matched_time_field = mapper.config.subkey.first

    mapper.config.dsl.after_insert do |match|
      key          = select(:key, match)
      matched_time = match[matched_time_field]

      lock key do
        # -0.001 instead of (slice: :before) because of uuid
        records = catalog.get key, start: { source_time: matched_time - 0.001 }
        records.select! do |it|
          not it[:matched_time] or
          it[:matched_time] <= match[matched_time_field]
        end

        records.each do |record|
          next_actions.propagate :remove, record[:action_result]
          record[:action_result] = action.call record[:action_data].dup, match
          next_actions.propagate :insert, record[:action_result]

          record[:matched_time] = matched_time
          catalog.insert record
        end
      end
    end

    mapper.config.dsl.after_remove do |match|
      key          = select(:key, match)
      matched_time = match[matched_time_field]

      lock key do
        records = catalog.get key, start: { source_time: matched_time }
        records.select! {|it| it[:matched_time] == match[matched_time_field] }

        records.each do |record|
          catalog.remove record
          next_actions.propagate :remove, record[:action_result]
          result = match_time :insert, key, record[:action_data]
          next_actions.propagate :insert, result
        end
      end
    end
  end

  def propagate(type, data)
    key = select(:key, data)

    lock key do
      match_time type, key, data
    end
  end

  private

  def match_time(type, key, data)
    source_time = data[source_time_field]

    unless source_time
      raise ArgumentError, "missing :#{source_time_field} in #{data.inspect}"
    end

    matched = mapper.one key,
                         reversed: true,
                         start: { matched_time_field => source_time, slice: :after }

    matched_time = matched[matched_time_field] if matched
    result       = action.call data.dup, matched
    subkey       = { source_time: source_time, matched_time: matched_time }

{'fraction_non_alphanumeric': 0.09266943291839558, 'fraction_numerical': 0.0031614305473226635, 'mean_word_length': 2.59772565742715, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, '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': '4110370', 'n_tokens_mistral': 1544, 'n_tokens_neox': 1488, 'n_words': 367}